sites

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

slock-visual-unlock-dpms-1.6.diff (3499B)


      1 diff --git a/config.def.h b/config.def.h
      2 index 9855e21..97a4842 100644
      3 --- a/config.def.h
      4 +++ b/config.def.h
      5 @@ -10,3 +10,8 @@ static const char *colorname[NUMCOLS] = {
      6  
      7  /* treat a cleared input like a wrong password (color) */
      8  static const int failonclear = 1;
      9 +
     10 +/* time in seconds before the monitor shuts down */
     11 +static int monitortime = 3;
     12 +/* time in seconds before the monitor shuts down, if visual_unlock is enabled */
     13 +static const int monitortime_vu = 0;
     14 diff --git a/slock.c b/slock.c
     15 index b5a9b04..c9ddfb2 100644
     16 --- a/slock.c
     17 +++ b/slock.c
     18 @@ -1,4 +1,5 @@
     19  /* See LICENSE file for license details. */
     20 +#include <X11/Xmd.h>
     21  #define _XOPEN_SOURCE 500
     22  #if HAVE_SHADOW_H
     23  #include <shadow.h>
     24 @@ -9,6 +10,7 @@
     25  #include <grp.h>
     26  #include <pwd.h>
     27  #include <stdarg.h>
     28 +#include <stdbool.h>
     29  #include <stdlib.h>
     30  #include <stdio.h>
     31  #include <string.h>
     32 @@ -16,6 +18,7 @@
     33  #include <spawn.h>
     34  #include <sys/types.h>
     35  #include <X11/extensions/Xrandr.h>
     36 +#include <X11/extensions/dpms.h>
     37  #include <X11/keysym.h>
     38  #include <X11/Xlib.h>
     39  #include <X11/Xutil.h>
     40 @@ -45,6 +48,8 @@ struct xrandr {
     41  	int errbase;
     42  };
     43  
     44 +bool visual_unlock = false;
     45 +
     46  #include "config.h"
     47  
     48  static void
     49 @@ -272,7 +277,8 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
     50  
     51  		/* input is grabbed: we can lock the screen */
     52  		if (ptgrab == GrabSuccess && kbgrab == GrabSuccess) {
     53 -			XMapRaised(dpy, lock->win);
     54 +			if (!visual_unlock)
     55 +				XMapRaised(dpy, lock->win);
     56  			if (rr->active)
     57  				XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
     58  
     59 @@ -301,7 +307,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
     60  static void
     61  usage(void)
     62  {
     63 -	die("usage: slock [-v] [cmd [arg ...]]\n");
     64 +	die("usage: slock [-u] [-v] [cmd [arg ...]]\n");
     65  }
     66  
     67  int
     68 @@ -315,14 +321,20 @@ main(int argc, char **argv) {
     69  	const char *hash;
     70  	Display *dpy;
     71  	int s, nlocks, nscreens;
     72 -
     73 -	ARGBEGIN {
     74 -	case 'v':
     75 -		puts("slock-"VERSION);
     76 -		return 0;
     77 -	default:
     78 -		usage();
     79 -	} ARGEND
     80 +	CARD16 standby, suspend, off;
     81 +	BOOL dpms_state;
     82 +
     83 +	if (argc > 1 && strcmp(argv[1], "-u") == 0) {
     84 +		visual_unlock = true;
     85 +	} else {
     86 +		ARGBEGIN {
     87 +		case 'v':
     88 +			puts("slock-"VERSION);
     89 +			return 0;
     90 +		default:
     91 +			usage();
     92 +		} ARGEND
     93 +	}
     94  
     95  	/* validate drop-user and -group */
     96  	errno = 0;
     97 @@ -375,6 +387,25 @@ main(int argc, char **argv) {
     98  	if (nlocks != nscreens)
     99  		return 1;
    100  
    101 +	/* DPMS magic to disable the monitor */
    102 +	if (visual_unlock) monitortime = monitortime_vu;
    103 +
    104 +	if (!DPMSCapable(dpy))
    105 +		die("slock: DPMSCapable failed\n");
    106 +	if (!DPMSInfo(dpy, &standby, &dpms_state))
    107 +		die("slock: DPMSInfo failed\n");
    108 +	if (!DPMSEnable(dpy) && !dpms_state)
    109 +		die("slock: DPMSEnable failed\n");
    110 +	if (!DPMSGetTimeouts(dpy, &standby, &suspend, &off))
    111 +		die("slock: DPMSGetTimeouts failed\n");
    112 +	if (!standby || !suspend || !off)
    113 +		die("slock: at least one DPMS variable is zero\n");
    114 +	if (!DPMSSetTimeouts(dpy, monitortime, monitortime, monitortime))
    115 +		die("slock: DPMSSetTimeouts failed\n");
    116 +	if (!DPMSSetTimeouts(dpy, monitortime, monitortime, monitortime))
    117 +		die("slock: DPMSSetTimeouts failed\n");
    118 +	XSync(dpy, 0);
    119 +
    120  	/* run post-lock command */
    121  	if (argc > 0) {
    122  		pid_t pid;
    123 @@ -389,5 +420,11 @@ main(int argc, char **argv) {
    124  	/* everything is now blank. Wait for the correct password */
    125  	readpw(dpy, &rr, locks, nscreens, hash);
    126  
    127 +	/* reset DPMS values to inital ones */
    128 +	DPMSSetTimeouts(dpy, standby, suspend, off);
    129 +	if (!dpms_state)
    130 +		DPMSDisable(dpy);
    131 +	XSync(dpy, 0);
    132 +
    133  	return 0;
    134  }