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.

504 lines
15 KiB

14 years ago
15 years ago
14 years ago
  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010-2011 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_cursor_t cursor;
  35. static xcb_key_symbols_t *symbols;
  36. static xcb_screen_t *scr;
  37. static pam_handle_t *pam_handle;
  38. static int input_position = 0;
  39. /* holds the password you enter (in UTF-8) */
  40. static char password[512];
  41. static bool modeswitch_active = false;
  42. static int modeswitchmask;
  43. static int numlockmask;
  44. static bool beep = false;
  45. #ifndef NOLIBCAIRO
  46. static cairo_surface_t *img = NULL;
  47. static bool tile = false;
  48. #endif
  49. /*
  50. * Draws global image with fill color onto a pixmap with the given
  51. * resolution and returns it.
  52. *
  53. */
  54. xcb_pixmap_t draw_image(xcb_visualtype_t *vistype, u_int32_t* resolution, char* color) {
  55. xcb_pixmap_t bg_pixmap = XCB_NONE;
  56. #ifndef NOLIBCAIRO
  57. if (!img)
  58. return bg_pixmap;
  59. bg_pixmap = create_bg_pixmap(conn, scr, resolution, color);
  60. /* Initialize cairo */
  61. cairo_surface_t *output;
  62. output = cairo_xcb_surface_create(conn, bg_pixmap, vistype,
  63. resolution[0], resolution[1]);
  64. cairo_t *ctx = cairo_create(output);
  65. if (!tile) {
  66. cairo_set_source_surface(ctx, img, 0, 0);
  67. cairo_paint(ctx);
  68. } else {
  69. /* create a pattern and fill a rectangle as big as the screen */
  70. cairo_pattern_t *pattern;
  71. pattern = cairo_pattern_create_for_surface(img);
  72. cairo_set_source(ctx, pattern);
  73. cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
  74. cairo_rectangle(ctx, 0, 0, resolution[0], resolution[1]);
  75. cairo_fill(ctx);
  76. cairo_pattern_destroy(pattern);
  77. }
  78. cairo_surface_destroy(output);
  79. cairo_destroy(ctx);
  80. #endif
  81. return bg_pixmap;
  82. }
  83. static void input_done() {
  84. if (input_position == 0)
  85. return;
  86. /* TODO: change cursor during authentication? */
  87. if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
  88. printf("successfully authenticated\n");
  89. exit(0);
  90. }
  91. fprintf(stderr, "Authentication failure\n");
  92. /* beep on authentication failure, if enabled */
  93. if (beep) {
  94. xcb_bell(conn, 100);
  95. xcb_flush(conn);
  96. }
  97. }
  98. /*
  99. * Called when the user releases a key. We need to leave the Mode_switch
  100. * state when the user releases the Mode_switch key.
  101. *
  102. */
  103. static void handle_key_release(xcb_key_release_event_t *event) {
  104. //printf("releasing %d, state raw = %d\n", event->detail, event->state);
  105. /* fix state */
  106. event->state &= ~numlockmask;
  107. xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, event->state);
  108. if (sym == XK_Mode_switch) {
  109. //printf("Mode switch disabled\n");
  110. modeswitch_active = false;
  111. }
  112. }
  113. /*
  114. * Handle key presses. Fixes state, then looks up the key symbol for the
  115. * given keycode, then looks up the key symbol (as UCS-2), converts it to
  116. * UTF-8 and stores it in the password array.
  117. *
  118. */
  119. static void handle_key_press(xcb_key_press_event_t *event) {
  120. //printf("keypress %d, state raw = %d\n", event->detail, event->state);
  121. xcb_keysym_t sym0, sym1, sym;
  122. if (modeswitch_active) {
  123. sym0 = xcb_key_press_lookup_keysym(symbols, event, 4);
  124. sym1 = xcb_key_press_lookup_keysym(symbols, event, 5);
  125. } else {
  126. sym0 = xcb_key_press_lookup_keysym(symbols, event, 0);
  127. sym1 = xcb_key_press_lookup_keysym(symbols, event, 1);
  128. }
  129. switch (sym0) {
  130. case XK_Mode_switch:
  131. //printf("Mode switch enabled\n");
  132. modeswitch_active = true;
  133. return;
  134. case XK_Return:
  135. case XK_KP_Enter:
  136. input_done();
  137. case XK_Escape:
  138. input_position = 0;
  139. password[input_position] = '\0';
  140. return;
  141. case XK_BackSpace:
  142. if (input_position == 0)
  143. return;
  144. /* decrement input_position to point to the previous glyph */
  145. u8_dec(password, &input_position);
  146. password[input_position] = '\0';
  147. //printf("new input position = %d, new password = %s\n", input_position, password);
  148. return;
  149. }
  150. if ((input_position + 8) >= sizeof(password))
  151. return;
  152. if ((event->state & numlockmask) && xcb_is_keypad_key(sym1)) {
  153. /* this key was a keypad key */
  154. if ((event->state & XCB_MOD_MASK_SHIFT))
  155. sym = sym0;
  156. else sym = sym1;
  157. } else {
  158. if ((event->state & XCB_MOD_MASK_SHIFT))
  159. sym = sym1;
  160. else sym = sym0;
  161. }
  162. #if 0
  163. /* FIXME: handle all of these? */
  164. printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
  165. printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
  166. printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
  167. printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
  168. printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
  169. printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
  170. printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
  171. #endif
  172. if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
  173. return;
  174. //printf("sym = %c (%d)\n", sym, sym);
  175. /* convert the keysym to UCS */
  176. uint16_t ucs = keysym2ucs(sym);
  177. if ((int16_t)ucs == -1) {
  178. fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
  179. return;
  180. }
  181. /* store the UCS in a string to convert it */
  182. uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
  183. /* store it in the password array as UTF-8 */
  184. input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
  185. password[input_position] = '\0';
  186. //printf("current password = %s\n", password);
  187. }
  188. /*
  189. * A visibility notify event will be received when the visibility (= can the
  190. * user view the complete window) changes, so for example when a popup overlays
  191. * some area of the i3lock window.
  192. *
  193. * In this case, we raise our window on top so that the popup (or whatever is
  194. * hiding us) gets hidden.
  195. *
  196. */
  197. static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
  198. if (event->state != XCB_VISIBILITY_UNOBSCURED) {
  199. uint32_t values[] = { XCB_STACK_MODE_ABOVE };
  200. xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
  201. xcb_flush(conn);
  202. }
  203. }
  204. /*
  205. * Called when the keyboard mapping changes. We update our symbols and re-grab
  206. * pointer/keyboard.
  207. *
  208. */
  209. static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
  210. xcb_refresh_keyboard_mapping(symbols, event);
  211. xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
  212. xcb_ungrab_keyboard(conn, XCB_CURRENT_TIME);
  213. grab_pointer_and_keyboard(conn, scr, cursor);
  214. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  215. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  216. xcb_flush(conn);
  217. }
  218. /*
  219. * Called when the properties on the root window change, e.g. when the screen
  220. * resolution changes. If so we update the window to cover the whole screen
  221. * and also redraw the image, if any.
  222. *
  223. */
  224. void handle_screen_resize(xcb_visualtype_t *vistype, xcb_window_t win, uint32_t* last_resolution, char* color) {
  225. xcb_get_geometry_cookie_t geomc;
  226. xcb_get_geometry_reply_t *geom;
  227. geomc = xcb_get_geometry(conn, scr->root);
  228. if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL) {
  229. return;
  230. }
  231. if (last_resolution[0] == geom->width && last_resolution[1] == geom->height)
  232. return;
  233. last_resolution[0] = geom->width;
  234. last_resolution[1] = geom->height;
  235. #ifndef NOLIBCAIRO
  236. if (img) {
  237. xcb_pixmap_t bg_pixmap = draw_image(vistype, last_resolution, color);
  238. xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
  239. }
  240. #endif
  241. uint32_t mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
  242. xcb_configure_window(conn, win, mask, last_resolution);
  243. xcb_flush(conn);
  244. }
  245. /*
  246. * Callback function for PAM. We only react on password request callbacks.
  247. *
  248. */
  249. static int conv_callback(int num_msg, const struct pam_message **msg,
  250. struct pam_response **resp, void *appdata_ptr)
  251. {
  252. if (num_msg == 0)
  253. return 1;
  254. /* PAM expects an array of responses, one for each message */
  255. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  256. perror("calloc");
  257. return 1;
  258. }
  259. for (int c = 0; c < num_msg; c++) {
  260. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  261. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  262. continue;
  263. /* return code is currently not used but should be set to zero */
  264. resp[c]->resp_retcode = 0;
  265. if ((resp[c]->resp = strdup(password)) == NULL) {
  266. perror("strdup");
  267. return 1;
  268. }
  269. }
  270. return 0;
  271. }
  272. int main(int argc, char *argv[]) {
  273. bool dont_fork = false;
  274. bool dpms = false;
  275. char color[7] = "ffffff";
  276. char *username;
  277. #ifndef NOLIBCAIRO
  278. char *image_path = NULL;
  279. #endif
  280. int ret;
  281. struct pam_conv conv = {conv_callback, NULL};
  282. int screen;
  283. xcb_visualtype_t *vistype;
  284. xcb_generic_event_t *event;
  285. xcb_window_t win;
  286. int curs_choice = CURS_NONE;
  287. char o;
  288. int optind = 0;
  289. struct option longopts[] = {
  290. {"version", no_argument, NULL, 'v'},
  291. {"nofork", no_argument, NULL, 'n'},
  292. {"beep", no_argument, NULL, 'b'},
  293. {"dpms", no_argument, NULL, 'd'},
  294. {"color", required_argument, NULL, 'c'},
  295. {"pointer", required_argument, NULL , 'p'},
  296. #ifndef NOLIBCAIRO
  297. {"image", required_argument, NULL, 'i'},
  298. {"tiling", no_argument, NULL, 't'},
  299. #endif
  300. {NULL, no_argument, NULL, 0}
  301. };
  302. if ((username = getenv("USER")) == NULL)
  303. errx(1, "USER environment variable not set, please set it.\n");
  304. while ((o = getopt_long(argc, argv, "vnbdc:p:"
  305. #ifndef NOLIBCAIRO
  306. "i:t"
  307. #endif
  308. , longopts, &optind)) != -1) {
  309. switch (o) {
  310. case 'v':
  311. errx(EXIT_SUCCESS, "version " VERSION " © 2010-2011 Michael Stapelberg\n");
  312. case 'n':
  313. dont_fork = true;
  314. break;
  315. case 'b':
  316. beep = true;
  317. break;
  318. case 'd':
  319. dpms = true;
  320. break;
  321. case 'c': {
  322. char *arg = optarg;
  323. /* Skip # if present */
  324. if (arg[0] == '#')
  325. arg++;
  326. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  327. errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
  328. break;
  329. }
  330. #ifndef NOLIBCAIRO
  331. case 'i':
  332. image_path = strdup(optarg);
  333. break;
  334. case 't':
  335. tile = true;
  336. break;
  337. #endif
  338. case 'p':
  339. if (!strcmp(optarg, "win")) {
  340. curs_choice = CURS_WIN;
  341. }
  342. if (!strcmp(optarg, "default")) {
  343. curs_choice = CURS_DEFAULT;
  344. }
  345. break;
  346. default:
  347. errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-p win|default]"
  348. #ifndef NOLIBCAIRO
  349. " [-i image.png] [-t]"
  350. #else
  351. " (compiled with NOLIBCAIRO)"
  352. #endif
  353. "\n");
  354. }
  355. }
  356. /* Initialize PAM */
  357. ret = pam_start("i3lock", username, &conv, &pam_handle);
  358. if (ret != PAM_SUCCESS)
  359. errx(EXIT_FAILURE, "PAM: %s\n", pam_strerror(pam_handle, ret));
  360. /* Initialize connection to X11 */
  361. if ((conn = xcb_connect(NULL, &screen)) == NULL ||
  362. xcb_connection_has_error(conn))
  363. errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
  364. if (!dont_fork) {
  365. /* In the parent process, we exit */
  366. if (fork() != 0)
  367. return 0;
  368. }
  369. /* if DPMS is enabled, check if the X server really supports it */
  370. if (dpms) {
  371. xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
  372. xcb_dpms_capable_reply_t *dpmsr;
  373. if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
  374. fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
  375. dpms = false;
  376. }
  377. }
  378. scr = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  379. vistype = get_root_visual_type(scr);
  380. uint32_t last_resolution[2] = {scr->width_in_pixels, scr->height_in_pixels};
  381. #ifndef NOLIBCAIRO
  382. if (image_path) {
  383. /* Create a pixmap to render on, fill it with the background color */
  384. img = cairo_image_surface_create_from_png(image_path);
  385. }
  386. #endif
  387. /* Pixmap on which the image is rendered to (if any) */
  388. xcb_pixmap_t bg_pixmap = draw_image(vistype, last_resolution, color);
  389. /* open the fullscreen window, already with the correct pixmap in place */
  390. win = open_fullscreen_window(conn, scr, color, bg_pixmap);
  391. cursor = create_cursor(conn, scr, win, curs_choice);
  392. grab_pointer_and_keyboard(conn, scr, cursor);
  393. symbols = xcb_key_symbols_alloc(conn);
  394. modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
  395. numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
  396. if (dpms)
  397. dpms_turn_off_screen(conn);
  398. while ((event = xcb_wait_for_event(conn))) {
  399. if (event->response_type == 0)
  400. errx(1, "XCB: Invalid event received");
  401. /* Strip off the highest bit (set if the event is generated) */
  402. int type = (event->response_type & 0x7F);
  403. if (type == XCB_KEY_PRESS) {
  404. handle_key_press((xcb_key_press_event_t*)event);
  405. continue;
  406. }
  407. if (type == XCB_KEY_RELEASE) {
  408. handle_key_release((xcb_key_release_event_t*)event);
  409. /* If this was the backspace or escape key we are back at an
  410. * empty input, so turn off the screen if DPMS is enabled */
  411. if (dpms && input_position == 0)
  412. dpms_turn_off_screen(conn);
  413. continue;
  414. }
  415. if (type == XCB_VISIBILITY_NOTIFY) {
  416. handle_visibility_notify((xcb_visibility_notify_event_t*)event);
  417. continue;
  418. }
  419. if (type == XCB_MAPPING_NOTIFY) {
  420. handle_mapping_notify((xcb_mapping_notify_event_t*)event);
  421. continue;
  422. }
  423. if (type == XCB_CONFIGURE_NOTIFY) {
  424. handle_screen_resize(vistype, win, last_resolution, color);
  425. continue;
  426. }
  427. printf("WARNING: unhandled event of type %d\n", type);
  428. }
  429. return 0;
  430. }