1 /* $Id: mac.c,v 1.55 2003/03/17 19:00:36 ben Exp $ */
3 * Copyright (c) 1999, 2003 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 <AEDataModel.h>
33 #include <AppleEvents.h>
34 #include <Quickdraw.h>
36 #include <MacWindows.h>
39 #include <Appearance.h>
40 #include <CodeFragments.h>
46 #include <Navigation.h>
47 #include <Resources.h>
49 #include <TextCommon.h>
50 #include <ToolUtils.h>
51 #include <UnicodeConverter.h>
56 #include <stdlib.h> /* putty.h needs size_t */
57 #include <stdio.h> /* for vsprintf */
59 #define PUTTY_DO_GLOBALS
70 static int borednow = FALSE;
71 struct mac_gestalts mac_gestalts;
74 static void mac_startup(void);
75 static void mac_eventloop(void);
76 #pragma noreturn (mac_eventloop)
77 static void mac_event(EventRecord *);
78 static void mac_contentclick(WindowPtr, EventRecord *);
79 static void mac_growwindow(WindowPtr, EventRecord *);
80 static void mac_activatewindow(WindowPtr, EventRecord *);
81 static void mac_suspendresume(EventRecord *);
82 static void mac_activateabout(WindowPtr, EventRecord *);
83 static void mac_updatewindow(WindowPtr);
84 static void mac_updatelicence(WindowPtr);
85 static void mac_keypress(EventRecord *);
86 static int mac_windowtype(WindowPtr);
87 static void mac_menucommand(long);
88 static void mac_openlicence(void);
89 static void mac_adjustcursor(RgnHandle);
90 static void mac_adjustmenus(void);
91 static void mac_closewindow(WindowPtr);
92 static void mac_zoomwindow(WindowPtr, short);
93 #pragma noreturn (cleanup_exit)
100 struct mac_windows windows;
102 int main (int argc, char **argv) {
108 #pragma noreturn (main)
110 static void mac_startup(void) {
114 #if !TARGET_API_MAC_CARBON
115 /* Init Memory Manager */
118 InitGraf(&qd.thePort);
119 /* Init Font Manager */
121 /* Init Window Manager */
123 /* Init Menu Manager */
127 /* Init Dialog Manager */
132 /* Get base system version (only used if there's no better selector) */
133 if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
134 (mac_gestalts.sysvers &= 0xffff) < 0x700)
135 fatalbox("PuTTY requires System 7 or newer");
136 /* Find out if we've got Color Quickdraw */
137 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
138 mac_gestalts.qdvers = gestaltOriginalQD;
139 /* ... and the Appearance Manager? */
140 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
141 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
142 mac_gestalts.apprvers = 0x0100;
144 mac_gestalts.apprvers = 0;
145 #if TARGET_RT_MAC_CFM
146 /* Paranoia: Did we manage to pull in AppearanceLib? */
147 if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
148 mac_gestalts.apprvers = 0;
151 mac_gestalts.cntlattr = 0;
152 mac_gestalts.windattr = 0;
154 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
155 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr ||
156 &SetControlViewSize == kUnresolvedCFragSymbolAddress)
157 mac_gestalts.cntlattr = 0;
158 /* Mac OS 8.5 Window Manager? */
159 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
160 &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
161 mac_gestalts.windattr = 0;
162 /* Mac OS 8.5 Menu Manager? */
163 if (Gestalt(gestaltMenuMgrAttr, &mac_gestalts.menuattr) != noErr)
164 mac_gestalts.menuattr = 0;
166 /* Text Encoding Conversion Manager? */
168 #if TARGET_RT_MAC_CFM
169 &TECGetInfo == kUnresolvedCFragSymbolAddress ||
171 InitializeUnicodeConverter(NULL) != noErr ||
173 TECGetInfo(&ti) != noErr)
174 mac_gestalts.encvvers = 0;
176 mac_gestalts.encvvers = (*ti)->tecVersion;
177 mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
178 DisposeHandle((Handle)ti);
180 /* Navigation Services? */
181 if (NavServicesAvailable())
182 mac_gestalts.navsvers = NavLibraryVersion();
184 mac_gestalts.navsvers = 0;
188 /* We've been tested with the Appearance Manager */
189 if (mac_gestalts.apprvers != 0)
190 RegisterAppearanceClient();
192 menuBar = GetNewMBar(128);
194 fatalbox("Unable to create menu bar.");
196 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
197 if (mac_gestalts.menuattr & gestaltMenuMgrAquaLayoutMask) {
198 DeleteMenuItem(GetMenuHandle(mFile), iQuit);
199 /* Also delete the separator above the Quit item. */
200 DeleteMenuItem(GetMenuHandle(mFile), iQuit - 1);
205 windows.about = NULL;
206 windows.licence = NULL;
208 default_protocol = be_default_protocol;
209 /* Find the appropriate default port. */
212 default_port = 0; /* illegal */
213 for (i = 0; backends[i].backend != NULL; i++)
214 if (backends[i].protocol == default_protocol) {
215 default_port = backends[i].backend->default_port;
219 flags = FLAG_INTERACTIVE;
221 #if !TARGET_API_MAC_CARBON
226 /* Set the default directory for loading and saving settings. */
227 /* XXX Should we create it? */
228 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
229 LMSetSFSaveDisk(-vol);
230 LMSetCurDirStore(dirid);
235 /* Install Apple Event handlers. */
236 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
237 NewAEEventHandlerUPP(&mac_aevt_oapp), 0, FALSE);
238 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
239 NewAEEventHandlerUPP(&mac_aevt_odoc), 0, FALSE);
240 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
241 NewAEEventHandlerUPP(&mac_aevt_pdoc), 0, FALSE);
242 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
243 NewAEEventHandlerUPP(&mac_aevt_quit), 0, FALSE);
246 static void mac_eventloop(void) {
254 mac_adjustcursor(cursrgn);
255 gotevent = WaitNextEvent(everyEvent, &event, sleeptime, cursrgn);
257 * XXX For now, limit sleep time to 1/10 s to work around
258 * wake-before-sleep race in MacTCP code.
261 mac_adjustcursor(cursrgn);
263 /* Ensure we get a null event when the real ones run out. */
276 static void mac_event(EventRecord *event) {
280 switch (event->what) {
282 part = FindWindow(event->where, &window);
286 mac_menucommand(MenuSelect(event->where));
288 #if !TARGET_API_MAC_CARBON
290 SystemClick(event, window);
294 if (window != FrontWindow())
295 /* XXX: check for movable modal dboxes? */
296 SelectWindow(window);
298 mac_contentclick(window, event);
301 if (TrackGoAway(window, event->where))
302 mac_closewindow(window);
305 /* XXX: moveable modal check? */
306 #if TARGET_API_MAC_CARBON
310 GetQDGlobalsScreenBits(&screenBits);
311 DragWindow(window, event->where, &screenBits.bounds);
314 DragWindow(window, event->where, &qd.screenBits.bounds);
318 mac_growwindow(window, event);
322 if (TrackBox(window, event->where, part))
323 mac_zoomwindow(window, part);
332 mac_activatewindow((WindowPtr)event->message, event);
335 mac_updatewindow((WindowPtr)event->message);
337 #if !TARGET_API_MAC_CARBON
339 if (HiWord(event->message) != noErr) {
342 SetPt(&pt, 120, 120);
343 DIBadMount(pt, event->message);
348 switch ((event->message & osEvtMessageMask) >> 24) {
349 case suspendResumeMessage:
350 mac_suspendresume(event);
354 case kHighLevelEvent:
355 AEProcessAppleEvent(event); /* errors? */
360 static void mac_contentclick(WindowPtr window, EventRecord *event)
363 if (mac_wininfo(window)->click != NULL)
364 (*mac_wininfo(window)->click)(window, event);
367 static void mac_growwindow(WindowPtr window, EventRecord *event)
370 if (mac_wininfo(window)->grow != NULL)
371 (*mac_wininfo(window)->grow)(window, event);
374 static void mac_activatewindow(WindowPtr window, EventRecord *event)
378 if (mac_wininfo(window)->activate != NULL)
379 (*mac_wininfo(window)->activate)(window, event);
382 static void mac_updatewindow(WindowPtr window)
385 if (mac_wininfo(window)->update != NULL)
386 (*mac_wininfo(window)->update)(window);
390 * Work out what kind of window we're dealing with.
392 static int mac_windowtype(WindowPtr window)
395 #if !TARGET_API_MAC_CARBON
396 if (GetWindowKind(window) < 0)
399 return ((WinInfo *)GetWRefCon(window))->wtype;
405 static void mac_keypress(EventRecord *event) {
408 window = mac_frontwindow();
410 * Check for a command-key combination, but ignore it if it counts
411 * as a meta-key combination and we're in a terminal window.
413 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
414 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
415 mac_windowtype(window) == wTerminal)*/) {
417 mac_menucommand(MenuKey(event->message & charCodeMask));
419 if (window != NULL && mac_wininfo(window)->key != NULL)
420 (*mac_wininfo(window)->key)(window, event);
424 static void mac_menucommand(long result) {
427 #if !TARGET_API_MAC_CARBON
431 menu = HiWord(result);
432 item = LoWord(result);
433 window = mac_frontwindow();
434 /* Things which do the same whatever window we're in. */
441 #if !TARGET_API_MAC_CARBON
443 GetMenuItemText(GetMenuHandle(mApple), item, da);
458 mac_closewindow(window);
475 /* If we get here, handling is up to window-specific code. */
476 if (window != NULL && mac_wininfo(window)->menu != NULL)
477 (*mac_wininfo(window)->menu)(window, menu, item);
483 static void mac_closewindow(WindowPtr window) {
485 switch (mac_windowtype(window)) {
486 #if !TARGET_API_MAC_CARBON
488 CloseDeskAcc(GetWindowKind(window));
492 if (mac_wininfo(window)->close != NULL)
493 (*mac_wininfo(window)->close)(window);
498 static void mac_suspendresume(EventRecord *event)
501 EventRecord fakeevent;
504 * We're called either before we're suspended or after we're
505 * resumed, so we're the front application at this point.
507 front = FrontWindow();
509 fakeevent.what = activateEvt;
510 fakeevent.message = (UInt32)front;
511 fakeevent.when = event->when;
512 fakeevent.where = event->where;
513 fakeevent.modifiers =
514 (event->message & resumeFlag) ? activeFlag : 0;
515 mac_activatewindow(front, &fakeevent);
519 static void mac_zoomwindow(WindowPtr window, short part) {
521 /* FIXME: do something */
525 * Make the menus look right before the user gets to see them.
527 #if TARGET_API_MAC_CARBON
528 #define EnableItem EnableMenuItem
529 #define DisableItem DisableMenuItem
531 static void mac_adjustmenus(void) {
535 window = mac_frontwindow();
536 menu = GetMenuHandle(mApple);
538 EnableItem(menu, iAbout);
540 menu = GetMenuHandle(mFile);
542 EnableItem(menu, iNew);
544 EnableItem(menu, iClose);
546 DisableItem(menu, iClose);
547 EnableItem(menu, iQuit);
549 if (window != NULL && mac_wininfo(window)->adjustmenus != NULL)
550 (*mac_wininfo(window)->adjustmenus)(window);
552 DisableItem(menu, iSave);
553 DisableItem(menu, iSaveAs);
554 DisableItem(menu, iDuplicate);
555 menu = GetMenuHandle(mEdit);
556 DisableItem(menu, 0);
557 menu = GetMenuHandle(mWindow);
558 DisableItem(menu, 0); /* Until we get more than 1 item on it. */
564 * Make sure the right cursor's being displayed.
566 static void mac_adjustcursor(RgnHandle cursrgn) {
568 WindowPtr window, front;
570 #if TARGET_API_MAC_CARBON
576 LocalToGlobal(&mouse);
577 part = FindWindow(mouse, &window);
578 front = FrontWindow();
579 if (part != inContent || window == NULL || window != front) {
580 /* Cursor isn't in the front window, so switch to arrow */
581 #if TARGET_API_MAC_CARBON
582 GetQDGlobalsArrow(&arrow);
585 SetCursor(&qd.arrow);
587 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
589 #if TARGET_API_MAC_CARBON
591 GetPortVisibleRegion(GetWindowPort(front), visrgn);
592 DiffRgn(cursrgn, visrgn, cursrgn);
595 DiffRgn(cursrgn, front->visRgn, cursrgn);
599 if (mac_wininfo(window)->adjustcursor != NULL)
600 (*mac_wininfo(window)->adjustcursor)(window, mouse, cursrgn);
602 #if TARGET_API_MAC_CARBON
603 GetQDGlobalsArrow(&arrow);
605 GetPortVisibleRegion(GetWindowPort(window), cursrgn);
607 SetCursor(&qd.arrow);
608 CopyRgn(window->visRgn, cursrgn);
614 pascal OSErr mac_aevt_quit(const AppleEvent *req, AppleEvent *reply,
620 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
621 &type, NULL, 0, &size) == noErr)
622 return errAEParamMissed;
628 void cleanup_exit(int status)
631 #if !TARGET_RT_MAC_CFM
632 if (mac_gestalts.encvvers != 0)
633 TerminateUnicodeConverter();
639 /* This should only kill the current session, not the whole application. */
640 void connection_fatal(void *fontend, char *fmt, ...) {
645 /* We'd like stuff to be a Pascal string */
646 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
648 ParamText(stuff, NULL, NULL, NULL);
649 StopAlert(128, NULL);
653 /* Null SSH agent client -- never finds an agent. */
655 int agent_exists(void)
661 void agent_query(void *in, int inlen, void **out, int *outlen)
668 /* Temporary null routines for testing. */
671 * FIXME: verify_ssh_host_key() should be passed a frontend handle,
672 * but backends have to have a terminal handle instead, because they
673 * pass it to from_backend(), so we accept a terminal handle here as
674 * well, and hope no-one tries to call us with sensible arguments.
676 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
677 char *keystr, char *fingerprint)
680 Terminal *term = frontend;
681 Session *s = term->frontend;
684 * This function is horribly wrong. For one thing, the alert
685 * shouldn't be modal, it should be movable modal, or a sheet in
686 * Aqua. Also, PuTTY might be in the background, in which case we
687 * should use the Notification Manager to wake up the user. In
688 * any case, we shouldn't hold up processing of other connections'
689 * data just because this one's waiting for the user. It should
690 * also handle a host key cache, of course, and see the note above
691 * about the "frontend" argument and the one below about closing
692 * the connection. All in all, a bit of a mess really.
695 stuff[0] = sprintf((char *)(&stuff[1]),
696 "The server's key fingerprint is: %s\n"
697 "Continue connecting?", fingerprint);
698 ParamText(stuff, NULL, NULL, NULL);
699 if (CautionAlert(wQuestion, NULL) == 2) {
701 * User chose "Cancel". Unfortunately, if I tear the
702 * connection down here, Bad Things happen when I return. I
703 * think this function should actually return something
704 * telling the SSH code to abandon the connection.
709 void askcipher(void *frontend, char *ciphername, int cs)
714 void old_keyfile_warning(void)
719 FontSpec platform_default_fontspec(char const *name)
724 if (!strcmp(name, "Font")) {
725 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
727 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
729 GetFontName(HiWord(smfs), ret.name);
730 if (ret.name[0] == 0)
731 memcpy(ret.name, "\pMonaco", 7);
732 ret.size = LoWord(smfs);
734 memcpy(ret.name, "\pMonaco", 7);
745 Filename platform_default_filename(const char *name)
748 if (!strcmp(name, "LogFileName"))
749 FSMakeFSSpec(0, 0, "\pputty.log", &ret.fss);
751 memset(&ret, 0, sizeof(ret));
755 char *platform_default_s(char const *name)
760 int platform_default_i(char const *name, int def)
763 /* Non-raw cut and paste of line-drawing chars works badly on the
764 * current Unix stub implementation of the Unicode functions.
765 * So I'm going to temporarily set the default to raw mode so
766 * that the failure mode isn't quite so drastically horrid.
767 * When Unicode comes in, this can all be put right. */
768 if (!strcmp(name, "RawCNP"))
773 void platform_get_x11_auth(char *display, int *proto,
774 unsigned char *data, int *datalen)
776 /* SGT: I have no idea whether Mac X servers need anything here. */
781 * c-file-style: "simon"