1 /* $Id: mac.c,v 1.33 2003/01/18 20:09:21 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);
173 if (Gestalt(gestaltOpenTpt, &mac_gestalts.otptattr) != noErr ||
174 (mac_gestalts.otptattr & gestaltOpenTptTCPPresentMask) == 0 ||
176 mac_gestalts.otptattr = 0;
177 if (mac_gestalts.otptattr == 0) {
179 if (Gestalt(FOUR_CHAR_CODE('mtcp'), &mac_gestalts.mtcpvers) != noErr)
180 mac_gestalts.mtcpvers = 0;
181 if (mac_gestalts.mtcpvers > 0) {
182 if (mactcp_init() != noErr)
183 mac_gestalts.mtcpvers = 0;
186 mac_gestalts.mtcpvers = 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');
200 windows.about = NULL;
201 windows.licence = NULL;
203 default_protocol = be_default_protocol;
204 /* Find the appropriate default port. */
207 default_port = 0; /* illegal */
208 for (i = 0; backends[i].backend != NULL; i++)
209 if (backends[i].protocol == default_protocol) {
210 default_port = backends[i].backend->default_port;
214 flags = FLAG_INTERACTIVE;
220 /* Set the default directory for loading and saving settings. */
221 /* XXX Should we create it? */
222 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
223 LMSetSFSaveDisk(-vol);
224 LMSetCurDirStore(dirid);
229 static void mac_eventloop(void) {
236 mac_adjustcursor(cursrgn);
237 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
238 mac_adjustcursor(cursrgn);
241 if (mac_gestalts.mtcpvers != 0)
243 if (mac_gestalts.otptattr != 0)
250 static void mac_event(EventRecord *event) {
255 switch (event->what) {
257 part = FindWindow(event->where, &window);
261 mac_menucommand(MenuSelect(event->where));
264 SystemClick(event, window);
267 if (window != FrontWindow())
268 /* XXX: check for movable modal dboxes? */
269 SelectWindow(window);
271 mac_contentclick(window, event);
274 if (TrackGoAway(window, event->where))
275 mac_closewindow(window);
278 /* XXX: moveable modal check? */
279 DragWindow(window, event->where, &qd.screenBits.bounds);
282 mac_growwindow(window, event);
286 if (TrackBox(window, event->where, part))
287 mac_zoomwindow(window, part);
296 mac_activatewindow((WindowPtr)event->message, event);
299 mac_updatewindow((WindowPtr)event->message);
302 if (HiWord(event->message) != noErr) {
303 SetPt(&pt, 120, 120);
304 DIBadMount(pt, event->message);
310 static void mac_contentclick(WindowPtr window, EventRecord *event) {
313 switch (mac_windowtype(window)) {
315 mac_clickterm(window, event);
318 if (DialogSelect(event, &window, &item))
326 mac_clickdlg(window, event);
331 static void mac_growwindow(WindowPtr window, EventRecord *event) {
333 switch (mac_windowtype(window)) {
335 mac_growterm(window, event);
339 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
342 active = (event->modifiers & activeFlag) != 0;
344 switch (mac_windowtype(window)) {
346 mac_activateterm(window, active);
349 mac_activatedlg(window, event);
352 mac_activateabout(window, event);
357 static void mac_activateabout(WindowPtr window, EventRecord *event) {
358 DialogItemType itemtype;
364 active = (event->modifiers & activeFlag) != 0;
365 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
366 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
367 DialogSelect(event, &window, &item);
370 static void mac_updatewindow(WindowPtr window) {
372 switch (mac_windowtype(window)) {
374 mac_updateterm(window);
379 UpdateDialog(window, window->visRgn);
383 mac_updatelicence(window);
388 static void mac_updatelicence(WindowPtr window)
396 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
397 TextFont(HiWord(fondsize));
398 TextSize(LoWord(fondsize));
399 h = Get1Resource('TEXT', wLicence);
400 len = GetResourceSizeOnDisk(h);
403 TETextBox(*h, len, &window->portRect, teFlushDefault);
410 * Work out what kind of window we're dealing with.
411 * Concept shamelessly nicked from SurfWriter.
413 static int mac_windowtype(WindowPtr window) {
419 kind = ((WindowPeek)window)->windowKind;
422 if (GetWVariant(window) == zoomDocProc)
424 refcon = GetWRefCon(window);
434 static void mac_keypress(EventRecord *event) {
437 window = FrontWindow();
439 * Check for a command-key combination, but ignore it if it counts
440 * as a meta-key combination and we're in a terminal window.
442 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
443 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
444 mac_windowtype(window) == wTerminal)*/) {
446 mac_menucommand(MenuKey(event->message & charCodeMask));
448 switch (mac_windowtype(window)) {
450 mac_keyterm(window, event);
456 static void mac_menucommand(long result) {
461 menu = HiWord(result);
462 item = LoWord(result);
463 window = FrontWindow();
464 /* Things which do the same whatever window we're in. */
472 GetMenuItemText(GetMenuHandle(mApple), item, da);
486 mac_closewindow(window);
500 /* If we get here, handling is up to window-specific code. */
501 switch (mac_windowtype(window)) {
503 mac_menuterm(window, menu, item);
510 static void mac_openabout(void) {
511 DialogItemType itemtype;
518 SelectWindow(windows.about);
520 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
521 vers = (VersRecHndl)Get1Resource('vers', 1);
522 if (vers != NULL && *vers != NULL) {
523 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
524 GetDialogItem(windows.about, wiAboutVersion,
525 &itemtype, &item, &box);
526 assert(itemtype & kStaticTextDialogItem);
527 SetDialogItemText(item, longvers);
529 ShowWindow(windows.about);
533 static void mac_openlicence(void) {
536 SelectWindow(windows.licence);
538 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
539 ShowWindow(windows.licence);
543 static void mac_closewindow(WindowPtr window) {
545 switch (mac_windowtype(window)) {
547 CloseDeskAcc(((WindowPeek)window)->windowKind);
550 mac_closeterm(window);
553 windows.about = NULL;
554 DisposeDialog(window);
557 windows.licence = NULL;
558 DisposeWindow(window);
563 static void mac_zoomwindow(WindowPtr window, short part) {
565 /* FIXME: do something */
569 * Make the menus look right before the user gets to see them.
571 static void mac_adjustmenus(void) {
575 window = FrontWindow();
576 menu = GetMenuHandle(mApple);
578 EnableItem(menu, iAbout);
580 menu = GetMenuHandle(mFile);
582 EnableItem(menu, iNew);
584 EnableItem(menu, iClose);
586 DisableItem(menu, iClose);
587 EnableItem(menu, iQuit);
589 switch (mac_windowtype(window)) {
591 DisableItem(menu, iSave); /* XXX enable if modified */
592 EnableItem(menu, iSaveAs);
593 menu = GetMenuHandle(mEdit);
594 DisableItem(menu, 0);
597 mac_adjusttermmenus(window);
600 DisableItem(menu, iSave);
601 DisableItem(menu, iSaveAs);
602 menu = GetMenuHandle(mEdit);
603 DisableItem(menu, 0);
610 * Make sure the right cursor's being displayed.
612 static void mac_adjustcursor(RgnHandle cursrgn) {
614 WindowPtr window, front;
618 LocalToGlobal(&mouse);
619 part = FindWindow(mouse, &window);
620 front = FrontWindow();
621 if (part != inContent || window == NULL || window != front) {
622 /* Cursor isn't in the front window, so switch to arrow */
623 SetCursor(&qd.arrow);
624 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
626 DiffRgn(cursrgn, front->visRgn, cursrgn);
628 switch (mac_windowtype(window)) {
630 mac_adjusttermcursor(window, mouse, cursrgn);
633 SetCursor(&qd.arrow);
634 CopyRgn(window->visRgn, cursrgn);
640 void cleanup_exit(int status)
643 #if !TARGET_RT_MAC_CFM
644 if (mac_gestalts.encvvers != 0)
645 TerminateUnicodeConverter();
651 void fatalbox(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 void modalfatalbox(char *fmt, ...) {
669 /* We'd like stuff to be a Pascal string */
670 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
672 ParamText(stuff, NULL, NULL, NULL);
673 StopAlert(128, NULL);
677 /* This should only kill the current session, not the whole application. */
678 void connection_fatal(void *fontend, char *fmt, ...) {
683 /* We'd like stuff to be a Pascal string */
684 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
686 ParamText(stuff, NULL, NULL, NULL);
687 StopAlert(128, NULL);
691 /* Null SSH agent client -- never finds an agent. */
693 int agent_exists(void)
699 void agent_query(void *in, int inlen, void **out, int *outlen)
706 /* Temporary null routines for testing. */
708 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
709 char *keystr, char *fingerprint)
714 void askcipher(void *frontend, char *ciphername, int cs)
719 void old_keyfile_warning(void)
724 char *platform_default_s(char const *name)
728 static char cname[256];
730 if (!strcmp(name, "Font")) {
731 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
733 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
735 GetFontName(HiWord(smfs), pname);
738 p2cstrcpy(cname, pname);
746 int platform_default_i(char const *name, int def)
750 if (!strcmp(name, "FontHeight")) {
751 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
753 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
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"