mac_support_cocoa.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*******************************
  2. Mac support for HID Test GUI
  3. Alan Ott
  4. Signal 11 Software
  5. *******************************/
  6. #include <fx.h>
  7. #import <Cocoa/Cocoa.h>
  8. #ifndef MAC_OS_X_VERSION_10_12
  9. #define MAC_OS_X_VERSION_10_12 101200
  10. #endif
  11. // macOS 10.12 deprecated NSAnyEventMask in favor of NSEventMaskAny
  12. #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12
  13. #define NSEventMaskAny NSAnyEventMask
  14. #endif
  15. extern FXMainWindow *g_main_window;
  16. @interface MyAppDelegate : NSObject<NSApplicationDelegate>
  17. {
  18. }
  19. @end
  20. @implementation MyAppDelegate
  21. - (void) applicationWillBecomeActive:(NSNotification*)notif
  22. {
  23. printf("WillBecomeActive\n");
  24. g_main_window->show();
  25. }
  26. - (void) applicationWillTerminate:(NSNotification*)notif
  27. {
  28. /* Doesn't get called. Not sure why */
  29. printf("WillTerminate\n");
  30. FXApp::instance()->exit();
  31. }
  32. - (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)sender
  33. {
  34. /* Doesn't get called. Not sure why */
  35. printf("ShouldTerminate\n");
  36. return YES;
  37. }
  38. - (void) applicationWillHide:(NSNotification*)notif
  39. {
  40. printf("WillHide\n");
  41. g_main_window->hide();
  42. }
  43. - (void) handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
  44. {
  45. printf("QuitEvent\n");
  46. FXApp::instance()->exit();
  47. }
  48. @end
  49. extern "C" {
  50. void
  51. init_apple_message_system()
  52. {
  53. static MyAppDelegate *d = [MyAppDelegate new];
  54. [[NSApplication sharedApplication] setDelegate:d];
  55. /* Register for Apple Events. */
  56. /* This is from
  57. http://stackoverflow.com/questions/1768497/application-exit-event */
  58. NSAppleEventManager *aem = [NSAppleEventManager sharedAppleEventManager];
  59. [aem setEventHandler:d
  60. andSelector:@selector(handleQuitEvent:withReplyEvent:)
  61. forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
  62. }
  63. void
  64. check_apple_events()
  65. {
  66. NSApplication *app = [NSApplication sharedApplication];
  67. NSAutoreleasePool *pool = [NSAutoreleasePool new];
  68. while (1) {
  69. NSEvent* event = [NSApp nextEventMatchingMask:NSEventMaskAny
  70. untilDate:nil
  71. inMode:NSDefaultRunLoopMode
  72. dequeue:YES];
  73. if (event == NULL)
  74. break;
  75. else {
  76. //printf("Event happened: Type: %d\n", event->_type);
  77. [app sendEvent: event];
  78. }
  79. }
  80. [pool release];
  81. }
  82. } /* extern "C" */