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.

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