4 * i3lock - an improved version of slock
6 * i3lock © 2009 Michael Stapelberg and contributors
7 * i3lock © 2009 Jan-Erik Rediger
8 * slock © 2006-2008 Anselm R Garbe
10 * See file LICENSE for license information.
12 * Note that on any error (calloc is out of memory for example)
13 * we do not do anything so that the user can fix the error by
14 * himself (kill X to get more free memory or stop some other
15 * program using SSH/console).
18 #define _XOPEN_SOURCE 500
28 #include <sys/types.h>
29 #include <X11/keysym.h>
31 #include <X11/Xutil.h>
33 #include <X11/extensions/dpms.h>
38 #include <security/pam_appl.h>
40 static char passwd[256];
43 * displays an xpm image tiled over the whole screen
44 * (the image will be visible on all screens
45 * when using a multi monitor setup)
48 static void tile_image(XpmImage *image, int disp_height, int disp_width,
49 Display *dpy, Pixmap pix, Window w, GC gc)
51 int rows = (int)ceil(disp_height / (float)image->height),
52 cols = (int)ceil(disp_width / (float)image->width);
54 for (int y = 0; y < rows; y++) {
55 for (int x = 0; x < cols; x++) {
56 XCopyArea(dpy, pix, w, gc, 0, 0,
57 image->width, image->height,
58 image->width * x, image->height * y);
64 * Returns the colorpixel to use for the given hex color (think of HTML).
66 * The hex_color may not start with #, for example FF00FF works.
68 * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
69 * This has to be done by the caller.
72 static uint32_t get_colorpixel(char *hex) {
73 char strgroups[3][3] = {{hex[0], hex[1], '\0'},
74 {hex[2], hex[3], '\0'},
75 {hex[4], hex[5], '\0'}};
76 uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
77 (strtol(strgroups[1], NULL, 16)),
78 (strtol(strgroups[2], NULL, 16))};
80 return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
84 * Check if given file can be opened => exists
87 static bool file_exists(const char *filename)
89 FILE * file = fopen(filename, "r");
99 * Puts the given XPM error code to stderr
102 static void print_xpm_error(int err)
106 fprintf(stderr, "XPM: Could not parse or alloc requested color\n");
109 fprintf(stderr, "XPM: Cannot open file\n");
112 fprintf(stderr, "XPM: invalid XPM file\n");
115 fprintf(stderr, "XPM: Not enough memory\n");
118 fprintf(stderr, "XPM: Color not found\n");
125 * Callback function for PAM. We only react on password request callbacks.
128 static int conv_callback(int num_msg, const struct pam_message **msg,
129 struct pam_response **resp, void *appdata_ptr)
134 /* PAM expects an arry of responses, one for each message */
135 if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
140 for (int c = 0; c < num_msg; c++) {
141 if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
142 msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
145 /* return code is currently not used but should be set to zero */
146 resp[c]->resp_retcode = 0;
147 if ((resp[c]->resp = strdup(passwd)) == NULL) {
156 int main(int argc, char *argv[])
158 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
166 /* By default, fork, don’t beep and don’t turn off monitor */
167 bool dont_fork = false;
170 bool xpm_image = false;
172 char xpm_image_path[256];
173 char color[7] = "ffffff"; // white
181 XSetWindowAttributes wa;
183 pam_handle_t *handle;
184 struct pam_conv conv = {conv_callback, NULL};
188 static struct option long_options[] = {
189 {"version", no_argument, NULL, 'v'},
190 {"nofork", no_argument, NULL, 'n'},
191 {"beep", no_argument, NULL, 'b'},
192 {"dpms", no_argument, NULL, 'd'},
193 {"image", required_argument, NULL, 'i'},
194 {"color", required_argument, NULL, 'c'},
195 {"tiling", no_argument, NULL, 't'},
196 {NULL, no_argument, NULL, 0}
199 while ((opt = getopt_long(argc, argv, "vnbdi:c:t", long_options, &optind)) != -1) {
202 errx(0, "i3lock-"VERSION", © 2009 Michael Stapelberg\n"
203 "based on slock, which is © 2006-2008 Anselm R Garbe\n");
214 strncpy(xpm_image_path, optarg, 255);
220 /* Skip # if present */
224 if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
225 errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
233 errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.xpm] [-c color] [-t]\n");
237 if ((username = getenv("USER")) == NULL)
238 errx(1, "USER environment variable not set, please set it.\n");
240 int ret = pam_start("i3lock", username, &conv, &handle);
241 if (ret != PAM_SUCCESS)
242 errx(1, "PAM: %s\n", pam_strerror(handle, ret));
244 if(!(dpy = XOpenDisplay(0)))
245 errx(1, "i3lock: cannot open display\n");
246 screen = DefaultScreen(dpy);
247 root = RootWindow(dpy, screen);
255 wa.override_redirect = 1;
256 wa.background_pixel = get_colorpixel(color);
257 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
258 0, DefaultDepth(dpy, screen), CopyFromParent,
259 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
260 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
261 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
262 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
263 XDefineCursor(dpy, w, invisible);
266 if (xpm_image && file_exists(xpm_image_path)) {
267 GC gc = XDefaultGC(dpy, 0);
268 int depth = DefaultDepth(dpy, screen);
269 int disp_width = DisplayWidth(dpy, screen);
270 int disp_height = DisplayHeight(dpy, screen);
271 Pixmap pix = XCreatePixmap(dpy, w, disp_width, disp_height, depth);
275 int err = XpmReadFileToXpmImage(xpm_image_path, &xpm_image, &xpm_info);
277 print_xpm_error(err);
281 err = XpmCreatePixmapFromXpmImage(dpy, w, &xpm_image, &pix, 0, 0);
283 print_xpm_error(err);
288 tile_image(&xpm_image, disp_height, disp_width, dpy, pix, w, gc);
290 XCopyArea(dpy, pix, w, gc, 0, 0, disp_width, disp_height, 0, 0);
293 for(len = 1000; len; len--) {
294 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask,
295 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
299 if((running = running && (len > 0))) {
300 for(len = 1000; len; len--) {
301 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
311 /* main event loop */
312 while(running && !XNextEvent(dpy, &ev)) {
313 if (len == 0 && dpms && DPMSCapable(dpy)) {
315 DPMSForceLevel(dpy, DPMSModeOff);
318 if(ev.type != KeyPress)
322 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
323 if(IsKeypadKey(ksym)) {
324 if(ksym == XK_KP_Enter)
326 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
327 ksym = (ksym - XK_KP_0) + XK_0;
329 if(IsFunctionKey(ksym) ||
331 IsMiscFunctionKey(ksym) ||
333 IsPrivateKeypadKey(ksym))
338 /* Skip empty passwords */
341 if ((ret = pam_authenticate(handle, 0)) == PAM_SUCCESS)
344 fprintf(stderr, "PAM: %s\n", pam_strerror(handle, ret));
358 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
359 memcpy(passwd + len, buf, num);
365 XUngrabPointer(dpy, CurrentTime);
366 XFreePixmap(dpy, pmap);
367 XDestroyWindow(dpy, w);