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