1 /* $Id: mac.c,v 1.42 2003/02/01 23:42:30 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 <Resources.h>
48 #include <TextCommon.h>
49 #include <ToolUtils.h>
50 #include <UnicodeConverter.h>
55 #include <stdlib.h> /* putty.h needs size_t */
56 #include <stdio.h> /* for vsprintf */
58 #define PUTTY_DO_GLOBALS
65 #if !TARGET_API_MAC_CARBON
72 static int borednow
= FALSE
;
73 struct mac_gestalts mac_gestalts
;
75 static void mac_startup(void);
76 static void mac_eventloop(void);
77 #pragma noreturn (mac_eventloop)
78 static void mac_event(EventRecord
*);
79 static void mac_contentclick(WindowPtr
, EventRecord
*);
80 static void mac_growwindow(WindowPtr
, EventRecord
*);
81 static void mac_activatewindow(WindowPtr
, EventRecord
*);
82 static void mac_activateabout(WindowPtr
, EventRecord
*);
83 static void mac_updatewindow(WindowPtr
);
84 static void mac_updatelicence(WindowPtr
);
85 static void mac_keypress(EventRecord
*);
86 static int mac_windowtype(WindowPtr
);
87 static void mac_menucommand(long);
88 static void mac_openabout(void);
89 static void mac_openlicence(void);
90 static void mac_adjustcursor(RgnHandle
);
91 static void mac_adjustmenus(void);
92 static void mac_closewindow(WindowPtr
);
93 static void mac_zoomwindow(WindowPtr
, short);
94 #pragma noreturn (cleanup_exit)
101 struct mac_windows windows
;
103 int main (int argc
, char **argv
) {
109 #pragma noreturn (main)
111 static void mac_startup(void) {
115 #if !TARGET_API_MAC_CARBON
116 /* Init Memory Manager */
119 InitGraf(&qd
.thePort
);
120 /* Init Font Manager */
122 /* Init Window Manager */
124 /* Init Menu Manager */
128 /* Init Dialog Manager */
133 /* Get base system version (only used if there's no better selector) */
134 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
135 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
136 fatalbox("PuTTY requires System 7 or newer");
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;
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;
152 mac_gestalts
.cntlattr
= 0;
153 mac_gestalts
.windattr
= 0;
155 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
156 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
157 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
158 mac_gestalts
.cntlattr
= 0;
159 /* Mac OS 8.5 Window Manager? */
160 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
161 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
162 mac_gestalts
.windattr
= 0;
164 /* Text Encoding Conversion Manager? */
166 #if TARGET_RT_MAC_CFM
167 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
169 InitializeUnicodeConverter(NULL
) != noErr
||
171 TECGetInfo(&ti
) != noErr
)
172 mac_gestalts
.encvvers
= 0;
174 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
175 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
176 DisposeHandle((Handle
)ti
);
181 /* We've been tested with the Appearance Manager */
182 if (mac_gestalts
.apprvers
!= 0)
183 RegisterAppearanceClient();
185 menuBar
= GetNewMBar(128);
187 fatalbox("Unable to create menu bar.");
189 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
193 windows
.about
= NULL
;
194 windows
.licence
= NULL
;
196 default_protocol
= be_default_protocol
;
197 /* Find the appropriate default port. */
200 default_port
= 0; /* illegal */
201 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
202 if (backends
[i
].protocol
== default_protocol
) {
203 default_port
= backends
[i
].backend
->default_port
;
207 flags
= FLAG_INTERACTIVE
;
209 #if !TARGET_API_MAC_CARBON
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
);
223 /* Install Apple Event handlers. */
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
);
230 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
231 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
234 static void mac_eventloop(void) {
241 mac_adjustcursor(cursrgn
);
242 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
243 mac_adjustcursor(cursrgn
);
254 static void mac_event(EventRecord
*event
) {
258 switch (event
->what
) {
260 part
= FindWindow(event
->where
, &window
);
264 mac_menucommand(MenuSelect(event
->where
));
266 #if !TARGET_API_MAC_CARBON
268 SystemClick(event
, window
);
272 if (window
!= FrontWindow())
273 /* XXX: check for movable modal dboxes? */
274 SelectWindow(window
);
276 mac_contentclick(window
, event
);
279 if (TrackGoAway(window
, event
->where
))
280 mac_closewindow(window
);
283 /* XXX: moveable modal check? */
284 #if TARGET_API_MAC_CARBON
288 GetQDGlobalsScreenBits(&screenBits
);
289 DragWindow(window
, event
->where
, &screenBits
.bounds
);
292 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
296 mac_growwindow(window
, event
);
300 if (TrackBox(window
, event
->where
, part
))
301 mac_zoomwindow(window
, part
);
310 mac_activatewindow((WindowPtr
)event
->message
, event
);
313 mac_updatewindow((WindowPtr
)event
->message
);
315 #if !TARGET_API_MAC_CARBON
317 if (HiWord(event
->message
) != noErr
) {
320 SetPt(&pt
, 120, 120);
321 DIBadMount(pt
, event
->message
);
325 case kHighLevelEvent
:
326 AEProcessAppleEvent(event
); /* errors? */
331 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
335 switch (mac_windowtype(window
)) {
337 mac_clickterm(window
, event
);
340 dialog
= GetDialogFromWindow(window
);
341 if (DialogSelect(event
, &dialog
, &item
))
349 mac_clickdlg(window
, event
);
354 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
356 switch (mac_windowtype(window
)) {
358 mac_growterm(window
, event
);
362 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
365 active
= (event
->modifiers
& activeFlag
) != 0;
367 switch (mac_windowtype(window
)) {
369 mac_activateterm(window
, active
);
372 mac_activatedlg(window
, event
);
375 mac_activateabout(window
, event
);
380 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
382 DialogItemType itemtype
;
388 dialog
= GetDialogFromWindow(window
);
389 active
= (event
->modifiers
& activeFlag
) != 0;
390 GetDialogItem(dialog
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
391 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
392 DialogSelect(event
, &dialog
, &item
);
395 static void mac_updatewindow(WindowPtr window
)
397 #if TARGET_API_MAC_CARBON
401 switch (mac_windowtype(window
)) {
403 mac_updateterm(window
);
408 #if TARGET_API_MAC_CARBON
410 GetPortVisibleRegion(GetWindowPort(window
), rgn
);
411 UpdateDialog(GetDialogFromWindow(window
), rgn
);
414 UpdateDialog(window
, window
->visRgn
);
419 mac_updatelicence(window
);
424 static void mac_updatelicence(WindowPtr window
)
431 SetPort((GrafPtr
)GetWindowPort(window
));
433 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
434 TextFont(HiWord(fondsize
));
435 TextSize(LoWord(fondsize
));
436 h
= Get1Resource('TEXT', wLicence
);
437 len
= GetResourceSizeOnDisk(h
);
438 #if TARGET_API_MAC_CARBON
439 GetPortBounds(GetWindowPort(window
), &textrect
);
441 textrect
= window
->portRect
;
445 TETextBox(*h
, len
, &textrect
, teFlushDefault
);
452 * Work out what kind of window we're dealing with.
453 * Concept shamelessly nicked from SurfWriter.
455 static int mac_windowtype(WindowPtr window
) {
461 kind
= GetWindowKind(window
);
464 if (GetWVariant(window
) == zoomDocProc
)
466 refcon
= GetWRefCon(window
);
476 static void mac_keypress(EventRecord
*event
) {
479 window
= FrontWindow();
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.
484 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
485 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
486 mac_windowtype(window) == wTerminal)*/) {
488 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
490 switch (mac_windowtype(window
)) {
492 mac_keyterm(window
, event
);
498 static void mac_menucommand(long result
) {
501 #if !TARGET_API_MAC_CARBON
505 menu
= HiWord(result
);
506 item
= LoWord(result
);
507 window
= FrontWindow();
508 /* Things which do the same whatever window we're in. */
510 #if !TARGET_API_MAC_CARBON
517 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
532 mac_closewindow(window
);
549 /* If we get here, handling is up to window-specific code. */
550 switch (mac_windowtype(window
)) {
552 mac_menuterm(window
, menu
, item
);
559 static void mac_openabout(void) {
560 DialogItemType itemtype
;
567 SelectWindow(windows
.about
);
570 GetDialogWindow(GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1));
571 vers
= (VersRecHndl
)Get1Resource('vers', 1);
572 if (vers
!= NULL
&& *vers
!= NULL
) {
573 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
574 GetDialogItem(GetDialogFromWindow(windows
.about
), wiAboutVersion
,
575 &itemtype
, &item
, &box
);
576 assert(itemtype
& kStaticTextDialogItem
);
577 SetDialogItemText(item
, longvers
);
579 ShowWindow(windows
.about
);
583 static void mac_openlicence(void) {
586 SelectWindow(windows
.licence
);
588 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
589 ShowWindow(windows
.licence
);
593 static void mac_closewindow(WindowPtr window
) {
595 switch (mac_windowtype(window
)) {
596 #if !TARGET_API_MAC_CARBON
598 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
602 mac_closeterm(window
);
605 windows
.about
= NULL
;
606 DisposeDialog(GetDialogFromWindow(window
));
609 windows
.licence
= NULL
;
610 DisposeWindow(window
);
615 static void mac_zoomwindow(WindowPtr window
, short part
) {
617 /* FIXME: do something */
621 * Make the menus look right before the user gets to see them.
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
628 static void mac_adjustmenus(void) {
632 window
= FrontWindow();
633 menu
= GetMenuHandle(mApple
);
635 EnableItem(menu
, iAbout
);
637 menu
= GetMenuHandle(mFile
);
639 EnableItem(menu
, iNew
);
641 EnableItem(menu
, iClose
);
643 DisableItem(menu
, iClose
);
644 EnableItem(menu
, iQuit
);
646 switch (mac_windowtype(window
)) {
648 DisableItem(menu
, iSave
); /* XXX enable if modified */
649 EnableItem(menu
, iSaveAs
);
650 EnableItem(menu
, iDuplicate
);
651 menu
= GetMenuHandle(mEdit
);
652 DisableItem(menu
, 0);
655 mac_adjusttermmenus(window
);
658 DisableItem(menu
, iSave
);
659 DisableItem(menu
, iSaveAs
);
660 DisableItem(menu
, iDuplicate
);
661 menu
= GetMenuHandle(mEdit
);
662 DisableItem(menu
, 0);
669 * Make sure the right cursor's being displayed.
671 static void mac_adjustcursor(RgnHandle cursrgn
) {
673 WindowPtr window
, front
;
675 #if TARGET_API_MAC_CARBON
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 */
686 #if TARGET_API_MAC_CARBON
687 GetQDGlobalsArrow(&arrow
);
690 SetCursor(&qd
.arrow
);
692 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
694 #if TARGET_API_MAC_CARBON
696 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
697 DiffRgn(cursrgn
, visrgn
, cursrgn
);
700 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
704 switch (mac_windowtype(window
)) {
706 mac_adjusttermcursor(window
, mouse
, cursrgn
);
709 #if TARGET_API_MAC_CARBON
710 GetQDGlobalsArrow(&arrow
);
712 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
714 SetCursor(&qd
.arrow
);
715 CopyRgn(window
->visRgn
, cursrgn
);
722 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
728 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
729 &type
, NULL
, 0, &size
) == noErr
)
730 return errAEParamMissed
;
736 void cleanup_exit(int status
)
739 #if !TARGET_RT_MAC_CFM
740 if (mac_gestalts
.encvvers
!= 0)
741 TerminateUnicodeConverter();
747 void fatalbox(char *fmt
, ...) {
752 /* We'd like stuff to be a Pascal string */
753 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
755 ParamText(stuff
, NULL
, NULL
, NULL
);
756 StopAlert(128, NULL
);
760 void modalfatalbox(char *fmt
, ...) {
765 /* We'd like stuff to be a Pascal string */
766 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
768 ParamText(stuff
, NULL
, NULL
, NULL
);
769 StopAlert(128, NULL
);
773 /* This should only kill the current session, not the whole application. */
774 void connection_fatal(void *fontend
, 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 /* Null SSH agent client -- never finds an agent. */
789 int agent_exists(void)
795 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
802 /* Temporary null routines for testing. */
804 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
805 char *keystr
, char *fingerprint
)
810 void askcipher(void *frontend
, char *ciphername
, int cs
)
815 void old_keyfile_warning(void)
820 FontSpec
platform_default_fontspec(char const *name
)
825 if (!strcmp(name
, "Font")) {
826 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
828 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
830 GetFontName(HiWord(smfs
), ret
.name
);
831 if (ret
.name
[0] == 0)
832 memcpy(ret
.name
, "\pMonaco", 7);
833 ret
.size
= LoWord(smfs
);
835 memcpy(ret
.name
, "\pMonaco", 7);
846 Filename
platform_default_filename(const char *name
)
849 if (!strcmp(name
, "LogFileName"))
850 FSMakeFSSpec(0, 0, "\pputty.log", &ret
.fss
);
852 memset(&ret
, 0, sizeof(ret
));
856 char *platform_default_s(char const *name
)
861 int platform_default_i(char const *name
, int def
)
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"))
874 void platform_get_x11_auth(char *display
, int *proto
,
875 unsigned char *data
, int *datalen
)
877 /* SGT: I have no idea whether Mac X servers need anything here. */
880 Filename
filename_from_str(const char *str
)
885 /* XXX This fails for filenames over 255 characters long. */
887 FSMakeFSSpec(0, 0, tmp
, &ret
.fss
);
892 * Convert a filename to a string for display purposes.
893 * See pp 2-44--2-46 of IM:Files
895 * XXX static storage considered harmful
897 const char *filename_to_str(const Filename
*fn
)
902 static char *path
= NULL
;
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;
913 pb
.dirInfo
.ioDrDirID
= pb
.dirInfo
.ioDrParID
;
914 err
= PBGetCatInfoSync(&pb
);
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
);
923 } while (pb
.dirInfo
.ioDrDirID
!= fsRtDirID
);
927 int filename_equal(Filename f1
, Filename f2
)
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;
936 int filename_is_null(Filename fn
)
939 return fn
.fss
.vRefNum
== 0 && fn
.fss
.parID
== 0 && fn
.fss
.name
[0] == 0;
942 FILE *f_open(Filename fn
, char const *mode
)
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
);
955 HSetVol(NULL
, savevol
, savedir
);
961 * c-file-style: "simon"