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.

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