1 /* $Id: mac.c,v 1.34 2003/01/20 22:55: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)
93 static pascal OSErr
mac_aevt_quit(const AppleEvent
*, AppleEvent
*, long);
100 struct mac_windows windows
;
102 int main (int argc
, char **argv
) {
108 #pragma noreturn (main)
110 static void mac_startup(void) {
114 /* Init Memory Manager */
117 InitGraf(&qd
.thePort
);
118 /* Init Font Manager */
120 /* Init Window Manager */
122 /* Init Menu Manager */
126 /* Init Dialog Manager */
130 /* Get base system version (only used if there's no better selector) */
131 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
132 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
133 fatalbox("PuTTY requires System 7 or newer");
134 /* Find out if we've got Color Quickdraw */
135 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
136 mac_gestalts
.qdvers
= gestaltOriginalQD
;
137 /* ... and the Appearance Manager? */
138 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
139 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
140 mac_gestalts
.apprvers
= 0x0100;
142 mac_gestalts
.apprvers
= 0;
143 #if TARGET_RT_MAC_CFM
144 /* Paranoia: Did we manage to pull in AppearanceLib? */
145 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
146 mac_gestalts
.apprvers
= 0;
149 mac_gestalts
.cntlattr
= 0;
150 mac_gestalts
.windattr
= 0;
152 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
153 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
154 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
155 mac_gestalts
.cntlattr
= 0;
156 /* Mac OS 8.5 Window Manager? */
157 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
158 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
159 mac_gestalts
.windattr
= 0;
161 /* Text Encoding Conversion Manager? */
163 #if TARGET_RT_MAC_CFM
164 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
166 InitializeUnicodeConverter(NULL
) != noErr
||
168 TECGetInfo(&ti
) != noErr
)
169 mac_gestalts
.encvvers
= 0;
171 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
172 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
173 DisposeHandle((Handle
)ti
);
176 #if 0 /* OpenTransport? */
177 if (Gestalt(gestaltOpenTpt
, &mac_gestalts
.otptattr
) != noErr
||
178 (mac_gestalts
.otptattr
& gestaltOpenTptTCPPresentMask
) == 0 ||
181 mac_gestalts
.otptattr
= 0;
182 if (mac_gestalts
.otptattr
== 0) {
184 if (Gestalt(FOUR_CHAR_CODE('mtcp'), &mac_gestalts
.mtcpvers
) != noErr
)
185 mac_gestalts
.mtcpvers
= 0;
186 if (mac_gestalts
.mtcpvers
> 0) {
187 if (mactcp_init() != noErr
)
188 mac_gestalts
.mtcpvers
= 0;
191 mac_gestalts
.mtcpvers
= 0;
193 /* We've been tested with the Appearance Manager */
194 if (mac_gestalts
.apprvers
!= 0)
195 RegisterAppearanceClient();
197 menuBar
= GetNewMBar(128);
199 fatalbox("Unable to create menu bar.");
201 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
205 windows
.about
= NULL
;
206 windows
.licence
= NULL
;
208 default_protocol
= be_default_protocol
;
209 /* Find the appropriate default port. */
212 default_port
= 0; /* illegal */
213 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
214 if (backends
[i
].protocol
== default_protocol
) {
215 default_port
= backends
[i
].backend
->default_port
;
219 flags
= FLAG_INTERACTIVE
;
225 /* Set the default directory for loading and saving settings. */
226 /* XXX Should we create it? */
227 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
228 LMSetSFSaveDisk(-vol
);
229 LMSetCurDirStore(dirid
);
233 /* Install Apple Event handlers. */
234 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
235 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
238 static void mac_eventloop(void) {
245 mac_adjustcursor(cursrgn
);
246 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
247 mac_adjustcursor(cursrgn
);
252 if (mac_gestalts
.mtcpvers
!= 0)
254 if (mac_gestalts
.otptattr
!= 0)
261 static void mac_event(EventRecord
*event
) {
266 switch (event
->what
) {
268 part
= FindWindow(event
->where
, &window
);
272 mac_menucommand(MenuSelect(event
->where
));
275 SystemClick(event
, window
);
278 if (window
!= FrontWindow())
279 /* XXX: check for movable modal dboxes? */
280 SelectWindow(window
);
282 mac_contentclick(window
, event
);
285 if (TrackGoAway(window
, event
->where
))
286 mac_closewindow(window
);
289 /* XXX: moveable modal check? */
290 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
293 mac_growwindow(window
, event
);
297 if (TrackBox(window
, event
->where
, part
))
298 mac_zoomwindow(window
, part
);
307 mac_activatewindow((WindowPtr
)event
->message
, event
);
310 mac_updatewindow((WindowPtr
)event
->message
);
313 if (HiWord(event
->message
) != noErr
) {
314 SetPt(&pt
, 120, 120);
315 DIBadMount(pt
, event
->message
);
318 case kHighLevelEvent
:
319 AEProcessAppleEvent(event
); /* errors? */
324 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
327 switch (mac_windowtype(window
)) {
329 mac_clickterm(window
, event
);
332 if (DialogSelect(event
, &window
, &item
))
340 mac_clickdlg(window
, event
);
345 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
347 switch (mac_windowtype(window
)) {
349 mac_growterm(window
, event
);
353 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
356 active
= (event
->modifiers
& activeFlag
) != 0;
358 switch (mac_windowtype(window
)) {
360 mac_activateterm(window
, active
);
363 mac_activatedlg(window
, event
);
366 mac_activateabout(window
, event
);
371 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
372 DialogItemType itemtype
;
378 active
= (event
->modifiers
& activeFlag
) != 0;
379 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
380 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
381 DialogSelect(event
, &window
, &item
);
384 static void mac_updatewindow(WindowPtr window
) {
386 switch (mac_windowtype(window
)) {
388 mac_updateterm(window
);
393 UpdateDialog(window
, window
->visRgn
);
397 mac_updatelicence(window
);
402 static void mac_updatelicence(WindowPtr window
)
410 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
411 TextFont(HiWord(fondsize
));
412 TextSize(LoWord(fondsize
));
413 h
= Get1Resource('TEXT', wLicence
);
414 len
= GetResourceSizeOnDisk(h
);
417 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
424 * Work out what kind of window we're dealing with.
425 * Concept shamelessly nicked from SurfWriter.
427 static int mac_windowtype(WindowPtr window
) {
433 kind
= ((WindowPeek
)window
)->windowKind
;
436 if (GetWVariant(window
) == zoomDocProc
)
438 refcon
= GetWRefCon(window
);
448 static void mac_keypress(EventRecord
*event
) {
451 window
= FrontWindow();
453 * Check for a command-key combination, but ignore it if it counts
454 * as a meta-key combination and we're in a terminal window.
456 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
457 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
458 mac_windowtype(window) == wTerminal)*/) {
460 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
462 switch (mac_windowtype(window
)) {
464 mac_keyterm(window
, event
);
470 static void mac_menucommand(long result
) {
475 menu
= HiWord(result
);
476 item
= LoWord(result
);
477 window
= FrontWindow();
478 /* Things which do the same whatever window we're in. */
486 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
500 mac_closewindow(window
);
514 /* If we get here, handling is up to window-specific code. */
515 switch (mac_windowtype(window
)) {
517 mac_menuterm(window
, menu
, item
);
524 static void mac_openabout(void) {
525 DialogItemType itemtype
;
532 SelectWindow(windows
.about
);
534 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
535 vers
= (VersRecHndl
)Get1Resource('vers', 1);
536 if (vers
!= NULL
&& *vers
!= NULL
) {
537 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
538 GetDialogItem(windows
.about
, wiAboutVersion
,
539 &itemtype
, &item
, &box
);
540 assert(itemtype
& kStaticTextDialogItem
);
541 SetDialogItemText(item
, longvers
);
543 ShowWindow(windows
.about
);
547 static void mac_openlicence(void) {
550 SelectWindow(windows
.licence
);
552 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
553 ShowWindow(windows
.licence
);
557 static void mac_closewindow(WindowPtr window
) {
559 switch (mac_windowtype(window
)) {
561 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
564 mac_closeterm(window
);
567 windows
.about
= NULL
;
568 DisposeDialog(window
);
571 windows
.licence
= NULL
;
572 DisposeWindow(window
);
577 static void mac_zoomwindow(WindowPtr window
, short part
) {
579 /* FIXME: do something */
583 * Make the menus look right before the user gets to see them.
585 static void mac_adjustmenus(void) {
589 window
= FrontWindow();
590 menu
= GetMenuHandle(mApple
);
592 EnableItem(menu
, iAbout
);
594 menu
= GetMenuHandle(mFile
);
596 EnableItem(menu
, iNew
);
598 EnableItem(menu
, iClose
);
600 DisableItem(menu
, iClose
);
601 EnableItem(menu
, iQuit
);
603 switch (mac_windowtype(window
)) {
605 DisableItem(menu
, iSave
); /* XXX enable if modified */
606 EnableItem(menu
, iSaveAs
);
607 menu
= GetMenuHandle(mEdit
);
608 DisableItem(menu
, 0);
611 mac_adjusttermmenus(window
);
614 DisableItem(menu
, iSave
);
615 DisableItem(menu
, iSaveAs
);
616 menu
= GetMenuHandle(mEdit
);
617 DisableItem(menu
, 0);
624 * Make sure the right cursor's being displayed.
626 static void mac_adjustcursor(RgnHandle cursrgn
) {
628 WindowPtr window
, front
;
632 LocalToGlobal(&mouse
);
633 part
= FindWindow(mouse
, &window
);
634 front
= FrontWindow();
635 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
636 /* Cursor isn't in the front window, so switch to arrow */
637 SetCursor(&qd
.arrow
);
638 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
640 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
642 switch (mac_windowtype(window
)) {
644 mac_adjusttermcursor(window
, mouse
, cursrgn
);
647 SetCursor(&qd
.arrow
);
648 CopyRgn(window
->visRgn
, cursrgn
);
654 static pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
662 void cleanup_exit(int status
)
665 #if !TARGET_RT_MAC_CFM
666 if (mac_gestalts
.encvvers
!= 0)
667 TerminateUnicodeConverter();
673 void fatalbox(char *fmt
, ...) {
678 /* We'd like stuff to be a Pascal string */
679 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
681 ParamText(stuff
, NULL
, NULL
, NULL
);
682 StopAlert(128, NULL
);
686 void modalfatalbox(char *fmt
, ...) {
691 /* We'd like stuff to be a Pascal string */
692 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
694 ParamText(stuff
, NULL
, NULL
, NULL
);
695 StopAlert(128, NULL
);
699 /* This should only kill the current session, not the whole application. */
700 void connection_fatal(void *fontend
, char *fmt
, ...) {
705 /* We'd like stuff to be a Pascal string */
706 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
708 ParamText(stuff
, NULL
, NULL
, NULL
);
709 StopAlert(128, NULL
);
713 /* Null SSH agent client -- never finds an agent. */
715 int agent_exists(void)
721 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
728 /* Temporary null routines for testing. */
730 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
731 char *keystr
, char *fingerprint
)
736 void askcipher(void *frontend
, char *ciphername
, int cs
)
741 void old_keyfile_warning(void)
746 char *platform_default_s(char const *name
)
750 static char cname
[256];
752 if (!strcmp(name
, "Font")) {
753 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
755 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
757 GetFontName(HiWord(smfs
), pname
);
760 p2cstrcpy(cname
, pname
);
768 int platform_default_i(char const *name
, int def
)
772 if (!strcmp(name
, "FontHeight")) {
773 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
775 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
782 /* Non-raw cut and paste of line-drawing chars works badly on the
783 * current Unix stub implementation of the Unicode functions.
784 * So I'm going to temporarily set the default to raw mode so
785 * that the failure mode isn't quite so drastically horrid.
786 * When Unicode comes in, this can all be put right. */
787 if (!strcmp(name
, "RawCNP"))
792 void platform_get_x11_auth(char *display
, int *proto
,
793 unsigned char *data
, int *datalen
)
795 /* SGT: I have no idea whether Mac X servers need anything here. */
800 * c-file-style: "simon"