1 /* $Id: mac.c,v 1.24 2003/01/11 19:43:59 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(FOUR_CHAR_CODE('mtcp'), &mac_gestalts.mtcpvers) != noErr)
174 mac_gestalts.mtcpvers = 0;
175 if (mac_gestalts.mtcpvers > 0) {
176 if (mactcp_init() != noErr)
177 mac_gestalts.mtcpvers = 0;
180 /* We've been tested with the Appearance Manager */
181 if (mac_gestalts.apprvers != 0)
182 RegisterAppearanceClient();
184 menuBar = GetNewMBar(128);
186 fatalbox("Unable to create menu bar.");
188 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
192 windows.about = NULL;
193 windows.licence = NULL;
195 default_protocol = DEFAULT_PROTOCOL;
196 default_port = DEFAULT_PORT;
197 flags = FLAG_INTERACTIVE;
200 * Really grotty hack to ensure that anything that looks at the
201 * global "cfg" variable gets something vaguely sensible.
202 * Obviously, nothing should actually be using it, but that will
203 * take a while to arrange.
205 do_defaults(NULL, &cfg);
211 /* Set the default directory for loading and saving settings. */
212 /* XXX Should we create it? */
213 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
214 LMSetSFSaveDisk(-vol);
215 LMSetCurDirStore(dirid);
221 static void mac_eventloop(void) {
228 mac_adjustcursor(cursrgn);
229 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
230 mac_adjustcursor(cursrgn);
233 if (mac_gestalts.mtcpvers != 0)
240 static void mac_event(EventRecord *event) {
245 switch (event->what) {
247 part = FindWindow(event->where, &window);
251 mac_menucommand(MenuSelect(event->where));
254 SystemClick(event, window);
257 if (window != FrontWindow())
258 /* XXX: check for movable modal dboxes? */
259 SelectWindow(window);
261 mac_contentclick(window, event);
264 if (TrackGoAway(window, event->where))
265 mac_closewindow(window);
268 /* XXX: moveable modal check? */
269 DragWindow(window, event->where, &qd.screenBits.bounds);
272 mac_growwindow(window, event);
276 if (TrackBox(window, event->where, part))
277 mac_zoomwindow(window, part);
286 mac_activatewindow((WindowPtr)event->message, event);
289 mac_updatewindow((WindowPtr)event->message);
292 if (HiWord(event->message) != noErr) {
293 SetPt(&pt, 120, 120);
294 DIBadMount(pt, event->message);
300 static void mac_contentclick(WindowPtr window, EventRecord *event) {
303 switch (mac_windowtype(window)) {
305 mac_clickterm(window, event);
308 if (DialogSelect(event, &window, &item))
316 mac_clickdlg(window, event);
321 static void mac_growwindow(WindowPtr window, EventRecord *event) {
323 switch (mac_windowtype(window)) {
325 mac_growterm(window, event);
329 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
332 active = (event->modifiers & activeFlag) != 0;
334 switch (mac_windowtype(window)) {
336 mac_activateterm(window, active);
339 mac_activatedlg(window, event);
342 mac_activateabout(window, event);
347 static void mac_activateabout(WindowPtr window, EventRecord *event) {
348 DialogItemType itemtype;
354 active = (event->modifiers & activeFlag) != 0;
355 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
356 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
357 DialogSelect(event, &window, &item);
360 static void mac_updatewindow(WindowPtr window) {
362 switch (mac_windowtype(window)) {
364 mac_updateterm(window);
369 UpdateDialog(window, window->visRgn);
373 mac_updatelicence(window);
378 static void mac_updatelicence(WindowPtr window)
386 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
387 TextFont(HiWord(fondsize));
388 TextSize(LoWord(fondsize));
389 h = Get1Resource('TEXT', wLicence);
390 len = GetResourceSizeOnDisk(h);
393 TETextBox(*h, len, &window->portRect, teFlushDefault);
400 * Work out what kind of window we're dealing with.
401 * Concept shamelessly nicked from SurfWriter.
403 static int mac_windowtype(WindowPtr window) {
409 kind = ((WindowPeek)window)->windowKind;
412 if (GetWVariant(window) == zoomDocProc)
414 refcon = GetWRefCon(window);
424 static void mac_keypress(EventRecord *event) {
427 window = FrontWindow();
429 * Check for a command-key combination, but ignore it if it counts
430 * as a meta-key combination and we're in a terminal window.
432 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
433 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
434 mac_windowtype(window) == wTerminal)*/) {
436 mac_menucommand(MenuKey(event->message & charCodeMask));
438 switch (mac_windowtype(window)) {
440 mac_keyterm(window, event);
446 static void mac_menucommand(long result) {
451 menu = HiWord(result);
452 item = LoWord(result);
453 window = FrontWindow();
454 /* Things which do the same whatever window we're in. */
462 GetMenuItemText(GetMenuHandle(mApple), item, da);
476 mac_closewindow(window);
484 /* If we get here, handling is up to window-specific code. */
485 switch (mac_windowtype(window)) {
487 mac_menuterm(window, menu, item);
494 static void mac_openabout(void) {
495 DialogItemType itemtype;
502 SelectWindow(windows.about);
504 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
505 vers = (VersRecHndl)Get1Resource('vers', 1);
506 if (vers != NULL && *vers != NULL) {
507 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
508 GetDialogItem(windows.about, wiAboutVersion,
509 &itemtype, &item, &box);
510 assert(itemtype & kStaticTextDialogItem);
511 SetDialogItemText(item, longvers);
513 ShowWindow(windows.about);
517 static void mac_openlicence(void) {
520 SelectWindow(windows.licence);
522 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
523 ShowWindow(windows.licence);
527 static void mac_closewindow(WindowPtr window) {
529 switch (mac_windowtype(window)) {
531 CloseDeskAcc(((WindowPeek)window)->windowKind);
534 /* FIXME: end session and stuff */
537 windows.about = NULL;
541 windows.licence = NULL;
550 static void mac_zoomwindow(WindowPtr window, short part) {
552 /* FIXME: do something */
556 * Make the menus look right before the user gets to see them.
558 static void mac_adjustmenus(void) {
562 window = FrontWindow();
563 menu = GetMenuHandle(mApple);
565 EnableItem(menu, iAbout);
567 menu = GetMenuHandle(mFile);
569 EnableItem(menu, iNew);
571 EnableItem(menu, iClose);
573 DisableItem(menu, iClose);
574 EnableItem(menu, iQuit);
576 switch (mac_windowtype(window)) {
578 mac_adjusttermmenus(window);
581 menu = GetMenuHandle(mEdit);
582 DisableItem(menu, 0);
589 * Make sure the right cursor's being displayed.
591 static void mac_adjustcursor(RgnHandle cursrgn) {
593 WindowPtr window, front;
597 LocalToGlobal(&mouse);
598 part = FindWindow(mouse, &window);
599 front = FrontWindow();
600 if (part != inContent || window == NULL || window != front) {
601 /* Cursor isn't in the front window, so switch to arrow */
602 SetCursor(&qd.arrow);
603 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
605 DiffRgn(cursrgn, front->visRgn, cursrgn);
607 switch (mac_windowtype(window)) {
609 mac_adjusttermcursor(window, mouse, cursrgn);
612 SetCursor(&qd.arrow);
613 CopyRgn(window->visRgn, cursrgn);
619 void cleanup_exit(int status)
622 #if !TARGET_RT_MAC_CFM
623 if (mac_gestalts.encvvers != 0)
624 TerminateUnicodeConverter();
626 if (mac_gestalts.mtcpvers != 0)
631 void fatalbox(char *fmt, ...) {
636 /* We'd like stuff to be a Pascal string */
637 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
639 ParamText(stuff, NULL, NULL, NULL);
640 StopAlert(128, NULL);
644 void modalfatalbox(char *fmt, ...) {
649 /* We'd like stuff to be a Pascal string */
650 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
652 ParamText(stuff, NULL, NULL, NULL);
653 StopAlert(128, NULL);
657 /* This should only kill the current session, not the whole application. */
658 void connection_fatal(void *fontend, char *fmt, ...) {
663 /* We'd like stuff to be a Pascal string */
664 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
666 ParamText(stuff, NULL, NULL, NULL);
667 StopAlert(128, NULL);
671 /* Null SSH agent client -- never finds an agent. */
673 int agent_exists(void)
679 void agent_query(void *in, int inlen, void **out, int *outlen)
686 /* Temporary null routines for testing. */
688 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
689 char *keystr, char *fingerprint)
694 void askcipher(void *frontend, char *ciphername, int cs)
699 void old_keyfile_warning(void)
704 char *platform_default_s(char *name)
708 static char cname[256];
710 if (!strcmp(name, "Font")) {
711 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
713 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
715 GetFontName(HiWord(smfs), pname);
718 p2cstrcpy(cname, pname);
726 int platform_default_i(char *name, int def)
730 if (!strcmp(name, "FontHeight")) {
731 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
733 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
740 /* Non-raw cut and paste of line-drawing chars works badly on the
741 * current Unix stub implementation of the Unicode functions.
742 * So I'm going to temporarily set the default to raw mode so
743 * that the failure mode isn't quite so drastically horrid.
744 * When Unicode comes in, this can all be put right. */
745 if (!strcmp(name, "RawCNP"))
750 void platform_get_x11_auth(char *display, int *proto,
751 unsigned char *data, int *datalen)
753 /* SGT: I have no idea whether Mac X servers need anything here. */
758 * c-file-style: "simon"