[SOLVED] How to, XEvent, while loops and response time
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
does any one know how to increase response time for a button click within these loops?
the sleep is for when the time is up it runs, loads the next image then changes the display to the new image. ( dockapp )
I've been trying to figure out how to put an onclick event in so it fires as soon as it has been click, and not wait until the next cycle of sleep. I have had no luck in it. the command call works it is the time delay that is has between the time clicked and time executed being governed more by the seep then a queue.
Code:
//while (1)
while (!sleep(( doc.getStime())))
{
// here systems sleep time set in seconds
// user input it waits until the sleep time
// is up
while (XPending(doc.getDisplay()))
{
XNextEvent(doc.getDisplay(), &doc.event);
switch (doc.event.type)
{
case ButtonPress:
{
if (doc.event.xbutton.button == Button1)
cout<<"Button pressed"<<endl;
std::string cmd = doc.getCommandOne();
system( cmd.c_str() );
XSync(doc.getDisplay(), doc.getScreen());
break;
}
default:
break;
}
// here systems sleep time set in seconds
// button time never regitures.
//
// if (!sleep(doc.getStime()))
// {
// doc.DisplayImage();
// }
}// end inner while loop
// here systems sleep time set in seconds
// user input it waits until the sleep time
// is up
doc.DisplayImage();
}// end control while loop
If yes, check my LQ blog, there's one about using that function.
Don't use sleep, use pure event driven to have great performance.
Yeah, that sleep is holding me up, no pun intended. I'm sitting here right now being baffled about which new path to take because of sleep.
it was originally just a simple while loop to just keep the Window showing until its next update with a different image. Redraw is taken care of by default behavior for the window manager.
Code:
while ( !sleep( doc.getStime() )) { doc.DisplayImage(); }
As soon as time is up, it pulls a different image from a list, loads it, sets it to the drawable ,then displays it then goes back to sleep again holding the loop in suspension.
Now adding this XEvent:
I think I'd be more of an int handle then file (io read write) handle. Because it is more time related in waiting for a time to elapse before before pulling a different image to display, then
while waiting on a set time to elapse to do something, if anything else was to take place within the time it is waiting then check to see what it is (XEvent) , if the XEvent is what it's looking for then act on it in between the time it is just "standing around waiting for the time to run out to do the other thing".
so it needs to keep looking for other events to be taking place then preform an action on them if they meet the requirements. else do not bother with them.
As neither of these event are read write actions pertaining to a file, then I'd have to say they are a int handle.
Forking came to mind, just before this post of yours did.
Thanks for the lead.
This program only has an io event that only happens after a set time elapses.
Timers tend to be done by the higher level toolkits like Xt/Motif/Gtk and so on. Xlib itself doesn't provide a way to do it. However, there's a well known, down and dirty approach that uses select() on Xlibs internal file descriptor.
Here's a function you can use in place of XNextEvent:
Code:
#include <unistd.h>
#include <sys/time.h>
#include <X11/Xlib.h>
Bool XNextEventTimed(Display *display, XEvent *event_return, struct timeval *timeout)
{
/*
* Usage:
* struct timeval = { .tv_sec = 5, .tv_usec = 0 };
* if (XNextEventTimed(display, &event, &timeout) == True) {
* //.. do your event processing switch
* } else {
* //.. do your timeout thing
* }
*
* Return:
* True if an event was selected, or False when no event
* was found prior to the timeout or when select returned
* an error (most likely EINTR).
*/
if (timeout == NULL) {
XNextEvent(display, event_return);
return True;
}
if (XPending(display) == 0) {
int fd = ConnectionNumber(display);
fd_set readset;
FD_ZERO(&readset);
FD_SET(fd, &readset);
if (select(fd + 1, &readset, NULL, NULL, timeout) > 0) {
XNextEvent(display, event_return);
return True;
} else {
return False;
}
} else {
XNextEvent(display, event_return);
return True;
}
}
And you can use it in your event loop, something like this:
You probably should read up on select(), especially the comments relating to timeval and how it is handled on return (linux does it differently to other UNIXes).
I can't take credit for the function. I've modified it slightly and added the comments, but I found the original out there on usenet way back when.
Anyway, that might give you something to work with.
Last edited by GazL; 05-18-2018 at 12:38 PM.
Reason: fixed whitespace
Timers tend to be done by the higher level toolkits like Xt/Motif/Gtk and so on. Xlib itself doesn't provide a way to do it. However, there's a well known, down and dirty approach that uses select() on Xlibs internal file descriptor.
Here's a function you can use in place of XNextEvent:
Code:
removed due to its already posted ....
You probably should read up on select(), especially the comments relating to timeval and how it is handled on return (linux does it differently to other UNIXes).
I can't take credit for the function. I've modified it slightly and added the comments, but I found the original out there on usenet way back when.
Anyway, that might give you something to work with.
Thanks or this code, as I have seen exactly what you are talking about in, "Xlib itself doesn't provide a way to do it" I have been mulling over how to use that to call it when it looks to be only for a I/O event that has to take place. synchronous I/O multiplexing
Quote:
Description
select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the
file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is
considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without blocking.
which leads me to this which I just now googled to.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.