1fc898ea |
1 | /* $Id: mac.c,v 1.15 2003/01/04 00:13:18 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 |
1fc898ea |
143 | #if TARGET_CPU_68K |
144 | mac_gestalts.cntlattr = 0; |
145 | mac_gestalts.windattr = 0; |
146 | #else |
d082ac49 |
147 | /* Mac OS 8.5 Control Manager (proportional scrollbars)? */ |
1fc898ea |
148 | if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr || |
149 | &SetControlViewSize == kUnresolvedCFragSymbolAddress) |
d082ac49 |
150 | mac_gestalts.cntlattr = 0; |
151 | /* Mac OS 8.5 Window Manager? */ |
1fc898ea |
152 | if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr || |
153 | &SetWindowContentColor == kUnresolvedCFragSymbolAddress) |
d082ac49 |
154 | mac_gestalts.windattr = 0; |
1fc898ea |
155 | #endif |
8768ce31 |
156 | /* Text Encoding Conversion Manager? */ |
157 | if ( |
158 | #if TARGET_RT_MAC_CFM |
159 | &TECGetInfo == kUnresolvedCFragSymbolAddress || |
160 | #else |
161 | InitializeUnicodeConverter(NULL) != noErr || |
162 | #endif |
163 | TECGetInfo(&ti) != noErr) |
164 | mac_gestalts.encvvers = 0; |
165 | else { |
166 | mac_gestalts.encvvers = (*ti)->tecVersion; |
379836ca |
167 | mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures; |
8768ce31 |
168 | DisposeHandle((Handle)ti); |
169 | } |
d082ac49 |
170 | |
171 | /* We've been tested with the Appearance Manager */ |
172 | if (mac_gestalts.apprvers != 0) |
173 | RegisterAppearanceClient(); |
174 | |
175 | menuBar = GetNewMBar(128); |
176 | if (menuBar == NULL) |
177 | fatalbox("Unable to create menu bar."); |
178 | SetMenuBar(menuBar); |
179 | AppendResMenu(GetMenuHandle(mApple), 'DRVR'); |
180 | mac_adjustmenus(); |
181 | DrawMenuBar(); |
182 | InitCursor(); |
183 | windows.about = NULL; |
184 | windows.licence = NULL; |
185 | |
bf873e8e |
186 | { |
187 | short vol; |
188 | long dirid; |
189 | |
190 | /* Set the default directory for loading and saving settings. */ |
191 | /* XXX Should we create it? */ |
192 | if (get_session_dir(FALSE, &vol, &dirid) == noErr) { |
193 | LMSetSFSaveDisk(-vol); |
194 | LMSetCurDirStore(dirid); |
195 | } |
196 | } |
d082ac49 |
197 | init_ucs(); |
198 | } |
199 | |
200 | static void mac_eventloop(void) { |
201 | Boolean gotevent; |
202 | EventRecord event; |
203 | RgnHandle cursrgn; |
204 | |
205 | cursrgn = NewRgn(); |
206 | for (;;) { |
207 | mac_adjustcursor(cursrgn); |
208 | gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn); |
209 | mac_adjustcursor(cursrgn); |
210 | if (gotevent) |
211 | mac_event(&event); |
d082ac49 |
212 | } |
213 | DisposeRgn(cursrgn); |
214 | } |
215 | |
216 | static void mac_event(EventRecord *event) { |
217 | short part; |
218 | WindowPtr window; |
219 | Point pt; |
220 | |
221 | switch (event->what) { |
222 | case mouseDown: |
223 | part = FindWindow(event->where, &window); |
224 | switch (part) { |
225 | case inMenuBar: |
226 | mac_adjustmenus(); |
227 | mac_menucommand(MenuSelect(event->where)); |
228 | break; |
229 | case inSysWindow: |
230 | SystemClick(event, window); |
231 | break; |
232 | case inContent: |
233 | if (window != FrontWindow()) |
234 | /* XXX: check for movable modal dboxes? */ |
235 | SelectWindow(window); |
236 | else |
237 | mac_contentclick(window, event); |
238 | break; |
239 | case inGoAway: |
240 | if (TrackGoAway(window, event->where)) |
241 | mac_closewindow(window); |
242 | break; |
243 | case inDrag: |
244 | /* XXX: moveable modal check? */ |
245 | DragWindow(window, event->where, &qd.screenBits.bounds); |
246 | break; |
247 | case inGrow: |
248 | mac_growwindow(window, event); |
249 | break; |
250 | case inZoomIn: |
251 | case inZoomOut: |
252 | if (TrackBox(window, event->where, part)) |
253 | mac_zoomwindow(window, part); |
254 | break; |
255 | } |
256 | break; |
257 | case keyDown: |
258 | case autoKey: |
259 | mac_keypress(event); |
260 | break; |
261 | case activateEvt: |
262 | mac_activatewindow((WindowPtr)event->message, event); |
263 | break; |
264 | case updateEvt: |
265 | mac_updatewindow((WindowPtr)event->message); |
266 | break; |
267 | case diskEvt: |
268 | if (HiWord(event->message) != noErr) { |
269 | SetPt(&pt, 120, 120); |
270 | DIBadMount(pt, event->message); |
271 | } |
272 | break; |
273 | } |
274 | } |
275 | |
276 | static void mac_contentclick(WindowPtr window, EventRecord *event) { |
277 | short item; |
278 | |
279 | switch (mac_windowtype(window)) { |
280 | case wTerminal: |
281 | mac_clickterm(window, event); |
282 | break; |
283 | case wAbout: |
34e9a202 |
284 | if (DialogSelect(event, &window, &item)) |
d082ac49 |
285 | switch (item) { |
286 | case wiAboutLicence: |
ebd78b62 |
287 | mac_openlicence(); |
d082ac49 |
288 | break; |
289 | } |
290 | break; |
6cb61a05 |
291 | case wSettings: |
292 | mac_clickdlg(window, event); |
293 | break; |
d082ac49 |
294 | } |
295 | } |
296 | |
297 | static void mac_growwindow(WindowPtr window, EventRecord *event) { |
298 | |
299 | switch (mac_windowtype(window)) { |
300 | case wTerminal: |
301 | mac_growterm(window, event); |
302 | } |
303 | } |
304 | |
305 | static void mac_activatewindow(WindowPtr window, EventRecord *event) { |
306 | int active; |
307 | |
308 | active = (event->modifiers & activeFlag) != 0; |
309 | mac_adjustmenus(); |
310 | switch (mac_windowtype(window)) { |
311 | case wTerminal: |
312 | mac_activateterm(window, active); |
313 | break; |
6cb61a05 |
314 | case wSettings: |
315 | mac_activatedlg(window, event); |
316 | break; |
d082ac49 |
317 | case wAbout: |
318 | mac_activateabout(window, event); |
319 | break; |
320 | } |
321 | } |
322 | |
323 | static void mac_activateabout(WindowPtr window, EventRecord *event) { |
324 | DialogItemType itemtype; |
325 | Handle itemhandle; |
326 | short item; |
327 | Rect itemrect; |
328 | int active; |
329 | |
330 | active = (event->modifiers & activeFlag) != 0; |
331 | GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect); |
332 | HiliteControl((ControlHandle)itemhandle, active ? 0 : 255); |
333 | DialogSelect(event, &window, &item); |
334 | } |
335 | |
336 | static void mac_updatewindow(WindowPtr window) { |
337 | |
338 | switch (mac_windowtype(window)) { |
339 | case wTerminal: |
340 | mac_updateterm(window); |
341 | break; |
342 | case wAbout: |
6cb61a05 |
343 | case wSettings: |
d082ac49 |
344 | BeginUpdate(window); |
345 | UpdateDialog(window, window->visRgn); |
346 | EndUpdate(window); |
347 | break; |
348 | case wLicence: |
ebd78b62 |
349 | mac_updatelicence(window); |
350 | break; |
351 | } |
352 | } |
353 | |
354 | static void mac_updatelicence(WindowPtr window) |
355 | { |
356 | Handle h; |
357 | int len; |
36577dc9 |
358 | long fondsize; |
ebd78b62 |
359 | |
360 | SetPort(window); |
361 | BeginUpdate(window); |
36577dc9 |
362 | fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize); |
363 | TextFont(HiWord(fondsize)); |
364 | TextSize(LoWord(fondsize)); |
ebd78b62 |
365 | h = Get1Resource('TEXT', wLicence); |
366 | len = GetResourceSizeOnDisk(h); |
367 | if (h != NULL) { |
368 | HLock(h); |
369 | TETextBox(*h, len, &window->portRect, teFlushDefault); |
370 | HUnlock(h); |
d082ac49 |
371 | } |
ebd78b62 |
372 | EndUpdate(window); |
d082ac49 |
373 | } |
374 | |
375 | /* |
376 | * Work out what kind of window we're dealing with. |
377 | * Concept shamelessly nicked from SurfWriter. |
378 | */ |
379 | static int mac_windowtype(WindowPtr window) { |
380 | int kind; |
6cb61a05 |
381 | long refcon; |
d082ac49 |
382 | |
383 | if (window == NULL) |
384 | return wNone; |
385 | kind = ((WindowPeek)window)->windowKind; |
386 | if (kind < 0) |
387 | return wDA; |
388 | if (GetWVariant(window) == zoomDocProc) |
389 | return wTerminal; |
6cb61a05 |
390 | refcon = GetWRefCon(window); |
391 | if (refcon < 1024) |
392 | return refcon; |
393 | else |
394 | return wSettings; |
d082ac49 |
395 | } |
396 | |
397 | /* |
398 | * Handle a key press |
399 | */ |
400 | static void mac_keypress(EventRecord *event) { |
401 | WindowPtr window; |
402 | |
403 | window = FrontWindow(); |
404 | /* |
405 | * Check for a command-key combination, but ignore it if it counts |
406 | * as a meta-key combination and we're in a terminal window. |
407 | */ |
408 | if (event->what == keyDown && (event->modifiers & cmdKey) /*&& |
409 | !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers && |
410 | mac_windowtype(window) == wTerminal)*/) { |
411 | mac_adjustmenus(); |
412 | mac_menucommand(MenuKey(event->message & charCodeMask)); |
413 | } else { |
414 | switch (mac_windowtype(window)) { |
415 | case wTerminal: |
416 | mac_keyterm(window, event); |
417 | break; |
418 | } |
419 | } |
420 | } |
421 | |
422 | static void mac_menucommand(long result) { |
423 | short menu, item; |
424 | Str255 da; |
425 | WindowPtr window; |
426 | |
427 | menu = HiWord(result); |
428 | item = LoWord(result); |
429 | window = FrontWindow(); |
430 | /* Things which do the same whatever window we're in. */ |
431 | switch (menu) { |
432 | case mApple: |
433 | switch (item) { |
434 | case iAbout: |
435 | mac_openabout(); |
436 | goto done; |
437 | default: |
438 | GetMenuItemText(GetMenuHandle(mApple), item, da); |
439 | OpenDeskAcc(da); |
440 | goto done; |
441 | } |
442 | break; |
443 | case mFile: |
444 | switch (item) { |
445 | case iNew: |
446 | mac_newsession(); |
447 | goto done; |
ce283213 |
448 | case iOpen: |
449 | mac_opensession(); |
450 | goto done; |
d082ac49 |
451 | case iClose: |
452 | mac_closewindow(window); |
453 | goto done; |
454 | case iQuit: |
455 | mac_shutdown(); |
456 | goto done; |
457 | } |
458 | break; |
459 | } |
460 | /* If we get here, handling is up to window-specific code. */ |
461 | switch (mac_windowtype(window)) { |
462 | case wTerminal: |
463 | mac_menuterm(window, menu, item); |
464 | break; |
465 | } |
466 | done: |
467 | HiliteMenu(0); |
468 | } |
469 | |
470 | static void mac_openabout(void) { |
471 | DialogItemType itemtype; |
472 | Handle item; |
473 | VersRecHndl vers; |
474 | Rect box; |
475 | StringPtr longvers; |
476 | |
477 | if (windows.about) |
478 | SelectWindow(windows.about); |
479 | else { |
480 | windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1); |
5dbc118e |
481 | vers = (VersRecHndl)Get1Resource('vers', 1); |
482 | if (vers != NULL && *vers != NULL) { |
483 | longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1; |
484 | GetDialogItem(windows.about, wiAboutVersion, |
485 | &itemtype, &item, &box); |
486 | assert(itemtype & kStaticTextDialogItem); |
487 | SetDialogItemText(item, longvers); |
488 | } |
d082ac49 |
489 | ShowWindow(windows.about); |
490 | } |
491 | } |
492 | |
ebd78b62 |
493 | static void mac_openlicence(void) { |
ebd78b62 |
494 | |
495 | if (windows.licence) |
496 | SelectWindow(windows.licence); |
497 | else { |
498 | windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1); |
499 | ShowWindow(windows.licence); |
500 | } |
501 | } |
502 | |
d082ac49 |
503 | static void mac_closewindow(WindowPtr window) { |
504 | |
505 | switch (mac_windowtype(window)) { |
506 | case wDA: |
507 | CloseDeskAcc(((WindowPeek)window)->windowKind); |
508 | break; |
509 | case wTerminal: |
510 | /* FIXME: end session and stuff */ |
511 | break; |
512 | case wAbout: |
513 | windows.about = NULL; |
514 | CloseWindow(window); |
515 | break; |
ebd78b62 |
516 | case wLicence: |
517 | windows.licence = NULL; |
518 | CloseWindow(window); |
519 | break; |
d082ac49 |
520 | default: |
521 | CloseWindow(window); |
522 | break; |
523 | } |
524 | } |
525 | |
526 | static void mac_zoomwindow(WindowPtr window, short part) { |
527 | |
528 | /* FIXME: do something */ |
529 | } |
530 | |
531 | /* |
532 | * Make the menus look right before the user gets to see them. |
533 | */ |
534 | static void mac_adjustmenus(void) { |
535 | WindowPtr window; |
536 | MenuHandle menu; |
537 | |
538 | window = FrontWindow(); |
539 | menu = GetMenuHandle(mApple); |
540 | EnableItem(menu, 0); |
541 | EnableItem(menu, iAbout); |
542 | |
543 | menu = GetMenuHandle(mFile); |
544 | EnableItem(menu, 0); |
545 | EnableItem(menu, iNew); |
546 | if (window != NULL) |
547 | EnableItem(menu, iClose); |
548 | else |
549 | DisableItem(menu, iClose); |
550 | EnableItem(menu, iQuit); |
551 | |
552 | switch (mac_windowtype(window)) { |
553 | case wTerminal: |
554 | mac_adjusttermmenus(window); |
555 | break; |
556 | default: |
557 | menu = GetMenuHandle(mEdit); |
558 | DisableItem(menu, 0); |
559 | break; |
560 | } |
561 | DrawMenuBar(); |
562 | } |
563 | |
564 | /* |
565 | * Make sure the right cursor's being displayed. |
566 | */ |
567 | static void mac_adjustcursor(RgnHandle cursrgn) { |
568 | Point mouse; |
569 | WindowPtr window, front; |
570 | short part; |
571 | |
572 | GetMouse(&mouse); |
573 | LocalToGlobal(&mouse); |
574 | part = FindWindow(mouse, &window); |
575 | front = FrontWindow(); |
576 | if (part != inContent || window == NULL || window != front) { |
577 | /* Cursor isn't in the front window, so switch to arrow */ |
578 | SetCursor(&qd.arrow); |
579 | SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX); |
580 | if (front != NULL) |
581 | DiffRgn(cursrgn, front->visRgn, cursrgn); |
582 | } else { |
583 | switch (mac_windowtype(window)) { |
584 | case wTerminal: |
585 | mac_adjusttermcursor(window, mouse, cursrgn); |
586 | break; |
587 | default: |
588 | SetCursor(&qd.arrow); |
589 | CopyRgn(window->visRgn, cursrgn); |
590 | break; |
591 | } |
592 | } |
593 | } |
594 | |
595 | static void mac_shutdown(void) { |
596 | |
713b8b7a |
597 | #if !TARGET_RT_MAC_CFM |
8768ce31 |
598 | if (mac_gestalts.encvvers != 0) |
599 | TerminateUnicodeConverter(); |
713b8b7a |
600 | #endif |
d082ac49 |
601 | exit(0); |
602 | } |
603 | |
604 | void fatalbox(char *fmt, ...) { |
605 | va_list ap; |
606 | Str255 stuff; |
607 | |
608 | va_start(ap, fmt); |
609 | /* We'd like stuff to be a Pascal string */ |
610 | stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap); |
611 | va_end(ap); |
612 | ParamText(stuff, NULL, NULL, NULL); |
613 | StopAlert(128, nil); |
614 | exit(1); |
615 | } |
616 | |
617 | void modalfatalbox(char *fmt, ...) { |
618 | va_list ap; |
619 | Str255 stuff; |
620 | |
621 | va_start(ap, fmt); |
622 | /* We'd like stuff to be a Pascal string */ |
623 | stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap); |
624 | va_end(ap); |
625 | ParamText(stuff, NULL, NULL, NULL); |
626 | StopAlert(128, nil); |
627 | exit(1); |
628 | } |
629 | |
630 | /* |
631 | * Local Variables: |
632 | * c-file-style: "simon" |
633 | * End: |
634 | */ |