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.

303 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. cairo_pattern_destroy(outer_pat);
  128. /* Draw an inner seperator line. */
  129. cairo_set_source_rgb(ctx, 0, 0, 0);
  130. cairo_set_line_width(ctx, 2.0);
  131. cairo_arc(ctx,
  132. (resolution[0] / 2) /* x */,
  133. (resolution[1] / 2) /* y */,
  134. BUTTON_RADIUS - 5 /* radius */,
  135. 0,
  136. 2 * M_PI);
  137. cairo_stroke(ctx);
  138. cairo_set_line_width(ctx, 10.0);
  139. /* Display a (centered) text of the current PAM state. */
  140. char *text = NULL;
  141. switch (pam_state) {
  142. case STATE_PAM_VERIFY:
  143. text = "verifying…";
  144. break;
  145. case STATE_PAM_WRONG:
  146. text = "wrong!";
  147. break;
  148. default:
  149. break;
  150. }
  151. if (text) {
  152. cairo_text_extents_t extents;
  153. double x, y;
  154. cairo_set_source_rgb(ctx, 0, 0, 0);
  155. cairo_set_font_size(ctx, 28.0);
  156. cairo_text_extents(ctx, text, &extents);
  157. x = (resolution[0] / 2.0) - ((extents.width / 2) + extents.x_bearing);
  158. y = (resolution[1] / 2.0) - ((extents.height / 2) + extents.y_bearing);
  159. cairo_move_to(ctx, x, y);
  160. cairo_show_text(ctx, text);
  161. cairo_close_path(ctx);
  162. }
  163. /* After the user pressed any valid key or the backspace key, we
  164. * highlight a random part of the unlock indicator to confirm this
  165. * keypress. */
  166. if (unlock_state == STATE_KEY_ACTIVE ||
  167. unlock_state == STATE_BACKSPACE_ACTIVE) {
  168. cairo_new_sub_path(ctx);
  169. double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
  170. cairo_arc(ctx, resolution[0] / 2 /* x */, resolution[1] / 2 /* y */,
  171. BUTTON_RADIUS /* radius */, highlight_start,
  172. highlight_start + (M_PI / 3.0));
  173. if (unlock_state == STATE_KEY_ACTIVE) {
  174. /* For normal keys, we use a lighter green. */
  175. outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
  176. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 219.0/255, 0);
  177. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 219.0/255, 0);
  178. } else {
  179. /* For backspace, we use red. */
  180. outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
  181. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 219.0/255, 139.0/255, 0);
  182. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 219.0/255, 51.0/255, 0);
  183. }
  184. cairo_set_source(ctx, outer_pat);
  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. (resolution[0] / 2) /* x */,
  191. (resolution[1] / 2) /* 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. (resolution[0] / 2) /* x */,
  198. (resolution[1] / 2) /* 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. cairo_pattern_destroy(outer_pat);
  204. }
  205. }
  206. cairo_surface_destroy(output);
  207. cairo_destroy(ctx);
  208. #endif
  209. return bg_pixmap;
  210. }
  211. /*
  212. * Calls draw_image on a new pixmap and swaps that with the current pixmap
  213. *
  214. */
  215. void redraw_screen() {
  216. xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
  217. xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
  218. /* XXX: Possible optimization: Only update the area in the middle of the
  219. * screen instead of the whole screen. */
  220. xcb_clear_area(conn, 0, win, 0, 0, screen->width_in_pixels, screen->height_in_pixels);
  221. xcb_free_pixmap(conn, bg_pixmap);
  222. xcb_flush(conn);
  223. }
  224. /*
  225. * Hides the unlock indicator completely when there is no content in the
  226. * password buffer.
  227. *
  228. */
  229. static void clear_indicator(EV_P_ ev_timer *w, int revents) {
  230. if (input_position == 0) {
  231. unlock_state = STATE_STARTED;
  232. } else unlock_state = STATE_KEY_PRESSED;
  233. redraw_screen();
  234. ev_timer_stop(main_loop, clear_indicator_timeout);
  235. free(clear_indicator_timeout);
  236. clear_indicator_timeout = NULL;
  237. }
  238. /*
  239. * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
  240. * after an unsuccessful authentication attempt.
  241. *
  242. */
  243. void start_clear_indicator_timeout() {
  244. if (clear_indicator_timeout) {
  245. ev_timer_stop(main_loop, clear_indicator_timeout);
  246. ev_timer_set(clear_indicator_timeout, 1.0, 0.);
  247. ev_timer_start(main_loop, clear_indicator_timeout);
  248. } else {
  249. /* When there is no memory, we just don’t have a timeout. We cannot
  250. * exit() here, since that would effectively unlock the screen. */
  251. if (!(clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1)))
  252. return;
  253. ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
  254. ev_timer_start(main_loop, clear_indicator_timeout);
  255. }
  256. }
  257. /*
  258. * Stops the clear_indicator timeout.
  259. *
  260. */
  261. void stop_clear_indicator_timeout() {
  262. if (clear_indicator_timeout) {
  263. ev_timer_stop(main_loop, clear_indicator_timeout);
  264. free(clear_indicator_timeout);
  265. clear_indicator_timeout = NULL;
  266. }
  267. }