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