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.

328 lines
11 KiB

  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010-2012 Michael Stapelberg
  5. *
  6. * See LICENSE for licensing information
  7. *
  8. */
  9. #include <stdbool.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <xcb/xcb.h>
  13. #include <xcb/xcb_keysyms.h>
  14. #include <ev.h>
  15. #ifndef NOLIBCAIRO
  16. #include <cairo.h>
  17. #include <cairo/cairo-xcb.h>
  18. #endif
  19. #include "xcb.h"
  20. #include "unlock_indicator.h"
  21. #include "xinerama.h"
  22. #define BUTTON_RADIUS 90
  23. #define BUTTON_SPACE (BUTTON_RADIUS + 5)
  24. #define BUTTON_CENTER (BUTTON_RADIUS + 5)
  25. #define BUTTON_DIAMETER (2 * BUTTON_SPACE)
  26. /*******************************************************************************
  27. * Variables defined in i3lock.c.
  28. ******************************************************************************/
  29. /* The current position in the input buffer. Useful to determine if any
  30. * characters of the password have already been entered or not. */
  31. int input_position;
  32. /* The ev main loop. */
  33. struct ev_loop *main_loop;
  34. /* The lock window. */
  35. extern xcb_window_t win;
  36. /* The current resolution of the X11 root window. */
  37. extern uint32_t last_resolution[2];
  38. /* Whether the unlock indicator is enabled (defaults to true). */
  39. extern bool unlock_indicator;
  40. /* A Cairo surface containing the specified image (-i), if any. */
  41. extern cairo_surface_t *img;
  42. /* Whether the image should be tiled. */
  43. extern bool tile;
  44. /* The background color to use (in hex). */
  45. extern char color[7];
  46. /*******************************************************************************
  47. * Local variables.
  48. ******************************************************************************/
  49. static struct ev_timer *clear_indicator_timeout;
  50. /* Cache the screen’s visual, necessary for creating a Cairo context. */
  51. static xcb_visualtype_t *vistype;
  52. /* Maintain the current unlock/PAM state to draw the appropriate unlock
  53. * indicator. */
  54. unlock_state_t unlock_state;
  55. pam_state_t pam_state;
  56. /*
  57. * Draws global image with fill color onto a pixmap with the given
  58. * resolution and returns it.
  59. *
  60. */
  61. xcb_pixmap_t draw_image(uint32_t *resolution) {
  62. xcb_pixmap_t bg_pixmap = XCB_NONE;
  63. #ifndef NOLIBCAIRO
  64. if (!vistype)
  65. vistype = get_root_visual_type(screen);
  66. bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
  67. /* Initialize cairo: Create one in-memory surface to render the unlock
  68. * indicator on, create one XCB surface to actually draw (one or more,
  69. * depending on the amount of screens) unlock indicators on. */
  70. cairo_surface_t *output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, BUTTON_DIAMETER, BUTTON_DIAMETER);
  71. cairo_t *ctx = cairo_create(output);
  72. cairo_surface_t *xcb_output = cairo_xcb_surface_create(conn, bg_pixmap, vistype, resolution[0], resolution[1]);
  73. cairo_t *xcb_ctx = cairo_create(xcb_output);
  74. if (img) {
  75. if (!tile) {
  76. cairo_set_source_surface(xcb_ctx, img, 0, 0);
  77. cairo_paint(xcb_ctx);
  78. } else {
  79. /* create a pattern and fill a rectangle as big as the screen */
  80. cairo_pattern_t *pattern;
  81. pattern = cairo_pattern_create_for_surface(img);
  82. cairo_set_source(xcb_ctx, pattern);
  83. cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
  84. cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
  85. cairo_fill(xcb_ctx);
  86. cairo_pattern_destroy(pattern);
  87. }
  88. } else {
  89. char strgroups[3][3] = {{color[0], color[1], '\0'},
  90. {color[2], color[3], '\0'},
  91. {color[4], color[5], '\0'}};
  92. uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
  93. (strtol(strgroups[1], NULL, 16)),
  94. (strtol(strgroups[2], NULL, 16))};
  95. cairo_set_source_rgb(xcb_ctx, rgb16[0], rgb16[1], rgb16[2]);
  96. cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
  97. cairo_fill(xcb_ctx);
  98. }
  99. if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
  100. /* Draw a (centered) circle with transparent background. */
  101. cairo_set_line_width(ctx, 10.0);
  102. cairo_arc(ctx,
  103. BUTTON_CENTER /* x */,
  104. BUTTON_CENTER /* y */,
  105. BUTTON_RADIUS /* radius */,
  106. 0 /* start */,
  107. 2 * M_PI /* end */);
  108. /* Use the appropriate color for the different PAM states
  109. * (currently verifying, wrong password, or default) */
  110. switch (pam_state) {
  111. case STATE_PAM_VERIFY:
  112. cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
  113. break;
  114. case STATE_PAM_WRONG:
  115. cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
  116. break;
  117. default:
  118. cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
  119. break;
  120. }
  121. cairo_fill_preserve(ctx);
  122. switch (pam_state) {
  123. case STATE_PAM_VERIFY:
  124. cairo_set_source_rgb(ctx, 51.0/255, 0, 250.0/255);
  125. break;
  126. case STATE_PAM_WRONG:
  127. cairo_set_source_rgb(ctx, 125.0/255, 51.0/255, 0);
  128. break;
  129. case STATE_PAM_IDLE:
  130. cairo_set_source_rgb(ctx, 51.0/255, 125.0/255, 0);
  131. break;
  132. }
  133. cairo_stroke(ctx);
  134. /* Draw an inner seperator line. */
  135. cairo_set_source_rgb(ctx, 0, 0, 0);
  136. cairo_set_line_width(ctx, 2.0);
  137. cairo_arc(ctx,
  138. BUTTON_CENTER /* x */,
  139. BUTTON_CENTER /* y */,
  140. BUTTON_RADIUS - 5 /* radius */,
  141. 0,
  142. 2 * M_PI);
  143. cairo_stroke(ctx);
  144. cairo_set_line_width(ctx, 10.0);
  145. /* Display a (centered) text of the current PAM state. */
  146. char *text = NULL;
  147. switch (pam_state) {
  148. case STATE_PAM_VERIFY:
  149. text = "verifying…";
  150. break;
  151. case STATE_PAM_WRONG:
  152. text = "wrong!";
  153. break;
  154. default:
  155. break;
  156. }
  157. if (text) {
  158. cairo_text_extents_t extents;
  159. double x, y;
  160. cairo_set_source_rgb(ctx, 0, 0, 0);
  161. cairo_set_font_size(ctx, 28.0);
  162. cairo_text_extents(ctx, text, &extents);
  163. x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing);
  164. y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing);
  165. cairo_move_to(ctx, x, y);
  166. cairo_show_text(ctx, text);
  167. cairo_close_path(ctx);
  168. }
  169. /* After the user pressed any valid key or the backspace key, we
  170. * highlight a random part of the unlock indicator to confirm this
  171. * keypress. */
  172. if (unlock_state == STATE_KEY_ACTIVE ||
  173. unlock_state == STATE_BACKSPACE_ACTIVE) {
  174. cairo_new_sub_path(ctx);
  175. double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
  176. cairo_arc(ctx,
  177. BUTTON_CENTER /* x */,
  178. BUTTON_CENTER /* y */,
  179. BUTTON_RADIUS /* radius */,
  180. highlight_start,
  181. highlight_start + (M_PI / 3.0));
  182. if (unlock_state == STATE_KEY_ACTIVE) {
  183. /* For normal keys, we use a lighter green. */
  184. cairo_set_source_rgb(ctx, 51.0/255, 219.0/255, 0);
  185. } else {
  186. /* For backspace, we use red. */
  187. cairo_set_source_rgb(ctx, 219.0/255, 51.0/255, 0);
  188. }
  189. cairo_stroke(ctx);
  190. /* Draw two little separators for the highlighted part of the
  191. * unlock indicator. */
  192. cairo_set_source_rgb(ctx, 0, 0, 0);
  193. cairo_arc(ctx,
  194. BUTTON_CENTER /* x */,
  195. BUTTON_CENTER /* y */,
  196. BUTTON_RADIUS /* radius */,
  197. highlight_start /* start */,
  198. highlight_start + (M_PI / 128.0) /* end */);
  199. cairo_stroke(ctx);
  200. cairo_arc(ctx,
  201. BUTTON_CENTER /* x */,
  202. BUTTON_CENTER /* y */,
  203. BUTTON_RADIUS /* radius */,
  204. highlight_start + (M_PI / 3.0) /* start */,
  205. (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */);
  206. cairo_stroke(ctx);
  207. }
  208. }
  209. if (xr_screens > 0) {
  210. /* Composite the unlock indicator in the middle of each screen. */
  211. for (int screen = 0; screen < xr_screens; screen++) {
  212. int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (BUTTON_DIAMETER / 2)));
  213. int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (BUTTON_DIAMETER / 2)));
  214. cairo_set_source_surface(xcb_ctx, output, x, y);
  215. cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER);
  216. cairo_fill(xcb_ctx);
  217. }
  218. } else {
  219. /* We have no information about the screen sizes/positions, so we just
  220. * place the unlock indicator in the middle of the X root window and
  221. * hope for the best. */
  222. int x = (last_resolution[0] / 2);
  223. int y = (last_resolution[1] / 2);
  224. cairo_set_source_surface(xcb_ctx, output, x, y);
  225. cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER);
  226. cairo_fill(xcb_ctx);
  227. }
  228. cairo_surface_destroy(xcb_output);
  229. cairo_surface_destroy(output);
  230. cairo_destroy(ctx);
  231. cairo_destroy(xcb_ctx);
  232. #endif
  233. return bg_pixmap;
  234. }
  235. /*
  236. * Calls draw_image on a new pixmap and swaps that with the current pixmap
  237. *
  238. */
  239. void redraw_screen() {
  240. xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
  241. xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
  242. /* XXX: Possible optimization: Only update the area in the middle of the
  243. * screen instead of the whole screen. */
  244. xcb_clear_area(conn, 0, win, 0, 0, screen->width_in_pixels, screen->height_in_pixels);
  245. xcb_free_pixmap(conn, bg_pixmap);
  246. xcb_flush(conn);
  247. }
  248. /*
  249. * Hides the unlock indicator completely when there is no content in the
  250. * password buffer.
  251. *
  252. */
  253. static void clear_indicator(EV_P_ ev_timer *w, int revents) {
  254. if (input_position == 0) {
  255. unlock_state = STATE_STARTED;
  256. } else unlock_state = STATE_KEY_PRESSED;
  257. redraw_screen();
  258. ev_timer_stop(main_loop, clear_indicator_timeout);
  259. free(clear_indicator_timeout);
  260. clear_indicator_timeout = NULL;
  261. }
  262. /*
  263. * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
  264. * after an unsuccessful authentication attempt.
  265. *
  266. */
  267. void start_clear_indicator_timeout() {
  268. if (clear_indicator_timeout) {
  269. ev_timer_stop(main_loop, clear_indicator_timeout);
  270. ev_timer_set(clear_indicator_timeout, 1.0, 0.);
  271. ev_timer_start(main_loop, clear_indicator_timeout);
  272. } else {
  273. /* When there is no memory, we just don’t have a timeout. We cannot
  274. * exit() here, since that would effectively unlock the screen. */
  275. if (!(clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1)))
  276. return;
  277. ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
  278. ev_timer_start(main_loop, clear_indicator_timeout);
  279. }
  280. }
  281. /*
  282. * Stops the clear_indicator timeout.
  283. *
  284. */
  285. void stop_clear_indicator_timeout() {
  286. if (clear_indicator_timeout) {
  287. ev_timer_stop(main_loop, clear_indicator_timeout);
  288. free(clear_indicator_timeout);
  289. clear_indicator_timeout = NULL;
  290. }
  291. }