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.

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