1 /* $Id: mac.c,v 1.53 2003/03/01 15:12:03 ben Exp $ */
3 * Copyright (c) 1999, 2003 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
69 static int borednow
= FALSE
;
70 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_suspendresume(EventRecord
*);
81 static void mac_activateabout(WindowPtr
, EventRecord
*);
82 static void mac_updatewindow(WindowPtr
);
83 static void mac_updatelicence(WindowPtr
);
84 static void mac_keypress(EventRecord
*);
85 static int mac_windowtype(WindowPtr
);
86 static void mac_menucommand(long);
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 #if !TARGET_API_MAC_CARBON
114 /* Init Memory Manager */
117 InitGraf(&qd
.thePort
);
118 /* Init Font Manager */
120 /* Init Window Manager */
122 /* Init Menu Manager */
126 /* Init Dialog Manager */
131 /* Get base system version (only used if there's no better selector) */
132 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
133 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
134 fatalbox("PuTTY requires System 7 or newer");
135 /* Find out if we've got Color Quickdraw */
136 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
137 mac_gestalts
.qdvers
= gestaltOriginalQD
;
138 /* ... and the Appearance Manager? */
139 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
140 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
141 mac_gestalts
.apprvers
= 0x0100;
143 mac_gestalts
.apprvers
= 0;
144 #if TARGET_RT_MAC_CFM
145 /* Paranoia: Did we manage to pull in AppearanceLib? */
146 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
147 mac_gestalts
.apprvers
= 0;
150 mac_gestalts
.cntlattr
= 0;
151 mac_gestalts
.windattr
= 0;
153 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
154 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
155 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
156 mac_gestalts
.cntlattr
= 0;
157 /* Mac OS 8.5 Window Manager? */
158 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
159 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
160 mac_gestalts
.windattr
= 0;
161 /* Mac OS 8.5 Menu Manager? */
162 if (Gestalt(gestaltMenuMgrAttr
, &mac_gestalts
.menuattr
) != noErr
)
163 mac_gestalts
.menuattr
= 0;
165 /* Text Encoding Conversion Manager? */
167 #if TARGET_RT_MAC_CFM
168 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
170 InitializeUnicodeConverter(NULL
) != noErr
||
172 TECGetInfo(&ti
) != noErr
)
173 mac_gestalts
.encvvers
= 0;
175 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
176 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
177 DisposeHandle((Handle
)ti
);
179 /* Navigation Services? */
180 if (NavServicesAvailable())
181 mac_gestalts
.navsvers
= NavLibraryVersion();
183 mac_gestalts
.navsvers
= 0;
187 /* We've been tested with the Appearance Manager */
188 if (mac_gestalts
.apprvers
!= 0)
189 RegisterAppearanceClient();
191 menuBar
= GetNewMBar(128);
193 fatalbox("Unable to create menu bar.");
195 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
196 if (mac_gestalts
.menuattr
& gestaltMenuMgrAquaLayoutMask
)
197 DeleteMenuItem(GetMenuHandle(mFile
), iQuit
);
201 windows
.about
= NULL
;
202 windows
.licence
= NULL
;
204 default_protocol
= be_default_protocol
;
205 /* Find the appropriate default port. */
208 default_port
= 0; /* illegal */
209 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
210 if (backends
[i
].protocol
== default_protocol
) {
211 default_port
= backends
[i
].backend
->default_port
;
215 flags
= FLAG_INTERACTIVE
;
217 #if !TARGET_API_MAC_CARBON
222 /* Set the default directory for loading and saving settings. */
223 /* XXX Should we create it? */
224 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
225 LMSetSFSaveDisk(-vol
);
226 LMSetCurDirStore(dirid
);
231 /* Install Apple Event handlers. */
232 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
233 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
234 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
235 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
236 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
237 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
238 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
239 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
242 static void mac_eventloop(void) {
250 mac_adjustcursor(cursrgn
);
251 gotevent
= WaitNextEvent(everyEvent
, &event
, sleeptime
, cursrgn
);
253 * XXX For now, limit sleep time to 1/10 s to work around
254 * wake-before-sleep race in MacTCP code.
257 mac_adjustcursor(cursrgn
);
259 /* Ensure we get a null event when the real ones run out. */
272 static void mac_event(EventRecord
*event
) {
276 switch (event
->what
) {
278 part
= FindWindow(event
->where
, &window
);
282 mac_menucommand(MenuSelect(event
->where
));
284 #if !TARGET_API_MAC_CARBON
286 SystemClick(event
, window
);
290 if (window
!= FrontWindow())
291 /* XXX: check for movable modal dboxes? */
292 SelectWindow(window
);
294 mac_contentclick(window
, event
);
297 if (TrackGoAway(window
, event
->where
))
298 mac_closewindow(window
);
301 /* XXX: moveable modal check? */
302 #if TARGET_API_MAC_CARBON
306 GetQDGlobalsScreenBits(&screenBits
);
307 DragWindow(window
, event
->where
, &screenBits
.bounds
);
310 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
314 mac_growwindow(window
, event
);
318 if (TrackBox(window
, event
->where
, part
))
319 mac_zoomwindow(window
, part
);
328 mac_activatewindow((WindowPtr
)event
->message
, event
);
331 mac_updatewindow((WindowPtr
)event
->message
);
333 #if !TARGET_API_MAC_CARBON
335 if (HiWord(event
->message
) != noErr
) {
338 SetPt(&pt
, 120, 120);
339 DIBadMount(pt
, event
->message
);
344 switch ((event
->message
& osEvtMessageMask
) >> 24) {
345 case suspendResumeMessage
:
346 mac_suspendresume(event
);
350 case kHighLevelEvent
:
351 AEProcessAppleEvent(event
); /* errors? */
356 static void mac_contentclick(WindowPtr window
, EventRecord
*event
)
359 if (mac_wininfo(window
)->click
!= NULL
)
360 (*mac_wininfo(window
)->click
)(window
, event
);
363 static void mac_growwindow(WindowPtr window
, EventRecord
*event
)
366 if (mac_wininfo(window
)->grow
!= NULL
)
367 (*mac_wininfo(window
)->grow
)(window
, event
);
370 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
)
374 if (mac_wininfo(window
)->activate
!= NULL
)
375 (*mac_wininfo(window
)->activate
)(window
, event
);
378 static void mac_updatewindow(WindowPtr window
)
381 if (mac_wininfo(window
)->update
!= NULL
)
382 (*mac_wininfo(window
)->update
)(window
);
386 * Work out what kind of window we're dealing with.
388 static int mac_windowtype(WindowPtr window
)
391 #if !TARGET_API_MAC_CARBON
392 if (GetWindowKind(window
) < 0)
395 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
401 static void mac_keypress(EventRecord
*event
) {
404 window
= mac_frontwindow();
406 * Check for a command-key combination, but ignore it if it counts
407 * as a meta-key combination and we're in a terminal window.
409 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
410 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
411 mac_windowtype(window) == wTerminal)*/) {
413 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
415 if (window
!= NULL
&& mac_wininfo(window
)->key
!= NULL
)
416 (*mac_wininfo(window
)->key
)(window
, event
);
420 static void mac_menucommand(long result
) {
423 #if !TARGET_API_MAC_CARBON
427 menu
= HiWord(result
);
428 item
= LoWord(result
);
429 window
= mac_frontwindow();
430 /* Things which do the same whatever window we're in. */
437 #if !TARGET_API_MAC_CARBON
439 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
454 mac_closewindow(window
);
471 /* If we get here, handling is up to window-specific code. */
472 if (window
!= NULL
&& mac_wininfo(window
)->menu
!= NULL
)
473 (*mac_wininfo(window
)->menu
)(window
, menu
, item
);
479 static void mac_closewindow(WindowPtr window
) {
481 switch (mac_windowtype(window
)) {
482 #if !TARGET_API_MAC_CARBON
484 CloseDeskAcc(GetWindowKind(window
));
488 if (mac_wininfo(window
)->close
!= NULL
)
489 (*mac_wininfo(window
)->close
)(window
);
494 static void mac_suspendresume(EventRecord
*event
)
497 EventRecord fakeevent
;
500 * We're called either before we're suspended or after we're
501 * resumed, so we're the front application at this point.
503 front
= FrontWindow();
505 fakeevent
.what
= activateEvt
;
506 fakeevent
.message
= (UInt32
)front
;
507 fakeevent
.when
= event
->when
;
508 fakeevent
.where
= event
->where
;
509 fakeevent
.modifiers
=
510 (event
->message
& resumeFlag
) ? activeFlag
: 0;
511 mac_activatewindow(front
, &fakeevent
);
515 static void mac_zoomwindow(WindowPtr window
, short part
) {
517 /* FIXME: do something */
521 * Make the menus look right before the user gets to see them.
523 #if TARGET_API_MAC_CARBON
524 #define EnableItem EnableMenuItem
525 #define DisableItem DisableMenuItem
527 static void mac_adjustmenus(void) {
531 window
= mac_frontwindow();
532 menu
= GetMenuHandle(mApple
);
534 EnableItem(menu
, iAbout
);
536 menu
= GetMenuHandle(mFile
);
538 EnableItem(menu
, iNew
);
540 EnableItem(menu
, iClose
);
542 DisableItem(menu
, iClose
);
543 EnableItem(menu
, iQuit
);
545 if (window
!= NULL
&& mac_wininfo(window
)->adjustmenus
!= NULL
)
546 (*mac_wininfo(window
)->adjustmenus
)(window
);
548 DisableItem(menu
, iSave
);
549 DisableItem(menu
, iSaveAs
);
550 DisableItem(menu
, iDuplicate
);
551 menu
= GetMenuHandle(mEdit
);
552 DisableItem(menu
, 0);
553 menu
= GetMenuHandle(mWindow
);
554 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
560 * Make sure the right cursor's being displayed.
562 static void mac_adjustcursor(RgnHandle cursrgn
) {
564 WindowPtr window
, front
;
566 #if TARGET_API_MAC_CARBON
572 LocalToGlobal(&mouse
);
573 part
= FindWindow(mouse
, &window
);
574 front
= FrontWindow();
575 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
576 /* Cursor isn't in the front window, so switch to arrow */
577 #if TARGET_API_MAC_CARBON
578 GetQDGlobalsArrow(&arrow
);
581 SetCursor(&qd
.arrow
);
583 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
585 #if TARGET_API_MAC_CARBON
587 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
588 DiffRgn(cursrgn
, visrgn
, cursrgn
);
591 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
595 if (mac_wininfo(window
)->adjustcursor
!= NULL
)
596 (*mac_wininfo(window
)->adjustcursor
)(window
, mouse
, cursrgn
);
598 #if TARGET_API_MAC_CARBON
599 GetQDGlobalsArrow(&arrow
);
601 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
603 SetCursor(&qd
.arrow
);
604 CopyRgn(window
->visRgn
, cursrgn
);
610 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
616 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
617 &type
, NULL
, 0, &size
) == noErr
)
618 return errAEParamMissed
;
624 void cleanup_exit(int status
)
627 #if !TARGET_RT_MAC_CFM
628 if (mac_gestalts
.encvvers
!= 0)
629 TerminateUnicodeConverter();
635 /* This should only kill the current session, not the whole application. */
636 void connection_fatal(void *fontend
, char *fmt
, ...) {
641 /* We'd like stuff to be a Pascal string */
642 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
644 ParamText(stuff
, NULL
, NULL
, NULL
);
645 StopAlert(128, NULL
);
649 /* Null SSH agent client -- never finds an agent. */
651 int agent_exists(void)
657 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
664 /* Temporary null routines for testing. */
666 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
667 char *keystr
, char *fingerprint
)
672 void askcipher(void *frontend
, char *ciphername
, int cs
)
677 void old_keyfile_warning(void)
682 FontSpec
platform_default_fontspec(char const *name
)
687 if (!strcmp(name
, "Font")) {
688 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
690 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
692 GetFontName(HiWord(smfs
), ret
.name
);
693 if (ret
.name
[0] == 0)
694 memcpy(ret
.name
, "\pMonaco", 7);
695 ret
.size
= LoWord(smfs
);
697 memcpy(ret
.name
, "\pMonaco", 7);
708 Filename
platform_default_filename(const char *name
)
711 if (!strcmp(name
, "LogFileName"))
712 FSMakeFSSpec(0, 0, "\pputty.log", &ret
.fss
);
714 memset(&ret
, 0, sizeof(ret
));
718 char *platform_default_s(char const *name
)
723 int platform_default_i(char const *name
, int def
)
726 /* Non-raw cut and paste of line-drawing chars works badly on the
727 * current Unix stub implementation of the Unicode functions.
728 * So I'm going to temporarily set the default to raw mode so
729 * that the failure mode isn't quite so drastically horrid.
730 * When Unicode comes in, this can all be put right. */
731 if (!strcmp(name
, "RawCNP"))
736 void platform_get_x11_auth(char *display
, int *proto
,
737 unsigned char *data
, int *datalen
)
739 /* SGT: I have no idea whether Mac X servers need anything here. */
744 * c-file-style: "simon"