2 * vim:ts=4:sw=4:expandtab
4 * © 2010 Michael Stapelberg
6 * See LICENSE for licensing information
17 #include <xcb/xcb_keysyms.h>
20 #include <security/pam_appl.h>
21 /* FIXME: can we get rid of this header? */
22 #include <X11/keysym.h>
28 #include <cairo/cairo-xcb.h>
31 #include "keysym2ucs.h"
32 #include "ucs2_to_utf8.h"
36 static xcb_connection_t *conn;
37 static xcb_key_symbols_t *symbols;
38 static xcb_screen_t *scr;
39 static pam_handle_t *pam_handle;
40 static int input_position = 0;
41 /* holds the password you enter (in UTF-8) */
42 static char password[512];
43 static bool modeswitch_active = false;
44 static int modeswitchmask;
45 static int numlockmask;
46 static bool beep = false;
49 static cairo_surface_t *img = NULL;
50 static cairo_t *ctx = NULL;
51 static bool tile = false;
54 static void input_done() {
55 if (input_position == 0)
58 /* TODO: change cursor during authentication? */
59 if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
60 printf("successfully authenticated\n");
64 /* beep on authentication failure, if enabled */
72 * Called when we should draw the image (if any).
75 static void handle_expose_event() {
81 /* create a pattern and fill a rectangle as big as the screen */
82 cairo_pattern_t *pattern;
83 pattern = cairo_pattern_create_for_surface(img);
84 cairo_set_source(ctx, pattern);
85 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
86 cairo_rectangle(ctx, 0, 0, scr->width_in_pixels, scr->height_in_pixels);
89 /* otherwise, just paint the image */
97 * Called when the user releases a key. We need to leave the Mode_switch
98 * state when the user releases the Mode_switch key.
101 static void handle_key_release(xcb_key_release_event_t *event) {
102 //printf("releasing %d, state raw = %d\n", event->detail, event->state);
105 event->state &= ~numlockmask;
107 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
108 if (sym == XK_Mode_switch) {
109 //printf("Mode switch disabled\n");
110 modeswitch_active = false;
115 * Handle key presses. Fixes state, then looks up the key symbol for the
116 * given keycode, then looks up the key symbol (as UCS-2), converts it to
117 * UTF-8 and stores it in the password array.
120 static void handle_key_press(xcb_key_press_event_t *event) {
121 //printf("keypress %d, state raw = %d\n", event->detail, event->state);
123 xcb_keysym_t sym0, sym1, sym;
124 if (modeswitch_active) {
125 sym0 = xcb_key_press_lookup_keysym(symbols, event, 4);
126 sym1 = xcb_key_press_lookup_keysym(symbols, event, 5);
128 sym0 = xcb_key_press_lookup_keysym(symbols, event, 0);
129 sym1 = xcb_key_press_lookup_keysym(symbols, event, 1);
133 //printf("Mode switch enabled\n");
134 modeswitch_active = true;
142 password[input_position] = '\0';
146 if (input_position == 0)
149 /* decrement input_position to point to the previous glyph */
150 u8_dec(password, &input_position);
151 password[input_position] = '\0';
152 //printf("new input position = %d, new password = %s\n", input_position, password);
156 if ((input_position + 8) >= sizeof(password))
159 if ((event->state & numlockmask) && xcb_is_keypad_key(sym1)) {
160 /* this key was a keypad key */
161 if ((event->state & XCB_MOD_MASK_SHIFT))
165 if ((event->state & XCB_MOD_MASK_SHIFT))
171 /* FIXME: handle all of these? */
172 printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
173 printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
174 printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
175 printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
176 printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
177 printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
178 printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
181 if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
184 //printf("sym = %c (%d)\n", sym, sym);
186 /* convert the keysym to UCS */
187 uint16_t ucs = keysym2ucs(sym);
188 if ((int16_t)ucs == -1) {
189 fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
193 /* store the UCS in a string to convert it */
194 uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
196 /* store it in the password array as UTF-8 */
197 input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
198 password[input_position] = '\0';
199 //printf("current password = %s\n", password);
203 * A visibility notify event will be received when the visibility (= can the
204 * user view the complete window) changes, so for example when a popup overlays
205 * some area of the i3lock window.
207 * In this case, we raise our window on top so that the popup (or whatever is
208 * hiding us) gets hidden.
211 void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
212 printf("visibility notify (window 0x%08x, state %d)\n", event->window, event->state);
213 if (event->state != XCB_VISIBILITY_UNOBSCURED) {
214 printf("window is obscured (not fully visible), raising\n");
215 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
216 xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
222 * Callback function for PAM. We only react on password request callbacks.
225 static int conv_callback(int num_msg, const struct pam_message **msg,
226 struct pam_response **resp, void *appdata_ptr)
231 /* PAM expects an array of responses, one for each message */
232 if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
237 for (int c = 0; c < num_msg; c++) {
238 if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
239 msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
242 /* return code is currently not used but should be set to zero */
243 resp[c]->resp_retcode = 0;
244 if ((resp[c]->resp = strdup(password)) == NULL) {
253 int main(int argc, char *argv[]) {
254 bool dont_fork = false;
256 char color[7] = "ffffff";
259 char *image_path = NULL;
262 struct pam_conv conv = {conv_callback, NULL};
264 xcb_visualtype_t *vistype;
265 xcb_generic_event_t *event;
268 int curs_choice = CURS_NONE;
271 struct option longopts[] = {
272 {"version", no_argument, NULL, 'v'},
273 {"nofork", no_argument, NULL, 'n'},
274 {"beep", no_argument, NULL, 'b'},
275 {"dpms", no_argument, NULL, 'd'},
276 {"color", required_argument, NULL, 'c'},
277 {"pointer", required_argument, NULL , 'p'},
279 {"image", required_argument, NULL, 'i'},
280 {"tiling", no_argument, NULL, 't'},
282 {NULL, no_argument, NULL, 0}
285 if ((username = getenv("USER")) == NULL)
286 errx(1, "USER environment variable not set, please set it.\n");
288 while ((o = getopt_long(argc, argv, "vnbdc:p:"
292 , longopts, &optind)) != -1) {
295 errx(EXIT_SUCCESS, "version " VERSION " © 2010 Michael Stapelberg\n");
308 /* Skip # if present */
312 if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
313 errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
319 image_path = strdup(optarg);
326 if (!strcmp(optarg, "win")) {
327 curs_choice = CURS_WIN;
329 if (!strcmp(optarg, "default")) {
330 curs_choice = CURS_DEFAULT;
334 errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-p win|default]"
336 " [-i image.png] [-t]"
338 " (compiled with NOLIBCAIRO)"
345 ret = pam_start("i3lock", username, &conv, &pam_handle);
346 if (ret != PAM_SUCCESS)
347 errx(EXIT_FAILURE, "PAM: %s\n", pam_strerror(pam_handle, ret));
349 /* Initialize connection to X11 */
350 if ((conn = xcb_connect(NULL, &screen)) == NULL ||
351 xcb_connection_has_error(conn))
352 errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
355 /* In the parent process, we exit */
360 /* if DPMS is enabled, check if the X server really supports it */
362 xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
363 xcb_dpms_capable_reply_t *dpmsr;
364 if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
365 fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
370 scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
371 vistype = get_root_visual_type(scr);
373 /* open the fullscreen window and flush immediately afterwards so that X11
374 * can generate an expose event while we load the PNG file (so that we are
375 * ready to handle the expose event immediately afterwards) */
376 win = open_fullscreen_window(conn, scr, color);
378 cursor = create_cursor(conn, scr, win, curs_choice);
380 grab_pointer_and_keyboard(conn, scr, cursor);
382 symbols = xcb_key_symbols_alloc(conn);
383 modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
384 numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
388 img = cairo_image_surface_create_from_png(image_path);
391 /* Initialize cairo */
392 cairo_surface_t *output;
393 output = cairo_xcb_surface_create(conn, win, vistype,
394 scr->width_in_pixels, scr->height_in_pixels);
395 ctx = cairo_create(output);
397 cairo_set_source_surface(ctx, img, 0, 0);
399 handle_expose_event();
404 dpms_turn_off_screen(conn);
406 while ((event = xcb_wait_for_event(conn))) {
407 if (event->response_type == 0)
408 errx(1, "XCB: Invalid event received");
410 /* Strip off the highest bit (set if the event is generated) */
411 int type = (event->response_type & 0x7F);
413 if (type == XCB_EXPOSE) {
414 handle_expose_event();
418 if (type == XCB_KEY_PRESS) {
419 handle_key_press((xcb_key_press_event_t*)event);
423 if (type == XCB_KEY_RELEASE) {
424 handle_key_release((xcb_key_release_event_t*)event);
426 /* If this was the backspace or escape key we are back at an
427 * empty input, so turn off the screen if DPMS is enabled */
428 if (dpms && input_position == 0)
429 dpms_turn_off_screen(conn);
434 if (type == XCB_VISIBILITY_NOTIFY) {
435 handle_visibility_notify((xcb_visibility_notify_event_t*)event);
439 printf("WARNING: unhandled event of type %d\n", type);