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.

358 lines
13 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 "randr.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. auth_state_t auth_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_indicator &&
  121. (unlock_state >= STATE_KEY_PRESSED || auth_state > STATE_AUTH_IDLE)) {
  122. cairo_scale(ctx, scaling_factor(), scaling_factor());
  123. /* Draw a (centered) circle with transparent background. */
  124. cairo_set_line_width(ctx, 10.0);
  125. cairo_arc(ctx,
  126. BUTTON_CENTER /* x */,
  127. BUTTON_CENTER /* y */,
  128. BUTTON_RADIUS /* radius */,
  129. 0 /* start */,
  130. 2 * M_PI /* end */);
  131. /* Use the appropriate color for the different PAM states
  132. * (currently verifying, wrong password, or default) */
  133. switch (auth_state) {
  134. case STATE_AUTH_VERIFY:
  135. case STATE_AUTH_LOCK:
  136. cairo_set_source_rgba(ctx, 0, 114.0 / 255, 255.0 / 255, 0.75);
  137. break;
  138. case STATE_AUTH_WRONG:
  139. case STATE_I3LOCK_LOCK_FAILED:
  140. cairo_set_source_rgba(ctx, 250.0 / 255, 0, 0, 0.75);
  141. break;
  142. default:
  143. cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
  144. break;
  145. }
  146. cairo_fill_preserve(ctx);
  147. switch (auth_state) {
  148. case STATE_AUTH_VERIFY:
  149. case STATE_AUTH_LOCK:
  150. cairo_set_source_rgb(ctx, 51.0 / 255, 0, 250.0 / 255);
  151. break;
  152. case STATE_AUTH_WRONG:
  153. case STATE_I3LOCK_LOCK_FAILED:
  154. cairo_set_source_rgb(ctx, 125.0 / 255, 51.0 / 255, 0);
  155. break;
  156. case STATE_AUTH_IDLE:
  157. cairo_set_source_rgb(ctx, 51.0 / 255, 125.0 / 255, 0);
  158. break;
  159. }
  160. cairo_stroke(ctx);
  161. /* Draw an inner seperator line. */
  162. cairo_set_source_rgb(ctx, 0, 0, 0);
  163. cairo_set_line_width(ctx, 2.0);
  164. cairo_arc(ctx,
  165. BUTTON_CENTER /* x */,
  166. BUTTON_CENTER /* y */,
  167. BUTTON_RADIUS - 5 /* radius */,
  168. 0,
  169. 2 * M_PI);
  170. cairo_stroke(ctx);
  171. cairo_set_line_width(ctx, 10.0);
  172. /* Display a (centered) text of the current PAM state. */
  173. char *text = NULL;
  174. /* We don't want to show more than a 3-digit number. */
  175. char buf[4];
  176. cairo_set_source_rgb(ctx, 0, 0, 0);
  177. cairo_select_font_face(ctx, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
  178. cairo_set_font_size(ctx, 28.0);
  179. switch (auth_state) {
  180. case STATE_AUTH_VERIFY:
  181. text = "verifying…";
  182. break;
  183. case STATE_AUTH_LOCK:
  184. text = "locking…";
  185. break;
  186. case STATE_AUTH_WRONG:
  187. text = "wrong!";
  188. break;
  189. case STATE_I3LOCK_LOCK_FAILED:
  190. text = "lock failed!";
  191. break;
  192. default:
  193. if (show_failed_attempts && failed_attempts > 0) {
  194. if (failed_attempts > 999) {
  195. text = "> 999";
  196. } else {
  197. snprintf(buf, sizeof(buf), "%d", failed_attempts);
  198. text = buf;
  199. }
  200. cairo_set_source_rgb(ctx, 1, 0, 0);
  201. cairo_set_font_size(ctx, 32.0);
  202. }
  203. break;
  204. }
  205. if (text) {
  206. cairo_text_extents_t extents;
  207. double x, y;
  208. cairo_text_extents(ctx, text, &extents);
  209. x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing);
  210. y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing);
  211. cairo_move_to(ctx, x, y);
  212. cairo_show_text(ctx, text);
  213. cairo_close_path(ctx);
  214. }
  215. if (auth_state == STATE_AUTH_WRONG && (modifier_string != NULL)) {
  216. cairo_text_extents_t extents;
  217. double x, y;
  218. cairo_set_font_size(ctx, 14.0);
  219. cairo_text_extents(ctx, modifier_string, &extents);
  220. x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing);
  221. y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing) + 28.0;
  222. cairo_move_to(ctx, x, y);
  223. cairo_show_text(ctx, modifier_string);
  224. cairo_close_path(ctx);
  225. }
  226. /* After the user pressed any valid key or the backspace key, we
  227. * highlight a random part of the unlock indicator to confirm this
  228. * keypress. */
  229. if (unlock_state == STATE_KEY_ACTIVE ||
  230. unlock_state == STATE_BACKSPACE_ACTIVE) {
  231. cairo_new_sub_path(ctx);
  232. double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
  233. cairo_arc(ctx,
  234. BUTTON_CENTER /* x */,
  235. BUTTON_CENTER /* y */,
  236. BUTTON_RADIUS /* radius */,
  237. highlight_start,
  238. highlight_start + (M_PI / 3.0));
  239. if (unlock_state == STATE_KEY_ACTIVE) {
  240. /* For normal keys, we use a lighter green. */
  241. cairo_set_source_rgb(ctx, 51.0 / 255, 219.0 / 255, 0);
  242. } else {
  243. /* For backspace, we use red. */
  244. cairo_set_source_rgb(ctx, 219.0 / 255, 51.0 / 255, 0);
  245. }
  246. cairo_stroke(ctx);
  247. /* Draw two little separators for the highlighted part of the
  248. * unlock indicator. */
  249. cairo_set_source_rgb(ctx, 0, 0, 0);
  250. cairo_arc(ctx,
  251. BUTTON_CENTER /* x */,
  252. BUTTON_CENTER /* y */,
  253. BUTTON_RADIUS /* radius */,
  254. highlight_start /* start */,
  255. highlight_start + (M_PI / 128.0) /* end */);
  256. cairo_stroke(ctx);
  257. cairo_arc(ctx,
  258. BUTTON_CENTER /* x */,
  259. BUTTON_CENTER /* y */,
  260. BUTTON_RADIUS /* radius */,
  261. (highlight_start + (M_PI / 3.0)) - (M_PI / 128.0) /* start */,
  262. highlight_start + (M_PI / 3.0) /* end */);
  263. cairo_stroke(ctx);
  264. }
  265. }
  266. if (xr_screens > 0) {
  267. /* Composite the unlock indicator in the middle of each screen. */
  268. for (int screen = 0; screen < xr_screens; screen++) {
  269. int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (button_diameter_physical / 2)));
  270. int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (button_diameter_physical / 2)));
  271. cairo_set_source_surface(xcb_ctx, output, x, y);
  272. cairo_rectangle(xcb_ctx, x, y, button_diameter_physical, button_diameter_physical);
  273. cairo_fill(xcb_ctx);
  274. }
  275. } else {
  276. /* We have no information about the screen sizes/positions, so we just
  277. * place the unlock indicator in the middle of the X root window and
  278. * hope for the best. */
  279. int x = (last_resolution[0] / 2) - (button_diameter_physical / 2);
  280. int y = (last_resolution[1] / 2) - (button_diameter_physical / 2);
  281. cairo_set_source_surface(xcb_ctx, output, x, y);
  282. cairo_rectangle(xcb_ctx, x, y, button_diameter_physical, button_diameter_physical);
  283. cairo_fill(xcb_ctx);
  284. }
  285. cairo_surface_destroy(xcb_output);
  286. cairo_surface_destroy(output);
  287. cairo_destroy(ctx);
  288. cairo_destroy(xcb_ctx);
  289. return bg_pixmap;
  290. }
  291. /*
  292. * Calls draw_image on a new pixmap and swaps that with the current pixmap
  293. *
  294. */
  295. void redraw_screen(void) {
  296. DEBUG("redraw_screen(unlock_state = %d, auth_state = %d)\n", unlock_state, auth_state);
  297. xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
  298. xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){bg_pixmap});
  299. /* XXX: Possible optimization: Only update the area in the middle of the
  300. * screen instead of the whole screen. */
  301. xcb_clear_area(conn, 0, win, 0, 0, last_resolution[0], last_resolution[1]);
  302. xcb_free_pixmap(conn, bg_pixmap);
  303. xcb_flush(conn);
  304. }
  305. /*
  306. * Hides the unlock indicator completely when there is no content in the
  307. * password buffer.
  308. *
  309. */
  310. void clear_indicator(void) {
  311. if (input_position == 0) {
  312. unlock_state = STATE_STARTED;
  313. } else
  314. unlock_state = STATE_KEY_PRESSED;
  315. redraw_screen();
  316. }