1 /* $Id: mac.c,v 1.19 2003/01/05 12:53:38 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);
221 static void mac_event(EventRecord *event) {
226 switch (event->what) {
228 part = FindWindow(event->where, &window);
232 mac_menucommand(MenuSelect(event->where));
235 SystemClick(event, window);
238 if (window != FrontWindow())
239 /* XXX: check for movable modal dboxes? */
240 SelectWindow(window);
242 mac_contentclick(window, event);
245 if (TrackGoAway(window, event->where))
246 mac_closewindow(window);
249 /* XXX: moveable modal check? */
250 DragWindow(window, event->where, &qd.screenBits.bounds);
253 mac_growwindow(window, event);
257 if (TrackBox(window, event->where, part))
258 mac_zoomwindow(window, part);
267 mac_activatewindow((WindowPtr)event->message, event);
270 mac_updatewindow((WindowPtr)event->message);
273 if (HiWord(event->message) != noErr) {
274 SetPt(&pt, 120, 120);
275 DIBadMount(pt, event->message);
281 static void mac_contentclick(WindowPtr window, EventRecord *event) {
284 switch (mac_windowtype(window)) {
286 mac_clickterm(window, event);
289 if (DialogSelect(event, &window, &item))
297 mac_clickdlg(window, event);
302 static void mac_growwindow(WindowPtr window, EventRecord *event) {
304 switch (mac_windowtype(window)) {
306 mac_growterm(window, event);
310 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
313 active = (event->modifiers & activeFlag) != 0;
315 switch (mac_windowtype(window)) {
317 mac_activateterm(window, active);
320 mac_activatedlg(window, event);
323 mac_activateabout(window, event);
328 static void mac_activateabout(WindowPtr window, EventRecord *event) {
329 DialogItemType itemtype;
335 active = (event->modifiers & activeFlag) != 0;
336 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
337 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
338 DialogSelect(event, &window, &item);
341 static void mac_updatewindow(WindowPtr window) {
343 switch (mac_windowtype(window)) {
345 mac_updateterm(window);
350 UpdateDialog(window, window->visRgn);
354 mac_updatelicence(window);
359 static void mac_updatelicence(WindowPtr window)
367 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
368 TextFont(HiWord(fondsize));
369 TextSize(LoWord(fondsize));
370 h = Get1Resource('TEXT', wLicence);
371 len = GetResourceSizeOnDisk(h);
374 TETextBox(*h, len, &window->portRect, teFlushDefault);
381 * Work out what kind of window we're dealing with.
382 * Concept shamelessly nicked from SurfWriter.
384 static int mac_windowtype(WindowPtr window) {
390 kind = ((WindowPeek)window)->windowKind;
393 if (GetWVariant(window) == zoomDocProc)
395 refcon = GetWRefCon(window);
405 static void mac_keypress(EventRecord *event) {
408 window = FrontWindow();
410 * Check for a command-key combination, but ignore it if it counts
411 * as a meta-key combination and we're in a terminal window.
413 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
414 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
415 mac_windowtype(window) == wTerminal)*/) {
417 mac_menucommand(MenuKey(event->message & charCodeMask));
419 switch (mac_windowtype(window)) {
421 mac_keyterm(window, event);
427 static void mac_menucommand(long result) {
432 menu = HiWord(result);
433 item = LoWord(result);
434 window = FrontWindow();
435 /* Things which do the same whatever window we're in. */
443 GetMenuItemText(GetMenuHandle(mApple), item, da);
457 mac_closewindow(window);
465 /* If we get here, handling is up to window-specific code. */
466 switch (mac_windowtype(window)) {
468 mac_menuterm(window, menu, item);
475 static void mac_openabout(void) {
476 DialogItemType itemtype;
483 SelectWindow(windows.about);
485 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
486 vers = (VersRecHndl)Get1Resource('vers', 1);
487 if (vers != NULL && *vers != NULL) {
488 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
489 GetDialogItem(windows.about, wiAboutVersion,
490 &itemtype, &item, &box);
491 assert(itemtype & kStaticTextDialogItem);
492 SetDialogItemText(item, longvers);
494 ShowWindow(windows.about);
498 static void mac_openlicence(void) {
501 SelectWindow(windows.licence);
503 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
504 ShowWindow(windows.licence);
508 static void mac_closewindow(WindowPtr window) {
510 switch (mac_windowtype(window)) {
512 CloseDeskAcc(((WindowPeek)window)->windowKind);
515 /* FIXME: end session and stuff */
518 windows.about = NULL;
522 windows.licence = NULL;
531 static void mac_zoomwindow(WindowPtr window, short part) {
533 /* FIXME: do something */
537 * Make the menus look right before the user gets to see them.
539 static void mac_adjustmenus(void) {
543 window = FrontWindow();
544 menu = GetMenuHandle(mApple);
546 EnableItem(menu, iAbout);
548 menu = GetMenuHandle(mFile);
550 EnableItem(menu, iNew);
552 EnableItem(menu, iClose);
554 DisableItem(menu, iClose);
555 EnableItem(menu, iQuit);
557 switch (mac_windowtype(window)) {
559 mac_adjusttermmenus(window);
562 menu = GetMenuHandle(mEdit);
563 DisableItem(menu, 0);
570 * Make sure the right cursor's being displayed.
572 static void mac_adjustcursor(RgnHandle cursrgn) {
574 WindowPtr window, front;
578 LocalToGlobal(&mouse);
579 part = FindWindow(mouse, &window);
580 front = FrontWindow();
581 if (part != inContent || window == NULL || window != front) {
582 /* Cursor isn't in the front window, so switch to arrow */
583 SetCursor(&qd.arrow);
584 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
586 DiffRgn(cursrgn, front->visRgn, cursrgn);
588 switch (mac_windowtype(window)) {
590 mac_adjusttermcursor(window, mouse, cursrgn);
593 SetCursor(&qd.arrow);
594 CopyRgn(window->visRgn, cursrgn);
600 static void mac_shutdown(void) {
602 #if !TARGET_RT_MAC_CFM
603 if (mac_gestalts.encvvers != 0)
604 TerminateUnicodeConverter();
610 void fatalbox(char *fmt, ...) {
615 /* We'd like stuff to be a Pascal string */
616 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
618 ParamText(stuff, NULL, NULL, NULL);
619 StopAlert(128, NULL);
623 void modalfatalbox(char *fmt, ...) {
628 /* We'd like stuff to be a Pascal string */
629 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
631 ParamText(stuff, NULL, NULL, NULL);
632 StopAlert(128, NULL);
636 /* This should only kill the current session, not the whole application. */
637 void connection_fatal(void *fontend, char *fmt, ...) {
642 /* We'd like stuff to be a Pascal string */
643 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
645 ParamText(stuff, NULL, NULL, NULL);
646 StopAlert(128, NULL);
652 * c-file-style: "simon"