1 /* $Id: mac.c,v 1.18 2003/01/05 10:52:56 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 <Quickdraw.h>
34 #include <MacWindows.h>
37 #include <Appearance.h>
38 #include <CodeFragments.h>
44 #include <Resources.h>
46 #include <TextCommon.h>
47 #include <ToolUtils.h>
48 #include <UnicodeConverter.h>
53 #include <stdlib.h> /* putty.h needs size_t */
54 #include <stdio.h> /* for vsprintf */
56 #define PUTTY_DO_GLOBALS
65 struct mac_gestalts mac_gestalts
;
67 static void mac_startup(void);
68 static void mac_eventloop(void);
69 #pragma noreturn (mac_eventloop)
70 static void mac_event(EventRecord
*);
71 static void mac_contentclick(WindowPtr
, EventRecord
*);
72 static void mac_growwindow(WindowPtr
, EventRecord
*);
73 static void mac_activatewindow(WindowPtr
, EventRecord
*);
74 static void mac_activateabout(WindowPtr
, EventRecord
*);
75 static void mac_updatewindow(WindowPtr
);
76 static void mac_updatelicence(WindowPtr
);
77 static void mac_keypress(EventRecord
*);
78 static int mac_windowtype(WindowPtr
);
79 static void mac_menucommand(long);
80 static void mac_openabout(void);
81 static void mac_openlicence(void);
82 static void mac_adjustcursor(RgnHandle
);
83 static void mac_adjustmenus(void);
84 static void mac_closewindow(WindowPtr
);
85 static void mac_zoomwindow(WindowPtr
, short);
86 static void mac_shutdown(void);
87 #pragma noreturn (mac_shutdown)
94 struct mac_windows windows
;
96 int main (int argc
, char **argv
) {
102 #pragma noreturn (main)
104 static void mac_startup(void) {
108 /* Init Memory Manager */
111 InitGraf(&qd
.thePort
);
112 /* Init Font Manager */
114 /* Init Window Manager */
116 /* Init Menu Manager */
120 /* Init Dialog Manager */
124 /* Get base system version (only used if there's no better selector) */
125 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
126 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
127 fatalbox("PuTTY requires System 7 or newer");
128 /* Find out if we've got Color Quickdraw */
129 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
130 mac_gestalts
.qdvers
= gestaltOriginalQD
;
131 /* ... and the Appearance Manager? */
132 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
133 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
134 mac_gestalts
.apprvers
= 0x0100;
136 mac_gestalts
.apprvers
= 0;
137 #if TARGET_RT_MAC_CFM
138 /* Paranoia: Did we manage to pull in AppearanceLib? */
139 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
140 mac_gestalts
.apprvers
= 0;
143 mac_gestalts
.cntlattr
= 0;
144 mac_gestalts
.windattr
= 0;
146 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
147 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
148 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
149 mac_gestalts
.cntlattr
= 0;
150 /* Mac OS 8.5 Window Manager? */
151 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
152 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
153 mac_gestalts
.windattr
= 0;
155 /* Text Encoding Conversion Manager? */
157 #if TARGET_RT_MAC_CFM
158 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
160 InitializeUnicodeConverter(NULL
) != noErr
||
162 TECGetInfo(&ti
) != noErr
)
163 mac_gestalts
.encvvers
= 0;
165 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
166 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
167 DisposeHandle((Handle
)ti
);
172 /* We've been tested with the Appearance Manager */
173 if (mac_gestalts
.apprvers
!= 0)
174 RegisterAppearanceClient();
176 menuBar
= GetNewMBar(128);
178 fatalbox("Unable to create menu bar.");
180 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
184 windows
.about
= NULL
;
185 windows
.licence
= NULL
;
187 default_protocol
= DEFAULT_PROTOCOL
;
188 default_port
= DEFAULT_PORT
;
194 /* Set the default directory for loading and saving settings. */
195 /* XXX Should we create it? */
196 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
197 LMSetSFSaveDisk(-vol
);
198 LMSetCurDirStore(dirid
);
204 static void mac_eventloop(void) {
211 mac_adjustcursor(cursrgn
);
212 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
213 mac_adjustcursor(cursrgn
);
220 static void mac_event(EventRecord
*event
) {
225 switch (event
->what
) {
227 part
= FindWindow(event
->where
, &window
);
231 mac_menucommand(MenuSelect(event
->where
));
234 SystemClick(event
, window
);
237 if (window
!= FrontWindow())
238 /* XXX: check for movable modal dboxes? */
239 SelectWindow(window
);
241 mac_contentclick(window
, event
);
244 if (TrackGoAway(window
, event
->where
))
245 mac_closewindow(window
);
248 /* XXX: moveable modal check? */
249 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
252 mac_growwindow(window
, event
);
256 if (TrackBox(window
, event
->where
, part
))
257 mac_zoomwindow(window
, part
);
266 mac_activatewindow((WindowPtr
)event
->message
, event
);
269 mac_updatewindow((WindowPtr
)event
->message
);
272 if (HiWord(event
->message
) != noErr
) {
273 SetPt(&pt
, 120, 120);
274 DIBadMount(pt
, event
->message
);
280 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
283 switch (mac_windowtype(window
)) {
285 mac_clickterm(window
, event
);
288 if (DialogSelect(event
, &window
, &item
))
296 mac_clickdlg(window
, event
);
301 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
303 switch (mac_windowtype(window
)) {
305 mac_growterm(window
, event
);
309 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
312 active
= (event
->modifiers
& activeFlag
) != 0;
314 switch (mac_windowtype(window
)) {
316 mac_activateterm(window
, active
);
319 mac_activatedlg(window
, event
);
322 mac_activateabout(window
, event
);
327 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
328 DialogItemType itemtype
;
334 active
= (event
->modifiers
& activeFlag
) != 0;
335 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
336 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
337 DialogSelect(event
, &window
, &item
);
340 static void mac_updatewindow(WindowPtr window
) {
342 switch (mac_windowtype(window
)) {
344 mac_updateterm(window
);
349 UpdateDialog(window
, window
->visRgn
);
353 mac_updatelicence(window
);
358 static void mac_updatelicence(WindowPtr window
)
366 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
367 TextFont(HiWord(fondsize
));
368 TextSize(LoWord(fondsize
));
369 h
= Get1Resource('TEXT', wLicence
);
370 len
= GetResourceSizeOnDisk(h
);
373 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
380 * Work out what kind of window we're dealing with.
381 * Concept shamelessly nicked from SurfWriter.
383 static int mac_windowtype(WindowPtr window
) {
389 kind
= ((WindowPeek
)window
)->windowKind
;
392 if (GetWVariant(window
) == zoomDocProc
)
394 refcon
= GetWRefCon(window
);
404 static void mac_keypress(EventRecord
*event
) {
407 window
= FrontWindow();
409 * Check for a command-key combination, but ignore it if it counts
410 * as a meta-key combination and we're in a terminal window.
412 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
413 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
414 mac_windowtype(window) == wTerminal)*/) {
416 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
418 switch (mac_windowtype(window
)) {
420 mac_keyterm(window
, event
);
426 static void mac_menucommand(long result
) {
431 menu
= HiWord(result
);
432 item
= LoWord(result
);
433 window
= FrontWindow();
434 /* Things which do the same whatever window we're in. */
442 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
456 mac_closewindow(window
);
464 /* If we get here, handling is up to window-specific code. */
465 switch (mac_windowtype(window
)) {
467 mac_menuterm(window
, menu
, item
);
474 static void mac_openabout(void) {
475 DialogItemType itemtype
;
482 SelectWindow(windows
.about
);
484 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
485 vers
= (VersRecHndl
)Get1Resource('vers', 1);
486 if (vers
!= NULL
&& *vers
!= NULL
) {
487 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
488 GetDialogItem(windows
.about
, wiAboutVersion
,
489 &itemtype
, &item
, &box
);
490 assert(itemtype
& kStaticTextDialogItem
);
491 SetDialogItemText(item
, longvers
);
493 ShowWindow(windows
.about
);
497 static void mac_openlicence(void) {
500 SelectWindow(windows
.licence
);
502 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
503 ShowWindow(windows
.licence
);
507 static void mac_closewindow(WindowPtr window
) {
509 switch (mac_windowtype(window
)) {
511 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
514 /* FIXME: end session and stuff */
517 windows
.about
= NULL
;
521 windows
.licence
= NULL
;
530 static void mac_zoomwindow(WindowPtr window
, short part
) {
532 /* FIXME: do something */
536 * Make the menus look right before the user gets to see them.
538 static void mac_adjustmenus(void) {
542 window
= FrontWindow();
543 menu
= GetMenuHandle(mApple
);
545 EnableItem(menu
, iAbout
);
547 menu
= GetMenuHandle(mFile
);
549 EnableItem(menu
, iNew
);
551 EnableItem(menu
, iClose
);
553 DisableItem(menu
, iClose
);
554 EnableItem(menu
, iQuit
);
556 switch (mac_windowtype(window
)) {
558 mac_adjusttermmenus(window
);
561 menu
= GetMenuHandle(mEdit
);
562 DisableItem(menu
, 0);
569 * Make sure the right cursor's being displayed.
571 static void mac_adjustcursor(RgnHandle cursrgn
) {
573 WindowPtr window
, front
;
577 LocalToGlobal(&mouse
);
578 part
= FindWindow(mouse
, &window
);
579 front
= FrontWindow();
580 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
581 /* Cursor isn't in the front window, so switch to arrow */
582 SetCursor(&qd
.arrow
);
583 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
585 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
587 switch (mac_windowtype(window
)) {
589 mac_adjusttermcursor(window
, mouse
, cursrgn
);
592 SetCursor(&qd
.arrow
);
593 CopyRgn(window
->visRgn
, cursrgn
);
599 static void mac_shutdown(void) {
601 #if !TARGET_RT_MAC_CFM
602 if (mac_gestalts
.encvvers
!= 0)
603 TerminateUnicodeConverter();
609 void fatalbox(char *fmt
, ...) {
614 /* We'd like stuff to be a Pascal string */
615 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
617 ParamText(stuff
, NULL
, NULL
, NULL
);
618 StopAlert(128, NULL
);
622 void modalfatalbox(char *fmt
, ...) {
627 /* We'd like stuff to be a Pascal string */
628 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
630 ParamText(stuff
, NULL
, NULL
, NULL
);
631 StopAlert(128, NULL
);
635 /* This should only kill the current session, not the whole application. */
636 void connection_fatal(void *fontend
, char *fmt
, ...) {
641 /* We'd like stuff to be a Pascal string */
642 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
644 ParamText(stuff
, NULL
, NULL
, NULL
);
645 StopAlert(128, NULL
);
651 * c-file-style: "simon"