1 /* $Id: mac.c,v 1.35 2003/01/23 22:57:43 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
);
175 #if 0 /* OpenTransport? */
176 if (Gestalt(gestaltOpenTpt
, &mac_gestalts
.otptattr
) != noErr
||
177 (mac_gestalts
.otptattr
& gestaltOpenTptTCPPresentMask
) == 0 ||
180 mac_gestalts
.otptattr
= 0;
181 if (mac_gestalts
.otptattr
== 0) {
183 if (Gestalt(FOUR_CHAR_CODE('mtcp'), &mac_gestalts
.mtcpvers
) != noErr
)
184 mac_gestalts
.mtcpvers
= 0;
185 if (mac_gestalts
.mtcpvers
> 0) {
186 if (mactcp_init() != noErr
)
187 mac_gestalts
.mtcpvers
= 0;
190 mac_gestalts
.mtcpvers
= 0;
192 /* We've been tested with the Appearance Manager */
193 if (mac_gestalts
.apprvers
!= 0)
194 RegisterAppearanceClient();
196 menuBar
= GetNewMBar(128);
198 fatalbox("Unable to create menu bar.");
200 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
204 windows
.about
= NULL
;
205 windows
.licence
= NULL
;
207 default_protocol
= be_default_protocol
;
208 /* Find the appropriate default port. */
211 default_port
= 0; /* illegal */
212 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
213 if (backends
[i
].protocol
== default_protocol
) {
214 default_port
= backends
[i
].backend
->default_port
;
218 flags
= FLAG_INTERACTIVE
;
224 /* Set the default directory for loading and saving settings. */
225 /* XXX Should we create it? */
226 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
227 LMSetSFSaveDisk(-vol
);
228 LMSetCurDirStore(dirid
);
232 /* Install Apple Event handlers. */
233 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
234 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
235 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
236 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
237 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
238 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
239 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
240 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
243 static void mac_eventloop(void) {
250 mac_adjustcursor(cursrgn
);
251 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
252 mac_adjustcursor(cursrgn
);
257 if (mac_gestalts
.mtcpvers
!= 0)
259 if (mac_gestalts
.otptattr
!= 0)
266 static void mac_event(EventRecord
*event
) {
271 switch (event
->what
) {
273 part
= FindWindow(event
->where
, &window
);
277 mac_menucommand(MenuSelect(event
->where
));
280 SystemClick(event
, window
);
283 if (window
!= FrontWindow())
284 /* XXX: check for movable modal dboxes? */
285 SelectWindow(window
);
287 mac_contentclick(window
, event
);
290 if (TrackGoAway(window
, event
->where
))
291 mac_closewindow(window
);
294 /* XXX: moveable modal check? */
295 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
298 mac_growwindow(window
, event
);
302 if (TrackBox(window
, event
->where
, part
))
303 mac_zoomwindow(window
, part
);
312 mac_activatewindow((WindowPtr
)event
->message
, event
);
315 mac_updatewindow((WindowPtr
)event
->message
);
318 if (HiWord(event
->message
) != noErr
) {
319 SetPt(&pt
, 120, 120);
320 DIBadMount(pt
, event
->message
);
323 case kHighLevelEvent
:
324 AEProcessAppleEvent(event
); /* errors? */
329 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
332 switch (mac_windowtype(window
)) {
334 mac_clickterm(window
, event
);
337 if (DialogSelect(event
, &window
, &item
))
345 mac_clickdlg(window
, event
);
350 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
352 switch (mac_windowtype(window
)) {
354 mac_growterm(window
, event
);
358 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
361 active
= (event
->modifiers
& activeFlag
) != 0;
363 switch (mac_windowtype(window
)) {
365 mac_activateterm(window
, active
);
368 mac_activatedlg(window
, event
);
371 mac_activateabout(window
, event
);
376 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
377 DialogItemType itemtype
;
383 active
= (event
->modifiers
& activeFlag
) != 0;
384 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
385 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
386 DialogSelect(event
, &window
, &item
);
389 static void mac_updatewindow(WindowPtr window
) {
391 switch (mac_windowtype(window
)) {
393 mac_updateterm(window
);
398 UpdateDialog(window
, window
->visRgn
);
402 mac_updatelicence(window
);
407 static void mac_updatelicence(WindowPtr window
)
415 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
416 TextFont(HiWord(fondsize
));
417 TextSize(LoWord(fondsize
));
418 h
= Get1Resource('TEXT', wLicence
);
419 len
= GetResourceSizeOnDisk(h
);
422 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
429 * Work out what kind of window we're dealing with.
430 * Concept shamelessly nicked from SurfWriter.
432 static int mac_windowtype(WindowPtr window
) {
438 kind
= ((WindowPeek
)window
)->windowKind
;
441 if (GetWVariant(window
) == zoomDocProc
)
443 refcon
= GetWRefCon(window
);
453 static void mac_keypress(EventRecord
*event
) {
456 window
= FrontWindow();
458 * Check for a command-key combination, but ignore it if it counts
459 * as a meta-key combination and we're in a terminal window.
461 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
462 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
463 mac_windowtype(window) == wTerminal)*/) {
465 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
467 switch (mac_windowtype(window
)) {
469 mac_keyterm(window
, event
);
475 static void mac_menucommand(long result
) {
480 menu
= HiWord(result
);
481 item
= LoWord(result
);
482 window
= FrontWindow();
483 /* Things which do the same whatever window we're in. */
491 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
505 mac_closewindow(window
);
519 /* If we get here, handling is up to window-specific code. */
520 switch (mac_windowtype(window
)) {
522 mac_menuterm(window
, menu
, item
);
529 static void mac_openabout(void) {
530 DialogItemType itemtype
;
537 SelectWindow(windows
.about
);
539 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
540 vers
= (VersRecHndl
)Get1Resource('vers', 1);
541 if (vers
!= NULL
&& *vers
!= NULL
) {
542 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
543 GetDialogItem(windows
.about
, wiAboutVersion
,
544 &itemtype
, &item
, &box
);
545 assert(itemtype
& kStaticTextDialogItem
);
546 SetDialogItemText(item
, longvers
);
548 ShowWindow(windows
.about
);
552 static void mac_openlicence(void) {
555 SelectWindow(windows
.licence
);
557 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
558 ShowWindow(windows
.licence
);
562 static void mac_closewindow(WindowPtr window
) {
564 switch (mac_windowtype(window
)) {
566 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
569 mac_closeterm(window
);
572 windows
.about
= NULL
;
573 DisposeDialog(window
);
576 windows
.licence
= NULL
;
577 DisposeWindow(window
);
582 static void mac_zoomwindow(WindowPtr window
, short part
) {
584 /* FIXME: do something */
588 * Make the menus look right before the user gets to see them.
590 static void mac_adjustmenus(void) {
594 window
= FrontWindow();
595 menu
= GetMenuHandle(mApple
);
597 EnableItem(menu
, iAbout
);
599 menu
= GetMenuHandle(mFile
);
601 EnableItem(menu
, iNew
);
603 EnableItem(menu
, iClose
);
605 DisableItem(menu
, iClose
);
606 EnableItem(menu
, iQuit
);
608 switch (mac_windowtype(window
)) {
610 DisableItem(menu
, iSave
); /* XXX enable if modified */
611 EnableItem(menu
, iSaveAs
);
612 menu
= GetMenuHandle(mEdit
);
613 DisableItem(menu
, 0);
616 mac_adjusttermmenus(window
);
619 DisableItem(menu
, iSave
);
620 DisableItem(menu
, iSaveAs
);
621 menu
= GetMenuHandle(mEdit
);
622 DisableItem(menu
, 0);
629 * Make sure the right cursor's being displayed.
631 static void mac_adjustcursor(RgnHandle cursrgn
) {
633 WindowPtr window
, front
;
637 LocalToGlobal(&mouse
);
638 part
= FindWindow(mouse
, &window
);
639 front
= FrontWindow();
640 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
641 /* Cursor isn't in the front window, so switch to arrow */
642 SetCursor(&qd
.arrow
);
643 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
645 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
647 switch (mac_windowtype(window
)) {
649 mac_adjusttermcursor(window
, mouse
, cursrgn
);
652 SetCursor(&qd
.arrow
);
653 CopyRgn(window
->visRgn
, cursrgn
);
659 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
665 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
666 &type
, NULL
, 0, &size
) == noErr
)
667 return errAEParamMissed
;
673 void cleanup_exit(int status
)
676 #if !TARGET_RT_MAC_CFM
677 if (mac_gestalts
.encvvers
!= 0)
678 TerminateUnicodeConverter();
684 void fatalbox(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 void modalfatalbox(char *fmt
, ...) {
702 /* We'd like stuff to be a Pascal string */
703 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
705 ParamText(stuff
, NULL
, NULL
, NULL
);
706 StopAlert(128, NULL
);
710 /* This should only kill the current session, not the whole application. */
711 void connection_fatal(void *fontend
, char *fmt
, ...) {
716 /* We'd like stuff to be a Pascal string */
717 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
719 ParamText(stuff
, NULL
, NULL
, NULL
);
720 StopAlert(128, NULL
);
724 /* Null SSH agent client -- never finds an agent. */
726 int agent_exists(void)
732 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
739 /* Temporary null routines for testing. */
741 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
742 char *keystr
, char *fingerprint
)
747 void askcipher(void *frontend
, char *ciphername
, int cs
)
752 void old_keyfile_warning(void)
757 char *platform_default_s(char const *name
)
761 static char cname
[256];
763 if (!strcmp(name
, "Font")) {
764 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
766 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
768 GetFontName(HiWord(smfs
), pname
);
771 p2cstrcpy(cname
, pname
);
779 int platform_default_i(char const *name
, int def
)
783 if (!strcmp(name
, "FontHeight")) {
784 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
786 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
793 /* Non-raw cut and paste of line-drawing chars works badly on the
794 * current Unix stub implementation of the Unicode functions.
795 * So I'm going to temporarily set the default to raw mode so
796 * that the failure mode isn't quite so drastically horrid.
797 * When Unicode comes in, this can all be put right. */
798 if (!strcmp(name
, "RawCNP"))
803 void platform_get_x11_auth(char *display
, int *proto
,
804 unsigned char *data
, int *datalen
)
806 /* SGT: I have no idea whether Mac X servers need anything here. */
811 * c-file-style: "simon"