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_suspendresume(EventRecord
*);
75 static void mac_updatewindow(WindowPtr
);
76 static void mac_keypress(EventRecord
*);
77 static int mac_windowtype(WindowPtr
);
78 static void mac_menucommand(long);
79 static void mac_adjustcursor(RgnHandle
);
80 static void mac_adjustmenus(void);
81 static void mac_closewindow(WindowPtr
);
82 static void mac_zoomwindow(WindowPtr
, short);
83 #pragma noreturn (cleanup_exit)
90 struct mac_windows windows
;
92 struct mac_gestalts mac_gestalts
;
94 int main (int argc
, char **argv
) {
100 #pragma noreturn (main)
102 static void mac_startup(void) {
106 #if !TARGET_API_MAC_CARBON
107 /* Init Memory Manager */
110 InitGraf(&qd
.thePort
);
111 /* Init Font Manager */
113 /* Init Window Manager */
115 /* Init Menu Manager */
119 /* Init Dialog Manager */
123 /* Get base system version (only used if there's no better selector) */
124 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
125 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
126 fatalbox("PuTTYgen requires System 7 or newer");
127 /* Find out if we've got Color Quickdraw */
128 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
129 mac_gestalts
.qdvers
= gestaltOriginalQD
;
130 /* ... and the Appearance Manager? */
131 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
132 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
133 mac_gestalts
.apprvers
= 0x0100;
135 mac_gestalts
.apprvers
= 0;
136 #if TARGET_RT_MAC_CFM
137 /* Paranoia: Did we manage to pull in AppearanceLib? */
138 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
139 mac_gestalts
.apprvers
= 0;
142 mac_gestalts
.cntlattr
= 0;
143 mac_gestalts
.windattr
= 0;
145 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
146 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
147 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
148 mac_gestalts
.cntlattr
= 0;
149 /* Mac OS 8.5 Window Manager? */
150 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
151 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
152 mac_gestalts
.windattr
= 0;
154 /* Text Encoding Conversion Manager? */
156 #if TARGET_RT_MAC_CFM
157 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
159 InitializeUnicodeConverter(NULL
) != noErr
||
161 TECGetInfo(&ti
) != noErr
)
162 mac_gestalts
.encvvers
= 0;
164 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
165 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
166 DisposeHandle((Handle
)ti
);
168 /* Navigation Services? */
169 if (NavServicesAvailable())
170 mac_gestalts
.navsvers
= NavLibraryVersion();
172 mac_gestalts
.navsvers
= 0;
174 /* We've been tested with the Appearance Manager */
175 if (mac_gestalts
.apprvers
!= 0)
176 RegisterAppearanceClient();
178 menuBar
= GetNewMBar(128);
180 fatalbox("Unable to create menu bar.");
182 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
186 windows
.about
= NULL
;
187 windows
.licence
= NULL
;
189 flags
= FLAG_INTERACTIVE
;
191 /* Install Apple Event handlers. */
193 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
194 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
195 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
196 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
197 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
198 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
200 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
201 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
204 static void mac_eventloop(void) {
208 Point mousenow
, mousethen
;
213 GetMouse(&mousethen
);
215 mac_adjustcursor(cursrgn
);
216 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
217 mac_adjustcursor(cursrgn
);
218 front
= mac_frontwindow();
220 ks
= mac_windowkey(front
);
221 if (ks
->collecting_entropy
) {
223 if (mousenow
.h
!= mousethen
.h
|| mousenow
.v
!= mousethen
.v
) {
224 ks
->entropy
[ks
->entropy_got
++] = *(unsigned *)&mousenow
;
225 ks
->entropy
[ks
->entropy_got
++] = TickCount();
226 if (ks
->entropy_got
>= ks
->entropy_required
)
227 ks
->collecting_entropy
= 0;
228 SetControlValue(ks
->progress
, ks
->entropy_got
);
229 mousethen
= mousenow
;
231 SetEmptyRgn(cursrgn
);
243 static void mac_event(EventRecord
*event
) {
247 switch (event
->what
) {
249 part
= FindWindow(event
->where
, &window
);
253 mac_menucommand(MenuSelect(event
->where
));
255 #if !TARGET_API_MAC_CARBON
257 SystemClick(event
, window
);
261 if (window
!= FrontWindow())
262 /* XXX: check for movable modal dboxes? */
263 SelectWindow(window
);
265 mac_contentclick(window
, event
);
268 if (TrackGoAway(window
, event
->where
))
269 mac_closewindow(window
);
272 /* XXX: moveable modal check? */
273 #if TARGET_API_MAC_CARBON
277 GetQDGlobalsScreenBits(&screenBits
);
278 DragWindow(window
, event
->where
, &screenBits
.bounds
);
281 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
285 mac_growwindow(window
, event
);
289 if (TrackBox(window
, event
->where
, part
))
290 mac_zoomwindow(window
, part
);
299 mac_activatewindow((WindowPtr
)event
->message
, event
);
302 mac_updatewindow((WindowPtr
)event
->message
);
304 #if !TARGET_API_MAC_CARBON
306 if (HiWord(event
->message
) != noErr
) {
309 SetPt(&pt
, 120, 120);
310 DIBadMount(pt
, event
->message
);
315 switch ((event
->message
& osEvtMessageMask
) >> 24) {
316 case suspendResumeMessage
:
317 mac_suspendresume(event
);
321 case kHighLevelEvent
:
322 AEProcessAppleEvent(event
); /* errors? */
327 static void mac_contentclick(WindowPtr window
, EventRecord
*event
)
330 if (mac_wininfo(window
)->click
!= NULL
)
331 (*mac_wininfo(window
)->click
)(window
, event
);
334 static void mac_growwindow(WindowPtr window
, EventRecord
*event
)
337 if (mac_wininfo(window
)->grow
!= NULL
)
338 (*mac_wininfo(window
)->grow
)(window
, event
);
341 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
)
345 if (mac_wininfo(window
)->activate
!= NULL
)
346 (*mac_wininfo(window
)->activate
)(window
, event
);
349 static void mac_updatewindow(WindowPtr window
)
352 if (mac_wininfo(window
)->update
!= NULL
)
353 (*mac_wininfo(window
)->update
)(window
);
357 * Work out what kind of window we're dealing with.
359 static int mac_windowtype(WindowPtr window
)
362 #if !TARGET_API_MAC_CARBON
363 if (GetWindowKind(window
) < 0)
366 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
372 static void mac_keypress(EventRecord
*event
)
376 window
= FrontWindow();
377 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
)) {
379 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
381 if (mac_wininfo(window
)->key
!= NULL
)
382 (*mac_wininfo(window
)->key
)(window
, event
);
386 static void mac_menucommand(long result
)
390 #if !TARGET_API_MAC_CARBON
394 menu
= HiWord(result
);
395 item
= LoWord(result
);
396 window
= FrontWindow();
397 /* Things which do the same whatever window we're in. */
404 #if !TARGET_API_MAC_CARBON
406 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
418 mac_closewindow(window
);
426 /* If we get here, handling is up to window-specific code. */
427 if (mac_wininfo(window
)->menu
!= NULL
)
428 (*mac_wininfo(window
)->menu
)(window
, menu
, item
);
434 static void mac_closewindow(WindowPtr window
)
437 switch (mac_windowtype(window
)) {
438 #if !TARGET_API_MAC_CARBON
440 CloseDeskAcc(GetWindowKind(window
));
444 if (mac_wininfo(window
)->close
!= NULL
)
445 (*mac_wininfo(window
)->close
)(window
);
449 static void mac_suspendresume(EventRecord
*event
)
452 EventRecord fakeevent
;
455 * We're called either before we're suspended or after we're
456 * resumed, so we're the front application at this point.
458 front
= FrontWindow();
460 fakeevent
.what
= activateEvt
;
461 fakeevent
.message
= (UInt32
)front
;
462 fakeevent
.when
= event
->when
;
463 fakeevent
.where
= event
->where
;
464 fakeevent
.modifiers
=
465 (event
->message
& resumeFlag
) ? activeFlag
: 0;
466 mac_activatewindow(front
, &fakeevent
);
470 static void mac_zoomwindow(WindowPtr window
, short part
) {
472 /* FIXME: do something */
476 * Make the menus look right before the user gets to see them.
478 #if TARGET_API_MAC_CARBON
479 #define EnableItem EnableMenuItem
480 #define DisableItem DisableMenuItem
482 static void mac_adjustmenus(void) {
486 window
= FrontWindow();
487 menu
= GetMenuHandle(mApple
);
489 EnableItem(menu
, iAbout
);
491 menu
= GetMenuHandle(mFile
);
493 EnableItem(menu
, iNew
);
495 EnableItem(menu
, iClose
);
497 DisableItem(menu
, iClose
);
498 EnableItem(menu
, iQuit
);
500 if (mac_wininfo(window
)->adjustmenus
!= NULL
)
501 (*mac_wininfo(window
)->adjustmenus
)(window
);
503 DisableItem(menu
, iSave
);
504 DisableItem(menu
, iSaveAs
);
505 menu
= GetMenuHandle(mEdit
);
506 DisableItem(menu
, 0);
507 menu
= GetMenuHandle(mWindow
);
508 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
514 * Make sure the right cursor's being displayed.
516 static void mac_adjustcursor(RgnHandle cursrgn
)
519 WindowPtr window
, front
;
521 #if TARGET_API_MAC_CARBON
527 LocalToGlobal(&mouse
);
528 part
= FindWindow(mouse
, &window
);
529 front
= FrontWindow();
530 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
531 /* Cursor isn't in the front window, so switch to arrow */
532 #if TARGET_API_MAC_CARBON
533 GetQDGlobalsArrow(&arrow
);
536 SetCursor(&qd
.arrow
);
538 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
540 #if TARGET_API_MAC_CARBON
542 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
543 DiffRgn(cursrgn
, visrgn
, cursrgn
);
546 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
550 if (mac_wininfo(window
)->adjustcursor
!= NULL
)
551 (*mac_wininfo(window
)->adjustcursor
)(window
, mouse
, cursrgn
);
553 #if TARGET_API_MAC_CARBON
554 GetQDGlobalsArrow(&arrow
);
556 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
558 SetCursor(&qd
.arrow
);
559 CopyRgn(window
->visRgn
, cursrgn
);
565 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
571 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
572 &type
, NULL
, 0, &size
) == noErr
)
573 return errAEParamMissed
;
579 void cleanup_exit(int status
)
582 #if !TARGET_RT_MAC_CFM
583 if (mac_gestalts
.encvvers
!= 0)
584 TerminateUnicodeConverter();
591 * c-file-style: "simon"