| 162 | } |
| 163 | |
| 164 | void JoystickReader::wait_for_events(const int timeout_ms) { |
| 165 | auto current = m_curr_values.values; |
| 166 | int n_polled_events = 0; |
| 167 | SDL_Event event; |
| 168 | bool any_new_data = false; |
| 169 | // wait for at least one event with a timeut |
| 170 | if (!SDL_WaitEventTimeout(&event, timeout_ms)) { |
| 171 | // m_console->debug("Got no event after 100ms"); |
| 172 | return; |
| 173 | } |
| 174 | // process this event |
| 175 | auto ret = process_event(&event, current); |
| 176 | if (ret == 2 || ret == 5 || ret == 4) { |
| 177 | any_new_data = true; |
| 178 | n_polled_events++; |
| 179 | } |
| 180 | if (ret == -1) { |
| 181 | // Terminate the Joystick reader, such that SDL is terminated |
| 182 | m_console->warn( |
| 183 | "Terminating joystick reader, it won't restart until openhd is " |
| 184 | "restarted"); |
| 185 | terminate = true; |
| 186 | return; |
| 187 | } |
| 188 | // and then get as many more events as we can get (we already spun up the |
| 189 | // thread anyways) |
| 190 | while (SDL_PollEvent(&event)) { |
| 191 | ret = process_event(&event, current); |
| 192 | if (ret == 2 || ret == 5 || ret == 4) { |
| 193 | any_new_data = true; |
| 194 | n_polled_events++; |
| 195 | } |
| 196 | } |
| 197 | // m_console->debug("N polled events:{}",n_polled_events); |
| 198 | if (any_new_data) { |
| 199 | std::lock_guard<std::mutex> guard(m_curr_values_mutex); |
| 200 | for (int i = 0; i < m_curr_values.values.size(); i++) { |
| 201 | m_curr_values.values[i] = current[i]; |
| 202 | } |
| 203 | m_curr_values.last_update = std::chrono::steady_clock::now(); |
| 204 | m_curr_values.considered_connected = true; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | int JoystickReader::process_event(void* event1, |
| 209 | std::array<uint16_t, N_CHANNELS>& current) { |