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.

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