|
Home > Archive > Unix Programming > December 2007 > Problem with using sleep in a loop
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem with using sleep in a loop
|
|
| Tomdumon@gmail.com 2007-12-16, 1:37 am |
| Hey,
I've been trying to fix this for a while. I was writing a code to send
a buttonpress in X, left arrow then right arrow. I managed to get that
working but it was a bit too rapid so I decided to add a sleep
inbetween that's where the trouble began. It doesn't do anything, it
doesn't get to the loop. I was thinking it might be to do with the
signals (it works fine without a delay function). I've even tried
replacing sleep with other things like comparing cpu times or loops
but to no success.
Anyway here's the source:
#include <stdio.h>
#include <X11/extensions/XTest.h>
#include <X11/Xlib.h>
#include <unistd.h>
Display *dpy;
int main()
{
sleep(4);
if ((dpy = XOpenDisplay("Glorioso.localdomain:0.0")) == NULL){
printf("It XXXXed up");
exit(1);
}
while(1){
XTestFakeKeyEvent(dpy, 100, True, CurrentTime);
XTestFakeKeyEvent(dpy, 100, False, CurrentTime);
sleep(2);
XTestFakeKeyEvent(dpy, 102, True, CurrentTime);
XTestFakeKeyEvent(dpy, 102, False, CurrentTime);
sleep(2);
}
XCloseDisplay(dpy);
}
| |
| Måns Rullgård 2007-12-16, 7:32 am |
| Tomdumon@gmail.com writes:
> Hey,
>
> I've been trying to fix this for a while. I was writing a code to send
> a buttonpress in X, left arrow then right arrow. I managed to get that
> working but it was a bit too rapid so I decided to add a sleep
> inbetween that's where the trouble began. It doesn't do anything, it
> doesn't get to the loop. I was thinking it might be to do with the
> signals (it works fine without a delay function). I've even tried
> replacing sleep with other things like comparing cpu times or loops
> but to no success.
>
> Anyway here's the source:
> #include <stdio.h>
> #include <X11/extensions/XTest.h>
> #include <X11/Xlib.h>
> #include <unistd.h>
>
> Display *dpy;
>
> int main()
> {
> sleep(4);
>
> if ((dpy = XOpenDisplay("Glorioso.localdomain:0.0")) == NULL){
> printf("It XXXXed up");
> exit(1);
> }
> while(1){
>
> XTestFakeKeyEvent(dpy, 100, True, CurrentTime);
> XTestFakeKeyEvent(dpy, 100, False, CurrentTime);
> sleep(2);
> XTestFakeKeyEvent(dpy, 102, True, CurrentTime);
> XTestFakeKeyEvent(dpy, 102, False, CurrentTime);
> sleep(2);
> }
>
> XCloseDisplay(dpy);
> }
Xlib is probably buffering the messages to the server. Try adding an
XFlush(dpy) call before each sleep().
--
Måns Rullgård
mans@mansr.com
| |
| Tomdumon@gmail.com 2007-12-16, 7:32 am |
| On 16 Dec, 11:33, M=E5ns Rullg=E5rd <m...@mansr.com> wrote:
> Tomdu...@gmail.com writes:
>
>
>
>
>
>
>
>
> Xlib is probably buffering the messages to the server. Try adding an
> XFlush(dpy) call before each sleep().
>
> --
> M=E5ns Rullg=E5rd
> m...@mansr.com
Thanks , would you mind explaining it a bit more so I don't mess up
in future?
| |
| John Tsiombikas 2007-12-16, 1:26 pm |
| On 2007-12-16, Tomdumon@gmail.com <Tomdumon@gmail.com> wrote:
>
>
> Thanks , would you mind explaining it a bit more so I don't mess up
> in future?
Xlib implements an asyncronous communication model with the X server.
When you call an Xlib function that corresponds to an X11 protocol
request, that request is appended to a local request buffer. Then when
enough of them have been collected, they are transmited all together to
the X server.
For that reason, two syncronization functions are provided by Xlib:
XFlush, and XSync.
XFlush flush the request buffer, forcing their transmission, and return
to the caller.
XSync will flush the buffer, then wait for acknowledgment from the X
server that all the requests have been processed, before returning.
Make sure to not call these two functions, unless absolutely necessary,
as they will slow down things considerably (especially XSync).
--
John Tsiombikas (Nuclear / Mindlapse)
http://nuclear.sdf-eu.org/
|
|
|
|
|