2 * vim:ts=4:sw=4:expandtab
4 * © 2010-2012 Michael Stapelberg
6 * See LICENSE for licensing information
13 #include <xcb/xcb_keysyms.h>
17 #include <cairo/cairo-xcb.h>
21 #include "unlock_indicator.h"
23 #define BUTTON_RADIUS 90
24 #define BUTTON_SPACE (BUTTON_RADIUS + 5)
25 #define BUTTON_CENTER (BUTTON_RADIUS + 5)
26 #define BUTTON_DIAMETER (5 * BUTTON_SPACE)
28 /*******************************************************************************
29 * Variables defined in i3lock.c.
30 ******************************************************************************/
32 /* The lock window. */
33 extern xcb_window_t win;
35 /* The current resolution of the X11 root window. */
36 extern uint32_t last_resolution[2];
38 /* Whether the unlock indicator is enabled (defaults to true). */
39 extern bool unlock_indicator;
41 /* A Cairo surface containing the specified image (-i), if any. */
42 extern cairo_surface_t *img;
43 /* Whether the image should be tiled. */
45 /* The background color to use (in hex). */
48 /*******************************************************************************
50 ******************************************************************************/
52 /* Cache the screen’s visual, necessary for creating a Cairo context. */
53 static xcb_visualtype_t *vistype;
55 /* Maintain the current unlock/PAM state to draw the appropriate unlock
57 unlock_state_t unlock_state;
58 pam_state_t pam_state;
61 * Draws global image with fill color onto a pixmap with the given
62 * resolution and returns it.
65 xcb_pixmap_t draw_image(uint32_t *resolution) {
66 xcb_pixmap_t bg_pixmap = XCB_NONE;
70 vistype = get_root_visual_type(screen);
71 bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
72 /* Initialize cairo */
73 cairo_surface_t *output;
74 output = cairo_xcb_surface_create(conn, bg_pixmap, vistype,
75 resolution[0], resolution[1]);
76 cairo_t *ctx = cairo_create(output);
79 cairo_set_source_surface(ctx, img, 0, 0);
82 /* create a pattern and fill a rectangle as big as the screen */
83 cairo_pattern_t *pattern;
84 pattern = cairo_pattern_create_for_surface(img);
85 cairo_set_source(ctx, pattern);
86 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
87 cairo_rectangle(ctx, 0, 0, resolution[0], resolution[1]);
89 cairo_pattern_destroy(pattern);
93 if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
94 cairo_pattern_t *outer_pat = NULL;
96 outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
98 case STATE_PAM_VERIFY:
99 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 0, 250.0/255);
100 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 0, 250.0/255);
102 case STATE_PAM_WRONG:
103 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 255.0/250, 139.0/255, 0);
104 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 125.0/255, 51.0/255, 0);
107 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 125.0/255, 0);
108 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 125.0/255, 0);
112 /* Draw a (centered) circle with transparent background. */
113 cairo_set_line_width(ctx, 10.0);
115 (resolution[0] / 2) /* x */,
116 (resolution[1] / 2) /* y */,
117 BUTTON_RADIUS /* radius */,
121 /* Use the appropriate color for the different PAM states
122 * (currently verifying, wrong password, or default) */
124 case STATE_PAM_VERIFY:
125 cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
127 case STATE_PAM_WRONG:
128 cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
131 cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
134 cairo_fill_preserve(ctx);
135 cairo_set_source(ctx, outer_pat);
138 /* Draw an inner seperator line. */
139 cairo_set_source_rgb(ctx, 0, 0, 0);
140 cairo_set_line_width(ctx, 2.0);
142 (resolution[0] / 2) /* x */,
143 (resolution[1] / 2) /* y */,
144 BUTTON_RADIUS - 5 /* radius */,
149 cairo_set_line_width(ctx, 10.0);
151 /* Display a (centered) text of the current PAM state. */
154 case STATE_PAM_VERIFY:
157 case STATE_PAM_WRONG:
165 cairo_text_extents_t extents;
168 cairo_set_source_rgb(ctx, 0, 0, 0);
169 cairo_set_font_size(ctx, 28.0);
171 cairo_text_extents(ctx, text, &extents);
172 x = (resolution[0] / 2.0) - ((extents.width / 2) + extents.x_bearing);
173 y = (resolution[1] / 2.0) - ((extents.height / 2) + extents.y_bearing);
175 cairo_move_to(ctx, x, y);
176 cairo_show_text(ctx, text);
177 cairo_close_path(ctx);
180 /* After the user pressed any valid key or the backspace key, we
181 * highlight a random part of the unlock indicator to confirm this
183 if (unlock_state == STATE_KEY_ACTIVE ||
184 unlock_state == STATE_BACKSPACE_ACTIVE) {
185 cairo_new_sub_path(ctx);
186 double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
187 cairo_arc(ctx, resolution[0] / 2 /* x */, resolution[1] / 2 /* y */,
188 BUTTON_RADIUS /* radius */, highlight_start,
189 highlight_start + (M_PI / 3.0));
190 if (unlock_state == STATE_KEY_ACTIVE) {
191 /* For normal keys, we use a lighter green. */
192 outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
193 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 219.0/255, 0);
194 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 219.0/255, 0);
196 /* For backspace, we use red. */
197 outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
198 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 219.0/255, 139.0/255, 0);
199 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 219.0/255, 51.0/255, 0);
201 cairo_set_source(ctx, outer_pat);
204 /* Draw two little separators for the highlighted part of the
205 * unlock indicator. */
206 cairo_set_source_rgb(ctx, 0, 0, 0);
208 (resolution[0] / 2) /* x */,
209 (resolution[1] / 2) /* y */,
210 BUTTON_RADIUS /* radius */,
211 highlight_start /* start */,
212 highlight_start + (M_PI / 128.0) /* end */);
215 (resolution[0] / 2) /* x */,
216 (resolution[1] / 2) /* y */,
217 BUTTON_RADIUS /* radius */,
218 highlight_start + (M_PI / 3.0) /* start */,
219 (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */);
224 cairo_surface_destroy(output);
231 * Calls draw_image on a new pixmap and swaps that with the current pixmap
234 void redraw_screen() {
235 xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
236 xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
237 /* XXX: Possible optimization: Only update the area in the middle of the
238 * screen instead of the whole screen. */
239 xcb_clear_area(conn, 0, win, 0, 0, screen->width_in_pixels, screen->height_in_pixels);
240 xcb_free_pixmap(conn, bg_pixmap);