1 /* $Id: mac.c,v 1.20 2003/01/08 22:46:12 ben Exp $ */
3 * Copyright (c) 1999 Ben Harris
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * mac.c -- miscellaneous Mac-specific routines
32 #include <Quickdraw.h>
34 #include <MacWindows.h>
37 #include <Appearance.h>
38 #include <CodeFragments.h>
44 #include <Resources.h>
46 #include <TextCommon.h>
47 #include <ToolUtils.h>
48 #include <UnicodeConverter.h>
53 #include <stdlib.h> /* putty.h needs size_t */
54 #include <stdio.h> /* for vsprintf */
56 #define PUTTY_DO_GLOBALS
68 struct mac_gestalts mac_gestalts;
70 static void mac_startup(void);
71 static void mac_eventloop(void);
72 #pragma noreturn (mac_eventloop)
73 static void mac_event(EventRecord *);
74 static void mac_contentclick(WindowPtr, EventRecord *);
75 static void mac_growwindow(WindowPtr, EventRecord *);
76 static void mac_activatewindow(WindowPtr, EventRecord *);
77 static void mac_activateabout(WindowPtr, EventRecord *);
78 static void mac_updatewindow(WindowPtr);
79 static void mac_updatelicence(WindowPtr);
80 static void mac_keypress(EventRecord *);
81 static int mac_windowtype(WindowPtr);
82 static void mac_menucommand(long);
83 static void mac_openabout(void);
84 static void mac_openlicence(void);
85 static void mac_adjustcursor(RgnHandle);
86 static void mac_adjustmenus(void);
87 static void mac_closewindow(WindowPtr);
88 static void mac_zoomwindow(WindowPtr, short);
89 static void mac_shutdown(void);
90 #pragma noreturn (cleanup_exit)
97 struct mac_windows windows;
99 int main (int argc, char **argv) {
105 #pragma noreturn (main)
107 static void mac_startup(void) {
111 /* Init Memory Manager */
114 InitGraf(&qd.thePort);
115 /* Init Font Manager */
117 /* Init Window Manager */
119 /* Init Menu Manager */
123 /* Init Dialog Manager */
127 /* Get base system version (only used if there's no better selector) */
128 if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
129 (mac_gestalts.sysvers &= 0xffff) < 0x700)
130 fatalbox("PuTTY requires System 7 or newer");
131 /* Find out if we've got Color Quickdraw */
132 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
133 mac_gestalts.qdvers = gestaltOriginalQD;
134 /* ... and the Appearance Manager? */
135 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
136 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
137 mac_gestalts.apprvers = 0x0100;
139 mac_gestalts.apprvers = 0;
140 #if TARGET_RT_MAC_CFM
141 /* Paranoia: Did we manage to pull in AppearanceLib? */
142 if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
143 mac_gestalts.apprvers = 0;
146 mac_gestalts.cntlattr = 0;
147 mac_gestalts.windattr = 0;
149 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
150 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr ||
151 &SetControlViewSize == kUnresolvedCFragSymbolAddress)
152 mac_gestalts.cntlattr = 0;
153 /* Mac OS 8.5 Window Manager? */
154 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
155 &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
156 mac_gestalts.windattr = 0;
158 /* Text Encoding Conversion Manager? */
160 #if TARGET_RT_MAC_CFM
161 &TECGetInfo == kUnresolvedCFragSymbolAddress ||
163 InitializeUnicodeConverter(NULL) != noErr ||
165 TECGetInfo(&ti) != noErr)
166 mac_gestalts.encvvers = 0;
168 mac_gestalts.encvvers = (*ti)->tecVersion;
169 mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
170 DisposeHandle((Handle)ti);
175 /* We've been tested with the Appearance Manager */
176 if (mac_gestalts.apprvers != 0)
177 RegisterAppearanceClient();
179 menuBar = GetNewMBar(128);
181 fatalbox("Unable to create menu bar.");
183 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
187 windows.about = NULL;
188 windows.licence = NULL;
190 default_protocol = DEFAULT_PROTOCOL;
191 default_port = DEFAULT_PORT;
192 flags = FLAG_INTERACTIVE;
195 * Really grotty hack to ensure that anything that looks at the
196 * global "cfg" variable gets something vaguely sensible.
197 * Obviously, nothing should actually be using it, but that will
198 * take a while to arrange.
200 do_defaults(NULL, &cfg);
206 /* Set the default directory for loading and saving settings. */
207 /* XXX Should we create it? */
208 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
209 LMSetSFSaveDisk(-vol);
210 LMSetCurDirStore(dirid);
216 static void mac_eventloop(void) {
223 mac_adjustcursor(cursrgn);
224 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
225 mac_adjustcursor(cursrgn);
234 static void mac_event(EventRecord *event) {
239 switch (event->what) {
241 part = FindWindow(event->where, &window);
245 mac_menucommand(MenuSelect(event->where));
248 SystemClick(event, window);
251 if (window != FrontWindow())
252 /* XXX: check for movable modal dboxes? */
253 SelectWindow(window);
255 mac_contentclick(window, event);
258 if (TrackGoAway(window, event->where))
259 mac_closewindow(window);
262 /* XXX: moveable modal check? */
263 DragWindow(window, event->where, &qd.screenBits.bounds);
266 mac_growwindow(window, event);
270 if (TrackBox(window, event->where, part))
271 mac_zoomwindow(window, part);
280 mac_activatewindow((WindowPtr)event->message, event);
283 mac_updatewindow((WindowPtr)event->message);
286 if (HiWord(event->message) != noErr) {
287 SetPt(&pt, 120, 120);
288 DIBadMount(pt, event->message);
294 static void mac_contentclick(WindowPtr window, EventRecord *event) {
297 switch (mac_windowtype(window)) {
299 mac_clickterm(window, event);
302 if (DialogSelect(event, &window, &item))
310 mac_clickdlg(window, event);
315 static void mac_growwindow(WindowPtr window, EventRecord *event) {
317 switch (mac_windowtype(window)) {
319 mac_growterm(window, event);
323 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
326 active = (event->modifiers & activeFlag) != 0;
328 switch (mac_windowtype(window)) {
330 mac_activateterm(window, active);
333 mac_activatedlg(window, event);
336 mac_activateabout(window, event);
341 static void mac_activateabout(WindowPtr window, EventRecord *event) {
342 DialogItemType itemtype;
348 active = (event->modifiers & activeFlag) != 0;
349 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
350 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
351 DialogSelect(event, &window, &item);
354 static void mac_updatewindow(WindowPtr window) {
356 switch (mac_windowtype(window)) {
358 mac_updateterm(window);
363 UpdateDialog(window, window->visRgn);
367 mac_updatelicence(window);
372 static void mac_updatelicence(WindowPtr window)
380 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
381 TextFont(HiWord(fondsize));
382 TextSize(LoWord(fondsize));
383 h = Get1Resource('TEXT', wLicence);
384 len = GetResourceSizeOnDisk(h);
387 TETextBox(*h, len, &window->portRect, teFlushDefault);
394 * Work out what kind of window we're dealing with.
395 * Concept shamelessly nicked from SurfWriter.
397 static int mac_windowtype(WindowPtr window) {
403 kind = ((WindowPeek)window)->windowKind;
406 if (GetWVariant(window) == zoomDocProc)
408 refcon = GetWRefCon(window);
418 static void mac_keypress(EventRecord *event) {
421 window = FrontWindow();
423 * Check for a command-key combination, but ignore it if it counts
424 * as a meta-key combination and we're in a terminal window.
426 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
427 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
428 mac_windowtype(window) == wTerminal)*/) {
430 mac_menucommand(MenuKey(event->message & charCodeMask));
432 switch (mac_windowtype(window)) {
434 mac_keyterm(window, event);
440 static void mac_menucommand(long result) {
445 menu = HiWord(result);
446 item = LoWord(result);
447 window = FrontWindow();
448 /* Things which do the same whatever window we're in. */
456 GetMenuItemText(GetMenuHandle(mApple), item, da);
470 mac_closewindow(window);
478 /* If we get here, handling is up to window-specific code. */
479 switch (mac_windowtype(window)) {
481 mac_menuterm(window, menu, item);
488 static void mac_openabout(void) {
489 DialogItemType itemtype;
496 SelectWindow(windows.about);
498 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
499 vers = (VersRecHndl)Get1Resource('vers', 1);
500 if (vers != NULL && *vers != NULL) {
501 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
502 GetDialogItem(windows.about, wiAboutVersion,
503 &itemtype, &item, &box);
504 assert(itemtype & kStaticTextDialogItem);
505 SetDialogItemText(item, longvers);
507 ShowWindow(windows.about);
511 static void mac_openlicence(void) {
514 SelectWindow(windows.licence);
516 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
517 ShowWindow(windows.licence);
521 static void mac_closewindow(WindowPtr window) {
523 switch (mac_windowtype(window)) {
525 CloseDeskAcc(((WindowPeek)window)->windowKind);
528 /* FIXME: end session and stuff */
531 windows.about = NULL;
535 windows.licence = NULL;
544 static void mac_zoomwindow(WindowPtr window, short part) {
546 /* FIXME: do something */
550 * Make the menus look right before the user gets to see them.
552 static void mac_adjustmenus(void) {
556 window = FrontWindow();
557 menu = GetMenuHandle(mApple);
559 EnableItem(menu, iAbout);
561 menu = GetMenuHandle(mFile);
563 EnableItem(menu, iNew);
565 EnableItem(menu, iClose);
567 DisableItem(menu, iClose);
568 EnableItem(menu, iQuit);
570 switch (mac_windowtype(window)) {
572 mac_adjusttermmenus(window);
575 menu = GetMenuHandle(mEdit);
576 DisableItem(menu, 0);
583 * Make sure the right cursor's being displayed.
585 static void mac_adjustcursor(RgnHandle cursrgn) {
587 WindowPtr window, front;
591 LocalToGlobal(&mouse);
592 part = FindWindow(mouse, &window);
593 front = FrontWindow();
594 if (part != inContent || window == NULL || window != front) {
595 /* Cursor isn't in the front window, so switch to arrow */
596 SetCursor(&qd.arrow);
597 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
599 DiffRgn(cursrgn, front->visRgn, cursrgn);
601 switch (mac_windowtype(window)) {
603 mac_adjusttermcursor(window, mouse, cursrgn);
606 SetCursor(&qd.arrow);
607 CopyRgn(window->visRgn, cursrgn);
613 void cleanup_exit(int status)
616 #if !TARGET_RT_MAC_CFM
617 if (mac_gestalts.encvvers != 0)
618 TerminateUnicodeConverter();
624 void fatalbox(char *fmt, ...) {
629 /* We'd like stuff to be a Pascal string */
630 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
632 ParamText(stuff, NULL, NULL, NULL);
633 StopAlert(128, NULL);
637 void modalfatalbox(char *fmt, ...) {
642 /* We'd like stuff to be a Pascal string */
643 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
645 ParamText(stuff, NULL, NULL, NULL);
646 StopAlert(128, NULL);
650 /* This should only kill the current session, not the whole application. */
651 void connection_fatal(void *fontend, char *fmt, ...) {
656 /* We'd like stuff to be a Pascal string */
657 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
659 ParamText(stuff, NULL, NULL, NULL);
660 StopAlert(128, NULL);
664 /* Null SSH agent client -- never finds an agent. */
666 int agent_exists(void)
672 void agent_query(void *in, int inlen, void **out, int *outlen)
679 /* Temporary null routines for testing. */
681 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
682 char *keystr, char *fingerprint)
687 void askcipher(void *frontend, char *ciphername, int cs)
692 void old_keyfile_warning(void)
699 * c-file-style: "simon"