Fix various breakages my Carbonisation caused in the Classic build, notably
[u/mdw/putty] / mac / mac.c
CommitLineData
cc2be455 1/* $Id: mac.c,v 1.44 2003/02/04 00:33:11 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>
97cfddb2 32#include <AEDataModel.h>
33#include <AppleEvents.h>
d082ac49 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>
bf873e8e 45#include <LowMem.h>
9ef3c1ca 46#include <Navigation.h>
d082ac49 47#include <Resources.h>
36577dc9 48#include <Script.h>
8768ce31 49#include <TextCommon.h>
d082ac49 50#include <ToolUtils.h>
8768ce31 51#include <UnicodeConverter.h>
d082ac49 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"
0c4b7799 63#include "ssh.h"
d082ac49 64#include "mac.h"
65
fc34fbaf 66#if !TARGET_API_MAC_CARBON
d082ac49 67QDGlobals qd;
fc34fbaf 68#endif
d082ac49 69
0c4b7799 70Session *sesslist;
71
d082ac49 72static int cold = 1;
97cfddb2 73static int borednow = FALSE;
d082ac49 74struct mac_gestalts mac_gestalts;
75
76static void mac_startup(void);
77static void mac_eventloop(void);
78#pragma noreturn (mac_eventloop)
79static void mac_event(EventRecord *);
80static void mac_contentclick(WindowPtr, EventRecord *);
81static void mac_growwindow(WindowPtr, EventRecord *);
82static void mac_activatewindow(WindowPtr, EventRecord *);
83static void mac_activateabout(WindowPtr, EventRecord *);
84static void mac_updatewindow(WindowPtr);
ebd78b62 85static void mac_updatelicence(WindowPtr);
d082ac49 86static void mac_keypress(EventRecord *);
87static int mac_windowtype(WindowPtr);
88static void mac_menucommand(long);
89static void mac_openabout(void);
ebd78b62 90static void mac_openlicence(void);
d082ac49 91static void mac_adjustcursor(RgnHandle);
92static void mac_adjustmenus(void);
93static void mac_closewindow(WindowPtr);
94static void mac_zoomwindow(WindowPtr, short);
0c4b7799 95#pragma noreturn (cleanup_exit)
d082ac49 96
97struct mac_windows {
98 WindowPtr about;
99 WindowPtr licence;
100};
101
102struct mac_windows windows;
103
104int main (int argc, char **argv) {
105
106 mac_startup();
107 mac_eventloop();
108}
109
110#pragma noreturn (main)
111
112static void mac_startup(void) {
113 Handle menuBar;
8768ce31 114 TECInfoHandle ti;
d082ac49 115
fc34fbaf 116#if !TARGET_API_MAC_CARBON
d082ac49 117 /* Init Memory Manager */
118 MaxApplZone();
119 /* Init QuickDraw */
120 InitGraf(&qd.thePort);
121 /* Init Font Manager */
122 InitFonts();
123 /* Init Window Manager */
124 InitWindows();
125 /* Init Menu Manager */
126 InitMenus();
127 /* Init TextEdit */
128 TEInit();
129 /* Init Dialog Manager */
f3ab148c 130 InitDialogs(NULL);
fc34fbaf 131#endif
d082ac49 132 cold = 0;
133
56ed4cf7 134 /* Get base system version (only used if there's no better selector) */
135 if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
6e1063b1 136 (mac_gestalts.sysvers &= 0xffff) < 0x700)
56ed4cf7 137 fatalbox("PuTTY requires System 7 or newer");
d082ac49 138 /* Find out if we've got Color Quickdraw */
139 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
140 mac_gestalts.qdvers = gestaltOriginalQD;
141 /* ... and the Appearance Manager? */
142 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
143 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
144 mac_gestalts.apprvers = 0x0100;
145 else
146 mac_gestalts.apprvers = 0;
147#if TARGET_RT_MAC_CFM
148 /* Paranoia: Did we manage to pull in AppearanceLib? */
149 if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
150 mac_gestalts.apprvers = 0;
151#endif
1fc898ea 152#if TARGET_CPU_68K
153 mac_gestalts.cntlattr = 0;
154 mac_gestalts.windattr = 0;
155#else
d082ac49 156 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
1fc898ea 157 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr ||
158 &SetControlViewSize == kUnresolvedCFragSymbolAddress)
d082ac49 159 mac_gestalts.cntlattr = 0;
160 /* Mac OS 8.5 Window Manager? */
1fc898ea 161 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
162 &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
d082ac49 163 mac_gestalts.windattr = 0;
1fc898ea 164#endif
8768ce31 165 /* Text Encoding Conversion Manager? */
166 if (
167#if TARGET_RT_MAC_CFM
168 &TECGetInfo == kUnresolvedCFragSymbolAddress ||
169#else
170 InitializeUnicodeConverter(NULL) != noErr ||
171#endif
172 TECGetInfo(&ti) != noErr)
173 mac_gestalts.encvvers = 0;
174 else {
175 mac_gestalts.encvvers = (*ti)->tecVersion;
379836ca 176 mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
8768ce31 177 DisposeHandle((Handle)ti);
178 }
9ef3c1ca 179 /* Navigation Services? */
180 if (NavServicesAvailable())
181 mac_gestalts.navsvers = NavLibraryVersion();
182 else
183 mac_gestalts.navsvers = 0;
97cfddb2 184
f6fc0010 185 sk_init();
2beb0fb0 186
d082ac49 187 /* We've been tested with the Appearance Manager */
188 if (mac_gestalts.apprvers != 0)
189 RegisterAppearanceClient();
190
191 menuBar = GetNewMBar(128);
192 if (menuBar == NULL)
193 fatalbox("Unable to create menu bar.");
194 SetMenuBar(menuBar);
195 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
196 mac_adjustmenus();
197 DrawMenuBar();
198 InitCursor();
199 windows.about = NULL;
200 windows.licence = NULL;
201
ffa79828 202 default_protocol = be_default_protocol;
203 /* Find the appropriate default port. */
204 {
ffa79828 205 int i;
1adaeb2e 206 default_port = 0; /* illegal */
ffa79828 207 for (i = 0; backends[i].backend != NULL; i++)
208 if (backends[i].protocol == default_protocol) {
209 default_port = backends[i].backend->default_port;
210 break;
211 }
212 }
0c4b7799 213 flags = FLAG_INTERACTIVE;
214
fc34fbaf 215#if !TARGET_API_MAC_CARBON
bf873e8e 216 {
217 short vol;
218 long dirid;
219
220 /* Set the default directory for loading and saving settings. */
221 /* XXX Should we create it? */
222 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
223 LMSetSFSaveDisk(-vol);
224 LMSetCurDirStore(dirid);
225 }
226 }
fc34fbaf 227#endif
97cfddb2 228
229 /* Install Apple Event handlers. */
d70e3b83 230 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
231 NewAEEventHandlerUPP(&mac_aevt_oapp), 0, FALSE);
232 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
233 NewAEEventHandlerUPP(&mac_aevt_odoc), 0, FALSE);
234 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
235 NewAEEventHandlerUPP(&mac_aevt_pdoc), 0, FALSE);
97cfddb2 236 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
237 NewAEEventHandlerUPP(&mac_aevt_quit), 0, FALSE);
d082ac49 238}
239
240static void mac_eventloop(void) {
241 Boolean gotevent;
242 EventRecord event;
243 RgnHandle cursrgn;
244
245 cursrgn = NewRgn();
246 for (;;) {
247 mac_adjustcursor(cursrgn);
248 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
249 mac_adjustcursor(cursrgn);
250 if (gotevent)
251 mac_event(&event);
97cfddb2 252 if (borednow)
253 cleanup_exit(0);
f6fc0010 254 sk_poll();
0c4b7799 255 mac_pollterm();
d082ac49 256 }
257 DisposeRgn(cursrgn);
258}
259
260static void mac_event(EventRecord *event) {
261 short part;
262 WindowPtr window;
d082ac49 263
264 switch (event->what) {
265 case mouseDown:
266 part = FindWindow(event->where, &window);
267 switch (part) {
268 case inMenuBar:
269 mac_adjustmenus();
270 mac_menucommand(MenuSelect(event->where));
271 break;
fc34fbaf 272#if !TARGET_API_MAC_CARBON
d082ac49 273 case inSysWindow:
274 SystemClick(event, window);
275 break;
fc34fbaf 276#endif
d082ac49 277 case inContent:
278 if (window != FrontWindow())
279 /* XXX: check for movable modal dboxes? */
280 SelectWindow(window);
281 else
282 mac_contentclick(window, event);
283 break;
284 case inGoAway:
285 if (TrackGoAway(window, event->where))
286 mac_closewindow(window);
287 break;
288 case inDrag:
289 /* XXX: moveable modal check? */
fc34fbaf 290#if TARGET_API_MAC_CARBON
291 {
292 BitMap screenBits;
293
294 GetQDGlobalsScreenBits(&screenBits);
295 DragWindow(window, event->where, &screenBits.bounds);
296 }
297#else
d082ac49 298 DragWindow(window, event->where, &qd.screenBits.bounds);
fc34fbaf 299#endif
d082ac49 300 break;
301 case inGrow:
302 mac_growwindow(window, event);
303 break;
304 case inZoomIn:
305 case inZoomOut:
306 if (TrackBox(window, event->where, part))
307 mac_zoomwindow(window, part);
308 break;
309 }
310 break;
311 case keyDown:
312 case autoKey:
313 mac_keypress(event);
314 break;
315 case activateEvt:
316 mac_activatewindow((WindowPtr)event->message, event);
317 break;
318 case updateEvt:
319 mac_updatewindow((WindowPtr)event->message);
320 break;
fc34fbaf 321#if !TARGET_API_MAC_CARBON
d082ac49 322 case diskEvt:
323 if (HiWord(event->message) != noErr) {
fc34fbaf 324 Point pt;
325
d082ac49 326 SetPt(&pt, 120, 120);
327 DIBadMount(pt, event->message);
328 }
329 break;
fc34fbaf 330#endif
97cfddb2 331 case kHighLevelEvent:
332 AEProcessAppleEvent(event); /* errors? */
333 break;
d082ac49 334 }
335}
336
337static void mac_contentclick(WindowPtr window, EventRecord *event) {
338 short item;
fc34fbaf 339 DialogRef dialog;
d082ac49 340
341 switch (mac_windowtype(window)) {
342 case wTerminal:
343 mac_clickterm(window, event);
344 break;
345 case wAbout:
fc34fbaf 346 dialog = GetDialogFromWindow(window);
347 if (DialogSelect(event, &dialog, &item))
d082ac49 348 switch (item) {
349 case wiAboutLicence:
ebd78b62 350 mac_openlicence();
d082ac49 351 break;
352 }
353 break;
6cb61a05 354 case wSettings:
355 mac_clickdlg(window, event);
356 break;
d082ac49 357 }
358}
359
360static void mac_growwindow(WindowPtr window, EventRecord *event) {
361
362 switch (mac_windowtype(window)) {
363 case wTerminal:
364 mac_growterm(window, event);
365 }
366}
367
368static void mac_activatewindow(WindowPtr window, EventRecord *event) {
369 int active;
370
371 active = (event->modifiers & activeFlag) != 0;
372 mac_adjustmenus();
373 switch (mac_windowtype(window)) {
374 case wTerminal:
375 mac_activateterm(window, active);
376 break;
6cb61a05 377 case wSettings:
378 mac_activatedlg(window, event);
379 break;
d082ac49 380 case wAbout:
381 mac_activateabout(window, event);
382 break;
383 }
384}
385
386static void mac_activateabout(WindowPtr window, EventRecord *event) {
fc34fbaf 387 DialogRef dialog;
d082ac49 388 DialogItemType itemtype;
389 Handle itemhandle;
390 short item;
391 Rect itemrect;
392 int active;
393
fc34fbaf 394 dialog = GetDialogFromWindow(window);
d082ac49 395 active = (event->modifiers & activeFlag) != 0;
fc34fbaf 396 GetDialogItem(dialog, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
d082ac49 397 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
fc34fbaf 398 DialogSelect(event, &dialog, &item);
d082ac49 399}
400
fc34fbaf 401static void mac_updatewindow(WindowPtr window)
402{
403#if TARGET_API_MAC_CARBON
404 RgnHandle rgn;
405#endif
d082ac49 406
407 switch (mac_windowtype(window)) {
408 case wTerminal:
409 mac_updateterm(window);
410 break;
411 case wAbout:
6cb61a05 412 case wSettings:
d082ac49 413 BeginUpdate(window);
fc34fbaf 414#if TARGET_API_MAC_CARBON
415 rgn = NewRgn();
416 GetPortVisibleRegion(GetWindowPort(window), rgn);
417 UpdateDialog(GetDialogFromWindow(window), rgn);
418 DisposeRgn(rgn);
419#else
d082ac49 420 UpdateDialog(window, window->visRgn);
fc34fbaf 421#endif
d082ac49 422 EndUpdate(window);
423 break;
424 case wLicence:
ebd78b62 425 mac_updatelicence(window);
426 break;
427 }
428}
429
430static void mac_updatelicence(WindowPtr window)
431{
432 Handle h;
433 int len;
36577dc9 434 long fondsize;
fc34fbaf 435 Rect textrect;
ebd78b62 436
fc34fbaf 437 SetPort((GrafPtr)GetWindowPort(window));
ebd78b62 438 BeginUpdate(window);
36577dc9 439 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
440 TextFont(HiWord(fondsize));
441 TextSize(LoWord(fondsize));
ebd78b62 442 h = Get1Resource('TEXT', wLicence);
443 len = GetResourceSizeOnDisk(h);
fc34fbaf 444#if TARGET_API_MAC_CARBON
445 GetPortBounds(GetWindowPort(window), &textrect);
446#else
447 textrect = window->portRect;
448#endif
ebd78b62 449 if (h != NULL) {
450 HLock(h);
fc34fbaf 451 TETextBox(*h, len, &textrect, teFlushDefault);
ebd78b62 452 HUnlock(h);
d082ac49 453 }
ebd78b62 454 EndUpdate(window);
d082ac49 455}
456
457/*
458 * Work out what kind of window we're dealing with.
459 * Concept shamelessly nicked from SurfWriter.
460 */
461static int mac_windowtype(WindowPtr window) {
462 int kind;
6cb61a05 463 long refcon;
d082ac49 464
465 if (window == NULL)
466 return wNone;
fc34fbaf 467 kind = GetWindowKind(window);
d082ac49 468 if (kind < 0)
469 return wDA;
470 if (GetWVariant(window) == zoomDocProc)
471 return wTerminal;
6cb61a05 472 refcon = GetWRefCon(window);
473 if (refcon < 1024)
474 return refcon;
475 else
476 return wSettings;
d082ac49 477}
478
479/*
480 * Handle a key press
481 */
482static void mac_keypress(EventRecord *event) {
483 WindowPtr window;
484
485 window = FrontWindow();
486 /*
487 * Check for a command-key combination, but ignore it if it counts
488 * as a meta-key combination and we're in a terminal window.
489 */
490 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
491 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
492 mac_windowtype(window) == wTerminal)*/) {
493 mac_adjustmenus();
494 mac_menucommand(MenuKey(event->message & charCodeMask));
495 } else {
496 switch (mac_windowtype(window)) {
497 case wTerminal:
498 mac_keyterm(window, event);
499 break;
500 }
501 }
502}
503
504static void mac_menucommand(long result) {
505 short menu, item;
d082ac49 506 WindowPtr window;
fc34fbaf 507#if !TARGET_API_MAC_CARBON
508 Str255 da;
509#endif
d082ac49 510
511 menu = HiWord(result);
512 item = LoWord(result);
513 window = FrontWindow();
514 /* Things which do the same whatever window we're in. */
515 switch (menu) {
516 case mApple:
517 switch (item) {
518 case iAbout:
519 mac_openabout();
520 goto done;
cc2be455 521#if !TARGET_API_MAC_CARBON
d082ac49 522 default:
523 GetMenuItemText(GetMenuHandle(mApple), item, da);
524 OpenDeskAcc(da);
525 goto done;
cc2be455 526#endif
d082ac49 527 }
528 break;
529 case mFile:
530 switch (item) {
531 case iNew:
532 mac_newsession();
533 goto done;
ce283213 534 case iOpen:
535 mac_opensession();
536 goto done;
d082ac49 537 case iClose:
538 mac_closewindow(window);
539 goto done;
b537dd42 540 case iSave:
541 mac_savesession();
542 goto done;
543 case iSaveAs:
544 mac_savesessionas();
545 goto done;
5211281f 546 case iDuplicate:
547 mac_dupsession();
548 goto done;
d082ac49 549 case iQuit:
0c4b7799 550 cleanup_exit(0);
d082ac49 551 goto done;
552 }
553 break;
554 }
555 /* If we get here, handling is up to window-specific code. */
556 switch (mac_windowtype(window)) {
557 case wTerminal:
558 mac_menuterm(window, menu, item);
559 break;
560 }
561 done:
562 HiliteMenu(0);
563}
564
565static void mac_openabout(void) {
566 DialogItemType itemtype;
567 Handle item;
568 VersRecHndl vers;
569 Rect box;
570 StringPtr longvers;
571
572 if (windows.about)
573 SelectWindow(windows.about);
574 else {
fc34fbaf 575 windows.about =
576 GetDialogWindow(GetNewDialog(wAbout, NULL, (WindowPtr)-1));
5dbc118e 577 vers = (VersRecHndl)Get1Resource('vers', 1);
578 if (vers != NULL && *vers != NULL) {
579 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
fc34fbaf 580 GetDialogItem(GetDialogFromWindow(windows.about), wiAboutVersion,
5dbc118e 581 &itemtype, &item, &box);
582 assert(itemtype & kStaticTextDialogItem);
583 SetDialogItemText(item, longvers);
584 }
d082ac49 585 ShowWindow(windows.about);
586 }
587}
588
ebd78b62 589static void mac_openlicence(void) {
ebd78b62 590
591 if (windows.licence)
592 SelectWindow(windows.licence);
593 else {
594 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
595 ShowWindow(windows.licence);
596 }
597}
598
d082ac49 599static void mac_closewindow(WindowPtr window) {
600
601 switch (mac_windowtype(window)) {
fc34fbaf 602#if !TARGET_API_MAC_CARBON
d082ac49 603 case wDA:
604 CloseDeskAcc(((WindowPeek)window)->windowKind);
605 break;
fc34fbaf 606#endif
d082ac49 607 case wTerminal:
fabd1805 608 mac_closeterm(window);
d082ac49 609 break;
610 case wAbout:
611 windows.about = NULL;
fc34fbaf 612 DisposeDialog(GetDialogFromWindow(window));
d082ac49 613 break;
ebd78b62 614 case wLicence:
615 windows.licence = NULL;
fabd1805 616 DisposeWindow(window);
d082ac49 617 break;
618 }
619}
620
621static void mac_zoomwindow(WindowPtr window, short part) {
622
623 /* FIXME: do something */
624}
625
626/*
627 * Make the menus look right before the user gets to see them.
628 */
fc34fbaf 629#if TARGET_API_MAC_CARBON
fc34fbaf 630#define EnableItem EnableMenuItem
631#define DisableItem DisableMenuItem
632#endif
d082ac49 633static void mac_adjustmenus(void) {
634 WindowPtr window;
635 MenuHandle menu;
636
637 window = FrontWindow();
638 menu = GetMenuHandle(mApple);
639 EnableItem(menu, 0);
640 EnableItem(menu, iAbout);
641
642 menu = GetMenuHandle(mFile);
643 EnableItem(menu, 0);
644 EnableItem(menu, iNew);
645 if (window != NULL)
646 EnableItem(menu, iClose);
647 else
648 DisableItem(menu, iClose);
649 EnableItem(menu, iQuit);
650
651 switch (mac_windowtype(window)) {
b537dd42 652 case wSettings:
653 DisableItem(menu, iSave); /* XXX enable if modified */
654 EnableItem(menu, iSaveAs);
5211281f 655 EnableItem(menu, iDuplicate);
b537dd42 656 menu = GetMenuHandle(mEdit);
657 DisableItem(menu, 0);
658 break;
d082ac49 659 case wTerminal:
660 mac_adjusttermmenus(window);
661 break;
662 default:
b537dd42 663 DisableItem(menu, iSave);
664 DisableItem(menu, iSaveAs);
5211281f 665 DisableItem(menu, iDuplicate);
d082ac49 666 menu = GetMenuHandle(mEdit);
667 DisableItem(menu, 0);
668 break;
669 }
670 DrawMenuBar();
671}
672
673/*
674 * Make sure the right cursor's being displayed.
675 */
676static void mac_adjustcursor(RgnHandle cursrgn) {
677 Point mouse;
678 WindowPtr window, front;
679 short part;
fc34fbaf 680#if TARGET_API_MAC_CARBON
681 Cursor arrow;
682 RgnHandle visrgn;
683#endif
d082ac49 684
685 GetMouse(&mouse);
686 LocalToGlobal(&mouse);
687 part = FindWindow(mouse, &window);
688 front = FrontWindow();
689 if (part != inContent || window == NULL || window != front) {
690 /* Cursor isn't in the front window, so switch to arrow */
fc34fbaf 691#if TARGET_API_MAC_CARBON
692 GetQDGlobalsArrow(&arrow);
693 SetCursor(&arrow);
694#else
d082ac49 695 SetCursor(&qd.arrow);
fc34fbaf 696#endif
d082ac49 697 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
fc34fbaf 698 if (front != NULL) {
699#if TARGET_API_MAC_CARBON
700 visrgn = NewRgn();
701 GetPortVisibleRegion(GetWindowPort(front), visrgn);
702 DiffRgn(cursrgn, visrgn, cursrgn);
703 DisposeRgn(visrgn);
704#else
d082ac49 705 DiffRgn(cursrgn, front->visRgn, cursrgn);
fc34fbaf 706#endif
707 }
d082ac49 708 } else {
709 switch (mac_windowtype(window)) {
710 case wTerminal:
711 mac_adjusttermcursor(window, mouse, cursrgn);
712 break;
713 default:
fc34fbaf 714#if TARGET_API_MAC_CARBON
715 GetQDGlobalsArrow(&arrow);
716 SetCursor(&arrow);
717 GetPortVisibleRegion(GetWindowPort(window), cursrgn);
718#else
d082ac49 719 SetCursor(&qd.arrow);
720 CopyRgn(window->visRgn, cursrgn);
fc34fbaf 721#endif
d082ac49 722 break;
723 }
724 }
725}
726
d70e3b83 727pascal OSErr mac_aevt_quit(const AppleEvent *req, AppleEvent *reply,
97cfddb2 728 long refcon)
729{
d70e3b83 730 DescType type;
731 Size size;
732
733 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
734 &type, NULL, 0, &size) == noErr)
735 return errAEParamMissed;
97cfddb2 736
737 borednow = 1;
738 return noErr;
739}
740
0c4b7799 741void cleanup_exit(int status)
742{
d082ac49 743
713b8b7a 744#if !TARGET_RT_MAC_CFM
8768ce31 745 if (mac_gestalts.encvvers != 0)
746 TerminateUnicodeConverter();
713b8b7a 747#endif
27a3458f 748 sk_cleanup();
0c4b7799 749 exit(status);
d082ac49 750}
751
752void fatalbox(char *fmt, ...) {
753 va_list ap;
754 Str255 stuff;
755
756 va_start(ap, fmt);
757 /* We'd like stuff to be a Pascal string */
758 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
759 va_end(ap);
760 ParamText(stuff, NULL, NULL, NULL);
f3ab148c 761 StopAlert(128, NULL);
04b0fa02 762 cleanup_exit(1);
d082ac49 763}
764
765void modalfatalbox(char *fmt, ...) {
766 va_list ap;
767 Str255 stuff;
768
769 va_start(ap, fmt);
770 /* We'd like stuff to be a Pascal string */
771 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
772 va_end(ap);
773 ParamText(stuff, NULL, NULL, NULL);
f3ab148c 774 StopAlert(128, NULL);
04b0fa02 775 cleanup_exit(1);
d082ac49 776}
777
2beb0fb0 778/* This should only kill the current session, not the whole application. */
779void connection_fatal(void *fontend, char *fmt, ...) {
780 va_list ap;
781 Str255 stuff;
782
783 va_start(ap, fmt);
784 /* We'd like stuff to be a Pascal string */
785 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
786 va_end(ap);
787 ParamText(stuff, NULL, NULL, NULL);
788 StopAlert(128, NULL);
04b0fa02 789 cleanup_exit(1);
2beb0fb0 790}
791
0c4b7799 792/* Null SSH agent client -- never finds an agent. */
793
794int agent_exists(void)
795{
796
797 return FALSE;
798}
799
800void agent_query(void *in, int inlen, void **out, int *outlen)
801{
802
803 *out = NULL;
804 *outlen = 0;
805}
806
807/* Temporary null routines for testing. */
808
809void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
810 char *keystr, char *fingerprint)
811{
812
813}
814
815void askcipher(void *frontend, char *ciphername, int cs)
816{
817
818}
819
820void old_keyfile_warning(void)
821{
822
823}
824
e3c5b245 825FontSpec platform_default_fontspec(char const *name)
5a9eb105 826{
9a30e26b 827 FontSpec ret;
a499fbf9 828 long smfs;
a499fbf9 829
830 if (!strcmp(name, "Font")) {
831 smfs = GetScriptVariable(smSystemScript, smScriptMonoFondSize);
832 if (smfs == 0)
833 smfs = GetScriptVariable(smRoman, smScriptMonoFondSize);
834 if (smfs != 0) {
e3c5b245 835 GetFontName(HiWord(smfs), ret.name);
836 if (ret.name[0] == 0)
837 memcpy(ret.name, "\pMonaco", 7);
838 ret.size = LoWord(smfs);
9a30e26b 839 } else {
e3c5b245 840 memcpy(ret.name, "\pMonaco", 7);
841 ret.size = 9;
9a30e26b 842 }
e3c5b245 843 ret.face = 0;
9a30e26b 844 } else {
e3c5b245 845 ret.name[0] = 0;
a499fbf9 846 }
9a30e26b 847
848 return ret;
849}
850
851Filename platform_default_filename(const char *name)
852{
853 Filename ret;
854 if (!strcmp(name, "LogFileName"))
02cf4001 855 FSMakeFSSpec(0, 0, "\pputty.log", &ret.fss);
9a30e26b 856 else
02cf4001 857 memset(&ret, 0, sizeof(ret));
9a30e26b 858 return ret;
859}
860
861char *platform_default_s(char const *name)
862{
5a9eb105 863 return NULL;
864}
865
f5d2d791 866int platform_default_i(char const *name, int def)
5a9eb105 867{
a499fbf9 868
5a9eb105 869 /* Non-raw cut and paste of line-drawing chars works badly on the
870 * current Unix stub implementation of the Unicode functions.
871 * So I'm going to temporarily set the default to raw mode so
872 * that the failure mode isn't quite so drastically horrid.
873 * When Unicode comes in, this can all be put right. */
874 if (!strcmp(name, "RawCNP"))
875 return 1;
876 return def;
877}
878
e0e7dff8 879void platform_get_x11_auth(char *display, int *proto,
880 unsigned char *data, int *datalen)
881{
882 /* SGT: I have no idea whether Mac X servers need anything here. */
883}
884
9fab77dc 885Filename filename_from_str(const char *str)
9a30e26b 886{
887 Filename ret;
02cf4001 888 Str255 tmp;
889
890 /* XXX This fails for filenames over 255 characters long. */
891 c2pstrcpy(tmp, str);
892 FSMakeFSSpec(0, 0, tmp, &ret.fss);
9a30e26b 893 return ret;
894}
895
02cf4001 896/*
897 * Convert a filename to a string for display purposes.
898 * See pp 2-44--2-46 of IM:Files
899 *
900 * XXX static storage considered harmful
901 */
9fab77dc 902const char *filename_to_str(const Filename *fn)
9a30e26b 903{
02cf4001 904 CInfoPBRec pb;
905 Str255 dirname;
906 OSErr err;
907 static char *path = NULL;
908 char *newpath;
909
910 if (path != NULL) sfree(path);
911 path = smalloc(fn->fss.name[0]);
912 p2cstrcpy(path, fn->fss.name);
913 pb.dirInfo.ioNamePtr = dirname;
914 pb.dirInfo.ioVRefNum = fn->fss.vRefNum;
915 pb.dirInfo.ioDrParID = fn->fss.parID;
916 pb.dirInfo.ioFDirIndex = -1;
917 do {
918 pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
919 err = PBGetCatInfoSync(&pb);
920
921 /* XXX Assume not A/UX */
922 newpath = smalloc(strlen(path) + dirname[0] + 2);
923 p2cstrcpy(newpath, dirname);
924 strcat(newpath, ":");
925 strcat(newpath, path);
926 sfree(path);
927 path = newpath;
928 } while (pb.dirInfo.ioDrDirID != fsRtDirID);
929 return path;
9a30e26b 930}
931
932int filename_equal(Filename f1, Filename f2)
933{
02cf4001 934
935 return f1.fss.vRefNum == f2.fss.vRefNum &&
936 f1.fss.parID == f2.fss.parID &&
937 f1.fss.name[0] == f2.fss.name[0] &&
938 memcmp(f1.fss.name + 1, f2.fss.name + 1, f1.fss.name[0]) == 0;
9a30e26b 939}
940
941int filename_is_null(Filename fn)
942{
02cf4001 943
944 return fn.fss.vRefNum == 0 && fn.fss.parID == 0 && fn.fss.name[0] == 0;
945}
946
947FILE *f_open(Filename fn, char const *mode)
948{
949 short savevol;
950 long savedir;
951 char tmp[256];
952 FILE *ret;
953
954 HGetVol(NULL, &savevol, &savedir);
955 if (HSetVol(NULL, fn.fss.vRefNum, fn.fss.parID) == noErr) {
956 p2cstrcpy(tmp, fn.fss.name);
957 ret = fopen(tmp, mode);
958 } else
959 ret = NULL;
960 HSetVol(NULL, savevol, savedir);
961 return ret;
9a30e26b 962}
963
d082ac49 964/*
965 * Local Variables:
966 * c-file-style: "simon"
967 * End:
968 */