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