Skip to content

Commit

Permalink
Merge pull request #679 from p12tic/x11-fix-event-wait-race-condition
Browse files Browse the repository at this point in the history
XWindowsEventQueueBuffer: Fix delays when waiting for new events
  • Loading branch information
shymega authored May 30, 2020
2 parents 8891364 + 722b7d6 commit b373d8e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/lib/platform/XWindowsEventQueueBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ XWindowsEventQueueBuffer::~XWindowsEventQueueBuffer()
close(m_pipefd[1]);
}

int XWindowsEventQueueBuffer::getPendingCountLocked()
{
Lock lock(&m_mutex);
// work around a bug in old libx11 which causes the first XPending not to read events under
// certain conditions. The issue happens when libx11 has not yet received replies for all
// flushed events. In that case, internally XPending will not try to process received events
// as the reply for the last event was not found. As a result, XPending will return the number
// of pending events without regard to the events it has just read.
// https://gitlab.freedesktop.org/xorg/lib/libx11/-/merge_requests/1 fixes this on libx11 side.
m_impl->XPending(m_display);
return m_impl->XPending(m_display);
}

void
XWindowsEventQueueBuffer::waitForEvent(double dtimeout)
{
Expand Down Expand Up @@ -163,7 +176,7 @@ XWindowsEventQueueBuffer::waitForEvent(double dtimeout)
// we want to give the cpu a chance s owe up this to 25ms
#define TIMEOUT_DELAY 25

while (((dtimeout < 0.0) || (remaining > 0)) && QLength(m_display)==0 && retval==0){
while (((dtimeout < 0.0) || (remaining > 0)) && getPendingCountLocked() == 0 && retval == 0) {
#if HAVE_POLL
retval = poll(pfds, 2, TIMEOUT_DELAY); //16ms = 60hz, but we make it > to play nicely with the cpu
if (pfds[1].revents & POLLIN) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/platform/XWindowsEventQueueBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class XWindowsEventQueueBuffer : public IEventQueueBuffer {
private:
void flush();

int getPendingCountLocked();

private:
typedef std::vector<XEvent> EventList;
IXWindowsImpl* m_impl;
Expand Down

2 comments on commit b373d8e

@richwalker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this commit doesn't fix the "sticking special keys" problem - just tested it and SHIFT is recognised when pressed but not when released, and only keys typed with SHIFT held are recognised until the pointer leaves and re-enters the screen

@richwalker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

barrier.tc.log.gz
barrier.t560.log.gz
these DEBUG1 level log files show t560 as server, tc as client. I move the cursor onto tc, click in a window, type sSsSs, move cursor back to t560 and back to tc, and then type tTtTt.
I've gzipped them for size - let me know if there is a better way to send them.
from the timestamps this looks like a "bad" key press:
[2020-07-03T12:24:52] DEBUG1: unable to match modifier state (0000,1021) for key 39

Please sign in to comment.