]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac/mac.c
6335aabef2430c45fa106f1f5b55cdf41ae72f2a
[PuTTY.git] / mac / mac.c
1 /* $Id: mac.c,v 1.29 2003/01/14 19:42:00 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 }
219
220 static void mac_eventloop(void) {
221     Boolean gotevent;
222     EventRecord event;
223     RgnHandle cursrgn;
224
225     cursrgn = NewRgn();
226     for (;;) {
227         mac_adjustcursor(cursrgn);
228         gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
229         mac_adjustcursor(cursrgn);
230         if (gotevent)
231             mac_event(&event);
232         if (mac_gestalts.mtcpvers != 0)
233             mactcp_poll();
234         if (mac_gestalts.otptattr != 0)
235             ot_poll();
236         mac_pollterm();
237     }
238     DisposeRgn(cursrgn);
239 }
240
241 static void mac_event(EventRecord *event) {
242     short part;
243     WindowPtr window;
244     Point pt;
245
246     switch (event->what) {
247       case mouseDown:
248         part = FindWindow(event->where, &window);
249         switch (part) {
250           case inMenuBar:
251             mac_adjustmenus();
252             mac_menucommand(MenuSelect(event->where));
253             break;
254           case inSysWindow:
255             SystemClick(event, window);
256             break;
257           case inContent:
258             if (window != FrontWindow())
259                 /* XXX: check for movable modal dboxes? */
260                 SelectWindow(window);
261             else
262                 mac_contentclick(window, event);
263             break;
264           case inGoAway:
265             if (TrackGoAway(window, event->where))
266                 mac_closewindow(window);
267             break;
268           case inDrag:
269             /* XXX: moveable modal check? */
270             DragWindow(window, event->where, &qd.screenBits.bounds);
271             break;
272           case inGrow:
273             mac_growwindow(window, event);
274             break;
275           case inZoomIn:
276           case inZoomOut:
277             if (TrackBox(window, event->where, part))
278                 mac_zoomwindow(window, part);
279             break;
280         }
281         break;
282       case keyDown:
283       case autoKey:
284         mac_keypress(event);
285         break;
286       case activateEvt:
287         mac_activatewindow((WindowPtr)event->message, event);
288         break;
289       case updateEvt:
290         mac_updatewindow((WindowPtr)event->message);
291         break;
292       case diskEvt:
293         if (HiWord(event->message) != noErr) {
294             SetPt(&pt, 120, 120);
295             DIBadMount(pt, event->message);
296         }
297         break;
298     }
299 }
300
301 static void mac_contentclick(WindowPtr window, EventRecord *event) {
302     short item;
303
304     switch (mac_windowtype(window)) {
305       case wTerminal:
306         mac_clickterm(window, event);
307         break;
308       case wAbout:
309         if (DialogSelect(event, &window, &item))
310             switch (item) {
311               case wiAboutLicence:
312                 mac_openlicence();
313                 break;
314             }
315         break;
316       case wSettings:
317         mac_clickdlg(window, event);
318         break;
319     }
320 }
321
322 static void mac_growwindow(WindowPtr window, EventRecord *event) {
323
324     switch (mac_windowtype(window)) {
325       case wTerminal:
326         mac_growterm(window, event);
327     }
328 }
329
330 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
331     int active;
332
333     active = (event->modifiers & activeFlag) != 0;
334     mac_adjustmenus();
335     switch (mac_windowtype(window)) {
336       case wTerminal:
337         mac_activateterm(window, active);
338         break;
339       case wSettings:
340         mac_activatedlg(window, event);
341         break;
342       case wAbout:
343         mac_activateabout(window, event);
344         break;
345     }
346 }
347
348 static void mac_activateabout(WindowPtr window, EventRecord *event) {
349     DialogItemType itemtype;
350     Handle itemhandle;
351     short item;
352     Rect itemrect;
353     int active;
354
355     active = (event->modifiers & activeFlag) != 0;
356     GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
357     HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
358     DialogSelect(event, &window, &item);
359 }
360
361 static void mac_updatewindow(WindowPtr window) {
362
363     switch (mac_windowtype(window)) {
364       case wTerminal:
365         mac_updateterm(window);
366         break;
367       case wAbout:
368       case wSettings:
369         BeginUpdate(window);
370         UpdateDialog(window, window->visRgn);
371         EndUpdate(window);
372         break;
373       case wLicence:
374         mac_updatelicence(window);
375         break;
376     }
377 }
378
379 static void mac_updatelicence(WindowPtr window)
380 {
381     Handle h;
382     int len;
383     long fondsize;
384
385     SetPort(window);
386     BeginUpdate(window);
387     fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
388     TextFont(HiWord(fondsize));
389     TextSize(LoWord(fondsize));
390     h = Get1Resource('TEXT', wLicence);
391     len = GetResourceSizeOnDisk(h);
392     if (h != NULL) {
393         HLock(h);
394         TETextBox(*h, len, &window->portRect, teFlushDefault);
395         HUnlock(h);
396     }
397     EndUpdate(window);
398 }
399
400 /*
401  * Work out what kind of window we're dealing with.
402  * Concept shamelessly nicked from SurfWriter.
403  */
404 static int mac_windowtype(WindowPtr window) {
405     int kind;
406     long refcon;
407
408     if (window == NULL)
409         return wNone;
410     kind = ((WindowPeek)window)->windowKind;
411     if (kind < 0)
412         return wDA;
413     if (GetWVariant(window) == zoomDocProc)
414         return wTerminal;
415     refcon = GetWRefCon(window);
416     if (refcon < 1024)
417         return refcon;
418     else
419         return wSettings;
420 }
421
422 /*
423  * Handle a key press
424  */
425 static void mac_keypress(EventRecord *event) {
426     WindowPtr window;
427
428     window = FrontWindow();
429     /*
430      * Check for a command-key combination, but ignore it if it counts
431      * as a meta-key combination and we're in a terminal window.
432      */
433     if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
434         !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
435             mac_windowtype(window) == wTerminal)*/) {
436         mac_adjustmenus();
437         mac_menucommand(MenuKey(event->message & charCodeMask));
438     } else {
439         switch (mac_windowtype(window)) {
440           case wTerminal:
441             mac_keyterm(window, event);
442             break;
443         }
444     }       
445 }
446
447 static void mac_menucommand(long result) {
448     short menu, item;
449     Str255 da;
450     WindowPtr window;
451
452     menu = HiWord(result);
453     item = LoWord(result);
454     window = FrontWindow();
455     /* Things which do the same whatever window we're in. */
456     switch (menu) {
457       case mApple:
458         switch (item) {
459           case iAbout:
460             mac_openabout();
461             goto done;
462           default:
463             GetMenuItemText(GetMenuHandle(mApple), item, da);
464             OpenDeskAcc(da);
465             goto done;
466         }
467         break;
468       case mFile:
469         switch (item) {
470           case iNew:
471             mac_newsession();
472             goto done;
473           case iOpen:
474             mac_opensession();
475             goto done;
476           case iClose:
477             mac_closewindow(window);
478             goto done;
479           case iQuit:
480             cleanup_exit(0);
481             goto done;
482         }
483         break;
484     }
485     /* If we get here, handling is up to window-specific code. */
486     switch (mac_windowtype(window)) {
487       case wTerminal:
488         mac_menuterm(window, menu, item);
489         break;
490     }
491   done:
492     HiliteMenu(0);
493 }
494
495 static void mac_openabout(void) {
496     DialogItemType itemtype;
497     Handle item;
498     VersRecHndl vers;
499     Rect box;
500     StringPtr longvers;
501
502     if (windows.about)
503         SelectWindow(windows.about);
504     else {
505         windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
506         vers = (VersRecHndl)Get1Resource('vers', 1);
507         if (vers != NULL && *vers != NULL) {
508             longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
509             GetDialogItem(windows.about, wiAboutVersion,
510                           &itemtype, &item, &box);
511             assert(itemtype & kStaticTextDialogItem);
512             SetDialogItemText(item, longvers);
513         }
514         ShowWindow(windows.about);
515     }
516 }
517
518 static void mac_openlicence(void) {
519
520     if (windows.licence)
521         SelectWindow(windows.licence);
522     else {
523         windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
524         ShowWindow(windows.licence);
525     }
526 }
527
528 static void mac_closewindow(WindowPtr window) {
529
530     switch (mac_windowtype(window)) {
531       case wDA:
532         CloseDeskAcc(((WindowPeek)window)->windowKind);
533         break;
534       case wTerminal:
535         /* FIXME: end session and stuff */
536         break;
537       case wAbout:
538         windows.about = NULL;
539         CloseWindow(window);
540         break;
541       case wLicence:
542         windows.licence = NULL;
543         CloseWindow(window);
544         break;
545       default:
546         CloseWindow(window);
547         break;
548     }
549 }
550
551 static void mac_zoomwindow(WindowPtr window, short part) {
552
553     /* FIXME: do something */
554 }
555
556 /*
557  * Make the menus look right before the user gets to see them.
558  */
559 static void mac_adjustmenus(void) {
560     WindowPtr window;
561     MenuHandle menu;
562
563     window = FrontWindow();
564     menu = GetMenuHandle(mApple);
565     EnableItem(menu, 0);
566     EnableItem(menu, iAbout);
567
568     menu = GetMenuHandle(mFile);
569     EnableItem(menu, 0);
570     EnableItem(menu, iNew);
571     if (window != NULL)
572         EnableItem(menu, iClose);
573     else
574         DisableItem(menu, iClose);
575     EnableItem(menu, iQuit);
576
577     switch (mac_windowtype(window)) {
578       case wTerminal:
579         mac_adjusttermmenus(window);
580         break;
581       default:
582         menu = GetMenuHandle(mEdit);
583         DisableItem(menu, 0);
584         break;
585     }
586     DrawMenuBar();
587 }
588
589 /*
590  * Make sure the right cursor's being displayed.
591  */
592 static void mac_adjustcursor(RgnHandle cursrgn) {
593     Point mouse;
594     WindowPtr window, front;
595     short part;
596
597     GetMouse(&mouse);
598     LocalToGlobal(&mouse);
599     part = FindWindow(mouse, &window);
600     front = FrontWindow();
601     if (part != inContent || window == NULL || window != front) {
602         /* Cursor isn't in the front window, so switch to arrow */
603         SetCursor(&qd.arrow);
604         SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
605         if (front != NULL)
606             DiffRgn(cursrgn, front->visRgn, cursrgn);
607     } else {
608         switch (mac_windowtype(window)) {
609           case wTerminal:
610             mac_adjusttermcursor(window, mouse, cursrgn);
611             break;
612           default:
613             SetCursor(&qd.arrow);
614             CopyRgn(window->visRgn, cursrgn);
615             break;
616         }
617     }
618 }
619
620 void cleanup_exit(int status)
621 {
622
623 #if !TARGET_RT_MAC_CFM
624     if (mac_gestalts.encvvers != 0)
625         TerminateUnicodeConverter();
626 #endif
627     sk_cleanup();
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     cleanup_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     cleanup_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     cleanup_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 const *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 const *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  */