src/mac_support.cpp
changeset 12 7268128148b8
parent 0 b112defa99c0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/mac_support.cpp	Thu May 22 07:16:55 2014 +0200
     1.3 @@ -0,0 +1,134 @@
     1.4 +/*******************************
     1.5 + Mac support for HID Test GUI
     1.6 + 
     1.7 + Alan Ott
     1.8 + Signal 11 Software
     1.9 +
    1.10 + Some of this code is from Apple Documentation, most notably
    1.11 + http://developer.apple.com/legacy/mac/library/documentation/AppleScript/Conceptual/AppleEvents/AppleEvents.pdf 
    1.12 +*******************************/
    1.13 +
    1.14 +#include <Carbon/Carbon.h>
    1.15 +#include <fx.h>
    1.16 +
    1.17 +
    1.18 +extern FXMainWindow *g_main_window;
    1.19 +
    1.20 +static pascal OSErr HandleQuitMessage(const AppleEvent *theAppleEvent, AppleEvent 
    1.21 +									  *reply, long handlerRefcon) 
    1.22 +{
    1.23 +	puts("Quitting\n");
    1.24 +	FXApp::instance()->exit();
    1.25 +	return 0;
    1.26 +}
    1.27 +
    1.28 +static pascal OSErr HandleReopenMessage(const AppleEvent *theAppleEvent, AppleEvent 
    1.29 +									  *reply, long handlerRefcon) 
    1.30 +{
    1.31 +	puts("Showing");
    1.32 +	g_main_window->show();
    1.33 +	return 0;
    1.34 +}
    1.35 +
    1.36 +static pascal OSErr HandleWildCardMessage(const AppleEvent *theAppleEvent, AppleEvent 
    1.37 +									  *reply, long handlerRefcon) 
    1.38 +{
    1.39 +	puts("WildCard\n");
    1.40 +	return 0;
    1.41 +}
    1.42 +
    1.43 +OSStatus AEHandler(EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon) 
    1.44 +{ 
    1.45 +    Boolean     release = false; 
    1.46 +    EventRecord eventRecord; 
    1.47 +    OSErr       ignoreErrForThisSample; 
    1.48 +	
    1.49 +    // Events of type kEventAppleEvent must be removed from the queue 
    1.50 +    //  before being passed to AEProcessAppleEvent. 
    1.51 +    if (IsEventInQueue(GetMainEventQueue(), inEvent)) 
    1.52 +    { 
    1.53 +        // RemoveEventFromQueue will release the event, which will 
    1.54 +        //  destroy it if we don't retain it first. 
    1.55 +        RetainEvent(inEvent); 
    1.56 +        release = true; 
    1.57 +        RemoveEventFromQueue(GetMainEventQueue(), inEvent); 
    1.58 +    } 
    1.59 +    // Convert the event ref to the type AEProcessAppleEvent expects. 
    1.60 +    ConvertEventRefToEventRecord(inEvent, &eventRecord); 
    1.61 +    ignoreErrForThisSample = AEProcessAppleEvent(&eventRecord); 
    1.62 +    if (release) 
    1.63 +        ReleaseEvent(inEvent); 
    1.64 +    // This Carbon event has been handled, even if no AppleEvent handlers 
    1.65 +    //  were installed for the Apple event. 
    1.66 +    return noErr; 
    1.67 +}
    1.68 +
    1.69 +static void HandleEvent(EventRecord *event) 
    1.70 +{ 
    1.71 +	//printf("What: %d message %x\n", event->what, event->message);
    1.72 +	if (event->what == osEvt) {
    1.73 +		if (((event->message >> 24) & 0xff) == suspendResumeMessage) {
    1.74 +			if (event->message & resumeFlag) {
    1.75 +				g_main_window->show();				
    1.76 +			}
    1.77 +		}
    1.78 +	}
    1.79 +
    1.80 +#if 0
    1.81 +    switch (event->what) 
    1.82 +    { 
    1.83 +        case mouseDown: 
    1.84 +            //HandleMouseDown(event); 
    1.85 +            break; 
    1.86 +        case keyDown: 
    1.87 +        case autoKey: 
    1.88 +            //HandleKeyPress(event); 
    1.89 +            break; 
    1.90 +        case kHighLevelEvent: 
    1.91 +			puts("Calling ProcessAppleEvent\n");
    1.92 +            AEProcessAppleEvent(event); 
    1.93 +            break; 
    1.94 +    } 
    1.95 +#endif
    1.96 +} 
    1.97 +
    1.98 +void
    1.99 +init_apple_message_system()
   1.100 +{
   1.101 +	OSErr err;
   1.102 +	static const EventTypeSpec appleEvents[] = 
   1.103 +	{
   1.104 +		{ kEventClassAppleEvent, kEventAppleEvent }
   1.105 +	};
   1.106 +	
   1.107 +	/* Install the handler for Apple Events */
   1.108 +	InstallApplicationEventHandler(NewEventHandlerUPP(AEHandler), 
   1.109 +	              GetEventTypeCount(appleEvents), appleEvents, 0, NULL); 
   1.110 +
   1.111 +	/* Install handlers for the individual Apple Events that come
   1.112 +	   from the Dock icon: the Reopen (click), and the Quit messages. */
   1.113 +	err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, 
   1.114 +	              NewAEEventHandlerUPP(HandleQuitMessage), 0, false);
   1.115 +	err = AEInstallEventHandler(kCoreEventClass, kAEReopenApplication, 
   1.116 +	              NewAEEventHandlerUPP(HandleReopenMessage), 0, false);
   1.117 +#if 0
   1.118 +	// Left as an example of a wild card match.
   1.119 +	err = AEInstallEventHandler(kCoreEventClass, typeWildCard, 
   1.120 +	              NewAEEventHandlerUPP(HandleWildMessage), 0, false);
   1.121 +#endif
   1.122 +}
   1.123 +
   1.124 +void
   1.125 +check_apple_events()
   1.126 +{
   1.127 +	RgnHandle       cursorRgn = NULL; 
   1.128 +	Boolean         gotEvent=TRUE; 
   1.129 +	EventRecord     event; 
   1.130 +
   1.131 +	while (gotEvent) { 
   1.132 +		gotEvent = WaitNextEvent(everyEvent, &event, 0L/*timeout*/, cursorRgn); 
   1.133 +		if (gotEvent) { 
   1.134 +			HandleEvent(&event); 
   1.135 +		} 
   1.136 +	}
   1.137 +}