2 * vim:ts=4:sw=4:expandtab
4 * © 2010-2012 Michael Stapelberg
6 * See LICENSE for licensing information
13 #include <xcb/xcb_keysyms.h>
18 #include <cairo/cairo-xcb.h>
22 #include "unlock_indicator.h"
24 #define BUTTON_RADIUS 90
25 #define BUTTON_SPACE (BUTTON_RADIUS + 5)
26 #define BUTTON_CENTER (BUTTON_RADIUS + 5)
27 #define BUTTON_DIAMETER (5 * BUTTON_SPACE)
29 /*******************************************************************************
30 * Variables defined in i3lock.c.
31 ******************************************************************************/
33 /* The current position in the input buffer. Useful to determine if any
34 * characters of the password have already been entered or not. */
37 /* The ev main loop. */
38 struct ev_loop *main_loop;
40 /* The lock window. */
41 extern xcb_window_t win;
43 /* The current resolution of the X11 root window. */
44 extern uint32_t last_resolution[2];
46 /* Whether the unlock indicator is enabled (defaults to true). */
47 extern bool unlock_indicator;
49 /* A Cairo surface containing the specified image (-i), if any. */
50 extern cairo_surface_t *img;
51 /* Whether the image should be tiled. */
53 /* The background color to use (in hex). */
56 /*******************************************************************************
58 ******************************************************************************/
60 static struct ev_timer *clear_indicator_timeout;
62 /* Cache the screen’s visual, necessary for creating a Cairo context. */
63 static xcb_visualtype_t *vistype;
65 /* Maintain the current unlock/PAM state to draw the appropriate unlock
67 unlock_state_t unlock_state;
68 pam_state_t pam_state;
71 * Draws global image with fill color onto a pixmap with the given
72 * resolution and returns it.
75 xcb_pixmap_t draw_image(uint32_t *resolution) {
76 xcb_pixmap_t bg_pixmap = XCB_NONE;
80 vistype = get_root_visual_type(screen);
81 bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
82 /* Initialize cairo */
83 cairo_surface_t *output;
84 output = cairo_xcb_surface_create(conn, bg_pixmap, vistype,
85 resolution[0], resolution[1]);
86 cairo_t *ctx = cairo_create(output);
89 cairo_set_source_surface(ctx, img, 0, 0);
92 /* create a pattern and fill a rectangle as big as the screen */
93 cairo_pattern_t *pattern;
94 pattern = cairo_pattern_create_for_surface(img);
95 cairo_set_source(ctx, pattern);
96 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
97 cairo_rectangle(ctx, 0, 0, resolution[0], resolution[1]);
99 cairo_pattern_destroy(pattern);
103 if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
104 cairo_pattern_t *outer_pat = NULL;
106 outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
108 case STATE_PAM_VERIFY:
109 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 0, 250.0/255);
110 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 0, 250.0/255);
112 case STATE_PAM_WRONG:
113 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 255.0/250, 139.0/255, 0);
114 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 125.0/255, 51.0/255, 0);
117 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 125.0/255, 0);
118 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 125.0/255, 0);
122 /* Draw a (centered) circle with transparent background. */
123 cairo_set_line_width(ctx, 10.0);
125 (resolution[0] / 2) /* x */,
126 (resolution[1] / 2) /* y */,
127 BUTTON_RADIUS /* radius */,
131 /* Use the appropriate color for the different PAM states
132 * (currently verifying, wrong password, or default) */
134 case STATE_PAM_VERIFY:
135 cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
137 case STATE_PAM_WRONG:
138 cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
141 cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
144 cairo_fill_preserve(ctx);
145 cairo_set_source(ctx, outer_pat);
148 /* Draw an inner seperator line. */
149 cairo_set_source_rgb(ctx, 0, 0, 0);
150 cairo_set_line_width(ctx, 2.0);
152 (resolution[0] / 2) /* x */,
153 (resolution[1] / 2) /* y */,
154 BUTTON_RADIUS - 5 /* radius */,
159 cairo_set_line_width(ctx, 10.0);
161 /* Display a (centered) text of the current PAM state. */
164 case STATE_PAM_VERIFY:
167 case STATE_PAM_WRONG:
175 cairo_text_extents_t extents;
178 cairo_set_source_rgb(ctx, 0, 0, 0);
179 cairo_set_font_size(ctx, 28.0);
181 cairo_text_extents(ctx, text, &extents);
182 x = (resolution[0] / 2.0) - ((extents.width / 2) + extents.x_bearing);
183 y = (resolution[1] / 2.0) - ((extents.height / 2) + extents.y_bearing);
185 cairo_move_to(ctx, x, y);
186 cairo_show_text(ctx, text);
187 cairo_close_path(ctx);
190 /* After the user pressed any valid key or the backspace key, we
191 * highlight a random part of the unlock indicator to confirm this
193 if (unlock_state == STATE_KEY_ACTIVE ||
194 unlock_state == STATE_BACKSPACE_ACTIVE) {
195 cairo_new_sub_path(ctx);
196 double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
197 cairo_arc(ctx, resolution[0] / 2 /* x */, resolution[1] / 2 /* y */,
198 BUTTON_RADIUS /* radius */, highlight_start,
199 highlight_start + (M_PI / 3.0));
200 if (unlock_state == STATE_KEY_ACTIVE) {
201 /* For normal keys, we use a lighter green. */
202 outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
203 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 219.0/255, 0);
204 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 219.0/255, 0);
206 /* For backspace, we use red. */
207 outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
208 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 219.0/255, 139.0/255, 0);
209 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 219.0/255, 51.0/255, 0);
211 cairo_set_source(ctx, outer_pat);
214 /* Draw two little separators for the highlighted part of the
215 * unlock indicator. */
216 cairo_set_source_rgb(ctx, 0, 0, 0);
218 (resolution[0] / 2) /* x */,
219 (resolution[1] / 2) /* y */,
220 BUTTON_RADIUS /* radius */,
221 highlight_start /* start */,
222 highlight_start + (M_PI / 128.0) /* end */);
225 (resolution[0] / 2) /* x */,
226 (resolution[1] / 2) /* y */,
227 BUTTON_RADIUS /* radius */,
228 highlight_start + (M_PI / 3.0) /* start */,
229 (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */);
234 cairo_surface_destroy(output);
241 * Calls draw_image on a new pixmap and swaps that with the current pixmap
244 void redraw_screen() {
245 xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
246 xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
247 /* XXX: Possible optimization: Only update the area in the middle of the
248 * screen instead of the whole screen. */
249 xcb_clear_area(conn, 0, win, 0, 0, screen->width_in_pixels, screen->height_in_pixels);
250 xcb_free_pixmap(conn, bg_pixmap);
255 * Hides the unlock indicator completely when there is no content in the
259 static void clear_indicator(EV_P_ ev_timer *w, int revents) {
260 if (input_position == 0) {
261 unlock_state = STATE_STARTED;
262 } else unlock_state = STATE_KEY_PRESSED;
265 ev_timer_stop(main_loop, clear_indicator_timeout);
266 free(clear_indicator_timeout);
267 clear_indicator_timeout = NULL;
271 * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
272 * after an unsuccessful authentication attempt.
275 void start_clear_indicator_timeout() {
276 if (clear_indicator_timeout) {
277 ev_timer_stop(main_loop, clear_indicator_timeout);
278 ev_timer_set(clear_indicator_timeout, 1.0, 0.);
279 ev_timer_start(main_loop, clear_indicator_timeout);
281 /* When there is no memory, we just don’t have a timeout. We cannot
282 * exit() here, since that would effectively unlock the screen. */
283 if (!(clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1)))
285 ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
286 ev_timer_start(main_loop, clear_indicator_timeout);
291 * Stops the clear_indicator timeout.
294 void stop_clear_indicator_timeout() {
295 if (clear_indicator_timeout) {
296 ev_timer_stop(main_loop, clear_indicator_timeout);
297 free(clear_indicator_timeout);
298 clear_indicator_timeout = NULL;