Try three times to grab keyboard.

I should probably make it try forever.  Who knows.  Maybe this is a bad idea.
This commit is contained in:
Neale Pickett 2008-04-18 11:43:12 -06:00
parent 1d7330dce4
commit 9c41538e70
3 changed files with 31 additions and 15 deletions

View File

@ -40,22 +40,24 @@ main(int argc, char *argv[])
char obuf[4096]; char obuf[4096];
int obuflen = 0; int obuflen = 0;
struct pollfd fds[2]; struct pollfd fds[2];
int i;
if (! (display = XOpenDisplay(NULL))) raise("cannot open display"); if (! (display = XOpenDisplay(NULL))) raise("cannot open display");
root = DefaultRootWindow(display); root = DefaultRootWindow(display);
(void)XUngrabPointer(display, CurrentTime); for (i = 0; i < 3; ) {
if (GrabSuccess != XGrabPointer(display, root, if ((! (i & 1)) && (GrabSuccess == XGrabKeyboard(display, root,
False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, True,
GrabModeAsync, GrabModeAsync, GrabModeAsync, GrabModeAsync,
None, None, CurrentTime)) { CurrentTime))) {
raise("cannot grab pointer"); i |= 1;
} }
(void)XUngrabKeyboard(display, CurrentTime); if ((! (i & 2)) && (GrabSuccess == XGrabPointer(display, root,
if (GrabSuccess != XGrabKeyboard(display, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
True, GrabModeAsync, GrabModeAsync,
GrabModeAsync, GrabModeAsync, None, None, CurrentTime))) {
CurrentTime)) { i |= 2;
raise("cannot grab keyboard"); }
(void)poll(NULL, 0, 100);
} }
fds[0].fd = STDOUT_FILENO; fds[0].fd = STDOUT_FILENO;

16
xss.c
View File

@ -21,6 +21,7 @@
#include <stdio.h> #include <stdio.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/scrnsaver.h> #include <X11/extensions/scrnsaver.h>
#include "obj.h" #include "obj.h"
@ -47,18 +48,31 @@ main(int argc, char *argv[])
try { try {
int ss_event, ss_error; int ss_event, ss_error;
int screen;
XEvent event; XEvent event;
Window root;
if (! (display = XOpenDisplay(NULL))) raise("cannot open display"); if (! (display = XOpenDisplay(NULL))) raise("cannot open display");
screen = DefaultScreen(display);
if (! XScreenSaverQueryExtension(display, &ss_event, &ss_error)) { if (! XScreenSaverQueryExtension(display, &ss_event, &ss_error)) {
raise("X server does not support MIT-SCREEN-SAVER extension."); raise("X server does not support MIT-SCREEN-SAVER extension.");
} }
XScreenSaverSelectInput(display, DefaultRootWindow(display), ScreenSaverNotifyMask); if (! XScreenSaverRegister(display, screen, (XID)getpid(), XA_INTEGER)) {
raise("cannot register screen saver, is another one already running?");
}
root = RootWindow(display, screen);
XScreenSaverSelectInput(display, root, ScreenSaverNotifyMask);
while (! XNextEvent(display, &event)) { while (! XNextEvent(display, &event)) {
if (ss_event == event.type) { if (ss_event == event.type) {
XScreenSaverNotifyEvent *sevent = (XScreenSaverNotifyEvent *)&event; XScreenSaverNotifyEvent *sevent = (XScreenSaverNotifyEvent *)&event;
if (ScreenSaverOn == sevent->state) { if (ScreenSaverOn == sevent->state) {
#if 0
XScreenSaverInfo info;
if (XScreenSaverQueryInfo(display, (Drawable)root, &info)) {
XLowerWindow(display, info.window);
}
#endif
if (! child) { if (! child) {
child = fork(); child = fork();
if (0 == child) { if (0 == child) {

View File

@ -58,8 +58,8 @@ main(int argc, char * const argv[])
CopyFromParent, InputOutput, CopyFromParent, CopyFromParent, InputOutput, CopyFromParent,
CWOverrideRedirect | CWBackPixel, CWOverrideRedirect | CWBackPixel,
&wa); &wa);
XAllocColor(display, DefaultColormap(display, screen), &black);
pmap = XCreateBitmapFromData(display, w, "\0", 1, 1); pmap = XCreateBitmapFromData(display, w, "\0", 1, 1);
black.pixel = BlackPixel(display, screen);
invisible = XCreatePixmapCursor(display, pmap, pmap, &black, &black, 0, 0); invisible = XCreatePixmapCursor(display, pmap, pmap, &black, &black, 0, 0);
XDefineCursor(display, w, invisible); XDefineCursor(display, w, invisible);
XMapRaised(display, w); XMapRaised(display, w);