slock-multi-shapes-1.5.diff (5707B)
1 diff --git a/config.def.h b/config.def.h 2 index 9855e21..e681e8b 100644 3 --- a/config.def.h 4 +++ b/config.def.h 5 @@ -3,10 +3,21 @@ static const char *user = "nobody"; 6 static const char *group = "nogroup"; 7 8 static const char *colorname[NUMCOLS] = { 9 - [INIT] = "black", /* after initialization */ 10 + [BG] = "black", /* background */ 11 + [INIT] = "#4f525c", /* after initialization */ 12 [INPUT] = "#005577", /* during input */ 13 [FAILED] = "#CC3333", /* wrong password */ 14 }; 15 16 /* treat a cleared input like a wrong password (color) */ 17 static const int failonclear = 1; 18 + 19 +/* 20 +* Shapes: 21 +* 0: square 22 +* 1: circle 23 +*/ 24 +static const int shape = 0; 25 +/* size of square in px */ 26 +static const int shapesize = 50; 27 +static const int shapegap = 35; 28 diff --git a/slock.c b/slock.c 29 index b2f14e3..501b3be 100644 30 --- a/slock.c 31 +++ b/slock.c 32 @@ -25,6 +25,7 @@ 33 char *argv0; 34 35 enum { 36 + BG, 37 INIT, 38 INPUT, 39 FAILED, 40 @@ -36,6 +37,8 @@ struct lock { 41 Window root, win; 42 Pixmap pmap; 43 unsigned long colors[NUMCOLS]; 44 + GC gc; 45 + XRRScreenResources *rrsr; 46 }; 47 48 struct xrandr { 49 @@ -124,13 +127,69 @@ gethash(void) 50 return hash; 51 } 52 53 +static void 54 +draw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, 55 + unsigned int color, unsigned int len) 56 +{ 57 + int screen, crtc; 58 + XRRCrtcInfo* rrci; 59 + 60 + if (rr->active) { 61 + for (screen = 0; screen < nscreens; screen++) { 62 + XSetWindowBackground(dpy, locks[screen]->win,locks[screen]->colors[BG]); 63 + XClearWindow(dpy, locks[screen]->win); 64 + XSetForeground(dpy, locks[screen]->gc, locks[screen]->colors[color]); 65 + for (crtc = 0; crtc < locks[screen]->rrsr->ncrtc; ++crtc) { 66 + rrci = XRRGetCrtcInfo(dpy, 67 + locks[screen]->rrsr, 68 + locks[screen]->rrsr->crtcs[crtc]); 69 + /* skip disabled crtc */ 70 + if (rrci->noutput > 0) { 71 + int leftBound = rrci->x + (rrci->width - len * shapesize - (len - 1) * shapegap) / 2; 72 + for (int shapei = 0; shapei < len; shapei++) { 73 + int x = leftBound + shapei * (shapesize + shapegap); 74 + if (shape == 0) { 75 + XFillRectangle(dpy, 76 + locks[screen]->win, 77 + locks[screen]->gc, 78 + x, 79 + rrci->y + (rrci->height - shapesize) / 2, 80 + shapesize, 81 + shapesize); 82 + } else if (shape == 1) { 83 + XFillArc(dpy, 84 + locks[screen]->win, 85 + locks[screen]->gc, 86 + x, 87 + rrci->y + (rrci->height - shapesize) / 2, 88 + shapesize, 89 + shapesize, 90 + 0, 91 + 360 * 64); 92 + } 93 + } 94 + 95 + } 96 + XRRFreeCrtcInfo(rrci); 97 + } 98 + } 99 + } else { 100 + for (screen = 0; screen < nscreens; screen++) { 101 + XSetWindowBackground(dpy, 102 + locks[screen]->win, 103 + locks[screen]->colors[color]); 104 + XClearWindow(dpy, locks[screen]->win); 105 + } 106 + } 107 +} 108 + 109 static void 110 readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, 111 const char *hash) 112 { 113 XRRScreenChangeNotifyEvent *rre; 114 char buf[32], passwd[256], *inputhash; 115 - int num, screen, running, failure, oldc; 116 + int num, screen, running, failure, oldc, oldlen; 117 unsigned int len, color; 118 KeySym ksym; 119 XEvent ev; 120 @@ -139,6 +198,7 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, 121 running = 1; 122 failure = 0; 123 oldc = INIT; 124 + oldlen = 0; 125 126 while (running && !XNextEvent(dpy, &ev)) { 127 if (ev.type == KeyPress) { 128 @@ -188,14 +248,14 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, 129 break; 130 } 131 color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT); 132 - if (running && oldc != color) { 133 - for (screen = 0; screen < nscreens; screen++) { 134 - XSetWindowBackground(dpy, 135 - locks[screen]->win, 136 - locks[screen]->colors[color]); 137 - XClearWindow(dpy, locks[screen]->win); 138 + if (running && (oldc != color || oldlen != len)) { 139 + int lenToUse = len; 140 + if (lenToUse < 1) { 141 + lenToUse = 1; 142 } 143 + draw(dpy, rr, locks, nscreens, color, lenToUse); 144 oldc = color; 145 + oldlen = len; 146 } 147 } else if (rr->active && ev.type == rr->evbase + RRScreenChangeNotify) { 148 rre = (XRRScreenChangeNotifyEvent*)&ev; 149 @@ -228,6 +288,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) 150 XColor color, dummy; 151 XSetWindowAttributes wa; 152 Cursor invisible; 153 + XGCValues gcvalues; 154 155 if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock)))) 156 return NULL; 157 @@ -243,7 +304,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) 158 159 /* init */ 160 wa.override_redirect = 1; 161 - wa.background_pixel = lock->colors[INIT]; 162 + wa.background_pixel = lock->colors[BG]; 163 lock->win = XCreateWindow(dpy, lock->root, 0, 0, 164 DisplayWidth(dpy, lock->screen), 165 DisplayHeight(dpy, lock->screen), 166 @@ -255,6 +316,10 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) 167 invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, 168 &color, &color, 0, 0); 169 XDefineCursor(dpy, lock->win, invisible); 170 + lock->gc = XCreateGC(dpy, lock->win, 0, &gcvalues); 171 + XSetForeground(dpy, lock->gc, lock->colors[INIT]); 172 + if (rr->active) 173 + lock->rrsr = XRRGetScreenResourcesCurrent(dpy, lock->root); 174 175 /* Try to grab mouse pointer *and* keyboard for 600ms, else fail the lock */ 176 for (i = 0, ptgrab = kbgrab = -1; i < 6; i++) { 177 @@ -388,6 +453,9 @@ main(int argc, char **argv) { 178 } 179 } 180 181 + /* draw the initial rectangle */ 182 + draw(dpy, &rr, locks, nscreens, INIT, 1); 183 + 184 /* everything is now blank. Wait for the correct password */ 185 readpw(dpy, &rr, locks, nscreens, hash); 186