1 /* $Id: mac.c,v 1.54 2003/03/06 23:44:47 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);
202 windows.about = NULL;
203 windows.licence = NULL;
205 default_protocol = be_default_protocol;
206 /* Find the appropriate default port. */
209 default_port = 0; /* illegal */
210 for (i = 0; backends[i].backend != NULL; i++)
211 if (backends[i].protocol == default_protocol) {
212 default_port = backends[i].backend->default_port;
216 flags = FLAG_INTERACTIVE;
218 #if !TARGET_API_MAC_CARBON
223 /* Set the default directory for loading and saving settings. */
224 /* XXX Should we create it? */
225 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
226 LMSetSFSaveDisk(-vol);
227 LMSetCurDirStore(dirid);
232 /* Install Apple Event handlers. */
233 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
234 NewAEEventHandlerUPP(&mac_aevt_oapp), 0, FALSE);
235 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
236 NewAEEventHandlerUPP(&mac_aevt_odoc), 0, FALSE);
237 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
238 NewAEEventHandlerUPP(&mac_aevt_pdoc), 0, FALSE);
239 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
240 NewAEEventHandlerUPP(&mac_aevt_quit), 0, FALSE);
243 static void mac_eventloop(void) {
251 mac_adjustcursor(cursrgn);
252 gotevent = WaitNextEvent(everyEvent, &event, sleeptime, cursrgn);
254 * XXX For now, limit sleep time to 1/10 s to work around
255 * wake-before-sleep race in MacTCP code.
258 mac_adjustcursor(cursrgn);
260 /* Ensure we get a null event when the real ones run out. */
273 static void mac_event(EventRecord *event) {
277 switch (event->what) {
279 part = FindWindow(event->where, &window);
283 mac_menucommand(MenuSelect(event->where));
285 #if !TARGET_API_MAC_CARBON
287 SystemClick(event, window);
291 if (window != FrontWindow())
292 /* XXX: check for movable modal dboxes? */
293 SelectWindow(window);
295 mac_contentclick(window, event);
298 if (TrackGoAway(window, event->where))
299 mac_closewindow(window);
302 /* XXX: moveable modal check? */
303 #if TARGET_API_MAC_CARBON
307 GetQDGlobalsScreenBits(&screenBits);
308 DragWindow(window, event->where, &screenBits.bounds);
311 DragWindow(window, event->where, &qd.screenBits.bounds);
315 mac_growwindow(window, event);
319 if (TrackBox(window, event->where, part))
320 mac_zoomwindow(window, part);
329 mac_activatewindow((WindowPtr)event->message, event);
332 mac_updatewindow((WindowPtr)event->message);
334 #if !TARGET_API_MAC_CARBON
336 if (HiWord(event->message) != noErr) {
339 SetPt(&pt, 120, 120);
340 DIBadMount(pt, event->message);
345 switch ((event->message & osEvtMessageMask) >> 24) {
346 case suspendResumeMessage:
347 mac_suspendresume(event);
351 case kHighLevelEvent:
352 AEProcessAppleEvent(event); /* errors? */
357 static void mac_contentclick(WindowPtr window, EventRecord *event)
360 if (mac_wininfo(window)->click != NULL)
361 (*mac_wininfo(window)->click)(window, event);
364 static void mac_growwindow(WindowPtr window, EventRecord *event)
367 if (mac_wininfo(window)->grow != NULL)
368 (*mac_wininfo(window)->grow)(window, event);
371 static void mac_activatewindow(WindowPtr window, EventRecord *event)
375 if (mac_wininfo(window)->activate != NULL)
376 (*mac_wininfo(window)->activate)(window, event);
379 static void mac_updatewindow(WindowPtr window)
382 if (mac_wininfo(window)->update != NULL)
383 (*mac_wininfo(window)->update)(window);
387 * Work out what kind of window we're dealing with.
389 static int mac_windowtype(WindowPtr window)
392 #if !TARGET_API_MAC_CARBON
393 if (GetWindowKind(window) < 0)
396 return ((WinInfo *)GetWRefCon(window))->wtype;
402 static void mac_keypress(EventRecord *event) {
405 window = mac_frontwindow();
407 * Check for a command-key combination, but ignore it if it counts
408 * as a meta-key combination and we're in a terminal window.
410 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
411 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
412 mac_windowtype(window) == wTerminal)*/) {
414 mac_menucommand(MenuKey(event->message & charCodeMask));
416 if (window != NULL && mac_wininfo(window)->key != NULL)
417 (*mac_wininfo(window)->key)(window, event);
421 static void mac_menucommand(long result) {
424 #if !TARGET_API_MAC_CARBON
428 menu = HiWord(result);
429 item = LoWord(result);
430 window = mac_frontwindow();
431 /* Things which do the same whatever window we're in. */
438 #if !TARGET_API_MAC_CARBON
440 GetMenuItemText(GetMenuHandle(mApple), item, da);
455 mac_closewindow(window);
472 /* If we get here, handling is up to window-specific code. */
473 if (window != NULL && mac_wininfo(window)->menu != NULL)
474 (*mac_wininfo(window)->menu)(window, menu, item);
480 static void mac_closewindow(WindowPtr window) {
482 switch (mac_windowtype(window)) {
483 #if !TARGET_API_MAC_CARBON
485 CloseDeskAcc(GetWindowKind(window));
489 if (mac_wininfo(window)->close != NULL)
490 (*mac_wininfo(window)->close)(window);
495 static void mac_suspendresume(EventRecord *event)
498 EventRecord fakeevent;
501 * We're called either before we're suspended or after we're
502 * resumed, so we're the front application at this point.
504 front = FrontWindow();
506 fakeevent.what = activateEvt;
507 fakeevent.message = (UInt32)front;
508 fakeevent.when = event->when;
509 fakeevent.where = event->where;
510 fakeevent.modifiers =
511 (event->message & resumeFlag) ? activeFlag : 0;
512 mac_activatewindow(front, &fakeevent);
516 static void mac_zoomwindow(WindowPtr window, short part) {
518 /* FIXME: do something */
522 * Make the menus look right before the user gets to see them.
524 #if TARGET_API_MAC_CARBON
525 #define EnableItem EnableMenuItem
526 #define DisableItem DisableMenuItem
528 static void mac_adjustmenus(void) {
532 window = mac_frontwindow();
533 menu = GetMenuHandle(mApple);
535 EnableItem(menu, iAbout);
537 menu = GetMenuHandle(mFile);
539 EnableItem(menu, iNew);
541 EnableItem(menu, iClose);
543 DisableItem(menu, iClose);
544 EnableItem(menu, iQuit);
546 if (window != NULL && mac_wininfo(window)->adjustmenus != NULL)
547 (*mac_wininfo(window)->adjustmenus)(window);
549 DisableItem(menu, iSave);
550 DisableItem(menu, iSaveAs);
551 DisableItem(menu, iDuplicate);
552 menu = GetMenuHandle(mEdit);
553 DisableItem(menu, 0);
554 menu = GetMenuHandle(mWindow);
555 DisableItem(menu, 0); /* Until we get more than 1 item on it. */
561 * Make sure the right cursor's being displayed.
563 static void mac_adjustcursor(RgnHandle cursrgn) {
565 WindowPtr window, front;
567 #if TARGET_API_MAC_CARBON
573 LocalToGlobal(&mouse);
574 part = FindWindow(mouse, &window);
575 front = FrontWindow();
576 if (part != inContent || window == NULL || window != front) {
577 /* Cursor isn't in the front window, so switch to arrow */
578 #if TARGET_API_MAC_CARBON
579 GetQDGlobalsArrow(&arrow);
582 SetCursor(&qd.arrow);
584 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
586 #if TARGET_API_MAC_CARBON
588 GetPortVisibleRegion(GetWindowPort(front), visrgn);
589 DiffRgn(cursrgn, visrgn, cursrgn);
592 DiffRgn(cursrgn, front->visRgn, cursrgn);
596 if (mac_wininfo(window)->adjustcursor != NULL)
597 (*mac_wininfo(window)->adjustcursor)(window, mouse, cursrgn);
599 #if TARGET_API_MAC_CARBON
600 GetQDGlobalsArrow(&arrow);
602 GetPortVisibleRegion(GetWindowPort(window), cursrgn);
604 SetCursor(&qd.arrow);
605 CopyRgn(window->visRgn, cursrgn);
611 pascal OSErr mac_aevt_quit(const AppleEvent *req, AppleEvent *reply,
617 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
618 &type, NULL, 0, &size) == noErr)
619 return errAEParamMissed;
625 void cleanup_exit(int status)
628 #if !TARGET_RT_MAC_CFM
629 if (mac_gestalts.encvvers != 0)
630 TerminateUnicodeConverter();
636 /* This should only kill the current session, not the whole application. */
637 void connection_fatal(void *fontend, 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 /* Null SSH agent client -- never finds an agent. */
652 int agent_exists(void)
658 void agent_query(void *in, int inlen, void **out, int *outlen)
665 /* Temporary null routines for testing. */
668 * FIXME: verify_ssh_host_key() should be passed a frontend handle,
669 * but backends have to have a terminal handle instead, because they
670 * pass it to from_backend(), so we accept a terminal handle here as
671 * well, and hope no-one tries to call us with sensible arguments.
673 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
674 char *keystr, char *fingerprint)
677 Terminal *term = frontend;
678 Session *s = term->frontend;
681 * This function is horribly wrong. For one thing, the alert
682 * shouldn't be modal, it should be movable modal, or a sheet in
683 * Aqua. Also, PuTTY might be in the background, in which case we
684 * should use the Notification Manager to wake up the user. In
685 * any case, we shouldn't hold up processing of other connections'
686 * data just because this one's waiting for the user. It should
687 * also handle a host key cache, of course, and see the note above
688 * about the "frontend" argument and the one below about closing
689 * the connection. All in all, a bit of a mess really.
692 stuff[0] = sprintf((char *)(&stuff[1]),
693 "The server's key fingerprint is: %s\n"
694 "Continue connecting?", fingerprint);
695 ParamText(stuff, NULL, NULL, NULL);
696 if (CautionAlert(wQuestion, NULL) == 2) {
698 * User chose "Cancel". Unfortunately, if I tear the
699 * connection down here, Bad Things happen when I return. I
700 * think this function should actually return something
701 * telling the SSH code to abandon the connection.
706 void askcipher(void *frontend, char *ciphername, int cs)
711 void old_keyfile_warning(void)
716 FontSpec platform_default_fontspec(char const *name)
721 if (!strcmp(name, "Font")) {
722 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
724 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
726 GetFontName(HiWord(smfs), ret.name);
727 if (ret.name[0] == 0)
728 memcpy(ret.name, "\pMonaco", 7);
729 ret.size = LoWord(smfs);
731 memcpy(ret.name, "\pMonaco", 7);
742 Filename platform_default_filename(const char *name)
745 if (!strcmp(name, "LogFileName"))
746 FSMakeFSSpec(0, 0, "\pputty.log", &ret.fss);
748 memset(&ret, 0, sizeof(ret));
752 char *platform_default_s(char const *name)
757 int platform_default_i(char const *name, int def)
760 /* Non-raw cut and paste of line-drawing chars works badly on the
761 * current Unix stub implementation of the Unicode functions.
762 * So I'm going to temporarily set the default to raw mode so
763 * that the failure mode isn't quite so drastically horrid.
764 * When Unicode comes in, this can all be put right. */
765 if (!strcmp(name, "RawCNP"))
770 void platform_get_x11_auth(char *display, int *proto,
771 unsigned char *data, int *datalen)
773 /* SGT: I have no idea whether Mac X servers need anything here. */
778 * c-file-style: "simon"