1 /* $Id: mac.c,v 1.46 2003/02/07 01:33:24 ben Exp $ */
3 * Copyright (c) 1999 Ben Harris
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
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
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
28 * mac.c -- miscellaneous Mac-specific routines
32 #include <AEDataModel.h>
33 #include <AppleEvents.h>
34 #include <Quickdraw.h>
36 #include <MacWindows.h>
39 #include <Appearance.h>
40 #include <CodeFragments.h>
46 #include <Navigation.h>
47 #include <Resources.h>
49 #include <TextCommon.h>
50 #include <ToolUtils.h>
51 #include <UnicodeConverter.h>
56 #include <stdlib.h> /* putty.h needs size_t */
57 #include <stdio.h> /* for vsprintf */
59 #define PUTTY_DO_GLOBALS
66 #if TARGET_API_MAC_CARBON
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
73 const CFAllocatorRef kCFAllocatorDefault
= NULL
;
81 static int borednow
= FALSE
;
82 struct mac_gestalts mac_gestalts
;
84 static void mac_startup(void);
85 static void mac_eventloop(void);
86 #pragma noreturn (mac_eventloop)
87 static void mac_event(EventRecord
*);
88 static void mac_contentclick(WindowPtr
, EventRecord
*);
89 static void mac_growwindow(WindowPtr
, EventRecord
*);
90 static void mac_activatewindow(WindowPtr
, EventRecord
*);
91 static void mac_activateabout(WindowPtr
, EventRecord
*);
92 static void mac_updatewindow(WindowPtr
);
93 static void mac_updatelicence(WindowPtr
);
94 static void mac_keypress(EventRecord
*);
95 static int mac_windowtype(WindowPtr
);
96 static void mac_menucommand(long);
97 static void mac_openabout(void);
98 static void mac_openlicence(void);
99 static void mac_adjustcursor(RgnHandle
);
100 static void mac_adjustmenus(void);
101 static void mac_closewindow(WindowPtr
);
102 static void mac_zoomwindow(WindowPtr
, short);
103 #pragma noreturn (cleanup_exit)
110 struct mac_windows windows
;
112 int main (int argc
, char **argv
) {
118 #pragma noreturn (main)
120 static void mac_startup(void) {
124 #if !TARGET_API_MAC_CARBON
125 /* Init Memory Manager */
128 InitGraf(&qd
.thePort
);
129 /* Init Font Manager */
131 /* Init Window Manager */
133 /* Init Menu Manager */
137 /* Init Dialog Manager */
142 /* Get base system version (only used if there's no better selector) */
143 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
144 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
145 fatalbox("PuTTY requires System 7 or newer");
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;
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;
161 mac_gestalts
.cntlattr
= 0;
162 mac_gestalts
.windattr
= 0;
164 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
165 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
166 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
167 mac_gestalts
.cntlattr
= 0;
168 /* Mac OS 8.5 Window Manager? */
169 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
170 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
171 mac_gestalts
.windattr
= 0;
173 /* Text Encoding Conversion Manager? */
175 #if TARGET_RT_MAC_CFM
176 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
178 InitializeUnicodeConverter(NULL
) != noErr
||
180 TECGetInfo(&ti
) != noErr
)
181 mac_gestalts
.encvvers
= 0;
183 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
184 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
185 DisposeHandle((Handle
)ti
);
187 /* Navigation Services? */
188 if (NavServicesAvailable())
189 mac_gestalts
.navsvers
= NavLibraryVersion();
191 mac_gestalts
.navsvers
= 0;
195 /* We've been tested with the Appearance Manager */
196 if (mac_gestalts
.apprvers
!= 0)
197 RegisterAppearanceClient();
199 menuBar
= GetNewMBar(128);
201 fatalbox("Unable to create menu bar.");
203 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
207 windows
.about
= NULL
;
208 windows
.licence
= NULL
;
210 default_protocol
= be_default_protocol
;
211 /* Find the appropriate default port. */
214 default_port
= 0; /* illegal */
215 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
216 if (backends
[i
].protocol
== default_protocol
) {
217 default_port
= backends
[i
].backend
->default_port
;
221 flags
= FLAG_INTERACTIVE
;
223 #if !TARGET_API_MAC_CARBON
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
);
237 /* Install Apple Event handlers. */
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
);
244 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
245 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
248 static void mac_eventloop(void) {
255 mac_adjustcursor(cursrgn
);
256 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
257 mac_adjustcursor(cursrgn
);
268 static void mac_event(EventRecord
*event
) {
272 switch (event
->what
) {
274 part
= FindWindow(event
->where
, &window
);
278 mac_menucommand(MenuSelect(event
->where
));
280 #if !TARGET_API_MAC_CARBON
282 SystemClick(event
, window
);
286 if (window
!= FrontWindow())
287 /* XXX: check for movable modal dboxes? */
288 SelectWindow(window
);
290 mac_contentclick(window
, event
);
293 if (TrackGoAway(window
, event
->where
))
294 mac_closewindow(window
);
297 /* XXX: moveable modal check? */
298 #if TARGET_API_MAC_CARBON
302 GetQDGlobalsScreenBits(&screenBits
);
303 DragWindow(window
, event
->where
, &screenBits
.bounds
);
306 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
310 mac_growwindow(window
, event
);
314 if (TrackBox(window
, event
->where
, part
))
315 mac_zoomwindow(window
, part
);
324 mac_activatewindow((WindowPtr
)event
->message
, event
);
327 mac_updatewindow((WindowPtr
)event
->message
);
329 #if !TARGET_API_MAC_CARBON
331 if (HiWord(event
->message
) != noErr
) {
334 SetPt(&pt
, 120, 120);
335 DIBadMount(pt
, event
->message
);
339 case kHighLevelEvent
:
340 AEProcessAppleEvent(event
); /* errors? */
345 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
349 switch (mac_windowtype(window
)) {
351 mac_clickterm(window
, event
);
354 dialog
= GetDialogFromWindow(window
);
355 if (DialogSelect(event
, &dialog
, &item
))
363 mac_clickdlg(window
, event
);
368 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
370 switch (mac_windowtype(window
)) {
372 mac_growterm(window
, event
);
376 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
379 active
= (event
->modifiers
& activeFlag
) != 0;
381 switch (mac_windowtype(window
)) {
383 mac_activateterm(window
, active
);
386 mac_activatedlg(window
, event
);
389 mac_activateabout(window
, event
);
394 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
396 DialogItemType itemtype
;
402 dialog
= GetDialogFromWindow(window
);
403 active
= (event
->modifiers
& activeFlag
) != 0;
404 GetDialogItem(dialog
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
405 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
406 DialogSelect(event
, &dialog
, &item
);
409 static void mac_updatewindow(WindowPtr window
)
411 #if TARGET_API_MAC_CARBON
415 switch (mac_windowtype(window
)) {
417 mac_updateterm(window
);
422 #if TARGET_API_MAC_CARBON
424 GetPortVisibleRegion(GetWindowPort(window
), rgn
);
425 UpdateDialog(GetDialogFromWindow(window
), rgn
);
428 UpdateDialog(window
, window
->visRgn
);
433 mac_updatelicence(window
);
438 static void mac_updatelicence(WindowPtr window
)
445 SetPort((GrafPtr
)GetWindowPort(window
));
447 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
448 TextFont(HiWord(fondsize
));
449 TextSize(LoWord(fondsize
));
450 h
= Get1Resource('TEXT', wLicence
);
451 len
= GetResourceSizeOnDisk(h
);
452 #if TARGET_API_MAC_CARBON
453 GetPortBounds(GetWindowPort(window
), &textrect
);
455 textrect
= window
->portRect
;
459 TETextBox(*h
, len
, &textrect
, teFlushDefault
);
466 * Work out what kind of window we're dealing with.
468 static int mac_windowtype(WindowPtr window
)
471 #if !TARGET_API_MAC_CARBON
472 if (GetWindowKind(window
) < 0)
475 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
481 static void mac_keypress(EventRecord
*event
) {
484 window
= FrontWindow();
486 * Check for a command-key combination, but ignore it if it counts
487 * as a meta-key combination and we're in a terminal window.
489 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
490 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
491 mac_windowtype(window) == wTerminal)*/) {
493 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
495 switch (mac_windowtype(window
)) {
497 mac_keyterm(window
, event
);
503 static void mac_menucommand(long result
) {
506 #if !TARGET_API_MAC_CARBON
510 menu
= HiWord(result
);
511 item
= LoWord(result
);
512 window
= FrontWindow();
513 /* Things which do the same whatever window we're in. */
520 #if !TARGET_API_MAC_CARBON
522 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
537 mac_closewindow(window
);
554 /* If we get here, handling is up to window-specific code. */
555 switch (mac_windowtype(window
)) {
557 mac_menuterm(window
, menu
, item
);
564 static void mac_openabout(void) {
565 DialogItemType itemtype
;
573 SelectWindow(windows
.about
);
576 GetDialogWindow(GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1));
577 wi
= smalloc(sizeof(*wi
));
580 SetWRefCon(windows
.about
, (long)wi
);
581 vers
= (VersRecHndl
)Get1Resource('vers', 1);
582 if (vers
!= NULL
&& *vers
!= NULL
) {
583 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
584 GetDialogItem(GetDialogFromWindow(windows
.about
), wiAboutVersion
,
585 &itemtype
, &item
, &box
);
586 assert(itemtype
& kStaticTextDialogItem
);
587 SetDialogItemText(item
, longvers
);
589 ShowWindow(windows
.about
);
593 static void mac_openlicence(void) {
597 SelectWindow(windows
.licence
);
599 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
600 wi
= smalloc(sizeof(*wi
));
602 wi
->wtype
= wLicence
;
603 SetWRefCon(windows
.licence
, (long)wi
);
604 ShowWindow(windows
.licence
);
608 static void mac_closewindow(WindowPtr window
) {
610 switch (mac_windowtype(window
)) {
611 #if !TARGET_API_MAC_CARBON
613 CloseDeskAcc(GetWindowKind(window
));
617 mac_closeterm(window
);
620 windows
.about
= NULL
;
621 DisposeDialog(GetDialogFromWindow(window
));
624 windows
.licence
= NULL
;
625 DisposeWindow(window
);
630 static void mac_zoomwindow(WindowPtr window
, short part
) {
632 /* FIXME: do something */
636 * Make the menus look right before the user gets to see them.
638 #if TARGET_API_MAC_CARBON
639 #define EnableItem EnableMenuItem
640 #define DisableItem DisableMenuItem
642 static void mac_adjustmenus(void) {
646 window
= FrontWindow();
647 menu
= GetMenuHandle(mApple
);
649 EnableItem(menu
, iAbout
);
651 menu
= GetMenuHandle(mFile
);
653 EnableItem(menu
, iNew
);
655 EnableItem(menu
, iClose
);
657 DisableItem(menu
, iClose
);
658 EnableItem(menu
, iQuit
);
660 switch (mac_windowtype(window
)) {
662 DisableItem(menu
, iSave
); /* XXX enable if modified */
663 EnableItem(menu
, iSaveAs
);
664 EnableItem(menu
, iDuplicate
);
665 menu
= GetMenuHandle(mEdit
);
666 DisableItem(menu
, 0);
669 mac_adjusttermmenus(window
);
672 DisableItem(menu
, iSave
);
673 DisableItem(menu
, iSaveAs
);
674 DisableItem(menu
, iDuplicate
);
675 menu
= GetMenuHandle(mEdit
);
676 DisableItem(menu
, 0);
683 * Make sure the right cursor's being displayed.
685 static void mac_adjustcursor(RgnHandle cursrgn
) {
687 WindowPtr window
, front
;
689 #if TARGET_API_MAC_CARBON
695 LocalToGlobal(&mouse
);
696 part
= FindWindow(mouse
, &window
);
697 front
= FrontWindow();
698 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
699 /* Cursor isn't in the front window, so switch to arrow */
700 #if TARGET_API_MAC_CARBON
701 GetQDGlobalsArrow(&arrow
);
704 SetCursor(&qd
.arrow
);
706 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
708 #if TARGET_API_MAC_CARBON
710 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
711 DiffRgn(cursrgn
, visrgn
, cursrgn
);
714 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
718 switch (mac_windowtype(window
)) {
720 mac_adjusttermcursor(window
, mouse
, cursrgn
);
723 #if TARGET_API_MAC_CARBON
724 GetQDGlobalsArrow(&arrow
);
726 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
728 SetCursor(&qd
.arrow
);
729 CopyRgn(window
->visRgn
, cursrgn
);
736 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
742 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
743 &type
, NULL
, 0, &size
) == noErr
)
744 return errAEParamMissed
;
750 void cleanup_exit(int status
)
753 #if !TARGET_RT_MAC_CFM
754 if (mac_gestalts
.encvvers
!= 0)
755 TerminateUnicodeConverter();
761 void fatalbox(char *fmt
, ...) {
766 /* We'd like stuff to be a Pascal string */
767 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
769 ParamText(stuff
, NULL
, NULL
, NULL
);
770 StopAlert(128, NULL
);
774 void modalfatalbox(char *fmt
, ...) {
779 /* We'd like stuff to be a Pascal string */
780 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
782 ParamText(stuff
, NULL
, NULL
, NULL
);
783 StopAlert(128, NULL
);
787 /* This should only kill the current session, not the whole application. */
788 void connection_fatal(void *fontend
, char *fmt
, ...) {
793 /* We'd like stuff to be a Pascal string */
794 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
796 ParamText(stuff
, NULL
, NULL
, NULL
);
797 StopAlert(128, NULL
);
801 /* Null SSH agent client -- never finds an agent. */
803 int agent_exists(void)
809 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
816 /* Temporary null routines for testing. */
818 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
819 char *keystr
, char *fingerprint
)
824 void askcipher(void *frontend
, char *ciphername
, int cs
)
829 void old_keyfile_warning(void)
834 FontSpec
platform_default_fontspec(char const *name
)
839 if (!strcmp(name
, "Font")) {
840 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
842 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
844 GetFontName(HiWord(smfs
), ret
.name
);
845 if (ret
.name
[0] == 0)
846 memcpy(ret
.name
, "\pMonaco", 7);
847 ret
.size
= LoWord(smfs
);
849 memcpy(ret
.name
, "\pMonaco", 7);
860 Filename
platform_default_filename(const char *name
)
863 if (!strcmp(name
, "LogFileName"))
864 FSMakeFSSpec(0, 0, "\pputty.log", &ret
.fss
);
866 memset(&ret
, 0, sizeof(ret
));
870 char *platform_default_s(char const *name
)
875 int platform_default_i(char const *name
, int def
)
878 /* Non-raw cut and paste of line-drawing chars works badly on the
879 * current Unix stub implementation of the Unicode functions.
880 * So I'm going to temporarily set the default to raw mode so
881 * that the failure mode isn't quite so drastically horrid.
882 * When Unicode comes in, this can all be put right. */
883 if (!strcmp(name
, "RawCNP"))
888 void platform_get_x11_auth(char *display
, int *proto
,
889 unsigned char *data
, int *datalen
)
891 /* SGT: I have no idea whether Mac X servers need anything here. */
894 Filename
filename_from_str(const char *str
)
899 /* XXX This fails for filenames over 255 characters long. */
901 FSMakeFSSpec(0, 0, tmp
, &ret
.fss
);
906 * Convert a filename to a string for display purposes.
907 * See pp 2-44--2-46 of IM:Files
909 * XXX static storage considered harmful
911 const char *filename_to_str(const Filename
*fn
)
916 static char *path
= NULL
;
919 if (path
!= NULL
) sfree(path
);
920 path
= smalloc(fn
->fss
.name
[0]);
921 p2cstrcpy(path
, fn
->fss
.name
);
922 pb
.dirInfo
.ioNamePtr
= dirname
;
923 pb
.dirInfo
.ioVRefNum
= fn
->fss
.vRefNum
;
924 pb
.dirInfo
.ioDrParID
= fn
->fss
.parID
;
925 pb
.dirInfo
.ioFDirIndex
= -1;
927 pb
.dirInfo
.ioDrDirID
= pb
.dirInfo
.ioDrParID
;
928 err
= PBGetCatInfoSync(&pb
);
930 /* XXX Assume not A/UX */
931 newpath
= smalloc(strlen(path
) + dirname
[0] + 2);
932 p2cstrcpy(newpath
, dirname
);
933 strcat(newpath
, ":");
934 strcat(newpath
, path
);
937 } while (pb
.dirInfo
.ioDrDirID
!= fsRtDirID
);
941 int filename_equal(Filename f1
, Filename f2
)
944 return f1
.fss
.vRefNum
== f2
.fss
.vRefNum
&&
945 f1
.fss
.parID
== f2
.fss
.parID
&&
946 f1
.fss
.name
[0] == f2
.fss
.name
[0] &&
947 memcmp(f1
.fss
.name
+ 1, f2
.fss
.name
+ 1, f1
.fss
.name
[0]) == 0;
950 int filename_is_null(Filename fn
)
953 return fn
.fss
.vRefNum
== 0 && fn
.fss
.parID
== 0 && fn
.fss
.name
[0] == 0;
956 FILE *f_open(Filename fn
, char const *mode
)
963 HGetVol(NULL
, &savevol
, &savedir
);
964 if (HSetVol(NULL
, fn
.fss
.vRefNum
, fn
.fss
.parID
) == noErr
) {
965 p2cstrcpy(tmp
, fn
.fss
.name
);
966 ret
= fopen(tmp
, mode
);
969 HSetVol(NULL
, savevol
, savedir
);
975 * c-file-style: "simon"