2 * vim:ts=4:sw=4:expandtab
4 * © 2010 Michael Stapelberg
6 * See LICENSE for licensing information
17 #include <xcb/xcb_image.h>
18 #include <xcb/xcb_keysyms.h>
22 #include <security/pam_appl.h>
23 /* FIXME: can we get rid of this header? */
24 #include <X11/keysym.h>
28 #include "keysym2ucs.h"
29 #include "ucs2_to_utf8.h"
32 static xcb_connection_t *conn;
33 static xcb_key_symbols_t *symbols;
34 static cairo_t *ctx = NULL;
35 static pam_handle_t *pam_handle;
36 static int input_position = 0;
37 /* holds the password you enter (in UTF-8) */
38 static char password[512];
39 static bool modeswitch_active = false;
40 static int modeswitchmask;
41 static int numlockmask;
42 static bool beep = false;
44 static void input_done() {
45 if (input_position == 0)
48 /* TODO: change cursor during authentication? */
49 if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
50 printf("successfully authenticated\n");
54 /* beep on authentication failure, if enabled */
62 * Called when we should draw the image (if any).
65 static void handle_expose_event() {
74 * Called when the user releases a key. We need to leave the Mode_switch
75 * state when the user releases the Mode_switch key.
78 static void handle_key_release(xcb_key_release_event_t *event) {
79 printf("releasing %d, state raw = %d\n", event->detail, event->state);
82 event->state &= ~numlockmask;
84 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
85 if (sym == XK_Mode_switch) {
86 printf("Mode switch disabled\n");
87 modeswitch_active = false;
92 * Handle key presses. Fixes state, then looks up the key symbol for the
93 * given keycode, then looks up the key symbol (as UCS-2), converts it to
94 * UTF-8 and stores it in the password array.
97 static void handle_key_press(xcb_key_press_event_t *event) {
98 printf("keypress %d, state raw = %d\n", event->detail, event->state);
101 if (modeswitch_active)
102 event->state |= modeswitchmask;
104 /* Apparantly, after activating numlock once, the numlock modifier
105 * stays turned on (use xev(1) to verify). So, to resolve useful
106 * keysyms, we remove the numlock flag from the event state */
107 event->state &= ~numlockmask;
109 if ((input_position + 8) >= sizeof(password))
112 xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
115 printf("Mode switch enabled\n");
116 modeswitch_active = true;
123 password[input_position] = '\0';
127 if (input_position == 0)
130 /* decrement input_position to point to the previous glyph */
131 u8_dec(password, &input_position);
132 password[input_position] = '\0';
133 printf("new input position = %d, new password = %s\n", input_position, password);
137 /* FIXME: handle all of these? */
138 printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
139 printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
140 printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
141 printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
142 printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
143 printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
144 printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
146 if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
149 printf("sym = %c (%d)\n", sym, sym);
151 /* convert the keysym to UCS */
152 uint16_t ucs = keysym2ucs(sym);
153 if ((int16_t)ucs == -1) {
154 fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
158 /* store the UCS in a string to convert it */
159 uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
161 /* store it in the password array as UTF-8 */
162 input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
163 password[input_position] = '\0';
164 printf("current password = %s\n", password);
168 * Callback function for PAM. We only react on password request callbacks.
171 static int conv_callback(int num_msg, const struct pam_message **msg,
172 struct pam_response **resp, void *appdata_ptr)
177 /* PAM expects an arry of responses, one for each message */
178 if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
183 for (int c = 0; c < num_msg; c++) {
184 if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
185 msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
188 /* return code is currently not used but should be set to zero */
189 resp[c]->resp_retcode = 0;
190 if ((resp[c]->resp = strdup(password)) == NULL) {
199 int main(int argc, char *argv[]) {
200 bool dont_fork = false;
202 char color[7] = "ffffff";
204 char *image_path = NULL;
206 struct pam_conv conv = {conv_callback, NULL};
208 cairo_surface_t *img = NULL;
209 xcb_visualtype_t *vistype;
210 xcb_generic_event_t *event;
215 struct option longopts[] = {
216 {"version", no_argument, NULL, 'v'},
217 {"nofork", no_argument, NULL, 'n'},
218 {"beep", no_argument, NULL, 'b'},
219 {"dpms", no_argument, NULL, 'd'},
220 {"image", required_argument, NULL, 'i'},
221 {"color", required_argument, NULL, 'c'},
222 {"tiling", no_argument, NULL, 't'},
223 {"pointer", required_argument, NULL , 'p'},
224 {NULL, no_argument, NULL, 0}
227 if ((username = getenv("USER")) == NULL)
228 errx(1, "USER environment variable not set, please set it.\n");
230 while ((o = getopt_long(argc, argv, "vnbdi:c:tp:", longopts, &optind)) != -1) {
233 errx(EXIT_SUCCESS, "i3lock © 2010 Michael Stapelberg\n");
244 image_path = strdup(optarg);
249 /* Skip # if present */
253 if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
254 errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
259 /* TODO: tile image */
265 errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.png] [-c color] [-t] [-p win|default]\n");
270 ret = pam_start("i3lock", username, &conv, &pam_handle);
271 if (ret != PAM_SUCCESS)
272 errx(EXIT_FAILURE, "PAM: %s\n", pam_strerror(pam_handle, ret));
274 /* Initialize connection to X11 */
275 if ((conn = xcb_connect(NULL, &screen)) == NULL)
276 err(EXIT_FAILURE, "xcb_connect()");
279 /* In the parent process, we exit */
284 /* if DPMS is enabled, check if the X server really supports it */
286 xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
287 xcb_dpms_capable_reply_t *dpmsr;
288 if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
289 fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
294 scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
295 vistype = get_root_visual_type(scr);
297 /* open the fullscreen window and flush immediately afterwards so that X11
298 * can generate an expose event while we load the PNG file (so that we are
299 * ready to handle the expose event immediately afterwards) */
300 win = open_fullscreen_window(conn, scr, color);
302 grab_pointer_and_keyboard(conn, scr);
305 img = cairo_image_surface_create_from_png(image_path);
307 symbols = xcb_key_symbols_alloc(conn);
308 modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
309 numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
313 /* Initialize cairo */
314 cairo_surface_t *output;
315 output = cairo_xcb_surface_create(conn, win, vistype,
316 scr->width_in_pixels, scr->height_in_pixels);
317 /* TODO: tiling of the image */
318 ctx = cairo_create(output);
319 cairo_set_source_surface(ctx, img, 0, 0);
321 handle_expose_event();
325 dpms_turn_off_screen(conn);
327 while ((event = xcb_wait_for_event(conn))) {
328 int type = x_event_type(event);
330 if (type == XCB_EXPOSE) {
331 handle_expose_event();
335 if (type == XCB_KEY_PRESS) {
336 handle_key_press((xcb_key_press_event_t*)event);
340 if (type == XCB_KEY_RELEASE) {
341 handle_key_release((xcb_key_release_event_t*)event);
343 /* If this was the backspace or escape key we are back at an
344 * empty input, so turn off the screen if DPMS is enabled */
345 if (dpms && input_position == 0)
346 dpms_turn_off_screen(conn);
351 printf("WARNING: unhandled event of type %d\n", type);