Nice. I've run into a problem while working on my X11 port of my toolkit. I haven't been able to solve it for almost a month and that is why my development is stalled. Maybe a few bright minds can figure it out.
My toolkit is powered by a main event loop, like most GUI applications. There is a class, X11Application, that supports a generic run() function for applications. The function looks, basically, like:
Code:
while (running()) {
handle_xevents(); // Contains a XCheckIfEvent loop to queue up all events
while (ltk_system()->send_next_event()) {} // Dispatches all the queued events
root_widget_redraw(); // Repaint the widget hierachy as needed
screen->send_screen(); // Send changes to the Window
}
Faily simple. The problem is that I want it to support a multimedia timer. My LTKSystem class supports registering timers and handling elapsed time. That is:
Code:
ltk_system()->handle_timer(elapsedTime);
will check to see if any timers need to be fired, and if so it will call the timer's callback function. The function then returns the amount of time we need to wait until the next timer.
What I need is an efficient way to combine these mechinisms. I can do something like this:
Code:
while (running()) {
delay = ltk_system()->handle_timer(delay);
handle_xevents(); // Contains a XCheckIfEvent loop to queue up all events
while (ltk_system()->send_next_event) {} // Dispatches all the queued events
root_widget_redraw(); // Repaint the widget hierachy as needed
screen->send_screen(); // Send changes to the Window
some_sleep_function(delay);
}
But this has the problem of not accepting user input until a timer is fired. So, if there happens to be only a 1sec timer running, then the UI will only be updated once a second.
Another option is removing the sleep(). This would work, but the loop is sooo busy that the program will take up 100% CPU while it loops largely for no reason.
Another option is to make the loop run with a certain granularity. Here is the problem, even on my computer (Athlon 64 3500+) I cannot find a good balance between performance and responsiveness.
There must be some way to handle this problem. I tried a few multi-threaded solutions but they all failed since XLib is not threadsafe. Perhaps there is another way to arrange it so that Threading will not be an issue.
ANY Ideas?? Thanks
(PS, I am selling a bunch of small 5VDC 3A powersupplies with 100-240VAC input. They are perfect for powering circuits and electronics projects. I'll post them on the FS thread later today.)
Bookmarks