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.

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