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.

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