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.

535 lines
16 KiB

14 years ago
15 years ago
14 years ago
  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010-2011 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. #ifndef NOLIBCAIRO
  26. #include <cairo.h>
  27. #include <cairo/cairo-xcb.h>
  28. #endif
  29. #include "keysym2ucs.h"
  30. #include "ucs2_to_utf8.h"
  31. #include "xcb.h"
  32. #include "cursors.h"
  33. static xcb_connection_t *conn;
  34. static xcb_cursor_t cursor;
  35. static xcb_key_symbols_t *symbols;
  36. static xcb_screen_t *scr;
  37. static pam_handle_t *pam_handle;
  38. static int input_position = 0;
  39. /* holds the password you enter (in UTF-8) */
  40. static char password[512];
  41. static bool modeswitch_active = false;
  42. static bool iso_level3_shift_active = false;
  43. static int modeswitchmask;
  44. static int numlockmask;
  45. static bool beep = false;
  46. #ifndef NOLIBCAIRO
  47. static cairo_surface_t *img = NULL;
  48. static bool tile = false;
  49. #endif
  50. /*
  51. * Draws global image with fill color onto a pixmap with the given
  52. * resolution and returns it.
  53. *
  54. */
  55. xcb_pixmap_t draw_image(xcb_visualtype_t *vistype, u_int32_t* resolution, char* color) {
  56. xcb_pixmap_t bg_pixmap = XCB_NONE;
  57. #ifndef NOLIBCAIRO
  58. if (!img)
  59. return bg_pixmap;
  60. bg_pixmap = create_bg_pixmap(conn, scr, resolution, color);
  61. /* Initialize cairo */
  62. cairo_surface_t *output;
  63. output = cairo_xcb_surface_create(conn, bg_pixmap, vistype,
  64. resolution[0], resolution[1]);
  65. cairo_t *ctx = cairo_create(output);
  66. if (!tile) {
  67. cairo_set_source_surface(ctx, img, 0, 0);
  68. cairo_paint(ctx);
  69. } else {
  70. /* create a pattern and fill a rectangle as big as the screen */
  71. cairo_pattern_t *pattern;
  72. pattern = cairo_pattern_create_for_surface(img);
  73. cairo_set_source(ctx, pattern);
  74. cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
  75. cairo_rectangle(ctx, 0, 0, resolution[0], resolution[1]);
  76. cairo_fill(ctx);
  77. cairo_pattern_destroy(pattern);
  78. }
  79. cairo_surface_destroy(output);
  80. cairo_destroy(ctx);
  81. #endif
  82. return bg_pixmap;
  83. }
  84. static void input_done() {
  85. if (input_position == 0)
  86. return;
  87. /* TODO: change cursor during authentication? */
  88. if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
  89. printf("successfully authenticated\n");
  90. exit(0);
  91. }
  92. fprintf(stderr, "Authentication failure\n");
  93. /* beep on authentication failure, if enabled */
  94. if (beep) {
  95. xcb_bell(conn, 100);
  96. xcb_flush(conn);
  97. }
  98. }
  99. /*
  100. * Called when the user releases a key. We need to leave the Mode_switch
  101. * state when the user releases the Mode_switch key.
  102. *
  103. */
  104. static void handle_key_release(xcb_key_release_event_t *event) {
  105. //printf("releasing %d, state raw = %d\n", event->detail, event->state);
  106. /* fix state */
  107. event->state &= ~numlockmask;
  108. xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
  109. if (sym == XK_Mode_switch) {
  110. //printf("Mode switch disabled\n");
  111. modeswitch_active = false;
  112. } else if (sym == XK_ISO_Level3_Shift) {
  113. iso_level3_shift_active = false;
  114. }
  115. }
  116. /*
  117. * Handle key presses. Fixes state, then looks up the key symbol for the
  118. * given keycode, then looks up the key symbol (as UCS-2), converts it to
  119. * UTF-8 and stores it in the password array.
  120. *
  121. */
  122. static void handle_key_press(xcb_key_press_event_t *event) {
  123. //printf("keypress %d, state raw = %d\n", event->detail, event->state);
  124. xcb_keysym_t sym0, sym1, sym;
  125. /* For each keycode, there is a list of symbols. The list could look like this:
  126. * $ xmodmap -pke | grep 'keycode 38'
  127. * keycode 38 = a A adiaeresis Adiaeresis o O
  128. * In non-X11 terminology, the symbols for the keycode 38 (the key labeled
  129. * with "a" on my keyboard) are "a A ä Ä o O".
  130. * Another form to display the same information is using xkbcomp:
  131. * $ xkbcomp $DISPLAY /tmp/xkb.dump
  132. * Then open /tmp/xkb.dump and search for '\<a\>' (in VIM regexp-language):
  133. *
  134. * symbols[Group1]= [ a, A, o, O ],
  135. * symbols[Group2]= [ adiaeresis, Adiaeresis ]
  136. *
  137. * So there are two *groups*, one containing 'a A' and one containing 'ä
  138. * Ä'. You can use Mode_switch to switch between these groups. You can use
  139. * ISO_Level3_Shift to reach the 'o O' part of the first group (its the
  140. * same group, just an even higher shift level).
  141. *
  142. * So, using the "logical" XKB information, the following lookup will be
  143. * performed:
  144. *
  145. * Neither Mode_switch nor ISO_Level3_Shift active: group 1, column 0 and 1
  146. * Mode_switch active: group 2, column 0 and 1
  147. * ISO_Level3_Shift active: group 1, column 2 and 3
  148. *
  149. * Using the column index which xcb_key_press_lookup_keysym uses (and
  150. * xmodmap prints out), the following lookup will be performed:
  151. *
  152. * Neither Mode_switch nor ISO_Level3_Shift active: column 0 and 1
  153. * Mode_switch active: column 2 and 3
  154. * ISO_Level3_Shift active: column 4 and 5
  155. */
  156. int base_column = 0;
  157. if (modeswitch_active)
  158. base_column = 2;
  159. if (iso_level3_shift_active)
  160. base_column = 4;
  161. sym0 = xcb_key_press_lookup_keysym(symbols, event, base_column);
  162. sym1 = xcb_key_press_lookup_keysym(symbols, event, base_column + 1);
  163. switch (sym0) {
  164. case XK_Mode_switch:
  165. //printf("Mode switch enabled\n");
  166. modeswitch_active = true;
  167. return;
  168. case XK_ISO_Level3_Shift:
  169. DEBUG("ISO_Level3_Shift enabled\n");
  170. iso_level3_shift_active = true;
  171. return;
  172. case XK_Return:
  173. case XK_KP_Enter:
  174. input_done();
  175. case XK_Escape:
  176. input_position = 0;
  177. password[input_position] = '\0';
  178. return;
  179. case XK_BackSpace:
  180. if (input_position == 0)
  181. return;
  182. /* decrement input_position to point to the previous glyph */
  183. u8_dec(password, &input_position);
  184. password[input_position] = '\0';
  185. //printf("new input position = %d, new password = %s\n", input_position, password);
  186. return;
  187. }
  188. if ((input_position + 8) >= sizeof(password))
  189. return;
  190. if ((event->state & numlockmask) && xcb_is_keypad_key(sym1)) {
  191. /* this key was a keypad key */
  192. if ((event->state & XCB_MOD_MASK_SHIFT))
  193. sym = sym0;
  194. else sym = sym1;
  195. } else {
  196. if ((event->state & XCB_MOD_MASK_SHIFT))
  197. sym = sym1;
  198. else sym = sym0;
  199. }
  200. #if 0
  201. /* FIXME: handle all of these? */
  202. printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
  203. printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
  204. printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
  205. printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
  206. printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
  207. printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
  208. printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
  209. #endif
  210. if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
  211. return;
  212. //printf("sym = %c (%d)\n", sym, sym);
  213. /* convert the keysym to UCS */
  214. uint16_t ucs = keysym2ucs(sym);
  215. if ((int16_t)ucs == -1) {
  216. fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
  217. return;
  218. }
  219. /* store the UCS in a string to convert it */
  220. uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
  221. /* store it in the password array as UTF-8 */
  222. input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
  223. password[input_position] = '\0';
  224. //printf("current password = %s\n", password);
  225. }
  226. /*
  227. * A visibility notify event will be received when the visibility (= can the
  228. * user view the complete window) changes, so for example when a popup overlays
  229. * some area of the i3lock window.
  230. *
  231. * In this case, we raise our window on top so that the popup (or whatever is
  232. * hiding us) gets hidden.
  233. *
  234. */
  235. static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
  236. if (event->state != XCB_VISIBILITY_UNOBSCURED) {
  237. uint32_t values[] = { XCB_STACK_MODE_ABOVE };
  238. xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
  239. xcb_flush(conn);
  240. }
  241. }
  242. /*
  243. * Called when the keyboard mapping changes. We update our symbols.
  244. *
  245. */
  246. static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
  247. xcb_refresh_keyboard_mapping(symbols, event);
  248. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  249. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  250. }
  251. /*
  252. * Called when the properties on the root window change, e.g. when the screen
  253. * resolution changes. If so we update the window to cover the whole screen
  254. * and also redraw the image, if any.
  255. *
  256. */
  257. void handle_screen_resize(xcb_visualtype_t *vistype, xcb_window_t win, uint32_t* last_resolution, char* color) {
  258. xcb_get_geometry_cookie_t geomc;
  259. xcb_get_geometry_reply_t *geom;
  260. geomc = xcb_get_geometry(conn, scr->root);
  261. if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL) {
  262. return;
  263. }
  264. if (last_resolution[0] == geom->width && last_resolution[1] == geom->height)
  265. return;
  266. last_resolution[0] = geom->width;
  267. last_resolution[1] = geom->height;
  268. #ifndef NOLIBCAIRO
  269. if (img) {
  270. xcb_pixmap_t bg_pixmap = draw_image(vistype, last_resolution, color);
  271. xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
  272. }
  273. #endif
  274. uint32_t mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
  275. xcb_configure_window(conn, win, mask, last_resolution);
  276. xcb_flush(conn);
  277. }
  278. /*
  279. * Callback function for PAM. We only react on password request callbacks.
  280. *
  281. */
  282. static int conv_callback(int num_msg, const struct pam_message **msg,
  283. struct pam_response **resp, void *appdata_ptr)
  284. {
  285. if (num_msg == 0)
  286. return 1;
  287. /* PAM expects an array of responses, one for each message */
  288. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  289. perror("calloc");
  290. return 1;
  291. }
  292. for (int c = 0; c < num_msg; c++) {
  293. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  294. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  295. continue;
  296. /* return code is currently not used but should be set to zero */
  297. resp[c]->resp_retcode = 0;
  298. if ((resp[c]->resp = strdup(password)) == NULL) {
  299. perror("strdup");
  300. return 1;
  301. }
  302. }
  303. return 0;
  304. }
  305. int main(int argc, char *argv[]) {
  306. bool dont_fork = false;
  307. bool dpms = false;
  308. char color[7] = "ffffff";
  309. char *username;
  310. #ifndef NOLIBCAIRO
  311. char *image_path = NULL;
  312. #endif
  313. int ret;
  314. struct pam_conv conv = {conv_callback, NULL};
  315. int screen;
  316. xcb_visualtype_t *vistype;
  317. xcb_generic_event_t *event;
  318. xcb_window_t win;
  319. int curs_choice = CURS_NONE;
  320. char o;
  321. int optind = 0;
  322. struct option longopts[] = {
  323. {"version", no_argument, NULL, 'v'},
  324. {"nofork", no_argument, NULL, 'n'},
  325. {"beep", no_argument, NULL, 'b'},
  326. {"dpms", no_argument, NULL, 'd'},
  327. {"color", required_argument, NULL, 'c'},
  328. {"pointer", required_argument, NULL , 'p'},
  329. #ifndef NOLIBCAIRO
  330. {"image", required_argument, NULL, 'i'},
  331. {"tiling", no_argument, NULL, 't'},
  332. #endif
  333. {NULL, no_argument, NULL, 0}
  334. };
  335. if ((username = getenv("USER")) == NULL)
  336. errx(1, "USER environment variable not set, please set it.\n");
  337. while ((o = getopt_long(argc, argv, "vnbdc:p:"
  338. #ifndef NOLIBCAIRO
  339. "i:t"
  340. #endif
  341. , longopts, &optind)) != -1) {
  342. switch (o) {
  343. case 'v':
  344. errx(EXIT_SUCCESS, "version " VERSION " © 2010-2011 Michael Stapelberg\n");
  345. case 'n':
  346. dont_fork = true;
  347. break;
  348. case 'b':
  349. beep = true;
  350. break;
  351. case 'd':
  352. dpms = true;
  353. break;
  354. case 'c': {
  355. char *arg = optarg;
  356. /* Skip # if present */
  357. if (arg[0] == '#')
  358. arg++;
  359. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  360. errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
  361. break;
  362. }
  363. #ifndef NOLIBCAIRO
  364. case 'i':
  365. image_path = strdup(optarg);
  366. break;
  367. case 't':
  368. tile = true;
  369. break;
  370. #endif
  371. case 'p':
  372. if (!strcmp(optarg, "win")) {
  373. curs_choice = CURS_WIN;
  374. } else if (!strcmp(optarg, "default")) {
  375. curs_choice = CURS_DEFAULT;
  376. } else {
  377. errx(1, "i3lock: Invalid pointer type given. Expected one of \"win\" or \"default\".\n");
  378. }
  379. break;
  380. default:
  381. errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-p win|default]"
  382. #ifndef NOLIBCAIRO
  383. " [-i image.png] [-t]"
  384. #else
  385. " (compiled with NOLIBCAIRO)"
  386. #endif
  387. "\n");
  388. }
  389. }
  390. /* Initialize PAM */
  391. ret = pam_start("i3lock", username, &conv, &pam_handle);
  392. if (ret != PAM_SUCCESS)
  393. errx(EXIT_FAILURE, "PAM: %s\n", pam_strerror(pam_handle, ret));
  394. /* Initialize connection to X11 */
  395. if ((conn = xcb_connect(NULL, &screen)) == NULL ||
  396. xcb_connection_has_error(conn))
  397. errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
  398. if (!dont_fork) {
  399. /* In the parent process, we exit */
  400. if (fork() != 0)
  401. return 0;
  402. }
  403. /* if DPMS is enabled, check if the X server really supports it */
  404. if (dpms) {
  405. xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
  406. xcb_dpms_capable_reply_t *dpmsr;
  407. if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
  408. fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
  409. dpms = false;
  410. }
  411. }
  412. scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  413. vistype = get_root_visual_type(scr);
  414. uint32_t last_resolution[2] = {scr->width_in_pixels, scr->height_in_pixels};
  415. #ifndef NOLIBCAIRO
  416. if (image_path) {
  417. /* Create a pixmap to render on, fill it with the background color */
  418. img = cairo_image_surface_create_from_png(image_path);
  419. }
  420. #endif
  421. /* Pixmap on which the image is rendered to (if any) */
  422. xcb_pixmap_t bg_pixmap = draw_image(vistype, last_resolution, color);
  423. /* open the fullscreen window, already with the correct pixmap in place */
  424. win = open_fullscreen_window(conn, scr, color, bg_pixmap);
  425. cursor = create_cursor(conn, scr, win, curs_choice);
  426. grab_pointer_and_keyboard(conn, scr, cursor);
  427. symbols = xcb_key_symbols_alloc(conn);
  428. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  429. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  430. if (dpms)
  431. dpms_turn_off_screen(conn);
  432. while ((event = xcb_wait_for_event(conn))) {
  433. if (event->response_type == 0)
  434. errx(1, "XCB: Invalid event received");
  435. /* Strip off the highest bit (set if the event is generated) */
  436. int type = (event->response_type & 0x7F);
  437. if (type == XCB_KEY_PRESS) {
  438. handle_key_press((xcb_key_press_event_t*)event);
  439. continue;
  440. }
  441. if (type == 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. continue;
  448. }
  449. if (type == XCB_VISIBILITY_NOTIFY) {
  450. handle_visibility_notify((xcb_visibility_notify_event_t*)event);
  451. continue;
  452. }
  453. if (type == XCB_MAPPING_NOTIFY) {
  454. handle_mapping_notify((xcb_mapping_notify_event_t*)event);
  455. continue;
  456. }
  457. if (type == XCB_CONFIGURE_NOTIFY) {
  458. handle_screen_resize(vistype, win, last_resolution, color);
  459. continue;
  460. }
  461. printf("WARNING: unhandled event of type %d\n", type);
  462. }
  463. return 0;
  464. }