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.

742 lines
24 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/Xutil.h>
  23. #include <X11/keysym.h>
  24. #include <getopt.h>
  25. #include <string.h>
  26. #include <ev.h>
  27. #include <sys/mman.h>
  28. #ifndef NOLIBCAIRO
  29. #include <cairo.h>
  30. #include <cairo/cairo-xcb.h>
  31. #endif
  32. #include "i3lock.h"
  33. #include "keysym2ucs.h"
  34. #include "ucs2_to_utf8.h"
  35. #include "xcb.h"
  36. #include "cursors.h"
  37. #include "unlock_indicator.h"
  38. #include "xinerama.h"
  39. char color[7] = "ffffff";
  40. uint32_t last_resolution[2];
  41. xcb_window_t win;
  42. static xcb_cursor_t cursor;
  43. static xcb_key_symbols_t *symbols;
  44. static pam_handle_t *pam_handle;
  45. int input_position = 0;
  46. /* Holds the password you enter (in UTF-8). */
  47. static char password[512];
  48. static bool modeswitch_active = false;
  49. static bool iso_level3_shift_active = false;
  50. static bool iso_level5_shift_active = false;
  51. static int numlockmask;
  52. static int capslockmask;
  53. static bool beep = false;
  54. bool debug_mode = false;
  55. static bool dpms = false;
  56. bool unlock_indicator = true;
  57. static bool dont_fork = false;
  58. struct ev_loop *main_loop;
  59. static struct ev_timer *clear_pam_wrong_timeout;
  60. extern unlock_state_t unlock_state;
  61. extern pam_state_t pam_state;
  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(void) {
  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(void) {
  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. DEBUG("successfully authenticated\n");
  109. clear_password_memory();
  110. exit(0);
  111. }
  112. if (debug_mode)
  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. /* Whether the user currently holds down the shift key. */
  255. bool shift = (event->state & XCB_MOD_MASK_SHIFT);
  256. /* Whether Caps Lock (all lowercase alphabetic keys will be replaced by
  257. * their uppercase variant) is active at the moment. */
  258. bool capslock = (event->state & capslockmask);
  259. DEBUG("shift = %d, capslock = %d\n",
  260. shift, capslock);
  261. if ((event->state & numlockmask) && xcb_is_keypad_key(sym1)) {
  262. /* this key was a keypad key */
  263. if (shift)
  264. sym = sym0;
  265. else sym = sym1;
  266. } else {
  267. xcb_keysym_t upper, lower;
  268. XConvertCase(sym0, (KeySym*)&lower, (KeySym*)&upper);
  269. DEBUG("sym0 = %c (%d), sym1 = %c (%d), lower = %c (%d), upper = %c (%d)\n",
  270. sym0, sym0, sym1, sym1, lower, lower, upper, upper);
  271. /* If there is no difference between the uppercase and lowercase
  272. * variant of this key, we consider Caps Lock off it is only relevant
  273. * for alphabetic keys, unlike Shift Lock. */
  274. if (lower == upper) {
  275. capslock = false;
  276. DEBUG("lower == upper, now shift = %d, capslock = %d\n",
  277. shift, capslock);
  278. }
  279. /* In two different cases we need to use the uppercase keysym:
  280. * 1) The user holds shift, no lock is active.
  281. * 2) Any of the two locks is active.
  282. */
  283. if ((shift && !capslock) || (!shift && capslock))
  284. sym = sym1;
  285. else sym = sym0;
  286. }
  287. #if 0
  288. /* FIXME: handle all of these? */
  289. printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
  290. printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
  291. printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
  292. printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
  293. printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
  294. printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
  295. printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
  296. #endif
  297. if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
  298. return;
  299. DEBUG("resolved to keysym = %c (%d)\n", sym, sym);
  300. /* convert the keysym to UCS */
  301. uint16_t ucs = keysym2ucs(sym);
  302. if ((int16_t)ucs == -1) {
  303. if (debug_mode)
  304. fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
  305. return;
  306. }
  307. /* store the UCS in a string to convert it */
  308. uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
  309. DEBUG("input part = %s\n", inp);
  310. /* store it in the password array as UTF-8 */
  311. input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
  312. password[input_position] = '\0';
  313. DEBUG("current password = %s\n", password);
  314. unlock_state = STATE_KEY_ACTIVE;
  315. redraw_screen();
  316. unlock_state = STATE_KEY_PRESSED;
  317. struct ev_timer *timeout = calloc(sizeof(struct ev_timer), 1);
  318. if (timeout) {
  319. ev_timer_init(timeout, redraw_timeout, 0.25, 0.);
  320. ev_timer_start(main_loop, timeout);
  321. }
  322. stop_clear_indicator_timeout();
  323. }
  324. /*
  325. * A visibility notify event will be received when the visibility (= can the
  326. * user view the complete window) changes, so for example when a popup overlays
  327. * some area of the i3lock window.
  328. *
  329. * In this case, we raise our window on top so that the popup (or whatever is
  330. * hiding us) gets hidden.
  331. *
  332. */
  333. static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
  334. if (event->state != XCB_VISIBILITY_UNOBSCURED) {
  335. uint32_t values[] = { XCB_STACK_MODE_ABOVE };
  336. xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
  337. xcb_flush(conn);
  338. }
  339. }
  340. /*
  341. * Called when the keyboard mapping changes. We update our symbols.
  342. *
  343. */
  344. static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
  345. xcb_refresh_keyboard_mapping(symbols, event);
  346. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  347. }
  348. /*
  349. * Called when the properties on the root window change, e.g. when the screen
  350. * resolution changes. If so we update the window to cover the whole screen
  351. * and also redraw the image, if any.
  352. *
  353. */
  354. void handle_screen_resize(void) {
  355. xcb_get_geometry_cookie_t geomc;
  356. xcb_get_geometry_reply_t *geom;
  357. geomc = xcb_get_geometry(conn, screen->root);
  358. if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL)
  359. return;
  360. if (last_resolution[0] == geom->width &&
  361. last_resolution[1] == geom->height) {
  362. free(geom);
  363. return;
  364. }
  365. last_resolution[0] = geom->width;
  366. last_resolution[1] = geom->height;
  367. free(geom);
  368. #ifndef NOLIBCAIRO
  369. redraw_screen();
  370. #endif
  371. uint32_t mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
  372. xcb_configure_window(conn, win, mask, last_resolution);
  373. xcb_flush(conn);
  374. xinerama_query_screens();
  375. redraw_screen();
  376. }
  377. /*
  378. * Callback function for PAM. We only react on password request callbacks.
  379. *
  380. */
  381. static int conv_callback(int num_msg, const struct pam_message **msg,
  382. struct pam_response **resp, void *appdata_ptr)
  383. {
  384. if (num_msg == 0)
  385. return 1;
  386. /* PAM expects an array of responses, one for each message */
  387. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  388. perror("calloc");
  389. return 1;
  390. }
  391. for (int c = 0; c < num_msg; c++) {
  392. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  393. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  394. continue;
  395. /* return code is currently not used but should be set to zero */
  396. resp[c]->resp_retcode = 0;
  397. if ((resp[c]->resp = strdup(password)) == NULL) {
  398. perror("strdup");
  399. return 1;
  400. }
  401. }
  402. return 0;
  403. }
  404. /*
  405. * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
  406. * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
  407. *
  408. */
  409. static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
  410. /* empty, because xcb_prepare_cb and xcb_check_cb are used */
  411. }
  412. /*
  413. * Flush before blocking (and waiting for new events)
  414. *
  415. */
  416. static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
  417. xcb_flush(conn);
  418. }
  419. /*
  420. * Instead of polling the X connection socket we leave this to
  421. * xcb_poll_for_event() which knows better than we can ever know.
  422. *
  423. */
  424. static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
  425. xcb_generic_event_t *event;
  426. while ((event = xcb_poll_for_event(conn)) != NULL) {
  427. if (event->response_type == 0) {
  428. xcb_generic_error_t *error = (xcb_generic_error_t*)event;
  429. if (debug_mode)
  430. fprintf(stderr, "X11 Error received! sequence 0x%x, error_code = %d\n",
  431. error->sequence, error->error_code);
  432. free(event);
  433. continue;
  434. }
  435. /* Strip off the highest bit (set if the event is generated) */
  436. int type = (event->response_type & 0x7F);
  437. switch (type) {
  438. case XCB_KEY_PRESS:
  439. handle_key_press((xcb_key_press_event_t*)event);
  440. break;
  441. case XCB_KEY_RELEASE:
  442. handle_key_release((xcb_key_release_event_t*)event);
  443. /* If this was the backspace or escape key we are back at an
  444. * empty input, so turn off the screen if DPMS is enabled */
  445. if (dpms && input_position == 0)
  446. dpms_turn_off_screen(conn);
  447. break;
  448. case XCB_VISIBILITY_NOTIFY:
  449. handle_visibility_notify((xcb_visibility_notify_event_t*)event);
  450. break;
  451. case XCB_MAP_NOTIFY:
  452. if (!dont_fork) {
  453. /* After the first MapNotify, we never fork again. We don’t
  454. * expect to get another MapNotify, but better be sure */
  455. dont_fork = true;
  456. /* In the parent process, we exit */
  457. if (fork() != 0)
  458. exit(0);
  459. }
  460. break;
  461. case XCB_MAPPING_NOTIFY:
  462. handle_mapping_notify((xcb_mapping_notify_event_t*)event);
  463. break;
  464. case XCB_CONFIGURE_NOTIFY:
  465. handle_screen_resize();
  466. break;
  467. }
  468. free(event);
  469. }
  470. }
  471. int main(int argc, char *argv[]) {
  472. char *username;
  473. #ifndef NOLIBCAIRO
  474. char *image_path = NULL;
  475. #endif
  476. int ret;
  477. struct pam_conv conv = {conv_callback, NULL};
  478. int nscreen;
  479. int curs_choice = CURS_NONE;
  480. char o;
  481. int optind = 0;
  482. struct option longopts[] = {
  483. {"version", no_argument, NULL, 'v'},
  484. {"nofork", no_argument, NULL, 'n'},
  485. {"beep", no_argument, NULL, 'b'},
  486. {"dpms", no_argument, NULL, 'd'},
  487. {"color", required_argument, NULL, 'c'},
  488. {"pointer", required_argument, NULL , 'p'},
  489. {"debug", no_argument, NULL, 0},
  490. {"help", no_argument, NULL, 'h'},
  491. {"no-unlock-indicator", no_argument, NULL, 'u'},
  492. #ifndef NOLIBCAIRO
  493. {"image", required_argument, NULL, 'i'},
  494. {"tiling", no_argument, NULL, 't'},
  495. #endif
  496. {NULL, no_argument, NULL, 0}
  497. };
  498. if ((username = getenv("USER")) == NULL)
  499. errx(1, "USER environment variable not set, please set it.\n");
  500. while ((o = getopt_long(argc, argv, "hvnbdc:p:u"
  501. #ifndef NOLIBCAIRO
  502. "i:t"
  503. #endif
  504. , longopts, &optind)) != -1) {
  505. switch (o) {
  506. case 'v':
  507. errx(EXIT_SUCCESS, "version " VERSION " © 2010-2012 Michael Stapelberg");
  508. case 'n':
  509. dont_fork = true;
  510. break;
  511. case 'b':
  512. beep = true;
  513. break;
  514. case 'd':
  515. dpms = true;
  516. break;
  517. case 'c': {
  518. char *arg = optarg;
  519. /* Skip # if present */
  520. if (arg[0] == '#')
  521. arg++;
  522. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  523. errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
  524. break;
  525. }
  526. case 'u':
  527. unlock_indicator = false;
  528. break;
  529. #ifndef NOLIBCAIRO
  530. case 'i':
  531. image_path = strdup(optarg);
  532. break;
  533. case 't':
  534. tile = true;
  535. break;
  536. #endif
  537. case 'p':
  538. if (!strcmp(optarg, "win")) {
  539. curs_choice = CURS_WIN;
  540. } else if (!strcmp(optarg, "default")) {
  541. curs_choice = CURS_DEFAULT;
  542. } else {
  543. errx(1, "i3lock: Invalid pointer type given. Expected one of \"win\" or \"default\".\n");
  544. }
  545. break;
  546. case 0:
  547. if (strcmp(longopts[optind].name, "debug") == 0)
  548. debug_mode = true;
  549. break;
  550. default:
  551. errx(1, "Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-u] [-p win|default]"
  552. #ifndef NOLIBCAIRO
  553. " [-i image.png] [-t]"
  554. #else
  555. " (compiled with NOLIBCAIRO)"
  556. #endif
  557. );
  558. }
  559. }
  560. /* We need (relatively) random numbers for highlighting a random part of
  561. * the unlock indicator upon keypresses. */
  562. srand(time(NULL));
  563. /* Initialize PAM */
  564. ret = pam_start("i3lock", username, &conv, &pam_handle);
  565. if (ret != PAM_SUCCESS)
  566. errx(EXIT_FAILURE, "PAM: %s", pam_strerror(pam_handle, ret));
  567. /* Lock the area where we store the password in memory, we don’t want it to
  568. * be swapped to disk. Since Linux 2.6.9, this does not require any
  569. * privileges, just enough bytes in the RLIMIT_MEMLOCK limit. */
  570. if (mlock(password, sizeof(password)) != 0)
  571. err(EXIT_FAILURE, "Could not lock page in memory, check RLIMIT_MEMLOCK");
  572. /* Initialize connection to X11 */
  573. if ((conn = xcb_connect(NULL, &nscreen)) == NULL ||
  574. xcb_connection_has_error(conn))
  575. errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
  576. xinerama_init();
  577. xinerama_query_screens();
  578. /* if DPMS is enabled, check if the X server really supports it */
  579. if (dpms) {
  580. xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
  581. xcb_dpms_capable_reply_t *dpmsr;
  582. if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL))) {
  583. if (!dpmsr->capable) {
  584. if (debug_mode)
  585. fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
  586. dpms = false;
  587. }
  588. free(dpmsr);
  589. }
  590. }
  591. screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  592. last_resolution[0] = screen->width_in_pixels;
  593. last_resolution[1] = screen->height_in_pixels;
  594. xcb_change_window_attributes(conn, screen->root, XCB_CW_EVENT_MASK,
  595. (uint32_t[]){ XCB_EVENT_MASK_STRUCTURE_NOTIFY });
  596. #ifndef NOLIBCAIRO
  597. if (image_path) {
  598. /* Create a pixmap to render on, fill it with the background color */
  599. img = cairo_image_surface_create_from_png(image_path);
  600. /* In case loading failed, we just pretend no -i was specified. */
  601. if (cairo_surface_status(img) != CAIRO_STATUS_SUCCESS) {
  602. if (debug_mode)
  603. fprintf(stderr, "Could not load image \"%s\": cairo surface status %d\n",
  604. image_path, cairo_surface_status(img));
  605. img = NULL;
  606. }
  607. }
  608. #endif
  609. /* Pixmap on which the image is rendered to (if any) */
  610. xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
  611. /* open the fullscreen window, already with the correct pixmap in place */
  612. win = open_fullscreen_window(conn, screen, color, bg_pixmap);
  613. xcb_free_pixmap(conn, bg_pixmap);
  614. cursor = create_cursor(conn, screen, win, curs_choice);
  615. grab_pointer_and_keyboard(conn, screen, cursor);
  616. symbols = xcb_key_symbols_alloc(conn);
  617. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  618. capslockmask = get_mod_mask(conn, symbols, XK_Caps_Lock);
  619. DEBUG("numlock mask = %d\n", numlockmask);
  620. DEBUG("caps lock mask = %d\n", capslockmask);
  621. if (dpms)
  622. dpms_turn_off_screen(conn);
  623. /* Initialize the libev event loop. */
  624. main_loop = EV_DEFAULT;
  625. if (main_loop == NULL)
  626. errx(EXIT_FAILURE, "Could not initialize libev. Bad LIBEV_FLAGS?\n");
  627. struct ev_io *xcb_watcher = calloc(sizeof(struct ev_io), 1);
  628. struct ev_check *xcb_check = calloc(sizeof(struct ev_check), 1);
  629. struct ev_prepare *xcb_prepare = calloc(sizeof(struct ev_prepare), 1);
  630. ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
  631. ev_io_start(main_loop, xcb_watcher);
  632. ev_check_init(xcb_check, xcb_check_cb);
  633. ev_check_start(main_loop, xcb_check);
  634. ev_prepare_init(xcb_prepare, xcb_prepare_cb);
  635. ev_prepare_start(main_loop, xcb_prepare);
  636. /* Invoke the event callback once to catch all the events which were
  637. * received up until now. ev will only pick up new events (when the X11
  638. * file descriptor becomes readable). */
  639. ev_invoke(main_loop, xcb_check, 0);
  640. ev_loop(main_loop, 0);
  641. }