slock

My slock fork
git clone https://git.kausban.com/slock/raw/.git
Log | Files | Refs | README

slock-message-20191215-post_pam.diff (5857B)


      1 diff --unified --recursive --text --color a/config.def.h b/config.def.h
      2 --- a/config.def.h	2019-12-15 02:32:55.802402599 +0100
      3 +++ b/config.def.h	2019-12-15 02:34:38.442408519 +0100
      4 @@ -14,3 +14,12 @@
      5  
      6  /* PAM service that's used for authentication */
      7  static const char* pam_service = "login";
      8 +
      9 +/* default message */
     10 +static const char * message = "Suckless: Software that sucks less.";
     11 +
     12 +/* text color */
     13 +static const char * text_color = "#ffffff";
     14 +
     15 +/* text size (must be a valid size) */
     16 +static const char * font_name = "6x10";
     17 diff --unified --recursive --text --color a/config.mk b/config.mk
     18 --- a/config.mk	2019-12-15 02:32:55.802402599 +0100
     19 +++ b/config.mk	2019-12-15 02:34:48.715742442 +0100
     20 @@ -12,7 +12,7 @@
     21  
     22  # includes and libs
     23  INCS = -I. -I/usr/include -I${X11INC}
     24 -LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr -lpam
     25 +LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr -lpam -lXinerama
     26  
     27  # flags
     28  CPPFLAGS += -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H
     29 diff --unified --recursive --text --color a/slock.1 b/slock.1
     30 --- a/slock.1	2016-11-20 01:31:23.000000000 +0100
     31 +++ b/slock.1	2019-12-15 02:34:07.839073423 +0100
     32 @@ -6,6 +6,8 @@
     33  .Sh SYNOPSIS
     34  .Nm
     35  .Op Fl v
     36 +.Op Fl f
     37 +.Op Fl m Ar message
     38  .Op Ar cmd Op Ar arg ...
     39  .Sh DESCRIPTION
     40  .Nm
     41 @@ -16,6 +18,11 @@
     42  .Bl -tag -width Ds
     43  .It Fl v
     44  Print version information to stdout and exit.
     45 +.It Fl f
     46 +List all valid X fonts and exit.
     47 +.It Fl m Ar message
     48 +Overrides default slock lock message.
     49 +.TP
     50  .El
     51  .Sh SECURITY CONSIDERATIONS
     52  To make sure a locked screen can not be bypassed by switching VTs
     53 diff --unified --recursive --text --color a/slock.c b/slock.c
     54 --- a/slock.c	2019-12-15 02:32:55.802402599 +0100
     55 +++ b/slock.c	2019-12-15 02:34:07.839073423 +0100
     56 @@ -15,6 +15,7 @@
     57  #include <unistd.h>
     58  #include <sys/types.h>
     59  #include <X11/extensions/Xrandr.h>
     60 +#include <X11/extensions/Xinerama.h>
     61  #include <X11/keysym.h>
     62  #include <X11/Xlib.h>
     63  #include <X11/Xutil.h>
     64 @@ -29,6 +30,9 @@
     65  struct pam_conv pamc = {pam_conv, NULL};
     66  char passwd[256];
     67  
     68 +/* global count to prevent repeated error messages */
     69 +int count_error = 0;
     70 +
     71  enum {
     72  	INIT,
     73  	INPUT,
     74 @@ -114,6 +118,98 @@
     75  }
     76  #endif
     77  
     78 +static void
     79 +writemessage(Display *dpy, Window win, int screen)
     80 +{
     81 +	int len, line_len, width, height, s_width, s_height, i, j, k, tab_replace, tab_size;
     82 +	XGCValues gr_values;
     83 +	XFontStruct *fontinfo;
     84 +	XColor color, dummy;
     85 +	XineramaScreenInfo *xsi;
     86 +	GC gc;
     87 +	fontinfo = XLoadQueryFont(dpy, font_name);
     88 +
     89 +	if (fontinfo == NULL) {
     90 +		if (count_error == 0) {
     91 +			fprintf(stderr, "slock: Unable to load font \"%s\"\n", font_name);
     92 +			fprintf(stderr, "slock: Try listing fonts with 'slock -f'\n");
     93 +			count_error++;
     94 +		}
     95 +		return;
     96 +	}
     97 +
     98 +	tab_size = 8 * XTextWidth(fontinfo, " ", 1);
     99 +
    100 +	XAllocNamedColor(dpy, DefaultColormap(dpy, screen),
    101 +		 text_color, &color, &dummy);
    102 +
    103 +	gr_values.font = fontinfo->fid;
    104 +	gr_values.foreground = color.pixel;
    105 +	gc=XCreateGC(dpy,win,GCFont+GCForeground, &gr_values);
    106 +
    107 +	/*  To prevent "Uninitialized" warnings. */
    108 +	xsi = NULL;
    109 +
    110 +	/*
    111 +	 * Start formatting and drawing text
    112 +	 */
    113 +
    114 +	len = strlen(message);
    115 +
    116 +	/* Max max line length (cut at '\n') */
    117 +	line_len = 0;
    118 +	k = 0;
    119 +	for (i = j = 0; i < len; i++) {
    120 +		if (message[i] == '\n') {
    121 +			if (i - j > line_len)
    122 +				line_len = i - j;
    123 +			k++;
    124 +			i++;
    125 +			j = i;
    126 +		}
    127 +	}
    128 +	/* If there is only one line */
    129 +	if (line_len == 0)
    130 +		line_len = len;
    131 +
    132 +	if (XineramaIsActive(dpy)) {
    133 +		xsi = XineramaQueryScreens(dpy, &i);
    134 +		s_width = xsi[0].width;
    135 +		s_height = xsi[0].height;
    136 +	} else {
    137 +		s_width = DisplayWidth(dpy, screen);
    138 +		s_height = DisplayHeight(dpy, screen);
    139 +	}
    140 +
    141 +	height = s_height*3/7 - (k*20)/3;
    142 +	width  = (s_width - XTextWidth(fontinfo, message, line_len))/2;
    143 +
    144 +	/* Look for '\n' and print the text between them. */
    145 +	for (i = j = k = 0; i <= len; i++) {
    146 +		/* i == len is the special case for the last line */
    147 +		if (i == len || message[i] == '\n') {
    148 +			tab_replace = 0;
    149 +			while (message[j] == '\t' && j < i) {
    150 +				tab_replace++;
    151 +				j++;
    152 +			}
    153 +
    154 +			XDrawString(dpy, win, gc, width + tab_size*tab_replace, height + 20*k, message + j, i - j);
    155 +			while (i < len && message[i] == '\n') {
    156 +				i++;
    157 +				j = i;
    158 +				k++;
    159 +			}
    160 +		}
    161 +	}
    162 +
    163 +	/* xsi should not be NULL anyway if Xinerama is active, but to be safe */
    164 +	if (XineramaIsActive(dpy) && xsi != NULL)
    165 +			XFree(xsi);
    166 +}
    167 +
    168 +
    169 +
    170  static const char *
    171  gethash(void)
    172  {
    173 @@ -244,6 +340,7 @@
    174  					                     locks[screen]->win,
    175  					                     locks[screen]->colors[color]);
    176  					XClearWindow(dpy, locks[screen]->win);
    177 +					writemessage(dpy, locks[screen]->win, screen);
    178  				}
    179  				oldc = color;
    180  			}
    181 @@ -342,7 +439,7 @@
    182  static void
    183  usage(void)
    184  {
    185 -	die("usage: slock [-v] [cmd [arg ...]]\n");
    186 +	die("usage: slock [-v] [-f] [-m message] [cmd [arg ...]]\n");
    187  }
    188  
    189  int
    190 @@ -355,12 +452,25 @@
    191  	gid_t dgid;
    192  	const char *hash;
    193  	Display *dpy;
    194 -	int s, nlocks, nscreens;
    195 +	int i, s, nlocks, nscreens;
    196 +	int count_fonts;
    197 +	char **font_names;
    198  
    199  	ARGBEGIN {
    200  	case 'v':
    201  		fprintf(stderr, "slock-"VERSION"\n");
    202  		return 0;
    203 +	case 'm':
    204 +		message = EARGF(usage());
    205 +		break;
    206 +	case 'f':
    207 +		if (!(dpy = XOpenDisplay(NULL)))
    208 +			die("slock: cannot open display\n");
    209 +		font_names = XListFonts(dpy, "*", 10000 /* list 10000 fonts*/, &count_fonts);
    210 +		for (i=0; i<count_fonts; i++) {
    211 +			fprintf(stderr, "%s\n", *(font_names+i));
    212 +		}
    213 +		return 0;
    214  	default:
    215  		usage();
    216  	} ARGEND
    217 @@ -404,10 +514,12 @@
    218  	if (!(locks = calloc(nscreens, sizeof(struct lock *))))
    219  		die("slock: out of memory\n");
    220  	for (nlocks = 0, s = 0; s < nscreens; s++) {
    221 -		if ((locks[s] = lockscreen(dpy, &rr, s)) != NULL)
    222 +		if ((locks[s] = lockscreen(dpy, &rr, s)) != NULL) {
    223 +			writemessage(dpy, locks[s]->win, s);
    224  			nlocks++;
    225 -		else
    226 +		} else {
    227  			break;
    228 +		}
    229  	}
    230  	XSync(dpy, 0);
    231