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.

746 lines
24 KiB

15 years ago
  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010-2012 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/Xutil.h>
  23. #include <X11/keysym.h>
  24. #include <getopt.h>
  25. #include <string.h>
  26. #include <ev.h>
  27. #include <sys/mman.h>
  28. #ifndef NOLIBCAIRO
  29. #include <cairo.h>
  30. #include <cairo/cairo-xcb.h>
  31. #endif
  32. #include "i3lock.h"
  33. #include "keysym2ucs.h"
  34. #include "ucs2_to_utf8.h"
  35. #include "xcb.h"
  36. #include "cursors.h"
  37. #include "unlock_indicator.h"
  38. #include "xinerama.h"
  39. char color[7] = "ffffff";
  40. uint32_t last_resolution[2];
  41. xcb_window_t win;
  42. static xcb_cursor_t cursor;
  43. static xcb_key_symbols_t *symbols;
  44. static pam_handle_t *pam_handle;
  45. int input_position = 0;
  46. /* Holds the password you enter (in UTF-8). */
  47. static char password[512];
  48. static bool modeswitch_active = false;
  49. static bool iso_level3_shift_active = false;
  50. static bool iso_level5_shift_active = false;
  51. static int numlockmask;
  52. static int shiftlockmask;
  53. static int capslockmask;
  54. static bool beep = false;
  55. bool debug_mode = false;
  56. static bool dpms = false;
  57. bool unlock_indicator = true;
  58. static bool dont_fork = false;
  59. struct ev_loop *main_loop;
  60. static struct ev_timer *clear_pam_wrong_timeout;
  61. extern unlock_state_t unlock_state;
  62. extern pam_state_t pam_state;
  63. #ifndef NOLIBCAIRO
  64. cairo_surface_t *img = NULL;
  65. bool tile = false;
  66. #endif
  67. /*
  68. * Clears the memory which stored the password to be a bit safer against
  69. * cold-boot attacks.
  70. *
  71. */
  72. static void clear_password_memory(void) {
  73. /* A volatile pointer to the password buffer to prevent the compiler from
  74. * optimizing this out. */
  75. volatile char *vpassword = password;
  76. for (int c = 0; c < sizeof(password); c++)
  77. /* We store a non-random pattern which consists of the (irrelevant)
  78. * index plus (!) the value of the beep variable. This prevents the
  79. * compiler from optimizing the calls away, since the value of 'beep'
  80. * is not known at compile-time. */
  81. vpassword[c] = c + (int)beep;
  82. }
  83. /*
  84. * Resets pam_state to STATE_PAM_IDLE 2 seconds after an unsuccesful
  85. * authentication event.
  86. *
  87. */
  88. static void clear_pam_wrong(EV_P_ ev_timer *w, int revents) {
  89. DEBUG("clearing pam wrong\n");
  90. pam_state = STATE_PAM_IDLE;
  91. unlock_state = STATE_STARTED;
  92. redraw_screen();
  93. /* Now free this timeout. */
  94. ev_timer_stop(main_loop, clear_pam_wrong_timeout);
  95. free(clear_pam_wrong_timeout);
  96. clear_pam_wrong_timeout = NULL;
  97. }
  98. static void input_done(void) {
  99. if (input_position == 0)
  100. return;
  101. if (clear_pam_wrong_timeout) {
  102. ev_timer_stop(main_loop, clear_pam_wrong_timeout);
  103. free(clear_pam_wrong_timeout);
  104. clear_pam_wrong_timeout = NULL;
  105. }
  106. pam_state = STATE_PAM_VERIFY;
  107. redraw_screen();
  108. if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
  109. DEBUG("successfully authenticated\n");
  110. clear_password_memory();
  111. exit(0);
  112. }
  113. if (debug_mode)
  114. fprintf(stderr, "Authentication failure\n");
  115. pam_state = STATE_PAM_WRONG;
  116. redraw_screen();
  117. /* Clear this state after 2 seconds (unless the user enters another
  118. * password during that time). */
  119. ev_now_update(main_loop);
  120. if ((clear_pam_wrong_timeout = calloc(sizeof(struct ev_timer), 1))) {
  121. ev_timer_init(clear_pam_wrong_timeout, clear_pam_wrong, 2.0, 0.);
  122. ev_timer_start(main_loop, clear_pam_wrong_timeout);
  123. }
  124. /* Cancel the clear_indicator_timeout, it would hide the unlock indicator
  125. * too early. */
  126. stop_clear_indicator_timeout();
  127. /* beep on authentication failure, if enabled */
  128. if (beep) {
  129. xcb_bell(conn, 100);
  130. xcb_flush(conn);
  131. }
  132. }
  133. /*
  134. * Called when the user releases a key. We need to leave the Mode_switch
  135. * state when the user releases the Mode_switch key.
  136. *
  137. */
  138. static void handle_key_release(xcb_key_release_event_t *event) {
  139. DEBUG("releasing key %d, state raw = %d, modeswitch_active = %d, iso_level3_shift_active = %d, iso_level5_shift_active = %d\n",
  140. event->detail, event->state, modeswitch_active, iso_level3_shift_active, iso_level5_shift_active);
  141. /* We don’t care about the column here and just use the first symbol. Since
  142. * we only check for Mode_switch and ISO_Level3_Shift, this *should* work.
  143. * Also, if we would use the current column, we would look in the wrong
  144. * place. */
  145. xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, 0);
  146. if (sym == XK_Mode_switch) {
  147. //printf("Mode switch disabled\n");
  148. modeswitch_active = false;
  149. } else if (sym == XK_ISO_Level3_Shift) {
  150. iso_level3_shift_active = false;
  151. } else if (sym == XK_ISO_Level5_Shift) {
  152. iso_level5_shift_active = false;
  153. }
  154. DEBUG("release done. modeswitch_active = %d, iso_level3_shift_active = %d, iso_level5_shift_active = %d\n",
  155. modeswitch_active, iso_level3_shift_active, iso_level5_shift_active);
  156. }
  157. static void redraw_timeout(EV_P_ ev_timer *w, int revents) {
  158. redraw_screen();
  159. ev_timer_stop(main_loop, w);
  160. free(w);
  161. }
  162. /*
  163. * Handle key presses. Fixes state, then looks up the key symbol for the
  164. * given keycode, then looks up the key symbol (as UCS-2), converts it to
  165. * UTF-8 and stores it in the password array.
  166. *
  167. */
  168. static void handle_key_press(xcb_key_press_event_t *event) {
  169. DEBUG("keypress %d, state raw = %d, modeswitch_active = %d, iso_level3_shift_active = %d\n",
  170. event->detail, event->state, modeswitch_active, iso_level3_shift_active);
  171. xcb_keysym_t sym0, sym1, sym;
  172. /* For each keycode, there is a list of symbols. The list could look like this:
  173. * $ xmodmap -pke | grep 'keycode 38'
  174. * keycode 38 = a A adiaeresis Adiaeresis o O
  175. * In non-X11 terminology, the symbols for the keycode 38 (the key labeled
  176. * with "a" on my keyboard) are "a A ä Ä o O".
  177. * Another form to display the same information is using xkbcomp:
  178. * $ xkbcomp $DISPLAY /tmp/xkb.dump
  179. * Then open /tmp/xkb.dump and search for '\<a\>' (in VIM regexp-language):
  180. *
  181. * symbols[Group1]= [ a, A, o, O ],
  182. * symbols[Group2]= [ adiaeresis, Adiaeresis ]
  183. *
  184. * So there are two *groups*, one containing 'a A' and one containing 'ä
  185. * Ä'. You can use Mode_switch to switch between these groups. You can use
  186. * ISO_Level3_Shift to reach the 'o O' part of the first group (its the
  187. * same group, just an even higher shift level).
  188. *
  189. * So, using the "logical" XKB information, the following lookup will be
  190. * performed:
  191. *
  192. * Neither Mode_switch nor ISO_Level3_Shift active: group 1, column 0 and 1
  193. * Mode_switch active: group 2, column 0 and 1
  194. * ISO_Level3_Shift active: group 1, column 2 and 3
  195. *
  196. * Using the column index which xcb_key_press_lookup_keysym uses (and
  197. * xmodmap prints out), the following lookup will be performed:
  198. *
  199. * Neither Mode_switch nor ISO_Level3_Shift active: column 0 and 1
  200. * Mode_switch active: column 2 and 3
  201. * ISO_Level3_Shift active: column 4 and 5
  202. */
  203. int base_column = 0;
  204. if (modeswitch_active)
  205. base_column = 2;
  206. if (iso_level3_shift_active)
  207. base_column = 4;
  208. if (iso_level5_shift_active)
  209. base_column = 6;
  210. sym0 = xcb_key_press_lookup_keysym(symbols, event, base_column);
  211. sym1 = xcb_key_press_lookup_keysym(symbols, event, base_column + 1);
  212. switch (sym0) {
  213. case XK_Mode_switch:
  214. DEBUG("Mode switch enabled\n");
  215. modeswitch_active = true;
  216. return;
  217. case XK_ISO_Level3_Shift:
  218. DEBUG("ISO_Level3_Shift enabled\n");
  219. iso_level3_shift_active = true;
  220. return;
  221. case XK_ISO_Level5_Shift:
  222. DEBUG("ISO_Level5_Shift enabled\n");
  223. iso_level5_shift_active = true;
  224. return;
  225. case XK_Return:
  226. case XK_KP_Enter:
  227. input_done();
  228. case XK_Escape:
  229. input_position = 0;
  230. clear_password_memory();
  231. password[input_position] = '\0';
  232. /* Hide the unlock indicator after a bit if the password buffer is
  233. * empty. */
  234. start_clear_indicator_timeout();
  235. unlock_state = STATE_BACKSPACE_ACTIVE;
  236. redraw_screen();
  237. unlock_state = STATE_KEY_PRESSED;
  238. return;
  239. case XK_BackSpace:
  240. if (input_position == 0)
  241. return;
  242. /* decrement input_position to point to the previous glyph */
  243. u8_dec(password, &input_position);
  244. password[input_position] = '\0';
  245. /* Hide the unlock indicator after a bit if the password buffer is
  246. * empty. */
  247. start_clear_indicator_timeout();
  248. unlock_state = STATE_BACKSPACE_ACTIVE;
  249. redraw_screen();
  250. unlock_state = STATE_KEY_PRESSED;
  251. return;
  252. }
  253. if ((input_position + 8) >= sizeof(password))
  254. return;
  255. /* Whether the user currently holds down the shift key. */
  256. bool shift = (event->state & XCB_MOD_MASK_SHIFT);
  257. /* Whether Caps Lock (all lowercase alphabetic keys will be replaced by
  258. * their uppercase variant) is active at the moment. */
  259. bool capslock = (event->state & capslockmask);
  260. /* Whether Shift Lock (shift state is reversed) is active at the moment. */
  261. bool shiftlock = (event->state & shiftlockmask);
  262. /* Whether Caps Lock or Shift Lock is active at the moment. */
  263. bool lock = (capslock || shiftlock);
  264. DEBUG("shift = %d, lock = %d, capslock = %d, shiftlock = %d\n",
  265. shift, lock, capslock, shiftlock);
  266. if ((event->state & numlockmask) && xcb_is_keypad_key(sym1)) {
  267. /* this key was a keypad key */
  268. if (shift || shiftlock)
  269. sym = sym0;
  270. else sym = sym1;
  271. } else {
  272. xcb_keysym_t upper, lower;
  273. XConvertCase(sym0, (KeySym*)&lower, (KeySym*)&upper);
  274. DEBUG("sym0 = %c (%d), sym1 = %c (%d), lower = %c (%d), upper = %c (%d)\n",
  275. sym0, sym0, sym1, sym1, lower, lower, upper, upper);
  276. /* If there is no difference between the uppercase and lowercase
  277. * variant of this key, we consider Caps Lock off it is only relevant
  278. * for alphabetic keys, unlike Shift Lock. */
  279. if (lower == upper) {
  280. capslock = false;
  281. lock = (capslock || shiftlock);
  282. DEBUG("lower == upper, now shift = %d, lock = %d, capslock = %d, shiftlock = %d\n",
  283. shift, lock, capslock, shiftlock);
  284. }
  285. /* In two different cases we need to use the uppercase keysym:
  286. * 1) The user holds shift, no lock is active.
  287. * 2) Any of the two locks is active.
  288. */
  289. if ((shift && !lock) || (!shift && lock))
  290. sym = sym1;
  291. else sym = sym0;
  292. }
  293. #if 0
  294. /* FIXME: handle all of these? */
  295. printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
  296. printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
  297. printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
  298. printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
  299. printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
  300. printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
  301. printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
  302. #endif
  303. if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
  304. return;
  305. DEBUG("resolved to keysym = %c (%d)\n", sym, sym);
  306. /* convert the keysym to UCS */
  307. uint16_t ucs = keysym2ucs(sym);
  308. if ((int16_t)ucs == -1) {
  309. if (debug_mode)
  310. fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
  311. return;
  312. }
  313. /* store the UCS in a string to convert it */
  314. uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
  315. DEBUG("input part = %s\n", inp);
  316. /* store it in the password array as UTF-8 */
  317. input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
  318. password[input_position] = '\0';
  319. DEBUG("current password = %s\n", password);
  320. unlock_state = STATE_KEY_ACTIVE;
  321. redraw_screen();
  322. unlock_state = STATE_KEY_PRESSED;
  323. struct ev_timer *timeout = calloc(sizeof(struct ev_timer), 1);
  324. if (timeout) {
  325. ev_timer_init(timeout, redraw_timeout, 0.25, 0.);
  326. ev_timer_start(main_loop, timeout);
  327. }
  328. stop_clear_indicator_timeout();
  329. }
  330. /*
  331. * A visibility notify event will be received when the visibility (= can the
  332. * user view the complete window) changes, so for example when a popup overlays
  333. * some area of the i3lock window.
  334. *
  335. * In this case, we raise our window on top so that the popup (or whatever is
  336. * hiding us) gets hidden.
  337. *
  338. */
  339. static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
  340. if (event->state != XCB_VISIBILITY_UNOBSCURED) {
  341. uint32_t values[] = { XCB_STACK_MODE_ABOVE };
  342. xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
  343. xcb_flush(conn);
  344. }
  345. }
  346. /*
  347. * Called when the keyboard mapping changes. We update our symbols.
  348. *
  349. */
  350. static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
  351. xcb_refresh_keyboard_mapping(symbols, event);
  352. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  353. }
  354. /*
  355. * Called when the properties on the root window change, e.g. when the screen
  356. * resolution changes. If so we update the window to cover the whole screen
  357. * and also redraw the image, if any.
  358. *
  359. */
  360. void handle_screen_resize(void) {
  361. xcb_get_geometry_cookie_t geomc;
  362. xcb_get_geometry_reply_t *geom;
  363. geomc = xcb_get_geometry(conn, screen->root);
  364. if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL)
  365. return;
  366. if (last_resolution[0] == geom->width &&
  367. last_resolution[1] == geom->height) {
  368. free(geom);
  369. return;
  370. }
  371. last_resolution[0] = geom->width;
  372. last_resolution[1] = geom->height;
  373. free(geom);
  374. #ifndef NOLIBCAIRO
  375. redraw_screen();
  376. #endif
  377. uint32_t mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
  378. xcb_configure_window(conn, win, mask, last_resolution);
  379. xcb_flush(conn);
  380. }
  381. /*
  382. * Callback function for PAM. We only react on password request callbacks.
  383. *
  384. */
  385. static int conv_callback(int num_msg, const struct pam_message **msg,
  386. struct pam_response **resp, void *appdata_ptr)
  387. {
  388. if (num_msg == 0)
  389. return 1;
  390. /* PAM expects an array of responses, one for each message */
  391. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  392. perror("calloc");
  393. return 1;
  394. }
  395. for (int c = 0; c < num_msg; c++) {
  396. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  397. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  398. continue;
  399. /* return code is currently not used but should be set to zero */
  400. resp[c]->resp_retcode = 0;
  401. if ((resp[c]->resp = strdup(password)) == NULL) {
  402. perror("strdup");
  403. return 1;
  404. }
  405. }
  406. return 0;
  407. }
  408. /*
  409. * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
  410. * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
  411. *
  412. */
  413. static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
  414. /* empty, because xcb_prepare_cb and xcb_check_cb are used */
  415. }
  416. /*
  417. * Flush before blocking (and waiting for new events)
  418. *
  419. */
  420. static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
  421. xcb_flush(conn);
  422. }
  423. /*
  424. * Instead of polling the X connection socket we leave this to
  425. * xcb_poll_for_event() which knows better than we can ever know.
  426. *
  427. */
  428. static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
  429. xcb_generic_event_t *event;
  430. while ((event = xcb_poll_for_event(conn)) != NULL) {
  431. if (event->response_type == 0) {
  432. xcb_generic_error_t *error = (xcb_generic_error_t*)event;
  433. if (debug_mode)
  434. fprintf(stderr, "X11 Error received! sequence 0x%x, error_code = %d\n",
  435. error->sequence, error->error_code);
  436. free(event);
  437. continue;
  438. }
  439. /* Strip off the highest bit (set if the event is generated) */
  440. int type = (event->response_type & 0x7F);
  441. switch (type) {
  442. case XCB_KEY_PRESS:
  443. handle_key_press((xcb_key_press_event_t*)event);
  444. break;
  445. case XCB_KEY_RELEASE:
  446. handle_key_release((xcb_key_release_event_t*)event);
  447. /* If this was the backspace or escape key we are back at an
  448. * empty input, so turn off the screen if DPMS is enabled */
  449. if (dpms && input_position == 0)
  450. dpms_turn_off_screen(conn);
  451. break;
  452. case XCB_VISIBILITY_NOTIFY:
  453. handle_visibility_notify((xcb_visibility_notify_event_t*)event);
  454. break;
  455. case XCB_MAP_NOTIFY:
  456. if (!dont_fork) {
  457. /* After the first MapNotify, we never fork again. We don’t
  458. * expect to get another MapNotify, but better be sure */
  459. dont_fork = true;
  460. /* In the parent process, we exit */
  461. if (fork() != 0)
  462. exit(0);
  463. }
  464. break;
  465. case XCB_MAPPING_NOTIFY:
  466. handle_mapping_notify((xcb_mapping_notify_event_t*)event);
  467. break;
  468. case XCB_CONFIGURE_NOTIFY:
  469. handle_screen_resize();
  470. break;
  471. }
  472. free(event);
  473. }
  474. }
  475. int main(int argc, char *argv[]) {
  476. char *username;
  477. #ifndef NOLIBCAIRO
  478. char *image_path = NULL;
  479. #endif
  480. int ret;
  481. struct pam_conv conv = {conv_callback, NULL};
  482. int nscreen;
  483. int curs_choice = CURS_NONE;
  484. char o;
  485. int optind = 0;
  486. struct option longopts[] = {
  487. {"version", no_argument, NULL, 'v'},
  488. {"nofork", no_argument, NULL, 'n'},
  489. {"beep", no_argument, NULL, 'b'},
  490. {"dpms", no_argument, NULL, 'd'},
  491. {"color", required_argument, NULL, 'c'},
  492. {"pointer", required_argument, NULL , 'p'},
  493. {"debug", no_argument, NULL, 0},
  494. {"help", no_argument, NULL, 'h'},
  495. {"no-unlock-indicator", no_argument, NULL, 'u'},
  496. #ifndef NOLIBCAIRO
  497. {"image", required_argument, NULL, 'i'},
  498. {"tiling", no_argument, NULL, 't'},
  499. #endif
  500. {NULL, no_argument, NULL, 0}
  501. };
  502. if ((username = getenv("USER")) == NULL)
  503. errx(1, "USER environment variable not set, please set it.\n");
  504. while ((o = getopt_long(argc, argv, "hvnbdc:p:u"
  505. #ifndef NOLIBCAIRO
  506. "i:t"
  507. #endif
  508. , longopts, &optind)) != -1) {
  509. switch (o) {
  510. case 'v':
  511. errx(EXIT_SUCCESS, "version " VERSION " © 2010-2012 Michael Stapelberg");
  512. case 'n':
  513. dont_fork = true;
  514. break;
  515. case 'b':
  516. beep = true;
  517. break;
  518. case 'd':
  519. dpms = true;
  520. break;
  521. case 'c': {
  522. char *arg = optarg;
  523. /* Skip # if present */
  524. if (arg[0] == '#')
  525. arg++;
  526. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  527. errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
  528. break;
  529. }
  530. case 'u':
  531. unlock_indicator = false;
  532. break;
  533. #ifndef NOLIBCAIRO
  534. case 'i':
  535. image_path = strdup(optarg);
  536. break;
  537. case 't':
  538. tile = true;
  539. break;
  540. #endif
  541. case 'p':
  542. if (!strcmp(optarg, "win")) {
  543. curs_choice = CURS_WIN;
  544. } else if (!strcmp(optarg, "default")) {
  545. curs_choice = CURS_DEFAULT;
  546. } else {
  547. errx(1, "i3lock: Invalid pointer type given. Expected one of \"win\" or \"default\".\n");
  548. }
  549. break;
  550. case 0:
  551. if (strcmp(longopts[optind].name, "debug") == 0)
  552. debug_mode = true;
  553. break;
  554. default:
  555. errx(1, "Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-u] [-p win|default]"
  556. #ifndef NOLIBCAIRO
  557. " [-i image.png] [-t]"
  558. #else
  559. " (compiled with NOLIBCAIRO)"
  560. #endif
  561. );
  562. }
  563. }
  564. /* We need (relatively) random numbers for highlighting a random part of
  565. * the unlock indicator upon keypresses. */
  566. srand(time(NULL));
  567. /* Initialize PAM */
  568. ret = pam_start("i3lock", username, &conv, &pam_handle);
  569. if (ret != PAM_SUCCESS)
  570. errx(EXIT_FAILURE, "PAM: %s", pam_strerror(pam_handle, ret));
  571. /* Lock the area where we store the password in memory, we don’t want it to
  572. * be swapped to disk. Since Linux 2.6.9, this does not require any
  573. * privileges, just enough bytes in the RLIMIT_MEMLOCK limit. */
  574. if (mlock(password, sizeof(password)) != 0)
  575. err(EXIT_FAILURE, "Could not lock page in memory, check RLIMIT_MEMLOCK");
  576. /* Initialize connection to X11 */
  577. if ((conn = xcb_connect(NULL, &nscreen)) == NULL ||
  578. xcb_connection_has_error(conn))
  579. errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
  580. xinerama_init();
  581. xinerama_query_screens();
  582. /* if DPMS is enabled, check if the X server really supports it */
  583. if (dpms) {
  584. xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
  585. xcb_dpms_capable_reply_t *dpmsr;
  586. if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL))) {
  587. if (!dpmsr->capable) {
  588. if (debug_mode)
  589. fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
  590. dpms = false;
  591. }
  592. free(dpmsr);
  593. }
  594. }
  595. screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  596. last_resolution[0] = screen->width_in_pixels;
  597. last_resolution[1] = screen->height_in_pixels;
  598. #ifndef NOLIBCAIRO
  599. if (image_path) {
  600. /* Create a pixmap to render on, fill it with the background color */
  601. img = cairo_image_surface_create_from_png(image_path);
  602. /* In case loading failed, we just pretend no -i was specified. */
  603. if (cairo_surface_status(img) != CAIRO_STATUS_SUCCESS) {
  604. if (debug_mode)
  605. fprintf(stderr, "Could not load image \"%s\": cairo surface status %d\n",
  606. image_path, cairo_surface_status(img));
  607. img = NULL;
  608. }
  609. }
  610. #endif
  611. /* Pixmap on which the image is rendered to (if any) */
  612. xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
  613. /* open the fullscreen window, already with the correct pixmap in place */
  614. win = open_fullscreen_window(conn, screen, color, bg_pixmap);
  615. xcb_free_pixmap(conn, bg_pixmap);
  616. cursor = create_cursor(conn, screen, win, curs_choice);
  617. grab_pointer_and_keyboard(conn, screen, cursor);
  618. symbols = xcb_key_symbols_alloc(conn);
  619. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  620. shiftlockmask = get_mod_mask(conn, symbols, XK_Shift_Lock);
  621. capslockmask = get_mod_mask(conn, symbols, XK_Caps_Lock);
  622. DEBUG("shift lock mask = %d\n", shiftlockmask);
  623. DEBUG("caps lock mask = %d\n", capslockmask);
  624. if (dpms)
  625. dpms_turn_off_screen(conn);
  626. /* Initialize the libev event loop. */
  627. main_loop = EV_DEFAULT;
  628. if (main_loop == NULL)
  629. errx(EXIT_FAILURE, "Could not initialize libev. Bad LIBEV_FLAGS?\n");
  630. struct ev_io *xcb_watcher = calloc(sizeof(struct ev_io), 1);
  631. struct ev_check *xcb_check = calloc(sizeof(struct ev_check), 1);
  632. struct ev_prepare *xcb_prepare = calloc(sizeof(struct ev_prepare), 1);
  633. ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
  634. ev_io_start(main_loop, xcb_watcher);
  635. ev_check_init(xcb_check, xcb_check_cb);
  636. ev_check_start(main_loop, xcb_check);
  637. ev_prepare_init(xcb_prepare, xcb_prepare_cb);
  638. ev_prepare_start(main_loop, xcb_prepare);
  639. /* Invoke the event callback once to catch all the events which were
  640. * received up until now. ev will only pick up new events (when the X11
  641. * file descriptor becomes readable). */
  642. ev_invoke(main_loop, xcb_check, 0);
  643. ev_loop(main_loop, 0);
  644. }