0

I'm trying to work out a time-delay to display steady flickering. I didn't get it to work with sleep() or time.h, what seems to be logically.

How could I achieve something like, displaying a white rectangle for 10 seconds, then clear this area (also for 10 sec), in a loop.

In this question, a socket of the X11 connection is mentioned to do things like that. But where to dig in, in this matter?

Thanks

Community
  • 1
  • 1
k t
  • 343
  • 5
  • 15

1 Answers1

0

The file descriptor is in the Display structure and can be obtained with the macro ConnectionNumber(dis).

You could then use poll with a timeout to wait for an event to arrive or a timeout to occur.

As mentioned in the other questions, XPending will let you see if there are any events, so you don't actually need to check the file descriptor, you could do something like this:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <time.h>
#include <unistd.h>

int main() 
{
    XEvent ev;
    Display *dis = XOpenDisplay(NULL);
    Window win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, 0, BlackPixel (dis, 0), WhitePixel(dis, 0));
    XMapWindow(dis, win);
    GC gc = XCreateGC(dis, win, 0, 0);
    int draw = 1;
    time_t t = time(NULL) + 10;

    XMapWindow(dis, win);

    XSelectInput(dis, win, ExposureMask | KeyPressMask);

    while (1)  
    {
        if (XPending(dis) > 0)
        {
            XNextEvent(dis, &ev);
            switch  (ev.type) 
            {
                case Expose:   
                printf("Exposed.\n");
                    if (draw > 0)
                {
                    XFillRectangle(dis, win, gc, 100, 100, 300, 300);
                }
                break;

                case KeyPress:
                printf("KeyPress\n");
                /* Close the program if q is pressed.*/
                if (XLookupKeysym(&ev.xkey, 0) == XK_q) 
                    {
                    exit(0);
                }
                break;
            }
        }
        else
        {
            printf("Pending\n");
            sleep(1);
            if (time(NULL) >= t)
            {
                /* Force an exposure */
            XClearArea(dis, win, 100, 100, 300, 300, True);
            t += 10;
            draw = 1 - draw;
            }
        }
    }

    return 0;
}

Note: you might want to use something like usleep to get a finer timer granularity.

parkydr
  • 7,596
  • 3
  • 32
  • 42
  • hi, actually my computer is much to bad to get it done smoothly with Xlib I think (I tried it with usleep - always some recognizable irregular latency ). And There is always depending on the x11 display managing routines. Maybe i'll try working directly with my gfx card if I can achieve it. – k t Jun 17 '14 at 22:14