Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions hhpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

static int gIdleTimeout = 1;
static int gVerbose = 0;
Expand Down Expand Up @@ -90,8 +91,11 @@ static void delay(time_t sec, long msec) {
sleep.tv_sec = sec;
sleep.tv_nsec = (msec % 1000) * 1000 * 1000;

if (nanosleep(&sleep, NULL) == -1) {
signalHandler(0);
while (nanosleep(&sleep, &sleep) != 0) {
if (errno == EINTR)
continue;
else
signalHandler(0);
}
}

Expand Down Expand Up @@ -158,6 +162,9 @@ static int grabPointer(Display *dpy, Window win, Cursor cursor, unsigned int mas
}

static void waitForMotion(Display *dpy, Window win, int timeout) {
struct timeval sleep;
sleep.tv_sec = (timeout <= 1) ? 3 : timeout;

int ready = 0;
int xfd = ConnectionNumber(dpy);

Expand Down Expand Up @@ -193,7 +200,13 @@ static void waitForMotion(Display *dpy, Window win, int timeout) {
* is interruptible by signals, which allows ctrl+c to work. If we
* were to just use XNextEvent() (which blocks), ctrl+c would not
* work. */
ready = select(xfd + 1, &fds, NULL, NULL, NULL);
if ((ready = select(xfd + 1, &fds, NULL, NULL, &sleep)) == -1) {
if (working) perror("hhpc: error while select()'ing");
}

if (ready == 0) {
if (gVerbose) fprintf(stderr, "hhpc: timeout\n");
}

if (ready > 0) {
if (gVerbose) fprintf(stderr, "hhpc: event received, ungrabbing and sleeping\n");
Expand All @@ -211,12 +224,6 @@ static void waitForMotion(Display *dpy, Window win, int timeout) {

delay(timeout, 0);
}
else if (ready == 0) {
if (gVerbose) fprintf(stderr, "hhpc: timeout\n");
}
else {
if (working) perror("hhpc: error while select()'ing");
}
}

XUngrabPointer(dpy, CurrentTime);
Expand Down