slock-message-20180626-35633d4.diff - sites - public wiki contents of suckless.org
(HTM) git clone git://git.suckless.org/sites
(DIR) Log
(DIR) Files
(DIR) Refs
---
slock-message-20180626-35633d4.diff (4364B)
---
1 From 8384a863057f024868bd8455a82013f716197ab0 Mon Sep 17 00:00:00 2001
2 From: Blair Drummond <blair.robert.drummond@gmail.com>
3 Date: Tue, 26 Jun 2018 15:09:13 -0400
4 Subject: [PATCH] Adds [-m message] option to leave a message on the lockscreen
5
6 ---
7 config.def.h | 9 ++++++
8 slock.1 | 4 +++
9 slock.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++--
10 3 files changed, 87 insertions(+), 3 deletions(-)
11
12 diff --git a/config.def.h b/config.def.h
13 index 9855e21..7eb7a0d 100644
14 --- a/config.def.h
15 +++ b/config.def.h
16 @@ -10,3 +10,12 @@ static const char *colorname[NUMCOLS] = {
17
18 /* treat a cleared input like a wrong password (color) */
19 static const int failonclear = 1;
20 +
21 +/* default message */
22 +static const char * message = "Suckless: Software that sucks less.";
23 +
24 +/* text color */
25 +static const char * text_color = "#ffffff";
26 +
27 +/* text size (must be a valid size) */
28 +static const char * text_size = "6x10";
29 diff --git a/slock.1 b/slock.1
30 index 82cdcd6..541a264 100644
31 --- a/slock.1
32 +++ b/slock.1
33 @@ -6,6 +6,7 @@
34 .Sh SYNOPSIS
35 .Nm
36 .Op Fl v
37 +.Op Fl m Ar message
38 .Op Ar cmd Op Ar arg ...
39 .Sh DESCRIPTION
40 .Nm
41 @@ -16,6 +17,9 @@ is executed after the screen has been locked.
42 .Bl -tag -width Ds
43 .It Fl v
44 Print version information to stdout and exit.
45 +.It Fl m Ar message
46 +Overrides default slock lock message.
47 +.TP
48 .El
49 .Sh SECURITY CONSIDERATIONS
50 To make sure a locked screen can not be bypassed by switching VTs
51 diff --git a/slock.c b/slock.c
52 index 5ae738c..6a9a03f 100644
53 --- a/slock.c
54 +++ b/slock.c
55 @@ -61,6 +61,71 @@ die(const char *errstr, ...)
56 #include <fcntl.h>
57 #include <linux/oom.h>
58
59 +static void
60 +writemessage(Display *dpy, Window win, int screen)
61 +{
62 + int len, line_len, width, height, i, j, k, tab_replace, tab_size;
63 + XGCValues gr_values;
64 + XFontStruct *fontinfo;
65 + XColor color, dummy;
66 + GC gc;
67 + fontinfo = XLoadQueryFont(dpy, text_size);
68 + tab_size = 8 * XTextWidth(fontinfo, " ", 1);
69 +
70 + XAllocNamedColor(dpy, DefaultColormap(dpy, screen),
71 + text_color, &color, &dummy);
72 +
73 + gr_values.font = fontinfo->fid;
74 + gr_values.foreground = color.pixel;
75 + gc=XCreateGC(dpy,win,GCFont+GCForeground, &gr_values);
76 +
77 +
78 + /*
79 + * Start formatting and drawing text
80 + */
81 +
82 + len = strlen(message);
83 +
84 + /* Max max line length (cut at '\n') */
85 + line_len = 0;
86 + k = 0;
87 + for (i = j = 0; i < len; i++) {
88 + if (message[i] == '\n') {
89 + if (i - j > line_len)
90 + line_len = i - j;
91 + k++;
92 + i++;
93 + j = i;
94 + }
95 + }
96 + /* If there is only one line */
97 + if (line_len == 0)
98 + line_len = len;
99 +
100 + height = DisplayHeight(dpy, screen)*3/7 - (k*20)/3;
101 + width = (DisplayWidth(dpy, screen) - XTextWidth(fontinfo, message, line_len))/2;
102 +
103 + /* Look for '\n' and print the text between them. */
104 + for (i = j = k = 0; i <= len; i++) {
105 + /* i == len is the special case for the last line */
106 + if (i == len || message[i] == '\n') {
107 + tab_replace = 0;
108 + while (message[j] == '\t' && j < i) {
109 + tab_replace++;
110 + j++;
111 + }
112 +
113 + XDrawString(dpy, win, gc, width + tab_size*tab_replace, height + 20*k, message + j, i - j);
114 + while (i < len && message[i] == '\n') {
115 + i++;
116 + j = i;
117 + k++;
118 + }
119 + }
120 + }
121 +}
122 +
123 +
124 static void
125 dontkillme(void)
126 {
127 @@ -194,6 +259,7 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
128 locks[screen]->win,
129 locks[screen]->colors[color]);
130 XClearWindow(dpy, locks[screen]->win);
131 + writemessage(dpy, locks[screen]->win, screen);
132 }
133 oldc = color;
134 }
135 @@ -300,7 +366,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
136 static void
137 usage(void)
138 {
139 - die("usage: slock [-v] [cmd [arg ...]]\n");
140 + die("usage: slock [-v] [-m message] [cmd [arg ...]]\n");
141 }
142
143 int
144 @@ -319,6 +385,9 @@ main(int argc, char **argv) {
145 case 'v':
146 fprintf(stderr, "slock-"VERSION"\n");
147 return 0;
148 + case 'm':
149 + message = EARGF(usage());
150 + break;
151 default:
152 usage();
153 } ARGEND
154 @@ -363,10 +432,12 @@ main(int argc, char **argv) {
155 if (!(locks = calloc(nscreens, sizeof(struct lock *))))
156 die("slock: out of memory\n");
157 for (nlocks = 0, s = 0; s < nscreens; s++) {
158 - if ((locks[s] = lockscreen(dpy, &rr, s)) != NULL)
159 + if ((locks[s] = lockscreen(dpy, &rr, s)) != NULL) {
160 + writemessage(dpy, locks[s]->win, s);
161 nlocks++;
162 - else
163 + } else {
164 break;
165 + }
166 }
167 XSync(dpy, 0);
168
169 --
170 2.17.1
171