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
26 #include <sys/types.h>
27 #include <X11/keysym.h>
29 #include <X11/Xutil.h>
31 #include <X11/extensions/dpms.h>
35 #include <security/pam_appl.h>
37 static char passwd[256];
39 static void die(const char *errstr, ...) {
43 vfprintf(stderr, errstr, ap);
50 * Returns the colorpixel to use for the given hex color (think of HTML).
52 * The hex_color may not start with #, for example FF00FF works.
54 * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
55 * This has to be done by the caller.
58 uint32_t get_colorpixel(char *hex) {
59 char strgroups[3][3] = {{hex[0], hex[1], '\0'},
60 {hex[2], hex[3], '\0'},
61 {hex[4], hex[5], '\0'}};
62 uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
63 (strtol(strgroups[1], NULL, 16)),
64 (strtol(strgroups[2], NULL, 16))};
66 return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
70 * Check if given file can be opened => exists
73 bool file_exists(const char *filename)
75 FILE * file = fopen(filename, "r");
85 * Puts the given XPM error code to stderr
88 void print_xpm_error(int err)
92 fprintf(stderr, "XPM: Could not parse or alloc requested color\n");
95 fprintf(stderr, "XPM: Cannot open file\n");
98 fprintf(stderr, "XPM: invalid XPM file\n");
101 fprintf(stderr, "XPM: Not enough memory\n");
104 fprintf(stderr, "XPM: Color not found\n");
111 * Callback function for PAM. We only react on password request callbacks.
114 static int conv_callback(int num_msg, const struct pam_message **msg,
115 struct pam_response **resp, void *appdata_ptr) {
119 /* PAM expects an arry of responses, one for each message */
120 if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
125 for (int c = 0; c < num_msg; c++) {
126 if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
127 msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
130 /* return code is currently not used but should be set to zero */
131 resp[c]->resp_retcode = 0;
132 if ((resp[c]->resp = strdup(passwd)) == NULL) {
141 int main(int argc, char *argv[]) {
142 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
150 /* By default, fork, don’t beep and don’t turn off monitor */
151 bool dont_fork = false;
154 bool xpm_image = false;
155 char xpm_image_path[256];
156 char color[7] = "ffffff"; // white
164 XSetWindowAttributes wa;
166 pam_handle_t *handle;
167 struct pam_conv conv = {conv_callback, NULL};
171 static struct option long_options[] = {
172 {"version", no_argument, NULL, 'v'},
173 {"nofork", no_argument, NULL, 'n'},
174 {"beep", no_argument, NULL, 'b'},
175 {"dpms", no_argument, NULL, 'd'},
176 {"image", required_argument, NULL, 'i'},
177 {"color", required_argument, NULL, 'c'},
178 {NULL, no_argument, NULL, 0}
181 while ((opt = getopt_long(argc, argv, "vnbdi:c:", long_options, &optind)) != -1) {
184 die("i3lock-"VERSION", © 2009 Michael Stapelberg\n"
185 "based on slock, which is © 2006-2008 Anselm R Garbe\n");
196 strncpy(xpm_image_path, optarg, 255);
200 strncpy(color, optarg, 6);
203 char parsed_color[7];
204 if (strlen(color) != 6 || sscanf(color, "%06[0-9a-fA-F]", parsed_color) != 1)
205 die("color is invalid, color must be given in 6-byte format: rrggbb\n");
208 die("i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.xpm] [-c color]\n");
212 if ((username = getenv("USER")) == NULL)
213 die("USER environment variable not set, please set it.\n");
215 int ret = pam_start("i3lock", username, &conv, &handle);
216 if (ret != PAM_SUCCESS)
217 die("PAM: %s\n", pam_strerror(handle, ret));
219 if(!(dpy = XOpenDisplay(0)))
220 die("i3lock: cannot open display\n");
221 screen = DefaultScreen(dpy);
222 root = RootWindow(dpy, screen);
230 wa.override_redirect = 1;
231 wa.background_pixel = get_colorpixel(color);
232 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
233 0, DefaultDepth(dpy, screen), CopyFromParent,
234 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
235 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
236 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
237 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
238 XDefineCursor(dpy, w, invisible);
241 if(xpm_image && file_exists(xpm_image_path))
243 GC gc = XDefaultGC(dpy, 0);
244 int depth = DefaultDepth(dpy, screen);
245 int disp_width = DisplayWidth(dpy, screen);
246 int disp_height = DisplayHeight(dpy, screen);
247 Pixmap pix = XCreatePixmap(dpy, w, disp_width, disp_height, depth);
248 int err = XpmReadFileToPixmap(dpy, w, xpm_image_path, &pix, 0, 0);
250 print_xpm_error(err);
253 XCopyArea(dpy, pix, w, gc, 0, 0, disp_width, disp_height, 0, 0);
256 for(len = 1000; len; len--) {
257 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask,
258 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
262 if((running = running && (len > 0))) {
263 for(len = 1000; len; len--) {
264 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
274 /* main event loop */
275 while(running && !XNextEvent(dpy, &ev)) {
276 if (len == 0 && dpms && DPMSCapable(dpy)) {
278 DPMSForceLevel(dpy, DPMSModeOff);
281 if(ev.type != KeyPress)
285 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
286 if(IsKeypadKey(ksym)) {
287 if(ksym == XK_KP_Enter)
289 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
290 ksym = (ksym - XK_KP_0) + XK_0;
292 if(IsFunctionKey(ksym) ||
294 IsMiscFunctionKey(ksym) ||
296 IsPrivateKeypadKey(ksym))
301 if ((ret = pam_authenticate(handle, 0)) == PAM_SUCCESS)
304 fprintf(stderr, "PAM: %s\n", pam_strerror(handle, ret));
318 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
319 memcpy(passwd + len, buf, num);
325 XUngrabPointer(dpy, CurrentTime);
326 XFreePixmap(dpy, pmap);
327 XDestroyWindow(dpy, w);