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.

242 lines
8.6 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 <xcb/xcb_keysyms.h>
  14. #ifndef NOLIBCAIRO
  15. #include <cairo.h>
  16. #include <cairo/cairo-xcb.h>
  17. #endif
  18. #include "xcb.h"
  19. #include "unlock_indicator.h"
  20. #define BUTTON_RADIUS 90
  21. #define BUTTON_SPACE (BUTTON_RADIUS + 5)
  22. #define BUTTON_CENTER (BUTTON_RADIUS + 5)
  23. #define BUTTON_DIAMETER (5 * BUTTON_SPACE)
  24. /*******************************************************************************
  25. * Variables defined in i3lock.c.
  26. ******************************************************************************/
  27. /* The lock window. */
  28. extern xcb_window_t win;
  29. /* The current resolution of the X11 root window. */
  30. extern uint32_t last_resolution[2];
  31. /* Whether the unlock indicator is enabled (defaults to true). */
  32. extern bool unlock_indicator;
  33. /* A Cairo surface containing the specified image (-i), if any. */
  34. extern cairo_surface_t *img;
  35. /* Whether the image should be tiled. */
  36. extern bool tile;
  37. /* The background color to use (in hex). */
  38. extern char color[7];
  39. /*******************************************************************************
  40. * Local variables.
  41. ******************************************************************************/
  42. /* Cache the screen’s visual, necessary for creating a Cairo context. */
  43. static xcb_visualtype_t *vistype;
  44. /* Maintain the current unlock/PAM state to draw the appropriate unlock
  45. * indicator. */
  46. unlock_state_t unlock_state;
  47. pam_state_t pam_state;
  48. /*
  49. * Draws global image with fill color onto a pixmap with the given
  50. * resolution and returns it.
  51. *
  52. */
  53. xcb_pixmap_t draw_image(uint32_t *resolution) {
  54. xcb_pixmap_t bg_pixmap = XCB_NONE;
  55. #ifndef NOLIBCAIRO
  56. if (!vistype)
  57. vistype = get_root_visual_type(screen);
  58. bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
  59. /* Initialize cairo */
  60. cairo_surface_t *output;
  61. output = cairo_xcb_surface_create(conn, bg_pixmap, vistype,
  62. resolution[0], resolution[1]);
  63. cairo_t *ctx = cairo_create(output);
  64. if (img) {
  65. if (!tile) {
  66. cairo_set_source_surface(ctx, img, 0, 0);
  67. cairo_paint(ctx);
  68. } else {
  69. /* create a pattern and fill a rectangle as big as the screen */
  70. cairo_pattern_t *pattern;
  71. pattern = cairo_pattern_create_for_surface(img);
  72. cairo_set_source(ctx, pattern);
  73. cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
  74. cairo_rectangle(ctx, 0, 0, resolution[0], resolution[1]);
  75. cairo_fill(ctx);
  76. cairo_pattern_destroy(pattern);
  77. }
  78. }
  79. if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
  80. cairo_pattern_t *outer_pat = NULL;
  81. outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
  82. switch (pam_state) {
  83. case STATE_PAM_VERIFY:
  84. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 0, 250.0/255);
  85. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 0, 250.0/255);
  86. break;
  87. case STATE_PAM_WRONG:
  88. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 255.0/250, 139.0/255, 0);
  89. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 125.0/255, 51.0/255, 0);
  90. break;
  91. case STATE_PAM_IDLE:
  92. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 125.0/255, 0);
  93. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 125.0/255, 0);
  94. break;
  95. }
  96. /* Draw a (centered) circle with transparent background. */
  97. cairo_set_line_width(ctx, 10.0);
  98. cairo_arc(ctx,
  99. (resolution[0] / 2) /* x */,
  100. (resolution[1] / 2) /* y */,
  101. BUTTON_RADIUS /* radius */,
  102. 0 /* start */,
  103. 2 * M_PI /* end */);
  104. /* Use the appropriate color for the different PAM states
  105. * (currently verifying, wrong password, or default) */
  106. switch (pam_state) {
  107. case STATE_PAM_VERIFY:
  108. cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
  109. break;
  110. case STATE_PAM_WRONG:
  111. cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
  112. break;
  113. default:
  114. cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
  115. break;
  116. }
  117. cairo_fill_preserve(ctx);
  118. cairo_set_source(ctx, outer_pat);
  119. cairo_stroke(ctx);
  120. /* Draw an inner seperator line. */
  121. cairo_set_source_rgb(ctx, 0, 0, 0);
  122. cairo_set_line_width(ctx, 2.0);
  123. cairo_arc(ctx,
  124. (resolution[0] / 2) /* x */,
  125. (resolution[1] / 2) /* y */,
  126. BUTTON_RADIUS - 5 /* radius */,
  127. 0,
  128. 2 * M_PI);
  129. cairo_stroke(ctx);
  130. cairo_set_line_width(ctx, 10.0);
  131. /* Display a (centered) text of the current PAM state. */
  132. char *text = NULL;
  133. switch (pam_state) {
  134. case STATE_PAM_VERIFY:
  135. text = "verifying…";
  136. break;
  137. case STATE_PAM_WRONG:
  138. text = "wrong!";
  139. break;
  140. default:
  141. break;
  142. }
  143. if (text) {
  144. cairo_text_extents_t extents;
  145. double x, y;
  146. cairo_set_source_rgb(ctx, 0, 0, 0);
  147. cairo_set_font_size(ctx, 28.0);
  148. cairo_text_extents(ctx, text, &extents);
  149. x = (resolution[0] / 2.0) - ((extents.width / 2) + extents.x_bearing);
  150. y = (resolution[1] / 2.0) - ((extents.height / 2) + extents.y_bearing);
  151. cairo_move_to(ctx, x, y);
  152. cairo_show_text(ctx, text);
  153. cairo_close_path(ctx);
  154. }
  155. /* After the user pressed any valid key or the backspace key, we
  156. * highlight a random part of the unlock indicator to confirm this
  157. * keypress. */
  158. if (unlock_state == STATE_KEY_ACTIVE ||
  159. unlock_state == STATE_BACKSPACE_ACTIVE) {
  160. cairo_new_sub_path(ctx);
  161. double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
  162. cairo_arc(ctx, resolution[0] / 2 /* x */, resolution[1] / 2 /* y */,
  163. BUTTON_RADIUS /* radius */, highlight_start,
  164. highlight_start + (M_PI / 3.0));
  165. if (unlock_state == STATE_KEY_ACTIVE) {
  166. /* For normal keys, we use a lighter green. */
  167. outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
  168. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 219.0/255, 0);
  169. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 219.0/255, 0);
  170. } else {
  171. /* For backspace, we use red. */
  172. outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
  173. cairo_pattern_add_color_stop_rgb(outer_pat, 0, 219.0/255, 139.0/255, 0);
  174. cairo_pattern_add_color_stop_rgb(outer_pat, 1, 219.0/255, 51.0/255, 0);
  175. }
  176. cairo_set_source(ctx, outer_pat);
  177. cairo_stroke(ctx);
  178. /* Draw two little separators for the highlighted part of the
  179. * unlock indicator. */
  180. cairo_set_source_rgb(ctx, 0, 0, 0);
  181. cairo_arc(ctx,
  182. (resolution[0] / 2) /* x */,
  183. (resolution[1] / 2) /* y */,
  184. BUTTON_RADIUS /* radius */,
  185. highlight_start /* start */,
  186. highlight_start + (M_PI / 128.0) /* end */);
  187. cairo_stroke(ctx);
  188. cairo_arc(ctx,
  189. (resolution[0] / 2) /* x */,
  190. (resolution[1] / 2) /* y */,
  191. BUTTON_RADIUS /* radius */,
  192. highlight_start + (M_PI / 3.0) /* start */,
  193. (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */);
  194. cairo_stroke(ctx);
  195. }
  196. }
  197. cairo_surface_destroy(output);
  198. cairo_destroy(ctx);
  199. #endif
  200. return bg_pixmap;
  201. }
  202. /*
  203. * Calls draw_image on a new pixmap and swaps that with the current pixmap
  204. *
  205. */
  206. void redraw_screen() {
  207. xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
  208. xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
  209. /* XXX: Possible optimization: Only update the area in the middle of the
  210. * screen instead of the whole screen. */
  211. xcb_clear_area(conn, 0, win, 0, 0, screen->width_in_pixels, screen->height_in_pixels);
  212. xcb_free_pixmap(conn, bg_pixmap);
  213. xcb_flush(conn);
  214. }