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.

354 lines
10 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. * Callback function for PAM. We only react on password request callbacks.
  143. *
  144. */
  145. static int conv_callback(int num_msg, const struct pam_message **msg,
  146. struct pam_response **resp, void *appdata_ptr)
  147. {
  148. if (num_msg == 0)
  149. return 1;
  150. /* PAM expects an array of responses, one for each message */
  151. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  152. perror("calloc");
  153. return 1;
  154. }
  155. for (int c = 0; c < num_msg; c++) {
  156. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  157. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  158. continue;
  159. /* return code is currently not used but should be set to zero */
  160. resp[c]->resp_retcode = 0;
  161. if ((resp[c]->resp = strdup(password)) == NULL) {
  162. perror("strdup");
  163. return 1;
  164. }
  165. }
  166. return 0;
  167. }
  168. int main(int argc, char *argv[]) {
  169. bool dont_fork = false;
  170. bool dpms = false;
  171. char color[7] = "ffffff";
  172. char *username;
  173. char *image_path = NULL;
  174. int ret;
  175. struct pam_conv conv = {conv_callback, NULL};
  176. int screen;
  177. cairo_surface_t *img = NULL;
  178. xcb_visualtype_t *vistype;
  179. xcb_generic_event_t *event;
  180. xcb_screen_t *scr;
  181. xcb_window_t win;
  182. char o;
  183. int optind = 0;
  184. struct option longopts[] = {
  185. {"version", no_argument, NULL, 'v'},
  186. {"nofork", no_argument, NULL, 'n'},
  187. {"beep", no_argument, NULL, 'b'},
  188. {"dpms", no_argument, NULL, 'd'},
  189. {"image", required_argument, NULL, 'i'},
  190. {"color", required_argument, NULL, 'c'},
  191. {"tiling", no_argument, NULL, 't'},
  192. {"pointer", required_argument, NULL , 'p'},
  193. {NULL, no_argument, NULL, 0}
  194. };
  195. if ((username = getenv("USER")) == NULL)
  196. errx(1, "USER environment variable not set, please set it.\n");
  197. while ((o = getopt_long(argc, argv, "vnbdi:c:tp:", longopts, &optind)) != -1) {
  198. switch (o) {
  199. case 'v':
  200. errx(EXIT_SUCCESS, "i3lock © 2010 Michael Stapelberg\n");
  201. case 'n':
  202. dont_fork = true;
  203. break;
  204. case 'b':
  205. beep = true;
  206. break;
  207. case 'd':
  208. dpms = true;
  209. break;
  210. case 'i':
  211. image_path = strdup(optarg);
  212. break;
  213. case 'c': {
  214. char *arg = optarg;
  215. /* Skip # if present */
  216. if (arg[0] == '#')
  217. arg++;
  218. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  219. errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
  220. break;
  221. }
  222. case 't':
  223. /* TODO: tile image */
  224. break;
  225. case 'p':
  226. /* TODO: cursor */
  227. break;
  228. default:
  229. errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.png] [-c color] [-t] [-p win|default]\n");
  230. }
  231. }
  232. /* Initialize PAM */
  233. ret = pam_start("i3lock", username, &conv, &pam_handle);
  234. if (ret != PAM_SUCCESS)
  235. errx(EXIT_FAILURE, "PAM: %s\n", pam_strerror(pam_handle, ret));
  236. /* Initialize connection to X11 */
  237. if ((conn = xcb_connect(NULL, &screen)) == NULL)
  238. err(EXIT_FAILURE, "xcb_connect()");
  239. if (!dont_fork) {
  240. /* In the parent process, we exit */
  241. if (fork() != 0)
  242. return 0;
  243. }
  244. /* if DPMS is enabled, check if the X server really supports it */
  245. if (dpms) {
  246. xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
  247. xcb_dpms_capable_reply_t *dpmsr;
  248. if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
  249. fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
  250. dpms = false;
  251. }
  252. }
  253. scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  254. vistype = get_root_visual_type(scr);
  255. /* open the fullscreen window and flush immediately afterwards so that X11
  256. * can generate an expose event while we load the PNG file (so that we are
  257. * ready to handle the expose event immediately afterwards) */
  258. win = open_fullscreen_window(conn, scr, color);
  259. grab_pointer_and_keyboard(conn, scr);
  260. if (image_path)
  261. img = cairo_image_surface_create_from_png(image_path);
  262. symbols = xcb_key_symbols_alloc(conn);
  263. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  264. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  265. if (img) {
  266. /* Initialize cairo */
  267. cairo_surface_t *output;
  268. output = cairo_xcb_surface_create(conn, win, vistype,
  269. scr->width_in_pixels, scr->height_in_pixels);
  270. /* TODO: tiling of the image */
  271. ctx = cairo_create(output);
  272. cairo_set_source_surface(ctx, img, 0, 0);
  273. handle_expose_event();
  274. }
  275. if (dpms)
  276. dpms_turn_off_screen(conn);
  277. while ((event = xcb_wait_for_event(conn))) {
  278. int type = x_event_type(event);
  279. if (type == XCB_EXPOSE) {
  280. handle_expose_event();
  281. continue;
  282. }
  283. if (type == XCB_KEY_PRESS) {
  284. handle_key_press((xcb_key_press_event_t*)event);
  285. continue;
  286. }
  287. if (type == XCB_KEY_RELEASE) {
  288. handle_key_release((xcb_key_release_event_t*)event);
  289. /* If this was the backspace or escape key we are back at an
  290. * empty input, so turn off the screen if DPMS is enabled */
  291. if (dpms && input_position == 0)
  292. dpms_turn_off_screen(conn);
  293. continue;
  294. }
  295. printf("WARNING: unhandled event of type %d\n", type);
  296. }
  297. return 0;
  298. }