Consequences of Simon's recent deglobalisation changes.
[u/mdw/putty] / mac / mac.c
1 /* $Id: mac.c,v 1.27 2003/01/12 16:11:27 ben Exp $ */
2 /*
3 * Copyright (c) 1999 Ben Harris
4 * All rights reserved.
5 *
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
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
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
25 * SOFTWARE.
26 */
27 /*
28 * mac.c -- miscellaneous Mac-specific routines
29 */
30
31 #include <MacTypes.h>
32 #include <Quickdraw.h>
33 #include <Fonts.h>
34 #include <MacWindows.h>
35 #include <Menus.h>
36 #include <TextEdit.h>
37 #include <Appearance.h>
38 #include <CodeFragments.h>
39 #include <Dialogs.h>
40 #include <Devices.h>
41 #include <DiskInit.h>
42 #include <Gestalt.h>
43 #include <LowMem.h>
44 #include <Resources.h>
45 #include <Script.h>
46 #include <TextCommon.h>
47 #include <ToolUtils.h>
48 #include <UnicodeConverter.h>
49
50 #include <assert.h>
51 #include <limits.h>
52 #include <stdarg.h>
53 #include <stdlib.h> /* putty.h needs size_t */
54 #include <stdio.h> /* for vsprintf */
55
56 #define PUTTY_DO_GLOBALS
57
58 #include "macresid.h"
59 #include "putty.h"
60 #include "ssh.h"
61 #include "mac.h"
62
63 QDGlobals qd;
64
65 Session *sesslist;
66
67 static int cold = 1;
68 struct mac_gestalts mac_gestalts;
69
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)
91
92 struct mac_windows {
93 WindowPtr about;
94 WindowPtr licence;
95 };
96
97 struct mac_windows windows;
98
99 int main (int argc, char **argv) {
100
101 mac_startup();
102 mac_eventloop();
103 }
104
105 #pragma noreturn (main)
106
107 static void mac_startup(void) {
108 Handle menuBar;
109 TECInfoHandle ti;
110
111 /* Init Memory Manager */
112 MaxApplZone();
113 /* Init QuickDraw */
114 InitGraf(&qd.thePort);
115 /* Init Font Manager */
116 InitFonts();
117 /* Init Window Manager */
118 InitWindows();
119 /* Init Menu Manager */
120 InitMenus();
121 /* Init TextEdit */
122 TEInit();
123 /* Init Dialog Manager */
124 InitDialogs(NULL);
125 cold = 0;
126
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;
138 else
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;
144 #endif
145 #if TARGET_CPU_68K
146 mac_gestalts.cntlattr = 0;
147 mac_gestalts.windattr = 0;
148 #else
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;
157 #endif
158 /* Text Encoding Conversion Manager? */
159 if (
160 #if TARGET_RT_MAC_CFM
161 &TECGetInfo == kUnresolvedCFragSymbolAddress ||
162 #else
163 InitializeUnicodeConverter(NULL) != noErr ||
164 #endif
165 TECGetInfo(&ti) != noErr)
166 mac_gestalts.encvvers = 0;
167 else {
168 mac_gestalts.encvvers = (*ti)->tecVersion;
169 mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
170 DisposeHandle((Handle)ti);
171 }
172 /* OpenTransport? */
173 if (Gestalt(gestaltOpenTpt, &mac_gestalts.otptattr) != noErr ||
174 (mac_gestalts.otptattr & gestaltOpenTptTCPPresentMask) == 0 ||
175 ot_init() != noErr)
176 mac_gestalts.otptattr = 0;
177 if (mac_gestalts.otptattr == 0) {
178 /* MacTCP? */
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;
184 }
185 } else
186 mac_gestalts.mtcpvers = 0;
187
188 /* We've been tested with the Appearance Manager */
189 if (mac_gestalts.apprvers != 0)
190 RegisterAppearanceClient();
191
192 menuBar = GetNewMBar(128);
193 if (menuBar == NULL)
194 fatalbox("Unable to create menu bar.");
195 SetMenuBar(menuBar);
196 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
197 mac_adjustmenus();
198 DrawMenuBar();
199 InitCursor();
200 windows.about = NULL;
201 windows.licence = NULL;
202
203 default_protocol = DEFAULT_PROTOCOL;
204 default_port = DEFAULT_PORT;
205 flags = FLAG_INTERACTIVE;
206
207 {
208 short vol;
209 long dirid;
210
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);
216 }
217 }
218 init_ucs();
219 }
220
221 static void mac_eventloop(void) {
222 Boolean gotevent;
223 EventRecord event;
224 RgnHandle cursrgn;
225
226 cursrgn = NewRgn();
227 for (;;) {
228 mac_adjustcursor(cursrgn);
229 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
230 mac_adjustcursor(cursrgn);
231 if (gotevent)
232 mac_event(&event);
233 if (mac_gestalts.mtcpvers != 0)
234 mactcp_poll();
235 if (mac_gestalts.otptattr != 0)
236 ot_poll();
237 mac_pollterm();
238 }
239 DisposeRgn(cursrgn);
240 }
241
242 static void mac_event(EventRecord *event) {
243 short part;
244 WindowPtr window;
245 Point pt;
246
247 switch (event->what) {
248 case mouseDown:
249 part = FindWindow(event->where, &window);
250 switch (part) {
251 case inMenuBar:
252 mac_adjustmenus();
253 mac_menucommand(MenuSelect(event->where));
254 break;
255 case inSysWindow:
256 SystemClick(event, window);
257 break;
258 case inContent:
259 if (window != FrontWindow())
260 /* XXX: check for movable modal dboxes? */
261 SelectWindow(window);
262 else
263 mac_contentclick(window, event);
264 break;
265 case inGoAway:
266 if (TrackGoAway(window, event->where))
267 mac_closewindow(window);
268 break;
269 case inDrag:
270 /* XXX: moveable modal check? */
271 DragWindow(window, event->where, &qd.screenBits.bounds);
272 break;
273 case inGrow:
274 mac_growwindow(window, event);
275 break;
276 case inZoomIn:
277 case inZoomOut:
278 if (TrackBox(window, event->where, part))
279 mac_zoomwindow(window, part);
280 break;
281 }
282 break;
283 case keyDown:
284 case autoKey:
285 mac_keypress(event);
286 break;
287 case activateEvt:
288 mac_activatewindow((WindowPtr)event->message, event);
289 break;
290 case updateEvt:
291 mac_updatewindow((WindowPtr)event->message);
292 break;
293 case diskEvt:
294 if (HiWord(event->message) != noErr) {
295 SetPt(&pt, 120, 120);
296 DIBadMount(pt, event->message);
297 }
298 break;
299 }
300 }
301
302 static void mac_contentclick(WindowPtr window, EventRecord *event) {
303 short item;
304
305 switch (mac_windowtype(window)) {
306 case wTerminal:
307 mac_clickterm(window, event);
308 break;
309 case wAbout:
310 if (DialogSelect(event, &window, &item))
311 switch (item) {
312 case wiAboutLicence:
313 mac_openlicence();
314 break;
315 }
316 break;
317 case wSettings:
318 mac_clickdlg(window, event);
319 break;
320 }
321 }
322
323 static void mac_growwindow(WindowPtr window, EventRecord *event) {
324
325 switch (mac_windowtype(window)) {
326 case wTerminal:
327 mac_growterm(window, event);
328 }
329 }
330
331 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
332 int active;
333
334 active = (event->modifiers & activeFlag) != 0;
335 mac_adjustmenus();
336 switch (mac_windowtype(window)) {
337 case wTerminal:
338 mac_activateterm(window, active);
339 break;
340 case wSettings:
341 mac_activatedlg(window, event);
342 break;
343 case wAbout:
344 mac_activateabout(window, event);
345 break;
346 }
347 }
348
349 static void mac_activateabout(WindowPtr window, EventRecord *event) {
350 DialogItemType itemtype;
351 Handle itemhandle;
352 short item;
353 Rect itemrect;
354 int active;
355
356 active = (event->modifiers & activeFlag) != 0;
357 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
358 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
359 DialogSelect(event, &window, &item);
360 }
361
362 static void mac_updatewindow(WindowPtr window) {
363
364 switch (mac_windowtype(window)) {
365 case wTerminal:
366 mac_updateterm(window);
367 break;
368 case wAbout:
369 case wSettings:
370 BeginUpdate(window);
371 UpdateDialog(window, window->visRgn);
372 EndUpdate(window);
373 break;
374 case wLicence:
375 mac_updatelicence(window);
376 break;
377 }
378 }
379
380 static void mac_updatelicence(WindowPtr window)
381 {
382 Handle h;
383 int len;
384 long fondsize;
385
386 SetPort(window);
387 BeginUpdate(window);
388 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
389 TextFont(HiWord(fondsize));
390 TextSize(LoWord(fondsize));
391 h = Get1Resource('TEXT', wLicence);
392 len = GetResourceSizeOnDisk(h);
393 if (h != NULL) {
394 HLock(h);
395 TETextBox(*h, len, &window->portRect, teFlushDefault);
396 HUnlock(h);
397 }
398 EndUpdate(window);
399 }
400
401 /*
402 * Work out what kind of window we're dealing with.
403 * Concept shamelessly nicked from SurfWriter.
404 */
405 static int mac_windowtype(WindowPtr window) {
406 int kind;
407 long refcon;
408
409 if (window == NULL)
410 return wNone;
411 kind = ((WindowPeek)window)->windowKind;
412 if (kind < 0)
413 return wDA;
414 if (GetWVariant(window) == zoomDocProc)
415 return wTerminal;
416 refcon = GetWRefCon(window);
417 if (refcon < 1024)
418 return refcon;
419 else
420 return wSettings;
421 }
422
423 /*
424 * Handle a key press
425 */
426 static void mac_keypress(EventRecord *event) {
427 WindowPtr window;
428
429 window = FrontWindow();
430 /*
431 * Check for a command-key combination, but ignore it if it counts
432 * as a meta-key combination and we're in a terminal window.
433 */
434 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
435 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
436 mac_windowtype(window) == wTerminal)*/) {
437 mac_adjustmenus();
438 mac_menucommand(MenuKey(event->message & charCodeMask));
439 } else {
440 switch (mac_windowtype(window)) {
441 case wTerminal:
442 mac_keyterm(window, event);
443 break;
444 }
445 }
446 }
447
448 static void mac_menucommand(long result) {
449 short menu, item;
450 Str255 da;
451 WindowPtr window;
452
453 menu = HiWord(result);
454 item = LoWord(result);
455 window = FrontWindow();
456 /* Things which do the same whatever window we're in. */
457 switch (menu) {
458 case mApple:
459 switch (item) {
460 case iAbout:
461 mac_openabout();
462 goto done;
463 default:
464 GetMenuItemText(GetMenuHandle(mApple), item, da);
465 OpenDeskAcc(da);
466 goto done;
467 }
468 break;
469 case mFile:
470 switch (item) {
471 case iNew:
472 mac_newsession();
473 goto done;
474 case iOpen:
475 mac_opensession();
476 goto done;
477 case iClose:
478 mac_closewindow(window);
479 goto done;
480 case iQuit:
481 cleanup_exit(0);
482 goto done;
483 }
484 break;
485 }
486 /* If we get here, handling is up to window-specific code. */
487 switch (mac_windowtype(window)) {
488 case wTerminal:
489 mac_menuterm(window, menu, item);
490 break;
491 }
492 done:
493 HiliteMenu(0);
494 }
495
496 static void mac_openabout(void) {
497 DialogItemType itemtype;
498 Handle item;
499 VersRecHndl vers;
500 Rect box;
501 StringPtr longvers;
502
503 if (windows.about)
504 SelectWindow(windows.about);
505 else {
506 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
507 vers = (VersRecHndl)Get1Resource('vers', 1);
508 if (vers != NULL && *vers != NULL) {
509 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
510 GetDialogItem(windows.about, wiAboutVersion,
511 &itemtype, &item, &box);
512 assert(itemtype & kStaticTextDialogItem);
513 SetDialogItemText(item, longvers);
514 }
515 ShowWindow(windows.about);
516 }
517 }
518
519 static void mac_openlicence(void) {
520
521 if (windows.licence)
522 SelectWindow(windows.licence);
523 else {
524 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
525 ShowWindow(windows.licence);
526 }
527 }
528
529 static void mac_closewindow(WindowPtr window) {
530
531 switch (mac_windowtype(window)) {
532 case wDA:
533 CloseDeskAcc(((WindowPeek)window)->windowKind);
534 break;
535 case wTerminal:
536 /* FIXME: end session and stuff */
537 break;
538 case wAbout:
539 windows.about = NULL;
540 CloseWindow(window);
541 break;
542 case wLicence:
543 windows.licence = NULL;
544 CloseWindow(window);
545 break;
546 default:
547 CloseWindow(window);
548 break;
549 }
550 }
551
552 static void mac_zoomwindow(WindowPtr window, short part) {
553
554 /* FIXME: do something */
555 }
556
557 /*
558 * Make the menus look right before the user gets to see them.
559 */
560 static void mac_adjustmenus(void) {
561 WindowPtr window;
562 MenuHandle menu;
563
564 window = FrontWindow();
565 menu = GetMenuHandle(mApple);
566 EnableItem(menu, 0);
567 EnableItem(menu, iAbout);
568
569 menu = GetMenuHandle(mFile);
570 EnableItem(menu, 0);
571 EnableItem(menu, iNew);
572 if (window != NULL)
573 EnableItem(menu, iClose);
574 else
575 DisableItem(menu, iClose);
576 EnableItem(menu, iQuit);
577
578 switch (mac_windowtype(window)) {
579 case wTerminal:
580 mac_adjusttermmenus(window);
581 break;
582 default:
583 menu = GetMenuHandle(mEdit);
584 DisableItem(menu, 0);
585 break;
586 }
587 DrawMenuBar();
588 }
589
590 /*
591 * Make sure the right cursor's being displayed.
592 */
593 static void mac_adjustcursor(RgnHandle cursrgn) {
594 Point mouse;
595 WindowPtr window, front;
596 short part;
597
598 GetMouse(&mouse);
599 LocalToGlobal(&mouse);
600 part = FindWindow(mouse, &window);
601 front = FrontWindow();
602 if (part != inContent || window == NULL || window != front) {
603 /* Cursor isn't in the front window, so switch to arrow */
604 SetCursor(&qd.arrow);
605 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
606 if (front != NULL)
607 DiffRgn(cursrgn, front->visRgn, cursrgn);
608 } else {
609 switch (mac_windowtype(window)) {
610 case wTerminal:
611 mac_adjusttermcursor(window, mouse, cursrgn);
612 break;
613 default:
614 SetCursor(&qd.arrow);
615 CopyRgn(window->visRgn, cursrgn);
616 break;
617 }
618 }
619 }
620
621 void cleanup_exit(int status)
622 {
623
624 #if !TARGET_RT_MAC_CFM
625 if (mac_gestalts.encvvers != 0)
626 TerminateUnicodeConverter();
627 #endif
628 sk_cleanup();
629 exit(status);
630 }
631
632 void fatalbox(char *fmt, ...) {
633 va_list ap;
634 Str255 stuff;
635
636 va_start(ap, fmt);
637 /* We'd like stuff to be a Pascal string */
638 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
639 va_end(ap);
640 ParamText(stuff, NULL, NULL, NULL);
641 StopAlert(128, NULL);
642 cleanup_exit(1);
643 }
644
645 void modalfatalbox(char *fmt, ...) {
646 va_list ap;
647 Str255 stuff;
648
649 va_start(ap, fmt);
650 /* We'd like stuff to be a Pascal string */
651 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
652 va_end(ap);
653 ParamText(stuff, NULL, NULL, NULL);
654 StopAlert(128, NULL);
655 cleanup_exit(1);
656 }
657
658 /* This should only kill the current session, not the whole application. */
659 void connection_fatal(void *fontend, char *fmt, ...) {
660 va_list ap;
661 Str255 stuff;
662
663 va_start(ap, fmt);
664 /* We'd like stuff to be a Pascal string */
665 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
666 va_end(ap);
667 ParamText(stuff, NULL, NULL, NULL);
668 StopAlert(128, NULL);
669 cleanup_exit(1);
670 }
671
672 /* Null SSH agent client -- never finds an agent. */
673
674 int agent_exists(void)
675 {
676
677 return FALSE;
678 }
679
680 void agent_query(void *in, int inlen, void **out, int *outlen)
681 {
682
683 *out = NULL;
684 *outlen = 0;
685 }
686
687 /* Temporary null routines for testing. */
688
689 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
690 char *keystr, char *fingerprint)
691 {
692
693 }
694
695 void askcipher(void *frontend, char *ciphername, int cs)
696 {
697
698 }
699
700 void old_keyfile_warning(void)
701 {
702
703 }
704
705 char *platform_default_s(char *name)
706 {
707 long smfs;
708 Str255 pname;
709 static char cname[256];
710
711 if (!strcmp(name, "Font")) {
712 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
713 if (smfs == 0)
714 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
715 if (smfs != 0) {
716 GetFontName(HiWord(smfs), pname);
717 if (pname[0] == 0)
718 return "Monaco";
719 p2cstrcpy(cname, pname);
720 return cname;
721 } else
722 return "Monaco";
723 }
724 return NULL;
725 }
726
727 int platform_default_i(char *name, int def)
728 {
729 long smfs;
730
731 if (!strcmp(name, "FontHeight")) {
732 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
733 if (smfs == 0)
734 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
735 if (smfs != 0)
736 return LoWord(smfs);
737 else
738 return 9;
739 }
740
741 /* Non-raw cut and paste of line-drawing chars works badly on the
742 * current Unix stub implementation of the Unicode functions.
743 * So I'm going to temporarily set the default to raw mode so
744 * that the failure mode isn't quite so drastically horrid.
745 * When Unicode comes in, this can all be put right. */
746 if (!strcmp(name, "RawCNP"))
747 return 1;
748 return def;
749 }
750
751 void platform_get_x11_auth(char *display, int *proto,
752 unsigned char *data, int *datalen)
753 {
754 /* SGT: I have no idea whether Mac X servers need anything here. */
755 }
756
757 /*
758 * Local Variables:
759 * c-file-style: "simon"
760 * End:
761 */