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.

355 lines
10 KiB

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