slock-auto-timeout-20221002-35633d4.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-20221002-35633d4.diff (1932B)
       ---
            1 diff --git a/config.def.h b/config.def.h
            2 index 9855e21..354980b 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 [command] is executed */
           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 [timeoffset] seconds has passed */
           17 +static const char *command = "doas poweroff";
           18 diff --git a/slock.c b/slock.c
           19 index 5ae738c..7644bd9 100644
           20 --- a/slock.c
           21 +++ b/slock.c
           22 @@ -19,11 +19,16 @@
           23  #include <X11/Xlib.h>
           24  #include <X11/Xutil.h>
           25  
           26 +#include <time.h>
           27 +
           28  #include "arg.h"
           29  #include "util.h"
           30  
           31  char *argv0;
           32  
           33 +time_t lasttouched;
           34 +int runflag = 0;
           35 +
           36  enum {
           37          INIT,
           38          INPUT,
           39 @@ -140,7 +145,9 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
           40          failure = 0;
           41          oldc = INIT;
           42  
           43 -        while (running && !XNextEvent(dpy, &ev)) {
           44 +    while (running) {
           45 +        while (XPending(dpy)) {
           46 +            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 @@ -217,6 +224,17 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
           51                                      XRaiseWindow(dpy, locks[screen]->win);
           52                      }
           53          }
           54 +
           55 +        time_t currenttime;
           56 +        time(&currenttime);
           57 +
           58 +        if (currenttime >= lasttouched + timeoffset){
           59 +            if (!runonce || !runflag){
           60 +                runflag = 1;
           61 +                system(command);
           62 +            }
           63 +        }
           64 +        }
           65  }
           66  
           67  static struct lock *
           68 @@ -229,6 +247,8 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
           69          XSetWindowAttributes wa;
           70          Cursor invisible;
           71  
           72 +    time(&lasttouched);
           73 +
           74          if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock))))
           75                  return NULL;
           76