///////////////////////////////////////////////////////
| 110 | |
| 111 | //////////////////////////////////////////////////////////// |
| 112 | bool JoystickImpl::open(unsigned int index) |
| 113 | { |
| 114 | if (index >= Joystick::Count) |
| 115 | return false; |
| 116 | |
| 117 | // Retrieve activity states |
| 118 | ActivityStates& states = getActivity(); |
| 119 | const std::lock_guard lock(states.mutex); |
| 120 | |
| 121 | auto jni = Jni::attachCurrentThread(*states.activity); |
| 122 | if (!jni) |
| 123 | { |
| 124 | err() << "Failed to initialize JNI" << std::endl; |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | auto inputDeviceClass = JniInputDeviceClass::findClass(jni->getEnv()); |
| 129 | if (!inputDeviceClass) |
| 130 | return false; |
| 131 | |
| 132 | auto deviceIds = inputDeviceClass->getDeviceIds(); |
| 133 | if (!deviceIds) |
| 134 | return false; |
| 135 | |
| 136 | unsigned foundGamepadsSoFar = 0; |
| 137 | for (jsize i = 0; i < deviceIds->getLength(); ++i) |
| 138 | { |
| 139 | const auto deviceId = (*deviceIds)[i]; |
| 140 | auto inputDevice = inputDeviceClass->getDevice(deviceId); |
| 141 | if (!inputDevice) |
| 142 | continue; |
| 143 | |
| 144 | if (!inputDevice->supportsSource(AINPUT_SOURCE_GAMEPAD | AINPUT_SOURCE_JOYSTICK)) |
| 145 | continue; |
| 146 | |
| 147 | if (foundGamepadsSoFar < index) |
| 148 | { |
| 149 | ++foundGamepadsSoFar; |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | // Found device might be already registered. It might be even registered for |
| 154 | // another joystick index. So skip that and search for some other. |
| 155 | if (states.joystickStates.find(deviceId) != states.joystickStates.end()) |
| 156 | continue; |
| 157 | |
| 158 | if (const auto capabilities = getCapabilitiesFromJni(*inputDevice)) |
| 159 | m_capabilities = *capabilities; |
| 160 | else |
| 161 | return false; |
| 162 | |
| 163 | m_identification = Joystick::Identification{ |
| 164 | inputDevice->getName(), |
| 165 | inputDevice->getVendorId(), |
| 166 | inputDevice->getProductId(), |
| 167 | }; |
| 168 | |
| 169 | m_currentDeviceIdx = deviceId; |
nothing calls this directly
no test coverage detected