You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

878 lines
29 KiB

14 years ago
15 years ago
  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010-2011 Michael Stapelberg
  5. *
  6. * See LICENSE for licensing information
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <xcb/xcb.h>
  16. #include <xcb/dpms.h>
  17. #include <xcb/xcb_keysyms.h>
  18. #include <err.h>
  19. #include <assert.h>
  20. #include <security/pam_appl.h>
  21. /* FIXME: can we get rid of this header? */
  22. #include <X11/keysym.h>
  23. #include <getopt.h>
  24. #include <string.h>
  25. #include <math.h>
  26. #include <ev.h>
  27. #ifndef NOLIBCAIRO
  28. #include <cairo.h>
  29. #include <cairo/cairo-xcb.h>
  30. #endif
  31. #include "keysym2ucs.h"
  32. #include "ucs2_to_utf8.h"
  33. #include "xcb.h"
  34. #include "cursors.h"
  35. #define BUTTON_RADIUS 90
  36. #define BUTTON_SPACE (BUTTON_RADIUS + 5)
  37. #define BUTTON_CENTER (BUTTON_RADIUS + 5)
  38. #define BUTTON_DIAMETER (5 * BUTTON_SPACE)
  39. static char color[7] = "ffffff";
  40. static uint32_t last_resolution[2];
  41. static xcb_connection_t *conn;
  42. static xcb_window_t win;
  43. static xcb_visualtype_t *vistype;
  44. static xcb_cursor_t cursor;
  45. static xcb_key_symbols_t *symbols;
  46. static xcb_screen_t *scr;
  47. static pam_handle_t *pam_handle;
  48. static int input_position = 0;
  49. /* holds the password you enter (in UTF-8) */
  50. static char password[512];
  51. static bool modeswitch_active = false;
  52. static bool iso_level3_shift_active = false;
  53. static int modeswitchmask;
  54. static int numlockmask;
  55. static bool beep = false;
  56. static bool debug_mode = false;
  57. static bool dpms = false;
  58. static bool unlock_indicator = true;
  59. static struct ev_loop *main_loop;
  60. static struct ev_timer *clear_pam_wrong_timeout;
  61. static struct ev_timer *clear_indicator_timeout;
  62. static enum {
  63. STATE_STARTED = 0, /* default state */
  64. STATE_KEY_PRESSED = 1, /* key was pressed, show unlock indicator */
  65. STATE_KEY_ACTIVE = 2, /* a key was pressed recently, highlight part
  66. of the unlock indicator. */
  67. STATE_BACKSPACE_ACTIVE = 3 /* backspace was pressed recently, highlight
  68. part of the unlock indicator in red. */
  69. } unlock_state;
  70. static enum {
  71. STATE_PAM_IDLE = 0, /* no PAM interaction at the moment */
  72. STATE_PAM_VERIFY = 1, /* currently verifying the password via PAM */
  73. STATE_PAM_WRONG = 2 /* the password was wrong */
  74. } pam_state;
  75. #define DEBUG(fmt, ...) do { \
  76. if (debug_mode) \
  77. printf("[i3lock-debug] " fmt, ##__VA_ARGS__); \
  78. } while (0)
  79. #ifndef NOLIBCAIRO
  80. static cairo_surface_t *img = NULL;
  81. static bool tile = false;
  82. #endif
  83. /*
  84. * Draws global image with fill color onto a pixmap with the given
  85. * resolution and returns it.
  86. *
  87. */
  88. static xcb_pixmap_t draw_image(xcb_visualtype_t *vistype, u_int32_t* resolution) {
  89. xcb_pixmap_t bg_pixmap = XCB_NONE;
  90. #ifndef NOLIBCAIRO
  91. bg_pixmap = create_bg_pixmap(conn, scr, resolution, color);
  92. /* Initialize cairo */
  93. cairo_surface_t *output;
  94. output = cairo_xcb_surface_create(conn, bg_pixmap, vistype,
  95. resolution[0], resolution[1]);
  96. cairo_t *ctx = cairo_create(output);
  97. if (img) {
  98. if (!tile) {
  99. cairo_set_source_surface(ctx, img, 0, 0);
  100. cairo_paint(ctx);
  101. } else {
  102. /* create a pattern and fill a rectangle as big as the screen */
  103. cairo_pattern_t *pattern;
  104. pattern = cairo_pattern_create_for_surface(img);
  105. cairo_set_source(ctx, pattern);
  106. cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
  107. cairo_rectangle(ctx, 0, 0, resolution[0], resolution[1]);
  108. cairo_fill(ctx);
  109. cairo_pattern_destroy(pattern);
  110. }
  111. }
  112. if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
  113. cairo_pattern_t *outer_pat = NULL;
  114. outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
  115. switch (pam_state) {
  116. case STATE_PAM_VERIFY:
  117. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 0, 250.0/255);
  118. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 0, 250.0/255);
  119. break;
  120. case STATE_PAM_WRONG:
  121. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 255.0/250, 139.0/255, 0);
  122. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 125.0/255, 51.0/255, 0);
  123. break;
  124. case STATE_PAM_IDLE:
  125. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 125.0/255, 0);
  126. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 125.0/255, 0);
  127. break;
  128. }
  129. /* Draw a (centered) circle with transparent background. */
  130. cairo_set_line_width(ctx, 10.0);
  131. cairo_arc(ctx,
  132. (resolution[0] / 2) /* x */,
  133. (resolution[1] / 2) /* y */,
  134. BUTTON_RADIUS /* radius */,
  135. 0 /* start */,
  136. 2 * M_PI /* end */);
  137. /* Use the appropriate color for the different PAM states
  138. * (currently verifying, wrong password, or default) */
  139. switch (pam_state) {
  140. case STATE_PAM_VERIFY:
  141. cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
  142. break;
  143. case STATE_PAM_WRONG:
  144. cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
  145. break;
  146. default:
  147. cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
  148. break;
  149. }
  150. cairo_fill_preserve(ctx);
  151. cairo_set_source(ctx, outer_pat);
  152. cairo_stroke(ctx);
  153. /* Draw an inner seperator line. */
  154. cairo_set_source_rgb(ctx, 0, 0, 0);
  155. cairo_set_line_width(ctx, 2.0);
  156. cairo_arc(ctx,
  157. (resolution[0] / 2) /* x */,
  158. (resolution[1] / 2) /* y */,
  159. BUTTON_RADIUS - 5 /* radius */,
  160. 0,
  161. 2 * M_PI);
  162. cairo_stroke(ctx);
  163. cairo_set_line_width(ctx, 10.0);
  164. /* Display a (centered) text of the current PAM state. */
  165. char *text = NULL;
  166. switch (pam_state) {
  167. case STATE_PAM_VERIFY:
  168. text = "verifying…";
  169. break;
  170. case STATE_PAM_WRONG:
  171. text = "wrong!";
  172. break;
  173. default:
  174. break;
  175. }
  176. if (text) {
  177. cairo_text_extents_t extents;
  178. double x, y;
  179. cairo_set_source_rgb(ctx, 0, 0, 0);
  180. cairo_set_font_size(ctx, 28.0);
  181. cairo_text_extents(ctx, text, &extents);
  182. x = (resolution[0] / 2.0) - ((extents.width / 2) + extents.x_bearing);
  183. y = (resolution[1] / 2.0) - ((extents.height / 2) + extents.y_bearing);
  184. cairo_move_to(ctx, x, y);
  185. cairo_show_text(ctx, text);
  186. cairo_close_path(ctx);
  187. }
  188. /* After the user pressed any valid key or the backspace key, we
  189. * highlight a random part of the unlock indicator to confirm this
  190. * keypress. */
  191. if (unlock_state == STATE_KEY_ACTIVE ||
  192. unlock_state == STATE_BACKSPACE_ACTIVE) {
  193. cairo_new_sub_path(ctx);
  194. double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
  195. DEBUG("Highlighting part %.2f\n", highlight_start);
  196. cairo_arc(ctx, resolution[0] / 2 /* x */, resolution[1] / 2 /* y */,
  197. BUTTON_RADIUS /* radius */, highlight_start,
  198. highlight_start + (M_PI / 3.0));
  199. if (unlock_state == STATE_KEY_ACTIVE) {
  200. /* For normal keys, we use a lighter green. */
  201. outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
  202. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 219.0/255, 0);
  203. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 219.0/255, 0);
  204. } else {
  205. /* For backspace, we use red. */
  206. outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
  207. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 219.0/255, 139.0/255, 0);
  208. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 219.0/255, 51.0/255, 0);
  209. }
  210. cairo_set_source(ctx, outer_pat);
  211. cairo_stroke(ctx);
  212. /* Draw two little separators for the highlighted part of the
  213. * unlock indicator. */
  214. cairo_set_source_rgb(ctx, 0, 0, 0);
  215. cairo_arc(ctx,
  216. (resolution[0] / 2) /* x */,
  217. (resolution[1] / 2) /* y */,
  218. BUTTON_RADIUS /* radius */,
  219. highlight_start /* start */,
  220. highlight_start + (M_PI / 128.0) /* end */);
  221. cairo_stroke(ctx);
  222. cairo_arc(ctx,
  223. (resolution[0] / 2) /* x */,
  224. (resolution[1] / 2) /* y */,
  225. BUTTON_RADIUS /* radius */,
  226. highlight_start + (M_PI / 3.0) /* start */,
  227. (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */);
  228. cairo_stroke(ctx);
  229. }
  230. }
  231. cairo_surface_destroy(output);
  232. cairo_destroy(ctx);
  233. #endif
  234. return bg_pixmap;
  235. }
  236. /*
  237. * Calls draw_image on a new pixmap and swaps that with the current pixmap
  238. *
  239. */
  240. static void redraw_screen() {
  241. xcb_pixmap_t bg_pixmap = draw_image(vistype, last_resolution);
  242. xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
  243. /* XXX: Possible optimization: Only update the area in the middle of the
  244. * screen instead of the whole screen. */
  245. xcb_clear_area(conn, 0, win, 0, 0, scr->width_in_pixels, scr->height_in_pixels);
  246. xcb_free_pixmap(conn, bg_pixmap);
  247. xcb_flush(conn);
  248. }
  249. /*
  250. * Resets pam_state to STATE_PAM_IDLE 2 seconds after an unsuccesful
  251. * authentication event.
  252. *
  253. */
  254. static void clear_pam_wrong(EV_P_ ev_timer *w, int revents) {
  255. DEBUG("clearing pam wrong\n");
  256. pam_state = STATE_PAM_IDLE;
  257. unlock_state = STATE_STARTED;
  258. redraw_screen();
  259. }
  260. /*
  261. * Hides the unlock indicator completely when there is no content in the
  262. * password buffer.
  263. *
  264. */
  265. static void clear_indicator(EV_P_ ev_timer *w, int revents) {
  266. if (input_position == 0) {
  267. DEBUG("Clear indicator\n");
  268. unlock_state = STATE_STARTED;
  269. } else unlock_state = STATE_KEY_PRESSED;
  270. redraw_screen();
  271. }
  272. /*
  273. * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
  274. * after an unsuccessful authentication attempt.
  275. *
  276. */
  277. static void start_clear_indicator_timeout() {
  278. if (clear_indicator_timeout) {
  279. ev_timer_stop(main_loop, clear_indicator_timeout);
  280. ev_timer_set(clear_indicator_timeout, 1.0, 0.);
  281. ev_timer_start(main_loop, clear_indicator_timeout);
  282. } else {
  283. clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1);
  284. ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
  285. ev_timer_start(main_loop, clear_indicator_timeout);
  286. }
  287. }
  288. static void input_done() {
  289. if (input_position == 0)
  290. return;
  291. if (clear_pam_wrong_timeout) {
  292. ev_timer_stop(main_loop, clear_pam_wrong_timeout);
  293. clear_pam_wrong_timeout = NULL;
  294. }
  295. pam_state = STATE_PAM_VERIFY;
  296. redraw_screen();
  297. if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
  298. printf("successfully authenticated\n");
  299. exit(0);
  300. }
  301. fprintf(stderr, "Authentication failure\n");
  302. pam_state = STATE_PAM_WRONG;
  303. redraw_screen();
  304. /* Clear this state after 2 seconds (unless the user enters another
  305. * password during that time). */
  306. ev_now_update(main_loop);
  307. clear_pam_wrong_timeout = calloc(sizeof(struct ev_timer), 1);
  308. ev_timer_init(clear_pam_wrong_timeout, clear_pam_wrong, 2.0, 0.);
  309. ev_timer_start(main_loop, clear_pam_wrong_timeout);
  310. /* Cancel the clear_indicator_timeout, it would hide the unlock indicator
  311. * too early. */
  312. if (clear_indicator_timeout) {
  313. ev_timer_stop(main_loop, clear_indicator_timeout);
  314. clear_indicator_timeout = NULL;
  315. }
  316. /* beep on authentication failure, if enabled */
  317. if (beep) {
  318. xcb_bell(conn, 100);
  319. xcb_flush(conn);
  320. }
  321. }
  322. /*
  323. * Called when the user releases a key. We need to leave the Mode_switch
  324. * state when the user releases the Mode_switch key.
  325. *
  326. */
  327. static void handle_key_release(xcb_key_release_event_t *event) {
  328. DEBUG("releasing key %d, state raw = %d, modeswitch_active = %d, iso_level3_shift_active = %d\n",
  329. event->detail, event->state, modeswitch_active, iso_level3_shift_active);
  330. /* We don’t care about the column here and just use the first symbol. Since
  331. * we only check for Mode_switch and ISO_Level3_Shift, this *should* work.
  332. * Also, if we would use the current column, we would look in the wrong
  333. * place. */
  334. xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, 0);
  335. if (sym == XK_Mode_switch) {
  336. //printf("Mode switch disabled\n");
  337. modeswitch_active = false;
  338. } else if (sym == XK_ISO_Level3_Shift) {
  339. iso_level3_shift_active = false;
  340. }
  341. DEBUG("release done. modeswitch_active = %d, iso_level3_shift_active = %d\n",
  342. modeswitch_active, iso_level3_shift_active);
  343. }
  344. static void redraw_timeout(EV_P_ ev_timer *w, int revents) {
  345. redraw_screen();
  346. }
  347. /*
  348. * Handle key presses. Fixes state, then looks up the key symbol for the
  349. * given keycode, then looks up the key symbol (as UCS-2), converts it to
  350. * UTF-8 and stores it in the password array.
  351. *
  352. */
  353. static void handle_key_press(xcb_key_press_event_t *event) {
  354. DEBUG("keypress %d, state raw = %d, modeswitch_active = %d, iso_level3_shift_active = %d\n",
  355. event->detail, event->state, modeswitch_active, iso_level3_shift_active);
  356. xcb_keysym_t sym0, sym1, sym;
  357. /* For each keycode, there is a list of symbols. The list could look like this:
  358. * $ xmodmap -pke | grep 'keycode 38'
  359. * keycode 38 = a A adiaeresis Adiaeresis o O
  360. * In non-X11 terminology, the symbols for the keycode 38 (the key labeled
  361. * with "a" on my keyboard) are "a A ä Ä o O".
  362. * Another form to display the same information is using xkbcomp:
  363. * $ xkbcomp $DISPLAY /tmp/xkb.dump
  364. * Then open /tmp/xkb.dump and search for '\<a\>' (in VIM regexp-language):
  365. *
  366. * symbols[Group1]= [ a, A, o, O ],
  367. * symbols[Group2]= [ adiaeresis, Adiaeresis ]
  368. *
  369. * So there are two *groups*, one containing 'a A' and one containing 'ä
  370. * Ä'. You can use Mode_switch to switch between these groups. You can use
  371. * ISO_Level3_Shift to reach the 'o O' part of the first group (its the
  372. * same group, just an even higher shift level).
  373. *
  374. * So, using the "logical" XKB information, the following lookup will be
  375. * performed:
  376. *
  377. * Neither Mode_switch nor ISO_Level3_Shift active: group 1, column 0 and 1
  378. * Mode_switch active: group 2, column 0 and 1
  379. * ISO_Level3_Shift active: group 1, column 2 and 3
  380. *
  381. * Using the column index which xcb_key_press_lookup_keysym uses (and
  382. * xmodmap prints out), the following lookup will be performed:
  383. *
  384. * Neither Mode_switch nor ISO_Level3_Shift active: column 0 and 1
  385. * Mode_switch active: column 2 and 3
  386. * ISO_Level3_Shift active: column 4 and 5
  387. */
  388. int base_column = 0;
  389. if (modeswitch_active)
  390. base_column = 2;
  391. if (iso_level3_shift_active)
  392. base_column = 4;
  393. sym0 = xcb_key_press_lookup_keysym(symbols, event, base_column);
  394. sym1 = xcb_key_press_lookup_keysym(symbols, event, base_column + 1);
  395. switch (sym0) {
  396. case XK_Mode_switch:
  397. DEBUG("Mode switch enabled\n");
  398. modeswitch_active = true;
  399. return;
  400. case XK_ISO_Level3_Shift:
  401. DEBUG("ISO_Level3_Shift enabled\n");
  402. iso_level3_shift_active = true;
  403. return;
  404. case XK_Return:
  405. case XK_KP_Enter:
  406. input_done();
  407. case XK_Escape:
  408. input_position = 0;
  409. password[input_position] = '\0';
  410. return;
  411. case XK_BackSpace:
  412. if (input_position == 0)
  413. return;
  414. /* decrement input_position to point to the previous glyph */
  415. u8_dec(password, &input_position);
  416. password[input_position] = '\0';
  417. /* Clear this state after 2 seconds (unless the user enters another
  418. * password during that time). */
  419. start_clear_indicator_timeout();
  420. unlock_state = STATE_BACKSPACE_ACTIVE;
  421. redraw_screen();
  422. unlock_state = STATE_KEY_PRESSED;
  423. //printf("new input position = %d, new password = %s\n", input_position, password);
  424. return;
  425. }
  426. if ((input_position + 8) >= sizeof(password))
  427. return;
  428. if ((event->state & numlockmask) && xcb_is_keypad_key(sym1)) {
  429. /* this key was a keypad key */
  430. if ((event->state & XCB_MOD_MASK_SHIFT))
  431. sym = sym0;
  432. else sym = sym1;
  433. } else {
  434. if ((event->state & XCB_MOD_MASK_SHIFT))
  435. sym = sym1;
  436. else sym = sym0;
  437. }
  438. #if 0
  439. /* FIXME: handle all of these? */
  440. printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
  441. printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
  442. printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
  443. printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
  444. printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
  445. printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
  446. printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
  447. #endif
  448. if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
  449. return;
  450. DEBUG("resolved to keysym = %c (%d)\n", sym, sym);
  451. /* convert the keysym to UCS */
  452. uint16_t ucs = keysym2ucs(sym);
  453. if ((int16_t)ucs == -1) {
  454. fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
  455. return;
  456. }
  457. /* store the UCS in a string to convert it */
  458. uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
  459. DEBUG("input part = %s\n", inp);
  460. /* store it in the password array as UTF-8 */
  461. input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
  462. password[input_position] = '\0';
  463. DEBUG("current password = %s\n", password);
  464. unlock_state = STATE_KEY_ACTIVE;
  465. redraw_screen();
  466. unlock_state = STATE_KEY_PRESSED;
  467. struct ev_timer *timeout = calloc(sizeof(struct ev_timer), 1);
  468. ev_timer_init(timeout, redraw_timeout, 0.25, 0.);
  469. ev_timer_start(main_loop, timeout);
  470. if (clear_indicator_timeout) {
  471. ev_timer_stop(main_loop, clear_indicator_timeout);
  472. clear_indicator_timeout = NULL;
  473. }
  474. }
  475. /*
  476. * A visibility notify event will be received when the visibility (= can the
  477. * user view the complete window) changes, so for example when a popup overlays
  478. * some area of the i3lock window.
  479. *
  480. * In this case, we raise our window on top so that the popup (or whatever is
  481. * hiding us) gets hidden.
  482. *
  483. */
  484. static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
  485. if (event->state != XCB_VISIBILITY_UNOBSCURED) {
  486. uint32_t values[] = { XCB_STACK_MODE_ABOVE };
  487. xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
  488. xcb_flush(conn);
  489. }
  490. }
  491. /*
  492. * Called when the keyboard mapping changes. We update our symbols.
  493. *
  494. */
  495. static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
  496. xcb_refresh_keyboard_mapping(symbols, event);
  497. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  498. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  499. }
  500. /*
  501. * Called when the properties on the root window change, e.g. when the screen
  502. * resolution changes. If so we update the window to cover the whole screen
  503. * and also redraw the image, if any.
  504. *
  505. */
  506. void handle_screen_resize(xcb_visualtype_t *vistype, xcb_window_t win, uint32_t* last_resolution) {
  507. xcb_get_geometry_cookie_t geomc;
  508. xcb_get_geometry_reply_t *geom;
  509. geomc = xcb_get_geometry(conn, scr->root);
  510. if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL) {
  511. return;
  512. }
  513. if (last_resolution[0] == geom->width && last_resolution[1] == geom->height)
  514. return;
  515. last_resolution[0] = geom->width;
  516. last_resolution[1] = geom->height;
  517. #ifndef NOLIBCAIRO
  518. if (img) {
  519. xcb_pixmap_t bg_pixmap = draw_image(vistype, last_resolution);
  520. xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
  521. xcb_free_pixmap(conn, bg_pixmap);
  522. }
  523. #endif
  524. uint32_t mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
  525. xcb_configure_window(conn, win, mask, last_resolution);
  526. xcb_flush(conn);
  527. }
  528. /*
  529. * Callback function for PAM. We only react on password request callbacks.
  530. *
  531. */
  532. static int conv_callback(int num_msg, const struct pam_message **msg,
  533. struct pam_response **resp, void *appdata_ptr)
  534. {
  535. if (num_msg == 0)
  536. return 1;
  537. /* PAM expects an array of responses, one for each message */
  538. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  539. perror("calloc");
  540. return 1;
  541. }
  542. for (int c = 0; c < num_msg; c++) {
  543. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  544. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  545. continue;
  546. /* return code is currently not used but should be set to zero */
  547. resp[c]->resp_retcode = 0;
  548. if ((resp[c]->resp = strdup(password)) == NULL) {
  549. perror("strdup");
  550. return 1;
  551. }
  552. }
  553. return 0;
  554. }
  555. /*
  556. * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
  557. * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
  558. *
  559. */
  560. static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
  561. /* empty, because xcb_prepare_cb and xcb_check_cb are used */
  562. }
  563. /*
  564. * Flush before blocking (and waiting for new events)
  565. *
  566. */
  567. static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
  568. xcb_flush(conn);
  569. }
  570. /*
  571. * Instead of polling the X connection socket we leave this to
  572. * xcb_poll_for_event() which knows better than we can ever know.
  573. *
  574. */
  575. static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
  576. xcb_generic_event_t *event;
  577. while ((event = xcb_poll_for_event(conn)) != NULL) {
  578. if (event->response_type == 0) {
  579. xcb_generic_error_t *error = (xcb_generic_error_t*)event;
  580. fprintf(stderr, "X11 Error received! sequence 0x%x, error_code = %d\n",
  581. error->sequence, error->error_code);
  582. free(event);
  583. continue;
  584. }
  585. /* Strip off the highest bit (set if the event is generated) */
  586. int type = (event->response_type & 0x7F);
  587. if (type == XCB_KEY_PRESS) {
  588. handle_key_press((xcb_key_press_event_t*)event);
  589. continue;
  590. }
  591. if (type == XCB_KEY_RELEASE) {
  592. handle_key_release((xcb_key_release_event_t*)event);
  593. /* If this was the backspace or escape key we are back at an
  594. * empty input, so turn off the screen if DPMS is enabled */
  595. if (dpms && input_position == 0)
  596. dpms_turn_off_screen(conn);
  597. continue;
  598. }
  599. if (type == XCB_VISIBILITY_NOTIFY) {
  600. handle_visibility_notify((xcb_visibility_notify_event_t*)event);
  601. continue;
  602. }
  603. if (type == XCB_MAPPING_NOTIFY) {
  604. handle_mapping_notify((xcb_mapping_notify_event_t*)event);
  605. continue;
  606. }
  607. if (type == XCB_CONFIGURE_NOTIFY) {
  608. handle_screen_resize(vistype, win, last_resolution);
  609. continue;
  610. }
  611. printf("WARNING: unhandled event of type %d\n", type);
  612. free(event);
  613. }
  614. }
  615. int main(int argc, char *argv[]) {
  616. bool dont_fork = false;
  617. char *username;
  618. #ifndef NOLIBCAIRO
  619. char *image_path = NULL;
  620. #endif
  621. int ret;
  622. struct pam_conv conv = {conv_callback, NULL};
  623. int screen;
  624. int curs_choice = CURS_NONE;
  625. char o;
  626. int optind = 0;
  627. struct option longopts[] = {
  628. {"version", no_argument, NULL, 'v'},
  629. {"nofork", no_argument, NULL, 'n'},
  630. {"beep", no_argument, NULL, 'b'},
  631. {"dpms", no_argument, NULL, 'd'},
  632. {"color", required_argument, NULL, 'c'},
  633. {"pointer", required_argument, NULL , 'p'},
  634. {"debug", no_argument, NULL, 0},
  635. {"help", no_argument, NULL, 'h'},
  636. {"no-unlock-indicator", no_argument, NULL, 'u'},
  637. #ifndef NOLIBCAIRO
  638. {"image", required_argument, NULL, 'i'},
  639. {"tiling", no_argument, NULL, 't'},
  640. #endif
  641. {NULL, no_argument, NULL, 0}
  642. };
  643. if ((username = getenv("USER")) == NULL)
  644. errx(1, "USER environment variable not set, please set it.\n");
  645. while ((o = getopt_long(argc, argv, "hvnbdc:p:u"
  646. #ifndef NOLIBCAIRO
  647. "i:t"
  648. #endif
  649. , longopts, &optind)) != -1) {
  650. switch (o) {
  651. case 'v':
  652. errx(EXIT_SUCCESS, "version " VERSION " © 2010-2011 Michael Stapelberg");
  653. case 'n':
  654. dont_fork = true;
  655. break;
  656. case 'b':
  657. beep = true;
  658. break;
  659. case 'd':
  660. dpms = true;
  661. break;
  662. case 'c': {
  663. char *arg = optarg;
  664. /* Skip # if present */
  665. if (arg[0] == '#')
  666. arg++;
  667. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  668. errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
  669. break;
  670. }
  671. case 'u':
  672. unlock_indicator = false;
  673. break;
  674. #ifndef NOLIBCAIRO
  675. case 'i':
  676. image_path = strdup(optarg);
  677. break;
  678. case 't':
  679. tile = true;
  680. break;
  681. #endif
  682. case 'p':
  683. if (!strcmp(optarg, "win")) {
  684. curs_choice = CURS_WIN;
  685. } else if (!strcmp(optarg, "default")) {
  686. curs_choice = CURS_DEFAULT;
  687. } else {
  688. errx(1, "i3lock: Invalid pointer type given. Expected one of \"win\" or \"default\".\n");
  689. }
  690. break;
  691. case 0:
  692. if (strcmp(longopts[optind].name, "debug") == 0)
  693. debug_mode = true;
  694. break;
  695. default:
  696. errx(1, "Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-u] [-p win|default]"
  697. #ifndef NOLIBCAIRO
  698. " [-i image.png] [-t]"
  699. #else
  700. " (compiled with NOLIBCAIRO)"
  701. #endif
  702. );
  703. }
  704. }
  705. /* We need (relatively) random numbers for highlighting a random part of
  706. * the unlock indicator upon keypresses. */
  707. srand(time(NULL));
  708. /* Initialize PAM */
  709. ret = pam_start("i3lock", username, &conv, &pam_handle);
  710. if (ret != PAM_SUCCESS)
  711. errx(EXIT_FAILURE, "PAM: %s\n", pam_strerror(pam_handle, ret));
  712. /* Initialize connection to X11 */
  713. if ((conn = xcb_connect(NULL, &screen)) == NULL ||
  714. xcb_connection_has_error(conn))
  715. errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
  716. if (!dont_fork) {
  717. /* In the parent process, we exit */
  718. if (fork() != 0)
  719. return 0;
  720. }
  721. /* if DPMS is enabled, check if the X server really supports it */
  722. if (dpms) {
  723. xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
  724. xcb_dpms_capable_reply_t *dpmsr;
  725. if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
  726. fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
  727. dpms = false;
  728. }
  729. }
  730. scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  731. vistype = get_root_visual_type(scr);
  732. last_resolution[0] = scr->width_in_pixels;
  733. last_resolution[1] = scr->height_in_pixels;
  734. #ifndef NOLIBCAIRO
  735. if (image_path) {
  736. /* Create a pixmap to render on, fill it with the background color */
  737. img = cairo_image_surface_create_from_png(image_path);
  738. }
  739. #endif
  740. /* Pixmap on which the image is rendered to (if any) */
  741. xcb_pixmap_t bg_pixmap = draw_image(vistype, last_resolution);
  742. /* open the fullscreen window, already with the correct pixmap in place */
  743. win = open_fullscreen_window(conn, scr, color, bg_pixmap);
  744. xcb_free_pixmap(conn, bg_pixmap);
  745. cursor = create_cursor(conn, scr, win, curs_choice);
  746. grab_pointer_and_keyboard(conn, scr, cursor);
  747. symbols = xcb_key_symbols_alloc(conn);
  748. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  749. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  750. if (dpms)
  751. dpms_turn_off_screen(conn);
  752. /* Initialize the libev event loop. */
  753. main_loop = EV_DEFAULT;
  754. if (main_loop == NULL)
  755. errx(EXIT_FAILURE, "Could not initialize libev. Bad LIBEV_FLAGS?\n");
  756. struct ev_io *xcb_watcher = calloc(sizeof(struct ev_io), 1);
  757. struct ev_check *xcb_check = calloc(sizeof(struct ev_check), 1);
  758. struct ev_prepare *xcb_prepare = calloc(sizeof(struct ev_prepare), 1);
  759. ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
  760. ev_io_start(main_loop, xcb_watcher);
  761. ev_check_init(xcb_check, xcb_check_cb);
  762. ev_check_start(main_loop, xcb_check);
  763. ev_prepare_init(xcb_prepare, xcb_prepare_cb);
  764. ev_prepare_start(main_loop, xcb_prepare);
  765. xcb_flush(conn);
  766. ev_loop(main_loop, 0);
  767. }