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.

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