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.

466 lines
13 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 int modeswitchmask;
  43. static int numlockmask;
  44. static bool beep = false;
  45. #ifndef NOLIBCAIRO
  46. static cairo_surface_t *img = NULL;
  47. static cairo_t *ctx = NULL;
  48. static bool tile = false;
  49. #endif
  50. static void input_done() {
  51. if (input_position == 0)
  52. return;
  53. /* TODO: change cursor during authentication? */
  54. if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
  55. printf("successfully authenticated\n");
  56. exit(0);
  57. }
  58. fprintf(stderr, "Authentication failure\n");
  59. /* beep on authentication failure, if enabled */
  60. if (beep) {
  61. xcb_bell(conn, 100);
  62. xcb_flush(conn);
  63. }
  64. }
  65. /*
  66. * Called when we should draw the image (if any).
  67. *
  68. */
  69. static void handle_expose_event() {
  70. #ifndef NOLIBCAIRO
  71. if (!ctx)
  72. return;
  73. if (tile) {
  74. /* create a pattern and fill a rectangle as big as the screen */
  75. cairo_pattern_t *pattern;
  76. pattern = cairo_pattern_create_for_surface(img);
  77. cairo_set_source(ctx, pattern);
  78. cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
  79. cairo_rectangle(ctx, 0, 0, scr->width_in_pixels, scr->height_in_pixels);
  80. cairo_fill(ctx);
  81. } else {
  82. /* otherwise, just paint the image */
  83. cairo_paint(ctx);
  84. }
  85. #endif
  86. xcb_flush(conn);
  87. }
  88. /*
  89. * Called when the user releases a key. We need to leave the Mode_switch
  90. * state when the user releases the Mode_switch key.
  91. *
  92. */
  93. static void handle_key_release(xcb_key_release_event_t *event) {
  94. //printf("releasing %d, state raw = %d\n", event->detail, event->state);
  95. /* fix state */
  96. event->state &= ~numlockmask;
  97. xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
  98. if (sym == XK_Mode_switch) {
  99. //printf("Mode switch disabled\n");
  100. modeswitch_active = false;
  101. }
  102. }
  103. /*
  104. * Handle key presses. Fixes state, then looks up the key symbol for the
  105. * given keycode, then looks up the key symbol (as UCS-2), converts it to
  106. * UTF-8 and stores it in the password array.
  107. *
  108. */
  109. static void handle_key_press(xcb_key_press_event_t *event) {
  110. //printf("keypress %d, state raw = %d\n", event->detail, event->state);
  111. xcb_keysym_t sym0, sym1, sym;
  112. if (modeswitch_active) {
  113. sym0 = xcb_key_press_lookup_keysym(symbols, event, 4);
  114. sym1 = xcb_key_press_lookup_keysym(symbols, event, 5);
  115. } else {
  116. sym0 = xcb_key_press_lookup_keysym(symbols, event, 0);
  117. sym1 = xcb_key_press_lookup_keysym(symbols, event, 1);
  118. }
  119. switch (sym0) {
  120. case XK_Mode_switch:
  121. //printf("Mode switch enabled\n");
  122. modeswitch_active = true;
  123. return;
  124. case XK_Return:
  125. case XK_KP_Enter:
  126. input_done();
  127. case XK_Escape:
  128. input_position = 0;
  129. password[input_position] = '\0';
  130. return;
  131. case XK_BackSpace:
  132. if (input_position == 0)
  133. return;
  134. /* decrement input_position to point to the previous glyph */
  135. u8_dec(password, &input_position);
  136. password[input_position] = '\0';
  137. //printf("new input position = %d, new password = %s\n", input_position, password);
  138. return;
  139. }
  140. if ((input_position + 8) >= sizeof(password))
  141. return;
  142. if ((event->state & numlockmask) && xcb_is_keypad_key(sym1)) {
  143. /* this key was a keypad key */
  144. if ((event->state & XCB_MOD_MASK_SHIFT))
  145. sym = sym0;
  146. else sym = sym1;
  147. } else {
  148. if ((event->state & XCB_MOD_MASK_SHIFT))
  149. sym = sym1;
  150. else sym = sym0;
  151. }
  152. #if 0
  153. /* FIXME: handle all of these? */
  154. printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
  155. printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
  156. printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
  157. printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
  158. printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
  159. printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
  160. printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
  161. #endif
  162. if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
  163. return;
  164. //printf("sym = %c (%d)\n", sym, sym);
  165. /* convert the keysym to UCS */
  166. uint16_t ucs = keysym2ucs(sym);
  167. if ((int16_t)ucs == -1) {
  168. fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
  169. return;
  170. }
  171. /* store the UCS in a string to convert it */
  172. uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
  173. /* store it in the password array as UTF-8 */
  174. input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
  175. password[input_position] = '\0';
  176. //printf("current password = %s\n", password);
  177. }
  178. /*
  179. * A visibility notify event will be received when the visibility (= can the
  180. * user view the complete window) changes, so for example when a popup overlays
  181. * some area of the i3lock window.
  182. *
  183. * In this case, we raise our window on top so that the popup (or whatever is
  184. * hiding us) gets hidden.
  185. *
  186. */
  187. static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
  188. if (event->state != XCB_VISIBILITY_UNOBSCURED) {
  189. uint32_t values[] = { XCB_STACK_MODE_ABOVE };
  190. xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
  191. xcb_flush(conn);
  192. }
  193. }
  194. /*
  195. * Called when the keyboard mapping changes. We update our symbols and re-grab
  196. * pointer/keyboard.
  197. *
  198. */
  199. static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
  200. xcb_refresh_keyboard_mapping(symbols, event);
  201. xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
  202. xcb_ungrab_keyboard(conn, XCB_CURRENT_TIME);
  203. grab_pointer_and_keyboard(conn, scr, cursor);
  204. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  205. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  206. xcb_flush(conn);
  207. }
  208. /*
  209. * Callback function for PAM. We only react on password request callbacks.
  210. *
  211. */
  212. static int conv_callback(int num_msg, const struct pam_message **msg,
  213. struct pam_response **resp, void *appdata_ptr)
  214. {
  215. if (num_msg == 0)
  216. return 1;
  217. /* PAM expects an array of responses, one for each message */
  218. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  219. perror("calloc");
  220. return 1;
  221. }
  222. for (int c = 0; c < num_msg; c++) {
  223. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  224. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  225. continue;
  226. /* return code is currently not used but should be set to zero */
  227. resp[c]->resp_retcode = 0;
  228. if ((resp[c]->resp = strdup(password)) == NULL) {
  229. perror("strdup");
  230. return 1;
  231. }
  232. }
  233. return 0;
  234. }
  235. int main(int argc, char *argv[]) {
  236. bool dont_fork = false;
  237. bool dpms = false;
  238. char color[7] = "ffffff";
  239. char *username;
  240. #ifndef NOLIBCAIRO
  241. char *image_path = NULL;
  242. #endif
  243. int ret;
  244. struct pam_conv conv = {conv_callback, NULL};
  245. int screen;
  246. xcb_visualtype_t *vistype;
  247. xcb_generic_event_t *event;
  248. xcb_window_t win;
  249. int curs_choice = CURS_NONE;
  250. char o;
  251. int optind = 0;
  252. struct option longopts[] = {
  253. {"version", no_argument, NULL, 'v'},
  254. {"nofork", no_argument, NULL, 'n'},
  255. {"beep", no_argument, NULL, 'b'},
  256. {"dpms", no_argument, NULL, 'd'},
  257. {"color", required_argument, NULL, 'c'},
  258. {"pointer", required_argument, NULL , 'p'},
  259. #ifndef NOLIBCAIRO
  260. {"image", required_argument, NULL, 'i'},
  261. {"tiling", no_argument, NULL, 't'},
  262. #endif
  263. {NULL, no_argument, NULL, 0}
  264. };
  265. if ((username = getenv("USER")) == NULL)
  266. errx(1, "USER environment variable not set, please set it.\n");
  267. while ((o = getopt_long(argc, argv, "vnbdc:p:"
  268. #ifndef NOLIBCAIRO
  269. "i:t"
  270. #endif
  271. , longopts, &optind)) != -1) {
  272. switch (o) {
  273. case 'v':
  274. errx(EXIT_SUCCESS, "version " VERSION " © 2010-2011 Michael Stapelberg\n");
  275. case 'n':
  276. dont_fork = true;
  277. break;
  278. case 'b':
  279. beep = true;
  280. break;
  281. case 'd':
  282. dpms = true;
  283. break;
  284. case 'c': {
  285. char *arg = optarg;
  286. /* Skip # if present */
  287. if (arg[0] == '#')
  288. arg++;
  289. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  290. errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
  291. break;
  292. }
  293. #ifndef NOLIBCAIRO
  294. case 'i':
  295. image_path = strdup(optarg);
  296. break;
  297. case 't':
  298. tile = true;
  299. break;
  300. #endif
  301. case 'p':
  302. if (!strcmp(optarg, "win")) {
  303. curs_choice = CURS_WIN;
  304. }
  305. if (!strcmp(optarg, "default")) {
  306. curs_choice = CURS_DEFAULT;
  307. }
  308. break;
  309. default:
  310. errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-p win|default]"
  311. #ifndef NOLIBCAIRO
  312. " [-i image.png] [-t]"
  313. #else
  314. " (compiled with NOLIBCAIRO)"
  315. #endif
  316. "\n");
  317. }
  318. }
  319. /* Initialize PAM */
  320. ret = pam_start("i3lock", username, &conv, &pam_handle);
  321. if (ret != PAM_SUCCESS)
  322. errx(EXIT_FAILURE, "PAM: %s\n", pam_strerror(pam_handle, ret));
  323. /* Initialize connection to X11 */
  324. if ((conn = xcb_connect(NULL, &screen)) == NULL ||
  325. xcb_connection_has_error(conn))
  326. errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
  327. if (!dont_fork) {
  328. /* In the parent process, we exit */
  329. if (fork() != 0)
  330. return 0;
  331. }
  332. /* if DPMS is enabled, check if the X server really supports it */
  333. if (dpms) {
  334. xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
  335. xcb_dpms_capable_reply_t *dpmsr;
  336. if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
  337. fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
  338. dpms = false;
  339. }
  340. }
  341. scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  342. vistype = get_root_visual_type(scr);
  343. /* open the fullscreen window and flush immediately afterwards so that X11
  344. * can generate an expose event while we load the PNG file (so that we are
  345. * ready to handle the expose event immediately afterwards) */
  346. win = open_fullscreen_window(conn, scr, color);
  347. cursor = create_cursor(conn, scr, win, curs_choice);
  348. grab_pointer_and_keyboard(conn, scr, cursor);
  349. symbols = xcb_key_symbols_alloc(conn);
  350. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  351. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  352. #ifndef NOLIBCAIRO
  353. if (image_path)
  354. img = cairo_image_surface_create_from_png(image_path);
  355. if (img) {
  356. /* Initialize cairo */
  357. cairo_surface_t *output;
  358. output = cairo_xcb_surface_create(conn, win, vistype,
  359. scr->width_in_pixels, scr->height_in_pixels);
  360. ctx = cairo_create(output);
  361. if (!tile)
  362. cairo_set_source_surface(ctx, img, 0, 0);
  363. handle_expose_event();
  364. }
  365. #endif
  366. if (dpms)
  367. dpms_turn_off_screen(conn);
  368. while ((event = xcb_wait_for_event(conn))) {
  369. if (event->response_type == 0)
  370. errx(1, "XCB: Invalid event received");
  371. /* Strip off the highest bit (set if the event is generated) */
  372. int type = (event->response_type & 0x7F);
  373. if (type == XCB_EXPOSE) {
  374. handle_expose_event();
  375. continue;
  376. }
  377. if (type == XCB_KEY_PRESS) {
  378. handle_key_press((xcb_key_press_event_t*)event);
  379. continue;
  380. }
  381. if (type == XCB_KEY_RELEASE) {
  382. handle_key_release((xcb_key_release_event_t*)event);
  383. /* If this was the backspace or escape key we are back at an
  384. * empty input, so turn off the screen if DPMS is enabled */
  385. if (dpms && input_position == 0)
  386. dpms_turn_off_screen(conn);
  387. continue;
  388. }
  389. if (type == XCB_VISIBILITY_NOTIFY) {
  390. handle_visibility_notify((xcb_visibility_notify_event_t*)event);
  391. continue;
  392. }
  393. if (type == XCB_MAPPING_NOTIFY) {
  394. handle_mapping_notify((xcb_mapping_notify_event_t*)event);
  395. continue;
  396. }
  397. printf("WARNING: unhandled event of type %d\n", type);
  398. }
  399. return 0;
  400. }