ad_ffmpeg: return failure from init() if initial decode fails
[mplayer/greg.git] / input / ar.c
blob2f3fe16b24853d80d71e5ee08043f1eb45f02f4f
1 /*
2 * Apple Remote input interface
4 * Copyright (C) 2007 Zoltan Ponekker <pontscho at kac.poliod.hu>
6 * (modified a bit by Ulion <ulion2002 at gmail.com>)
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <IOKit/IOCFPlugIn.h>
26 #include <IOKit/hid/IOHIDLib.h>
27 #include <Carbon/Carbon.h>
29 #include <sys/types.h>
30 #include <unistd.h>
32 #include "input.h"
33 #include "ar.h"
34 #include "keycodes.h"
36 extern int slave_mode;
38 extern const double NSAppKitVersionNumber;
40 typedef struct cookie_keycode_map {
41 char *cookies;
42 int seq_len;
43 int keycode;
44 } cookie_keycode_map_t;
46 /* On tiger, 5 always follows 6; on leopard, 18 always follows 19.
47 * On leopard, there seems to be no cookie value of 5 or 6.
48 * Following is the shortened cookie sequence list
49 * keycode cookies_on_tiger cookies_on_leopard *down_state
50 * AR_PREV_HOLD 14+6+3+2 31+19+3+2 yes
51 * AR_NEXT_HOLD 14+6+4+2 31+19+4+2 yes
52 * AR_MENU_HOLD 14+6+14+6 31+19+31+19
53 * AR_VUP 14+12+11+6 31+29+28+19 yes
54 * AR_VDOWN 14+13+11+6 31+30+28+19 yes
55 * AR_MENU 14+7+6+14+7+6 31+20+19+31+20+19
56 * AR_PLAY 14+8+6+14+8+6 31+21+19+31+21+19
57 * AR_NEXT 14+9+6+14+9+6 31+22+19+31+22+19
58 * AR_PREV 14+10+6+14+10+6 31+23+19+31+23+19
59 * AR_PLAY_HOLD 18+14+6+18+14+6 35+31+19+35+31+19
61 * *down_state: A button with this feature has a pressed event and
62 * a released event, with which we can trace the state of the button.
63 * A button without this feature will only return one release event.
65 * hidden keys currently not implemented:
66 * hold for 5 secs
67 * MENU_NEXT_HOLD 15+14+6+15+14+6
68 * MENU_PREV_HOLD 16+14+6+16+14+6
69 * MENU_VUP_HOLD 20+14+6+20+14+6
70 * MENU_VDOWN_HOLD 19+14+6+19+14+6
72 * It seems that pressing 'menu' and 'play' on the Apple Remote for
73 * 5 seconds will trigger the make-pair function of the remote.
74 * MENU_PLAY_HOLD 21+15+14+6+15+14+6
77 static const cookie_keycode_map_t ar_codes_tiger[] = {
78 { "\x0E\x06\x03\x02", 4, AR_PREV_HOLD },
79 { "\x0E\x06\x04\x02", 4, AR_NEXT_HOLD },
80 { "\x0E\x06\x0E\x06", 4, AR_MENU_HOLD },
81 { "\x0E\x0C\x0B\x06", 4, AR_VUP },
82 { "\x0E\x0D\x0B\x06", 4, AR_VDOWN },
83 { "\x0E\x07\x06\x0E\x07\x06", 6, AR_MENU },
84 { "\x0E\x08\x06\x0E\x08\x06", 6, AR_PLAY },
85 { "\x0E\x09\x06\x0E\x09\x06", 6, AR_NEXT },
86 { "\x0E\x0A\x06\x0E\x0A\x06", 6, AR_PREV },
87 { "\x12\x0E\x06\x12\x0E\x06", 6, AR_PLAY_HOLD },
88 { NULL, 0, MP_INPUT_NOTHING },
91 static const cookie_keycode_map_t ar_codes_leopard[] = {
92 { "\x1F\x13\x03\x02", 4, AR_PREV_HOLD },
93 { "\x1F\x13\x04\x02", 4, AR_NEXT_HOLD },
94 { "\x1F\x13\x1F\x13", 4, AR_MENU_HOLD },
95 { "\x1F\x1D\x1C\x13", 4, AR_VUP },
96 { "\x1F\x1E\x1C\x13", 4, AR_VDOWN },
97 { "\x1F\x14\x13\x1F\x14\x13", 6, AR_MENU },
98 { "\x1F\x15\x13\x1F\x15\x13", 6, AR_PLAY },
99 { "\x1F\x16\x13\x1F\x16\x13", 6, AR_NEXT },
100 { "\x1F\x17\x13\x1F\x17\x13", 6, AR_PREV },
101 { "\x23\x1F\x13\x23\x1F\x13", 6, AR_PLAY_HOLD },
102 { NULL, 0, MP_INPUT_NOTHING },
105 static int is_leopard;
106 static const cookie_keycode_map_t *ar_codes;
108 static IOHIDQueueInterface **queue;
109 static IOHIDDeviceInterface **hidDeviceInterface = NULL;
110 static int initialized = 0;
111 static int hidDeviceIsOpen;
113 /* Maximum number of elements in queue before oldest elements
114 in queue begin to be lost. Set to 16 to hold at least 2 events. */
115 static const int MAX_QUEUE_SIZE = 16;
118 static int FindHIDDevices(mach_port_t masterPort,
119 io_iterator_t *hidObjectIterator)
121 CFMutableDictionaryRef hidMatchDictionary;
122 IOReturn ioReturnValue;
124 // Set up a matching dictionary to search the I/O Registry
125 // by class name for all HID class devices.
126 hidMatchDictionary = IOServiceMatching("AppleIRController");
128 // Now search I/O Registry for matching devices.
129 ioReturnValue = IOServiceGetMatchingServices(masterPort,
130 hidMatchDictionary,
131 hidObjectIterator);
133 // If search is unsuccessful, print message and hang.
134 if (ioReturnValue != kIOReturnSuccess ||
135 !IOIteratorIsValid(*hidObjectIterator)) {
136 return -1;
138 return 0;
141 static int getHIDCookies(IOHIDDeviceInterface122 **handle,
142 IOHIDElementCookie **cookies,
143 int *nr_cookies)
145 CFTypeRef object;
146 long number;
147 CFArrayRef elements;
148 CFDictionaryRef element;
149 CFIndex i;
151 *nr_cookies = 0;
153 if (!handle || !(*handle))
154 return -1;
156 // Copy all elements, since we're grabbing most of the elements
157 // for this device anyway, and thus, it's faster to iterate them
158 // ourselves. When grabbing only one or two elements, a matching
159 // dictionary should be passed in here instead of NULL.
160 if (((*handle)->copyMatchingElements(handle, NULL, &elements)) != kIOReturnSuccess)
161 return -1;
163 // No elements, still a valid result.
164 if (CFArrayGetCount(elements)==0)
165 return 0;
167 *cookies = calloc(CFArrayGetCount(elements), sizeof(IOHIDElementCookie));
168 if (*cookies == NULL)
169 return -1;
171 for (i=0; i<CFArrayGetCount(elements); i++) {
172 element = CFArrayGetValueAtIndex(elements, i);
174 // Get cookie.
175 object = CFDictionaryGetValue(element, CFSTR(kIOHIDElementCookieKey));
176 if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID())
177 continue;
178 if (!CFNumberGetValue((CFNumberRef)object, kCFNumberLongType, &number))
179 continue;
180 (*cookies)[(*nr_cookies)++] = (IOHIDElementCookie)number;
183 return 0;
186 static int CreateHIDDeviceInterface(io_object_t hidDevice,
187 IOHIDDeviceInterface ***hidDeviceInterface)
189 io_name_t className;
190 IOCFPlugInInterface **plugInInterface = NULL;
191 SInt32 score = 0;
193 if (IOObjectGetClass(hidDevice, className) != kIOReturnSuccess)
194 return -1;
196 if (IOCreatePlugInInterfaceForService(hidDevice,
197 kIOHIDDeviceUserClientTypeID,
198 kIOCFPlugInInterfaceID,
199 &plugInInterface,
200 &score) != kIOReturnSuccess)
201 return -1;
203 // Call a method of the intermediate plugin to create the device interface
204 if ((*plugInInterface)->QueryInterface(plugInInterface,
205 CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID),
206 (LPVOID)hidDeviceInterface) != S_OK
207 || *hidDeviceInterface == NULL || **hidDeviceInterface == NULL) {
208 (*plugInInterface)->Release(plugInInterface);
209 return -1;
212 (*plugInInterface)->Release(plugInInterface);
214 return 0;
217 int mp_input_ar_init(void)
219 io_iterator_t hidObjectIterator;
220 io_object_t hidDevice;
221 int i;
222 IOHIDElementCookie *cookies = NULL;
223 int nr_cookies = 0;
225 if (initialized)
226 mp_input_ar_close(-1);
228 if (floor(NSAppKitVersionNumber) <= 824 /* NSAppKitVersionNumber10_4 */) {
229 ar_codes = &ar_codes_tiger[0];
230 is_leopard = 0;
232 else {
233 ar_codes = &ar_codes_leopard[0];
234 is_leopard = 1;
237 if (FindHIDDevices(kIOMasterPortDefault, &hidObjectIterator))
238 return -1;
240 // Multiple controls could be found, we only use the first usable one.
241 while ((hidDevice = IOIteratorNext(hidObjectIterator))) {
242 if (CreateHIDDeviceInterface(hidDevice, &hidDeviceInterface) < 0) {
243 hidDeviceInterface = NULL;
244 IOObjectRelease(hidDevice);
245 continue;
247 if (getHIDCookies((IOHIDDeviceInterface122 **)hidDeviceInterface,
248 &cookies,
249 &nr_cookies) < 0) {
250 (*hidDeviceInterface)->Release(hidDeviceInterface);
251 hidDeviceInterface = NULL;
252 IOObjectRelease(hidDevice);
253 continue;
255 IOObjectRelease(hidDevice);
256 break;
258 if (hidDeviceInterface == NULL)
259 goto mp_input_ar_init_error;
261 // Open the device.
262 if ((*hidDeviceInterface)->open(hidDeviceInterface,
263 kIOHIDOptionsTypeSeizeDevice) != kIOReturnSuccess)
264 goto mp_input_ar_init_error;
265 hidDeviceIsOpen = 1;
267 if ((queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface)) == NULL
268 || *queue == NULL)
269 goto mp_input_ar_init_error;
271 // Create the queue.
272 (*queue)->create(queue, 0, MAX_QUEUE_SIZE);
274 // Add elements to the queue to make the queue work.
275 // On tiger, it's a sequence from 1 to 21,
276 // maybe it's the range of cookie values.
277 for (i = 0;i < nr_cookies;i++)
278 (*queue)->addElement(queue, cookies[i], 0);
280 // not used anymore
281 free(cookies);
283 // Start data delivery to the queue.
284 (*queue)->start(queue);
286 // not useful anymore
287 IOObjectRelease(hidObjectIterator);
289 initialized = 1;
290 return 0;
292 mp_input_ar_init_error:
293 free(cookies);
294 if (hidDeviceInterface != NULL) {
295 if (*hidDeviceInterface != NULL) {
296 (*hidDeviceInterface)->close(hidDeviceInterface);
297 (*hidDeviceInterface)->Release(hidDeviceInterface);
299 hidDeviceInterface = NULL;
301 IOObjectRelease(hidObjectIterator);
302 return -1;
305 static int is_mplayer_front(void)
307 ProcessSerialNumber myProc, frProc;
308 Boolean sameProc;
309 pid_t parentPID;
311 if (GetFrontProcess(&frProc) == noErr
312 && GetCurrentProcess(&myProc) == noErr
313 && SameProcess(&frProc, &myProc, &sameProc) == noErr) {
314 if (sameProc)
315 return 1;
316 // If MPlayer is running in slave mode, also check parent process.
317 if (slave_mode && GetProcessPID(&frProc, &parentPID) == noErr)
318 return parentPID==getppid();
320 return 0;
323 int mp_input_ar_read(void *ctx, int fd)
325 int i, down = 0;
326 int ret = MP_INPUT_NOTHING;
327 AbsoluteTime zeroTime = {0,0};
328 IOHIDEventStruct event;
329 static int prev_event = 0;
330 IOReturn result = kIOReturnSuccess;
332 const cookie_keycode_map_t *ar_code;
333 int cookie_nr = 0;
334 char cookie_queue[MAX_QUEUE_SIZE];
335 int value_queue[MAX_QUEUE_SIZE];
337 if (initialized == 0)
338 return MP_INPUT_NOTHING;
340 while ((result = (*queue)->getNextEvent(queue, &event, zeroTime, 0)) == kIOReturnSuccess) {
341 #ifdef TEST
342 printf(" - event cookie: %d, value: %d, long value: %d\n",
343 (int)event.elementCookie, (int)event.value, (int)event.longValue);
344 #endif
345 // Shorten cookie sequence by removing cookies value 5 and 18,
346 // since 5 always follows 6 (on tiger), 18 follows 19 (on leopard).
347 if ((int)event.elementCookie == 5
348 || ((int)event.elementCookie == 18 && is_leopard))
349 continue;
350 // Check valid cookie range.
351 if ((int)event.elementCookie > 35 || (int)event.elementCookie < 2) {
352 cookie_nr = 0;
353 continue;
355 cookie_queue[cookie_nr] = (char)(int)event.elementCookie;
356 value_queue[cookie_nr++] = event.value;
357 // 4 cookies are necessary to make up a valid sequence.
358 if (cookie_nr>=4) {
359 // Find matching sequence.
360 ar_code = ar_codes;
361 while (ar_code->cookies != NULL &&
362 (cookie_nr < ar_code->seq_len ||
363 0 != memcmp(ar_code->cookies,
364 &cookie_queue[cookie_nr-ar_code->seq_len],
365 ar_code->seq_len)))
366 ++ar_code;
367 if (ar_code->cookies != NULL) {
368 ret = ar_code->keycode;
369 switch (ret) {
370 // For these 4 keys, the remote can keep a hold state.
371 case AR_VUP:
372 case AR_VDOWN:
373 case AR_NEXT_HOLD:
374 case AR_PREV_HOLD:
375 for (i = cookie_nr-ar_code->seq_len; i < cookie_nr; ++i) {
376 if (value_queue[i]) {
377 down = MP_KEY_DOWN;
378 break;
381 break;
382 default:
383 down = 0;
389 if (!is_mplayer_front()) {
390 if (hidDeviceIsOpen) {
391 (*hidDeviceInterface)->close(hidDeviceInterface);
392 hidDeviceIsOpen = 0;
394 // Read out all pending events.
395 while (result == kIOReturnSuccess)
396 result = (*queue)->getNextEvent(queue, &event, zeroTime, 0);
398 return MP_INPUT_NOTHING;
400 // If we are switched from running as a foreground process to a
401 // background process and back again, re-open the device to make
402 // sure we are not affected by the system or other applications
403 // using the Apple Remote.
404 else if (!hidDeviceIsOpen) {
405 if ((*hidDeviceInterface)->open(hidDeviceInterface,
406 kIOHIDOptionsTypeSeizeDevice) == kIOReturnSuccess)
407 hidDeviceIsOpen = 1;
410 if (ret > 0)
411 prev_event = ret;
412 return ret | down;
415 void mp_input_ar_close(int fd)
417 if (initialized == 0)
418 return;
420 // Close the device.
421 (*hidDeviceInterface)->close(hidDeviceInterface);
423 // Stop data delivery to queue.
424 (*queue)->stop(queue);
425 // Dispose of queue.
426 (*queue)->dispose(queue);
427 // Release the queue we allocated.
428 (*queue)->Release(queue);
430 // Release the interface.
431 (*hidDeviceInterface)->Release(hidDeviceInterface);
433 initialized = 0;
436 #ifdef TEST
437 int main(void)
439 int ret;
441 if (mp_input_ar_init() < 0) {
442 printf("Unable to initialize Apple Remote.\n");
443 return 1;
446 while (1) {
447 switch ((ret = mp_input_ar_read(NULL, 0)) & ~MP_KEY_DOWN) {
448 case AR_PLAY: printf(" - AR_PLAY."); break;
449 case AR_PLAY_HOLD: printf(" - AR_PLAY_HOLD."); break;
450 case AR_NEXT: printf(" - AR_NEXT."); break;
451 case AR_NEXT_HOLD: printf(" - AR_NEXT_HOLD."); break;
452 case AR_PREV: printf(" - AR_PREV."); break;
453 case AR_PREV_HOLD: printf(" - AR_PREV_HOLD."); break;
454 case AR_MENU: printf(" - AR_MENU."); break;
455 case AR_MENU_HOLD: printf(" - AR_MENU_HOLD."); break;
456 case AR_VUP: printf(" - AR_VUP."); break;
457 case AR_VDOWN: printf(" - AR_VDOWN."); break;
459 if ((ret > 0 )&&(ret & MP_KEY_DOWN))
460 printf(" [hold]");
461 if (ret > 0)
462 printf("\n");
465 mp_input_ar_close(0);
466 return 0;
468 #endif