Search code examples
copenglx11glx

X: trigger events at fixed intervals


What's the best way of waiting a finite a mount of time for an expose event on X, then waking up and doing a redraw, even if not expose event has been received? The purpose is to have an opengl animation running at sometimes where at others I simply want to redraw if needed. Here is my code as I have it now, check below for pseudo-code of what I'm looking for:

    do {
        XNextEvent(dpy, &event);
        switch(event.type) {
            ...
            case Expose:
               need_redraw = True;
               break;
        }
    } while(XPending(dpy)); /* loop to compress events */

    if ( need_redraw )
    {
        // do redraw
    } 

And this is a pseudo-example of what I would like:

    bool animation_enabled = true;
    XPostTimeoutEventEvery( 0.3 ); // <-- X will send a "Timeout"
                                   // event each 0.3 seconds.

    do {
        XNextEvent(dpy, &event);
        switch(event.type) {
            ...
            case Expose:
               // Redraw if it is required
               need_redraw = True;
               break;
            // -- here -- 
            case Timeout:
               // Otherwise, after 0.3 seconds, redraw anyway if
               // the animation is running
               if ( animation_enabled )
               {
                  need_redraw = True;
               }
               break;


        }
    } while(XPending(dpy)); /* loop to compress events */

    if ( need_redraw )
    {
        // do redraw

        // potentially change "animation_enabled" value 
    } 

Solution

  • Just use a regular system timer; if a desired event doesn't arrive in time, just do whatever you want to do.

    X is not an application framework, it's a display protocol. Timers are outside (of that) scope of X11.

    Check here and the link provided in that answer.