-
Notifications
You must be signed in to change notification settings - Fork 3
/
iremoted.c
505 lines (428 loc) · 16.9 KB
/
iremoted.c
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
/*
* iremoted.c
* Display events received from the Apple Infrared Remote.
*
* gcc -Wall -o iremoted iremoted.c -framework IOKit -framework Carbon
*
* Copyright (c) 2006-2008 Amit Singh. All Rights Reserved.
*
* Keyboard arrow event extension (c) 2013 by Manuel Peuster <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define PROGNAME "iremoted"
#define PROGVERS "2.0"
#include <stdio.h>
#include <getopt.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/errno.h>
#include <sysexits.h>
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <IOKit/hid/IOHIDUsageTables.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h>
static struct option
long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "keynote", no_argument, 0, 'k' },
{ "arrows", no_argument, 0, 'a' },
{ 0, 0, 0, 0 },
};
static const char *options = "hka";
IOHIDElementCookie buttonNextID = 0;
IOHIDElementCookie buttonPreviousID = 0;
IOHIDElementCookie buttonUpID = 0;
IOHIDElementCookie buttonDownID = 0;
typedef struct cookie_struct
{
IOHIDElementCookie gButtonCookie_SystemAppMenu;
IOHIDElementCookie gButtonCookie_SystemMenuSelect;
IOHIDElementCookie gButtonCookie_SystemMenuRight;
IOHIDElementCookie gButtonCookie_SystemMenuLeft;
IOHIDElementCookie gButtonCookie_SystemMenuUp;
IOHIDElementCookie gButtonCookie_SystemMenuDown;
} *cookie_struct_t;
enum {
keynoteEventClass = 'Kntc',
slideForward = 'steF',
slideBackward = 'steB',
};
static const char *keynoteID = "com.apple.iWork.Keynote";
static int driveKeynote = 0;
static int driveKeyboardArrows = 0;
void usage(void);
OSStatus KeynoteChangeSlide(AEEventID eventID);
void print_errmsg_if_io_err(int expr, char *msg);
void print_errmsg_if_err(int expr, char *msg);
void QueueCallbackFunction(void *target, IOReturn result,
void *refcon, void *sender);
bool addQueueCallbacks(IOHIDQueueInterface **hqi);
void processQueue(IOHIDDeviceInterface **hidDeviceInterface,
cookie_struct_t cookies);
void doRun(IOHIDDeviceInterface **hidDeviceInterface,
cookie_struct_t cookies);
cookie_struct_t getHIDCookies(IOHIDDeviceInterface122 **handle);
void createHIDDeviceInterface(io_object_t hidDevice,
IOHIDDeviceInterface ***hdi);
void setupAndRun(void);
void
usage(void)
{
printf("%s (version %s)\n", PROGNAME, PROGVERS);
printf("Copyright (c) 2006-2008 Amit Singh. All Rights Reserved.\n");
printf("Displays events received from the Apple Infrared Remote.\n\n");
printf("Keyboard arrow event extension (c) 2013 by Manuel Peuster <[email protected]>.\n\n");
printf("Usage: %s [OPTIONS...]\n\nOptions:\n", PROGNAME);
printf(" -h, --help print this help message and exit\n");
printf(" -k, --keynote use forward/backward button presses for Keynote slide transition\n\n");
printf(" -a, --arrows use forward/backward/up/down button presses to generate the corresponding \n\t\tkeyboard arrow events (e.g. for Preview.app slide transition)\n\n");
printf("Please report bugs using the following contact information:\n"
"<URL:http://www.osxbook.com/software/bugs/>\n");
}
OSStatus
KeynoteChangeSlide(AEEventID eventID)
{
OSStatus err = noErr;
AppleEvent eventToSend = { typeNull, nil };
AppleEvent eventReply = { typeNull, nil };
AEBuildError eventBuildError;
err = AEBuildAppleEvent(
keynoteEventClass, // Event class for the resulting event
eventID, // Event ID for the resulting event
typeApplicationBundleID,
keynoteID,
strlen(keynoteID),
kAutoGenerateReturnID, // Return ID for the created event
kAnyTransactionID, // Transaction ID for this event
&eventToSend, // Pointer to location for storing result
&eventBuildError, // Pointer to error structure
"", // AEBuild format string describing the
NULL // AppleEvent record to be created
);
if (err != noErr) {
fprintf(stderr, "Failed to build Apple event (error %d).\n", (int)err);
return err;
}
err = AESend(&eventToSend,
&eventReply,
kAEWaitReply, // send mode (wait for reply)
kAENormalPriority,
kNoTimeOut,
nil, // no pointer to idle function
nil); // no pointer to filter function
if (err != noErr)
fprintf(stderr, "Failed to send Apple event (error %d).\n", (int)err);
// Dispose of the send/reply descs
AEDisposeDesc(&eventToSend);
AEDisposeDesc(&eventReply);
return err;
}
void
print_errmsg_if_io_err(int expr, char *msg)
{
IOReturn err = (expr);
if (err != kIOReturnSuccess) {
fprintf(stderr, "*** %s - %s(%x, %d).\n", msg, mach_error_string(err),
err, err & 0xffffff);
fflush(stderr);
exit(EX_OSERR);
}
}
void
print_errmsg_if_err(int expr, char *msg)
{
if (expr) {
fprintf(stderr, "*** %s.\n", msg);
fflush(stderr);
exit(EX_OSERR);
}
}
void
QueueCallbackFunction(void *target, IOReturn result, void *refcon, void *sender)
{
HRESULT ret = 0;
AbsoluteTime zeroTime = {0,0};
IOHIDQueueInterface **hqi;
IOHIDEventStruct event;
while (!ret) {
hqi = (IOHIDQueueInterface **)sender;
ret = (*hqi)->getNextEvent(hqi, &event, zeroTime, 0);
if (!ret) {
printf("%#x %s\n", (unsigned int)event.elementCookie,
(event.value == 0) ? "depressed" : "pressed");
fflush(stdout);
if(event.value && driveKeyboardArrows) {
// select correct CGKeyCode
CGKeyCode keycode = 0;
if (event.elementCookie == buttonNextID)
keycode = (CGKeyCode)124; // right
else if (event.elementCookie == buttonPreviousID)
keycode = (CGKeyCode)123; // left
else if (event.elementCookie == buttonUpID)
keycode = (CGKeyCode)126; // up
else if (event.elementCookie == buttonDownID)
keycode = (CGKeyCode)125; // down
if (keycode) {
printf("Sending keystroke with CGKeyCode: %hu\n", keycode);
// define events
CGEventRef keyDown = CGEventCreateKeyboardEvent(NULL, keycode, true);
CGEventRef keyUp = CGEventCreateKeyboardEvent(NULL, keycode, false);
// send key down and up events
CGEventPost(kCGAnnotatedSessionEventTap, keyDown);
CGEventPost(kCGAnnotatedSessionEventTap, keyUp);
// release resources
CFRelease(keyUp);
CFRelease(keyDown);
}
}
if (event.value && driveKeynote) {
if (event.elementCookie == buttonNextID)
KeynoteChangeSlide(slideForward);
else if (event.elementCookie == buttonPreviousID)
KeynoteChangeSlide(slideBackward);
}
}
}
}
bool
addQueueCallbacks(IOHIDQueueInterface **hqi)
{
IOReturn ret;
CFRunLoopSourceRef eventSource;
IOHIDQueueInterface ***privateData;
privateData = malloc(sizeof(*privateData));
*privateData = hqi;
ret = (*hqi)->createAsyncEventSource(hqi, &eventSource);
if (ret != kIOReturnSuccess)
return false;
ret = (*hqi)->setEventCallout(hqi, QueueCallbackFunction,
NULL, &privateData);
if (ret != kIOReturnSuccess)
return false;
CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource,
kCFRunLoopDefaultMode);
return true;
}
void
processQueue(IOHIDDeviceInterface **hidDeviceInterface, cookie_struct_t cookies)
{
HRESULT result;
IOHIDQueueInterface **queue;
queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
if (!queue) {
fprintf(stderr, "Failed to allocate event queue.\n");
return;
}
(void)(*queue)->create(queue, 0, 8);
(void)(*queue)->addElement(queue,
cookies->gButtonCookie_SystemAppMenu, 0);
(void)(*queue)->addElement(queue,
cookies->gButtonCookie_SystemMenuSelect, 0);
(void)(*queue)->addElement(queue,
cookies->gButtonCookie_SystemMenuRight, 0);
(void)(*queue)->addElement(queue,
cookies->gButtonCookie_SystemMenuLeft, 0);
(void)(*queue)->addElement(queue,
cookies->gButtonCookie_SystemMenuUp, 0);
(void)(*queue)->addElement(queue,
cookies->gButtonCookie_SystemMenuDown, 0);
addQueueCallbacks(queue);
result = (*queue)->start(queue);
CFRunLoopRun();
result = (*queue)->stop(queue);
result = (*queue)->dispose(queue);
(*queue)->Release(queue);
}
void
doRun(IOHIDDeviceInterface **hidDeviceInterface, cookie_struct_t cookies)
{
IOReturn ioReturnValue;
ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, 0);
processQueue(hidDeviceInterface, cookies);
if (ioReturnValue == KERN_SUCCESS)
ioReturnValue = (*hidDeviceInterface)->close(hidDeviceInterface);
(*hidDeviceInterface)->Release(hidDeviceInterface);
}
cookie_struct_t
getHIDCookies(IOHIDDeviceInterface122 **handle)
{
cookie_struct_t cookies;
IOHIDElementCookie cookie;
CFTypeRef object;
long number;
long usage;
long usagePage;
CFArrayRef elements;
CFDictionaryRef element;
IOReturn result;
if ((cookies = (cookie_struct_t)malloc(sizeof(*cookies))) == NULL) {
fprintf(stderr, "Failed to allocate cookie memory.\n");
exit(1);
}
memset(cookies, 0, sizeof(*cookies));
if (!handle || !(*handle))
return cookies;
result = (*handle)->copyMatchingElements(handle, NULL, &elements);
if (result != kIOReturnSuccess) {
fprintf(stderr, "Failed to copy cookies.\n");
exit(1);
}
CFIndex i;
for (i = 0; i < CFArrayGetCount(elements); i++) {
element = CFArrayGetValueAtIndex(elements, i);
object = (CFDictionaryGetValue(element, CFSTR(kIOHIDElementCookieKey)));
if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID())
continue;
if(!CFNumberGetValue((CFNumberRef) object, kCFNumberLongType, &number))
continue;
cookie = (IOHIDElementCookie)number;
object = CFDictionaryGetValue(element, CFSTR(kIOHIDElementUsageKey));
if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID())
continue;
if (!CFNumberGetValue((CFNumberRef)object, kCFNumberLongType, &number))
continue;
usage = number;
object = CFDictionaryGetValue(element,CFSTR(kIOHIDElementUsagePageKey));
if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID())
continue;
if (!CFNumberGetValue((CFNumberRef)object, kCFNumberLongType, &number))
continue;
usagePage = number;
if (usagePage == kHIDPage_GenericDesktop) {
switch (usage) {
case kHIDUsage_GD_SystemAppMenu:
cookies->gButtonCookie_SystemAppMenu = cookie;
break;
case kHIDUsage_GD_SystemMenu:
cookies->gButtonCookie_SystemMenuSelect = cookie;
break;
case kHIDUsage_GD_SystemMenuRight:
buttonNextID = cookie;
cookies->gButtonCookie_SystemMenuRight = cookie;
break;
case kHIDUsage_GD_SystemMenuLeft:
buttonPreviousID = cookie;
cookies->gButtonCookie_SystemMenuLeft = cookie;
break;
case kHIDUsage_GD_SystemMenuUp:
buttonUpID = cookie;
cookies->gButtonCookie_SystemMenuUp = cookie;
break;
case kHIDUsage_GD_SystemMenuDown:
buttonDownID = cookie;
cookies->gButtonCookie_SystemMenuDown = cookie;
break;
}
}
}
return cookies;
}
void
createHIDDeviceInterface(io_object_t hidDevice, IOHIDDeviceInterface ***hdi)
{
io_name_t className;
IOCFPlugInInterface **plugInInterface = NULL;
HRESULT plugInResult = S_OK;
SInt32 score = 0;
IOReturn ioReturnValue = kIOReturnSuccess;
ioReturnValue = IOObjectGetClass(hidDevice, className);
print_errmsg_if_io_err(ioReturnValue, "Failed to get class name.");
ioReturnValue = IOCreatePlugInInterfaceForService(
hidDevice,
kIOHIDDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&plugInInterface,
&score);
if (ioReturnValue != kIOReturnSuccess)
return;
plugInResult = (*plugInInterface)->QueryInterface(
plugInInterface,
CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID),
(LPVOID)hdi);
print_errmsg_if_err(plugInResult != S_OK,
"Failed to create device interface.\n");
(*plugInInterface)->Release(plugInInterface);
}
void
setupAndRun(void)
{
CFMutableDictionaryRef hidMatchDictionary = NULL;
io_service_t hidService = (io_service_t)0;
io_object_t hidDevice = (io_object_t)0;
IOHIDDeviceInterface **hidDeviceInterface = NULL;
IOReturn ioReturnValue = kIOReturnSuccess;
cookie_struct_t cookies;
hidMatchDictionary = IOServiceNameMatching("AppleIRController");
hidService = IOServiceGetMatchingService(kIOMasterPortDefault,
hidMatchDictionary);
if (!hidService) {
fprintf(stderr, "Apple Infrared Remote not found.\n");
exit(1);
}
hidDevice = (io_object_t)hidService;
createHIDDeviceInterface(hidDevice, &hidDeviceInterface);
cookies = getHIDCookies((IOHIDDeviceInterface122 **)hidDeviceInterface);
ioReturnValue = IOObjectRelease(hidDevice);
print_errmsg_if_io_err(ioReturnValue, "Failed to release HID.");
if (hidDeviceInterface == NULL) {
fprintf(stderr, "No HID.\n");
exit(1);
}
ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, 0);
doRun(hidDeviceInterface, cookies);
if (ioReturnValue == KERN_SUCCESS)
ioReturnValue = (*hidDeviceInterface)->close(hidDeviceInterface);
(*hidDeviceInterface)->Release(hidDeviceInterface);
}
int
main (int argc, char **argv)
{
int c, option_index = 0;
while ((c = getopt_long(argc, argv, options, long_options, &option_index))
!= -1) {
switch (c) {
case 'h':
usage();
exit(0);
break;
case 'k':
driveKeynote = 1;
break;
case 'a':
driveKeyboardArrows = 1;
break;
default:
usage();
exit(1);
break;
}
}
setupAndRun();
return 0;
}