From e5a9d8539b2ec56aacb1db455a4235e4d1ff41ca Mon Sep 17 00:00:00 2001 From: bakkeby Date: Thu, 9 Sep 2021 11:54:37 +0200 Subject: [PATCH] Adding auto-timeout patch --- README.md | 5 ++++- config.def.h | 11 +++++++++++ patches.def.h | 5 +++++ slock.c | 43 ++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 34f0e86bbc8126a716852b2d05b9d259d58aa879..fac9c14122fc444af856797c17ef94d693a71037 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ slock tool, how to install it and how it works. ### Changelog: -2021-09-09 - Added the failure-command and secret-password patches +2021-09-09 - Added the auto-timeout, failure-command and secret-password patches 2021-06-08 - Added the color message patch @@ -44,6 +44,9 @@ slock tool, how to install it and how it works. - enables transparency for slock - intended to be combined with a compositor that can blur the transparent background + - [auto-timeout](https://tools.suckless.org/slock/patches/auto-timeout/) + - allows for a command to be executed after a specified time of inactivity + - [blur_pixelated_screen](https://tools.suckless.org/slock/patches/blur-pixelated-screen/) - sets the lockscreen picture to a blured or pixelated screenshot diff --git a/config.def.h b/config.def.h index c009a752dbad843325d52bd6baba15cc483b5aae..84d88c3da0a91b12f77ab7ddea2edee4c112eab2 100644 --- a/config.def.h +++ b/config.def.h @@ -56,6 +56,17 @@ static const float alpha = 0.9; /* treat a cleared input like a wrong password (color) */ static const int failonclear = 1; +#if AUTO_TIMEOUT_PATCH +/* length of time (seconds) until */ +static const int timeoffset = 60; + +/* should [command] be run only once? */ +static const int runonce = 0; + +/* command to be run after [time] has passed */ +static const char *command = "doas poweroff"; +#endif // AUTO_TIMEOUT_PATCH + #if FAILURE_COMMAND_PATCH /* number of failed password attempts until failcommand is executed. Set to 0 to disable */ diff --git a/patches.def.h b/patches.def.h index ff68ce80b56d2f4f708ba1db56ff3fcde4ea50ec..7d40cc247bea461503c835a3ad162d4b37225906 100644 --- a/patches.def.h +++ b/patches.def.h @@ -16,6 +16,11 @@ */ #define ALPHA_PATCH 0 +/* This patch allows for a command to be executed after a specified time of inactivity. + * https://tools.suckless.org/slock/patches/auto-timeout/ + */ +#define AUTO_TIMEOUT_PATCH 0 + /* This patch sets the lockscreen picture to a blured or pixelated screenshot. * This patch depends on the Imlib2 library, uncomment the relevant line in * config.mk when enabling this patch. diff --git a/slock.c b/slock.c index bb8efa53d729799c4cd20f321e0186abc9e297f8..55a3bdf84c18b56b84d2407db140a1ebc0ffb7a0 100644 --- a/slock.c +++ b/slock.c @@ -33,9 +33,9 @@ #if MEDIAKEYS_PATCH #include #endif // MEDIAKEYS_PATCH -#if QUICKCANCEL_PATCH +#if QUICKCANCEL_PATCH || AUTO_TIMEOUT_PATCH #include -#endif // QUICKCANCEL_PATCH +#endif // QUICKCANCEL_PATCH / AUTO_TIMEOUT_PATCH #if DPMS_PATCH #include #endif // DPMS_PATCH @@ -48,6 +48,10 @@ char *argv0; int failtrack = 0; #endif // FAILURE_COMMAND_PATCH +#if AUTO_TIMEOUT_PATCH +static time_t lasttouched; +int runflag = 0; +#endif // AUTO_TIMEOUT_PATCH #if QUICKCANCEL_PATCH static time_t locktime; #endif // QUICKCANCEL_PATCH @@ -193,6 +197,9 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, #endif // PAMAUTH_PATCH int num, screen, running, failure, oldc; unsigned int len, color; + #if AUTO_TIMEOUT_PATCH + time_t currenttime; + #endif // AUTO_TIMEOUT_PATCH #if CAPSCOLOR_PATCH int caps; unsigned int indicators; @@ -213,11 +220,23 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, caps = indicators & 1; #endif // CAPSCOLOR_PATCH - while (running && !XNextEvent(dpy, &ev)) { + #if AUTO_TIMEOUT_PATCH + while (running) + #else + while (running && !XNextEvent(dpy, &ev)) + #endif // AUTO_TIMEOUT_PATCH + { + #if AUTO_TIMEOUT_PATCH + while (XPending(dpy)) { + XNextEvent(dpy, &ev); + #endif // AUTO_TIMEOUT_PATCH #if QUICKCANCEL_PATCH running = !((time(NULL) - locktime < timetocancel) && (ev.type == MotionNotify)); #endif // QUICKCANCEL_PATCH if (ev.type == KeyPress) { + #if AUTO_TIMEOUT_PATCH + time(&lasttouched); + #endif // AUTO_TIMEOUT_PATCH explicit_bzero(&buf, sizeof(buf)); num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0); if (IsKeypadKey(ksym)) { @@ -394,6 +413,20 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens, for (screen = 0; screen < nscreens; screen++) XRaiseWindow(dpy, locks[screen]->win); } + + #if AUTO_TIMEOUT_PATCH + } + + time(¤ttime); + + if (currenttime >= lasttouched + timeoffset) { + if (!runonce || !runflag) { + runflag = 1; + system(command); + } + lasttouched = currenttime; + } + #endif // AUTO_TIMEOUT_PATCH } } @@ -407,6 +440,10 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen) XSetWindowAttributes wa; Cursor invisible; + #if AUTO_TIMEOUT_PATCH + time(&lasttouched); + #endif // AUTO_TIMEOUT_PATCH + if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock)))) return NULL;