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
25 #include <sys/types.h>
26 #include <X11/keysym.h>
28 #include <X11/Xutil.h>
29 #include <X11/extensions/dpms.h>
33 #include <security/pam_appl.h>
35 static char passwd[256];
37 static void die(const char *errstr, ...) {
41 vfprintf(stderr, errstr, ap);
47 * Callback function for PAM. We only react on password request callbacks.
50 static int conv_callback(int num_msg, const struct pam_message **msg,
51 struct pam_response **resp, void *appdata_ptr) {
55 /* PAM expects an arry of responses, one for each message */
56 if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
61 for (int c = 0; c < num_msg; c++) {
62 if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
63 msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
66 /* return code is currently not used but should be set to zero */
67 resp[c]->resp_retcode = 0;
68 if ((resp[c]->resp = strdup(passwd)) == NULL) {
77 int main(int argc, char *argv[]) {
78 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
86 /* By default, fork, don’t beep and don’t turn off monitor */
87 bool dont_fork = false;
97 XSetWindowAttributes wa;
100 struct pam_conv conv = {conv_callback, NULL};
104 static struct option long_options[] = {
105 {"version", no_argument, NULL, 'v'},
106 {"nofork", no_argument, NULL, 'n'},
107 {"beep", no_argument, NULL, 'b'},
108 {"dpms", no_argument, NULL, 'd'},
109 {NULL, no_argument, NULL, 0}
112 while ((opt = getopt_long(argc, argv, "vnbd", long_options, &optind)) != -1) {
115 die("i3lock-"VERSION", © 2009 Michael Stapelberg\n"
116 "based on slock, which is © 2006-2008 Anselm R Garbe\n");
127 die("i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d]\n");
131 if ((username = getenv("USER")) == NULL)
132 die("USER environment variable not set, please set it.\n");
134 int ret = pam_start("i3lock", username, &conv, &handle);
135 if (ret != PAM_SUCCESS)
136 die("PAM: %s\n", pam_strerror(handle, ret));
138 if(!(dpy = XOpenDisplay(0)))
139 die("i3lock: cannot open display\n");
140 screen = DefaultScreen(dpy);
141 root = RootWindow(dpy, screen);
149 wa.override_redirect = 1;
150 wa.background_pixel = WhitePixel(dpy, screen);
151 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
152 0, DefaultDepth(dpy, screen), CopyFromParent,
153 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
154 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
155 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
156 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
157 XDefineCursor(dpy, w, invisible);
159 for(len = 1000; len; len--) {
160 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
161 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
165 if((running = running && (len > 0))) {
166 for(len = 1000; len; len--) {
167 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
177 /* main event loop */
178 while(running && !XNextEvent(dpy, &ev)) {
179 if (len == 0 && dpms && DPMSCapable(dpy)) {
181 DPMSForceLevel(dpy, DPMSModeOff);
184 if(ev.type != KeyPress)
188 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
189 if(IsKeypadKey(ksym)) {
190 if(ksym == XK_KP_Enter)
192 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
193 ksym = (ksym - XK_KP_0) + XK_0;
195 if(IsFunctionKey(ksym) ||
197 IsMiscFunctionKey(ksym) ||
199 IsPrivateKeypadKey(ksym))
204 if ((ret = pam_authenticate(handle, 0)) == PAM_SUCCESS)
207 fprintf(stderr, "PAM: %s\n", pam_strerror(handle, ret));
221 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
222 memcpy(passwd + len, buf, num);
228 XUngrabPointer(dpy, CurrentTime);
229 XFreePixmap(dpy, pmap);
230 XDestroyWindow(dpy, w);