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