fix xkeygrab latency bug, finally

This commit is contained in:
Neale Pickett 2012-03-01 17:40:12 -07:00
parent 92bb54aaa2
commit ebea23e5c0
1 changed files with 134 additions and 102 deletions

View File

@ -1,10 +1,11 @@
/* xkeygrab -- grab keyboard and mouse, writing typed lines to stdout /*
* xkeygrab -- grab keyboard and mouse, writing typed lines to stdout
* Copyright (C) 2008 Neale Pickett <neale@woozle.org> * Copyright (C) 2008 Neale Pickett <neale@woozle.org>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or
* it under the terms of the GNU General Public License as published by * modify it under the terms of the GNU General Public License as
* the Free Software Foundation, either version 3 of the License, or (at * published by the Free Software Foundation, either version 3 of the
* your option) any later version. * License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, but * This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of * WITHOUT ANY WARRANTY; without even the implied warranty of
@ -31,7 +32,7 @@ main(int argc, char *argv[])
Display *display = NULL; Display *display = NULL;
if (argc != 1) { if (argc != 1) {
(void)fprintf(stderr, "Usage: %s\n", argv[0]); (void) fprintf(stderr, "Usage: %s\n", argv[0]);
return 64; /* EX_USAGE */ return 64; /* EX_USAGE */
} }
@ -41,9 +42,28 @@ main(int argc, char *argv[])
int obuflen = 0; int obuflen = 0;
struct pollfd fds[2]; struct pollfd fds[2];
if (! (display = XOpenDisplay(NULL))) raise("cannot open display"); if (!(display = XOpenDisplay(NULL)))
raise("cannot open display");
root = DefaultRootWindow(display); root = DefaultRootWindow(display);
if (getenv("NOROOT")) {
int screen = DefaultScreen(display);
XSetWindowAttributes wa;
zero(wa);
wa.override_redirect = 1;
wa.background_pixel = BlackPixel(display, screen);
root = XCreateWindow(display, root,
0, 0,
50, 50, 0,
CopyFromParent, CopyFromParent,
CopyFromParent, CWBackPixel, &wa);
XMapWindow(display, root);
}
fds[0].fd = STDOUT_FILENO; fds[0].fd = STDOUT_FILENO;
fds[0].events = 0; fds[0].events = 0;
fds[1].fd = ConnectionNumber(display); fds[1].fd = ConnectionNumber(display);
@ -53,21 +73,26 @@ main(int argc, char *argv[])
int ret; int ret;
if (tograb) { if (tograb) {
if ((tograb & 1) && (GrabSuccess == XGrabKeyboard(display, root, if ((tograb & 1)
True, && (GrabSuccess ==
GrabModeAsync, GrabModeAsync, XGrabKeyboard(display, root, True, GrabModeAsync,
CurrentTime))) { GrabModeAsync, CurrentTime))) {
tograb |= 1; tograb |= 1;
} }
if ((tograb & 2) && (GrabSuccess == XGrabPointer(display, root, if ((tograb & 2)
False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, && (GrabSuccess ==
GrabModeAsync, GrabModeAsync, XGrabPointer(display, root, False,
None, None, CurrentTime))) { ButtonPressMask | ButtonReleaseMask |
PointerMotionMask, GrabModeAsync,
GrabModeAsync, None, None,
CurrentTime))) {
tograb |= 2; tograb |= 2;
} }
} }
ret = poll(fds, 2, (tograb?100:-1)); ret = poll(fds, 2, (tograb ? 100 : -1));
if (-1 == ret)
break;
if (fds[0].revents & POLLERR) { if (fds[0].revents & POLLERR) {
break; break;
@ -75,24 +100,29 @@ main(int argc, char *argv[])
if (fds[1].revents & POLLIN) { if (fds[1].revents & POLLIN) {
XEvent event; XEvent event;
(void)XNextEvent(display, &event); do {
(void) XNextEvent(display, &event);
if (KeyPress == event.type) { if (KeyPress == event.type) {
char buf[32]; char buf[32];
KeySym ksym; KeySym ksym;
int i; int i;
if (obuflen == sizeof(obuf)) continue; if (obuflen == sizeof(obuf))
i = XLookupString(&event.xkey, buf, sizeof(buf), &ksym, NULL); continue;
i = XLookupString(&event.xkey, buf, sizeof(buf),
&ksym, NULL);
switch (ksym) { switch (ksym) {
case XK_Return: case XK_Return:
if (obuflen) { if (obuflen) {
(void)write(STDOUT_FILENO, obuf, obuflen); (void) write(STDOUT_FILENO, obuf,
(void)write(STDOUT_FILENO, "\n", 1); obuflen);
(void) write(STDOUT_FILENO, "\n", 1);
obuflen = 0; obuflen = 0;
} }
break; break;
case XK_BackSpace: case XK_BackSpace:
if (obuflen) obuflen -= 1; if (obuflen)
obuflen -= 1;
break; break;
default: default:
if (1 == i) { if (1 == i) {
@ -108,22 +138,24 @@ main(int argc, char *argv[])
break; break;
} }
if (obuflen == sizeof(obuf)) { if (obuflen == sizeof(obuf)) {
(void)write(STDOUT_FILENO, obuf, obuflen); (void) write(STDOUT_FILENO, obuf, obuflen);
obuflen = 0; obuflen = 0;
} }
} }
} while (XPending(display));
} }
} }
} while (0); }
while (0);
if (display) { if (display) {
(void)XUngrabKeyboard(display, CurrentTime); (void) XUngrabKeyboard(display, CurrentTime);
(void)XUngrabPointer(display, CurrentTime); (void) XUngrabPointer(display, CurrentTime);
(void)XCloseDisplay(display); (void) XCloseDisplay(display);
} }
except { except {
(void)fprintf(stderr, "Error: %s\n", exception); (void) fprintf(stderr, "Error: %s\n", exception);
return 69; /* EX_UNAVAILABLE */ return 69; /* EX_UNAVAILABLE */
} }