]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac/mac.c
632b6a41488cfee91512e974e1bd6ed6504654df
[PuTTY.git] / mac / mac.c
1 /* $Id: mac.c,v 1.24 2003/01/11 19:43:59 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     /* MacTCP? */
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;
178     }
179
180     /* We've been tested with the Appearance Manager */
181     if (mac_gestalts.apprvers != 0)
182         RegisterAppearanceClient();
183
184     menuBar = GetNewMBar(128);
185     if (menuBar == NULL)
186         fatalbox("Unable to create menu bar.");
187     SetMenuBar(menuBar);
188     AppendResMenu(GetMenuHandle(mApple), 'DRVR');
189     mac_adjustmenus();
190     DrawMenuBar();
191     InitCursor();
192     windows.about = NULL;
193     windows.licence = NULL;
194
195     default_protocol = DEFAULT_PROTOCOL;
196     default_port = DEFAULT_PORT;
197     flags = FLAG_INTERACTIVE;
198
199     /*
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.
204      */
205     do_defaults(NULL, &cfg);
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         mac_pollterm();
236     }
237     DisposeRgn(cursrgn);
238 }
239
240 static void mac_event(EventRecord *event) {
241     short part;
242     WindowPtr window;
243     Point pt;
244
245     switch (event->what) {
246       case mouseDown:
247         part = FindWindow(event->where, &window);
248         switch (part) {
249           case inMenuBar:
250             mac_adjustmenus();
251             mac_menucommand(MenuSelect(event->where));
252             break;
253           case inSysWindow:
254             SystemClick(event, window);
255             break;
256           case inContent:
257             if (window != FrontWindow())
258                 /* XXX: check for movable modal dboxes? */
259                 SelectWindow(window);
260             else
261                 mac_contentclick(window, event);
262             break;
263           case inGoAway:
264             if (TrackGoAway(window, event->where))
265                 mac_closewindow(window);
266             break;
267           case inDrag:
268             /* XXX: moveable modal check? */
269             DragWindow(window, event->where, &qd.screenBits.bounds);
270             break;
271           case inGrow:
272             mac_growwindow(window, event);
273             break;
274           case inZoomIn:
275           case inZoomOut:
276             if (TrackBox(window, event->where, part))
277                 mac_zoomwindow(window, part);
278             break;
279         }
280         break;
281       case keyDown:
282       case autoKey:
283         mac_keypress(event);
284         break;
285       case activateEvt:
286         mac_activatewindow((WindowPtr)event->message, event);
287         break;
288       case updateEvt:
289         mac_updatewindow((WindowPtr)event->message);
290         break;
291       case diskEvt:
292         if (HiWord(event->message) != noErr) {
293             SetPt(&pt, 120, 120);
294             DIBadMount(pt, event->message);
295         }
296         break;
297     }
298 }
299
300 static void mac_contentclick(WindowPtr window, EventRecord *event) {
301     short item;
302
303     switch (mac_windowtype(window)) {
304       case wTerminal:
305         mac_clickterm(window, event);
306         break;
307       case wAbout:
308         if (DialogSelect(event, &window, &item))
309             switch (item) {
310               case wiAboutLicence:
311                 mac_openlicence();
312                 break;
313             }
314         break;
315       case wSettings:
316         mac_clickdlg(window, event);
317         break;
318     }
319 }
320
321 static void mac_growwindow(WindowPtr window, EventRecord *event) {
322
323     switch (mac_windowtype(window)) {
324       case wTerminal:
325         mac_growterm(window, event);
326     }
327 }
328
329 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
330     int active;
331
332     active = (event->modifiers & activeFlag) != 0;
333     mac_adjustmenus();
334     switch (mac_windowtype(window)) {
335       case wTerminal:
336         mac_activateterm(window, active);
337         break;
338       case wSettings:
339         mac_activatedlg(window, event);
340         break;
341       case wAbout:
342         mac_activateabout(window, event);
343         break;
344     }
345 }
346
347 static void mac_activateabout(WindowPtr window, EventRecord *event) {
348     DialogItemType itemtype;
349     Handle itemhandle;
350     short item;
351     Rect itemrect;
352     int active;
353
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);
358 }
359
360 static void mac_updatewindow(WindowPtr window) {
361
362     switch (mac_windowtype(window)) {
363       case wTerminal:
364         mac_updateterm(window);
365         break;
366       case wAbout:
367       case wSettings:
368         BeginUpdate(window);
369         UpdateDialog(window, window->visRgn);
370         EndUpdate(window);
371         break;
372       case wLicence:
373         mac_updatelicence(window);
374         break;
375     }
376 }
377
378 static void mac_updatelicence(WindowPtr window)
379 {
380     Handle h;
381     int len;
382     long fondsize;
383
384     SetPort(window);
385     BeginUpdate(window);
386     fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
387     TextFont(HiWord(fondsize));
388     TextSize(LoWord(fondsize));
389     h = Get1Resource('TEXT', wLicence);
390     len = GetResourceSizeOnDisk(h);
391     if (h != NULL) {
392         HLock(h);
393         TETextBox(*h, len, &window->portRect, teFlushDefault);
394         HUnlock(h);
395     }
396     EndUpdate(window);
397 }
398
399 /*
400  * Work out what kind of window we're dealing with.
401  * Concept shamelessly nicked from SurfWriter.
402  */
403 static int mac_windowtype(WindowPtr window) {
404     int kind;
405     long refcon;
406
407     if (window == NULL)
408         return wNone;
409     kind = ((WindowPeek)window)->windowKind;
410     if (kind < 0)
411         return wDA;
412     if (GetWVariant(window) == zoomDocProc)
413         return wTerminal;
414     refcon = GetWRefCon(window);
415     if (refcon < 1024)
416         return refcon;
417     else
418         return wSettings;
419 }
420
421 /*
422  * Handle a key press
423  */
424 static void mac_keypress(EventRecord *event) {
425     WindowPtr window;
426
427     window = FrontWindow();
428     /*
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.
431      */
432     if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
433         !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
434             mac_windowtype(window) == wTerminal)*/) {
435         mac_adjustmenus();
436         mac_menucommand(MenuKey(event->message & charCodeMask));
437     } else {
438         switch (mac_windowtype(window)) {
439           case wTerminal:
440             mac_keyterm(window, event);
441             break;
442         }
443     }       
444 }
445
446 static void mac_menucommand(long result) {
447     short menu, item;
448     Str255 da;
449     WindowPtr window;
450
451     menu = HiWord(result);
452     item = LoWord(result);
453     window = FrontWindow();
454     /* Things which do the same whatever window we're in. */
455     switch (menu) {
456       case mApple:
457         switch (item) {
458           case iAbout:
459             mac_openabout();
460             goto done;
461           default:
462             GetMenuItemText(GetMenuHandle(mApple), item, da);
463             OpenDeskAcc(da);
464             goto done;
465         }
466         break;
467       case mFile:
468         switch (item) {
469           case iNew:
470             mac_newsession();
471             goto done;
472           case iOpen:
473             mac_opensession();
474             goto done;
475           case iClose:
476             mac_closewindow(window);
477             goto done;
478           case iQuit:
479             cleanup_exit(0);
480             goto done;
481         }
482         break;
483     }
484     /* If we get here, handling is up to window-specific code. */
485     switch (mac_windowtype(window)) {
486       case wTerminal:
487         mac_menuterm(window, menu, item);
488         break;
489     }
490   done:
491     HiliteMenu(0);
492 }
493
494 static void mac_openabout(void) {
495     DialogItemType itemtype;
496     Handle item;
497     VersRecHndl vers;
498     Rect box;
499     StringPtr longvers;
500
501     if (windows.about)
502         SelectWindow(windows.about);
503     else {
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);
512         }
513         ShowWindow(windows.about);
514     }
515 }
516
517 static void mac_openlicence(void) {
518
519     if (windows.licence)
520         SelectWindow(windows.licence);
521     else {
522         windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
523         ShowWindow(windows.licence);
524     }
525 }
526
527 static void mac_closewindow(WindowPtr window) {
528
529     switch (mac_windowtype(window)) {
530       case wDA:
531         CloseDeskAcc(((WindowPeek)window)->windowKind);
532         break;
533       case wTerminal:
534         /* FIXME: end session and stuff */
535         break;
536       case wAbout:
537         windows.about = NULL;
538         CloseWindow(window);
539         break;
540       case wLicence:
541         windows.licence = NULL;
542         CloseWindow(window);
543         break;
544       default:
545         CloseWindow(window);
546         break;
547     }
548 }
549
550 static void mac_zoomwindow(WindowPtr window, short part) {
551
552     /* FIXME: do something */
553 }
554
555 /*
556  * Make the menus look right before the user gets to see them.
557  */
558 static void mac_adjustmenus(void) {
559     WindowPtr window;
560     MenuHandle menu;
561
562     window = FrontWindow();
563     menu = GetMenuHandle(mApple);
564     EnableItem(menu, 0);
565     EnableItem(menu, iAbout);
566
567     menu = GetMenuHandle(mFile);
568     EnableItem(menu, 0);
569     EnableItem(menu, iNew);
570     if (window != NULL)
571         EnableItem(menu, iClose);
572     else
573         DisableItem(menu, iClose);
574     EnableItem(menu, iQuit);
575
576     switch (mac_windowtype(window)) {
577       case wTerminal:
578         mac_adjusttermmenus(window);
579         break;
580       default:
581         menu = GetMenuHandle(mEdit);
582         DisableItem(menu, 0);
583         break;
584     }
585     DrawMenuBar();
586 }
587
588 /*
589  * Make sure the right cursor's being displayed.
590  */
591 static void mac_adjustcursor(RgnHandle cursrgn) {
592     Point mouse;
593     WindowPtr window, front;
594     short part;
595
596     GetMouse(&mouse);
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);
604         if (front != NULL)
605             DiffRgn(cursrgn, front->visRgn, cursrgn);
606     } else {
607         switch (mac_windowtype(window)) {
608           case wTerminal:
609             mac_adjusttermcursor(window, mouse, cursrgn);
610             break;
611           default:
612             SetCursor(&qd.arrow);
613             CopyRgn(window->visRgn, cursrgn);
614             break;
615         }
616     }
617 }
618
619 void cleanup_exit(int status)
620 {
621
622 #if !TARGET_RT_MAC_CFM
623     if (mac_gestalts.encvvers != 0)
624         TerminateUnicodeConverter();
625 #endif
626     if (mac_gestalts.mtcpvers != 0)
627         mactcp_shutdown();
628     exit(status);
629 }
630
631 void fatalbox(char *fmt, ...) {
632     va_list ap;
633     Str255 stuff;
634     
635     va_start(ap, fmt);
636     /* We'd like stuff to be a Pascal string */
637     stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
638     va_end(ap);
639     ParamText(stuff, NULL, NULL, NULL);
640     StopAlert(128, NULL);
641     exit(1);
642 }
643
644 void modalfatalbox(char *fmt, ...) {
645     va_list ap;
646     Str255 stuff;
647     
648     va_start(ap, fmt);
649     /* We'd like stuff to be a Pascal string */
650     stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
651     va_end(ap);
652     ParamText(stuff, NULL, NULL, NULL);
653     StopAlert(128, NULL);
654     exit(1);
655 }
656
657 /* This should only kill the current session, not the whole application. */
658 void connection_fatal(void *fontend, char *fmt, ...) {
659     va_list ap;
660     Str255 stuff;
661     
662     va_start(ap, fmt);
663     /* We'd like stuff to be a Pascal string */
664     stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
665     va_end(ap);
666     ParamText(stuff, NULL, NULL, NULL);
667     StopAlert(128, NULL);
668     exit(1);
669 }
670
671 /* Null SSH agent client -- never finds an agent. */
672
673 int agent_exists(void)
674 {
675
676     return FALSE;
677 }
678
679 void agent_query(void *in, int inlen, void **out, int *outlen)
680 {
681
682     *out = NULL;
683     *outlen = 0;
684 }
685
686 /* Temporary null routines for testing. */
687
688 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
689                          char *keystr, char *fingerprint)
690 {
691
692 }
693
694 void askcipher(void *frontend, char *ciphername, int cs)
695 {
696
697 }
698
699 void old_keyfile_warning(void)
700 {
701
702 }
703
704 char *platform_default_s(char *name)
705 {
706     long smfs;
707     Str255 pname;
708     static char cname[256];
709
710     if (!strcmp(name, "Font")) {
711         smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
712         if (smfs == 0)
713             smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
714         if (smfs != 0) {
715             GetFontName(HiWord(smfs), pname);
716             if (pname[0] == 0)
717                 return "Monaco";
718             p2cstrcpy(cname, pname);
719             return cname;
720         } else
721             return "Monaco";
722     }
723     return NULL;
724 }
725
726 int platform_default_i(char *name, int def)
727 {
728     long smfs;
729
730     if (!strcmp(name, "FontHeight")) {
731         smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
732         if (smfs == 0)
733             smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
734         if (smfs != 0)
735             return LoWord(smfs);
736         else
737             return 9;
738     }
739
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"))
746         return 1;
747     return def;
748 }
749
750 void platform_get_x11_auth(char *display, int *proto,
751                            unsigned char *data, int *datalen)
752 {
753     /* SGT: I have no idea whether Mac X servers need anything here. */
754 }
755
756 /*
757  * Local Variables:
758  * c-file-style: "simon"
759  * End:
760  */