4 * i3lock - an improved version of slock
6 * i3lock © 2009 Michael Stapelberg and contributors
7 * slock © 2006-2008 Anselm R Garbe
9 * See file LICENSE for license information.
11 * Note that on any error (calloc is out of memory for example)
12 * we do not do anything so that the user can fix the error by
13 * himself (kill X to get more free memory or stop some other
14 * program using SSH/console).
17 #define _XOPEN_SOURCE 500
27 #include <sys/types.h>
28 #include <X11/keysym.h>
30 #include <X11/Xutil.h>
32 #include <X11/extensions/dpms.h>
36 #include <security/pam_appl.h>
38 static char passwd[256];
41 * displays an xpm image tiled over the whole screen
42 * (the image will be visible on all screens
43 * when using a multi monitor setup)
46 void tiling_image(XpmImage *image,
47 int disp_height, int disp_width,
52 int rows = (int)ceil(disp_height / (float)image->height),
53 cols = (int)ceil(disp_width / (float)image->width);
58 for(y = 0; y < rows; y++) {
59 for(x = 0; x < cols; x++) {
60 XCopyArea(dpy, pix, w, gc, 0, 0, image->width, image->height, image->width * x, image->height * y);
65 static void die(const char *errstr, ...) {
69 vfprintf(stderr, errstr, ap);
75 * Returns the colorpixel to use for the given hex color (think of HTML).
77 * The hex_color may not start with #, for example FF00FF works.
79 * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
80 * This has to be done by the caller.
83 uint32_t get_colorpixel(char *hex) {
84 char strgroups[3][3] = {{hex[0], hex[1], '\0'},
85 {hex[2], hex[3], '\0'},
86 {hex[4], hex[5], '\0'}};
87 uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
88 (strtol(strgroups[1], NULL, 16)),
89 (strtol(strgroups[2], NULL, 16))};
91 return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
95 * Check if given file can be opened => exists
98 bool file_exists(const char *filename)
100 FILE * file = fopen(filename, "r");
110 * Puts the given XPM error code to stderr
113 void print_xpm_error(int err)
117 fprintf(stderr, "XPM: Could not parse or alloc requested color\n");
120 fprintf(stderr, "XPM: Cannot open file\n");
123 fprintf(stderr, "XPM: invalid XPM file\n");
126 fprintf(stderr, "XPM: Not enough memory\n");
129 fprintf(stderr, "XPM: Color not found\n");
136 * Callback function for PAM. We only react on password request callbacks.
139 static int conv_callback(int num_msg, const struct pam_message **msg,
140 struct pam_response **resp, void *appdata_ptr) {
144 /* PAM expects an arry of responses, one for each message */
145 if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
150 for (int c = 0; c < num_msg; c++) {
151 if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
152 msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
155 /* return code is currently not used but should be set to zero */
156 resp[c]->resp_retcode = 0;
157 if ((resp[c]->resp = strdup(passwd)) == NULL) {
166 int main(int argc, char *argv[]) {
167 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
175 /* By default, fork, don’t beep and don’t turn off monitor */
176 bool dont_fork = false;
179 bool xpm_image = false;
181 char xpm_image_path[256];
182 char color[7] = "ffffff"; // white
190 XSetWindowAttributes wa;
192 pam_handle_t *handle;
193 struct pam_conv conv = {conv_callback, NULL};
197 static struct option long_options[] = {
198 {"version", no_argument, NULL, 'v'},
199 {"nofork", no_argument, NULL, 'n'},
200 {"beep", no_argument, NULL, 'b'},
201 {"dpms", no_argument, NULL, 'd'},
202 {"image", required_argument, NULL, 'i'},
203 {"color", required_argument, NULL, 'c'},
204 {"tiling", no_argument, NULL, 't'},
205 {NULL, no_argument, NULL, 0}
208 while ((opt = getopt_long(argc, argv, "vnbdi:c:t", long_options, &optind)) != -1) {
211 die("i3lock-"VERSION", © 2009 Michael Stapelberg\n"
212 "based on slock, which is © 2006-2008 Anselm R Garbe\n");
223 strncpy(xpm_image_path, optarg, 255);
229 /* Skip # if present */
233 if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
234 die("color is invalid, color must be given in 6-byte format: rrggbb\n");
242 die("i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.xpm] [-c color] [-t]\n");
246 if ((username = getenv("USER")) == NULL)
247 die("USER environment variable not set, please set it.\n");
249 int ret = pam_start("i3lock", username, &conv, &handle);
250 if (ret != PAM_SUCCESS)
251 die("PAM: %s\n", pam_strerror(handle, ret));
253 if(!(dpy = XOpenDisplay(0)))
254 die("i3lock: cannot open display\n");
255 screen = DefaultScreen(dpy);
256 root = RootWindow(dpy, screen);
264 wa.override_redirect = 1;
265 wa.background_pixel = get_colorpixel(color);
266 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
267 0, DefaultDepth(dpy, screen), CopyFromParent,
268 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
269 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
270 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
271 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
272 XDefineCursor(dpy, w, invisible);
275 if (xpm_image && file_exists(xpm_image_path)) {
276 GC gc = XDefaultGC(dpy, 0);
277 int depth = DefaultDepth(dpy, screen);
278 int disp_width = DisplayWidth(dpy, screen);
279 int disp_height = DisplayHeight(dpy, screen);
280 Pixmap pix = XCreatePixmap(dpy, w, disp_width, disp_height, depth);
284 int err = XpmReadFileToXpmImage(xpm_image_path, &xpm_image, &xpm_info);
286 print_xpm_error(err);
290 err = XpmCreatePixmapFromXpmImage(dpy, w, &xpm_image, &pix, 0, 0);
292 print_xpm_error(err);
297 tiling_image(&xpm_image, disp_height, disp_width, dpy, pix, w, gc);
299 XCopyArea(dpy, pix, w, gc, 0, 0, disp_width, disp_height, 0, 0);
302 for(len = 1000; len; len--) {
303 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask,
304 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
308 if((running = running && (len > 0))) {
309 for(len = 1000; len; len--) {
310 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
320 /* main event loop */
321 while(running && !XNextEvent(dpy, &ev)) {
322 if (len == 0 && dpms && DPMSCapable(dpy)) {
324 DPMSForceLevel(dpy, DPMSModeOff);
327 if(ev.type != KeyPress)
331 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
332 if(IsKeypadKey(ksym)) {
333 if(ksym == XK_KP_Enter)
335 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
336 ksym = (ksym - XK_KP_0) + XK_0;
338 if(IsFunctionKey(ksym) ||
340 IsMiscFunctionKey(ksym) ||
342 IsPrivateKeypadKey(ksym))
347 if ((ret = pam_authenticate(handle, 0)) == PAM_SUCCESS)
350 fprintf(stderr, "PAM: %s\n", pam_strerror(handle, ret));
364 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
365 memcpy(passwd + len, buf, num);
371 XUngrabPointer(dpy, CurrentTime);
372 XFreePixmap(dpy, pmap);
373 XDestroyWindow(dpy, w);