| 1 | #include <stdio.h> |
| 2 | #include <unistd.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <ctype.h> |
| 5 | #include <sys/errno.h> |
| 6 | #include <sysexits.h> |
| 7 | #include <mach/mach.h> |
| 8 | #include <mach/mach_error.h> |
| 9 | #include <IOKit/IOKitLib.h> |
| 10 | #include <IOKit/IOCFPlugIn.h> |
| 11 | #include <IOKit/hid/IOHIDLib.h> |
| 12 | #include <IOKit/hid/IOHIDKeys.h> |
| 13 | #include <CoreFoundation/CoreFoundation.h> |
| 14 | |
| 15 | |
| 16 | #include <map> |
| 17 | #include <sstream> |
| 18 | #include <iostream> |
| 19 | |
| 20 | #include "AppleRemote.h" |
| 21 | |
| 22 | // static |
| 23 | AppleRemote* AppleRemote::_instance = 0; |
| 24 | // static |
| 25 | const char* const AppleRemote::AppleRemoteDeviceName = "AppleIRController"; |
| 26 | // static |
| 27 | const int AppleRemote::REMOTE_SWITCH_COOKIE=19; |
| 28 | |
| 29 | AppleRemote::Listener::~Listener() { |
| 30 | } |
| 31 | |
| 32 | // public |
| 33 | AppleRemote& |
| 34 | AppleRemote::instance() { |
| 35 | if (_instance==0) { |
| 36 | _instance = new AppleRemote(); |
| 37 | } |
| 38 | return *_instance; |
| 39 | } |
| 40 | |
| 41 | AppleRemote::~AppleRemote() { |
| 42 | stopListening(); |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | AppleRemote::isRemoteAvailable() { |
| 47 | io_object_t hidDevice = _findAppleRemoteDevice(); |
| 48 | if (hidDevice != 0) { |
| 49 | IOObjectRelease(hidDevice); |
| 50 | return true; |
| 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | bool |
| 56 | AppleRemote::isListeningToRemote() { |
| 57 | return (hidDeviceInterface != NULL && !cookies.empty() && queue != NULL); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | AppleRemote::setListener(AppleRemote::Listener* listener) { |
| 62 | _listener = listener; |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | AppleRemote::startListening() { |
| 67 | if (queue != NULL) { |
| 68 | // already listening |
| 69 | return; |
| 70 | } |
| 71 | io_object_t hidDevice = _findAppleRemoteDevice(); |
| 72 | if (hidDevice == 0) goto error; |
| 73 | if (!_createDeviceInterface(hidDevice)) goto error; |
| 74 | if (!_initCookies()) goto error; |
| 75 | if (!_openDevice()) goto error; |
| 76 | goto cleanup; |
| 77 | |
| 78 | error: |
| 79 | stopListening(); |
| 80 | |
| 81 | cleanup: |
| 82 | IOObjectRelease(hidDevice); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | AppleRemote::stopListening() { |
| 87 | if (queue != NULL) { |
| 88 | (*queue)->stop(queue); |
| 89 | (*queue)->dispose(queue); |
| 90 | (*queue)->Release(queue); |
| 91 | queue = NULL; |
| 92 | } |
| 93 | |
| 94 | if (!cookies.empty()) { |
| 95 | cookies.clear(); |
| 96 | } |
| 97 | |
| 98 | if (hidDeviceInterface != NULL) { |
| 99 | (*hidDeviceInterface)->close(hidDeviceInterface); |
| 100 | (*hidDeviceInterface)->Release(hidDeviceInterface); |
| 101 | hidDeviceInterface = NULL; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | AppleRemote::runLoop() { |
| 107 | CFRunLoopRun(); |
| 108 | } |
| 109 | |
| 110 | // protected |
| 111 | AppleRemote::AppleRemote(): |
| 112 | openInExclusiveMode(true), |
| 113 | hidDeviceInterface(0), |
| 114 | queue(0), |
| 115 | _listener(0) { |
| 116 | _initCookieMap(); |
| 117 | } |
| 118 | |
| 119 | // private |
| 120 | void |
| 121 | AppleRemote::_initCookieMap() { |
| 122 | cookieToButtonMapping["14_12_11_6_5_"] = VolumePlus; |
| 123 | cookieToButtonMapping["14_13_11_6_5_"] = VolumeMinus; |
| 124 | cookieToButtonMapping["14_7_6_5_14_7_6_5_"] = Menu; |
| 125 | cookieToButtonMapping["14_8_6_5_14_8_6_5_"] = Play; |
| 126 | cookieToButtonMapping["14_9_6_5_14_9_6_5_"] = Right; |
| 127 | cookieToButtonMapping["14_10_6_5_14_10_6_5_"] = Left; |
| 128 | cookieToButtonMapping["14_6_5_4_2_"] = RightHold; |
| 129 | cookieToButtonMapping["14_6_5_3_2_"] = LeftHold; |
| 130 | cookieToButtonMapping["14_6_5_14_6_5_"] = MenuHold; |
| 131 | cookieToButtonMapping["18_14_6_5_18_14_6_5_"] = PlaySleep; |
| 132 | cookieToButtonMapping["19_"] = ControlSwitched; |
| 133 | } |
| 134 | |
| 135 | // private |
| 136 | io_object_t |
| 137 | AppleRemote::_findAppleRemoteDevice() { |
| 138 | CFMutableDictionaryRef hidMatchDictionary = 0; |
| 139 | io_iterator_t hidObjectIterator = 0; |
| 140 | io_object_t hidDevice = 0; |
| 141 | |
| 142 | hidMatchDictionary = IOServiceMatching(AppleRemoteDeviceName); |
| 143 | |
| 144 | // check for matching devices |
| 145 | IOReturn ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, |
| 146 | hidMatchDictionary, |
| 147 | &hidObjectIterator); |
| 148 | if ((ioReturnValue == kIOReturnSuccess) |
| 149 | && (hidObjectIterator != 0)) { |
| 150 | hidDevice = IOIteratorNext(hidObjectIterator); |
| 151 | } |
| 152 | |
| 153 | // IOServiceGetMatchingServices consumes a reference to the dictionary, so we |
| 154 | // don't need to release the dictionary ref. |
| 155 | hidMatchDictionary = 0; |
| 156 | return hidDevice; |
| 157 | } |
| 158 | |
| 159 | // private |
| 160 | bool |
| 161 | AppleRemote::_initCookies() { |
| 162 | IOHIDDeviceInterface122** handle = (IOHIDDeviceInterface122**)hidDeviceInterface; |
| 163 | |
| 164 | CFArrayRef elements; |
| 165 | IOReturn success = |
| 166 | (*handle)->copyMatchingElements(handle,NULL,(CFArrayRef*)&elements); |
| 167 | |
| 168 | if (success == kIOReturnSuccess) { |
| 169 | for (CFIndex i = 0; i < CFArrayGetCount(elements); i++) { |
| 170 | CFDictionaryRef element = (CFDictionaryRef)CFArrayGetValueAtIndex(elements, i); |
| 171 | |
| 172 | CFTypeRef object = CFDictionaryGetValue(element, |
| 173 | CFSTR(kIOHIDElementCookieKey)); |
| 174 | if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) continue; |
| 175 | long number; |
| 176 | if (!CFNumberGetValue((CFNumberRef)object, kCFNumberLongType, &number)) { |
| 177 | continue; |
| 178 | } |
| 179 | IOHIDElementCookie cookie = (IOHIDElementCookie)number; |
| 180 | |
| 181 | cookies.push_back((int)cookie); |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | // private |
| 189 | bool |
| 190 | AppleRemote::_createDeviceInterface(io_object_t hidDevice) { |
| 191 | SInt32 score = 0; |
| 192 | IOCFPlugInInterface** plugInInterface = NULL; |
| 193 | |
| 194 | IOReturn ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice, |
| 195 | kIOHIDDeviceUserClientTypeID, |
| 196 | kIOCFPlugInInterfaceID, |
| 197 | &plugInInterface, |
| 198 | &score); |
| 199 | if (ioReturnValue == kIOReturnSuccess) { |
| 200 | HRESULT plugInResult = (*plugInInterface)->QueryInterface(plugInInterface, |
| 201 | CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), |
| 202 | (LPVOID*) (&hidDeviceInterface)); |
| 203 | if (plugInResult != S_OK) { |
| 204 | std::cerr << "AppleRemote Error: couldn't create device interface " << std::endl; |
| 205 | } |
| 206 | // Release |
| 207 | if (plugInInterface) (*plugInInterface)->Release(plugInInterface); |
| 208 | } |
| 209 | return hidDeviceInterface != 0; |
| 210 | } |
| 211 | |
| 212 | // private |
| 213 | bool |
| 214 | AppleRemote::_openDevice() { |
| 215 | IOHIDOptionsType openMode = kIOHIDOptionsTypeNone; |
| 216 | if (openInExclusiveMode) openMode = kIOHIDOptionsTypeSeizeDevice; |
| 217 | IOReturn ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, openMode); |
| 218 | |
| 219 | if (ioReturnValue != KERN_SUCCESS) { |
| 220 | std::cerr << "AppleRemote Error: when opening device" << std::endl; |
| 221 | return false; |
| 222 | } |
| 223 | queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface); |
| 224 | if (!queue) { |
| 225 | std::cerr << "AppleRemote Error allocating queue" << std::endl; |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | HRESULT result = (*queue)->create(queue, 0, 12); |
| 230 | if (result != S_OK || !queue) { |
| 231 | std::cerr << "AppleRemote Error creating queue" << std::endl; |
| 232 | } |
| 233 | |
| 234 | for (std::vector<int>::iterator iter = cookies.begin(); |
| 235 | iter != cookies.end(); |
| 236 | iter++) { |
| 237 | IOHIDElementCookie cookie = (IOHIDElementCookie)(*iter); |
| 238 | (*queue)->addElement(queue, cookie, 0); |
| 239 | } |
| 240 | |
| 241 | CFRunLoopSourceRef eventSource; |
| 242 | ioReturnValue = (*queue)->createAsyncEventSource(queue, &eventSource); |
| 243 | if (ioReturnValue != KERN_SUCCESS) { |
| 244 | std::cerr << "AppleRemote Error creating async event source" << std::endl; |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | ioReturnValue = (*queue)->setEventCallout(queue,QueueCallbackFunction, this, NULL); |
| 249 | if (ioReturnValue != KERN_SUCCESS) { |
| 250 | std::cerr << "AppleRemote Error registering callback" << std::endl; |
| 251 | return false; |
| 252 | } |
| 253 | CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode); |
| 254 | (*queue)->start(queue); |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | // static |
| 259 | void |
| 260 | AppleRemote::QueueCallbackFunction(void* target, IOReturn result, |
| 261 | void* refcon, void* sender) { |
| 262 | AppleRemote* remote = (AppleRemote*)target; |
| 263 | remote->_queueCallbackFunction(result,refcon,sender); |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | AppleRemote::_queueCallbackFunction(IOReturn result, void* /*refcon*/, void* /*sender*/) { |
| 268 | AbsoluteTime zeroTime = {0,0}; |
| 269 | SInt32 sumOfValues = 0; |
| 270 | std::stringstream cookieString; |
| 271 | |
| 272 | while (result == kIOReturnSuccess) { |
| 273 | IOHIDEventStruct event; |
| 274 | result = (*queue)->getNextEvent(queue, &event, zeroTime, 0); |
| 275 | if (result != kIOReturnSuccess) break; |
| 276 | |
| 277 | if (REMOTE_SWITCH_COOKIE == (int)event.elementCookie) { |
| 278 | remoteId=event.value; |
| 279 | _handleEventWithCookieString("19_",0); |
| 280 | } else { |
| 281 | sumOfValues+=event.value; |
| 282 | cookieString << std::dec << (int)event.elementCookie << "_"; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | _handleEventWithCookieString(cookieString.str(), sumOfValues); |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | AppleRemote::_handleEventWithCookieString(std::string cookieString, |
| 291 | SInt32 sumOfValues) { |
| 292 | std::map<std::string,AppleRemote::Event>::iterator ii = cookieToButtonMapping.find(cookieString); |
| 293 | if (ii != cookieToButtonMapping.end() && _listener) { |
| 294 | AppleRemote::Event buttonid = ii->second; |
| 295 | if (_listener) _listener->appleRemoteButton(buttonid, sumOfValues>0); |
| 296 | } |
| 297 | } |