1 /* $Id: mac.c,v 1.51 2003/02/23 11:58:59 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
;
72 static void mac_startup(void);
73 static void mac_eventloop(void);
74 #pragma noreturn (mac_eventloop)
75 static void mac_event(EventRecord
*);
76 static void mac_contentclick(WindowPtr
, EventRecord
*);
77 static void mac_growwindow(WindowPtr
, EventRecord
*);
78 static void mac_activatewindow(WindowPtr
, EventRecord
*);
79 static void mac_suspendresume(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_openlicence(void);
87 static void mac_adjustcursor(RgnHandle
);
88 static void mac_adjustmenus(void);
89 static void mac_closewindow(WindowPtr
);
90 static void mac_zoomwindow(WindowPtr
, short);
91 #pragma noreturn (cleanup_exit)
98 struct mac_windows windows
;
100 int main (int argc
, char **argv
) {
106 #pragma noreturn (main)
108 static void mac_startup(void) {
112 #if !TARGET_API_MAC_CARBON
113 /* Init Memory Manager */
116 InitGraf(&qd
.thePort
);
117 /* Init Font Manager */
119 /* Init Window Manager */
121 /* Init Menu Manager */
125 /* 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
);
175 /* Navigation Services? */
176 if (NavServicesAvailable())
177 mac_gestalts
.navsvers
= NavLibraryVersion();
179 mac_gestalts
.navsvers
= 0;
183 /* We've been tested with the Appearance Manager */
184 if (mac_gestalts
.apprvers
!= 0)
185 RegisterAppearanceClient();
187 menuBar
= GetNewMBar(128);
189 fatalbox("Unable to create menu bar.");
191 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
195 windows
.about
= NULL
;
196 windows
.licence
= NULL
;
198 default_protocol
= be_default_protocol
;
199 /* Find the appropriate default port. */
202 default_port
= 0; /* illegal */
203 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
204 if (backends
[i
].protocol
== default_protocol
) {
205 default_port
= backends
[i
].backend
->default_port
;
209 flags
= FLAG_INTERACTIVE
;
211 #if !TARGET_API_MAC_CARBON
216 /* Set the default directory for loading and saving settings. */
217 /* XXX Should we create it? */
218 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
219 LMSetSFSaveDisk(-vol
);
220 LMSetCurDirStore(dirid
);
225 /* Install Apple Event handlers. */
226 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
227 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
228 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
229 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
230 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
231 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
232 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
233 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
236 static void mac_eventloop(void) {
243 mac_adjustcursor(cursrgn
);
244 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
245 mac_adjustcursor(cursrgn
);
256 static void mac_event(EventRecord
*event
) {
260 switch (event
->what
) {
262 part
= FindWindow(event
->where
, &window
);
266 mac_menucommand(MenuSelect(event
->where
));
268 #if !TARGET_API_MAC_CARBON
270 SystemClick(event
, window
);
274 if (window
!= FrontWindow())
275 /* XXX: check for movable modal dboxes? */
276 SelectWindow(window
);
278 mac_contentclick(window
, event
);
281 if (TrackGoAway(window
, event
->where
))
282 mac_closewindow(window
);
285 /* XXX: moveable modal check? */
286 #if TARGET_API_MAC_CARBON
290 GetQDGlobalsScreenBits(&screenBits
);
291 DragWindow(window
, event
->where
, &screenBits
.bounds
);
294 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
);
317 #if !TARGET_API_MAC_CARBON
319 if (HiWord(event
->message
) != noErr
) {
322 SetPt(&pt
, 120, 120);
323 DIBadMount(pt
, event
->message
);
328 switch ((event
->message
& osEvtMessageMask
) >> 24) {
329 case suspendResumeMessage
:
330 mac_suspendresume(event
);
334 case kHighLevelEvent
:
335 AEProcessAppleEvent(event
); /* errors? */
340 static void mac_contentclick(WindowPtr window
, EventRecord
*event
)
343 if (mac_wininfo(window
)->click
!= NULL
)
344 (*mac_wininfo(window
)->click
)(window
, event
);
347 static void mac_growwindow(WindowPtr window
, EventRecord
*event
)
350 if (mac_wininfo(window
)->grow
!= NULL
)
351 (*mac_wininfo(window
)->grow
)(window
, event
);
354 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
)
358 if (mac_wininfo(window
)->activate
!= NULL
)
359 (*mac_wininfo(window
)->activate
)(window
, event
);
362 static void mac_updatewindow(WindowPtr window
)
365 if (mac_wininfo(window
)->update
!= NULL
)
366 (*mac_wininfo(window
)->update
)(window
);
370 * Work out what kind of window we're dealing with.
372 static int mac_windowtype(WindowPtr window
)
375 #if !TARGET_API_MAC_CARBON
376 if (GetWindowKind(window
) < 0)
379 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
385 static void mac_keypress(EventRecord
*event
) {
388 window
= mac_frontwindow();
390 * Check for a command-key combination, but ignore it if it counts
391 * as a meta-key combination and we're in a terminal window.
393 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
394 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
395 mac_windowtype(window) == wTerminal)*/) {
397 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
399 if (window
!= NULL
&& mac_wininfo(window
)->key
!= NULL
)
400 (*mac_wininfo(window
)->key
)(window
, event
);
404 static void mac_menucommand(long result
) {
407 #if !TARGET_API_MAC_CARBON
411 menu
= HiWord(result
);
412 item
= LoWord(result
);
413 window
= mac_frontwindow();
414 /* Things which do the same whatever window we're in. */
421 #if !TARGET_API_MAC_CARBON
423 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
438 mac_closewindow(window
);
455 /* If we get here, handling is up to window-specific code. */
456 if (window
!= NULL
&& mac_wininfo(window
)->menu
!= NULL
)
457 (*mac_wininfo(window
)->menu
)(window
, menu
, item
);
463 static void mac_closewindow(WindowPtr window
) {
465 switch (mac_windowtype(window
)) {
466 #if !TARGET_API_MAC_CARBON
468 CloseDeskAcc(GetWindowKind(window
));
472 if (mac_wininfo(window
)->close
!= NULL
)
473 (*mac_wininfo(window
)->close
)(window
);
478 static void mac_suspendresume(EventRecord
*event
)
481 EventRecord fakeevent
;
484 * We're called either before we're suspended or after we're
485 * resumed, so we're the front application at this point.
487 front
= FrontWindow();
489 fakeevent
.what
= activateEvt
;
490 fakeevent
.message
= (UInt32
)front
;
491 fakeevent
.when
= event
->when
;
492 fakeevent
.where
= event
->where
;
493 fakeevent
.modifiers
=
494 (event
->message
& resumeFlag
) ? activeFlag
: 0;
495 mac_activatewindow(front
, &fakeevent
);
499 static void mac_zoomwindow(WindowPtr window
, short part
) {
501 /* FIXME: do something */
505 * Make the menus look right before the user gets to see them.
507 #if TARGET_API_MAC_CARBON
508 #define EnableItem EnableMenuItem
509 #define DisableItem DisableMenuItem
511 static void mac_adjustmenus(void) {
515 window
= mac_frontwindow();
516 menu
= GetMenuHandle(mApple
);
518 EnableItem(menu
, iAbout
);
520 menu
= GetMenuHandle(mFile
);
522 EnableItem(menu
, iNew
);
524 EnableItem(menu
, iClose
);
526 DisableItem(menu
, iClose
);
527 EnableItem(menu
, iQuit
);
529 if (window
!= NULL
&& mac_wininfo(window
)->adjustmenus
!= NULL
)
530 (*mac_wininfo(window
)->adjustmenus
)(window
);
532 DisableItem(menu
, iSave
);
533 DisableItem(menu
, iSaveAs
);
534 DisableItem(menu
, iDuplicate
);
535 menu
= GetMenuHandle(mEdit
);
536 DisableItem(menu
, 0);
537 menu
= GetMenuHandle(mWindow
);
538 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
544 * Make sure the right cursor's being displayed.
546 static void mac_adjustcursor(RgnHandle cursrgn
) {
548 WindowPtr window
, front
;
550 #if TARGET_API_MAC_CARBON
556 LocalToGlobal(&mouse
);
557 part
= FindWindow(mouse
, &window
);
558 front
= FrontWindow();
559 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
560 /* Cursor isn't in the front window, so switch to arrow */
561 #if TARGET_API_MAC_CARBON
562 GetQDGlobalsArrow(&arrow
);
565 SetCursor(&qd
.arrow
);
567 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
569 #if TARGET_API_MAC_CARBON
571 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
572 DiffRgn(cursrgn
, visrgn
, cursrgn
);
575 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
579 if (mac_wininfo(window
)->adjustcursor
!= NULL
)
580 (*mac_wininfo(window
)->adjustcursor
)(window
, mouse
, cursrgn
);
582 #if TARGET_API_MAC_CARBON
583 GetQDGlobalsArrow(&arrow
);
585 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
587 SetCursor(&qd
.arrow
);
588 CopyRgn(window
->visRgn
, cursrgn
);
594 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
600 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
601 &type
, NULL
, 0, &size
) == noErr
)
602 return errAEParamMissed
;
608 void cleanup_exit(int status
)
611 #if !TARGET_RT_MAC_CFM
612 if (mac_gestalts
.encvvers
!= 0)
613 TerminateUnicodeConverter();
619 /* This should only kill the current session, not the whole application. */
620 void connection_fatal(void *fontend
, char *fmt
, ...) {
625 /* We'd like stuff to be a Pascal string */
626 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
628 ParamText(stuff
, NULL
, NULL
, NULL
);
629 StopAlert(128, NULL
);
633 /* Null SSH agent client -- never finds an agent. */
635 int agent_exists(void)
641 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
648 /* Temporary null routines for testing. */
650 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
651 char *keystr
, char *fingerprint
)
656 void askcipher(void *frontend
, char *ciphername
, int cs
)
661 void old_keyfile_warning(void)
666 FontSpec
platform_default_fontspec(char const *name
)
671 if (!strcmp(name
, "Font")) {
672 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
674 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
676 GetFontName(HiWord(smfs
), ret
.name
);
677 if (ret
.name
[0] == 0)
678 memcpy(ret
.name
, "\pMonaco", 7);
679 ret
.size
= LoWord(smfs
);
681 memcpy(ret
.name
, "\pMonaco", 7);
692 Filename
platform_default_filename(const char *name
)
695 if (!strcmp(name
, "LogFileName"))
696 FSMakeFSSpec(0, 0, "\pputty.log", &ret
.fss
);
698 memset(&ret
, 0, sizeof(ret
));
702 char *platform_default_s(char const *name
)
707 int platform_default_i(char const *name
, int def
)
710 /* Non-raw cut and paste of line-drawing chars works badly on the
711 * current Unix stub implementation of the Unicode functions.
712 * So I'm going to temporarily set the default to raw mode so
713 * that the failure mode isn't quite so drastically horrid.
714 * When Unicode comes in, this can all be put right. */
715 if (!strcmp(name
, "RawCNP"))
720 void platform_get_x11_auth(char *display
, int *proto
,
721 unsigned char *data
, int *datalen
)
723 /* SGT: I have no idea whether Mac X servers need anything here. */
728 * c-file-style: "simon"