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.

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