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.

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