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.

282 lines
10 KiB

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