| 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 "JoystickCocoa.h" |
| 10 | #include "api/JoystickTypes.h" |
| 11 | #include "utils/CommonMacros.h" |
| 12 | |
| 13 | #include <assert.h> |
| 14 | |
| 15 | using namespace JOYSTICK; |
| 16 | |
| 17 | #define MAX_JOYSTICK_BUTTONS 512 |
| 18 | |
| 19 | CJoystickCocoa::CJoystickCocoa(IOHIDDeviceRef device, CJoystickInterfaceCocoa* api) |
| 20 | : CJoystick(EJoystickInterface::COCOA), |
| 21 | m_api(api), |
| 22 | m_device(device), |
| 23 | m_bInitialized(false) |
| 24 | { |
| 25 | assert(m_api != nullptr); |
| 26 | |
| 27 | m_api->RegisterInputCallback(this, m_device); |
| 28 | |
| 29 | // Must initialize in the constructor to fill out joystick properties |
| 30 | Initialize(); |
| 31 | } |
| 32 | |
| 33 | CJoystickCocoa::~CJoystickCocoa(void) |
| 34 | { |
| 35 | m_api->UnregisterInputCallback(this); |
| 36 | } |
| 37 | |
| 38 | bool CJoystickCocoa::Equals(const CJoystick* rhs) const |
| 39 | { |
| 40 | const CJoystickCocoa* joystick = dynamic_cast<const CJoystickCocoa*>(rhs); |
| 41 | |
| 42 | return joystick && m_device == joystick->m_device; |
| 43 | } |
| 44 | |
| 45 | bool CJoystickCocoa::Initialize(void) |
| 46 | { |
| 47 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 48 | |
| 49 | if (!m_bInitialized) |
| 50 | { |
| 51 | CFArrayRef elements = IOHIDDeviceCopyMatchingElements(m_device, NULL, kIOHIDOptionsTypeNone); |
| 52 | |
| 53 | CFIndex n = CFArrayGetCount(elements); |
| 54 | for (CFIndex i = 0; i < n; i++) |
| 55 | { |
| 56 | IOHIDElementRef element = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i); |
| 57 | uint32_t usagePage = IOHIDElementGetUsagePage(element); |
| 58 | uint32_t usage = IOHIDElementGetUsage(element); |
| 59 | |
| 60 | if (usagePage == GENERIC_DESKTOP_USAGE_PAGE && |
| 61 | usage >= AXIS_MIN_USAGE_NUMBER && |
| 62 | usage <= AXIS_MAX_USAGE_NUMBER) |
| 63 | { |
| 64 | CFIndex min = IOHIDElementGetLogicalMin(element); |
| 65 | CFIndex max = IOHIDElementGetLogicalMax(element); |
| 66 | |
| 67 | CocoaAxis axis = { element, min, max }; |
| 68 | m_axes.push_back(axis); |
| 69 | } |
| 70 | else if (usagePage == BUTTON_USAGE_PAGE) |
| 71 | { |
| 72 | int buttonId = (int)usage - 1; |
| 73 | if (0 <= buttonId && buttonId < MAX_JOYSTICK_BUTTONS) |
| 74 | { |
| 75 | if (buttonId >= (int)m_buttons.size()) |
| 76 | m_buttons.resize(buttonId + 1); |
| 77 | |
| 78 | m_buttons[buttonId] = element; |
| 79 | } |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | // TODO: handle other usage pages |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | SetButtonCount(m_buttons.size()); |
| 88 | SetAxisCount(m_axes.size()); |
| 89 | |
| 90 | // Gather some identifying information |
| 91 | CFStringRef productRef = (CFStringRef)IOHIDDeviceGetProperty(m_device, CFSTR(kIOHIDProductKey)); |
| 92 | CFNumberRef vendorIdRef = (CFNumberRef)IOHIDDeviceGetProperty(m_device, CFSTR(kIOHIDVendorIDKey)); |
| 93 | CFNumberRef productIdRef = (CFNumberRef)IOHIDDeviceGetProperty(m_device, CFSTR(kIOHIDProductIDKey)); |
| 94 | |
| 95 | if (productRef) |
| 96 | { |
| 97 | char product_name[128] = { }; |
| 98 | CFStringGetCString(productRef, product_name, sizeof(product_name), kCFStringEncodingASCII); |
| 99 | SetName(product_name); |
| 100 | } |
| 101 | |
| 102 | if (vendorIdRef && productIdRef) |
| 103 | { |
| 104 | int vendorId = 0; |
| 105 | int productId = 0; |
| 106 | |
| 107 | CFNumberGetValue(vendorIdRef, kCFNumberIntType, &vendorId); |
| 108 | CFNumberGetValue(productIdRef, kCFNumberIntType, &productId); |
| 109 | |
| 110 | SetVendorID(vendorId); |
| 111 | SetProductID(productId); |
| 112 | } |
| 113 | |
| 114 | if (CJoystick::Initialize()) |
| 115 | m_bInitialized = true; |
| 116 | } |
| 117 | |
| 118 | return m_bInitialized; |
| 119 | } |
| 120 | |
| 121 | void CJoystickCocoa::Deinitialize(void) |
| 122 | { |
| 123 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 124 | |
| 125 | CJoystick::Deinitialize(); |
| 126 | |
| 127 | m_buttons.clear(); |
| 128 | m_axes.clear(); |
| 129 | |
| 130 | m_bInitialized = false; |
| 131 | } |
| 132 | |
| 133 | bool CJoystickCocoa::GetEvents(std::vector<kodi::addon::PeripheralEvent>& events) |
| 134 | { |
| 135 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 136 | return CJoystick::GetEvents(events); |
| 137 | } |
| 138 | |
| 139 | bool CJoystickCocoa::ScanEvents(void) |
| 140 | { |
| 141 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 142 | return m_bInitialized; // Events arrive asynchronously |
| 143 | } |
| 144 | |
| 145 | void CJoystickCocoa::SetButtonValue(unsigned int buttonIndex, JOYSTICK_STATE_BUTTON buttonValue) |
| 146 | { |
| 147 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 148 | CJoystick::SetButtonValue(buttonIndex, buttonValue); |
| 149 | } |
| 150 | |
| 151 | void CJoystickCocoa::SetHatValue(unsigned int hatIndex, JOYSTICK_STATE_HAT hatValue) |
| 152 | { |
| 153 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 154 | CJoystick::SetHatValue(hatIndex, hatValue); |
| 155 | } |
| 156 | |
| 157 | void CJoystickCocoa::SetAxisValue(unsigned int axisIndex, JOYSTICK_STATE_AXIS axisValue) |
| 158 | { |
| 159 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 160 | CJoystick::SetAxisValue(axisIndex, axisValue); |
| 161 | } |
| 162 | |
| 163 | void CJoystickCocoa::InputValueChanged(IOHIDValueRef value) |
| 164 | { |
| 165 | IOHIDElementRef element = IOHIDValueGetElement(value); |
| 166 | |
| 167 | for (unsigned int i = 0; i < m_axes.size(); i++) |
| 168 | { |
| 169 | if (m_axes[i].element == element) |
| 170 | { |
| 171 | float d = IOHIDValueGetIntegerValue(value); |
| 172 | float val = 2.0f * (d - m_axes[i].min) / (float)(m_axes[i].max - m_axes[i].min) - 1.0f; |
| 173 | SetAxisValue(i, val); |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | for (unsigned int i = 0; i < m_buttons.size(); i++) |
| 179 | { |
| 180 | if (m_buttons[i] == element) |
| 181 | { |
| 182 | bool bPressed = IOHIDValueGetIntegerValue(value) != 0; |
| 183 | SetButtonValue(i, bPressed ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 184 | return; |
| 185 | } |
| 186 | } |
| 187 | } |