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.

326 lines
12 KiB

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