We just got mailed a Dr Watson log which suggests I failed to
[u/mdw/putty] / mac / mac.c
1 /* $Id: mac.c,v 1.19 2003/01/05 12:53:38 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 "mac.h"
61
62 QDGlobals qd;
63
64 static int cold = 1;
65 struct mac_gestalts mac_gestalts;
66
67 static void mac_startup(void);
68 static void mac_eventloop(void);
69 #pragma noreturn (mac_eventloop)
70 static void mac_event(EventRecord *);
71 static void mac_contentclick(WindowPtr, EventRecord *);
72 static void mac_growwindow(WindowPtr, EventRecord *);
73 static void mac_activatewindow(WindowPtr, EventRecord *);
74 static void mac_activateabout(WindowPtr, EventRecord *);
75 static void mac_updatewindow(WindowPtr);
76 static void mac_updatelicence(WindowPtr);
77 static void mac_keypress(EventRecord *);
78 static int mac_windowtype(WindowPtr);
79 static void mac_menucommand(long);
80 static void mac_openabout(void);
81 static void mac_openlicence(void);
82 static void mac_adjustcursor(RgnHandle);
83 static void mac_adjustmenus(void);
84 static void mac_closewindow(WindowPtr);
85 static void mac_zoomwindow(WindowPtr, short);
86 static void mac_shutdown(void);
87 #pragma noreturn (mac_shutdown)
88
89 struct mac_windows {
90 WindowPtr about;
91 WindowPtr licence;
92 };
93
94 struct mac_windows windows;
95
96 int main (int argc, char **argv) {
97
98 mac_startup();
99 mac_eventloop();
100 }
101
102 #pragma noreturn (main)
103
104 static void mac_startup(void) {
105 Handle menuBar;
106 TECInfoHandle ti;
107
108 /* Init Memory Manager */
109 MaxApplZone();
110 /* Init QuickDraw */
111 InitGraf(&qd.thePort);
112 /* Init Font Manager */
113 InitFonts();
114 /* Init Window Manager */
115 InitWindows();
116 /* Init Menu Manager */
117 InitMenus();
118 /* Init TextEdit */
119 TEInit();
120 /* Init Dialog Manager */
121 InitDialogs(NULL);
122 cold = 0;
123
124 /* Get base system version (only used if there's no better selector) */
125 if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
126 (mac_gestalts.sysvers &= 0xffff) < 0x700)
127 fatalbox("PuTTY requires System 7 or newer");
128 /* Find out if we've got Color Quickdraw */
129 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
130 mac_gestalts.qdvers = gestaltOriginalQD;
131 /* ... and the Appearance Manager? */
132 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
133 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
134 mac_gestalts.apprvers = 0x0100;
135 else
136 mac_gestalts.apprvers = 0;
137 #if TARGET_RT_MAC_CFM
138 /* Paranoia: Did we manage to pull in AppearanceLib? */
139 if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
140 mac_gestalts.apprvers = 0;
141 #endif
142 #if TARGET_CPU_68K
143 mac_gestalts.cntlattr = 0;
144 mac_gestalts.windattr = 0;
145 #else
146 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
147 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr ||
148 &SetControlViewSize == kUnresolvedCFragSymbolAddress)
149 mac_gestalts.cntlattr = 0;
150 /* Mac OS 8.5 Window Manager? */
151 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
152 &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
153 mac_gestalts.windattr = 0;
154 #endif
155 /* Text Encoding Conversion Manager? */
156 if (
157 #if TARGET_RT_MAC_CFM
158 &TECGetInfo == kUnresolvedCFragSymbolAddress ||
159 #else
160 InitializeUnicodeConverter(NULL) != noErr ||
161 #endif
162 TECGetInfo(&ti) != noErr)
163 mac_gestalts.encvvers = 0;
164 else {
165 mac_gestalts.encvvers = (*ti)->tecVersion;
166 mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
167 DisposeHandle((Handle)ti);
168 }
169
170 mactcp_init();
171
172 /* We've been tested with the Appearance Manager */
173 if (mac_gestalts.apprvers != 0)
174 RegisterAppearanceClient();
175
176 menuBar = GetNewMBar(128);
177 if (menuBar == NULL)
178 fatalbox("Unable to create menu bar.");
179 SetMenuBar(menuBar);
180 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
181 mac_adjustmenus();
182 DrawMenuBar();
183 InitCursor();
184 windows.about = NULL;
185 windows.licence = NULL;
186
187 default_protocol = DEFAULT_PROTOCOL;
188 default_port = DEFAULT_PORT;
189
190 {
191 short vol;
192 long dirid;
193
194 /* Set the default directory for loading and saving settings. */
195 /* XXX Should we create it? */
196 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
197 LMSetSFSaveDisk(-vol);
198 LMSetCurDirStore(dirid);
199 }
200 }
201 init_ucs();
202 }
203
204 static void mac_eventloop(void) {
205 Boolean gotevent;
206 EventRecord event;
207 RgnHandle cursrgn;
208
209 cursrgn = NewRgn();
210 for (;;) {
211 mac_adjustcursor(cursrgn);
212 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
213 mac_adjustcursor(cursrgn);
214 if (gotevent)
215 mac_event(&event);
216 mactcp_poll();
217 }
218 DisposeRgn(cursrgn);
219 }
220
221 static void mac_event(EventRecord *event) {
222 short part;
223 WindowPtr window;
224 Point pt;
225
226 switch (event->what) {
227 case mouseDown:
228 part = FindWindow(event->where, &window);
229 switch (part) {
230 case inMenuBar:
231 mac_adjustmenus();
232 mac_menucommand(MenuSelect(event->where));
233 break;
234 case inSysWindow:
235 SystemClick(event, window);
236 break;
237 case inContent:
238 if (window != FrontWindow())
239 /* XXX: check for movable modal dboxes? */
240 SelectWindow(window);
241 else
242 mac_contentclick(window, event);
243 break;
244 case inGoAway:
245 if (TrackGoAway(window, event->where))
246 mac_closewindow(window);
247 break;
248 case inDrag:
249 /* XXX: moveable modal check? */
250 DragWindow(window, event->where, &qd.screenBits.bounds);
251 break;
252 case inGrow:
253 mac_growwindow(window, event);
254 break;
255 case inZoomIn:
256 case inZoomOut:
257 if (TrackBox(window, event->where, part))
258 mac_zoomwindow(window, part);
259 break;
260 }
261 break;
262 case keyDown:
263 case autoKey:
264 mac_keypress(event);
265 break;
266 case activateEvt:
267 mac_activatewindow((WindowPtr)event->message, event);
268 break;
269 case updateEvt:
270 mac_updatewindow((WindowPtr)event->message);
271 break;
272 case diskEvt:
273 if (HiWord(event->message) != noErr) {
274 SetPt(&pt, 120, 120);
275 DIBadMount(pt, event->message);
276 }
277 break;
278 }
279 }
280
281 static void mac_contentclick(WindowPtr window, EventRecord *event) {
282 short item;
283
284 switch (mac_windowtype(window)) {
285 case wTerminal:
286 mac_clickterm(window, event);
287 break;
288 case wAbout:
289 if (DialogSelect(event, &window, &item))
290 switch (item) {
291 case wiAboutLicence:
292 mac_openlicence();
293 break;
294 }
295 break;
296 case wSettings:
297 mac_clickdlg(window, event);
298 break;
299 }
300 }
301
302 static void mac_growwindow(WindowPtr window, EventRecord *event) {
303
304 switch (mac_windowtype(window)) {
305 case wTerminal:
306 mac_growterm(window, event);
307 }
308 }
309
310 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
311 int active;
312
313 active = (event->modifiers & activeFlag) != 0;
314 mac_adjustmenus();
315 switch (mac_windowtype(window)) {
316 case wTerminal:
317 mac_activateterm(window, active);
318 break;
319 case wSettings:
320 mac_activatedlg(window, event);
321 break;
322 case wAbout:
323 mac_activateabout(window, event);
324 break;
325 }
326 }
327
328 static void mac_activateabout(WindowPtr window, EventRecord *event) {
329 DialogItemType itemtype;
330 Handle itemhandle;
331 short item;
332 Rect itemrect;
333 int active;
334
335 active = (event->modifiers & activeFlag) != 0;
336 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
337 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
338 DialogSelect(event, &window, &item);
339 }
340
341 static void mac_updatewindow(WindowPtr window) {
342
343 switch (mac_windowtype(window)) {
344 case wTerminal:
345 mac_updateterm(window);
346 break;
347 case wAbout:
348 case wSettings:
349 BeginUpdate(window);
350 UpdateDialog(window, window->visRgn);
351 EndUpdate(window);
352 break;
353 case wLicence:
354 mac_updatelicence(window);
355 break;
356 }
357 }
358
359 static void mac_updatelicence(WindowPtr window)
360 {
361 Handle h;
362 int len;
363 long fondsize;
364
365 SetPort(window);
366 BeginUpdate(window);
367 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
368 TextFont(HiWord(fondsize));
369 TextSize(LoWord(fondsize));
370 h = Get1Resource('TEXT', wLicence);
371 len = GetResourceSizeOnDisk(h);
372 if (h != NULL) {
373 HLock(h);
374 TETextBox(*h, len, &window->portRect, teFlushDefault);
375 HUnlock(h);
376 }
377 EndUpdate(window);
378 }
379
380 /*
381 * Work out what kind of window we're dealing with.
382 * Concept shamelessly nicked from SurfWriter.
383 */
384 static int mac_windowtype(WindowPtr window) {
385 int kind;
386 long refcon;
387
388 if (window == NULL)
389 return wNone;
390 kind = ((WindowPeek)window)->windowKind;
391 if (kind < 0)
392 return wDA;
393 if (GetWVariant(window) == zoomDocProc)
394 return wTerminal;
395 refcon = GetWRefCon(window);
396 if (refcon < 1024)
397 return refcon;
398 else
399 return wSettings;
400 }
401
402 /*
403 * Handle a key press
404 */
405 static void mac_keypress(EventRecord *event) {
406 WindowPtr window;
407
408 window = FrontWindow();
409 /*
410 * Check for a command-key combination, but ignore it if it counts
411 * as a meta-key combination and we're in a terminal window.
412 */
413 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
414 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
415 mac_windowtype(window) == wTerminal)*/) {
416 mac_adjustmenus();
417 mac_menucommand(MenuKey(event->message & charCodeMask));
418 } else {
419 switch (mac_windowtype(window)) {
420 case wTerminal:
421 mac_keyterm(window, event);
422 break;
423 }
424 }
425 }
426
427 static void mac_menucommand(long result) {
428 short menu, item;
429 Str255 da;
430 WindowPtr window;
431
432 menu = HiWord(result);
433 item = LoWord(result);
434 window = FrontWindow();
435 /* Things which do the same whatever window we're in. */
436 switch (menu) {
437 case mApple:
438 switch (item) {
439 case iAbout:
440 mac_openabout();
441 goto done;
442 default:
443 GetMenuItemText(GetMenuHandle(mApple), item, da);
444 OpenDeskAcc(da);
445 goto done;
446 }
447 break;
448 case mFile:
449 switch (item) {
450 case iNew:
451 mac_newsession();
452 goto done;
453 case iOpen:
454 mac_opensession();
455 goto done;
456 case iClose:
457 mac_closewindow(window);
458 goto done;
459 case iQuit:
460 mac_shutdown();
461 goto done;
462 }
463 break;
464 }
465 /* If we get here, handling is up to window-specific code. */
466 switch (mac_windowtype(window)) {
467 case wTerminal:
468 mac_menuterm(window, menu, item);
469 break;
470 }
471 done:
472 HiliteMenu(0);
473 }
474
475 static void mac_openabout(void) {
476 DialogItemType itemtype;
477 Handle item;
478 VersRecHndl vers;
479 Rect box;
480 StringPtr longvers;
481
482 if (windows.about)
483 SelectWindow(windows.about);
484 else {
485 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
486 vers = (VersRecHndl)Get1Resource('vers', 1);
487 if (vers != NULL && *vers != NULL) {
488 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
489 GetDialogItem(windows.about, wiAboutVersion,
490 &itemtype, &item, &box);
491 assert(itemtype & kStaticTextDialogItem);
492 SetDialogItemText(item, longvers);
493 }
494 ShowWindow(windows.about);
495 }
496 }
497
498 static void mac_openlicence(void) {
499
500 if (windows.licence)
501 SelectWindow(windows.licence);
502 else {
503 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
504 ShowWindow(windows.licence);
505 }
506 }
507
508 static void mac_closewindow(WindowPtr window) {
509
510 switch (mac_windowtype(window)) {
511 case wDA:
512 CloseDeskAcc(((WindowPeek)window)->windowKind);
513 break;
514 case wTerminal:
515 /* FIXME: end session and stuff */
516 break;
517 case wAbout:
518 windows.about = NULL;
519 CloseWindow(window);
520 break;
521 case wLicence:
522 windows.licence = NULL;
523 CloseWindow(window);
524 break;
525 default:
526 CloseWindow(window);
527 break;
528 }
529 }
530
531 static void mac_zoomwindow(WindowPtr window, short part) {
532
533 /* FIXME: do something */
534 }
535
536 /*
537 * Make the menus look right before the user gets to see them.
538 */
539 static void mac_adjustmenus(void) {
540 WindowPtr window;
541 MenuHandle menu;
542
543 window = FrontWindow();
544 menu = GetMenuHandle(mApple);
545 EnableItem(menu, 0);
546 EnableItem(menu, iAbout);
547
548 menu = GetMenuHandle(mFile);
549 EnableItem(menu, 0);
550 EnableItem(menu, iNew);
551 if (window != NULL)
552 EnableItem(menu, iClose);
553 else
554 DisableItem(menu, iClose);
555 EnableItem(menu, iQuit);
556
557 switch (mac_windowtype(window)) {
558 case wTerminal:
559 mac_adjusttermmenus(window);
560 break;
561 default:
562 menu = GetMenuHandle(mEdit);
563 DisableItem(menu, 0);
564 break;
565 }
566 DrawMenuBar();
567 }
568
569 /*
570 * Make sure the right cursor's being displayed.
571 */
572 static void mac_adjustcursor(RgnHandle cursrgn) {
573 Point mouse;
574 WindowPtr window, front;
575 short part;
576
577 GetMouse(&mouse);
578 LocalToGlobal(&mouse);
579 part = FindWindow(mouse, &window);
580 front = FrontWindow();
581 if (part != inContent || window == NULL || window != front) {
582 /* Cursor isn't in the front window, so switch to arrow */
583 SetCursor(&qd.arrow);
584 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
585 if (front != NULL)
586 DiffRgn(cursrgn, front->visRgn, cursrgn);
587 } else {
588 switch (mac_windowtype(window)) {
589 case wTerminal:
590 mac_adjusttermcursor(window, mouse, cursrgn);
591 break;
592 default:
593 SetCursor(&qd.arrow);
594 CopyRgn(window->visRgn, cursrgn);
595 break;
596 }
597 }
598 }
599
600 static void mac_shutdown(void) {
601
602 #if !TARGET_RT_MAC_CFM
603 if (mac_gestalts.encvvers != 0)
604 TerminateUnicodeConverter();
605 #endif
606 mactcp_shutdown();
607 exit(0);
608 }
609
610 void fatalbox(char *fmt, ...) {
611 va_list ap;
612 Str255 stuff;
613
614 va_start(ap, fmt);
615 /* We'd like stuff to be a Pascal string */
616 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
617 va_end(ap);
618 ParamText(stuff, NULL, NULL, NULL);
619 StopAlert(128, NULL);
620 exit(1);
621 }
622
623 void modalfatalbox(char *fmt, ...) {
624 va_list ap;
625 Str255 stuff;
626
627 va_start(ap, fmt);
628 /* We'd like stuff to be a Pascal string */
629 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
630 va_end(ap);
631 ParamText(stuff, NULL, NULL, NULL);
632 StopAlert(128, NULL);
633 exit(1);
634 }
635
636 /* This should only kill the current session, not the whole application. */
637 void connection_fatal(void *fontend, char *fmt, ...) {
638 va_list ap;
639 Str255 stuff;
640
641 va_start(ap, fmt);
642 /* We'd like stuff to be a Pascal string */
643 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
644 va_end(ap);
645 ParamText(stuff, NULL, NULL, NULL);
646 StopAlert(128, NULL);
647 exit(1);
648 }
649
650 /*
651 * Local Variables:
652 * c-file-style: "simon"
653 * End:
654 */