| slock-auto-timeout.1.4.diff - sites - public wiki contents of suckless.org | |
| git clone git://git.suckless.org/sites | |
| Log | |
| Files | |
| Refs | |
| --- | |
| slock-auto-timeout.1.4.diff (5892B) | |
| --- | |
| 1 diff --git a/config.def.h b/config.def.h | |
| 2 index 9855e21..a1f636c 100644 | |
| 3 --- a/config.def.h | |
| 4 +++ b/config.def.h | |
| 5 @@ -10,3 +10,12 @@ static const char *colorname[NUMCOLS] = { | |
| 6 | |
| 7 /* treat a cleared input like a wrong password (color) */ | |
| 8 static const int failonclear = 1; | |
| 9 + | |
| 10 +/* length of time (seconds) until */ | |
| 11 +static const int timeoffset = 60; | |
| 12 + | |
| 13 +/* should [command] be run only once? */ | |
| 14 +static const int runonce = 0; | |
| 15 + | |
| 16 +/* command to be run after [time] has passed */ | |
| 17 +static const char *command = "doas poweroff" | |
| 18 diff --git a/slock b/slock | |
| 19 index 53777b4..7f65cd7 100755 | |
| 20 Binary files a/slock and b/slock differ | |
| 21 diff --git a/slock.c b/slock.c | |
| 22 index d2f0886..f6345bf 100644 | |
| 23 --- a/slock.c | |
| 24 +++ b/slock.c | |
| 25 @@ -19,11 +19,16 @@ | |
| 26 #include <X11/Xlib.h> | |
| 27 #include <X11/Xutil.h> | |
| 28 | |
| 29 +#include <time.h> | |
| 30 + | |
| 31 #include "arg.h" | |
| 32 #include "util.h" | |
| 33 | |
| 34 char *argv0; | |
| 35 | |
| 36 +time_t lasttouched; | |
| 37 +int runflag = 0; | |
| 38 + | |
| 39 enum { | |
| 40 INIT, | |
| 41 INPUT, | |
| 42 @@ -140,74 +145,89 @@ readpw(Display *dpy, struct xrandr *rr, struct loc… | |
| 43 failure = 0; | |
| 44 oldc = INIT; | |
| 45 | |
| 46 - while (running && !XNextEvent(dpy, &ev)) { | |
| 47 - if (ev.type == KeyPress) { | |
| 48 - explicit_bzero(&buf, sizeof(buf)); | |
| 49 - num = XLookupString(&ev.xkey, buf, sizeof(buf),… | |
| 50 - if (IsKeypadKey(ksym)) { | |
| 51 - if (ksym == XK_KP_Enter) | |
| 52 - ksym = XK_Return; | |
| 53 - else if (ksym >= XK_KP_0 && ksym <= XK_… | |
| 54 - ksym = (ksym - XK_KP_0) + XK_0; | |
| 55 - } | |
| 56 - if (IsFunctionKey(ksym) || | |
| 57 - IsKeypadKey(ksym) || | |
| 58 - IsMiscFunctionKey(ksym) || | |
| 59 - IsPFKey(ksym) || | |
| 60 - IsPrivateKeypadKey(ksym)) | |
| 61 - continue; | |
| 62 - switch (ksym) { | |
| 63 - case XK_Return: | |
| 64 - passwd[len] = '\0'; | |
| 65 - errno = 0; | |
| 66 - if (!(inputhash = crypt(passwd, hash))) | |
| 67 - fprintf(stderr, "slock: crypt: … | |
| 68 - else | |
| 69 - running = !!strcmp(inputhash, h… | |
| 70 - if (running) { | |
| 71 - XBell(dpy, 100); | |
| 72 - failure = 1; | |
| 73 + //while (running && !XNextEvent(dpy, &ev)) { | |
| 74 + while (running){ | |
| 75 + while (XPending(dpy)){ | |
| 76 + XNextEvent(dpy, &ev); | |
| 77 + if (ev.type == KeyPress) { | |
| 78 + time(&lasttouched); | |
| 79 + explicit_bzero(&buf, sizeof(buf)); | |
| 80 + num = XLookupString(&ev.xkey, buf, size… | |
| 81 + if (IsKeypadKey(ksym)) { | |
| 82 + if (ksym == XK_KP_Enter) | |
| 83 + ksym = XK_Return; | |
| 84 + else if (ksym >= XK_KP_0 && ksy… | |
| 85 + ksym = (ksym - XK_KP_0)… | |
| 86 } | |
| 87 - explicit_bzero(&passwd, sizeof(passwd)); | |
| 88 - len = 0; | |
| 89 - break; | |
| 90 - case XK_Escape: | |
| 91 - explicit_bzero(&passwd, sizeof(passwd)); | |
| 92 - len = 0; | |
| 93 - break; | |
| 94 - case XK_BackSpace: | |
| 95 - if (len) | |
| 96 - passwd[len--] = '\0'; | |
| 97 - break; | |
| 98 - default: | |
| 99 - if (num && !iscntrl((int)buf[0]) && | |
| 100 - (len + num < sizeof(passwd))) { | |
| 101 - memcpy(passwd + len, buf, num); | |
| 102 - len += num; | |
| 103 + if (IsFunctionKey(ksym) || | |
| 104 + IsKeypadKey(ksym) || | |
| 105 + IsMiscFunctionKey(ksym) || | |
| 106 + IsPFKey(ksym) || | |
| 107 + IsPrivateKeypadKey(ksym)) | |
| 108 + continue; | |
| 109 + switch (ksym) { | |
| 110 + case XK_Return: | |
| 111 + passwd[len] = '\0'; | |
| 112 + errno = 0; | |
| 113 + if (!(inputhash = crypt(passwd,… | |
| 114 + fprintf(stderr, "slock:… | |
| 115 + else | |
| 116 + running = !!strcmp(inpu… | |
| 117 + if (running) { | |
| 118 + XBell(dpy, 100); | |
| 119 + failure = 1; | |
| 120 + } | |
| 121 + explicit_bzero(&passwd, sizeof(… | |
| 122 + len = 0; | |
| 123 + break; | |
| 124 + case XK_Escape: | |
| 125 + explicit_bzero(&passwd, sizeof(… | |
| 126 + len = 0; | |
| 127 + break; | |
| 128 + case XK_BackSpace: | |
| 129 + if (len) | |
| 130 + passwd[len--] = '\0'; | |
| 131 + break; | |
| 132 + default: | |
| 133 + if (num && !iscntrl((int)buf[0]… | |
| 134 + (len + num < sizeof(passwd)… | |
| 135 + memcpy(passwd + len, bu… | |
| 136 + len += num; | |
| 137 + } | |
| 138 + break; | |
| 139 } | |
| 140 - break; | |
| 141 - } | |
| 142 - color = len ? INPUT : ((failure || failonclear)… | |
| 143 - if (running && oldc != color) { | |
| 144 - for (screen = 0; screen < nscreens; scr… | |
| 145 - XSetWindowBackground(dpy, | |
| 146 - locks[scre… | |
| 147 - locks[scre… | |
| 148 - XClearWindow(dpy, locks[screen]… | |
| 149 + color = len ? INPUT : ((failure || fail… | |
| 150 + if (running && oldc != color) { | |
| 151 + for (screen = 0; screen < nscre… | |
| 152 + XSetWindowBackground(dp… | |
| 153 + lo… | |
| 154 + lo… | |
| 155 + XClearWindow(dpy, locks… | |
| 156 + } | |
| 157 + oldc = color; | |
| 158 } | |
| 159 - oldc = color; | |
| 160 - } | |
| 161 - } else if (rr->active && ev.type == rr->evbase + RRScre… | |
| 162 - rre = (XRRScreenChangeNotifyEvent*)&ev; | |
| 163 - for (screen = 0; screen < nscreens; screen++) { | |
| 164 - if (locks[screen]->win == rre->window) { | |
| 165 - XResizeWindow(dpy, locks[screen… | |
| 166 - rre->width, rre->… | |
| 167 - XClearWindow(dpy, locks[screen]… | |
| 168 + } else if (rr->active && ev.type == rr->evbase … | |
| 169 + rre = (XRRScreenChangeNotifyEvent*)&ev; | |
| 170 + for (screen = 0; screen < nscreens; scr… | |
| 171 + if (locks[screen]->win == rre->… | |
| 172 + XResizeWindow(dpy, lock… | |
| 173 + rre->widt… | |
| 174 + XClearWindow(dpy, locks… | |
| 175 + } | |
| 176 } | |
| 177 + } else for (screen = 0; screen < nscreens; scre… | |
| 178 + XRaiseWindow(dpy, locks[screen]->win); | |
| 179 + } | |
| 180 + | |
| 181 + time_t currenttime; | |
| 182 + time(¤ttime); | |
| 183 + | |
| 184 + if (currenttime >= lasttouched + timeoffset){ | |
| 185 + if (!runonce || !runflag){ | |
| 186 + runflag = 1; | |
| 187 + system(command); | |
| 188 } | |
| 189 - } else for (screen = 0; screen < nscreens; screen++) | |
| 190 - XRaiseWindow(dpy, locks[screen]->win); | |
| 191 + } | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 @@ -221,6 +241,8 @@ lockscreen(Display *dpy, struct xrandr *rr, int scre… | |
| 196 XSetWindowAttributes wa; | |
| 197 Cursor invisible; | |
| 198 | |
| 199 + time(&lasttouched); | |
| 200 + | |
| 201 if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct … | |
| 202 return NULL; | |
| 203 | |
| 204 diff --git a/slock.o b/slock.o | |
| 205 index 4bb0bc2..286f838 100644 | |
| 206 Binary files a/slock.o and b/slock.o differ |