1 /* $Id: macpgen.c,v 1.4 2003/02/20 22:55:09 ben Exp $ */
3 * Copyright (c) 1999, 2003 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
29 * macpgen.c - PuTTYgen for Mac OS
33 #include <AEDataModel.h>
34 #include <AppleEvents.h>
35 #include <Quickdraw.h>
37 #include <MacWindows.h>
40 #include <Appearance.h>
41 #include <CodeFragments.h>
47 #include <Navigation.h>
48 #include <Resources.h>
50 #include <TextCommon.h>
51 #include <ToolUtils.h>
52 #include <UnicodeConverter.h>
57 #include <stdlib.h> /* putty.h needs size_t */
58 #include <stdio.h> /* for vsprintf */
60 #define PUTTY_DO_GLOBALS
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_updatewindow(WindowPtr
);
75 static void mac_keypress(EventRecord
*);
76 static int mac_windowtype(WindowPtr
);
77 static void mac_menucommand(long);
78 static void mac_adjustcursor(RgnHandle
);
79 static void mac_adjustmenus(void);
80 static void mac_closewindow(WindowPtr
);
81 static void mac_zoomwindow(WindowPtr
, short);
82 #pragma noreturn (cleanup_exit)
89 struct mac_windows windows
;
91 struct mac_gestalts mac_gestalts
;
93 int main (int argc
, char **argv
) {
99 #pragma noreturn (main)
101 static void mac_startup(void) {
105 #if !TARGET_API_MAC_CARBON
106 /* Init Memory Manager */
109 InitGraf(&qd
.thePort
);
110 /* Init Font Manager */
112 /* Init Window Manager */
114 /* Init Menu Manager */
118 /* Init Dialog Manager */
122 /* Get base system version (only used if there's no better selector) */
123 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
124 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
125 fatalbox("PuTTYgen requires System 7 or newer");
126 /* Find out if we've got Color Quickdraw */
127 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
128 mac_gestalts
.qdvers
= gestaltOriginalQD
;
129 /* ... and the Appearance Manager? */
130 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
131 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
132 mac_gestalts
.apprvers
= 0x0100;
134 mac_gestalts
.apprvers
= 0;
135 #if TARGET_RT_MAC_CFM
136 /* Paranoia: Did we manage to pull in AppearanceLib? */
137 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
138 mac_gestalts
.apprvers
= 0;
141 mac_gestalts
.cntlattr
= 0;
142 mac_gestalts
.windattr
= 0;
144 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
145 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
146 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
147 mac_gestalts
.cntlattr
= 0;
148 /* Mac OS 8.5 Window Manager? */
149 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
150 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
151 mac_gestalts
.windattr
= 0;
153 /* Text Encoding Conversion Manager? */
155 #if TARGET_RT_MAC_CFM
156 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
158 InitializeUnicodeConverter(NULL
) != noErr
||
160 TECGetInfo(&ti
) != noErr
)
161 mac_gestalts
.encvvers
= 0;
163 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
164 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
165 DisposeHandle((Handle
)ti
);
167 /* Navigation Services? */
168 if (NavServicesAvailable())
169 mac_gestalts
.navsvers
= NavLibraryVersion();
171 mac_gestalts
.navsvers
= 0;
173 /* We've been tested with the Appearance Manager */
174 if (mac_gestalts
.apprvers
!= 0)
175 RegisterAppearanceClient();
177 menuBar
= GetNewMBar(128);
179 fatalbox("Unable to create menu bar.");
181 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
185 windows
.about
= NULL
;
186 windows
.licence
= NULL
;
188 flags
= FLAG_INTERACTIVE
;
190 /* Install Apple Event handlers. */
192 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
193 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
194 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
195 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
196 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
197 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
199 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
200 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
203 static void mac_eventloop(void) {
207 Point mousenow
, mousethen
;
212 GetMouse(&mousethen
);
214 mac_adjustcursor(cursrgn
);
215 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
216 mac_adjustcursor(cursrgn
);
217 front
= mac_frontwindow();
219 ks
= mac_windowkey(front
);
220 if (ks
->collecting_entropy
) {
222 if (mousenow
.h
!= mousethen
.h
|| mousenow
.v
!= mousethen
.v
) {
223 ks
->entropy
[ks
->entropy_got
++] = *(unsigned *)&mousenow
;
224 ks
->entropy
[ks
->entropy_got
++] = TickCount();
225 if (ks
->entropy_got
>= ks
->entropy_required
)
226 ks
->collecting_entropy
= 0;
227 SetControlValue(ks
->progress
, ks
->entropy_got
);
228 mousethen
= mousenow
;
230 SetEmptyRgn(cursrgn
);
242 static void mac_event(EventRecord
*event
) {
246 switch (event
->what
) {
248 part
= FindWindow(event
->where
, &window
);
252 mac_menucommand(MenuSelect(event
->where
));
254 #if !TARGET_API_MAC_CARBON
256 SystemClick(event
, window
);
260 if (window
!= FrontWindow())
261 /* XXX: check for movable modal dboxes? */
262 SelectWindow(window
);
264 mac_contentclick(window
, event
);
267 if (TrackGoAway(window
, event
->where
))
268 mac_closewindow(window
);
271 /* XXX: moveable modal check? */
272 #if TARGET_API_MAC_CARBON
276 GetQDGlobalsScreenBits(&screenBits
);
277 DragWindow(window
, event
->where
, &screenBits
.bounds
);
280 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
284 mac_growwindow(window
, event
);
288 if (TrackBox(window
, event
->where
, part
))
289 mac_zoomwindow(window
, part
);
298 mac_activatewindow((WindowPtr
)event
->message
, event
);
301 mac_updatewindow((WindowPtr
)event
->message
);
303 #if !TARGET_API_MAC_CARBON
305 if (HiWord(event
->message
) != noErr
) {
308 SetPt(&pt
, 120, 120);
309 DIBadMount(pt
, event
->message
);
313 case kHighLevelEvent
:
314 AEProcessAppleEvent(event
); /* errors? */
319 static void mac_contentclick(WindowPtr window
, EventRecord
*event
)
322 if (mac_wininfo(window
)->click
!= NULL
)
323 (*mac_wininfo(window
)->click
)(window
, event
);
326 static void mac_growwindow(WindowPtr window
, EventRecord
*event
)
329 if (mac_wininfo(window
)->grow
!= NULL
)
330 (*mac_wininfo(window
)->grow
)(window
, event
);
333 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
)
337 if (mac_wininfo(window
)->activate
!= NULL
)
338 (*mac_wininfo(window
)->activate
)(window
, event
);
341 static void mac_updatewindow(WindowPtr window
)
344 if (mac_wininfo(window
)->update
!= NULL
)
345 (*mac_wininfo(window
)->update
)(window
);
349 * Work out what kind of window we're dealing with.
351 static int mac_windowtype(WindowPtr window
)
354 #if !TARGET_API_MAC_CARBON
355 if (GetWindowKind(window
) < 0)
358 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
364 static void mac_keypress(EventRecord
*event
)
368 window
= FrontWindow();
369 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
)) {
371 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
373 if (mac_wininfo(window
)->key
!= NULL
)
374 (*mac_wininfo(window
)->key
)(window
, event
);
378 static void mac_menucommand(long result
)
382 #if !TARGET_API_MAC_CARBON
386 menu
= HiWord(result
);
387 item
= LoWord(result
);
388 window
= FrontWindow();
389 /* Things which do the same whatever window we're in. */
396 #if !TARGET_API_MAC_CARBON
398 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
410 mac_closewindow(window
);
418 /* If we get here, handling is up to window-specific code. */
419 if (mac_wininfo(window
)->menu
!= NULL
)
420 (*mac_wininfo(window
)->menu
)(window
, menu
, item
);
426 static void mac_closewindow(WindowPtr window
)
429 switch (mac_windowtype(window
)) {
430 #if !TARGET_API_MAC_CARBON
432 CloseDeskAcc(GetWindowKind(window
));
436 if (mac_wininfo(window
)->close
!= NULL
)
437 (*mac_wininfo(window
)->close
)(window
);
441 static void mac_zoomwindow(WindowPtr window
, short part
) {
443 /* FIXME: do something */
447 * Make the menus look right before the user gets to see them.
449 #if TARGET_API_MAC_CARBON
450 #define EnableItem EnableMenuItem
451 #define DisableItem DisableMenuItem
453 static void mac_adjustmenus(void) {
457 window
= FrontWindow();
458 menu
= GetMenuHandle(mApple
);
460 EnableItem(menu
, iAbout
);
462 menu
= GetMenuHandle(mFile
);
464 EnableItem(menu
, iNew
);
466 EnableItem(menu
, iClose
);
468 DisableItem(menu
, iClose
);
469 EnableItem(menu
, iQuit
);
471 if (mac_wininfo(window
)->adjustmenus
!= NULL
)
472 (*mac_wininfo(window
)->adjustmenus
)(window
);
474 DisableItem(menu
, iSave
);
475 DisableItem(menu
, iSaveAs
);
476 menu
= GetMenuHandle(mEdit
);
477 DisableItem(menu
, 0);
478 menu
= GetMenuHandle(mWindow
);
479 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
485 * Make sure the right cursor's being displayed.
487 static void mac_adjustcursor(RgnHandle cursrgn
)
490 WindowPtr window
, front
;
492 #if TARGET_API_MAC_CARBON
498 LocalToGlobal(&mouse
);
499 part
= FindWindow(mouse
, &window
);
500 front
= FrontWindow();
501 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
502 /* Cursor isn't in the front window, so switch to arrow */
503 #if TARGET_API_MAC_CARBON
504 GetQDGlobalsArrow(&arrow
);
507 SetCursor(&qd
.arrow
);
509 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
511 #if TARGET_API_MAC_CARBON
513 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
514 DiffRgn(cursrgn
, visrgn
, cursrgn
);
517 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
521 if (mac_wininfo(window
)->adjustcursor
!= NULL
)
522 (*mac_wininfo(window
)->adjustcursor
)(window
, mouse
, cursrgn
);
524 #if TARGET_API_MAC_CARBON
525 GetQDGlobalsArrow(&arrow
);
527 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
529 SetCursor(&qd
.arrow
);
530 CopyRgn(window
->visRgn
, cursrgn
);
536 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
542 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
543 &type
, NULL
, 0, &size
) == noErr
)
544 return errAEParamMissed
;
550 void cleanup_exit(int status
)
553 #if !TARGET_RT_MAC_CFM
554 if (mac_gestalts
.encvvers
!= 0)
555 TerminateUnicodeConverter();
562 * c-file-style: "simon"