slock-auto-timeout.1.4.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) 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 lock **locks, int nscreens,
           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), &ksym, 0);
           50 -                        if (IsKeypadKey(ksym)) {
           51 -                                if (ksym == XK_KP_Enter)
           52 -                                        ksym = XK_Return;
           53 -                                else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
           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: %s\n", strerror(errno));
           68 -                                else
           69 -                                        running = !!strcmp(inputhash, hash);
           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, sizeof(buf), &ksym, 0);
           81 +                                if (IsKeypadKey(ksym)) {
           82 +                                        if (ksym == XK_KP_Enter)
           83 +                                                ksym = XK_Return;
           84 +                                        else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
           85 +                                                ksym = (ksym - XK_KP_0) + XK_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, hash)))
          114 +                                                fprintf(stderr, "slock: crypt: %s\n", strerror(errno));
          115 +                                        else
          116 +                                                running = !!strcmp(inputhash, hash);
          117 +                                        if (running) {
          118 +                                                XBell(dpy, 100);
          119 +                                                failure = 1;
          120 +                                        }
          121 +                                        explicit_bzero(&passwd, sizeof(passwd));
          122 +                                        len = 0;
          123 +                                        break;
          124 +                                case XK_Escape:
          125 +                                        explicit_bzero(&passwd, sizeof(passwd));
          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, buf, num);
          136 +                                                len += num;
          137 +                                        }
          138 +                                        break;
          139                                  }
          140 -                                break;
          141 -                        }
          142 -                        color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
          143 -                        if (running && oldc != color) {
          144 -                                for (screen = 0; screen < nscreens; screen++) {
          145 -                                        XSetWindowBackground(dpy,
          146 -                                                             locks[screen]->win,
          147 -                                                             locks[screen]->colors[color]);
          148 -                                        XClearWindow(dpy, locks[screen]->win);
          149 +                                color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
          150 +                                if (running && oldc != color) {
          151 +                                        for (screen = 0; screen < nscreens; screen++) {
          152 +                                                XSetWindowBackground(dpy,
          153 +                                                                     locks[screen]->win,
          154 +                                                                     locks[screen]->colors[color]);
          155 +                                                XClearWindow(dpy, locks[screen]->win);
          156 +                                        }
          157 +                                        oldc = color;
          158                                  }
          159 -                                oldc = color;
          160 -                        }
          161 -                } else if (rr->active && ev.type == rr->evbase + RRScreenChangeNotify) {
          162 -                        rre = (XRRScreenChangeNotifyEvent*)&ev;
          163 -                        for (screen = 0; screen < nscreens; screen++) {
          164 -                                if (locks[screen]->win == rre->window) {
          165 -                                        XResizeWindow(dpy, locks[screen]->win,
          166 -                                                      rre->width, rre->height);
          167 -                                        XClearWindow(dpy, locks[screen]->win);
          168 +                        } else if (rr->active && ev.type == rr->evbase + RRScreenChangeNotify) {
          169 +                                rre = (XRRScreenChangeNotifyEvent*)&ev;
          170 +                                for (screen = 0; screen < nscreens; screen++) {
          171 +                                        if (locks[screen]->win == rre->window) {
          172 +                                                XResizeWindow(dpy, locks[screen]->win,
          173 +                                                              rre->width, rre->height);
          174 +                                                XClearWindow(dpy, locks[screen]->win);
          175 +                                        }
          176                                  }
          177 +                        } else for (screen = 0; screen < nscreens; screen++)
          178 +                                XRaiseWindow(dpy, locks[screen]->win);
          179 +                }
          180 +                
          181 +                time_t currenttime;
          182 +                time(&currenttime);
          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 screen)
          196          XSetWindowAttributes wa;
          197          Cursor invisible;
          198  
          199 +        time(&lasttouched);
          200 +
          201          if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock))))
          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