mac_support.cpp 3.7 KB

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