Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Fix input events random delay on X11 #54313

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions platform/x11/os_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2319,27 +2319,34 @@ void OS_X11::_poll_events() {
{
MutexLock mutex_lock(events_mutex);

// Non-blocking wait for next event and remove it from the queue.
XEvent ev;
while (XCheckIfEvent(x11_display, &ev, _predicate_all_events, nullptr)) {
// Check if the input manager wants to process the event.
if (XFilterEvent(&ev, None)) {
// Event has been filtered by the Input Manager,
// it has to be ignored and a new one will be received.
continue;
}
_check_pending_events(polled_events);
}
}
}

// Handle selection request events directly in the event thread, because
// communication through the x server takes several events sent back and forth
// and we don't want to block other programs while processing only one each frame.
if (ev.type == SelectionRequest) {
_handle_selection_request_event(&(ev.xselectionrequest));
continue;
}
void OS_X11::_check_pending_events(LocalVector<XEvent> &r_events) {
// Flush to make sure to gather all pending events.
XFlush(x11_display);

polled_events.push_back(ev);
}
// Non-blocking wait for next event and remove it from the queue.
XEvent ev;
while (XCheckIfEvent(x11_display, &ev, _predicate_all_events, nullptr)) {
// Check if the input manager wants to process the event.
if (XFilterEvent(&ev, None)) {
// Event has been filtered by the Input Manager,
// it has to be ignored and a new one will be received.
continue;
}

// Handle selection request events directly in the event thread, because
// communication through the x server takes several events sent back and forth
// and we don't want to block other programs while processing only one each frame.
if (ev.type == SelectionRequest) {
_handle_selection_request_event(&(ev.xselectionrequest));
continue;
}

r_events.push_back(ev);
}
}

Expand All @@ -2361,6 +2368,9 @@ void OS_X11::process_xevents() {
MutexLock mutex_lock(events_mutex);
events = polled_events;
polled_events.clear();

// Check for more pending events to avoid an extra frame delay.
_check_pending_events(events);
}

for (uint32_t event_index = 0; event_index < events.size(); ++event_index) {
Expand Down
1 change: 1 addition & 0 deletions platform/x11/os_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class OS_X11 : public OS_Unix {
static void _poll_events_thread(void *ud);
bool _wait_for_events() const;
void _poll_events();
void _check_pending_events(LocalVector<XEvent> &r_events);

static Bool _predicate_all_events(Display *display, XEvent *event, XPointer arg);
static Bool _predicate_clipboard_selection(Display *display, XEvent *event, XPointer arg);
Expand Down