1 /* $Id: mac.c,v 1.48 2003/02/12 23:53:15 ben Exp $ */
3 * Copyright (c) 1999 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_openabout(void);
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
);
327 case kHighLevelEvent
:
328 AEProcessAppleEvent(event
); /* errors? */
333 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
337 switch (mac_windowtype(window
)) {
339 mac_clickterm(window
, event
);
342 dialog
= GetDialogFromWindow(window
);
343 if (DialogSelect(event
, &dialog
, &item
))
351 mac_clickdlg(window
, event
);
354 mac_clickeventlog(window
, event
);
359 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
361 switch (mac_windowtype(window
)) {
363 mac_growterm(window
, event
);
366 mac_groweventlog(window
, event
);
371 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
374 active
= (event
->modifiers
& activeFlag
) != 0;
376 switch (mac_windowtype(window
)) {
378 mac_activateterm(window
, active
);
381 mac_activatedlg(window
, event
);
384 mac_activateabout(window
, event
);
387 mac_activateeventlog(window
, event
);
392 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
394 DialogItemType itemtype
;
400 dialog
= GetDialogFromWindow(window
);
401 active
= (event
->modifiers
& activeFlag
) != 0;
402 GetDialogItem(dialog
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
403 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
404 DialogSelect(event
, &dialog
, &item
);
407 static void mac_updatewindow(WindowPtr window
)
409 #if TARGET_API_MAC_CARBON
413 switch (mac_windowtype(window
)) {
415 mac_updateterm(window
);
420 #if TARGET_API_MAC_CARBON
422 GetPortVisibleRegion(GetWindowPort(window
), rgn
);
423 UpdateDialog(GetDialogFromWindow(window
), rgn
);
426 UpdateDialog(window
, window
->visRgn
);
431 mac_updatelicence(window
);
434 mac_updateeventlog(window
);
439 static void mac_updatelicence(WindowPtr window
)
446 SetPort((GrafPtr
)GetWindowPort(window
));
448 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
449 TextFont(HiWord(fondsize
));
450 TextSize(LoWord(fondsize
));
451 h
= Get1Resource('TEXT', wLicence
);
452 len
= GetResourceSizeOnDisk(h
);
453 #if TARGET_API_MAC_CARBON
454 GetPortBounds(GetWindowPort(window
), &textrect
);
456 textrect
= window
->portRect
;
460 TETextBox(*h
, len
, &textrect
, teFlushDefault
);
467 * Work out what kind of window we're dealing with.
469 static int mac_windowtype(WindowPtr window
)
472 #if !TARGET_API_MAC_CARBON
473 if (GetWindowKind(window
) < 0)
476 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
482 static void mac_keypress(EventRecord
*event
) {
485 window
= FrontWindow();
487 * Check for a command-key combination, but ignore it if it counts
488 * as a meta-key combination and we're in a terminal window.
490 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
491 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
492 mac_windowtype(window) == wTerminal)*/) {
494 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
496 switch (mac_windowtype(window
)) {
498 mac_keyterm(window
, event
);
504 static void mac_menucommand(long result
) {
507 #if !TARGET_API_MAC_CARBON
511 menu
= HiWord(result
);
512 item
= LoWord(result
);
513 window
= FrontWindow();
514 /* Things which do the same whatever window we're in. */
521 #if !TARGET_API_MAC_CARBON
523 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
538 mac_closewindow(window
);
555 /* If we get here, handling is up to window-specific code. */
556 switch (mac_windowtype(window
)) {
558 mac_menuterm(window
, menu
, item
);
565 static void mac_openabout(void) {
566 DialogItemType itemtype
;
574 SelectWindow(windows
.about
);
577 GetDialogWindow(GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1));
578 wi
= smalloc(sizeof(*wi
));
581 SetWRefCon(windows
.about
, (long)wi
);
582 vers
= (VersRecHndl
)Get1Resource('vers', 1);
583 if (vers
!= NULL
&& *vers
!= NULL
) {
584 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
585 GetDialogItem(GetDialogFromWindow(windows
.about
), wiAboutVersion
,
586 &itemtype
, &item
, &box
);
587 assert(itemtype
& kStaticTextDialogItem
);
588 SetDialogItemText(item
, longvers
);
590 ShowWindow(windows
.about
);
594 static void mac_openlicence(void) {
598 SelectWindow(windows
.licence
);
600 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
601 wi
= smalloc(sizeof(*wi
));
603 wi
->wtype
= wLicence
;
604 SetWRefCon(windows
.licence
, (long)wi
);
605 ShowWindow(windows
.licence
);
609 static void mac_closewindow(WindowPtr window
) {
611 switch (mac_windowtype(window
)) {
612 #if !TARGET_API_MAC_CARBON
614 CloseDeskAcc(GetWindowKind(window
));
618 mac_closeterm(window
);
621 windows
.about
= NULL
;
622 DisposeDialog(GetDialogFromWindow(window
));
625 windows
.licence
= NULL
;
626 DisposeWindow(window
);
631 static void mac_zoomwindow(WindowPtr window
, short part
) {
633 /* FIXME: do something */
637 * Make the menus look right before the user gets to see them.
639 #if TARGET_API_MAC_CARBON
640 #define EnableItem EnableMenuItem
641 #define DisableItem DisableMenuItem
643 static void mac_adjustmenus(void) {
647 window
= FrontWindow();
648 menu
= GetMenuHandle(mApple
);
650 EnableItem(menu
, iAbout
);
652 menu
= GetMenuHandle(mFile
);
654 EnableItem(menu
, iNew
);
656 EnableItem(menu
, iClose
);
658 DisableItem(menu
, iClose
);
659 EnableItem(menu
, iQuit
);
661 switch (mac_windowtype(window
)) {
663 DisableItem(menu
, iSave
); /* XXX enable if modified */
664 EnableItem(menu
, iSaveAs
);
665 EnableItem(menu
, iDuplicate
);
666 menu
= GetMenuHandle(mEdit
);
667 DisableItem(menu
, 0);
670 mac_adjusttermmenus(window
);
673 DisableItem(menu
, iSave
);
674 DisableItem(menu
, iSaveAs
);
675 DisableItem(menu
, iDuplicate
);
676 menu
= GetMenuHandle(mEdit
);
677 DisableItem(menu
, 0);
678 menu
= GetMenuHandle(mWindow
);
679 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
686 * Make sure the right cursor's being displayed.
688 static void mac_adjustcursor(RgnHandle cursrgn
) {
690 WindowPtr window
, front
;
692 #if TARGET_API_MAC_CARBON
698 LocalToGlobal(&mouse
);
699 part
= FindWindow(mouse
, &window
);
700 front
= FrontWindow();
701 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
702 /* Cursor isn't in the front window, so switch to arrow */
703 #if TARGET_API_MAC_CARBON
704 GetQDGlobalsArrow(&arrow
);
707 SetCursor(&qd
.arrow
);
709 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
711 #if TARGET_API_MAC_CARBON
713 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
714 DiffRgn(cursrgn
, visrgn
, cursrgn
);
717 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
721 switch (mac_windowtype(window
)) {
723 mac_adjusttermcursor(window
, mouse
, cursrgn
);
726 #if TARGET_API_MAC_CARBON
727 GetQDGlobalsArrow(&arrow
);
729 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
731 SetCursor(&qd
.arrow
);
732 CopyRgn(window
->visRgn
, cursrgn
);
739 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
745 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
746 &type
, NULL
, 0, &size
) == noErr
)
747 return errAEParamMissed
;
753 void cleanup_exit(int status
)
756 #if !TARGET_RT_MAC_CFM
757 if (mac_gestalts
.encvvers
!= 0)
758 TerminateUnicodeConverter();
764 /* This should only kill the current session, not the whole application. */
765 void connection_fatal(void *fontend
, char *fmt
, ...) {
770 /* We'd like stuff to be a Pascal string */
771 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
773 ParamText(stuff
, NULL
, NULL
, NULL
);
774 StopAlert(128, NULL
);
778 /* Null SSH agent client -- never finds an agent. */
780 int agent_exists(void)
786 void agent_query(void *in
, int inlen
, void **out
, int *outlen
)
793 /* Temporary null routines for testing. */
795 void verify_ssh_host_key(void *frontend
, char *host
, int port
, char *keytype
,
796 char *keystr
, char *fingerprint
)
801 void askcipher(void *frontend
, char *ciphername
, int cs
)
806 void old_keyfile_warning(void)
811 FontSpec
platform_default_fontspec(char const *name
)
816 if (!strcmp(name
, "Font")) {
817 smfs
= GetScriptVariable(smSystemScript
, smScriptMonoFondSize
);
819 smfs
= GetScriptVariable(smRoman
, smScriptMonoFondSize
);
821 GetFontName(HiWord(smfs
), ret
.name
);
822 if (ret
.name
[0] == 0)
823 memcpy(ret
.name
, "\pMonaco", 7);
824 ret
.size
= LoWord(smfs
);
826 memcpy(ret
.name
, "\pMonaco", 7);
837 Filename
platform_default_filename(const char *name
)
840 if (!strcmp(name
, "LogFileName"))
841 FSMakeFSSpec(0, 0, "\pputty.log", &ret
.fss
);
843 memset(&ret
, 0, sizeof(ret
));
847 char *platform_default_s(char const *name
)
852 int platform_default_i(char const *name
, int def
)
855 /* Non-raw cut and paste of line-drawing chars works badly on the
856 * current Unix stub implementation of the Unicode functions.
857 * So I'm going to temporarily set the default to raw mode so
858 * that the failure mode isn't quite so drastically horrid.
859 * When Unicode comes in, this can all be put right. */
860 if (!strcmp(name
, "RawCNP"))
865 void platform_get_x11_auth(char *display
, int *proto
,
866 unsigned char *data
, int *datalen
)
868 /* SGT: I have no idea whether Mac X servers need anything here. */
873 * c-file-style: "simon"