Browse Source

added XRaiseWindow workaround when new clients are launched

master
anselm@garbe.us 13 years ago
parent
commit
d6e9e7d9e3
  1. 2
      LICENSE
  2. 72
      slock.c

2
LICENSE

@ -1,6 +1,6 @@
MIT/X Consortium License MIT/X Consortium License
© 2006-2008 Anselm R Garbe <garbeam at gmail dot com> © 2006-2012 Anselm R Garbe <anselm@garbe.us>
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),

72
slock.c

@ -22,25 +22,23 @@
#include <bsd_auth.h> #include <bsd_auth.h>
#endif #endif
struct st_lock { typedef struct {
int screen; int screen;
Window root, w; Window root, win;
Pixmap pmap; Pixmap pmap;
}; } Lock;
extern const char *__progname; static Lock **locks;
static int nscreens;
static Bool running = True;
static void static void
die(const char *errstr, ...) { die(const char *errstr, ...) {
va_list ap; va_list ap;
fprintf(stderr, "%s: ", __progname);
va_start(ap, errstr); va_start(ap, errstr);
vfprintf(stderr, errstr, ap); vfprintf(stderr, errstr, ap);
va_end(ap); va_end(ap);
fprintf(stderr, "\n");
fflush(stderr);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -52,7 +50,7 @@ getpw(void) { /* only run as root */
pw = getpwuid(getuid()); pw = getpwuid(getuid());
if(!pw) if(!pw)
die("cannot retrieve password entry (make sure to suid or sgid slock)"); die("slock: cannot retrieve password entry (make sure to suid or sgid slock)");
endpwent(); endpwent();
rval = pw->pw_passwd; rval = pw->pw_passwd;
@ -69,7 +67,7 @@ getpw(void) { /* only run as root */
/* drop privileges */ /* drop privileges */
if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0)
die("cannot drop privileges"); die("slock: cannot drop privileges");
return rval; return rval;
} }
#endif #endif
@ -82,10 +80,8 @@ readpw(Display *dpy, const char *pws)
#endif #endif
{ {
char buf[32], passwd[256]; char buf[32], passwd[256];
int num; int num, screen;
unsigned int len; unsigned int len;
Bool running = True;
KeySym ksym; KeySym ksym;
XEvent ev; XEvent ev;
@ -96,7 +92,6 @@ readpw(Display *dpy, const char *pws)
* had been removed and you can set it with "xset" or some other * had been removed and you can set it with "xset" or some other
* utility. This way the user can easily set a customized DPMS * utility. This way the user can easily set a customized DPMS
* timeout. */ * timeout. */
while(running && !XNextEvent(dpy, &ev)) { while(running && !XNextEvent(dpy, &ev)) {
if(ev.type == KeyPress) { if(ev.type == KeyPress) {
buf[0] = 0; buf[0] = 0;
@ -119,7 +114,7 @@ readpw(Display *dpy, const char *pws)
#else #else
running = strcmp(crypt(passwd, pws), pws); running = strcmp(crypt(passwd, pws), pws);
#endif #endif
if (running != 0) if(running != False)
XBell(dpy, 100); XBell(dpy, 100);
len = 0; len = 0;
break; break;
@ -138,27 +133,28 @@ readpw(Display *dpy, const char *pws)
break; break;
} }
} }
else for(screen = 0; screen < nscreens; screen++)
XMapRaised(dpy, locks[screen]->win);
} }
} }
static void static void
unlockscreen(Display *dpy, struct st_lock *lock) { unlockscreen(Display *dpy, Lock *lock) {
if(dpy == NULL || lock == NULL) if(dpy == NULL || lock == NULL)
return; return;
XUngrabPointer(dpy, CurrentTime); XUngrabPointer(dpy, CurrentTime);
XFreePixmap(dpy, lock->pmap); XFreePixmap(dpy, lock->pmap);
XDestroyWindow(dpy, lock->w); XDestroyWindow(dpy, lock->win);
free(lock); free(lock);
} }
static struct st_lock * static Lock *
lockscreen(Display *dpy, int screen) { lockscreen(Display *dpy, int screen) {
char curs[] = {0, 0, 0, 0, 0, 0, 0, 0}; char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned int len; unsigned int len;
struct st_lock *lock; Lock *lock;
Bool running = True;
XColor black, dummy; XColor black, dummy;
XSetWindowAttributes wa; XSetWindowAttributes wa;
Cursor invisible; Cursor invisible;
@ -166,7 +162,7 @@ lockscreen(Display *dpy, int screen) {
if(dpy == NULL || screen < 0) if(dpy == NULL || screen < 0)
return NULL; return NULL;
lock = malloc(sizeof(struct st_lock)); lock = malloc(sizeof(Lock));
if(lock == NULL) if(lock == NULL)
return NULL; return NULL;
@ -177,21 +173,21 @@ lockscreen(Display *dpy, int screen) {
/* init */ /* init */
wa.override_redirect = 1; wa.override_redirect = 1;
wa.background_pixel = BlackPixel(dpy, lock->screen); wa.background_pixel = BlackPixel(dpy, lock->screen);
lock->w = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen), lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
0, DefaultDepth(dpy, lock->screen), CopyFromParent, 0, DefaultDepth(dpy, lock->screen), CopyFromParent,
DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa); DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), "black", &black, &dummy); XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), "black", &black, &dummy);
lock->pmap = XCreateBitmapFromData(dpy, lock->w, curs, 8, 8); lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &black, &black, 0, 0); invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &black, &black, 0, 0);
XDefineCursor(dpy, lock->w, invisible); XDefineCursor(dpy, lock->win, invisible);
XMapRaised(dpy, lock->w); XMapRaised(dpy, lock->win);
for(len = 1000; len; len--) { for(len = 1000; len; len--) {
if(XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, if(XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess) GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
break; break;
usleep(1000); usleep(1000);
} }
if((running = running && (len > 0))) { if(running && (len > 0)) {
for(len = 1000; len; len--) { for(len = 1000; len; len--) {
if(XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) if(XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
== GrabSuccess) == GrabSuccess)
@ -211,24 +207,17 @@ lockscreen(Display *dpy, int screen) {
static void static void
usage(void) { usage(void) {
fprintf(stderr, "usage: %s -v", __progname); fprintf(stderr, "usage: slock [-v]");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
static int
xerrordummy(Display *dpy, XErrorEvent *ee) {
return 0;
}
int int
main(int argc, char **argv) { main(int argc, char **argv) {
#ifndef HAVE_BSD_AUTH #ifndef HAVE_BSD_AUTH
const char *pws; const char *pws;
#endif #endif
Display *dpy; Display *dpy;
int nscreens, screen; int screen;
struct st_lock **locks;
if((argc == 2) && !strcmp("-v", argv[1])) if((argc == 2) && !strcmp("-v", argv[1]))
die("slock-%s, © 2006-2012 Anselm R Garbe", VERSION); die("slock-%s, © 2006-2012 Anselm R Garbe", VERSION);
@ -236,25 +225,21 @@ main(int argc, char **argv) {
usage(); usage();
if(!getpwuid(getuid())) if(!getpwuid(getuid()))
die("no passwd entry for you"); die("slock: no passwd entry for you");
#ifndef HAVE_BSD_AUTH #ifndef HAVE_BSD_AUTH
pws = getpw(); pws = getpw();
#endif #endif
if(!(dpy = XOpenDisplay(0))) if(!(dpy = XOpenDisplay(0)))
die("cannot open display"); die("slock: cannot open display");
/* prevent default error handler to take over */
XSetErrorHandler(xerrordummy);
/* Get the number of screens in display "dpy" and blank them all. */ /* Get the number of screens in display "dpy" and blank them all. */
nscreens = ScreenCount(dpy); nscreens = ScreenCount(dpy);
locks = malloc(sizeof(struct st_lock *) * nscreens); locks = malloc(sizeof(Lock *) * nscreens);
if(locks == NULL) if(locks == NULL)
die("malloc: %s", strerror(errno)); die("slock: malloc: %s", strerror(errno));
for(screen = 0; screen < nscreens; screen++) for(screen = 0; screen < nscreens; screen++)
locks[screen] = lockscreen(dpy, screen); locks[screen] = lockscreen(dpy, screen);
XSync(dpy, False); XSync(dpy, False);
/* Everything is now blank. Now wait for the correct password. */ /* Everything is now blank. Now wait for the correct password. */
@ -269,7 +254,6 @@ main(int argc, char **argv) {
unlockscreen(dpy, locks[screen]); unlockscreen(dpy, locks[screen]);
free(locks); free(locks);
XCloseDisplay(dpy); XCloseDisplay(dpy);
return 0; return 0;

Loading…
Cancel
Save