src/mac_support.cpp
author sl
Thu, 22 May 2014 16:46:50 +0200
changeset 22 efa6ff02287c
parent 0 b112defa99c0
permissions -rw-r--r--
Sorting out and testing our display position command.
To avoid refresh artefact we will indeed need to use this feature.
It will go like that:
* Setup off screen buffer
* Swap frame buffer
sl@0
     1
/*******************************
sl@0
     2
 Mac support for HID Test GUI
sl@0
     3
 
sl@0
     4
 Alan Ott
sl@0
     5
 Signal 11 Software
sl@0
     6
sl@0
     7
 Some of this code is from Apple Documentation, most notably
sl@0
     8
 http://developer.apple.com/legacy/mac/library/documentation/AppleScript/Conceptual/AppleEvents/AppleEvents.pdf 
sl@0
     9
*******************************/
sl@0
    10
sl@0
    11
#include <Carbon/Carbon.h>
sl@0
    12
#include <fx.h>
sl@0
    13
sl@0
    14
sl@0
    15
extern FXMainWindow *g_main_window;
sl@0
    16
sl@0
    17
static pascal OSErr HandleQuitMessage(const AppleEvent *theAppleEvent, AppleEvent 
sl@0
    18
									  *reply, long handlerRefcon) 
sl@0
    19
{
sl@0
    20
	puts("Quitting\n");
sl@0
    21
	FXApp::instance()->exit();
sl@0
    22
	return 0;
sl@0
    23
}
sl@0
    24
sl@0
    25
static pascal OSErr HandleReopenMessage(const AppleEvent *theAppleEvent, AppleEvent 
sl@0
    26
									  *reply, long handlerRefcon) 
sl@0
    27
{
sl@0
    28
	puts("Showing");
sl@0
    29
	g_main_window->show();
sl@0
    30
	return 0;
sl@0
    31
}
sl@0
    32
sl@0
    33
static pascal OSErr HandleWildCardMessage(const AppleEvent *theAppleEvent, AppleEvent 
sl@0
    34
									  *reply, long handlerRefcon) 
sl@0
    35
{
sl@0
    36
	puts("WildCard\n");
sl@0
    37
	return 0;
sl@0
    38
}
sl@0
    39
sl@0
    40
OSStatus AEHandler(EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon) 
sl@0
    41
{ 
sl@0
    42
    Boolean     release = false; 
sl@0
    43
    EventRecord eventRecord; 
sl@0
    44
    OSErr       ignoreErrForThisSample; 
sl@0
    45
	
sl@0
    46
    // Events of type kEventAppleEvent must be removed from the queue 
sl@0
    47
    //  before being passed to AEProcessAppleEvent. 
sl@0
    48
    if (IsEventInQueue(GetMainEventQueue(), inEvent)) 
sl@0
    49
    { 
sl@0
    50
        // RemoveEventFromQueue will release the event, which will 
sl@0
    51
        //  destroy it if we don't retain it first. 
sl@0
    52
        RetainEvent(inEvent); 
sl@0
    53
        release = true; 
sl@0
    54
        RemoveEventFromQueue(GetMainEventQueue(), inEvent); 
sl@0
    55
    } 
sl@0
    56
    // Convert the event ref to the type AEProcessAppleEvent expects. 
sl@0
    57
    ConvertEventRefToEventRecord(inEvent, &eventRecord); 
sl@0
    58
    ignoreErrForThisSample = AEProcessAppleEvent(&eventRecord); 
sl@0
    59
    if (release) 
sl@0
    60
        ReleaseEvent(inEvent); 
sl@0
    61
    // This Carbon event has been handled, even if no AppleEvent handlers 
sl@0
    62
    //  were installed for the Apple event. 
sl@0
    63
    return noErr; 
sl@0
    64
}
sl@0
    65
sl@0
    66
static void HandleEvent(EventRecord *event) 
sl@0
    67
{ 
sl@0
    68
	//printf("What: %d message %x\n", event->what, event->message);
sl@0
    69
	if (event->what == osEvt) {
sl@0
    70
		if (((event->message >> 24) & 0xff) == suspendResumeMessage) {
sl@0
    71
			if (event->message & resumeFlag) {
sl@0
    72
				g_main_window->show();				
sl@0
    73
			}
sl@0
    74
		}
sl@0
    75
	}
sl@0
    76
sl@0
    77
#if 0
sl@0
    78
    switch (event->what) 
sl@0
    79
    { 
sl@0
    80
        case mouseDown: 
sl@0
    81
            //HandleMouseDown(event); 
sl@0
    82
            break; 
sl@0
    83
        case keyDown: 
sl@0
    84
        case autoKey: 
sl@0
    85
            //HandleKeyPress(event); 
sl@0
    86
            break; 
sl@0
    87
        case kHighLevelEvent: 
sl@0
    88
			puts("Calling ProcessAppleEvent\n");
sl@0
    89
            AEProcessAppleEvent(event); 
sl@0
    90
            break; 
sl@0
    91
    } 
sl@0
    92
#endif
sl@0
    93
} 
sl@0
    94
sl@0
    95
void
sl@0
    96
init_apple_message_system()
sl@0
    97
{
sl@0
    98
	OSErr err;
sl@0
    99
	static const EventTypeSpec appleEvents[] = 
sl@0
   100
	{
sl@0
   101
		{ kEventClassAppleEvent, kEventAppleEvent }
sl@0
   102
	};
sl@0
   103
	
sl@0
   104
	/* Install the handler for Apple Events */
sl@0
   105
	InstallApplicationEventHandler(NewEventHandlerUPP(AEHandler), 
sl@0
   106
	              GetEventTypeCount(appleEvents), appleEvents, 0, NULL); 
sl@0
   107
sl@0
   108
	/* Install handlers for the individual Apple Events that come
sl@0
   109
	   from the Dock icon: the Reopen (click), and the Quit messages. */
sl@0
   110
	err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, 
sl@0
   111
	              NewAEEventHandlerUPP(HandleQuitMessage), 0, false);
sl@0
   112
	err = AEInstallEventHandler(kCoreEventClass, kAEReopenApplication, 
sl@0
   113
	              NewAEEventHandlerUPP(HandleReopenMessage), 0, false);
sl@0
   114
#if 0
sl@0
   115
	// Left as an example of a wild card match.
sl@0
   116
	err = AEInstallEventHandler(kCoreEventClass, typeWildCard, 
sl@0
   117
	              NewAEEventHandlerUPP(HandleWildMessage), 0, false);
sl@0
   118
#endif
sl@0
   119
}
sl@0
   120
sl@0
   121
void
sl@0
   122
check_apple_events()
sl@0
   123
{
sl@0
   124
	RgnHandle       cursorRgn = NULL; 
sl@0
   125
	Boolean         gotEvent=TRUE; 
sl@0
   126
	EventRecord     event; 
sl@0
   127
sl@0
   128
	while (gotEvent) { 
sl@0
   129
		gotEvent = WaitNextEvent(everyEvent, &event, 0L/*timeout*/, cursorRgn); 
sl@0
   130
		if (gotEvent) { 
sl@0
   131
			HandleEvent(&event); 
sl@0
   132
		} 
sl@0
   133
	}
sl@0
   134
}