1 /* $Id: mac.c,v 1.47 2003/02/07 01:38:12 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
);
366 mac_clickeventlog(window
, event
);
371 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
373 switch (mac_windowtype(window
)) {
375 mac_growterm(window
, event
);
378 mac_groweventlog(window
, event
);
383 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
386 active
= (event
->modifiers
& activeFlag
) != 0;
388 switch (mac_windowtype(window
)) {
390 mac_activateterm(window
, active
);
393 mac_activatedlg(window
, event
);
396 mac_activateabout(window
, event
);
399 mac_activateeventlog(window
, event
);
404 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
406 DialogItemType itemtype
;
412 dialog
= GetDialogFromWindow(window
);
413 active
= (event
->modifiers
& activeFlag
) != 0;
414 GetDialogItem(dialog
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
415 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
416 DialogSelect(event
, &dialog
, &item
);
419 static void mac_updatewindow(WindowPtr window
)
421 #if TARGET_API_MAC_CARBON
425 switch (mac_windowtype(window
)) {
427 mac_updateterm(window
);
432 #if TARGET_API_MAC_CARBON
434 GetPortVisibleRegion(GetWindowPort(window
), rgn
);
435 UpdateDialog(GetDialogFromWindow(window
), rgn
);
438 UpdateDialog(window
, window
->visRgn
);
443 mac_updatelicence(window
);
446 mac_updateeventlog(window
);
451 static void mac_updatelicence(WindowPtr window
)
458 SetPort((GrafPtr
)GetWindowPort(window
));
460 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
461 TextFont(HiWord(fondsize
));
462 TextSize(LoWord(fondsize
));
463 h
= Get1Resource('TEXT', wLicence
);
464 len
= GetResourceSizeOnDisk(h
);
465 #if TARGET_API_MAC_CARBON
466 GetPortBounds(GetWindowPort(window
), &textrect
);
468 textrect
= window
->portRect
;
472 TETextBox(*h
, len
, &textrect
, teFlushDefault
);
479 * Work out what kind of window we're dealing with.
481 static int mac_windowtype(WindowPtr window
)
484 #if !TARGET_API_MAC_CARBON
485 if (GetWindowKind(window
) < 0)
488 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
494 static void mac_keypress(EventRecord
*event
) {
497 window
= FrontWindow();
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.
502 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
503 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
504 mac_windowtype(window) == wTerminal)*/) {
506 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
508 switch (mac_windowtype(window
)) {
510 mac_keyterm(window
, event
);
516 static void mac_menucommand(long result
) {
519 #if !TARGET_API_MAC_CARBON
523 menu
= HiWord(result
);
524 item
= LoWord(result
);
525 window
= FrontWindow();
526 /* Things which do the same whatever window we're in. */
533 #if !TARGET_API_MAC_CARBON
535 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
550 mac_closewindow(window
);
567 /* If we get here, handling is up to window-specific code. */
568 switch (mac_windowtype(window
)) {
570 mac_menuterm(window
, menu
, item
);
577 static void mac_openabout(void) {
578 DialogItemType itemtype
;
586 SelectWindow(windows
.about
);
589 GetDialogWindow(GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1));
590 wi
= smalloc(sizeof(*wi
));
593 SetWRefCon(windows
.about
, (long)wi
);
594 vers
= (VersRecHndl
)Get1Resource('vers', 1);
595 if (vers
!= NULL
&& *vers
!= NULL
) {
596 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
597 GetDialogItem(GetDialogFromWindow(windows
.about
), wiAboutVersion
,
598 &itemtype
, &item
, &box
);
599 assert(itemtype
& kStaticTextDialogItem
);
600 SetDialogItemText(item
, longvers
);
602 ShowWindow(windows
.about
);
606 static void mac_openlicence(void) {
610 SelectWindow(windows
.licence
);
612 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
613 wi
= smalloc(sizeof(*wi
));
615 wi
->wtype
= wLicence
;
616 SetWRefCon(windows
.licence
, (long)wi
);
617 ShowWindow(windows
.licence
);
621 static void mac_closewindow(WindowPtr window
) {
623 switch (mac_windowtype(window
)) {
624 #if !TARGET_API_MAC_CARBON
626 CloseDeskAcc(GetWindowKind(window
));
630 mac_closeterm(window
);
633 windows
.about
= NULL
;
634 DisposeDialog(GetDialogFromWindow(window
));
637 windows
.licence
= NULL
;
638 DisposeWindow(window
);
643 static void mac_zoomwindow(WindowPtr window
, short part
) {
645 /* FIXME: do something */
649 * Make the menus look right before the user gets to see them.
651 #if TARGET_API_MAC_CARBON
652 #define EnableItem EnableMenuItem
653 #define DisableItem DisableMenuItem
655 static void mac_adjustmenus(void) {
659 window
= FrontWindow();
660 menu
= GetMenuHandle(mApple
);
662 EnableItem(menu
, iAbout
);
664 menu
= GetMenuHandle(mFile
);
666 EnableItem(menu
, iNew
);
668 EnableItem(menu
, iClose
);
670 DisableItem(menu
, iClose
);
671 EnableItem(menu
, iQuit
);
673 switch (mac_windowtype(window
)) {
675 DisableItem(menu
, iSave
); /* XXX enable if modified */
676 EnableItem(menu
, iSaveAs
);
677 EnableItem(menu
, iDuplicate
);
678 menu
= GetMenuHandle(mEdit
);
679 DisableItem(menu
, 0);
682 mac_adjusttermmenus(window
);
685 DisableItem(menu
, iSave
);
686 DisableItem(menu
, iSaveAs
);
687 DisableItem(menu
, iDuplicate
);
688 menu
= GetMenuHandle(mEdit
);
689 DisableItem(menu
, 0);
690 menu
= GetMenuHandle(mWindow
);
691 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
698 * Make sure the right cursor's being displayed.
700 static void mac_adjustcursor(RgnHandle cursrgn
) {
702 WindowPtr window
, front
;
704 #if TARGET_API_MAC_CARBON
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 */
715 #if TARGET_API_MAC_CARBON
716 GetQDGlobalsArrow(&arrow
);
719 SetCursor(&qd
.arrow
);
721 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
723 #if TARGET_API_MAC_CARBON
725 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
726 DiffRgn(cursrgn
, visrgn
, cursrgn
);
729 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
733 switch (mac_windowtype(window
)) {
735 mac_adjusttermcursor(window
, mouse
, cursrgn
);
738 #if TARGET_API_MAC_CARBON
739 GetQDGlobalsArrow(&arrow
);
741 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
743 SetCursor(&qd
.arrow
);
744 CopyRgn(window
->visRgn
, cursrgn
);
751 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
757 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
758 &type
, NULL
, 0, &size
) == noErr
)
759 return errAEParamMissed
;
765 void cleanup_exit(int status
)
768 #if !TARGET_RT_MAC_CFM
769 if (mac_gestalts
.encvvers
!= 0)
770 TerminateUnicodeConverter();
776 void fatalbox(char *fmt
, ...) {
781 /* We'd like stuff to be a Pascal string */
782 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
784 ParamText(stuff
, NULL
, NULL
, NULL
);
785 StopAlert(128, NULL
);
789 void modalfatalbox(char *fmt
, ...) {
794 /* We'd like stuff to be a Pascal string */
795 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
797 ParamText(stuff
, NULL
, NULL
, NULL
);
798 StopAlert(128, NULL
);
802 /* This should only kill the current session, not the whole application. */
803 void connection_fatal(void *fontend
, char *fmt
, ...) {
808 /* We'd like stuff to be a Pascal string */
809 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
811 ParamText(stuff
, NULL
, NULL
, NULL
);
812 StopAlert(128, NULL
);
816 /* Null SSH agent client -- never finds an agent. */
818 int agent_exists(void)
824 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
831 /* Temporary null routines for testing. */
833 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
834 char *keystr
, char *fingerprint
)
839 void askcipher(void *frontend
, char *ciphername
, int cs
)
844 void old_keyfile_warning(void)
849 FontSpec
platform_default_fontspec(char const *name
)
854 if (!strcmp(name
, "Font")) {
855 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
857 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
859 GetFontName(HiWord(smfs
), ret
.name
);
860 if (ret
.name
[0] == 0)
861 memcpy(ret
.name
, "\pMonaco", 7);
862 ret
.size
= LoWord(smfs
);
864 memcpy(ret
.name
, "\pMonaco", 7);
875 Filename
platform_default_filename(const char *name
)
878 if (!strcmp(name
, "LogFileName"))
879 FSMakeFSSpec(0, 0, "\pputty.log", &ret
.fss
);
881 memset(&ret
, 0, sizeof(ret
));
885 char *platform_default_s(char const *name
)
890 int platform_default_i(char const *name
, int def
)
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"))
903 void platform_get_x11_auth(char *display
, int *proto
,
904 unsigned char *data
, int *datalen
)
906 /* SGT: I have no idea whether Mac X servers need anything here. */
909 Filename
filename_from_str(const char *str
)
914 /* XXX This fails for filenames over 255 characters long. */
916 FSMakeFSSpec(0, 0, tmp
, &ret
.fss
);
921 * Convert a filename to a string for display purposes.
922 * See pp 2-44--2-46 of IM:Files
924 * XXX static storage considered harmful
926 const char *filename_to_str(const Filename
*fn
)
931 static char *path
= NULL
;
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;
942 pb
.dirInfo
.ioDrDirID
= pb
.dirInfo
.ioDrParID
;
943 err
= PBGetCatInfoSync(&pb
);
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
);
952 } while (pb
.dirInfo
.ioDrDirID
!= fsRtDirID
);
956 int filename_equal(Filename f1
, Filename f2
)
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;
965 int filename_is_null(Filename fn
)
968 return fn
.fss
.vRefNum
== 0 && fn
.fss
.parID
== 0 && fn
.fss
.name
[0] == 0;
971 FILE *f_open(Filename fn
, char const *mode
)
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
);
984 HSetVol(NULL
, savevol
, savedir
);
990 * c-file-style: "simon"