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>
30 #include <X11/extensions/dpms.h>
34 #include <security/pam_appl.h>
36 static char passwd[256];
38 static void die(const char *errstr, ...) {
42 vfprintf(stderr, errstr, ap);
48 * Check if given file can be opened => exists
51 bool file_exists(const char *filename)
53 FILE * file = fopen(filename, "r");
63 * Puts the given XPM error code to stderr
66 void print_xpm_error(int err)
70 fprintf(stderr, "XPM: Could not parse or alloc requested color\n");
73 fprintf(stderr, "XPM: Cannot open file\n");
76 fprintf(stderr, "XPM: invalid XPM file\n");
79 fprintf(stderr, "XPM: Not enough memory\n");
82 fprintf(stderr, "XPM: Color not found\n");
89 * Callback function for PAM. We only react on password request callbacks.
92 static int conv_callback(int num_msg, const struct pam_message **msg,
93 struct pam_response **resp, void *appdata_ptr) {
97 /* PAM expects an arry of responses, one for each message */
98 if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
103 for (int c = 0; c < num_msg; c++) {
104 if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
105 msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
108 /* return code is currently not used but should be set to zero */
109 resp[c]->resp_retcode = 0;
110 if ((resp[c]->resp = strdup(passwd)) == NULL) {
119 int main(int argc, char *argv[]) {
120 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
128 /* By default, fork, don’t beep and don’t turn off monitor */
129 bool dont_fork = false;
132 bool xpm_image = false;
133 char xpm_image_path[256];
141 XSetWindowAttributes wa;
143 pam_handle_t *handle;
144 struct pam_conv conv = {conv_callback, NULL};
148 static struct option long_options[] = {
149 {"version", no_argument, NULL, 'v'},
150 {"nofork", no_argument, NULL, 'n'},
151 {"beep", no_argument, NULL, 'b'},
152 {"dpms", no_argument, NULL, 'd'},
153 {"image", required_argument, NULL, 'i'},
154 {NULL, no_argument, NULL, 0}
157 while ((opt = getopt_long(argc, argv, "vnbdi:", long_options, &optind)) != -1) {
160 die("i3lock-"VERSION", © 2009 Michael Stapelberg\n"
161 "based on slock, which is © 2006-2008 Anselm R Garbe\n");
172 strncpy(xpm_image_path, optarg, 255);
176 die("i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.xpm]\n");
180 if ((username = getenv("USER")) == NULL)
181 die("USER environment variable not set, please set it.\n");
183 int ret = pam_start("i3lock", username, &conv, &handle);
184 if (ret != PAM_SUCCESS)
185 die("PAM: %s\n", pam_strerror(handle, ret));
187 if(!(dpy = XOpenDisplay(0)))
188 die("i3lock: cannot open display\n");
189 screen = DefaultScreen(dpy);
190 root = RootWindow(dpy, screen);
198 wa.override_redirect = 1;
199 wa.background_pixel = WhitePixel(dpy, screen);
200 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
201 0, DefaultDepth(dpy, screen), CopyFromParent,
202 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
203 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
204 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
205 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
206 XDefineCursor(dpy, w, invisible);
209 if(xpm_image && file_exists(xpm_image_path))
211 GC gc = XDefaultGC(dpy, 0);
212 int depth = DefaultDepth(dpy, screen);
213 int disp_width = DisplayWidth(dpy, screen);
214 int disp_height = DisplayHeight(dpy, screen);
215 Pixmap pix = XCreatePixmap(dpy, w, disp_width, disp_height, depth);
216 int err = XpmReadFileToPixmap(dpy, w, xpm_image_path, &pix, 0, 0);
218 print_xpm_error(err);
221 XCopyArea(dpy, pix, w, gc, 0, 0, disp_width, disp_height, 0, 0);
224 for(len = 1000; len; len--) {
225 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask,
226 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
230 if((running = running && (len > 0))) {
231 for(len = 1000; len; len--) {
232 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
242 /* main event loop */
243 while(running && !XNextEvent(dpy, &ev)) {
244 if (len == 0 && dpms && DPMSCapable(dpy)) {
246 DPMSForceLevel(dpy, DPMSModeOff);
249 if(ev.type != KeyPress)
253 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
254 if(IsKeypadKey(ksym)) {
255 if(ksym == XK_KP_Enter)
257 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
258 ksym = (ksym - XK_KP_0) + XK_0;
260 if(IsFunctionKey(ksym) ||
262 IsMiscFunctionKey(ksym) ||
264 IsPrivateKeypadKey(ksym))
269 if ((ret = pam_authenticate(handle, 0)) == PAM_SUCCESS)
272 fprintf(stderr, "PAM: %s\n", pam_strerror(handle, ret));
286 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
287 memcpy(passwd + len, buf, num);
293 XUngrabPointer(dpy, CurrentTime);
294 XFreePixmap(dpy, pmap);
295 XDestroyWindow(dpy, w);