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.

730 lines
22 KiB

15 years ago
12 years ago
  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010-2013 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 <err.h>
  18. #include <assert.h>
  19. #include <security/pam_appl.h>
  20. #include <X11/Xlib-xcb.h>
  21. #include <getopt.h>
  22. #include <string.h>
  23. #include <ev.h>
  24. #include <sys/mman.h>
  25. #include <X11/XKBlib.h>
  26. #include <X11/extensions/XKBfile.h>
  27. #include <xkbcommon/xkbcommon.h>
  28. #include <cairo.h>
  29. #include <cairo/cairo-xcb.h>
  30. #include "i3lock.h"
  31. #include "xcb.h"
  32. #include "cursors.h"
  33. #include "unlock_indicator.h"
  34. #include "xinerama.h"
  35. /* We need this for libxkbfile */
  36. static Display *display;
  37. char color[7] = "ffffff";
  38. uint32_t last_resolution[2];
  39. xcb_window_t win;
  40. static xcb_cursor_t cursor;
  41. static pam_handle_t *pam_handle;
  42. int input_position = 0;
  43. /* Holds the password you enter (in UTF-8). */
  44. static char password[512];
  45. static bool beep = false;
  46. bool debug_mode = false;
  47. static bool dpms = false;
  48. bool unlock_indicator = true;
  49. static bool dont_fork = false;
  50. struct ev_loop *main_loop;
  51. static struct ev_timer *clear_pam_wrong_timeout;
  52. extern unlock_state_t unlock_state;
  53. extern pam_state_t pam_state;
  54. static struct xkb_state *xkb_state;
  55. static struct xkb_context *xkb_context;
  56. static struct xkb_keymap *xkb_keymap;
  57. cairo_surface_t *img = NULL;
  58. bool tile = false;
  59. bool ignore_empty_password = false;
  60. /* isutf, u8_dec © 2005 Jeff Bezanson, public domain */
  61. #define isutf(c) (((c) & 0xC0) != 0x80)
  62. /*
  63. * Decrements i to point to the previous unicode glyph
  64. *
  65. */
  66. void u8_dec(char *s, int *i) {
  67. (void)(isutf(s[--(*i)]) || isutf(s[--(*i)]) || isutf(s[--(*i)]) || --(*i));
  68. }
  69. /*
  70. * Loads the XKB keymap from the X11 server and feeds it to xkbcommon.
  71. * Necessary so that we can properly let xkbcommon track the keyboard state and
  72. * translate keypresses to utf-8.
  73. *
  74. * Ideally, xkbcommon would ship something like this itself, but as of now
  75. * (version 0.2.0), it doesnt.
  76. *
  77. * TODO: Once xcb-xkb is enabled by default and released, we should port this
  78. * code to xcb-xkb. See also https://github.com/xkbcommon/libxkbcommon/issues/1
  79. *
  80. */
  81. static bool load_keymap(void) {
  82. bool ret = false;
  83. XkbFileInfo result;
  84. memset(&result, '\0', sizeof(result));
  85. result.xkb = XkbGetKeyboard(display, XkbAllMapComponentsMask, XkbUseCoreKbd);
  86. if (result.xkb == NULL) {
  87. fprintf(stderr, "[i3lock] XKB: XkbGetKeyboard failed\n");
  88. return false;
  89. }
  90. FILE *temp = tmpfile();
  91. if (temp == NULL) {
  92. fprintf(stderr, "[i3lock] could not create tempfile\n");
  93. return false;
  94. }
  95. bool ok = XkbWriteXKBKeymap(temp, &result, false, false, NULL, NULL);
  96. if (!ok) {
  97. fprintf(stderr, "[i3lock] XkbWriteXKBKeymap failed\n");
  98. goto out;
  99. }
  100. rewind(temp);
  101. if (xkb_context == NULL) {
  102. if ((xkb_context = xkb_context_new(0)) == NULL) {
  103. fprintf(stderr, "[i3lock] could not create xkbcommon context\n");
  104. goto out;
  105. }
  106. }
  107. if (xkb_keymap != NULL)
  108. xkb_keymap_unref(xkb_keymap);
  109. if ((xkb_keymap = xkb_keymap_new_from_file(xkb_context, temp, XKB_KEYMAP_FORMAT_TEXT_V1, 0)) == NULL) {
  110. fprintf(stderr, "[i3lock] xkb_keymap_new_from_file failed\n");
  111. goto out;
  112. }
  113. struct xkb_state *new_state = xkb_state_new(xkb_keymap);
  114. if (new_state == NULL) {
  115. fprintf(stderr, "[i3lock] xkb_state_new failed\n");
  116. goto out;
  117. }
  118. /* Get the initial modifier state to be in sync with the X server.
  119. * See https://github.com/xkbcommon/libxkbcommon/issues/1 for why we ignore
  120. * the base and latched fields. */
  121. XkbStateRec state_rec;
  122. XkbGetState(display, XkbUseCoreKbd, &state_rec);
  123. xkb_state_update_mask(new_state,
  124. 0, 0, state_rec.locked_mods,
  125. 0, 0, state_rec.locked_group);
  126. if (xkb_state != NULL)
  127. xkb_state_unref(xkb_state);
  128. xkb_state = new_state;
  129. ret = true;
  130. out:
  131. XkbFreeKeyboard(result.xkb, XkbAllComponentsMask, true);
  132. fclose(temp);
  133. return ret;
  134. }
  135. /*
  136. * Clears the memory which stored the password to be a bit safer against
  137. * cold-boot attacks.
  138. *
  139. */
  140. static void clear_password_memory(void) {
  141. /* A volatile pointer to the password buffer to prevent the compiler from
  142. * optimizing this out. */
  143. volatile char *vpassword = password;
  144. for (int c = 0; c < sizeof(password); c++)
  145. /* We store a non-random pattern which consists of the (irrelevant)
  146. * index plus (!) the value of the beep variable. This prevents the
  147. * compiler from optimizing the calls away, since the value of 'beep'
  148. * is not known at compile-time. */
  149. vpassword[c] = c + (int)beep;
  150. }
  151. /*
  152. * Resets pam_state to STATE_PAM_IDLE 2 seconds after an unsuccesful
  153. * authentication event.
  154. *
  155. */
  156. static void clear_pam_wrong(EV_P_ ev_timer *w, int revents) {
  157. DEBUG("clearing pam wrong\n");
  158. pam_state = STATE_PAM_IDLE;
  159. unlock_state = STATE_STARTED;
  160. redraw_screen();
  161. /* Now free this timeout. */
  162. ev_timer_stop(main_loop, clear_pam_wrong_timeout);
  163. free(clear_pam_wrong_timeout);
  164. clear_pam_wrong_timeout = NULL;
  165. }
  166. static void clear_input(void) {
  167. input_position = 0;
  168. clear_password_memory();
  169. password[input_position] = '\0';
  170. /* Hide the unlock indicator after a bit if the password buffer is
  171. * empty. */
  172. start_clear_indicator_timeout();
  173. unlock_state = STATE_BACKSPACE_ACTIVE;
  174. redraw_screen();
  175. unlock_state = STATE_KEY_PRESSED;
  176. }
  177. static void input_done(void) {
  178. if (clear_pam_wrong_timeout) {
  179. ev_timer_stop(main_loop, clear_pam_wrong_timeout);
  180. free(clear_pam_wrong_timeout);
  181. clear_pam_wrong_timeout = NULL;
  182. }
  183. pam_state = STATE_PAM_VERIFY;
  184. redraw_screen();
  185. if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
  186. DEBUG("successfully authenticated\n");
  187. clear_password_memory();
  188. exit(0);
  189. }
  190. if (debug_mode)
  191. fprintf(stderr, "Authentication failure\n");
  192. pam_state = STATE_PAM_WRONG;
  193. clear_input();
  194. redraw_screen();
  195. /* Clear this state after 2 seconds (unless the user enters another
  196. * password during that time). */
  197. ev_now_update(main_loop);
  198. if ((clear_pam_wrong_timeout = calloc(sizeof(struct ev_timer), 1))) {
  199. ev_timer_init(clear_pam_wrong_timeout, clear_pam_wrong, 2.0, 0.);
  200. ev_timer_start(main_loop, clear_pam_wrong_timeout);
  201. }
  202. /* Cancel the clear_indicator_timeout, it would hide the unlock indicator
  203. * too early. */
  204. stop_clear_indicator_timeout();
  205. /* beep on authentication failure, if enabled */
  206. if (beep) {
  207. xcb_bell(conn, 100);
  208. xcb_flush(conn);
  209. }
  210. }
  211. /*
  212. * Called when the user releases a key. We need to leave the Mode_switch
  213. * state when the user releases the Mode_switch key.
  214. *
  215. */
  216. static void handle_key_release(xcb_key_release_event_t *event) {
  217. xkb_state_update_key(xkb_state, event->detail, XKB_KEY_UP);
  218. }
  219. static void redraw_timeout(EV_P_ ev_timer *w, int revents) {
  220. redraw_screen();
  221. ev_timer_stop(main_loop, w);
  222. free(w);
  223. }
  224. /*
  225. * Handle key presses. Fixes state, then looks up the key symbol for the
  226. * given keycode, then looks up the key symbol (as UCS-2), converts it to
  227. * UTF-8 and stores it in the password array.
  228. *
  229. */
  230. static void handle_key_press(xcb_key_press_event_t *event) {
  231. xkb_keysym_t ksym;
  232. char buffer[128];
  233. int n;
  234. bool ctrl;
  235. ksym = xkb_state_key_get_one_sym(xkb_state, event->detail);
  236. ctrl = xkb_state_mod_name_is_active(xkb_state, "Control", XKB_STATE_MODS_DEPRESSED);
  237. xkb_state_update_key(xkb_state, event->detail, XKB_KEY_DOWN);
  238. /* The buffer will be null-terminated, so n >= 2 for 1 actual character. */
  239. memset(buffer, '\0', sizeof(buffer));
  240. n = xkb_keysym_to_utf8(ksym, buffer, sizeof(buffer));
  241. switch (ksym) {
  242. case XKB_KEY_Return:
  243. case XKB_KEY_KP_Enter:
  244. case XKB_KEY_XF86ScreenSaver:
  245. if (ignore_empty_password && input_position == 0) {
  246. clear_input();
  247. return;
  248. }
  249. password[input_position] = '\0';
  250. unlock_state = STATE_KEY_PRESSED;
  251. redraw_screen();
  252. input_done();
  253. return;
  254. case XKB_KEY_u:
  255. if (ctrl) {
  256. DEBUG("C-u pressed\n");
  257. clear_input();
  258. return;
  259. }
  260. break;
  261. case XKB_KEY_Escape:
  262. clear_input();
  263. return;
  264. case XKB_KEY_BackSpace:
  265. if (input_position == 0)
  266. return;
  267. /* decrement input_position to point to the previous glyph */
  268. u8_dec(password, &input_position);
  269. password[input_position] = '\0';
  270. /* Hide the unlock indicator after a bit if the password buffer is
  271. * empty. */
  272. start_clear_indicator_timeout();
  273. unlock_state = STATE_BACKSPACE_ACTIVE;
  274. redraw_screen();
  275. unlock_state = STATE_KEY_PRESSED;
  276. return;
  277. }
  278. if ((input_position + 8) >= sizeof(password))
  279. return;
  280. #if 0
  281. /* FIXME: handle all of these? */
  282. printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
  283. printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
  284. printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
  285. printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
  286. printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
  287. printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
  288. printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
  289. #endif
  290. if (n < 2)
  291. return;
  292. /* store it in the password array as UTF-8 */
  293. memcpy(password+input_position, buffer, n-1);
  294. input_position += n-1;
  295. DEBUG("current password = %.*s\n", input_position, password);
  296. unlock_state = STATE_KEY_ACTIVE;
  297. redraw_screen();
  298. unlock_state = STATE_KEY_PRESSED;
  299. struct ev_timer *timeout = calloc(sizeof(struct ev_timer), 1);
  300. if (timeout) {
  301. ev_timer_init(timeout, redraw_timeout, 0.25, 0.);
  302. ev_timer_start(main_loop, timeout);
  303. }
  304. stop_clear_indicator_timeout();
  305. }
  306. /*
  307. * A visibility notify event will be received when the visibility (= can the
  308. * user view the complete window) changes, so for example when a popup overlays
  309. * some area of the i3lock window.
  310. *
  311. * In this case, we raise our window on top so that the popup (or whatever is
  312. * hiding us) gets hidden.
  313. *
  314. */
  315. static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
  316. if (event->state != XCB_VISIBILITY_UNOBSCURED) {
  317. uint32_t values[] = { XCB_STACK_MODE_ABOVE };
  318. xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
  319. xcb_flush(conn);
  320. }
  321. }
  322. /*
  323. * Called when the keyboard mapping changes. We update our symbols.
  324. *
  325. */
  326. static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
  327. /* We ignore errors — if the new keymap cannot be loaded it’s better if the
  328. * screen stays locked and the user intervenes by using killall i3lock. */
  329. (void)load_keymap();
  330. }
  331. /*
  332. * Called when the properties on the root window change, e.g. when the screen
  333. * resolution changes. If so we update the window to cover the whole screen
  334. * and also redraw the image, if any.
  335. *
  336. */
  337. void handle_screen_resize(void) {
  338. xcb_get_geometry_cookie_t geomc;
  339. xcb_get_geometry_reply_t *geom;
  340. geomc = xcb_get_geometry(conn, screen->root);
  341. if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL)
  342. return;
  343. if (last_resolution[0] == geom->width &&
  344. last_resolution[1] == geom->height) {
  345. free(geom);
  346. return;
  347. }
  348. last_resolution[0] = geom->width;
  349. last_resolution[1] = geom->height;
  350. free(geom);
  351. redraw_screen();
  352. uint32_t mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
  353. xcb_configure_window(conn, win, mask, last_resolution);
  354. xcb_flush(conn);
  355. xinerama_query_screens();
  356. redraw_screen();
  357. }
  358. /*
  359. * Callback function for PAM. We only react on password request callbacks.
  360. *
  361. */
  362. static int conv_callback(int num_msg, const struct pam_message **msg,
  363. struct pam_response **resp, void *appdata_ptr)
  364. {
  365. if (num_msg == 0)
  366. return 1;
  367. /* PAM expects an array of responses, one for each message */
  368. if ((*resp = calloc(num_msg, sizeof(struct pam_response))) == NULL) {
  369. perror("calloc");
  370. return 1;
  371. }
  372. for (int c = 0; c < num_msg; c++) {
  373. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  374. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  375. continue;
  376. /* return code is currently not used but should be set to zero */
  377. resp[c]->resp_retcode = 0;
  378. if ((resp[c]->resp = strdup(password)) == NULL) {
  379. perror("strdup");
  380. return 1;
  381. }
  382. }
  383. return 0;
  384. }
  385. /*
  386. * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
  387. * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
  388. *
  389. */
  390. static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
  391. /* empty, because xcb_prepare_cb and xcb_check_cb are used */
  392. }
  393. /*
  394. * Flush before blocking (and waiting for new events)
  395. *
  396. */
  397. static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
  398. xcb_flush(conn);
  399. }
  400. /*
  401. * Instead of polling the X connection socket we leave this to
  402. * xcb_poll_for_event() which knows better than we can ever know.
  403. *
  404. */
  405. static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
  406. xcb_generic_event_t *event;
  407. while ((event = xcb_poll_for_event(conn)) != NULL) {
  408. if (event->response_type == 0) {
  409. xcb_generic_error_t *error = (xcb_generic_error_t*)event;
  410. if (debug_mode)
  411. fprintf(stderr, "X11 Error received! sequence 0x%x, error_code = %d\n",
  412. error->sequence, error->error_code);
  413. free(event);
  414. continue;
  415. }
  416. /* Strip off the highest bit (set if the event is generated) */
  417. int type = (event->response_type & 0x7F);
  418. switch (type) {
  419. case XCB_KEY_PRESS:
  420. handle_key_press((xcb_key_press_event_t*)event);
  421. break;
  422. case XCB_KEY_RELEASE:
  423. handle_key_release((xcb_key_release_event_t*)event);
  424. /* If this was the backspace or escape key we are back at an
  425. * empty input, so turn off the screen if DPMS is enabled */
  426. if (dpms && input_position == 0)
  427. dpms_turn_off_screen(conn);
  428. break;
  429. case XCB_VISIBILITY_NOTIFY:
  430. handle_visibility_notify((xcb_visibility_notify_event_t*)event);
  431. break;
  432. case XCB_MAP_NOTIFY:
  433. if (!dont_fork) {
  434. /* After the first MapNotify, we never fork again. We don’t
  435. * expect to get another MapNotify, but better be sure */
  436. dont_fork = true;
  437. /* In the parent process, we exit */
  438. if (fork() != 0)
  439. exit(0);
  440. ev_loop_fork(EV_DEFAULT);
  441. }
  442. break;
  443. case XCB_MAPPING_NOTIFY:
  444. handle_mapping_notify((xcb_mapping_notify_event_t*)event);
  445. break;
  446. case XCB_CONFIGURE_NOTIFY:
  447. handle_screen_resize();
  448. break;
  449. }
  450. free(event);
  451. }
  452. }
  453. int main(int argc, char *argv[]) {
  454. char *username;
  455. char *image_path = NULL;
  456. int ret;
  457. struct pam_conv conv = {conv_callback, NULL};
  458. int curs_choice = CURS_NONE;
  459. int o;
  460. int optind = 0;
  461. struct option longopts[] = {
  462. {"version", no_argument, NULL, 'v'},
  463. {"nofork", no_argument, NULL, 'n'},
  464. {"beep", no_argument, NULL, 'b'},
  465. {"dpms", no_argument, NULL, 'd'},
  466. {"color", required_argument, NULL, 'c'},
  467. {"pointer", required_argument, NULL , 'p'},
  468. {"debug", no_argument, NULL, 0},
  469. {"help", no_argument, NULL, 'h'},
  470. {"no-unlock-indicator", no_argument, NULL, 'u'},
  471. {"image", required_argument, NULL, 'i'},
  472. {"tiling", no_argument, NULL, 't'},
  473. {"ignore-empty-password", no_argument, NULL, 'e'},
  474. {NULL, no_argument, NULL, 0}
  475. };
  476. if ((username = getenv("USER")) == NULL)
  477. errx(1, "USER environment variable not set, please set it.\n");
  478. while ((o = getopt_long(argc, argv, "hvnbdc:p:ui:te", longopts, &optind)) != -1) {
  479. switch (o) {
  480. case 'v':
  481. errx(EXIT_SUCCESS, "version " VERSION " © 2010-2012 Michael Stapelberg");
  482. case 'n':
  483. dont_fork = true;
  484. break;
  485. case 'b':
  486. beep = true;
  487. break;
  488. case 'd':
  489. dpms = true;
  490. break;
  491. case 'c': {
  492. char *arg = optarg;
  493. /* Skip # if present */
  494. if (arg[0] == '#')
  495. arg++;
  496. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  497. errx(1, "color is invalid, it must be given in 3-byte hexadecimal format: rrggbb\n");
  498. break;
  499. }
  500. case 'u':
  501. unlock_indicator = false;
  502. break;
  503. case 'i':
  504. image_path = strdup(optarg);
  505. break;
  506. case 't':
  507. tile = true;
  508. break;
  509. case 'p':
  510. if (!strcmp(optarg, "win")) {
  511. curs_choice = CURS_WIN;
  512. } else if (!strcmp(optarg, "default")) {
  513. curs_choice = CURS_DEFAULT;
  514. } else {
  515. errx(1, "i3lock: Invalid pointer type given. Expected one of \"win\" or \"default\".\n");
  516. }
  517. break;
  518. case 'e':
  519. ignore_empty_password = true;
  520. break;
  521. case 0:
  522. if (strcmp(longopts[optind].name, "debug") == 0)
  523. debug_mode = true;
  524. break;
  525. default:
  526. errx(1, "Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-u] [-p win|default]"
  527. " [-i image.png] [-t] [-e]"
  528. );
  529. }
  530. }
  531. /* We need (relatively) random numbers for highlighting a random part of
  532. * the unlock indicator upon keypresses. */
  533. srand(time(NULL));
  534. /* Initialize PAM */
  535. ret = pam_start("i3lock", username, &conv, &pam_handle);
  536. if (ret != PAM_SUCCESS)
  537. errx(EXIT_FAILURE, "PAM: %s", pam_strerror(pam_handle, ret));
  538. /* Using mlock() as non-super-user seems only possible in Linux. Users of other
  539. * operating systems should use encrypted swap/no swap (or remove the ifdef and
  540. * run i3lock as super-user). */
  541. #if defined(__linux__)
  542. /* Lock the area where we store the password in memory, we don’t want it to
  543. * be swapped to disk. Since Linux 2.6.9, this does not require any
  544. * privileges, just enough bytes in the RLIMIT_MEMLOCK limit. */
  545. if (mlock(password, sizeof(password)) != 0)
  546. err(EXIT_FAILURE, "Could not lock page in memory, check RLIMIT_MEMLOCK");
  547. #endif
  548. /* Initialize connection to X11 */
  549. if ((display = XOpenDisplay(NULL)) == NULL)
  550. errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
  551. XSetEventQueueOwner(display, XCBOwnsEventQueue);
  552. conn = XGetXCBConnection(display);
  553. /* Double checking that connection is good and operatable with xcb */
  554. if (xcb_connection_has_error(conn))
  555. errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
  556. /* When we cannot initially load the keymap, we better exit */
  557. if (!load_keymap())
  558. errx(EXIT_FAILURE, "Could not load keymap");
  559. xinerama_init();
  560. xinerama_query_screens();
  561. /* if DPMS is enabled, check if the X server really supports it */
  562. if (dpms) {
  563. xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
  564. xcb_dpms_capable_reply_t *dpmsr;
  565. if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL))) {
  566. if (!dpmsr->capable) {
  567. if (debug_mode)
  568. fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
  569. dpms = false;
  570. }
  571. free(dpmsr);
  572. }
  573. }
  574. screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  575. last_resolution[0] = screen->width_in_pixels;
  576. last_resolution[1] = screen->height_in_pixels;
  577. xcb_change_window_attributes(conn, screen->root, XCB_CW_EVENT_MASK,
  578. (uint32_t[]){ XCB_EVENT_MASK_STRUCTURE_NOTIFY });
  579. if (image_path) {
  580. /* Create a pixmap to render on, fill it with the background color */
  581. img = cairo_image_surface_create_from_png(image_path);
  582. /* In case loading failed, we just pretend no -i was specified. */
  583. if (cairo_surface_status(img) != CAIRO_STATUS_SUCCESS) {
  584. fprintf(stderr, "Could not load image \"%s\": %s\n",
  585. image_path, cairo_status_to_string(cairo_surface_status(img)));
  586. img = NULL;
  587. }
  588. }
  589. /* Pixmap on which the image is rendered to (if any) */
  590. xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
  591. /* open the fullscreen window, already with the correct pixmap in place */
  592. win = open_fullscreen_window(conn, screen, color, bg_pixmap);
  593. xcb_free_pixmap(conn, bg_pixmap);
  594. cursor = create_cursor(conn, screen, win, curs_choice);
  595. grab_pointer_and_keyboard(conn, screen, cursor);
  596. /* Load the keymap again to sync the current modifier state. Since we first
  597. * loaded the keymap, there might have been changes, but starting from now,
  598. * we should get all key presses/releases due to having grabbed the
  599. * keyboard. */
  600. (void)load_keymap();
  601. if (dpms)
  602. dpms_turn_off_screen(conn);
  603. /* Initialize the libev event loop. */
  604. main_loop = EV_DEFAULT;
  605. if (main_loop == NULL)
  606. errx(EXIT_FAILURE, "Could not initialize libev. Bad LIBEV_FLAGS?\n");
  607. struct ev_io *xcb_watcher = calloc(sizeof(struct ev_io), 1);
  608. struct ev_check *xcb_check = calloc(sizeof(struct ev_check), 1);
  609. struct ev_prepare *xcb_prepare = calloc(sizeof(struct ev_prepare), 1);
  610. ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
  611. ev_io_start(main_loop, xcb_watcher);
  612. ev_check_init(xcb_check, xcb_check_cb);
  613. ev_check_start(main_loop, xcb_check);
  614. ev_prepare_init(xcb_prepare, xcb_prepare_cb);
  615. ev_prepare_start(main_loop, xcb_prepare);
  616. /* Invoke the event callback once to catch all the events which were
  617. * received up until now. ev will only pick up new events (when the X11
  618. * file descriptor becomes readable). */
  619. ev_invoke(main_loop, xcb_check, 0);
  620. ev_loop(main_loop, 0);
  621. }