Introduce framework for authenticating with the local X server.
[u/mdw/putty] / mac / mac.c
CommitLineData
e0e7dff8 1/* $Id: mac.c,v 1.23 2003/01/10 18:33:35 simon 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"
0c4b7799 60#include "ssh.h"
d082ac49 61#include "mac.h"
62
63QDGlobals qd;
64
0c4b7799 65Session *sesslist;
66
d082ac49 67static int cold = 1;
68struct mac_gestalts mac_gestalts;
69
70static void mac_startup(void);
71static void mac_eventloop(void);
72#pragma noreturn (mac_eventloop)
73static void mac_event(EventRecord *);
74static void mac_contentclick(WindowPtr, EventRecord *);
75static void mac_growwindow(WindowPtr, EventRecord *);
76static void mac_activatewindow(WindowPtr, EventRecord *);
77static void mac_activateabout(WindowPtr, EventRecord *);
78static void mac_updatewindow(WindowPtr);
ebd78b62 79static void mac_updatelicence(WindowPtr);
d082ac49 80static void mac_keypress(EventRecord *);
81static int mac_windowtype(WindowPtr);
82static void mac_menucommand(long);
83static void mac_openabout(void);
ebd78b62 84static void mac_openlicence(void);
d082ac49 85static void mac_adjustcursor(RgnHandle);
86static void mac_adjustmenus(void);
87static void mac_closewindow(WindowPtr);
88static void mac_zoomwindow(WindowPtr, short);
89static void mac_shutdown(void);
0c4b7799 90#pragma noreturn (cleanup_exit)
d082ac49 91
92struct mac_windows {
93 WindowPtr about;
94 WindowPtr licence;
95};
96
97struct mac_windows windows;
98
99int main (int argc, char **argv) {
100
101 mac_startup();
102 mac_eventloop();
103}
104
105#pragma noreturn (main)
106
107static void mac_startup(void) {
108 Handle menuBar;
8768ce31 109 TECInfoHandle ti;
d082ac49 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 */
f3ab148c 124 InitDialogs(NULL);
d082ac49 125 cold = 0;
126
56ed4cf7 127 /* Get base system version (only used if there's no better selector) */
128 if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
6e1063b1 129 (mac_gestalts.sysvers &= 0xffff) < 0x700)
56ed4cf7 130 fatalbox("PuTTY requires System 7 or newer");
d082ac49 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
1fc898ea 145#if TARGET_CPU_68K
146 mac_gestalts.cntlattr = 0;
147 mac_gestalts.windattr = 0;
148#else
d082ac49 149 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
1fc898ea 150 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr ||
151 &SetControlViewSize == kUnresolvedCFragSymbolAddress)
d082ac49 152 mac_gestalts.cntlattr = 0;
153 /* Mac OS 8.5 Window Manager? */
1fc898ea 154 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
155 &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
d082ac49 156 mac_gestalts.windattr = 0;
1fc898ea 157#endif
8768ce31 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;
379836ca 169 mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
8768ce31 170 DisposeHandle((Handle)ti);
171 }
d082ac49 172
2beb0fb0 173 mactcp_init();
174
d082ac49 175 /* We've been tested with the Appearance Manager */
176 if (mac_gestalts.apprvers != 0)
177 RegisterAppearanceClient();
178
179 menuBar = GetNewMBar(128);
180 if (menuBar == NULL)
181 fatalbox("Unable to create menu bar.");
182 SetMenuBar(menuBar);
183 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
184 mac_adjustmenus();
185 DrawMenuBar();
186 InitCursor();
187 windows.about = NULL;
188 windows.licence = NULL;
189
2beb0fb0 190 default_protocol = DEFAULT_PROTOCOL;
191 default_port = DEFAULT_PORT;
0c4b7799 192 flags = FLAG_INTERACTIVE;
193
194 /*
195 * Really grotty hack to ensure that anything that looks at the
196 * global "cfg" variable gets something vaguely sensible.
197 * Obviously, nothing should actually be using it, but that will
198 * take a while to arrange.
199 */
200 do_defaults(NULL, &cfg);
2beb0fb0 201
bf873e8e 202 {
203 short vol;
204 long dirid;
205
206 /* Set the default directory for loading and saving settings. */
207 /* XXX Should we create it? */
208 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
209 LMSetSFSaveDisk(-vol);
210 LMSetCurDirStore(dirid);
211 }
212 }
d082ac49 213 init_ucs();
214}
215
216static void mac_eventloop(void) {
217 Boolean gotevent;
218 EventRecord event;
219 RgnHandle cursrgn;
220
221 cursrgn = NewRgn();
222 for (;;) {
223 mac_adjustcursor(cursrgn);
224 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
225 mac_adjustcursor(cursrgn);
226 if (gotevent)
227 mac_event(&event);
d25f9823 228 mactcp_poll();
0c4b7799 229 mac_pollterm();
d082ac49 230 }
231 DisposeRgn(cursrgn);
232}
233
234static void mac_event(EventRecord *event) {
235 short part;
236 WindowPtr window;
237 Point pt;
238
239 switch (event->what) {
240 case mouseDown:
241 part = FindWindow(event->where, &window);
242 switch (part) {
243 case inMenuBar:
244 mac_adjustmenus();
245 mac_menucommand(MenuSelect(event->where));
246 break;
247 case inSysWindow:
248 SystemClick(event, window);
249 break;
250 case inContent:
251 if (window != FrontWindow())
252 /* XXX: check for movable modal dboxes? */
253 SelectWindow(window);
254 else
255 mac_contentclick(window, event);
256 break;
257 case inGoAway:
258 if (TrackGoAway(window, event->where))
259 mac_closewindow(window);
260 break;
261 case inDrag:
262 /* XXX: moveable modal check? */
263 DragWindow(window, event->where, &qd.screenBits.bounds);
264 break;
265 case inGrow:
266 mac_growwindow(window, event);
267 break;
268 case inZoomIn:
269 case inZoomOut:
270 if (TrackBox(window, event->where, part))
271 mac_zoomwindow(window, part);
272 break;
273 }
274 break;
275 case keyDown:
276 case autoKey:
277 mac_keypress(event);
278 break;
279 case activateEvt:
280 mac_activatewindow((WindowPtr)event->message, event);
281 break;
282 case updateEvt:
283 mac_updatewindow((WindowPtr)event->message);
284 break;
285 case diskEvt:
286 if (HiWord(event->message) != noErr) {
287 SetPt(&pt, 120, 120);
288 DIBadMount(pt, event->message);
289 }
290 break;
291 }
292}
293
294static void mac_contentclick(WindowPtr window, EventRecord *event) {
295 short item;
296
297 switch (mac_windowtype(window)) {
298 case wTerminal:
299 mac_clickterm(window, event);
300 break;
301 case wAbout:
34e9a202 302 if (DialogSelect(event, &window, &item))
d082ac49 303 switch (item) {
304 case wiAboutLicence:
ebd78b62 305 mac_openlicence();
d082ac49 306 break;
307 }
308 break;
6cb61a05 309 case wSettings:
310 mac_clickdlg(window, event);
311 break;
d082ac49 312 }
313}
314
315static void mac_growwindow(WindowPtr window, EventRecord *event) {
316
317 switch (mac_windowtype(window)) {
318 case wTerminal:
319 mac_growterm(window, event);
320 }
321}
322
323static void mac_activatewindow(WindowPtr window, EventRecord *event) {
324 int active;
325
326 active = (event->modifiers & activeFlag) != 0;
327 mac_adjustmenus();
328 switch (mac_windowtype(window)) {
329 case wTerminal:
330 mac_activateterm(window, active);
331 break;
6cb61a05 332 case wSettings:
333 mac_activatedlg(window, event);
334 break;
d082ac49 335 case wAbout:
336 mac_activateabout(window, event);
337 break;
338 }
339}
340
341static void mac_activateabout(WindowPtr window, EventRecord *event) {
342 DialogItemType itemtype;
343 Handle itemhandle;
344 short item;
345 Rect itemrect;
346 int active;
347
348 active = (event->modifiers & activeFlag) != 0;
349 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
350 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
351 DialogSelect(event, &window, &item);
352}
353
354static void mac_updatewindow(WindowPtr window) {
355
356 switch (mac_windowtype(window)) {
357 case wTerminal:
358 mac_updateterm(window);
359 break;
360 case wAbout:
6cb61a05 361 case wSettings:
d082ac49 362 BeginUpdate(window);
363 UpdateDialog(window, window->visRgn);
364 EndUpdate(window);
365 break;
366 case wLicence:
ebd78b62 367 mac_updatelicence(window);
368 break;
369 }
370}
371
372static void mac_updatelicence(WindowPtr window)
373{
374 Handle h;
375 int len;
36577dc9 376 long fondsize;
ebd78b62 377
378 SetPort(window);
379 BeginUpdate(window);
36577dc9 380 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
381 TextFont(HiWord(fondsize));
382 TextSize(LoWord(fondsize));
ebd78b62 383 h = Get1Resource('TEXT', wLicence);
384 len = GetResourceSizeOnDisk(h);
385 if (h != NULL) {
386 HLock(h);
387 TETextBox(*h, len, &window->portRect, teFlushDefault);
388 HUnlock(h);
d082ac49 389 }
ebd78b62 390 EndUpdate(window);
d082ac49 391}
392
393/*
394 * Work out what kind of window we're dealing with.
395 * Concept shamelessly nicked from SurfWriter.
396 */
397static int mac_windowtype(WindowPtr window) {
398 int kind;
6cb61a05 399 long refcon;
d082ac49 400
401 if (window == NULL)
402 return wNone;
403 kind = ((WindowPeek)window)->windowKind;
404 if (kind < 0)
405 return wDA;
406 if (GetWVariant(window) == zoomDocProc)
407 return wTerminal;
6cb61a05 408 refcon = GetWRefCon(window);
409 if (refcon < 1024)
410 return refcon;
411 else
412 return wSettings;
d082ac49 413}
414
415/*
416 * Handle a key press
417 */
418static void mac_keypress(EventRecord *event) {
419 WindowPtr window;
420
421 window = FrontWindow();
422 /*
423 * Check for a command-key combination, but ignore it if it counts
424 * as a meta-key combination and we're in a terminal window.
425 */
426 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
427 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
428 mac_windowtype(window) == wTerminal)*/) {
429 mac_adjustmenus();
430 mac_menucommand(MenuKey(event->message & charCodeMask));
431 } else {
432 switch (mac_windowtype(window)) {
433 case wTerminal:
434 mac_keyterm(window, event);
435 break;
436 }
437 }
438}
439
440static void mac_menucommand(long result) {
441 short menu, item;
442 Str255 da;
443 WindowPtr window;
444
445 menu = HiWord(result);
446 item = LoWord(result);
447 window = FrontWindow();
448 /* Things which do the same whatever window we're in. */
449 switch (menu) {
450 case mApple:
451 switch (item) {
452 case iAbout:
453 mac_openabout();
454 goto done;
455 default:
456 GetMenuItemText(GetMenuHandle(mApple), item, da);
457 OpenDeskAcc(da);
458 goto done;
459 }
460 break;
461 case mFile:
462 switch (item) {
463 case iNew:
464 mac_newsession();
465 goto done;
ce283213 466 case iOpen:
467 mac_opensession();
468 goto done;
d082ac49 469 case iClose:
470 mac_closewindow(window);
471 goto done;
472 case iQuit:
0c4b7799 473 cleanup_exit(0);
d082ac49 474 goto done;
475 }
476 break;
477 }
478 /* If we get here, handling is up to window-specific code. */
479 switch (mac_windowtype(window)) {
480 case wTerminal:
481 mac_menuterm(window, menu, item);
482 break;
483 }
484 done:
485 HiliteMenu(0);
486}
487
488static void mac_openabout(void) {
489 DialogItemType itemtype;
490 Handle item;
491 VersRecHndl vers;
492 Rect box;
493 StringPtr longvers;
494
495 if (windows.about)
496 SelectWindow(windows.about);
497 else {
498 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
5dbc118e 499 vers = (VersRecHndl)Get1Resource('vers', 1);
500 if (vers != NULL && *vers != NULL) {
501 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
502 GetDialogItem(windows.about, wiAboutVersion,
503 &itemtype, &item, &box);
504 assert(itemtype & kStaticTextDialogItem);
505 SetDialogItemText(item, longvers);
506 }
d082ac49 507 ShowWindow(windows.about);
508 }
509}
510
ebd78b62 511static void mac_openlicence(void) {
ebd78b62 512
513 if (windows.licence)
514 SelectWindow(windows.licence);
515 else {
516 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
517 ShowWindow(windows.licence);
518 }
519}
520
d082ac49 521static void mac_closewindow(WindowPtr window) {
522
523 switch (mac_windowtype(window)) {
524 case wDA:
525 CloseDeskAcc(((WindowPeek)window)->windowKind);
526 break;
527 case wTerminal:
528 /* FIXME: end session and stuff */
529 break;
530 case wAbout:
531 windows.about = NULL;
532 CloseWindow(window);
533 break;
ebd78b62 534 case wLicence:
535 windows.licence = NULL;
536 CloseWindow(window);
537 break;
d082ac49 538 default:
539 CloseWindow(window);
540 break;
541 }
542}
543
544static void mac_zoomwindow(WindowPtr window, short part) {
545
546 /* FIXME: do something */
547}
548
549/*
550 * Make the menus look right before the user gets to see them.
551 */
552static void mac_adjustmenus(void) {
553 WindowPtr window;
554 MenuHandle menu;
555
556 window = FrontWindow();
557 menu = GetMenuHandle(mApple);
558 EnableItem(menu, 0);
559 EnableItem(menu, iAbout);
560
561 menu = GetMenuHandle(mFile);
562 EnableItem(menu, 0);
563 EnableItem(menu, iNew);
564 if (window != NULL)
565 EnableItem(menu, iClose);
566 else
567 DisableItem(menu, iClose);
568 EnableItem(menu, iQuit);
569
570 switch (mac_windowtype(window)) {
571 case wTerminal:
572 mac_adjusttermmenus(window);
573 break;
574 default:
575 menu = GetMenuHandle(mEdit);
576 DisableItem(menu, 0);
577 break;
578 }
579 DrawMenuBar();
580}
581
582/*
583 * Make sure the right cursor's being displayed.
584 */
585static void mac_adjustcursor(RgnHandle cursrgn) {
586 Point mouse;
587 WindowPtr window, front;
588 short part;
589
590 GetMouse(&mouse);
591 LocalToGlobal(&mouse);
592 part = FindWindow(mouse, &window);
593 front = FrontWindow();
594 if (part != inContent || window == NULL || window != front) {
595 /* Cursor isn't in the front window, so switch to arrow */
596 SetCursor(&qd.arrow);
597 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
598 if (front != NULL)
599 DiffRgn(cursrgn, front->visRgn, cursrgn);
600 } else {
601 switch (mac_windowtype(window)) {
602 case wTerminal:
603 mac_adjusttermcursor(window, mouse, cursrgn);
604 break;
605 default:
606 SetCursor(&qd.arrow);
607 CopyRgn(window->visRgn, cursrgn);
608 break;
609 }
610 }
611}
612
0c4b7799 613void cleanup_exit(int status)
614{
d082ac49 615
713b8b7a 616#if !TARGET_RT_MAC_CFM
8768ce31 617 if (mac_gestalts.encvvers != 0)
618 TerminateUnicodeConverter();
713b8b7a 619#endif
2beb0fb0 620 mactcp_shutdown();
0c4b7799 621 exit(status);
d082ac49 622}
623
624void fatalbox(char *fmt, ...) {
625 va_list ap;
626 Str255 stuff;
627
628 va_start(ap, fmt);
629 /* We'd like stuff to be a Pascal string */
630 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
631 va_end(ap);
632 ParamText(stuff, NULL, NULL, NULL);
f3ab148c 633 StopAlert(128, NULL);
d082ac49 634 exit(1);
635}
636
637void modalfatalbox(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);
f3ab148c 646 StopAlert(128, NULL);
d082ac49 647 exit(1);
648}
649
2beb0fb0 650/* This should only kill the current session, not the whole application. */
651void connection_fatal(void *fontend, 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
0c4b7799 664/* Null SSH agent client -- never finds an agent. */
665
666int agent_exists(void)
667{
668
669 return FALSE;
670}
671
672void agent_query(void *in, int inlen, void **out, int *outlen)
673{
674
675 *out = NULL;
676 *outlen = 0;
677}
678
679/* Temporary null routines for testing. */
680
681void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
682 char *keystr, char *fingerprint)
683{
684
685}
686
687void askcipher(void *frontend, char *ciphername, int cs)
688{
689
690}
691
692void old_keyfile_warning(void)
693{
694
695}
696
5a9eb105 697char *platform_default_s(char *name)
698{
a499fbf9 699 long smfs;
700 Str255 pname;
701 static char cname[256];
702
703 if (!strcmp(name, "Font")) {
704 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
705 if (smfs == 0)
706 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
707 if (smfs != 0) {
708 GetFontName(HiWord(smfs), pname);
709 if (pname[0] == 0)
710 return "Monaco";
711 p2cstrcpy(cname, pname);
712 return cname;
713 } else
714 return "Monaco";
715 }
5a9eb105 716 return NULL;
717}
718
719int platform_default_i(char *name, int def)
720{
a499fbf9 721 long smfs;
722
723 if (!strcmp(name, "FontHeight")) {
724 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
725 if (smfs == 0)
726 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
727 if (smfs != 0)
728 return LoWord(smfs);
729 else
730 return 9;
731 }
732
5a9eb105 733 /* Non-raw cut and paste of line-drawing chars works badly on the
734 * current Unix stub implementation of the Unicode functions.
735 * So I'm going to temporarily set the default to raw mode so
736 * that the failure mode isn't quite so drastically horrid.
737 * When Unicode comes in, this can all be put right. */
738 if (!strcmp(name, "RawCNP"))
739 return 1;
740 return def;
741}
742
e0e7dff8 743void platform_get_x11_auth(char *display, int *proto,
744 unsigned char *data, int *datalen)
745{
746 /* SGT: I have no idea whether Mac X servers need anything here. */
747}
748
d082ac49 749/*
750 * Local Variables:
751 * c-file-style: "simon"
752 * End:
753 */