Skip to content | Skip to navigation
This question pertains to X Windows programming
Okay the following code is commonly used in programs that require doing something at a given interval. This code happens to be from an X Windows application that draws the current time in a window. The code has been cut down for brevity and changed slightly to isolate only the parts needed.
I've used the code in my own programs and I've never questioned why it works. Like so many things in programming you learn because somebody tells you "that's how it's done, don't worry about it!". Well now I want to know why and for what reason.
My question is, why are we using the current X11 file descriptor to execute some code at a given interval. Why is this done?
From all my tests and I've done alot with this code, there is no other way to do it correctly with respect to X's event loop. If you do it other ways you'll find that X blocks it's event loop and your code does not get repeatedly called. Seems to only work correctly using a kernel select in the fashion below.
fd_set fd;
struct timeval tv;
/* Used to get at the X11 file desciptor */
int xfd = the_current_X_connection_number;
/* Assume the bits below are enclosed in an X11 event loop */
tv.tv_sec = 1;
FD_ZERO (&fd);
FD_SET (xfd, &fd);
if (select (xfd + 1, &fd, 0, 0, &tv) == 0)
{
/* Do something at the given interval */
}
I know this is kind of a niche question but maybe somebody can shed some light on it.
Well, actually I've never done any X Window (without "s" :-) ) programming (or any serious programming for that matter), but I'd be interested in the answer anyway as X Window is in my TODO list.
There's however some logic in your code sample, at least to me: you're writing to the xfd (either directly or indirectly using a function from a library) in your "Do something at the given interval", I guess? Then maybe, as X client applications can't write anything they want at anytime (still guessing, but that would make a huge mess), you have to wait for xfd to be "not ready for reading" (that is, the X server has stopped sending stuff and it's our turn). Then the call to select() would make sense.
But this kind of synchronous behaviour seems rather dumb to me for a professional-grade application, so again I might be writing nonsense.
PS: By the way, kernel "functions" don't exist as far as I know, there are system calls and library functions to use them indirectly... ok, ok, I'm out :-)
X Window (without "s" :-) )
I suppose if you want to get all wrapped around perfect details... That level of precision is seldom worth the effort though. Most people call it the X Windows System.
But this kind of synchronous behaviour seems rather dumb to me for a professional-grade application
Maybe but the only area I've seen something like this done is with regards to having a GUI clock update at a given interval. I'm sure there are other ways to have precise repainting in X at a given interval.
PS: By the way, kernel "functions" don't exist as far as I know, there are system calls and library functions to use them indirectly... ok, ok, I'm out :-)
I use the term function very loosely to mean any API/Library/routine/method/etc...