Blank the screen with color 0, add third color for failed logins

- Adds another color in config.def.h, COLOR_INIT
- Renames the colours from numerical ones to ones with meaningful names;
  COLOR_INPUT for when there is content in the input buffer and COLOR_EMPTY
  for when the input buffer has been cleared (backspaced or a failed attempt).
- Ensures XFreeColors frees the right number of colours. This is now derived
  from the size of `Lock->colors` rather than being an integer literal.
- Makes slock exhibit the behaviour described by Markus

The default colours are the same as the ones slock currently uses, with the
exception of the new color, which I have set to red, as it indicates someone
has either failed an attempt to unlock, or that they have entered input and
erased it all.
This commit is contained in:
David Phillips 2015-02-12 11:56:35 +13:00 committed by Markus Teich
parent a31b919111
commit f2ea92c3dd
2 changed files with 23 additions and 11 deletions

View file

@ -1,2 +1,5 @@
#define COLOR1 "black" static const char *colorname[NUMCOLS] = {
#define COLOR2 "#005577" "black", /* after initialization */
"#005577", /* during input */
"#CC3333", /* failed/cleared the input */
};

27
slock.c
View file

@ -22,13 +22,20 @@
#include <bsd_auth.h> #include <bsd_auth.h>
#endif #endif
enum {
INIT,
INPUT,
EMPTY,
NUMCOLS
};
#include "config.h" #include "config.h"
typedef struct { typedef struct {
int screen; int screen;
Window root, win; Window root, win;
Pixmap pmap; Pixmap pmap;
unsigned long colors[2]; unsigned long colors[NUMCOLS];
} Lock; } Lock;
static Lock **locks; static Lock **locks;
@ -162,12 +169,12 @@ readpw(Display *dpy, const char *pws)
} }
if (llen == 0 && len != 0) { if (llen == 0 && len != 0) {
for (screen = 0; screen < nscreens; screen++) { for (screen = 0; screen < nscreens; screen++) {
XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[1]); XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[INPUT]);
XClearWindow(dpy, locks[screen]->win); XClearWindow(dpy, locks[screen]->win);
} }
} else if (llen != 0 && len == 0) { } else if (llen != 0 && len == 0) {
for (screen = 0; screen < nscreens; screen++) { for (screen = 0; screen < nscreens; screen++) {
XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[0]); XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[EMPTY]);
XClearWindow(dpy, locks[screen]->win); XClearWindow(dpy, locks[screen]->win);
} }
} }
@ -185,7 +192,7 @@ unlockscreen(Display *dpy, Lock *lock)
return; return;
XUngrabPointer(dpy, CurrentTime); XUngrabPointer(dpy, CurrentTime);
XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, 2, 0); XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, NUMCOLS, 0);
XFreePixmap(dpy, lock->pmap); XFreePixmap(dpy, lock->pmap);
XDestroyWindow(dpy, lock->win); XDestroyWindow(dpy, lock->win);
@ -197,6 +204,7 @@ 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;
int i;
Lock *lock; Lock *lock;
XColor color, dummy; XColor color, dummy;
XSetWindowAttributes wa; XSetWindowAttributes wa;
@ -213,16 +221,17 @@ lockscreen(Display *dpy, int screen)
lock->root = RootWindow(dpy, lock->screen); lock->root = RootWindow(dpy, lock->screen);
for (i = 0; i < NUMCOLS; i++) {
XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy);
lock->colors[i] = color.pixel;
}
/* init */ /* init */
wa.override_redirect = 1; wa.override_redirect = 1;
wa.background_pixel = BlackPixel(dpy, lock->screen); wa.background_pixel = lock->colors[INIT];
lock->win = 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), COLOR2, &color, &dummy);
lock->colors[1] = color.pixel;
XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), COLOR1, &color, &dummy);
lock->colors[0] = color.pixel;
lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8); lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0); invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
XDefineCursor(dpy, lock->win, invisible); XDefineCursor(dpy, lock->win, invisible);