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.

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