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.

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