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