sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

slock-quickcancel-1.6.diff (2103B)


      1 From: Matthias Schoth <mschoth@gmail.com>
      2 Date: Sat, 11 Jul 2026 11:15:49 +0200
      3 Subject: [PATCH] Apply quickcancel
      4 
      5 Cancel slock by moving the mouse within a certain time-period after
      6 slock started. The time-period can be defined in seconds with the
      7 setting *timetocancel* in the config.h. This is useful if you forgot to
      8 disable `xautolock` during an activity that requires no input (e.g.
      9 reading text, watching video).
     10 
     11 Changes from the 1.4 version: use CLOCK_MONOTONIC instead of time(NULL)
     12 to prevent bypassing the lock by setting the system clock back.
     13 
     14 ---
     15  config.def.h | 3 +++
     16  slock.c      | 9 +++++++++
     17  2 files changed, 12 insertions(+)
     18 
     19 diff --git a/config.def.h b/config.def.h
     20 index 9855e21..e0bf95a 100644
     21 --- a/config.def.h
     22 +++ b/config.def.h
     23 @@ -10,3 +10,6 @@ static const char *colorname[NUMCOLS] = {
     24  
     25  /* treat a cleared input like a wrong password (color) */
     26  static const int failonclear = 1;
     27 +
     28 +/* time in seconds to cancel lock with mouse movement */
     29 +static const int timetocancel = 4;
     30 diff --git a/slock.c b/slock.c
     31 index f16781f..212b482 100644
     32 --- a/slock.c
     33 +++ b/slock.c
     34 @@ -13,6 +13,7 @@
     35  #include <stdio.h>
     36  #include <string.h>
     37  #include <unistd.h>
     38 +#include <time.h>
     39  #include <spawn.h>
     40  #include <sys/types.h>
     41  #include <X11/extensions/Xrandr.h>
     42 @@ -25,6 +26,8 @@
     43  
     44  char *argv0;
     45  
     46 +static struct timespec locktime;
     47 +
     48  enum {
     49  	INIT,
     50  	INPUT,
     51 @@ -142,6 +145,9 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
     52  	oldc = INIT;
     53  
     54  	while (running && !XNextEvent(dpy, &ev)) {
     55 +		struct timespec now;
     56 +		clock_gettime(CLOCK_MONOTONIC, &now);
     57 +		running = !((now.tv_sec - locktime.tv_sec < timetocancel) && (ev.type == MotionNotify));
     58  		if (ev.type == KeyPress) {
     59  			explicit_bzero(&buf, sizeof(buf));
     60  			num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
     61 @@ -280,6 +286,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
     62  				XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
     63  
     64  			XSelectInput(dpy, lock->root, SubstructureNotifyMask);
     65 +			clock_gettime(CLOCK_MONOTONIC, &locktime);
     66  			return lock;
     67  		}
     68  
     69 -- 
     70 2.23.0