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.

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