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.