1 /* $Id: mac.c,v 1.59 2003/05/10 12:27:38 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>
35 #include <Quickdraw.h>
37 #include <MacWindows.h>
40 #include <Appearance.h>
41 #include <CodeFragments.h>
47 #include <Navigation.h>
48 #include <Resources.h>
50 #include <TextCommon.h>
51 #include <ToolUtils.h>
52 #include <UnicodeConverter.h>
57 #include <stdlib.h> /* putty.h needs size_t */
58 #include <stdio.h> /* for vsprintf */
60 #define PUTTY_DO_GLOBALS
71 static int borednow
= FALSE
;
72 struct mac_gestalts mac_gestalts
;
75 static void mac_startup(void);
76 static void mac_eventloop(void);
77 #pragma noreturn (mac_eventloop)
78 static void mac_event(EventRecord
*);
79 static void mac_contentclick(WindowPtr
, EventRecord
*);
80 static void mac_growwindow(WindowPtr
, EventRecord
*);
81 static void mac_activatewindow(WindowPtr
, EventRecord
*);
82 static void mac_suspendresume(EventRecord
*);
83 static void mac_activateabout(WindowPtr
, EventRecord
*);
84 static void mac_updatewindow(WindowPtr
);
85 static void mac_updatelicence(WindowPtr
);
86 static void mac_keypress(EventRecord
*);
87 static int mac_windowtype(WindowPtr
);
88 static void mac_menucommand(long);
89 static void mac_openlicence(void);
90 static void mac_adjustcursor(RgnHandle
);
91 static void mac_adjustmenus(void);
92 static void mac_closewindow(WindowPtr
);
93 static void mac_zoomwindow(WindowPtr
, short);
94 #pragma noreturn (cleanup_exit)
101 struct mac_windows windows
;
103 int main (int argc
, char **argv
) {
109 #pragma noreturn (main)
111 static void mac_startup(void) {
115 #if !TARGET_API_MAC_CARBON
116 /* Init Memory Manager */
119 InitGraf(&qd
.thePort
);
120 /* Init Font Manager */
122 /* Init Window Manager */
124 /* Init Menu Manager */
128 /* Init Dialog Manager */
133 /* Get base system version (only used if there's no better selector) */
134 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
135 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
136 fatalbox("PuTTY requires System 7 or newer");
137 /* Find out if we've got Color Quickdraw */
138 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
139 mac_gestalts
.qdvers
= gestaltOriginalQD
;
140 /* ... and the Appearance Manager? */
141 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
142 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
143 mac_gestalts
.apprvers
= 0x0100;
145 mac_gestalts
.apprvers
= 0;
146 #if TARGET_RT_MAC_CFM
147 /* Paranoia: Did we manage to pull in AppearanceLib? */
148 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
149 mac_gestalts
.apprvers
= 0;
152 mac_gestalts
.cntlattr
= 0;
153 mac_gestalts
.windattr
= 0;
155 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
156 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
157 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
158 mac_gestalts
.cntlattr
= 0;
159 /* Mac OS 8.5 Window Manager? */
160 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
161 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
162 mac_gestalts
.windattr
= 0;
163 /* Mac OS 8.5 Menu Manager? */
164 if (Gestalt(gestaltMenuMgrAttr
, &mac_gestalts
.menuattr
) != noErr
)
165 mac_gestalts
.menuattr
= 0;
167 /* Text Encoding Conversion Manager? */
169 #if TARGET_RT_MAC_CFM
170 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
172 InitializeUnicodeConverter(NULL
) != noErr
||
174 TECGetInfo(&ti
) != noErr
)
175 mac_gestalts
.encvvers
= 0;
177 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
178 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
179 DisposeHandle((Handle
)ti
);
181 /* Navigation Services? */
182 if (NavServicesAvailable())
183 mac_gestalts
.navsvers
= NavLibraryVersion();
185 mac_gestalts
.navsvers
= 0;
189 /* We've been tested with the Appearance Manager */
190 if (mac_gestalts
.apprvers
!= 0)
191 RegisterAppearanceClient();
193 menuBar
= GetNewMBar(128);
195 fatalbox("Unable to create menu bar.");
197 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
198 if (mac_gestalts
.menuattr
& gestaltMenuMgrAquaLayoutMask
) {
199 DeleteMenuItem(GetMenuHandle(mFile
), iQuit
);
200 /* Also delete the separator above the Quit item. */
201 DeleteMenuItem(GetMenuHandle(mFile
), iQuit
- 1);
206 windows
.about
= NULL
;
207 windows
.licence
= NULL
;
209 default_protocol
= be_default_protocol
;
210 /* Find the appropriate default port. */
213 default_port
= 0; /* illegal */
214 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
215 if (backends
[i
].protocol
== default_protocol
) {
216 default_port
= backends
[i
].backend
->default_port
;
220 flags
= FLAG_INTERACTIVE
;
222 #if !TARGET_API_MAC_CARBON
227 /* Set the default directory for loading and saving settings. */
228 /* XXX Should we create it? */
229 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
230 LMSetSFSaveDisk(-vol
);
231 LMSetCurDirStore(dirid
);
236 /* Install Apple Event handlers. */
237 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
238 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
239 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
240 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
241 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
242 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
243 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
244 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
247 static void mac_eventloop(void) {
255 mac_adjustcursor(cursrgn
);
256 gotevent
= WaitNextEvent(everyEvent
, &event
, sleeptime
, cursrgn
);
258 * XXX For now, limit sleep time to 1/10 s to work around
259 * wake-before-sleep race in MacTCP code.
262 mac_adjustcursor(cursrgn
);
264 /* Ensure we get a null event when the real ones run out. */
273 if (mac_gestalts
.apprvers
>= 0x100 && mac_frontwindow() != NULL
)
274 IdleControls(mac_frontwindow());
279 static void mac_event(EventRecord
*event
) {
283 switch (event
->what
) {
285 part
= FindWindow(event
->where
, &window
);
289 mac_menucommand(MenuSelect(event
->where
));
291 #if !TARGET_API_MAC_CARBON
293 SystemClick(event
, window
);
297 if (window
!= FrontWindow())
298 /* XXX: check for movable modal dboxes? */
299 SelectWindow(window
);
301 mac_contentclick(window
, event
);
304 if (TrackGoAway(window
, event
->where
))
305 mac_closewindow(window
);
308 /* XXX: moveable modal check? */
309 #if TARGET_API_MAC_CARBON
313 GetQDGlobalsScreenBits(&screenBits
);
314 DragWindow(window
, event
->where
, &screenBits
.bounds
);
317 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
321 mac_growwindow(window
, event
);
325 if (TrackBox(window
, event
->where
, part
))
326 mac_zoomwindow(window
, part
);
335 mac_activatewindow((WindowPtr
)event
->message
, event
);
338 mac_updatewindow((WindowPtr
)event
->message
);
340 #if !TARGET_API_MAC_CARBON
342 if (HiWord(event
->message
) != noErr
) {
345 SetPt(&pt
, 120, 120);
346 DIBadMount(pt
, event
->message
);
351 switch ((event
->message
& osEvtMessageMask
) >> 24) {
352 case suspendResumeMessage
:
353 mac_suspendresume(event
);
357 case kHighLevelEvent
:
358 AEProcessAppleEvent(event
); /* errors? */
363 static void mac_contentclick(WindowPtr window
, EventRecord
*event
)
366 if (mac_wininfo(window
)->click
!= NULL
)
367 (*mac_wininfo(window
)->click
)(window
, event
);
370 static void mac_growwindow(WindowPtr window
, EventRecord
*event
)
373 if (mac_wininfo(window
)->grow
!= NULL
)
374 (*mac_wininfo(window
)->grow
)(window
, event
);
377 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
)
381 if (mac_wininfo(window
)->activate
!= NULL
)
382 (*mac_wininfo(window
)->activate
)(window
, event
);
385 static void mac_updatewindow(WindowPtr window
)
388 if (mac_wininfo(window
)->update
!= NULL
)
389 (*mac_wininfo(window
)->update
)(window
);
393 * Work out what kind of window we're dealing with.
395 static int mac_windowtype(WindowPtr window
)
398 #if !TARGET_API_MAC_CARBON
399 if (GetWindowKind(window
) < 0)
402 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
408 static void mac_keypress(EventRecord
*event
) {
411 window
= mac_frontwindow();
413 * Check for a command-key combination, but ignore it if it counts
414 * as a meta-key combination and we're in a terminal window.
416 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
417 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
418 mac_windowtype(window) == wTerminal)*/) {
420 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
422 if (window
!= NULL
&& mac_wininfo(window
)->key
!= NULL
)
423 (*mac_wininfo(window
)->key
)(window
, event
);
427 static void mac_menucommand(long result
) {
430 #if !TARGET_API_MAC_CARBON
434 menu
= HiWord(result
);
435 item
= LoWord(result
);
436 window
= mac_frontwindow();
437 /* Things which do the same whatever window we're in. */
444 #if !TARGET_API_MAC_CARBON
446 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
461 mac_closewindow(window
);
478 /* If we get here, handling is up to window-specific code. */
479 if (window
!= NULL
&& mac_wininfo(window
)->menu
!= NULL
)
480 (*mac_wininfo(window
)->menu
)(window
, menu
, item
);
486 static void mac_closewindow(WindowPtr window
) {
488 switch (mac_windowtype(window
)) {
489 #if !TARGET_API_MAC_CARBON
491 CloseDeskAcc(GetWindowKind(window
));
495 if (mac_wininfo(window
)->close
!= NULL
)
496 (*mac_wininfo(window
)->close
)(window
);
501 static void mac_suspendresume(EventRecord
*event
)
504 EventRecord fakeevent
;
507 * We're called either before we're suspended or after we're
508 * resumed, so we're the front application at this point.
510 front
= FrontWindow();
512 fakeevent
.what
= activateEvt
;
513 fakeevent
.message
= (UInt32
)front
;
514 fakeevent
.when
= event
->when
;
515 fakeevent
.where
= event
->where
;
516 fakeevent
.modifiers
=
517 (event
->message
& resumeFlag
) ? activeFlag
: 0;
518 mac_activatewindow(front
, &fakeevent
);
522 static void mac_zoomwindow(WindowPtr window
, short part
) {
524 /* FIXME: do something */
528 * Make the menus look right before the user gets to see them.
530 #if TARGET_API_MAC_CARBON
531 #define EnableItem EnableMenuItem
532 #define DisableItem DisableMenuItem
534 static void mac_adjustmenus(void) {
538 window
= mac_frontwindow();
539 menu
= GetMenuHandle(mApple
);
541 EnableItem(menu
, iAbout
);
543 menu
= GetMenuHandle(mFile
);
545 EnableItem(menu
, iNew
);
547 EnableItem(menu
, iClose
);
549 DisableItem(menu
, iClose
);
550 EnableItem(menu
, iQuit
);
552 if (window
!= NULL
&& mac_wininfo(window
)->adjustmenus
!= NULL
)
553 (*mac_wininfo(window
)->adjustmenus
)(window
);
555 DisableItem(menu
, iSave
);
556 DisableItem(menu
, iSaveAs
);
557 DisableItem(menu
, iDuplicate
);
558 menu
= GetMenuHandle(mEdit
);
559 DisableItem(menu
, 0);
560 menu
= GetMenuHandle(mWindow
);
561 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
567 * Make sure the right cursor's being displayed.
569 static void mac_adjustcursor(RgnHandle cursrgn
) {
571 WindowPtr window
, front
;
573 #if TARGET_API_MAC_CARBON
579 LocalToGlobal(&mouse
);
580 part
= FindWindow(mouse
, &window
);
581 front
= FrontWindow();
582 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
583 /* Cursor isn't in the front window, so switch to arrow */
584 #if TARGET_API_MAC_CARBON
585 GetQDGlobalsArrow(&arrow
);
588 SetCursor(&qd
.arrow
);
590 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
592 #if TARGET_API_MAC_CARBON
594 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
595 DiffRgn(cursrgn
, visrgn
, cursrgn
);
598 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
602 if (mac_wininfo(window
)->adjustcursor
!= NULL
)
603 (*mac_wininfo(window
)->adjustcursor
)(window
, mouse
, cursrgn
);
605 #if TARGET_API_MAC_CARBON
606 GetQDGlobalsArrow(&arrow
);
608 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
610 SetCursor(&qd
.arrow
);
611 CopyRgn(window
->visRgn
, cursrgn
);
617 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
623 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
624 &type
, NULL
, 0, &size
) == noErr
)
625 return errAEParamMissed
;
631 void cleanup_exit(int status
)
634 #if !TARGET_RT_MAC_CFM
635 if (mac_gestalts
.encvvers
!= 0)
636 TerminateUnicodeConverter();
642 /* This should only kill the current session, not the whole application. */
643 void connection_fatal(void *fontend
, char *fmt
, ...) {
648 /* We'd like stuff to be a Pascal string */
649 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
651 ParamText(stuff
, NULL
, NULL
, NULL
);
652 StopAlert(128, NULL
);
656 /* Null SSH agent client -- never finds an agent. */
658 int agent_exists(void)
664 int agent_query(void *in
, int inlen
, void **out
, int *outlen
,
665 void (*callback
)(void *, void *, int), void *callback_ctx
)
673 /* Temporary null routines for testing. */
675 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
676 char *keystr
, char *fingerprint
)
679 Session
*s
= frontend
;
682 * This function is horribly wrong. For one thing, the alert
683 * shouldn't be modal, it should be movable modal, or a sheet in
684 * Aqua. Also, PuTTY might be in the background, in which case we
685 * should use the Notification Manager to wake up the user. In
686 * any case, we shouldn't hold up processing of other connections'
687 * data just because this one's waiting for the user. It should
688 * also handle a host key cache, of course, and see the note below
689 * about closing the connection. All in all, a bit of a mess
693 stuff
[0] = sprintf((char *)(&stuff
[1]),
694 "The server's key fingerprint is: %s\n"
695 "Continue connecting?", fingerprint
);
696 ParamText(stuff
, NULL
, NULL
, NULL
);
697 if (CautionAlert(wQuestion
, NULL
) == 2) {
699 * User chose "Cancel". Unfortunately, if I tear the
700 * connection down here, Bad Things happen when I return. I
701 * think this function should actually return something
702 * telling the SSH code to abandon the connection.
707 void askcipher(void *frontend
, char *ciphername
, int cs
)
712 void old_keyfile_warning(void)
717 FontSpec
platform_default_fontspec(char const *name
)
722 if (!strcmp(name
, "Font")) {
723 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
725 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
727 GetFontName(HiWord(smfs
), ret
.name
);
728 if (ret
.name
[0] == 0)
729 memcpy(ret
.name
, "\pMonaco", 7);
730 ret
.size
= LoWord(smfs
);
732 memcpy(ret
.name
, "\pMonaco", 7);
743 Filename
platform_default_filename(const char *name
)
746 if (!strcmp(name
, "LogFileName"))
747 FSMakeFSSpec(0, 0, "\pputty.log", &ret
.fss
);
749 memset(&ret
, 0, sizeof(ret
));
753 char *platform_default_s(char const *name
)
758 int platform_default_i(char const *name
, int def
)
761 /* Non-raw cut and paste of line-drawing chars works badly on the
762 * current Unix stub implementation of the Unicode functions.
763 * So I'm going to temporarily set the default to raw mode so
764 * that the failure mode isn't quite so drastically horrid.
765 * When Unicode comes in, this can all be put right. */
766 if (!strcmp(name
, "RawCNP"))
771 void platform_get_x11_auth(char *display
, int *proto
,
772 unsigned char *data
, int *datalen
)
774 /* SGT: I have no idea whether Mac X servers need anything here. */
777 void update_specials_menu(void *frontend
)
779 Session
*s
= frontend
;
782 front
= mac_frontwindow();
783 if (front
!= NULL
&& mac_windowsession(front
) == s
)
789 * c-file-style: "simon"