///////////////////////////////////////////////////////
| 177 | |
| 178 | //////////////////////////////////////////////////////////// |
| 179 | bool JoystickImpl::open(unsigned int index) |
| 180 | { |
| 181 | const AutoreleasePool pool; |
| 182 | m_index = index; |
| 183 | m_hat = nullptr; |
| 184 | const Location deviceLoc = m_locationIDs[index]; // The device we need to load |
| 185 | |
| 186 | // Get all devices |
| 187 | CFSetRef devices = HIDJoystickManager::getInstance().copyJoysticks(); |
| 188 | if (devices == nullptr) |
| 189 | return false; |
| 190 | |
| 191 | // Get a usable copy of the joysticks devices. |
| 192 | const CFIndex joysticksCount = CFSetGetCount(devices); |
| 193 | std::vector<CFTypeRef> devicesArray(static_cast<std::size_t>(joysticksCount)); |
| 194 | CFSetGetValues(devices, devicesArray.data()); |
| 195 | |
| 196 | // Get the desired joystick. |
| 197 | IOHIDDeviceRef self = nil; |
| 198 | for (CFIndex i(0); self == nil && i < joysticksCount; ++i) |
| 199 | { |
| 200 | auto* d = static_cast<IOHIDDeviceRef>(const_cast<void*>(devicesArray[static_cast<std::size_t>(i)])); |
| 201 | if (deviceLoc == HIDInputManager::getLocationID(d)) |
| 202 | self = d; |
| 203 | } |
| 204 | |
| 205 | if (self == nil) |
| 206 | { |
| 207 | CFRelease(devices); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | m_identification.name = getDeviceString(self, CFSTR(kIOHIDProductKey), m_index); |
| 212 | m_identification.vendorId = getDeviceUint(self, CFSTR(kIOHIDVendorIDKey), m_index); |
| 213 | m_identification.productId = getDeviceUint(self, CFSTR(kIOHIDProductIDKey), m_index); |
| 214 | |
| 215 | // Get a list of all elements attached to the device. |
| 216 | CFArrayRef elements = IOHIDDeviceCopyMatchingElements(self, nullptr, kIOHIDOptionsTypeNone); |
| 217 | |
| 218 | if (elements == nullptr) |
| 219 | { |
| 220 | CFRelease(devices); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | // Go through all connected elements. |
| 225 | const CFIndex elementsCount = CFArrayGetCount(elements); |
| 226 | for (int i = 0; i < elementsCount; ++i) |
| 227 | { |
| 228 | auto* element = static_cast<IOHIDElementRef>(const_cast<void*>(CFArrayGetValueAtIndex(elements, i))); |
| 229 | switch (IOHIDElementGetUsagePage(element)) |
| 230 | { |
| 231 | case kHIDPage_GenericDesktop: |
| 232 | switch (IOHIDElementGetUsage(element)) |
| 233 | { |
| 234 | case kHIDUsage_GD_X: |
| 235 | m_axis[Joystick::Axis::X] = element; |
| 236 | break; |
nothing calls this directly
no test coverage detected