Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Handle Timer Events in Xlib

1,423 views
Skip to first unread message

Victor Zandy

unread,
May 20, 2001, 9:22:48 PM5/20/01
to

I am writing a simple Xlib program that uses a signal-based timer to
update the contents of windows (e.g., a clock). I am running XFree86
4.0.2 on x86 Linux.

The Xlib documentation and related FAQs makes it clear that I should
not call Xlib from the timer signal handler. So somehow I need to
handle the timer event from within the event loop. But the only way
appears to be polling, instead of blocking for events, which seems
expensive.

If I do need to poll, what is the common wisdom for choosing a polling
interval?

I understand that I could use Xt. But I'm curious about finding an
approach in Xlib.

How is the Xt timer implemented? Can I reinvent the Xt mechanism
strictly over Xlib?


Thanks,
Vic Zandy

Ken Lee

unread,
May 21, 2001, 1:03:55 AM5/21/01
to
In article <cpxk83b...@goat.cs.wisc.edu>, Victor Zandy
<za...@cs.wisc.edu> wrote:
>How is the Xt timer implemented? Can I reinvent the Xt mechanism
>strictly over Xlib?

Xt uses select(). You should be able to do the same with basic Xlib. One
reason to choose Xt is convenience functions like this (in addition to the
widget sets).

-------
Ken Lee, http://www.rahul.net/kenton/

Victor Zandy

unread,
May 21, 2001, 10:13:19 AM5/21/01
to
ken...@nojunk.rahul.net- (Ken Lee) writes:
> Xt uses select(). You should be able to do the same with basic Xlib. One
> reason to choose Xt is convenience functions like this (in addition to the
> widget sets).

Thanks. I didn't know how to get a descriptor to pass to select. In
a separate email, someone mentioned ConnectionNumber to me, which I
didn't know about.

Vic

kj hermans

unread,
May 22, 2001, 5:39:47 AM5/22/01
to
Victor Zandy <za...@cs.wisc.edu> wrote in message news:<cpxae46...@goat.cs.wisc.edu>...

Hm that was me; sorry to keep it from the group, but I have to get around this bloody
Google usenet posting interface first - damn ! they also post my email address - gotta fix that;
why can't these conglomerates simply refrain from buying each other out every usecond ?
anyway, my post was:

this is the code (feel free to use etcetera):

#include <sys/time.h>
#include <unistd.h>
#include <X11/Xlib.h>

Bool XNextEventTimed(Display* dsp, XEvent* event_return, struct timeval*
tv) {

// optimization

if (tv == NULL) {
XNextEvent(dsp, event_return);
return True;
}

// the real deal

if (XPending(dsp) == 0) {
int fd = ConnectionNumber(dsp);
fd_set readset;
FD_ZERO(&readset);
FD_SET(fd, &readset);
if (select(fd+1, &readset, NULL, NULL, tv) == 0) {
return False;
} else {
XNextEvent(dsp, event_return);
return True;
}
} else {
XNextEvent(dsp, event_return);
return True;
}
}

A bit adjusted, though; I have this in a separate file with a header
file including X11/Xlib.h and sys/time.h, but this ought to do just fine
when you paste it into your code. In case you're not familiar with
struct timeval, it has a second and a microsecond component and can be
initialized simply like this:

struct timeval tv = { 1, 0 }; // fall through every idle second

then you do;

if (XNextEventTimed(dpy, &event, &tv) == True) {
//.. do your event processing switch
} else {
//.. do your timeout thing
}

Mind you, the man page of select() says you can't rely on the values in
struct timeval after select() returns (even though on Linux you can) so
you'd have to do your own scheduling still.
Hoping to've been of some help,

KC

0 new messages