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.

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