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.

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