1 /* $Id: mac.c,v 1.49 2003/02/15 16:22:15 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_activateabout(WindowPtr
, EventRecord
*);
80 static void mac_updatewindow(WindowPtr
);
81 static void mac_updatelicence(WindowPtr
);
82 static void mac_keypress(EventRecord
*);
83 static int mac_windowtype(WindowPtr
);
84 static void mac_menucommand(long);
85 static void mac_openlicence(void);
86 static void mac_adjustcursor(RgnHandle
);
87 static void mac_adjustmenus(void);
88 static void mac_closewindow(WindowPtr
);
89 static void mac_zoomwindow(WindowPtr
, short);
90 #pragma noreturn (cleanup_exit)
97 struct mac_windows windows
;
99 int main (int argc
, char **argv
) {
105 #pragma noreturn (main)
107 static void mac_startup(void) {
111 #if !TARGET_API_MAC_CARBON
112 /* Init Memory Manager */
115 InitGraf(&qd
.thePort
);
116 /* Init Font Manager */
118 /* Init Window Manager */
120 /* Init Menu Manager */
124 /* 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
);
174 /* Navigation Services? */
175 if (NavServicesAvailable())
176 mac_gestalts
.navsvers
= NavLibraryVersion();
178 mac_gestalts
.navsvers
= 0;
182 /* We've been tested with the Appearance Manager */
183 if (mac_gestalts
.apprvers
!= 0)
184 RegisterAppearanceClient();
186 menuBar
= GetNewMBar(128);
188 fatalbox("Unable to create menu bar.");
190 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
194 windows
.about
= NULL
;
195 windows
.licence
= NULL
;
197 default_protocol
= be_default_protocol
;
198 /* Find the appropriate default port. */
201 default_port
= 0; /* illegal */
202 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
203 if (backends
[i
].protocol
== default_protocol
) {
204 default_port
= backends
[i
].backend
->default_port
;
208 flags
= FLAG_INTERACTIVE
;
210 #if !TARGET_API_MAC_CARBON
215 /* Set the default directory for loading and saving settings. */
216 /* XXX Should we create it? */
217 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
218 LMSetSFSaveDisk(-vol
);
219 LMSetCurDirStore(dirid
);
224 /* Install Apple Event handlers. */
225 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
226 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
227 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
228 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
229 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
230 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
231 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
232 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
235 static void mac_eventloop(void) {
242 mac_adjustcursor(cursrgn
);
243 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
244 mac_adjustcursor(cursrgn
);
255 static void mac_event(EventRecord
*event
) {
259 switch (event
->what
) {
261 part
= FindWindow(event
->where
, &window
);
265 mac_menucommand(MenuSelect(event
->where
));
267 #if !TARGET_API_MAC_CARBON
269 SystemClick(event
, window
);
273 if (window
!= FrontWindow())
274 /* XXX: check for movable modal dboxes? */
275 SelectWindow(window
);
277 mac_contentclick(window
, event
);
280 if (TrackGoAway(window
, event
->where
))
281 mac_closewindow(window
);
284 /* XXX: moveable modal check? */
285 #if TARGET_API_MAC_CARBON
289 GetQDGlobalsScreenBits(&screenBits
);
290 DragWindow(window
, event
->where
, &screenBits
.bounds
);
293 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
297 mac_growwindow(window
, event
);
301 if (TrackBox(window
, event
->where
, part
))
302 mac_zoomwindow(window
, part
);
311 mac_activatewindow((WindowPtr
)event
->message
, event
);
314 mac_updatewindow((WindowPtr
)event
->message
);
316 #if !TARGET_API_MAC_CARBON
318 if (HiWord(event
->message
) != noErr
) {
321 SetPt(&pt
, 120, 120);
322 DIBadMount(pt
, event
->message
);
326 case kHighLevelEvent
:
327 AEProcessAppleEvent(event
); /* errors? */
332 static void mac_contentclick(WindowPtr window
, EventRecord
*event
)
335 if (mac_wininfo(window
)->click
!= NULL
)
336 (*mac_wininfo(window
)->click
)(window
, event
);
339 static void mac_growwindow(WindowPtr window
, EventRecord
*event
)
342 if (mac_wininfo(window
)->grow
!= NULL
)
343 (*mac_wininfo(window
)->grow
)(window
, event
);
346 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
)
350 if (mac_wininfo(window
)->activate
!= NULL
)
351 (*mac_wininfo(window
)->activate
)(window
, event
);
354 static void mac_updatewindow(WindowPtr window
)
357 if (mac_wininfo(window
)->update
!= NULL
)
358 (*mac_wininfo(window
)->update
)(window
);
362 * Work out what kind of window we're dealing with.
364 static int mac_windowtype(WindowPtr window
)
367 #if !TARGET_API_MAC_CARBON
368 if (GetWindowKind(window
) < 0)
371 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
377 static void mac_keypress(EventRecord
*event
) {
380 window
= FrontWindow();
382 * Check for a command-key combination, but ignore it if it counts
383 * as a meta-key combination and we're in a terminal window.
385 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
386 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
387 mac_windowtype(window) == wTerminal)*/) {
389 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
391 if (mac_wininfo(window
)->key
!= NULL
)
392 (*mac_wininfo(window
)->key
)(window
, event
);
396 static void mac_menucommand(long result
) {
399 #if !TARGET_API_MAC_CARBON
403 menu
= HiWord(result
);
404 item
= LoWord(result
);
405 window
= FrontWindow();
406 /* Things which do the same whatever window we're in. */
413 #if !TARGET_API_MAC_CARBON
415 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
430 mac_closewindow(window
);
447 /* If we get here, handling is up to window-specific code. */
448 if (mac_wininfo(window
)->menu
!= NULL
)
449 (*mac_wininfo(window
)->menu
)(window
, menu
, item
);
455 static void mac_closewindow(WindowPtr window
) {
457 switch (mac_windowtype(window
)) {
458 #if !TARGET_API_MAC_CARBON
460 CloseDeskAcc(GetWindowKind(window
));
464 if (mac_wininfo(window
)->close
!= NULL
)
465 (*mac_wininfo(window
)->close
)(window
);
470 static void mac_zoomwindow(WindowPtr window
, short part
) {
472 /* FIXME: do something */
476 * Make the menus look right before the user gets to see them.
478 #if TARGET_API_MAC_CARBON
479 #define EnableItem EnableMenuItem
480 #define DisableItem DisableMenuItem
482 static void mac_adjustmenus(void) {
486 window
= FrontWindow();
487 menu
= GetMenuHandle(mApple
);
489 EnableItem(menu
, iAbout
);
491 menu
= GetMenuHandle(mFile
);
493 EnableItem(menu
, iNew
);
495 EnableItem(menu
, iClose
);
497 DisableItem(menu
, iClose
);
498 EnableItem(menu
, iQuit
);
500 if (mac_wininfo(window
)->adjustmenus
!= NULL
)
501 (*mac_wininfo(window
)->adjustmenus
)(window
);
503 DisableItem(menu
, iSave
);
504 DisableItem(menu
, iSaveAs
);
505 DisableItem(menu
, iDuplicate
);
506 menu
= GetMenuHandle(mEdit
);
507 DisableItem(menu
, 0);
508 menu
= GetMenuHandle(mWindow
);
509 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
515 * Make sure the right cursor's being displayed.
517 static void mac_adjustcursor(RgnHandle cursrgn
) {
519 WindowPtr window
, front
;
521 #if TARGET_API_MAC_CARBON
527 LocalToGlobal(&mouse
);
528 part
= FindWindow(mouse
, &window
);
529 front
= FrontWindow();
530 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
531 /* Cursor isn't in the front window, so switch to arrow */
532 #if TARGET_API_MAC_CARBON
533 GetQDGlobalsArrow(&arrow
);
536 SetCursor(&qd
.arrow
);
538 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
540 #if TARGET_API_MAC_CARBON
542 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
543 DiffRgn(cursrgn
, visrgn
, cursrgn
);
546 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
550 if (mac_wininfo(window
)->adjustcursor
!= NULL
)
551 (*mac_wininfo(window
)->adjustcursor
)(window
, mouse
, cursrgn
);
553 #if TARGET_API_MAC_CARBON
554 GetQDGlobalsArrow(&arrow
);
556 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
558 SetCursor(&qd
.arrow
);
559 CopyRgn(window
->visRgn
, cursrgn
);
565 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
571 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
572 &type
, NULL
, 0, &size
) == noErr
)
573 return errAEParamMissed
;
579 void cleanup_exit(int status
)
582 #if !TARGET_RT_MAC_CFM
583 if (mac_gestalts
.encvvers
!= 0)
584 TerminateUnicodeConverter();
590 /* This should only kill the current session, not the whole application. */
591 void connection_fatal(void *fontend
, char *fmt
, ...) {
596 /* We'd like stuff to be a Pascal string */
597 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
599 ParamText(stuff
, NULL
, NULL
, NULL
);
600 StopAlert(128, NULL
);
604 /* Null SSH agent client -- never finds an agent. */
606 int agent_exists(void)
612 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
619 /* Temporary null routines for testing. */
621 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
622 char *keystr
, char *fingerprint
)
627 void askcipher(void *frontend
, char *ciphername
, int cs
)
632 void old_keyfile_warning(void)
637 FontSpec
platform_default_fontspec(char const *name
)
642 if (!strcmp(name
, "Font")) {
643 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
645 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
647 GetFontName(HiWord(smfs
), ret
.name
);
648 if (ret
.name
[0] == 0)
649 memcpy(ret
.name
, "\pMonaco", 7);
650 ret
.size
= LoWord(smfs
);
652 memcpy(ret
.name
, "\pMonaco", 7);
663 Filename
platform_default_filename(const char *name
)
666 if (!strcmp(name
, "LogFileName"))
667 FSMakeFSSpec(0, 0, "\pputty.log", &ret
.fss
);
669 memset(&ret
, 0, sizeof(ret
));
673 char *platform_default_s(char const *name
)
678 int platform_default_i(char const *name
, int def
)
681 /* Non-raw cut and paste of line-drawing chars works badly on the
682 * current Unix stub implementation of the Unicode functions.
683 * So I'm going to temporarily set the default to raw mode so
684 * that the failure mode isn't quite so drastically horrid.
685 * When Unicode comes in, this can all be put right. */
686 if (!strcmp(name
, "RawCNP"))
691 void platform_get_x11_auth(char *display
, int *proto
,
692 unsigned char *data
, int *datalen
)
694 /* SGT: I have no idea whether Mac X servers need anything here. */
699 * c-file-style: "simon"