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