bf873e8e |
1 | /* $Id: mac.c,v 1.11 2002/12/30 19:01:44 ben Exp $ */ |
d082ac49 |
2 | /* |
3 | * Copyright (c) 1999 Ben Harris |
4 | * All rights reserved. |
5 | * |
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 |
13 | * conditions: |
14 | * |
15 | * The above copyright notice and this permission notice shall be |
16 | * included in all copies or substantial portions of the Software. |
17 | * |
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 |
25 | * SOFTWARE. |
26 | */ |
27 | /* |
28 | * mac.c -- miscellaneous Mac-specific routines |
29 | */ |
30 | |
31 | #include <MacTypes.h> |
32 | #include <Quickdraw.h> |
33 | #include <Fonts.h> |
34 | #include <MacWindows.h> |
35 | #include <Menus.h> |
36 | #include <TextEdit.h> |
37 | #include <Appearance.h> |
38 | #include <CodeFragments.h> |
39 | #include <Dialogs.h> |
40 | #include <Devices.h> |
41 | #include <DiskInit.h> |
42 | #include <Gestalt.h> |
bf873e8e |
43 | #include <LowMem.h> |
d082ac49 |
44 | #include <Resources.h> |
36577dc9 |
45 | #include <Script.h> |
8768ce31 |
46 | #include <TextCommon.h> |
d082ac49 |
47 | #include <ToolUtils.h> |
8768ce31 |
48 | #include <UnicodeConverter.h> |
d082ac49 |
49 | |
50 | #include <assert.h> |
51 | #include <limits.h> |
52 | #include <stdarg.h> |
53 | #include <stdlib.h> /* putty.h needs size_t */ |
54 | #include <stdio.h> /* for vsprintf */ |
55 | |
56 | #define PUTTY_DO_GLOBALS |
57 | |
58 | #include "macresid.h" |
59 | #include "putty.h" |
60 | #include "mac.h" |
61 | |
62 | QDGlobals qd; |
63 | |
64 | static int cold = 1; |
65 | struct mac_gestalts mac_gestalts; |
66 | |
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); |
ebd78b62 |
76 | static void mac_updatelicence(WindowPtr); |
d082ac49 |
77 | static void mac_keypress(EventRecord *); |
78 | static int mac_windowtype(WindowPtr); |
79 | static void mac_menucommand(long); |
80 | static void mac_openabout(void); |
ebd78b62 |
81 | static void mac_openlicence(void); |
d082ac49 |
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) |
88 | |
89 | struct mac_windows { |
90 | WindowPtr about; |
91 | WindowPtr licence; |
92 | }; |
93 | |
94 | struct mac_windows windows; |
95 | |
96 | int main (int argc, char **argv) { |
97 | |
98 | mac_startup(); |
99 | mac_eventloop(); |
100 | } |
101 | |
102 | #pragma noreturn (main) |
103 | |
104 | static void mac_startup(void) { |
105 | Handle menuBar; |
8768ce31 |
106 | TECInfoHandle ti; |
d082ac49 |
107 | |
108 | /* Init Memory Manager */ |
109 | MaxApplZone(); |
110 | /* Init QuickDraw */ |
111 | InitGraf(&qd.thePort); |
112 | /* Init Font Manager */ |
113 | InitFonts(); |
114 | /* Init Window Manager */ |
115 | InitWindows(); |
116 | /* Init Menu Manager */ |
117 | InitMenus(); |
118 | /* Init TextEdit */ |
119 | TEInit(); |
120 | /* Init Dialog Manager */ |
121 | InitDialogs(nil); |
122 | cold = 0; |
123 | |
56ed4cf7 |
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 | mac_gestalts.sysvers &= 0xffff; |
d082ac49 |
129 | /* Find out if we've got Color Quickdraw */ |
130 | if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr) |
131 | mac_gestalts.qdvers = gestaltOriginalQD; |
132 | /* ... and the Appearance Manager? */ |
133 | if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr) |
134 | if (Gestalt(gestaltAppearanceAttr, NULL) == noErr) |
135 | mac_gestalts.apprvers = 0x0100; |
136 | else |
137 | mac_gestalts.apprvers = 0; |
138 | #if TARGET_RT_MAC_CFM |
139 | /* Paranoia: Did we manage to pull in AppearanceLib? */ |
140 | if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress) |
141 | mac_gestalts.apprvers = 0; |
142 | #endif |
143 | /* Mac OS 8.5 Control Manager (proportional scrollbars)? */ |
144 | if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr) |
145 | mac_gestalts.cntlattr = 0; |
146 | /* Mac OS 8.5 Window Manager? */ |
147 | if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr) |
148 | mac_gestalts.windattr = 0; |
8768ce31 |
149 | /* Text Encoding Conversion Manager? */ |
150 | if ( |
151 | #if TARGET_RT_MAC_CFM |
152 | &TECGetInfo == kUnresolvedCFragSymbolAddress || |
153 | #else |
154 | InitializeUnicodeConverter(NULL) != noErr || |
155 | #endif |
156 | TECGetInfo(&ti) != noErr) |
157 | mac_gestalts.encvvers = 0; |
158 | else { |
159 | mac_gestalts.encvvers = (*ti)->tecVersion; |
160 | DisposeHandle((Handle)ti); |
161 | } |
d082ac49 |
162 | |
163 | /* We've been tested with the Appearance Manager */ |
164 | if (mac_gestalts.apprvers != 0) |
165 | RegisterAppearanceClient(); |
166 | |
167 | menuBar = GetNewMBar(128); |
168 | if (menuBar == NULL) |
169 | fatalbox("Unable to create menu bar."); |
170 | SetMenuBar(menuBar); |
171 | AppendResMenu(GetMenuHandle(mApple), 'DRVR'); |
172 | mac_adjustmenus(); |
173 | DrawMenuBar(); |
174 | InitCursor(); |
175 | windows.about = NULL; |
176 | windows.licence = NULL; |
177 | |
bf873e8e |
178 | { |
179 | short vol; |
180 | long dirid; |
181 | |
182 | /* Set the default directory for loading and saving settings. */ |
183 | /* XXX Should we create it? */ |
184 | if (get_session_dir(FALSE, &vol, &dirid) == noErr) { |
185 | LMSetSFSaveDisk(-vol); |
186 | LMSetCurDirStore(dirid); |
187 | } |
188 | } |
d082ac49 |
189 | init_ucs(); |
190 | } |
191 | |
192 | static void mac_eventloop(void) { |
193 | Boolean gotevent; |
194 | EventRecord event; |
195 | RgnHandle cursrgn; |
196 | |
197 | cursrgn = NewRgn(); |
198 | for (;;) { |
199 | mac_adjustcursor(cursrgn); |
200 | gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn); |
201 | mac_adjustcursor(cursrgn); |
202 | if (gotevent) |
203 | mac_event(&event); |
d082ac49 |
204 | } |
205 | DisposeRgn(cursrgn); |
206 | } |
207 | |
208 | static void mac_event(EventRecord *event) { |
209 | short part; |
210 | WindowPtr window; |
211 | Point pt; |
212 | |
213 | switch (event->what) { |
214 | case mouseDown: |
215 | part = FindWindow(event->where, &window); |
216 | switch (part) { |
217 | case inMenuBar: |
218 | mac_adjustmenus(); |
219 | mac_menucommand(MenuSelect(event->where)); |
220 | break; |
221 | case inSysWindow: |
222 | SystemClick(event, window); |
223 | break; |
224 | case inContent: |
225 | if (window != FrontWindow()) |
226 | /* XXX: check for movable modal dboxes? */ |
227 | SelectWindow(window); |
228 | else |
229 | mac_contentclick(window, event); |
230 | break; |
231 | case inGoAway: |
232 | if (TrackGoAway(window, event->where)) |
233 | mac_closewindow(window); |
234 | break; |
235 | case inDrag: |
236 | /* XXX: moveable modal check? */ |
237 | DragWindow(window, event->where, &qd.screenBits.bounds); |
238 | break; |
239 | case inGrow: |
240 | mac_growwindow(window, event); |
241 | break; |
242 | case inZoomIn: |
243 | case inZoomOut: |
244 | if (TrackBox(window, event->where, part)) |
245 | mac_zoomwindow(window, part); |
246 | break; |
247 | } |
248 | break; |
249 | case keyDown: |
250 | case autoKey: |
251 | mac_keypress(event); |
252 | break; |
253 | case activateEvt: |
254 | mac_activatewindow((WindowPtr)event->message, event); |
255 | break; |
256 | case updateEvt: |
257 | mac_updatewindow((WindowPtr)event->message); |
258 | break; |
259 | case diskEvt: |
260 | if (HiWord(event->message) != noErr) { |
261 | SetPt(&pt, 120, 120); |
262 | DIBadMount(pt, event->message); |
263 | } |
264 | break; |
265 | } |
266 | } |
267 | |
268 | static void mac_contentclick(WindowPtr window, EventRecord *event) { |
269 | short item; |
270 | |
271 | switch (mac_windowtype(window)) { |
272 | case wTerminal: |
273 | mac_clickterm(window, event); |
274 | break; |
275 | case wAbout: |
276 | if (DialogSelect(event, &(DialogPtr)window, &item)) |
277 | switch (item) { |
278 | case wiAboutLicence: |
ebd78b62 |
279 | mac_openlicence(); |
d082ac49 |
280 | break; |
281 | } |
282 | break; |
283 | } |
284 | } |
285 | |
286 | static void mac_growwindow(WindowPtr window, EventRecord *event) { |
287 | |
288 | switch (mac_windowtype(window)) { |
289 | case wTerminal: |
290 | mac_growterm(window, event); |
291 | } |
292 | } |
293 | |
294 | static void mac_activatewindow(WindowPtr window, EventRecord *event) { |
295 | int active; |
296 | |
297 | active = (event->modifiers & activeFlag) != 0; |
298 | mac_adjustmenus(); |
299 | switch (mac_windowtype(window)) { |
300 | case wTerminal: |
301 | mac_activateterm(window, active); |
302 | break; |
303 | case wAbout: |
304 | mac_activateabout(window, event); |
305 | break; |
306 | } |
307 | } |
308 | |
309 | static void mac_activateabout(WindowPtr window, EventRecord *event) { |
310 | DialogItemType itemtype; |
311 | Handle itemhandle; |
312 | short item; |
313 | Rect itemrect; |
314 | int active; |
315 | |
316 | active = (event->modifiers & activeFlag) != 0; |
317 | GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect); |
318 | HiliteControl((ControlHandle)itemhandle, active ? 0 : 255); |
319 | DialogSelect(event, &window, &item); |
320 | } |
321 | |
322 | static void mac_updatewindow(WindowPtr window) { |
323 | |
324 | switch (mac_windowtype(window)) { |
325 | case wTerminal: |
326 | mac_updateterm(window); |
327 | break; |
328 | case wAbout: |
329 | BeginUpdate(window); |
330 | UpdateDialog(window, window->visRgn); |
331 | EndUpdate(window); |
332 | break; |
333 | case wLicence: |
ebd78b62 |
334 | mac_updatelicence(window); |
335 | break; |
336 | } |
337 | } |
338 | |
339 | static void mac_updatelicence(WindowPtr window) |
340 | { |
341 | Handle h; |
342 | int len; |
36577dc9 |
343 | long fondsize; |
ebd78b62 |
344 | |
345 | SetPort(window); |
346 | BeginUpdate(window); |
36577dc9 |
347 | fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize); |
348 | TextFont(HiWord(fondsize)); |
349 | TextSize(LoWord(fondsize)); |
ebd78b62 |
350 | h = Get1Resource('TEXT', wLicence); |
351 | len = GetResourceSizeOnDisk(h); |
352 | if (h != NULL) { |
353 | HLock(h); |
354 | TETextBox(*h, len, &window->portRect, teFlushDefault); |
355 | HUnlock(h); |
d082ac49 |
356 | } |
ebd78b62 |
357 | EndUpdate(window); |
d082ac49 |
358 | } |
359 | |
360 | /* |
361 | * Work out what kind of window we're dealing with. |
362 | * Concept shamelessly nicked from SurfWriter. |
363 | */ |
364 | static int mac_windowtype(WindowPtr window) { |
365 | int kind; |
366 | |
367 | if (window == NULL) |
368 | return wNone; |
369 | kind = ((WindowPeek)window)->windowKind; |
370 | if (kind < 0) |
371 | return wDA; |
372 | if (GetWVariant(window) == zoomDocProc) |
373 | return wTerminal; |
374 | return GetWRefCon(window); |
375 | } |
376 | |
377 | /* |
378 | * Handle a key press |
379 | */ |
380 | static void mac_keypress(EventRecord *event) { |
381 | WindowPtr window; |
382 | |
383 | window = FrontWindow(); |
384 | /* |
385 | * Check for a command-key combination, but ignore it if it counts |
386 | * as a meta-key combination and we're in a terminal window. |
387 | */ |
388 | if (event->what == keyDown && (event->modifiers & cmdKey) /*&& |
389 | !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers && |
390 | mac_windowtype(window) == wTerminal)*/) { |
391 | mac_adjustmenus(); |
392 | mac_menucommand(MenuKey(event->message & charCodeMask)); |
393 | } else { |
394 | switch (mac_windowtype(window)) { |
395 | case wTerminal: |
396 | mac_keyterm(window, event); |
397 | break; |
398 | } |
399 | } |
400 | } |
401 | |
402 | static void mac_menucommand(long result) { |
403 | short menu, item; |
404 | Str255 da; |
405 | WindowPtr window; |
406 | |
407 | menu = HiWord(result); |
408 | item = LoWord(result); |
409 | window = FrontWindow(); |
410 | /* Things which do the same whatever window we're in. */ |
411 | switch (menu) { |
412 | case mApple: |
413 | switch (item) { |
414 | case iAbout: |
415 | mac_openabout(); |
416 | goto done; |
417 | default: |
418 | GetMenuItemText(GetMenuHandle(mApple), item, da); |
419 | OpenDeskAcc(da); |
420 | goto done; |
421 | } |
422 | break; |
423 | case mFile: |
424 | switch (item) { |
425 | case iNew: |
426 | mac_newsession(); |
427 | goto done; |
ce283213 |
428 | case iOpen: |
429 | mac_opensession(); |
430 | goto done; |
d082ac49 |
431 | case iClose: |
432 | mac_closewindow(window); |
433 | goto done; |
434 | case iQuit: |
435 | mac_shutdown(); |
436 | goto done; |
437 | } |
438 | break; |
439 | } |
440 | /* If we get here, handling is up to window-specific code. */ |
441 | switch (mac_windowtype(window)) { |
442 | case wTerminal: |
443 | mac_menuterm(window, menu, item); |
444 | break; |
445 | } |
446 | done: |
447 | HiliteMenu(0); |
448 | } |
449 | |
450 | static void mac_openabout(void) { |
451 | DialogItemType itemtype; |
452 | Handle item; |
453 | VersRecHndl vers; |
454 | Rect box; |
455 | StringPtr longvers; |
456 | |
457 | if (windows.about) |
458 | SelectWindow(windows.about); |
459 | else { |
460 | windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1); |
5dbc118e |
461 | vers = (VersRecHndl)Get1Resource('vers', 1); |
462 | if (vers != NULL && *vers != NULL) { |
463 | longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1; |
464 | GetDialogItem(windows.about, wiAboutVersion, |
465 | &itemtype, &item, &box); |
466 | assert(itemtype & kStaticTextDialogItem); |
467 | SetDialogItemText(item, longvers); |
468 | } |
d082ac49 |
469 | ShowWindow(windows.about); |
470 | } |
471 | } |
472 | |
ebd78b62 |
473 | static void mac_openlicence(void) { |
ebd78b62 |
474 | |
475 | if (windows.licence) |
476 | SelectWindow(windows.licence); |
477 | else { |
478 | windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1); |
479 | ShowWindow(windows.licence); |
480 | } |
481 | } |
482 | |
d082ac49 |
483 | static void mac_closewindow(WindowPtr window) { |
484 | |
485 | switch (mac_windowtype(window)) { |
486 | case wDA: |
487 | CloseDeskAcc(((WindowPeek)window)->windowKind); |
488 | break; |
489 | case wTerminal: |
490 | /* FIXME: end session and stuff */ |
491 | break; |
492 | case wAbout: |
493 | windows.about = NULL; |
494 | CloseWindow(window); |
495 | break; |
ebd78b62 |
496 | case wLicence: |
497 | windows.licence = NULL; |
498 | CloseWindow(window); |
499 | break; |
d082ac49 |
500 | default: |
501 | CloseWindow(window); |
502 | break; |
503 | } |
504 | } |
505 | |
506 | static void mac_zoomwindow(WindowPtr window, short part) { |
507 | |
508 | /* FIXME: do something */ |
509 | } |
510 | |
511 | /* |
512 | * Make the menus look right before the user gets to see them. |
513 | */ |
514 | static void mac_adjustmenus(void) { |
515 | WindowPtr window; |
516 | MenuHandle menu; |
517 | |
518 | window = FrontWindow(); |
519 | menu = GetMenuHandle(mApple); |
520 | EnableItem(menu, 0); |
521 | EnableItem(menu, iAbout); |
522 | |
523 | menu = GetMenuHandle(mFile); |
524 | EnableItem(menu, 0); |
525 | EnableItem(menu, iNew); |
526 | if (window != NULL) |
527 | EnableItem(menu, iClose); |
528 | else |
529 | DisableItem(menu, iClose); |
530 | EnableItem(menu, iQuit); |
531 | |
532 | switch (mac_windowtype(window)) { |
533 | case wTerminal: |
534 | mac_adjusttermmenus(window); |
535 | break; |
536 | default: |
537 | menu = GetMenuHandle(mEdit); |
538 | DisableItem(menu, 0); |
539 | break; |
540 | } |
541 | DrawMenuBar(); |
542 | } |
543 | |
544 | /* |
545 | * Make sure the right cursor's being displayed. |
546 | */ |
547 | static void mac_adjustcursor(RgnHandle cursrgn) { |
548 | Point mouse; |
549 | WindowPtr window, front; |
550 | short part; |
551 | |
552 | GetMouse(&mouse); |
553 | LocalToGlobal(&mouse); |
554 | part = FindWindow(mouse, &window); |
555 | front = FrontWindow(); |
556 | if (part != inContent || window == NULL || window != front) { |
557 | /* Cursor isn't in the front window, so switch to arrow */ |
558 | SetCursor(&qd.arrow); |
559 | SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX); |
560 | if (front != NULL) |
561 | DiffRgn(cursrgn, front->visRgn, cursrgn); |
562 | } else { |
563 | switch (mac_windowtype(window)) { |
564 | case wTerminal: |
565 | mac_adjusttermcursor(window, mouse, cursrgn); |
566 | break; |
567 | default: |
568 | SetCursor(&qd.arrow); |
569 | CopyRgn(window->visRgn, cursrgn); |
570 | break; |
571 | } |
572 | } |
573 | } |
574 | |
575 | static void mac_shutdown(void) { |
576 | |
713b8b7a |
577 | #if !TARGET_RT_MAC_CFM |
8768ce31 |
578 | if (mac_gestalts.encvvers != 0) |
579 | TerminateUnicodeConverter(); |
713b8b7a |
580 | #endif |
d082ac49 |
581 | exit(0); |
582 | } |
583 | |
584 | void fatalbox(char *fmt, ...) { |
585 | va_list ap; |
586 | Str255 stuff; |
587 | |
588 | va_start(ap, fmt); |
589 | /* We'd like stuff to be a Pascal string */ |
590 | stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap); |
591 | va_end(ap); |
592 | ParamText(stuff, NULL, NULL, NULL); |
593 | StopAlert(128, nil); |
594 | exit(1); |
595 | } |
596 | |
597 | void modalfatalbox(char *fmt, ...) { |
598 | va_list ap; |
599 | Str255 stuff; |
600 | |
601 | va_start(ap, fmt); |
602 | /* We'd like stuff to be a Pascal string */ |
603 | stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap); |
604 | va_end(ap); |
605 | ParamText(stuff, NULL, NULL, NULL); |
606 | StopAlert(128, nil); |
607 | exit(1); |
608 | } |
609 | |
610 | /* |
611 | * Local Variables: |
612 | * c-file-style: "simon" |
613 | * End: |
614 | */ |