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.

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