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.

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