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.

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