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.

384 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. #if 0
  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. #endif
  127. if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
  128. return;
  129. //printf("sym = %c (%d)\n", sym, sym);
  130. /* convert the keysym to UCS */
  131. uint16_t ucs = keysym2ucs(sym);
  132. if ((int16_t)ucs == -1) {
  133. fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
  134. return;
  135. }
  136. /* store the UCS in a string to convert it */
  137. uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
  138. /* store it in the password array as UTF-8 */
  139. input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
  140. password[input_position] = '\0';
  141. //printf("current password = %s\n", password);
  142. }
  143. /*
  144. * A visibility notify event will be received when the visibility (= can the
  145. * user view the complete window) changes, so for example when a popup overlays
  146. * some area of the i3lock window.
  147. *
  148. * In this case, we raise our window on top so that the popup (or whatever is
  149. * hiding us) gets hidden.
  150. *
  151. */
  152. void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
  153. printf("visibility notify (window 0x%08x, state %d)\n", event->window, event->state);
  154. if (event->state != XCB_VISIBILITY_UNOBSCURED) {
  155. printf("window is obscured (not fully visible), raising\n");
  156. uint32_t values[] = { XCB_STACK_MODE_ABOVE };
  157. xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
  158. xcb_flush(conn);
  159. }
  160. }
  161. /*
  162. * Callback function for PAM. We only react on password request callbacks.
  163. *
  164. */
  165. static int conv_callback(int num_msg, const struct pam_message **msg,
  166. struct pam_response **resp, void *appdata_ptr)
  167. {
  168. if (num_msg == 0)
  169. return 1;
  170. /* PAM expects an array of responses, one for each message */
  171. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  172. perror("calloc");
  173. return 1;
  174. }
  175. for (int c = 0; c < num_msg; c++) {
  176. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  177. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  178. continue;
  179. /* return code is currently not used but should be set to zero */
  180. resp[c]->resp_retcode = 0;
  181. if ((resp[c]->resp = strdup(password)) == NULL) {
  182. perror("strdup");
  183. return 1;
  184. }
  185. }
  186. return 0;
  187. }
  188. int main(int argc, char *argv[]) {
  189. bool dont_fork = false;
  190. bool dpms = false;
  191. char color[7] = "ffffff";
  192. char *username;
  193. char *image_path = NULL;
  194. int ret;
  195. struct pam_conv conv = {conv_callback, NULL};
  196. int screen;
  197. cairo_surface_t *img = NULL;
  198. xcb_visualtype_t *vistype;
  199. xcb_generic_event_t *event;
  200. xcb_screen_t *scr;
  201. xcb_window_t win;
  202. char o;
  203. int optind = 0;
  204. struct option longopts[] = {
  205. {"version", no_argument, NULL, 'v'},
  206. {"nofork", no_argument, NULL, 'n'},
  207. {"beep", no_argument, NULL, 'b'},
  208. {"dpms", no_argument, NULL, 'd'},
  209. {"image", required_argument, NULL, 'i'},
  210. {"color", required_argument, NULL, 'c'},
  211. {"tiling", no_argument, NULL, 't'},
  212. {"pointer", required_argument, NULL , 'p'},
  213. {NULL, no_argument, NULL, 0}
  214. };
  215. if ((username = getenv("USER")) == NULL)
  216. errx(1, "USER environment variable not set, please set it.\n");
  217. while ((o = getopt_long(argc, argv, "vnbdi:c:tp:", longopts, &optind)) != -1) {
  218. switch (o) {
  219. case 'v':
  220. errx(EXIT_SUCCESS, "i3lock © 2010 Michael Stapelberg\n");
  221. case 'n':
  222. dont_fork = true;
  223. break;
  224. case 'b':
  225. beep = true;
  226. break;
  227. case 'd':
  228. dpms = true;
  229. break;
  230. case 'i':
  231. image_path = strdup(optarg);
  232. break;
  233. case 'c': {
  234. char *arg = optarg;
  235. /* Skip # if present */
  236. if (arg[0] == '#')
  237. arg++;
  238. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  239. errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
  240. break;
  241. }
  242. case 't':
  243. /* TODO: tile image */
  244. break;
  245. case 'p':
  246. /* TODO: cursor */
  247. break;
  248. default:
  249. errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.png] [-c color] [-t] [-p win|default]\n");
  250. }
  251. }
  252. /* Initialize PAM */
  253. ret = pam_start("i3lock", username, &conv, &pam_handle);
  254. if (ret != PAM_SUCCESS)
  255. errx(EXIT_FAILURE, "PAM: %s\n", pam_strerror(pam_handle, ret));
  256. /* Initialize connection to X11 */
  257. if ((conn = xcb_connect(NULL, &screen)) == NULL)
  258. err(EXIT_FAILURE, "xcb_connect()");
  259. if (!dont_fork) {
  260. /* In the parent process, we exit */
  261. if (fork() != 0)
  262. return 0;
  263. }
  264. /* if DPMS is enabled, check if the X server really supports it */
  265. if (dpms) {
  266. xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
  267. xcb_dpms_capable_reply_t *dpmsr;
  268. if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
  269. fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
  270. dpms = false;
  271. }
  272. }
  273. scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  274. vistype = get_root_visual_type(scr);
  275. /* open the fullscreen window and flush immediately afterwards so that X11
  276. * can generate an expose event while we load the PNG file (so that we are
  277. * ready to handle the expose event immediately afterwards) */
  278. win = open_fullscreen_window(conn, scr, color);
  279. grab_pointer_and_keyboard(conn, scr);
  280. if (image_path)
  281. img = cairo_image_surface_create_from_png(image_path);
  282. symbols = xcb_key_symbols_alloc(conn);
  283. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  284. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  285. if (img) {
  286. /* Initialize cairo */
  287. cairo_surface_t *output;
  288. output = cairo_xcb_surface_create(conn, win, vistype,
  289. scr->width_in_pixels, scr->height_in_pixels);
  290. /* TODO: tiling of the image */
  291. ctx = cairo_create(output);
  292. cairo_set_source_surface(ctx, img, 0, 0);
  293. handle_expose_event();
  294. }
  295. if (dpms)
  296. dpms_turn_off_screen(conn);
  297. while ((event = xcb_wait_for_event(conn))) {
  298. if (event->response_type == 0)
  299. errx(1, "XCB: Invalid event received");
  300. /* Strip off the highest bit (set if the event is generated) */
  301. int type = (event->response_type & 0x7F);
  302. if (type == XCB_EXPOSE) {
  303. handle_expose_event();
  304. continue;
  305. }
  306. if (type == XCB_KEY_PRESS) {
  307. handle_key_press((xcb_key_press_event_t*)event);
  308. continue;
  309. }
  310. if (type == XCB_KEY_RELEASE) {
  311. handle_key_release((xcb_key_release_event_t*)event);
  312. /* If this was the backspace or escape key we are back at an
  313. * empty input, so turn off the screen if DPMS is enabled */
  314. if (dpms && input_position == 0)
  315. dpms_turn_off_screen(conn);
  316. continue;
  317. }
  318. if (type == XCB_VISIBILITY_NOTIFY) {
  319. handle_visibility_notify((xcb_visibility_notify_event_t*)event);
  320. continue;
  321. }
  322. printf("WARNING: unhandled event of type %d\n", type);
  323. }
  324. return 0;
  325. }