]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac/mac.c
a0bc998b49b4db58e0979e2ea0d93790a09ff641
[PuTTY.git] / mac / mac.c
1 /* $Id: mac.c,v 1.49 2003/02/15 16:22:15 ben Exp $ */
2 /*
3  * Copyright (c) 1999, 2003 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 <AEDataModel.h>
33 #include <AppleEvents.h>
34 #include <Quickdraw.h>
35 #include <Fonts.h>
36 #include <MacWindows.h>
37 #include <Menus.h>
38 #include <TextEdit.h>
39 #include <Appearance.h>
40 #include <CodeFragments.h>
41 #include <Dialogs.h>
42 #include <Devices.h>
43 #include <DiskInit.h>
44 #include <Gestalt.h>
45 #include <LowMem.h>
46 #include <Navigation.h>
47 #include <Resources.h>
48 #include <Script.h>
49 #include <TextCommon.h>
50 #include <ToolUtils.h>
51 #include <UnicodeConverter.h>
52
53 #include <assert.h>
54 #include <limits.h>
55 #include <stdarg.h>
56 #include <stdlib.h>             /* putty.h needs size_t */
57 #include <stdio.h>              /* for vsprintf */
58
59 #define PUTTY_DO_GLOBALS
60
61 #include "macresid.h"
62 #include "putty.h"
63 #include "ssh.h"
64 #include "mac.h"
65
66 Session *sesslist;
67
68 static int cold = 1;
69 static int borednow = FALSE;
70 struct mac_gestalts mac_gestalts;
71
72 static void mac_startup(void);
73 static void mac_eventloop(void);
74 #pragma noreturn (mac_eventloop)
75 static void mac_event(EventRecord *);
76 static void mac_contentclick(WindowPtr, EventRecord *);
77 static void mac_growwindow(WindowPtr, EventRecord *);
78 static void mac_activatewindow(WindowPtr, EventRecord *);
79 static void mac_activateabout(WindowPtr, EventRecord *);
80 static void mac_updatewindow(WindowPtr);
81 static void mac_updatelicence(WindowPtr);
82 static void mac_keypress(EventRecord *);
83 static int mac_windowtype(WindowPtr);
84 static void mac_menucommand(long);
85 static void mac_openlicence(void);
86 static void mac_adjustcursor(RgnHandle);
87 static void mac_adjustmenus(void);
88 static void mac_closewindow(WindowPtr);
89 static void mac_zoomwindow(WindowPtr, short);
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 #if !TARGET_API_MAC_CARBON
112     /* Init Memory Manager */
113     MaxApplZone();
114     /* Init QuickDraw */
115     InitGraf(&qd.thePort);
116     /* Init Font Manager */
117     InitFonts();
118     /* Init Window Manager */
119     InitWindows();
120     /* Init Menu Manager */
121     InitMenus();
122     /* Init TextEdit */
123     TEInit();
124     /* Init Dialog Manager */
125     InitDialogs(NULL);
126 #endif
127     cold = 0;
128     
129     /* Get base system version (only used if there's no better selector) */
130     if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
131         (mac_gestalts.sysvers &= 0xffff) < 0x700)
132         fatalbox("PuTTY requires System 7 or newer");
133     /* Find out if we've got Color Quickdraw */
134     if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
135         mac_gestalts.qdvers = gestaltOriginalQD;
136     /* ... and the Appearance Manager? */
137     if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
138         if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
139             mac_gestalts.apprvers = 0x0100;
140         else
141             mac_gestalts.apprvers = 0;
142 #if TARGET_RT_MAC_CFM
143     /* Paranoia: Did we manage to pull in AppearanceLib? */
144     if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
145         mac_gestalts.apprvers = 0;
146 #endif
147 #if TARGET_CPU_68K
148     mac_gestalts.cntlattr = 0;
149     mac_gestalts.windattr = 0;
150 #else
151     /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
152     if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr ||
153         &SetControlViewSize == kUnresolvedCFragSymbolAddress)
154         mac_gestalts.cntlattr = 0;
155     /* Mac OS 8.5 Window Manager? */
156     if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
157         &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
158         mac_gestalts.windattr = 0;
159 #endif
160     /* Text Encoding Conversion Manager? */
161     if (
162 #if TARGET_RT_MAC_CFM
163         &TECGetInfo == kUnresolvedCFragSymbolAddress ||
164 #else
165         InitializeUnicodeConverter(NULL) != noErr ||
166 #endif
167         TECGetInfo(&ti) != noErr)
168         mac_gestalts.encvvers = 0;
169     else {
170         mac_gestalts.encvvers = (*ti)->tecVersion;
171         mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
172         DisposeHandle((Handle)ti);
173     }
174     /* Navigation Services? */
175     if (NavServicesAvailable())
176         mac_gestalts.navsvers = NavLibraryVersion();
177     else
178         mac_gestalts.navsvers = 0;
179
180     sk_init();
181
182     /* We've been tested with the Appearance Manager */
183     if (mac_gestalts.apprvers != 0)
184         RegisterAppearanceClient();
185
186     menuBar = GetNewMBar(128);
187     if (menuBar == NULL)
188         fatalbox("Unable to create menu bar.");
189     SetMenuBar(menuBar);
190     AppendResMenu(GetMenuHandle(mApple), 'DRVR');
191     mac_adjustmenus();
192     DrawMenuBar();
193     InitCursor();
194     windows.about = NULL;
195     windows.licence = NULL;
196
197     default_protocol = be_default_protocol;
198     /* Find the appropriate default port. */
199     {
200         int i;
201         default_port = 0; /* illegal */
202         for (i = 0; backends[i].backend != NULL; i++)
203             if (backends[i].protocol == default_protocol) {
204                 default_port = backends[i].backend->default_port;
205                 break;
206             }
207     }
208     flags = FLAG_INTERACTIVE;
209
210 #if !TARGET_API_MAC_CARBON
211     {
212         short vol;
213         long dirid;
214
215         /* Set the default directory for loading and saving settings. */
216         /* XXX Should we create it? */
217         if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
218             LMSetSFSaveDisk(-vol);
219             LMSetCurDirStore(dirid);
220         }
221     }
222 #endif
223
224     /* Install Apple Event handlers. */
225     AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
226                           NewAEEventHandlerUPP(&mac_aevt_oapp), 0, FALSE);
227     AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
228                           NewAEEventHandlerUPP(&mac_aevt_odoc), 0, FALSE);
229     AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
230                           NewAEEventHandlerUPP(&mac_aevt_pdoc), 0, FALSE);
231     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
232                           NewAEEventHandlerUPP(&mac_aevt_quit), 0, FALSE);
233 }
234
235 static void mac_eventloop(void) {
236     Boolean gotevent;
237     EventRecord event;
238     RgnHandle cursrgn;
239
240     cursrgn = NewRgn();
241     for (;;) {
242         mac_adjustcursor(cursrgn);
243         gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
244         mac_adjustcursor(cursrgn);
245         if (gotevent)
246             mac_event(&event);
247         if (borednow)
248             cleanup_exit(0);
249         sk_poll();
250         mac_pollterm();
251     }
252     DisposeRgn(cursrgn);
253 }
254
255 static void mac_event(EventRecord *event) {
256     short part;
257     WindowPtr window;
258
259     switch (event->what) {
260       case mouseDown:
261         part = FindWindow(event->where, &window);
262         switch (part) {
263           case inMenuBar:
264             mac_adjustmenus();
265             mac_menucommand(MenuSelect(event->where));
266             break;
267 #if !TARGET_API_MAC_CARBON
268           case inSysWindow:
269             SystemClick(event, window);
270             break;
271 #endif
272           case inContent:
273             if (window != FrontWindow())
274                 /* XXX: check for movable modal dboxes? */
275                 SelectWindow(window);
276             else
277                 mac_contentclick(window, event);
278             break;
279           case inGoAway:
280             if (TrackGoAway(window, event->where))
281                 mac_closewindow(window);
282             break;
283           case inDrag:
284             /* XXX: moveable modal check? */
285 #if TARGET_API_MAC_CARBON
286             {
287                 BitMap screenBits;
288
289                 GetQDGlobalsScreenBits(&screenBits);
290                 DragWindow(window, event->where, &screenBits.bounds);
291             }
292 #else
293             DragWindow(window, event->where, &qd.screenBits.bounds);
294 #endif
295             break;
296           case inGrow:
297             mac_growwindow(window, event);
298             break;
299           case inZoomIn:
300           case inZoomOut:
301             if (TrackBox(window, event->where, part))
302                 mac_zoomwindow(window, part);
303             break;
304         }
305         break;
306       case keyDown:
307       case autoKey:
308         mac_keypress(event);
309         break;
310       case activateEvt:
311         mac_activatewindow((WindowPtr)event->message, event);
312         break;
313       case updateEvt:
314         mac_updatewindow((WindowPtr)event->message);
315         break;
316 #if !TARGET_API_MAC_CARBON
317       case diskEvt:
318         if (HiWord(event->message) != noErr) {
319             Point pt;
320
321             SetPt(&pt, 120, 120);
322             DIBadMount(pt, event->message);
323         }
324         break;
325 #endif
326       case kHighLevelEvent:
327         AEProcessAppleEvent(event); /* errors? */
328         break;
329     }
330 }
331
332 static void mac_contentclick(WindowPtr window, EventRecord *event)
333 {
334
335     if (mac_wininfo(window)->click != NULL)
336         (*mac_wininfo(window)->click)(window, event);
337 }
338
339 static void mac_growwindow(WindowPtr window, EventRecord *event)
340 {
341
342     if (mac_wininfo(window)->grow != NULL)
343         (*mac_wininfo(window)->grow)(window, event);
344 }
345
346 static void mac_activatewindow(WindowPtr window, EventRecord *event)
347 {
348
349     mac_adjustmenus();
350     if (mac_wininfo(window)->activate != NULL)
351         (*mac_wininfo(window)->activate)(window, event);
352 }
353
354 static void mac_updatewindow(WindowPtr window)
355 {
356
357     if (mac_wininfo(window)->update != NULL)
358         (*mac_wininfo(window)->update)(window);
359 }
360
361 /*
362  * Work out what kind of window we're dealing with.
363  */
364 static int mac_windowtype(WindowPtr window)
365 {
366
367 #if !TARGET_API_MAC_CARBON
368     if (GetWindowKind(window) < 0)
369         return wDA;
370 #endif
371     return ((WinInfo *)GetWRefCon(window))->wtype;
372 }
373
374 /*
375  * Handle a key press
376  */
377 static void mac_keypress(EventRecord *event) {
378     WindowPtr window;
379
380     window = FrontWindow();
381     /*
382      * Check for a command-key combination, but ignore it if it counts
383      * as a meta-key combination and we're in a terminal window.
384      */
385     if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
386         !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
387             mac_windowtype(window) == wTerminal)*/) {
388         mac_adjustmenus();
389         mac_menucommand(MenuKey(event->message & charCodeMask));
390     } else {
391         if (mac_wininfo(window)->key != NULL)
392             (*mac_wininfo(window)->key)(window, event);
393     }       
394 }
395
396 static void mac_menucommand(long result) {
397     short menu, item;
398     WindowPtr window;
399 #if !TARGET_API_MAC_CARBON
400     Str255 da;
401 #endif
402
403     menu = HiWord(result);
404     item = LoWord(result);
405     window = FrontWindow();
406     /* Things which do the same whatever window we're in. */
407     switch (menu) {
408       case mApple:
409         switch (item) {
410           case iAbout:
411             mac_openabout();
412             goto done;
413 #if !TARGET_API_MAC_CARBON
414           default:
415             GetMenuItemText(GetMenuHandle(mApple), item, da);
416             OpenDeskAcc(da);
417             goto done;
418 #endif
419         }
420         break;
421       case mFile:
422         switch (item) {
423           case iNew:
424             mac_newsession();
425             goto done;
426           case iOpen:
427             mac_opensession();
428             goto done;
429           case iClose:
430             mac_closewindow(window);
431             goto done;
432           case iSave:
433             mac_savesession();
434             goto done;
435           case iSaveAs:
436             mac_savesessionas();
437             goto done;
438           case iDuplicate:
439             mac_dupsession();
440             goto done;
441           case iQuit:
442             cleanup_exit(0);
443             goto done;
444         }
445         break;
446     }
447     /* If we get here, handling is up to window-specific code. */
448     if (mac_wininfo(window)->menu != NULL)
449         (*mac_wininfo(window)->menu)(window, menu, item);
450
451   done:
452     HiliteMenu(0);
453 }
454
455 static void mac_closewindow(WindowPtr window) {
456
457     switch (mac_windowtype(window)) {
458 #if !TARGET_API_MAC_CARBON
459       case wDA:
460         CloseDeskAcc(GetWindowKind(window));
461         break;
462 #endif
463       default:
464         if (mac_wininfo(window)->close != NULL)
465             (*mac_wininfo(window)->close)(window);
466         break;
467     }
468 }
469
470 static void mac_zoomwindow(WindowPtr window, short part) {
471
472     /* FIXME: do something */
473 }
474
475 /*
476  * Make the menus look right before the user gets to see them.
477  */
478 #if TARGET_API_MAC_CARBON
479 #define EnableItem EnableMenuItem
480 #define DisableItem DisableMenuItem
481 #endif
482 static void mac_adjustmenus(void) {
483     WindowPtr window;
484     MenuHandle menu;
485
486     window = FrontWindow();
487     menu = GetMenuHandle(mApple);
488     EnableItem(menu, 0);
489     EnableItem(menu, iAbout);
490
491     menu = GetMenuHandle(mFile);
492     EnableItem(menu, 0);
493     EnableItem(menu, iNew);
494     if (window != NULL)
495         EnableItem(menu, iClose);
496     else
497         DisableItem(menu, iClose);
498     EnableItem(menu, iQuit);
499
500     if (mac_wininfo(window)->adjustmenus != NULL)
501         (*mac_wininfo(window)->adjustmenus)(window);
502     else {
503         DisableItem(menu, iSave);
504         DisableItem(menu, iSaveAs);
505         DisableItem(menu, iDuplicate);
506         menu = GetMenuHandle(mEdit);
507         DisableItem(menu, 0);
508         menu = GetMenuHandle(mWindow);
509         DisableItem(menu, 0); /* Until we get more than 1 item on it. */
510     }
511     DrawMenuBar();
512 }
513
514 /*
515  * Make sure the right cursor's being displayed.
516  */
517 static void mac_adjustcursor(RgnHandle cursrgn) {
518     Point mouse;
519     WindowPtr window, front;
520     short part;
521 #if TARGET_API_MAC_CARBON
522     Cursor arrow;
523     RgnHandle visrgn;
524 #endif
525
526     GetMouse(&mouse);
527     LocalToGlobal(&mouse);
528     part = FindWindow(mouse, &window);
529     front = FrontWindow();
530     if (part != inContent || window == NULL || window != front) {
531         /* Cursor isn't in the front window, so switch to arrow */
532 #if TARGET_API_MAC_CARBON
533         GetQDGlobalsArrow(&arrow);
534         SetCursor(&arrow);
535 #else
536         SetCursor(&qd.arrow);
537 #endif
538         SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
539         if (front != NULL) {
540 #if TARGET_API_MAC_CARBON
541             visrgn = NewRgn();
542             GetPortVisibleRegion(GetWindowPort(front), visrgn);
543             DiffRgn(cursrgn, visrgn, cursrgn);
544             DisposeRgn(visrgn);
545 #else
546             DiffRgn(cursrgn, front->visRgn, cursrgn);
547 #endif
548         }
549     } else {
550         if (mac_wininfo(window)->adjustcursor != NULL)
551             (*mac_wininfo(window)->adjustcursor)(window, mouse, cursrgn);
552         else {
553 #if TARGET_API_MAC_CARBON
554             GetQDGlobalsArrow(&arrow);
555             SetCursor(&arrow);
556             GetPortVisibleRegion(GetWindowPort(window), cursrgn);
557 #else
558             SetCursor(&qd.arrow);
559             CopyRgn(window->visRgn, cursrgn);
560 #endif
561         }
562     }
563 }
564
565 pascal OSErr mac_aevt_quit(const AppleEvent *req, AppleEvent *reply,
566                                   long refcon)
567 {
568     DescType type;
569     Size size;
570
571     if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
572                           &type, NULL, 0, &size) == noErr)
573         return errAEParamMissed;
574
575     borednow = 1;
576     return noErr;
577 }
578
579 void cleanup_exit(int status)
580 {
581
582 #if !TARGET_RT_MAC_CFM
583     if (mac_gestalts.encvvers != 0)
584         TerminateUnicodeConverter();
585 #endif
586     sk_cleanup();
587     exit(status);
588 }
589
590 /* This should only kill the current session, not the whole application. */
591 void connection_fatal(void *fontend, char *fmt, ...) {
592     va_list ap;
593     Str255 stuff;
594     
595     va_start(ap, fmt);
596     /* We'd like stuff to be a Pascal string */
597     stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
598     va_end(ap);
599     ParamText(stuff, NULL, NULL, NULL);
600     StopAlert(128, NULL);
601     cleanup_exit(1);
602 }
603
604 /* Null SSH agent client -- never finds an agent. */
605
606 int agent_exists(void)
607 {
608
609     return FALSE;
610 }
611
612 void agent_query(void *in, int inlen, void **out, int *outlen)
613 {
614
615     *out = NULL;
616     *outlen = 0;
617 }
618
619 /* Temporary null routines for testing. */
620
621 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
622                          char *keystr, char *fingerprint)
623 {
624
625 }
626
627 void askcipher(void *frontend, char *ciphername, int cs)
628 {
629
630 }
631
632 void old_keyfile_warning(void)
633 {
634
635 }
636
637 FontSpec platform_default_fontspec(char const *name)
638 {
639     FontSpec ret;
640     long smfs;
641
642     if (!strcmp(name, "Font")) {
643         smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
644         if (smfs == 0)
645             smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
646         if (smfs != 0) {
647             GetFontName(HiWord(smfs), ret.name);
648             if (ret.name[0] == 0)
649                 memcpy(ret.name, "\pMonaco", 7);
650             ret.size = LoWord(smfs);
651         } else {
652             memcpy(ret.name, "\pMonaco", 7);
653             ret.size = 9;
654         }
655         ret.face = 0;
656     } else {
657         ret.name[0] = 0;
658     }
659
660     return ret;
661 }
662
663 Filename platform_default_filename(const char *name)
664 {
665     Filename ret;
666     if (!strcmp(name, "LogFileName"))
667         FSMakeFSSpec(0, 0, "\pputty.log", &ret.fss);
668     else
669         memset(&ret, 0, sizeof(ret));
670     return ret;
671 }
672
673 char *platform_default_s(char const *name)
674 {
675     return NULL;
676 }
677
678 int platform_default_i(char const *name, int def)
679 {
680
681     /* Non-raw cut and paste of line-drawing chars works badly on the
682      * current Unix stub implementation of the Unicode functions.
683      * So I'm going to temporarily set the default to raw mode so
684      * that the failure mode isn't quite so drastically horrid.
685      * When Unicode comes in, this can all be put right. */
686     if (!strcmp(name, "RawCNP"))
687         return 1;
688     return def;
689 }
690
691 void platform_get_x11_auth(char *display, int *proto,
692                            unsigned char *data, int *datalen)
693 {
694     /* SGT: I have no idea whether Mac X servers need anything here. */
695 }
696
697 /*
698  * Local Variables:
699  * c-file-style: "simon"
700  * End:
701  */