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"
25 #define BUTTON_RADIUS 90
26 #define BUTTON_SPACE (BUTTON_RADIUS + 5)
27 #define BUTTON_CENTER (BUTTON_RADIUS + 5)
28 #define BUTTON_DIAMETER (2 * BUTTON_SPACE)
30 /*******************************************************************************
31 * Variables defined in i3lock.c.
32 ******************************************************************************/
34 /* The current position in the input buffer. Useful to determine if any
35 * characters of the password have already been entered or not. */
38 /* The ev main loop. */
39 struct ev_loop *main_loop;
41 /* The lock window. */
42 extern xcb_window_t win;
44 /* The current resolution of the X11 root window. */
45 extern uint32_t last_resolution[2];
47 /* Whether the unlock indicator is enabled (defaults to true). */
48 extern bool unlock_indicator;
51 /* A Cairo surface containing the specified image (-i), if any. */
52 extern cairo_surface_t *img;
55 /* Whether the image should be tiled. */
57 /* The background color to use (in hex). */
60 /*******************************************************************************
62 ******************************************************************************/
64 static struct ev_timer *clear_indicator_timeout;
66 /* Cache the screen’s visual, necessary for creating a Cairo context. */
67 static xcb_visualtype_t *vistype;
69 /* Maintain the current unlock/PAM state to draw the appropriate unlock
71 unlock_state_t unlock_state;
72 pam_state_t pam_state;
75 * Draws global image with fill color onto a pixmap with the given
76 * resolution and returns it.
79 xcb_pixmap_t draw_image(uint32_t *resolution) {
80 xcb_pixmap_t bg_pixmap = XCB_NONE;
84 vistype = get_root_visual_type(screen);
85 bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
86 /* Initialize cairo: Create one in-memory surface to render the unlock
87 * indicator on, create one XCB surface to actually draw (one or more,
88 * depending on the amount of screens) unlock indicators on. */
89 cairo_surface_t *output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, BUTTON_DIAMETER, BUTTON_DIAMETER);
90 cairo_t *ctx = cairo_create(output);
92 cairo_surface_t *xcb_output = cairo_xcb_surface_create(conn, bg_pixmap, vistype, resolution[0], resolution[1]);
93 cairo_t *xcb_ctx = cairo_create(xcb_output);
97 cairo_set_source_surface(xcb_ctx, img, 0, 0);
100 /* create a pattern and fill a rectangle as big as the screen */
101 cairo_pattern_t *pattern;
102 pattern = cairo_pattern_create_for_surface(img);
103 cairo_set_source(xcb_ctx, pattern);
104 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
105 cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
107 cairo_pattern_destroy(pattern);
110 char strgroups[3][3] = {{color[0], color[1], '\0'},
111 {color[2], color[3], '\0'},
112 {color[4], color[5], '\0'}};
113 uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
114 (strtol(strgroups[1], NULL, 16)),
115 (strtol(strgroups[2], NULL, 16))};
116 cairo_set_source_rgb(xcb_ctx, rgb16[0] / 255.0, rgb16[1] / 255.0, rgb16[2] / 255.0);
117 cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
121 if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
122 /* Draw a (centered) circle with transparent background. */
123 cairo_set_line_width(ctx, 10.0);
125 BUTTON_CENTER /* x */,
126 BUTTON_CENTER /* 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);
147 case STATE_PAM_VERIFY:
148 cairo_set_source_rgb(ctx, 51.0/255, 0, 250.0/255);
150 case STATE_PAM_WRONG:
151 cairo_set_source_rgb(ctx, 125.0/255, 51.0/255, 0);
154 cairo_set_source_rgb(ctx, 51.0/255, 125.0/255, 0);
159 /* Draw an inner seperator line. */
160 cairo_set_source_rgb(ctx, 0, 0, 0);
161 cairo_set_line_width(ctx, 2.0);
163 BUTTON_CENTER /* x */,
164 BUTTON_CENTER /* y */,
165 BUTTON_RADIUS - 5 /* radius */,
170 cairo_set_line_width(ctx, 10.0);
172 /* Display a (centered) text of the current PAM state. */
175 case STATE_PAM_VERIFY:
178 case STATE_PAM_WRONG:
186 cairo_text_extents_t extents;
189 cairo_set_source_rgb(ctx, 0, 0, 0);
190 cairo_set_font_size(ctx, 28.0);
192 cairo_text_extents(ctx, text, &extents);
193 x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing);
194 y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing);
196 cairo_move_to(ctx, x, y);
197 cairo_show_text(ctx, text);
198 cairo_close_path(ctx);
201 /* After the user pressed any valid key or the backspace key, we
202 * highlight a random part of the unlock indicator to confirm this
204 if (unlock_state == STATE_KEY_ACTIVE ||
205 unlock_state == STATE_BACKSPACE_ACTIVE) {
206 cairo_new_sub_path(ctx);
207 double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
209 BUTTON_CENTER /* x */,
210 BUTTON_CENTER /* y */,
211 BUTTON_RADIUS /* radius */,
213 highlight_start + (M_PI / 3.0));
214 if (unlock_state == STATE_KEY_ACTIVE) {
215 /* For normal keys, we use a lighter green. */
216 cairo_set_source_rgb(ctx, 51.0/255, 219.0/255, 0);
218 /* For backspace, we use red. */
219 cairo_set_source_rgb(ctx, 219.0/255, 51.0/255, 0);
223 /* Draw two little separators for the highlighted part of the
224 * unlock indicator. */
225 cairo_set_source_rgb(ctx, 0, 0, 0);
227 BUTTON_CENTER /* x */,
228 BUTTON_CENTER /* y */,
229 BUTTON_RADIUS /* radius */,
230 highlight_start /* start */,
231 highlight_start + (M_PI / 128.0) /* end */);
234 BUTTON_CENTER /* x */,
235 BUTTON_CENTER /* y */,
236 BUTTON_RADIUS /* radius */,
237 highlight_start + (M_PI / 3.0) /* start */,
238 (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */);
243 if (xr_screens > 0) {
244 /* Composite the unlock indicator in the middle of each screen. */
245 for (int screen = 0; screen < xr_screens; screen++) {
246 int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (BUTTON_DIAMETER / 2)));
247 int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (BUTTON_DIAMETER / 2)));
248 cairo_set_source_surface(xcb_ctx, output, x, y);
249 cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER);
253 /* We have no information about the screen sizes/positions, so we just
254 * place the unlock indicator in the middle of the X root window and
255 * hope for the best. */
256 int x = (last_resolution[0] / 2);
257 int y = (last_resolution[1] / 2);
258 cairo_set_source_surface(xcb_ctx, output, x, y);
259 cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER);
263 cairo_surface_destroy(xcb_output);
264 cairo_surface_destroy(output);
266 cairo_destroy(xcb_ctx);
272 * Calls draw_image on a new pixmap and swaps that with the current pixmap
275 void redraw_screen() {
276 xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
277 xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
278 /* XXX: Possible optimization: Only update the area in the middle of the
279 * screen instead of the whole screen. */
280 xcb_clear_area(conn, 0, win, 0, 0, screen->width_in_pixels, screen->height_in_pixels);
281 xcb_free_pixmap(conn, bg_pixmap);
286 * Hides the unlock indicator completely when there is no content in the
290 static void clear_indicator(EV_P_ ev_timer *w, int revents) {
291 if (input_position == 0) {
292 unlock_state = STATE_STARTED;
293 } else unlock_state = STATE_KEY_PRESSED;
296 ev_timer_stop(main_loop, clear_indicator_timeout);
297 free(clear_indicator_timeout);
298 clear_indicator_timeout = NULL;
302 * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
303 * after an unsuccessful authentication attempt.
306 void start_clear_indicator_timeout() {
307 if (clear_indicator_timeout) {
308 ev_timer_stop(main_loop, clear_indicator_timeout);
309 ev_timer_set(clear_indicator_timeout, 1.0, 0.);
310 ev_timer_start(main_loop, clear_indicator_timeout);
312 /* When there is no memory, we just don’t have a timeout. We cannot
313 * exit() here, since that would effectively unlock the screen. */
314 if (!(clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1)))
316 ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
317 ev_timer_start(main_loop, clear_indicator_timeout);
322 * Stops the clear_indicator timeout.
325 void stop_clear_indicator_timeout() {
326 if (clear_indicator_timeout) {
327 ev_timer_stop(main_loop, clear_indicator_timeout);
328 free(clear_indicator_timeout);
329 clear_indicator_timeout = NULL;