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.

345 lines
12 KiB

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