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