1 /* $Id: mac.c,v 1.52 2003/02/27 23:34: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;
160 /* Mac OS 8.5 Menu Manager? */
161 if (Gestalt(gestaltMenuMgrAttr
, &mac_gestalts
.menuattr
) != noErr
)
162 mac_gestalts
.menuattr
= 0;
164 /* Text Encoding Conversion Manager? */
166 #if TARGET_RT_MAC_CFM
167 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
169 InitializeUnicodeConverter(NULL
) != noErr
||
171 TECGetInfo(&ti
) != noErr
)
172 mac_gestalts
.encvvers
= 0;
174 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
175 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
176 DisposeHandle((Handle
)ti
);
178 /* Navigation Services? */
179 if (NavServicesAvailable())
180 mac_gestalts
.navsvers
= NavLibraryVersion();
182 mac_gestalts
.navsvers
= 0;
186 /* We've been tested with the Appearance Manager */
187 if (mac_gestalts
.apprvers
!= 0)
188 RegisterAppearanceClient();
190 menuBar
= GetNewMBar(128);
192 fatalbox("Unable to create menu bar.");
194 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
195 if (mac_gestalts
.menuattr
& gestaltMenuMgrAquaLayoutMask
)
196 DeleteMenuItem(GetMenuHandle(mFile
), iQuit
);
200 windows
.about
= NULL
;
201 windows
.licence
= NULL
;
203 default_protocol
= be_default_protocol
;
204 /* Find the appropriate default port. */
207 default_port
= 0; /* illegal */
208 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
209 if (backends
[i
].protocol
== default_protocol
) {
210 default_port
= backends
[i
].backend
->default_port
;
214 flags
= FLAG_INTERACTIVE
;
216 #if !TARGET_API_MAC_CARBON
221 /* Set the default directory for loading and saving settings. */
222 /* XXX Should we create it? */
223 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
224 LMSetSFSaveDisk(-vol
);
225 LMSetCurDirStore(dirid
);
230 /* Install Apple Event handlers. */
231 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
232 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
233 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
234 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
235 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
236 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
237 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
238 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
241 static void mac_eventloop(void) {
248 mac_adjustcursor(cursrgn
);
249 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
250 mac_adjustcursor(cursrgn
);
261 static void mac_event(EventRecord
*event
) {
265 switch (event
->what
) {
267 part
= FindWindow(event
->where
, &window
);
271 mac_menucommand(MenuSelect(event
->where
));
273 #if !TARGET_API_MAC_CARBON
275 SystemClick(event
, window
);
279 if (window
!= FrontWindow())
280 /* XXX: check for movable modal dboxes? */
281 SelectWindow(window
);
283 mac_contentclick(window
, event
);
286 if (TrackGoAway(window
, event
->where
))
287 mac_closewindow(window
);
290 /* XXX: moveable modal check? */
291 #if TARGET_API_MAC_CARBON
295 GetQDGlobalsScreenBits(&screenBits
);
296 DragWindow(window
, event
->where
, &screenBits
.bounds
);
299 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
303 mac_growwindow(window
, event
);
307 if (TrackBox(window
, event
->where
, part
))
308 mac_zoomwindow(window
, part
);
317 mac_activatewindow((WindowPtr
)event
->message
, event
);
320 mac_updatewindow((WindowPtr
)event
->message
);
322 #if !TARGET_API_MAC_CARBON
324 if (HiWord(event
->message
) != noErr
) {
327 SetPt(&pt
, 120, 120);
328 DIBadMount(pt
, event
->message
);
333 switch ((event
->message
& osEvtMessageMask
) >> 24) {
334 case suspendResumeMessage
:
335 mac_suspendresume(event
);
339 case kHighLevelEvent
:
340 AEProcessAppleEvent(event
); /* errors? */
345 static void mac_contentclick(WindowPtr window
, EventRecord
*event
)
348 if (mac_wininfo(window
)->click
!= NULL
)
349 (*mac_wininfo(window
)->click
)(window
, event
);
352 static void mac_growwindow(WindowPtr window
, EventRecord
*event
)
355 if (mac_wininfo(window
)->grow
!= NULL
)
356 (*mac_wininfo(window
)->grow
)(window
, event
);
359 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
)
363 if (mac_wininfo(window
)->activate
!= NULL
)
364 (*mac_wininfo(window
)->activate
)(window
, event
);
367 static void mac_updatewindow(WindowPtr window
)
370 if (mac_wininfo(window
)->update
!= NULL
)
371 (*mac_wininfo(window
)->update
)(window
);
375 * Work out what kind of window we're dealing with.
377 static int mac_windowtype(WindowPtr window
)
380 #if !TARGET_API_MAC_CARBON
381 if (GetWindowKind(window
) < 0)
384 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
390 static void mac_keypress(EventRecord
*event
) {
393 window
= mac_frontwindow();
395 * Check for a command-key combination, but ignore it if it counts
396 * as a meta-key combination and we're in a terminal window.
398 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
399 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
400 mac_windowtype(window) == wTerminal)*/) {
402 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
404 if (window
!= NULL
&& mac_wininfo(window
)->key
!= NULL
)
405 (*mac_wininfo(window
)->key
)(window
, event
);
409 static void mac_menucommand(long result
) {
412 #if !TARGET_API_MAC_CARBON
416 menu
= HiWord(result
);
417 item
= LoWord(result
);
418 window
= mac_frontwindow();
419 /* Things which do the same whatever window we're in. */
426 #if !TARGET_API_MAC_CARBON
428 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
443 mac_closewindow(window
);
460 /* If we get here, handling is up to window-specific code. */
461 if (window
!= NULL
&& mac_wininfo(window
)->menu
!= NULL
)
462 (*mac_wininfo(window
)->menu
)(window
, menu
, item
);
468 static void mac_closewindow(WindowPtr window
) {
470 switch (mac_windowtype(window
)) {
471 #if !TARGET_API_MAC_CARBON
473 CloseDeskAcc(GetWindowKind(window
));
477 if (mac_wininfo(window
)->close
!= NULL
)
478 (*mac_wininfo(window
)->close
)(window
);
483 static void mac_suspendresume(EventRecord
*event
)
486 EventRecord fakeevent
;
489 * We're called either before we're suspended or after we're
490 * resumed, so we're the front application at this point.
492 front
= FrontWindow();
494 fakeevent
.what
= activateEvt
;
495 fakeevent
.message
= (UInt32
)front
;
496 fakeevent
.when
= event
->when
;
497 fakeevent
.where
= event
->where
;
498 fakeevent
.modifiers
=
499 (event
->message
& resumeFlag
) ? activeFlag
: 0;
500 mac_activatewindow(front
, &fakeevent
);
504 static void mac_zoomwindow(WindowPtr window
, short part
) {
506 /* FIXME: do something */
510 * Make the menus look right before the user gets to see them.
512 #if TARGET_API_MAC_CARBON
513 #define EnableItem EnableMenuItem
514 #define DisableItem DisableMenuItem
516 static void mac_adjustmenus(void) {
520 window
= mac_frontwindow();
521 menu
= GetMenuHandle(mApple
);
523 EnableItem(menu
, iAbout
);
525 menu
= GetMenuHandle(mFile
);
527 EnableItem(menu
, iNew
);
529 EnableItem(menu
, iClose
);
531 DisableItem(menu
, iClose
);
532 EnableItem(menu
, iQuit
);
534 if (window
!= NULL
&& mac_wininfo(window
)->adjustmenus
!= NULL
)
535 (*mac_wininfo(window
)->adjustmenus
)(window
);
537 DisableItem(menu
, iSave
);
538 DisableItem(menu
, iSaveAs
);
539 DisableItem(menu
, iDuplicate
);
540 menu
= GetMenuHandle(mEdit
);
541 DisableItem(menu
, 0);
542 menu
= GetMenuHandle(mWindow
);
543 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
549 * Make sure the right cursor's being displayed.
551 static void mac_adjustcursor(RgnHandle cursrgn
) {
553 WindowPtr window
, front
;
555 #if TARGET_API_MAC_CARBON
561 LocalToGlobal(&mouse
);
562 part
= FindWindow(mouse
, &window
);
563 front
= FrontWindow();
564 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
565 /* Cursor isn't in the front window, so switch to arrow */
566 #if TARGET_API_MAC_CARBON
567 GetQDGlobalsArrow(&arrow
);
570 SetCursor(&qd
.arrow
);
572 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
574 #if TARGET_API_MAC_CARBON
576 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
577 DiffRgn(cursrgn
, visrgn
, cursrgn
);
580 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
584 if (mac_wininfo(window
)->adjustcursor
!= NULL
)
585 (*mac_wininfo(window
)->adjustcursor
)(window
, mouse
, cursrgn
);
587 #if TARGET_API_MAC_CARBON
588 GetQDGlobalsArrow(&arrow
);
590 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
592 SetCursor(&qd
.arrow
);
593 CopyRgn(window
->visRgn
, cursrgn
);
599 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
605 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
606 &type
, NULL
, 0, &size
) == noErr
)
607 return errAEParamMissed
;
613 void cleanup_exit(int status
)
616 #if !TARGET_RT_MAC_CFM
617 if (mac_gestalts
.encvvers
!= 0)
618 TerminateUnicodeConverter();
624 /* This should only kill the current session, not the whole application. */
625 void connection_fatal(void *fontend
, char *fmt
, ...) {
630 /* We'd like stuff to be a Pascal string */
631 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
633 ParamText(stuff
, NULL
, NULL
, NULL
);
634 StopAlert(128, NULL
);
638 /* Null SSH agent client -- never finds an agent. */
640 int agent_exists(void)
646 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
653 /* Temporary null routines for testing. */
655 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
656 char *keystr
, char *fingerprint
)
661 void askcipher(void *frontend
, char *ciphername
, int cs
)
666 void old_keyfile_warning(void)
671 FontSpec
platform_default_fontspec(char const *name
)
676 if (!strcmp(name
, "Font")) {
677 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
679 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
681 GetFontName(HiWord(smfs
), ret
.name
);
682 if (ret
.name
[0] == 0)
683 memcpy(ret
.name
, "\pMonaco", 7);
684 ret
.size
= LoWord(smfs
);
686 memcpy(ret
.name
, "\pMonaco", 7);
697 Filename
platform_default_filename(const char *name
)
700 if (!strcmp(name
, "LogFileName"))
701 FSMakeFSSpec(0, 0, "\pputty.log", &ret
.fss
);
703 memset(&ret
, 0, sizeof(ret
));
707 char *platform_default_s(char const *name
)
712 int platform_default_i(char const *name
, int def
)
715 /* Non-raw cut and paste of line-drawing chars works badly on the
716 * current Unix stub implementation of the Unicode functions.
717 * So I'm going to temporarily set the default to raw mode so
718 * that the failure mode isn't quite so drastically horrid.
719 * When Unicode comes in, this can all be put right. */
720 if (!strcmp(name
, "RawCNP"))
725 void platform_get_x11_auth(char *display
, int *proto
,
726 unsigned char *data
, int *datalen
)
728 /* SGT: I have no idea whether Mac X servers need anything here. */
733 * c-file-style: "simon"