| 1 | /* |
| 2 | * Copyright (C) 2014-2021 Garrett Brown |
| 3 | * Copyright (C) 2014-2021 Team Kodi |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | * See LICENSE.md for more information. |
| 7 | */ |
| 8 | |
| 9 | #include "JoystickInterfaceCocoa.h" |
| 10 | #include "JoystickCocoa.h" |
| 11 | #include "api/JoystickManager.h" |
| 12 | #include "api/JoystickTypes.h" |
| 13 | |
| 14 | #include <algorithm> |
| 15 | |
| 16 | using namespace JOYSTICK; |
| 17 | |
| 18 | // --- MatchingDictionary ------------------------------------------------------ |
| 19 | |
| 20 | static CFMutableDictionaryRef MatchingDictionary(UInt32 inUsagePage, UInt32 inUsage) |
| 21 | { |
| 22 | CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorDefault, |
| 23 | 0, |
| 24 | &kCFTypeDictionaryKeyCallBacks, |
| 25 | &kCFTypeDictionaryValueCallBacks); |
| 26 | if (!dict) |
| 27 | return NULL; |
| 28 | |
| 29 | CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &inUsagePage); |
| 30 | if (!number) |
| 31 | { |
| 32 | CFRelease(dict); |
| 33 | return NULL; |
| 34 | } |
| 35 | |
| 36 | CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsagePageKey), number); |
| 37 | CFRelease(number); |
| 38 | |
| 39 | number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &inUsage); |
| 40 | if (!number) |
| 41 | { |
| 42 | CFRelease(dict); |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsageKey), number); |
| 47 | CFRelease(number); |
| 48 | |
| 49 | return dict; |
| 50 | } |
| 51 | |
| 52 | // --- CJoystickInterfaceCocoa ------------------------------------------------- |
| 53 | |
| 54 | CJoystickInterfaceCocoa::CJoystickInterfaceCocoa(void) |
| 55 | : m_manager(NULL) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | EJoystickInterface CJoystickInterfaceCocoa::Type(void) const |
| 60 | { |
| 61 | return EJoystickInterface::COCOA; |
| 62 | } |
| 63 | |
| 64 | bool CJoystickInterfaceCocoa::Initialize(void) |
| 65 | { |
| 66 | Deinitialize(); |
| 67 | |
| 68 | IOHIDManagerRef manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); |
| 69 | |
| 70 | CFMutableDictionaryRef criteria_arr[2]; |
| 71 | criteria_arr[0] = MatchingDictionary(GENERIC_DESKTOP_USAGE_PAGE, JOYSTICK_USAGE_NUMBER); |
| 72 | if (!criteria_arr[0]) |
| 73 | { |
| 74 | CFRelease(manager); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | criteria_arr[1] = MatchingDictionary(GENERIC_DESKTOP_USAGE_PAGE, GAMEPAD_USAGE_NUMBER); |
| 79 | if (!criteria_arr[1]) |
| 80 | { |
| 81 | CFRelease(criteria_arr[0]); |
| 82 | CFRelease(manager); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | CFArrayRef criteria = CFArrayCreate(kCFAllocatorDefault, (const void**)criteria_arr, 2, NULL); |
| 87 | if (!criteria) |
| 88 | { |
| 89 | CFRelease(criteria_arr[1]); |
| 90 | CFRelease(criteria_arr[0]); |
| 91 | CFRelease(manager); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | IOHIDManagerSetDeviceMatchingMultiple(manager, criteria); |
| 96 | CFRelease(criteria); |
| 97 | CFRelease(criteria_arr[1]); |
| 98 | CFRelease(criteria_arr[0]); |
| 99 | |
| 100 | IOHIDManagerRegisterDeviceMatchingCallback(manager, DeviceAddedCallback, this); |
| 101 | IOHIDManagerRegisterDeviceRemovalCallback(manager, DeviceRemovedCallback, this); |
| 102 | IOHIDManagerRegisterInputValueCallback(manager, InputValueChangedCallback, this); |
| 103 | |
| 104 | IOHIDManagerScheduleWithRunLoop(manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); |
| 105 | |
| 106 | IOReturn rv = IOHIDManagerOpen(manager, kIOHIDOptionsTypeNone); |
| 107 | if (rv != kIOReturnSuccess) |
| 108 | { |
| 109 | CFRelease(manager); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | m_manager = manager; |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | void CJoystickInterfaceCocoa::Deinitialize(void) |
| 119 | { |
| 120 | if (m_manager) |
| 121 | { |
| 122 | IOHIDManagerClose(m_manager, 0); |
| 123 | CFRelease(m_manager); |
| 124 | m_manager = NULL; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | bool CJoystickInterfaceCocoa::ScanForJoysticks(JoystickVector& joysticks) |
| 129 | { |
| 130 | std::lock_guard<std::recursive_mutex> lock(m_deviceDiscoveryMutex); |
| 131 | |
| 132 | for (auto it = m_discoveredDevices.begin(); it != m_discoveredDevices.end(); ++it) |
| 133 | joysticks.push_back(JoystickPtr(new CJoystickCocoa(*it, this))); |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | void CJoystickInterfaceCocoa::DeviceAdded(IOHIDDeviceRef device) |
| 139 | { |
| 140 | bool bDeviceAdded = false; |
| 141 | |
| 142 | { |
| 143 | std::lock_guard<std::recursive_mutex> lock(m_deviceDiscoveryMutex); |
| 144 | |
| 145 | if (std::find(m_discoveredDevices.begin(), m_discoveredDevices.end(), device) == m_discoveredDevices.end()) |
| 146 | { |
| 147 | m_discoveredDevices.push_back(device); |
| 148 | bDeviceAdded = true; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (bDeviceAdded) |
| 153 | { |
| 154 | CJoystickManager::Get().SetChanged(true); |
| 155 | CJoystickManager::Get().TriggerScan(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void CJoystickInterfaceCocoa::DeviceRemoved(IOHIDDeviceRef device) |
| 160 | { |
| 161 | { |
| 162 | std::lock_guard<std::recursive_mutex> lock(m_deviceDiscoveryMutex); |
| 163 | m_discoveredDevices.erase(std::remove(m_discoveredDevices.begin(), m_discoveredDevices.end(), device), m_discoveredDevices.end()); |
| 164 | } |
| 165 | |
| 166 | CJoystickManager::Get().SetChanged(true); |
| 167 | CJoystickManager::Get().TriggerScan(); |
| 168 | } |
| 169 | |
| 170 | void CJoystickInterfaceCocoa::InputValueChanged(IOHIDValueRef newValue) |
| 171 | { |
| 172 | IOHIDElementRef element = IOHIDValueGetElement(newValue); |
| 173 | IOHIDDeviceRef device = IOHIDElementGetDevice(element); |
| 174 | |
| 175 | std::lock_guard<std::recursive_mutex> lock(m_deviceInputMutex); |
| 176 | |
| 177 | for (std::vector<DeviceHandle>::iterator it = m_registeredDevices.begin(); it != m_registeredDevices.end(); ++it) |
| 178 | { |
| 179 | if (it->first == device) |
| 180 | it->second->InputValueChanged(newValue); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void CJoystickInterfaceCocoa::RegisterInputCallback(ICocoaInputCallback* callback, IOHIDDeviceRef device) |
| 185 | { |
| 186 | std::lock_guard<std::recursive_mutex> lock(m_deviceInputMutex); |
| 187 | |
| 188 | m_registeredDevices.push_back(std::make_pair(device, callback)); |
| 189 | } |
| 190 | |
| 191 | void CJoystickInterfaceCocoa::UnregisterInputCallback(ICocoaInputCallback* callback) |
| 192 | { |
| 193 | std::lock_guard<std::recursive_mutex> lock(m_deviceInputMutex); |
| 194 | |
| 195 | for (std::vector<DeviceHandle>::iterator it = m_registeredDevices.begin(); it != m_registeredDevices.end(); ) |
| 196 | { |
| 197 | if (it->second == callback) |
| 198 | it = m_registeredDevices.erase(it); |
| 199 | else |
| 200 | ++it; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void CJoystickInterfaceCocoa::DeviceAddedCallback(void* data, IOReturn result, |
| 205 | void* sender, IOHIDDeviceRef device) |
| 206 | { |
| 207 | CJoystickInterfaceCocoa* interface = static_cast<CJoystickInterfaceCocoa*>(data); |
| 208 | interface->DeviceAdded(device); |
| 209 | } |
| 210 | |
| 211 | void CJoystickInterfaceCocoa::DeviceRemovedCallback(void* data, IOReturn result, |
| 212 | void* sender, IOHIDDeviceRef device) |
| 213 | { |
| 214 | CJoystickInterfaceCocoa* interface = static_cast<CJoystickInterfaceCocoa*>(data); |
| 215 | interface->DeviceRemoved(device); |
| 216 | } |
| 217 | |
| 218 | void CJoystickInterfaceCocoa::InputValueChangedCallback(void* data, |
| 219 | IOReturn result, |
| 220 | void* sender, |
| 221 | IOHIDValueRef newValue) |
| 222 | { |
| 223 | CJoystickInterfaceCocoa* interface = static_cast<CJoystickInterfaceCocoa*>(data); |
| 224 | interface->InputValueChanged(newValue); |
| 225 | } |