-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardMonitor.m
712 lines (574 loc) · 20.3 KB
/
KeyboardMonitor.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#import "ISSUtils.h"
#import <IOKit/hid/IOHIDLib.h>
#import <IOKit/IOMessage.h>
#import "InputSourceSwitch.h"
#define kIOHIDEventDriverClass "IOHIDEventDriver"
#define kFnModifierUsagePageKey "FnModifierUsagePage"
#define kFnModifierUsageKey "FnModifierUsage"
@class KeyboardMonitorApplication;
enum {
kHIDUsage_AppleVendorKeyboard_Function = 0x03
};
typedef void (*DeviceEventCallBack) (int eventCode, void *info);
KeyboardMonitorApplication *MonitorApp;
@interface DeviceState : NSObject
- (instancetype) initWithDeallocBlock: (void (^) (void)) deallocBlock deviceEventCallBack: (DeviceEventCallBack) deviceEventCallback andInfo: (void *) info;
- (void) sendDeviceEvent: (int) eventCode;
@end
@implementation DeviceState {
DeviceEventCallBack _deviceEventCallback;
void *_deviceEventCallbackInfo;
void (^_deallocBlock) (void);
}
- (instancetype) initWithDeallocBlock: (void (^) (void)) deallocBlock deviceEventCallBack: (DeviceEventCallBack) deviceEventCallback andInfo: (void *) info {
if (self = [self init]) {
_deallocBlock = deallocBlock;
_deviceEventCallback = deviceEventCallback;
_deviceEventCallbackInfo = info;
}
return self;
}
- (void) sendDeviceEvent: (int) eventCode {
if (_deviceEventCallback)
_deviceEventCallback (eventCode, _deviceEventCallbackInfo);
}
- (void) dealloc {
if (_deallocBlock)
_deallocBlock ();
}
@end
@interface DeviceStateRegistry : NSObject
- (instancetype) init NS_UNAVAILABLE;
- (instancetype) initWithDeviceEventCallBack: (DeviceEventCallBack) deviceEventCallback andInfo: (void *) info;
- (DeviceState *) getDeviceStateForTag: (void *) tag withDeviceStateBuilder: (DeviceState *(^) (void (^) (void), DeviceEventCallBack, void *)) deviceStateBuilder;
@end
@implementation DeviceStateRegistry {
CFMutableDictionaryRef _deviceStateMap;
DeviceEventCallBack _deviceEventCallback;
void *_deviceEventCallbackInfo;
}
- (instancetype) initWithDeviceEventCallBack: (DeviceEventCallBack) deviceEventCallback andInfo: (void *) info {
if (self = [super init]) {
_deviceStateMap = CFDictionaryCreateMutable (kCFAllocatorDefault, 0, NULL, NULL);
if (!_deviceStateMap)
return nil;
_deviceEventCallback = deviceEventCallback;
_deviceEventCallbackInfo = info;
}
return self;
}
- (DeviceState *) getDeviceStateForTag: (void *) tag withDeviceStateBuilder: (DeviceState *(^) (void (^) (void), DeviceEventCallBack, void *)) deviceStateBuilder {
if (!tag)
return deviceStateBuilder (nil, NULL, NULL);
DeviceState *state = (__bridge DeviceState *) CFDictionaryGetValue (_deviceStateMap, tag);
if (state)
return state;
state = deviceStateBuilder (
^ {
CFDictionaryRemoveValue (_deviceStateMap, tag);
},
_deviceEventCallback, _deviceEventCallbackInfo
);
if (!state)
return nil;
CFDictionarySetValue (_deviceStateMap, tag, (__bridge void *) state);
return state;
}
- (void) dealloc {
if (_deviceStateMap)
CFRelease (_deviceStateMap);
}
@end
@interface KeyboardDeviceState : DeviceState
- (instancetype) initWithDeallocBlock: (void (^) (void)) deallocBlock deviceEventCallBack: (DeviceEventCallBack) deviceEventCallback andInfo: (void *) info;
- (void) handleKey: (uint32_t) key status: (BOOL) pressed;
@end
@implementation KeyboardDeviceState {
CFMutableSetRef _pressedKeys;
uint8_t _switchState;
}
- (instancetype) init {
return [self localInit: [super init]];
}
- (instancetype) initWithDeallocBlock: (void (^) (void)) deallocBlock deviceEventCallBack: (DeviceEventCallBack) deviceEventCallback andInfo: (void *) info {
return [self localInit: [super initWithDeallocBlock: deallocBlock deviceEventCallBack: deviceEventCallback andInfo: info]];
}
- (instancetype) __attribute__ ((objc_method_family (init))) localInit: (KeyboardDeviceState *) instance {
if ((self = instance)) {
_pressedKeys = CFSetCreateMutable (kCFAllocatorDefault, 0, NULL);
if (!_pressedKeys)
return nil;
}
return self;
}
- (void) handleKey: (uint32_t) key status: (BOOL) pressed {
if (pressed)
CFSetAddValue (_pressedKeys, (uint8_t *) NULL + key);
else
CFSetRemoveValue (_pressedKeys, (uint8_t *) NULL + key);
switch (_switchState) {
case 0:
if (pressed && key == kHIDUsage_KeyboardLeftAlt && CFSetGetCount (_pressedKeys) == 1) {
// L-Alt pressed on its own
_switchState = 1;
return;
}
break;
case 1:
if (pressed && key == kHIDUsage_KeyboardLeftShift) {
// L-Shift pressed
_switchState = 2;
return;
}
break;
default:
if (pressed)
break;
switch (key) {
case kHIDUsage_KeyboardLeftShift:
// L-Shift released
[self sendDeviceEvent: 0];
_switchState = 1;
return;
case kHIDUsage_KeyboardLeftAlt:
// L-Alt released
[self sendDeviceEvent: 0];
}
}
_switchState = 0;
}
- (void) dealloc {
if (_pressedKeys)
CFRelease (_pressedKeys);
}
@end
@interface KeyboardDeviceHandler : NSObject
- (instancetype) init NS_UNAVAILABLE;
- (instancetype) initWithDeviceReference: (IOHIDDeviceRef) deviceRef andDeviceStateRegistry: registry;
@end
@implementation KeyboardDeviceHandler {
IOHIDDeviceRef _deviceReference;
BOOL _opened;
KeyboardDeviceState *_deviceState;
uint32_t _fnModifierKeyUsagePage;
uint32_t _fnModifierKeyUsage;
IONotificationPortRef _busyStateNotifyPort;
io_object_t _busyStateNotification;
}
- (instancetype) initWithDeviceReference: (IOHIDDeviceRef) deviceRef andDeviceStateRegistry: deviceStateRegistry {
if (self = [super init]) {
if (!(_deviceReference = deviceRef))
return nil;
CFRetain (_deviceReference);
_deviceState = (KeyboardDeviceState *) [deviceStateRegistry
getDeviceStateForTag: [self deviceLocationTag]
withDeviceStateBuilder: ^ (void (^deallocBlock) (void), DeviceEventCallBack deviceEventCallback, void *info) {
return [[KeyboardDeviceState alloc] initWithDeallocBlock: deallocBlock deviceEventCallBack: deviceEventCallback andInfo: info];
}
];
if (!_deviceState) {
NSLog (@"Failed to obtain device state for device: %@", IOHIDDeviceGetProperty (_deviceReference, CFSTR (kIOHIDProductKey)));
return nil;
}
[self initiateFnModifierKeyIdentification];
IOReturn rv = IOHIDDeviceOpen (_deviceReference, kIOHIDOptionsTypeNone);
if (rv != kIOReturnSuccess) {
NSLog (@"Failed to open device: %@: 0x%08x", IOHIDDeviceGetProperty (_deviceReference, CFSTR (kIOHIDProductKey)), rv);
return nil;
}
_opened = YES;
IOHIDDeviceRegisterInputValueCallback (_deviceReference, inputValueCallback, (__bridge void *) self);
NSLog (@"Added device: %@", IOHIDDeviceGetProperty (_deviceReference, CFSTR (kIOHIDProductKey)));
}
return self;
}
- (void) initiateFnModifierKeyIdentification {
_fnModifierKeyUsagePage = kHIDPage_Undefined;
io_service_t service = IOHIDDeviceGetService (_deviceReference);
if (service == MACH_PORT_NULL)
return;
_busyStateNotifyPort = IONotificationPortCreate (kIOMasterPortDefault);
if (!_busyStateNotifyPort)
return;
if (
IOServiceAddInterestNotification (
_busyStateNotifyPort,
service,
kIOBusyInterest,
busyStateChangeCallback,
(__bridge void *) self,
&_busyStateNotification
) != KERN_SUCCESS
) {
IONotificationPortDestroy (_busyStateNotifyPort);
_busyStateNotifyPort = NULL;
return;
}
uint32_t busyState;
if (
IOServiceGetBusyState (service, &busyState) != KERN_SUCCESS
||
(busyStateChangeCallback (
(__bridge void *) self,
service,
kIOMessageServiceBusyStateChange,
(uint8_t *) NULL + busyState
), _busyStateNotifyPort)
)
CFRunLoopAddSource (
CFRunLoopGetMain (),
IONotificationPortGetRunLoopSource (_busyStateNotifyPort),
kCFRunLoopDefaultMode
);
}
- (void) identifyFnModifierKey: (io_service_t) service {
io_iterator_t childrenIterator;
if (
IORegistryEntryCreateIterator (
service,
kIOServicePlane,
kIORegistryIterateRecursively,
&childrenIterator
) != KERN_SUCCESS
)
return;
for (io_registry_entry_t child; (child = IOIteratorNext (childrenIterator)); IOObjectRelease (child)) {
if (!IOObjectConformsTo (child, kIOHIDEventDriverClass))
continue;
NSMutableDictionary *childProperties;
if (
IORegistryEntryCreateCFProperties (
child,
(void *) &childProperties,
kCFAllocatorDefault,
kNilOptions
) != KERN_SUCCESS
)
continue;
NSNumber *fnUsagePage, *fnUsage;
if (
(fnUsagePage = ensureNumber ((__bridge CFTypeRef) childProperties[@kFnModifierUsagePageKey]))
&&
(fnUsage = ensureNumber ((__bridge CFTypeRef) childProperties[@kFnModifierUsageKey]))
) {
_fnModifierKeyUsagePage = fnUsagePage.unsignedIntValue;
_fnModifierKeyUsage = fnUsage.unsignedIntValue;
IOObjectRelease (child);
break;
}
}
IOObjectRelease (childrenIterator);
}
- (void *) deviceLocationTag {
NSNumber *locationId = ensureNumber (IOHIDDeviceGetProperty (_deviceReference, CFSTR (kIOHIDLocationIDKey)));
return (uint8_t *) NULL + locationId.unsignedIntegerValue;
}
- (void) handleInputValue: (IOHIDValueRef) valueRef {
IOHIDElementRef elem = IOHIDValueGetElement (valueRef);
uint32_t usagePage = IOHIDElementGetUsagePage (elem);
uint32_t usage;
switch (usagePage) {
case kHIDPage_KeyboardOrKeypad:
usage = IOHIDElementGetUsage (elem);
if (!(usage >= kHIDUsage_KeyboardA && usage <= kHIDUsage_KeyboardRightGUI))
return; // not a key event
break;
case kHIDPage_Consumer:
usage = IOHIDElementGetUsage (elem);
if (!(usage == kHIDUsage_Csmr_Eject))
return; // not the 'Eject' key event
usage |= 0x10000;
break;
default:
if (usagePage != _fnModifierKeyUsagePage || _fnModifierKeyUsagePage == kHIDPage_Undefined)
return; // not anything we care about
usage = IOHIDElementGetUsage (elem);
if (!(usage == _fnModifierKeyUsage))
return; // not the Apple 'Fn' modifier key event
usage = kHIDUsage_AppleVendorKeyboard_Function | 0x20000;
break;
}
CFIndex valueLength = IOHIDValueGetLength (valueRef);
if (valueLength == 0 || valueLength > 8)
return; // not an expected value
[_deviceState handleKey: usage status: IOHIDValueGetIntegerValue (valueRef) != 0];
}
- (void) dealloc {
if (_busyStateNotifyPort) {
IOObjectRelease (_busyStateNotification);
IONotificationPortDestroy (_busyStateNotifyPort); // this releases the associated CFRunLoopSource
}
if (_opened) {
IOHIDDeviceClose (_deviceReference, kIOHIDOptionsTypeNone);
NSLog (@"Removed device: %@", IOHIDDeviceGetProperty (_deviceReference, CFSTR (kIOHIDProductKey)));
} else if (!_deviceReference)
return;
CFRelease (_deviceReference);
}
static NSNumber *ensureNumber (CFTypeRef reference) {
return (reference && CFGetTypeID (reference) == CFNumberGetTypeID ())
? (__bridge NSNumber *) reference
: nil;
}
static void inputValueCallback (void *context, IOReturn result, void *sender, IOHIDValueRef valueRef) {
[(__bridge KeyboardDeviceHandler *) context handleInputValue: valueRef];
}
static void busyStateChangeCallback (void *context, io_service_t service, uint32_t messageType, void *messageArgument) {
if (messageType != kIOMessageServiceBusyStateChange)
return;
if ((uint32_t) (messageArgument - NULL))
return; // busyState > 0
KeyboardDeviceHandler *instance = (__bridge KeyboardDeviceHandler *) context;
IOObjectRelease (instance->_busyStateNotification);
IONotificationPortDestroy (instance->_busyStateNotifyPort); // this releases the associated CFRunLoopSource
instance->_busyStateNotifyPort = NULL;
[instance identifyFnModifierKey: service];
}
@end
@interface DeviceTracker : NSObject
- (instancetype) init NS_UNAVAILABLE;
- (instancetype) initWithDeviceEventCallBack: (DeviceEventCallBack) deviceEventCallback andInfo: (void *) info;
@end
@implementation DeviceTracker {
IOHIDManagerRef _hidManager;
CFMutableDictionaryRef _deviceHandlerMap;
DeviceStateRegistry *_deviceStateRegistry;
}
- (instancetype) initWithDeviceEventCallBack: (DeviceEventCallBack) deviceEventCallback andInfo: (void *) info {
if (self = [super init]) {
_deviceHandlerMap = CFDictionaryCreateMutable (kCFAllocatorDefault, 0, NULL, &kCFTypeDictionaryValueCallBacks);
if (!_deviceHandlerMap)
return nil;
_deviceStateRegistry = [[DeviceStateRegistry alloc] initWithDeviceEventCallBack: deviceEventCallback andInfo: info];
if (!_deviceStateRegistry)
return nil;
_hidManager = IOHIDManagerCreate (kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (CFGetTypeID (_hidManager) != IOHIDManagerGetTypeID ())
return nil;
NSDictionary *matchingDictionary = deviceMatchingDictionary ();
if (!matchingDictionary)
return nil;
IOHIDManagerSetDeviceMatching (_hidManager, (__bridge CFDictionaryRef) matchingDictionary);
IOHIDManagerRegisterDeviceMatchingCallback (_hidManager, deviceAddedCallback, (__bridge void *) self);
IOHIDManagerRegisterDeviceRemovalCallback (_hidManager, deviceRemovedCallback, (__bridge void *) self);
IOHIDManagerScheduleWithRunLoop (_hidManager, CFRunLoopGetMain (), kCFRunLoopDefaultMode);
}
return self;
}
- (void) dealloc {
if (_hidManager)
IOHIDManagerUnscheduleFromRunLoop (_hidManager, CFRunLoopGetMain (), kCFRunLoopDefaultMode);
if (_deviceHandlerMap)
CFRelease (_deviceHandlerMap);
if (_hidManager)
CFRelease (_hidManager);
}
static NSDictionary *usagePairMatchingDictionary (uint32_t usagePage, uint32_t usage) {
return @{
@kIOHIDDeviceUsagePageKey: @(usagePage),
@kIOHIDDeviceUsageKey: @(usage)
};
}
static NSDictionary *deviceMatchingDictionary (void) {
@try {
return @{
@kIOHIDDeviceUsagePairsKey: @[
// this dictionary will match keyboard devices
usagePairMatchingDictionary (kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard),
// this dictionary will match consumer control devices
usagePairMatchingDictionary (kHIDPage_Consumer, kHIDUsage_Csmr_ConsumerControl)
]
};
} @catch (NSException *e) {
if ([e.name isEqualToString: NSInvalidArgumentException])
return nil;
@throw;
}
}
static void deviceAddedCallback (void *context, IOReturn result, void *sender, IOHIDDeviceRef deviceRef) {
DeviceTracker *instance = (__bridge DeviceTracker *) context;
if (CFDictionaryContainsKey (instance->_deviceHandlerMap, deviceRef))
return;
KeyboardDeviceHandler *handler = [[KeyboardDeviceHandler alloc]
initWithDeviceReference: deviceRef
andDeviceStateRegistry: instance->_deviceStateRegistry
];
if (!handler)
return;
CFDictionarySetValue (instance->_deviceHandlerMap, deviceRef, (__bridge void *) handler);
}
static void deviceRemovedCallback (void *context, IOReturn result, void *sender, IOHIDDeviceRef deviceRef) {
DeviceTracker *instance = (__bridge DeviceTracker *) context;
if (!CFDictionaryContainsKey (instance->_deviceHandlerMap, deviceRef))
return;
CFDictionaryRemoveValue (instance->_deviceHandlerMap, deviceRef);
}
@end
@interface KeyboardMonitorApplication : NSObject
- (int) runWithClientPortHolder: (ISSUMachPortHolder *) clientPortHolder;
- (void) quitWithReturnValue: (int) returnValue;
@end
@implementation KeyboardMonitorApplication {
ISSUMachPort *_listenPort;
ISSUMachPort *_sendPort;
DeviceTracker *_tracker;
int _returnValue;
}
- (instancetype) init {
if (self = [super init]) {
if (!(_listenPort = [ISSUMachPort new])) {
NSLog (@"Failed to create listen mach port.");
return nil;
}
if (![_listenPort scheduleInRunLoop: CFRunLoopGetMain () forMode: kCFRunLoopDefaultMode]) {
NSLog (@"Failed to schedule listen mach port.");
return nil;
}
}
return self;
}
- (void) createTracker {
if (!_tracker)
if (!(_tracker = [[DeviceTracker alloc]
initWithDeviceEventCallBack: &deviceEventHandler
andInfo: (__bridge void *) self
])) {
NSLog (@"Failed to create device tracker.");
[self quitWithReturnValue: 3];
}
}
- (void) destroyTracker {
if (_tracker)
_tracker = nil;
}
- (int) runWithClientPortHolder: (ISSUMachPortHolder *) clientPortHolder {
_returnValue = 0;
@try {
_sendPort = [clientPortHolder get];
mach_port_t machPort = _listenPort.machPort;
if (!ISSUPortRightsSend (_sendPort, &machPort, 1)) {
NSLog (@"Failed to send mach port rights.");
return 2;
}
[_sendPort setInvalidationCallBack: &sendPortInvalidationHandler andInfo: (__bridge void *) self];
[_listenPort setMessageCallBack: &portExchangeHandler andInfo: (__bridge void *) self];
CFRunLoopRun ();
return _returnValue;
} @finally {
[_listenPort setMessageCallBack: NULL andInfo: NULL];
_sendPort = nil;
}
}
- (void) quitWithReturnValue: (int) returnValue {
CFRunLoopStop (CFRunLoopGetMain ());
_returnValue = returnValue;
}
static void portExchangeHandler (ISSUMachPort *port, mach_msg_header_t *msg, void *info) {
KeyboardMonitorApplication *instance = (__bridge KeyboardMonitorApplication *) info;
mach_port_t machPort[2];
switch (ISSUPortRightsReceive (msg, machPort, ISSUArrayLength (machPort))) {
default: // > 1
if (!ISSUResetBootstrapPort (machPort[1])) {
NSLog (@"Failed to reset bootstrap port.");
goto error_exit;
}
// fall through
case 1:
break;
case 0:
case -1:
NSLog (@"Failed to receive mach port rights.");
goto error_exit;
}
if (!(instance->_sendPort = [[ISSUMachPort alloc] initWithMachPort: machPort[0]])) {
NSLog (@"Failed to create send mach port.");
goto error_exit;
}
[instance->_sendPort setInvalidationCallBack: &sendPortInvalidationHandler andInfo: info];
[instance->_listenPort setMessageCallBack: &commandHandler andInfo: info];
return;
error_exit:
[instance quitWithReturnValue: 3];
}
static void commandHandler (ISSUMachPort *port, mach_msg_header_t *msg, void *info) {
KeyboardMonitorApplication *instance = (__bridge KeyboardMonitorApplication *) info;
int command;
if (!ISSUCommandReceive (msg, &command)) {
NSLog (@"Invalid message received.");
goto error_exit;
}
switch (command) {
case ISS_CMD_ACTIVATE_MONITOR:
[instance createTracker];
break;
case ISS_CMD_DEACTIVATE_MONITOR:
[instance destroyTracker];
break;
default:
NSLog (@"Invalid command received: %d", command);
goto error_exit;
}
return;
error_exit:
[instance quitWithReturnValue: 3];
}
static void sendPortInvalidationHandler (ISSUMachPort *port, void *info) {
KeyboardMonitorApplication *instance = (__bridge KeyboardMonitorApplication *) info;
// invalidation of the send port means the switcher has closed
// the port, which is likely because it has exited, so here we
// arrange for our clean exit too
[instance quitWithReturnValue: 0];
}
static void deviceEventHandler (int eventCode, void *info) {
KeyboardMonitorApplication *instance = (__bridge KeyboardMonitorApplication *) info;
if (!ISSUCommandSend (instance->_sendPort, ISS_CMD_PERFORM_SWITCH)) {
NSLog (@"Failed to send monitor event.");
[instance quitWithReturnValue: 3];
}
}
@end
static void handleSignalAsQuit (void *info) {
[MonitorApp quitWithReturnValue: 0];
}
static ISSUSignalHandlerTableEntry signalHandlerTable[] = {
{SIGTERM, &handleSignalAsQuit},
{SIGINT, &handleSignalAsQuit}
};
static ISSUMachPortHolder *createClientPort (void) {
NSString *name = ISSUGetPerProcessName (ISS_SERVER_PORT_NAME, getppid ());
if (!name)
return nil;
ISSUMachPort *port = [ISSUMachPort portForName: name];
if (!port)
return nil;
return [ISSUMachPortHolder holderWithPort: port];
}
int autoreleasedMain (int argc, char * const argv[]) {
NSArray *signalHandlerManagers;
ISSUMachPortHolder *clientPortHolder;
if (!(signalHandlerManagers = ISSUSetupSignalHandlers (signalHandlerTable, ISSUArrayLength (signalHandlerTable)))) {
NSLog (@"Failed to setup signal handlers.");
goto error_exit;
}
if (geteuid () != 0) {
NSURL *url = ISSUGetAbsoluteFileURL (argv[0]);
NSLog (
@"Keyboard monitor process is not running with superuser privileges, "
"it will not be able to monitor keyboard events when secure input is "
"enabled. Consider setting setuid root on its binary: %s",
url ? url.fileSystemRepresentation : argv[0]
);
}
if (!(clientPortHolder = createClientPort ())) {
NSLog (@"Failed to create client mach port.");
goto error_exit;
}
MonitorApp = [KeyboardMonitorApplication new];
if (!MonitorApp)
goto error_exit;
int rv = [MonitorApp runWithClientPortHolder: clientPortHolder];
MonitorApp = nil; // release the global instance
return rv;
error_exit:
return 2;
}