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