Search code examples
x11repaintxlib

X11: How to delay repainting until all events are processed?


I'm writing a program that has an X11/Xlib interface, and my event processing loop looks like this:

while (XNextEvent(display, &ev) >= 0) {
    switch (ev.type) {
        // Process events
    }
}

The problem is when the window is resized, I get a bunch of Expose events telling me which parts of the window to redraw. If I redraw them in direct response to the events, the redraw operation lags terribly because it is so slow (after resizing I get to see all the newly invalidated rectangles refresh one by one.)

What I would like to do is to record the updated window size as it changes, and only run one redraw operation on the entire window (or at least only two rectangles) when there are no more events left to process.

Unfortunately I can't see a way to do this. I tried this:

do {
    XPeekEvent(display, &ev);
    while (XCheckMaskEvent(display, ExposureMask | StructureNotifyMask, &ev)) {
        switch (ev.type) {
            // Process events, record but don't process redraw events
        }
    }
    // No more events, do combined redraw here
}

Which does actually work, but it's a little inefficient, and if an event arrives that I am not interested in the XCheckMaskEvent call doesn't remove it from the queue, so it stays there stopping XPeekEvent from blocking, resulting in 100% CPU use.

I was just wondering whether there is a standard way to achieve the delayed/combined redraw that I am after? Many of the Xlib event processing functions seem to block, so they're not really suitable to use if you want to do some processing just before they block, but only if they would block!


EDIT: For the record, this is the solution I used. It's a simplified version of n.m.'s:

while (XNextEvent(display, &ev) >= 0) {
    switch (ev.type) {
        // Process events, remember any redraws needed later
    }
    if (!XPending(display)) {
        // No more events, redraw if needed
    }
}

Solution

  • Try something like the following (not actually tested):

    while (TRUE) {
      if (XPending(display) || !pendingRedraws) {
        // if an event is pending, fetch it and process it
        // otherwise, we have neither events nor pending redraws, so we can
        // safely block on the event queue
        XNextEvent (display, &ev);
        if (isExposeEvent(&ev)) {
          pendingRedraws = TRUE;
        }
        else {
          processEvent(&ev);
        }
      }
      else {
        // we must have a pending redraw
        redraw();
        pendingRedraws = FALSE;
      }
    }
    

    It could be beneficial to wait for 10 ms or so before doing the redraw. Unfortunately the raw Xlib has no interface for timers. You need a higher-level toolkit for that (all toolkits including Xt have some kind of timer interface), or work directly with the underlying socket of the X11 connection.