1 /* $Id: mac.c,v 1.39 2003/02/01 15:44:08 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
70 static int borednow
= FALSE
;
71 struct mac_gestalts mac_gestalts
;
73 static void mac_startup(void);
74 static void mac_eventloop(void);
75 #pragma noreturn (mac_eventloop)
76 static void mac_event(EventRecord
*);
77 static void mac_contentclick(WindowPtr
, EventRecord
*);
78 static void mac_growwindow(WindowPtr
, EventRecord
*);
79 static void mac_activatewindow(WindowPtr
, EventRecord
*);
80 static void mac_activateabout(WindowPtr
, EventRecord
*);
81 static void mac_updatewindow(WindowPtr
);
82 static void mac_updatelicence(WindowPtr
);
83 static void mac_keypress(EventRecord
*);
84 static int mac_windowtype(WindowPtr
);
85 static void mac_menucommand(long);
86 static void mac_openabout(void);
87 static void mac_openlicence(void);
88 static void mac_adjustcursor(RgnHandle
);
89 static void mac_adjustmenus(void);
90 static void mac_closewindow(WindowPtr
);
91 static void mac_zoomwindow(WindowPtr
, short);
92 #pragma noreturn (cleanup_exit)
99 struct mac_windows windows
;
101 int main (int argc
, char **argv
) {
107 #pragma noreturn (main)
109 static void mac_startup(void) {
113 /* Init Memory Manager */
116 InitGraf(&qd
.thePort
);
117 /* Init Font Manager */
119 /* Init Window Manager */
121 /* Init Menu Manager */
125 /* Init Dialog Manager */
129 /* Get base system version (only used if there's no better selector) */
130 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
131 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
132 fatalbox("PuTTY requires System 7 or newer");
133 /* Find out if we've got Color Quickdraw */
134 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
135 mac_gestalts
.qdvers
= gestaltOriginalQD
;
136 /* ... and the Appearance Manager? */
137 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
138 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
139 mac_gestalts
.apprvers
= 0x0100;
141 mac_gestalts
.apprvers
= 0;
142 #if TARGET_RT_MAC_CFM
143 /* Paranoia: Did we manage to pull in AppearanceLib? */
144 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
145 mac_gestalts
.apprvers
= 0;
148 mac_gestalts
.cntlattr
= 0;
149 mac_gestalts
.windattr
= 0;
151 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
152 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
153 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
154 mac_gestalts
.cntlattr
= 0;
155 /* Mac OS 8.5 Window Manager? */
156 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
157 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
158 mac_gestalts
.windattr
= 0;
160 /* Text Encoding Conversion Manager? */
162 #if TARGET_RT_MAC_CFM
163 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
165 InitializeUnicodeConverter(NULL
) != noErr
||
167 TECGetInfo(&ti
) != noErr
)
168 mac_gestalts
.encvvers
= 0;
170 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
171 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
172 DisposeHandle((Handle
)ti
);
177 /* We've been tested with the Appearance Manager */
178 if (mac_gestalts
.apprvers
!= 0)
179 RegisterAppearanceClient();
181 menuBar
= GetNewMBar(128);
183 fatalbox("Unable to create menu bar.");
185 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
189 windows
.about
= NULL
;
190 windows
.licence
= NULL
;
192 default_protocol
= be_default_protocol
;
193 /* Find the appropriate default port. */
196 default_port
= 0; /* illegal */
197 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
198 if (backends
[i
].protocol
== default_protocol
) {
199 default_port
= backends
[i
].backend
->default_port
;
203 flags
= FLAG_INTERACTIVE
;
209 /* Set the default directory for loading and saving settings. */
210 /* XXX Should we create it? */
211 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
212 LMSetSFSaveDisk(-vol
);
213 LMSetCurDirStore(dirid
);
217 /* Install Apple Event handlers. */
218 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
219 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
220 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
221 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
222 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
223 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
224 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
225 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
228 static void mac_eventloop(void) {
235 mac_adjustcursor(cursrgn
);
236 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
237 mac_adjustcursor(cursrgn
);
248 static void mac_event(EventRecord
*event
) {
253 switch (event
->what
) {
255 part
= FindWindow(event
->where
, &window
);
259 mac_menucommand(MenuSelect(event
->where
));
262 SystemClick(event
, window
);
265 if (window
!= FrontWindow())
266 /* XXX: check for movable modal dboxes? */
267 SelectWindow(window
);
269 mac_contentclick(window
, event
);
272 if (TrackGoAway(window
, event
->where
))
273 mac_closewindow(window
);
276 /* XXX: moveable modal check? */
277 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
280 mac_growwindow(window
, event
);
284 if (TrackBox(window
, event
->where
, part
))
285 mac_zoomwindow(window
, part
);
294 mac_activatewindow((WindowPtr
)event
->message
, event
);
297 mac_updatewindow((WindowPtr
)event
->message
);
300 if (HiWord(event
->message
) != noErr
) {
301 SetPt(&pt
, 120, 120);
302 DIBadMount(pt
, event
->message
);
305 case kHighLevelEvent
:
306 AEProcessAppleEvent(event
); /* errors? */
311 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
314 switch (mac_windowtype(window
)) {
316 mac_clickterm(window
, event
);
319 if (DialogSelect(event
, &window
, &item
))
327 mac_clickdlg(window
, event
);
332 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
334 switch (mac_windowtype(window
)) {
336 mac_growterm(window
, event
);
340 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
343 active
= (event
->modifiers
& activeFlag
) != 0;
345 switch (mac_windowtype(window
)) {
347 mac_activateterm(window
, active
);
350 mac_activatedlg(window
, event
);
353 mac_activateabout(window
, event
);
358 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
359 DialogItemType itemtype
;
365 active
= (event
->modifiers
& activeFlag
) != 0;
366 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
367 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
368 DialogSelect(event
, &window
, &item
);
371 static void mac_updatewindow(WindowPtr window
) {
373 switch (mac_windowtype(window
)) {
375 mac_updateterm(window
);
380 UpdateDialog(window
, window
->visRgn
);
384 mac_updatelicence(window
);
389 static void mac_updatelicence(WindowPtr window
)
397 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
398 TextFont(HiWord(fondsize
));
399 TextSize(LoWord(fondsize
));
400 h
= Get1Resource('TEXT', wLicence
);
401 len
= GetResourceSizeOnDisk(h
);
404 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
411 * Work out what kind of window we're dealing with.
412 * Concept shamelessly nicked from SurfWriter.
414 static int mac_windowtype(WindowPtr window
) {
420 kind
= ((WindowPeek
)window
)->windowKind
;
423 if (GetWVariant(window
) == zoomDocProc
)
425 refcon
= GetWRefCon(window
);
435 static void mac_keypress(EventRecord
*event
) {
438 window
= FrontWindow();
440 * Check for a command-key combination, but ignore it if it counts
441 * as a meta-key combination and we're in a terminal window.
443 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
444 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
445 mac_windowtype(window) == wTerminal)*/) {
447 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
449 switch (mac_windowtype(window
)) {
451 mac_keyterm(window
, event
);
457 static void mac_menucommand(long result
) {
462 menu
= HiWord(result
);
463 item
= LoWord(result
);
464 window
= FrontWindow();
465 /* Things which do the same whatever window we're in. */
473 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
487 mac_closewindow(window
);
504 /* If we get here, handling is up to window-specific code. */
505 switch (mac_windowtype(window
)) {
507 mac_menuterm(window
, menu
, item
);
514 static void mac_openabout(void) {
515 DialogItemType itemtype
;
522 SelectWindow(windows
.about
);
524 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
525 vers
= (VersRecHndl
)Get1Resource('vers', 1);
526 if (vers
!= NULL
&& *vers
!= NULL
) {
527 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
528 GetDialogItem(windows
.about
, wiAboutVersion
,
529 &itemtype
, &item
, &box
);
530 assert(itemtype
& kStaticTextDialogItem
);
531 SetDialogItemText(item
, longvers
);
533 ShowWindow(windows
.about
);
537 static void mac_openlicence(void) {
540 SelectWindow(windows
.licence
);
542 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
543 ShowWindow(windows
.licence
);
547 static void mac_closewindow(WindowPtr window
) {
549 switch (mac_windowtype(window
)) {
551 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
554 mac_closeterm(window
);
557 windows
.about
= NULL
;
558 DisposeDialog(window
);
561 windows
.licence
= NULL
;
562 DisposeWindow(window
);
567 static void mac_zoomwindow(WindowPtr window
, short part
) {
569 /* FIXME: do something */
573 * Make the menus look right before the user gets to see them.
575 static void mac_adjustmenus(void) {
579 window
= FrontWindow();
580 menu
= GetMenuHandle(mApple
);
582 EnableItem(menu
, iAbout
);
584 menu
= GetMenuHandle(mFile
);
586 EnableItem(menu
, iNew
);
588 EnableItem(menu
, iClose
);
590 DisableItem(menu
, iClose
);
591 EnableItem(menu
, iQuit
);
593 switch (mac_windowtype(window
)) {
595 DisableItem(menu
, iSave
); /* XXX enable if modified */
596 EnableItem(menu
, iSaveAs
);
597 EnableItem(menu
, iDuplicate
);
598 menu
= GetMenuHandle(mEdit
);
599 DisableItem(menu
, 0);
602 mac_adjusttermmenus(window
);
605 DisableItem(menu
, iSave
);
606 DisableItem(menu
, iSaveAs
);
607 DisableItem(menu
, iDuplicate
);
608 menu
= GetMenuHandle(mEdit
);
609 DisableItem(menu
, 0);
616 * Make sure the right cursor's being displayed.
618 static void mac_adjustcursor(RgnHandle cursrgn
) {
620 WindowPtr window
, front
;
624 LocalToGlobal(&mouse
);
625 part
= FindWindow(mouse
, &window
);
626 front
= FrontWindow();
627 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
628 /* Cursor isn't in the front window, so switch to arrow */
629 SetCursor(&qd
.arrow
);
630 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
632 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
634 switch (mac_windowtype(window
)) {
636 mac_adjusttermcursor(window
, mouse
, cursrgn
);
639 SetCursor(&qd
.arrow
);
640 CopyRgn(window
->visRgn
, cursrgn
);
646 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
652 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
653 &type
, NULL
, 0, &size
) == noErr
)
654 return errAEParamMissed
;
660 void cleanup_exit(int status
)
663 #if !TARGET_RT_MAC_CFM
664 if (mac_gestalts
.encvvers
!= 0)
665 TerminateUnicodeConverter();
671 void fatalbox(char *fmt
, ...) {
676 /* We'd like stuff to be a Pascal string */
677 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
679 ParamText(stuff
, NULL
, NULL
, NULL
);
680 StopAlert(128, NULL
);
684 void modalfatalbox(char *fmt
, ...) {
689 /* We'd like stuff to be a Pascal string */
690 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
692 ParamText(stuff
, NULL
, NULL
, NULL
);
693 StopAlert(128, NULL
);
697 /* This should only kill the current session, not the whole application. */
698 void connection_fatal(void *fontend
, char *fmt
, ...) {
703 /* We'd like stuff to be a Pascal string */
704 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
706 ParamText(stuff
, NULL
, NULL
, NULL
);
707 StopAlert(128, NULL
);
711 /* Null SSH agent client -- never finds an agent. */
713 int agent_exists(void)
719 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
726 /* Temporary null routines for testing. */
728 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
729 char *keystr
, char *fingerprint
)
734 void askcipher(void *frontend
, char *ciphername
, int cs
)
739 void old_keyfile_warning(void)
744 FontSpec
platform_default_fontspec(char const *name
)
749 if (!strcmp(name
, "Font")) {
750 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
752 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
754 GetFontName(HiWord(smfs
), ret
.name
);
755 if (ret
.name
[0] == 0)
756 memcpy(ret
.name
, "\pMonaco", 7);
757 ret
.size
= LoWord(smfs
);
759 memcpy(ret
.name
, "\pMonaco", 7);
770 Filename
platform_default_filename(const char *name
)
773 if (!strcmp(name
, "LogFileName"))
774 strcpy(ret
.path
, "putty.log");
780 char *platform_default_s(char const *name
)
785 int platform_default_i(char const *name
, int def
)
788 /* Non-raw cut and paste of line-drawing chars works badly on the
789 * current Unix stub implementation of the Unicode functions.
790 * So I'm going to temporarily set the default to raw mode so
791 * that the failure mode isn't quite so drastically horrid.
792 * When Unicode comes in, this can all be put right. */
793 if (!strcmp(name
, "RawCNP"))
798 void platform_get_x11_auth(char *display
, int *proto
,
799 unsigned char *data
, int *datalen
)
801 /* SGT: I have no idea whether Mac X servers need anything here. */
804 Filename
filename_from_str(char *str
)
807 strncpy(ret
.path
, str
, sizeof(ret
.path
));
808 ret
.path
[sizeof(ret
.path
)-1] = '\0';
812 char *filename_to_str(Filename fn
)
814 /* FIXME: Memory leak! */
816 return dupstr(fn
.path
);
819 int filename_equal(Filename f1
, Filename f2
)
821 return !strcmp(f1
.path
, f2
.path
);
824 int filename_is_null(Filename fn
)
831 * c-file-style: "simon"