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.

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