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.

443 lines
13 KiB

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