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.

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