XNextEvent select [solved]
Hello, I have a program that uses XNextEvent in my main loop. When an event occurs, I handle it then the program blocks on XNextEvent.
The problem: I want XNextEvent to block, but I want XNextEvent to timeout and unblock after a certain amount of time (say, 0.1sec). I've been trying to figure out how I could accomplish this with the XNextEvent and XPeekEvent family of functions. I'm handling the problem now by creating two threads. One loops on XNextEvent. The other loops on select. I would really like to remove any threads I don't need. This problem has been solved. Read this post of this thread. |
use XPending.
|
Thanks, but I really don't see how I could use XPending to fix this situation. I'm assuming you mean I can check XPending inside of my timer thread. That is, something like:
Code:
while(1) { Code:
while(1) { Code:
void* XInputThread(void*) { The second snippit _works_ but it is sort of hackerish. The threaded model is much cleaner and more responsive, but it just won't work in XLib. How do people normally solve this problem? Thank you. |
Quote:
Quote:
Quote:
You can have multiple threads, but only one of them can use XLib. Why not lowering your timer granularity to an acceptable value ? |
Quote:
|
Sorry to pull this thread from the grave, but I still need a solution. I tried lowering my timer granularity and calling XCheckIfEvent. This method works... but it uses way too much CPU time. I need a way to block on input or a timer. Just like the select() call for the console.
I am trying to accept user input and use a timer to drive some animation. It would be ideal if the time is adjustable. Thanks |
Hi -
If many libraries that are higher-level than Xlib support timers. For example, Xt (and consequently any Xt-based GUI, like Motif) has "XtAppAddTimeOut()". Another example is the GTK+ library, which has gtk_timeout_add () (which I use in one of my own animation viewers). Perhaps you might consider trying a higher-level library, instead of coding directly in Xlib. Or perhaps you can look at one of these implementations and see how they do it (although I doubt you'll find anything terribly useful). As far as I know, Xlib doesn't provide any way to create "user defined events", so that option's out, too. Xlib *can*, however, generate an event when you change a property. Perhaps your "timer" thread might be able to call "XChangeProperty()" and generate a usable Xlib event without too much overhead. Just a thought... Hope that helps (at least a little bit) Your .. PSM |
Quote:
|
Quote:
|
I finally came up with a solution to the problem. The answer is simple but not obvious. I never realized that you could obtain a file descriptor to an X11 display. This example I wrote does this and shows how one can make a loop that accepts X Events while allowing a timeout. Creating an interval timer from my example is just as simple.
Code:
#include <stdio.h> |
And here is the interval timer that will still accept X input.
Code:
#include <stdio.h> |
Thanks for the feedback.
I was wondering what the ConnectionNumber call could be used for, now I have a good answer. By the way, your code won't be portable on OpenVMS. |
Quote:
EDIT: The function is XMultiplexInput(). I suppose I could do some #ifdefs and make it so the code will run on both. However, if I was using OpenVMS then I wouldn't of had this problem in the first place. :) And for sake of simplicity.. |
All times are GMT -5. The time now is 03:31 PM. |