///////////////////////////////////////////////////////
| 227 | |
| 228 | //////////////////////////////////////////////////////////// |
| 229 | std::optional<Event> WindowImpl::waitEvent(Time timeout) |
| 230 | { |
| 231 | const auto timedOut = [timeout, startTime = std::chrono::steady_clock::now()] |
| 232 | { |
| 233 | const bool infiniteTimeout = timeout == Time::Zero; |
| 234 | return !infiniteTimeout && (std::chrono::steady_clock::now() - startTime) >= timeout.toDuration(); |
| 235 | }; |
| 236 | |
| 237 | // If the event queue is empty, let's first check if new events are available from the OS |
| 238 | if (m_events.empty()) |
| 239 | populateEventQueue(); |
| 240 | |
| 241 | // Here we use a manual wait loop instead of the optimized wait-event provided by the OS, |
| 242 | // so that we don't skip joystick events (which require polling) |
| 243 | while (m_events.empty() && !timedOut()) |
| 244 | { |
| 245 | sleep(milliseconds(10)); |
| 246 | populateEventQueue(); |
| 247 | } |
| 248 | |
| 249 | return popEvent(); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | //////////////////////////////////////////////////////////// |