2 * vim:ts=4:sw=4:expandtab
4 * © 2010-2012 Michael Stapelberg
6 * See LICENSE for licensing information
17 #include <cairo/cairo-xcb.h>
21 #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 (2 * 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;
50 /* A Cairo surface containing the specified image (-i), if any. */
51 extern cairo_surface_t *img;
54 /* Whether the image should be tiled. */
56 /* The background color to use (in hex). */
59 /*******************************************************************************
61 ******************************************************************************/
63 static struct ev_timer *clear_indicator_timeout;
65 /* Cache the screen’s visual, necessary for creating a Cairo context. */
66 static xcb_visualtype_t *vistype;
68 /* Maintain the current unlock/PAM state to draw the appropriate unlock
70 unlock_state_t unlock_state;
71 pam_state_t pam_state;
74 * Draws global image with fill color onto a pixmap with the given
75 * resolution and returns it.
78 xcb_pixmap_t draw_image(uint32_t *resolution) {
79 xcb_pixmap_t bg_pixmap = XCB_NONE;
83 vistype = get_root_visual_type(screen);
84 bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
85 /* Initialize cairo: Create one in-memory surface to render the unlock
86 * indicator on, create one XCB surface to actually draw (one or more,
87 * depending on the amount of screens) unlock indicators on. */
88 cairo_surface_t *output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, BUTTON_DIAMETER, BUTTON_DIAMETER);
89 cairo_t *ctx = cairo_create(output);
91 cairo_surface_t *xcb_output = cairo_xcb_surface_create(conn, bg_pixmap, vistype, resolution[0], resolution[1]);
92 cairo_t *xcb_ctx = cairo_create(xcb_output);
96 cairo_set_source_surface(xcb_ctx, img, 0, 0);
99 /* create a pattern and fill a rectangle as big as the screen */
100 cairo_pattern_t *pattern;
101 pattern = cairo_pattern_create_for_surface(img);
102 cairo_set_source(xcb_ctx, pattern);
103 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
104 cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
106 cairo_pattern_destroy(pattern);
109 char strgroups[3][3] = {{color[0], color[1], '\0'},
110 {color[2], color[3], '\0'},
111 {color[4], color[5], '\0'}};
112 uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
113 (strtol(strgroups[1], NULL, 16)),
114 (strtol(strgroups[2], NULL, 16))};
115 cairo_set_source_rgb(xcb_ctx, rgb16[0] / 255.0, rgb16[1] / 255.0, rgb16[2] / 255.0);
116 cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
120 if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
121 /* Draw a (centered) circle with transparent background. */
122 cairo_set_line_width(ctx, 10.0);
124 BUTTON_CENTER /* x */,
125 BUTTON_CENTER /* y */,
126 BUTTON_RADIUS /* radius */,
130 /* Use the appropriate color for the different PAM states
131 * (currently verifying, wrong password, or default) */
133 case STATE_PAM_VERIFY:
134 cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
136 case STATE_PAM_WRONG:
137 cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
140 cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
143 cairo_fill_preserve(ctx);
146 case STATE_PAM_VERIFY:
147 cairo_set_source_rgb(ctx, 51.0/255, 0, 250.0/255);
149 case STATE_PAM_WRONG:
150 cairo_set_source_rgb(ctx, 125.0/255, 51.0/255, 0);
153 cairo_set_source_rgb(ctx, 51.0/255, 125.0/255, 0);
158 /* Draw an inner seperator line. */
159 cairo_set_source_rgb(ctx, 0, 0, 0);
160 cairo_set_line_width(ctx, 2.0);
162 BUTTON_CENTER /* x */,
163 BUTTON_CENTER /* y */,
164 BUTTON_RADIUS - 5 /* radius */,
169 cairo_set_line_width(ctx, 10.0);
171 /* Display a (centered) text of the current PAM state. */
174 case STATE_PAM_VERIFY:
177 case STATE_PAM_WRONG:
185 cairo_text_extents_t extents;
188 cairo_set_source_rgb(ctx, 0, 0, 0);
189 cairo_set_font_size(ctx, 28.0);
191 cairo_text_extents(ctx, text, &extents);
192 x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing);
193 y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing);
195 cairo_move_to(ctx, x, y);
196 cairo_show_text(ctx, text);
197 cairo_close_path(ctx);
200 /* After the user pressed any valid key or the backspace key, we
201 * highlight a random part of the unlock indicator to confirm this
203 if (unlock_state == STATE_KEY_ACTIVE ||
204 unlock_state == STATE_BACKSPACE_ACTIVE) {
205 cairo_new_sub_path(ctx);
206 double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
208 BUTTON_CENTER /* x */,
209 BUTTON_CENTER /* y */,
210 BUTTON_RADIUS /* radius */,
212 highlight_start + (M_PI / 3.0));
213 if (unlock_state == STATE_KEY_ACTIVE) {
214 /* For normal keys, we use a lighter green. */
215 cairo_set_source_rgb(ctx, 51.0/255, 219.0/255, 0);
217 /* For backspace, we use red. */
218 cairo_set_source_rgb(ctx, 219.0/255, 51.0/255, 0);
222 /* Draw two little separators for the highlighted part of the
223 * unlock indicator. */
224 cairo_set_source_rgb(ctx, 0, 0, 0);
226 BUTTON_CENTER /* x */,
227 BUTTON_CENTER /* y */,
228 BUTTON_RADIUS /* radius */,
229 highlight_start /* start */,
230 highlight_start + (M_PI / 128.0) /* end */);
233 BUTTON_CENTER /* x */,
234 BUTTON_CENTER /* y */,
235 BUTTON_RADIUS /* radius */,
236 highlight_start + (M_PI / 3.0) /* start */,
237 (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */);
242 if (xr_screens > 0) {
243 /* Composite the unlock indicator in the middle of each screen. */
244 for (int screen = 0; screen < xr_screens; screen++) {
245 int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (BUTTON_DIAMETER / 2)));
246 int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (BUTTON_DIAMETER / 2)));
247 cairo_set_source_surface(xcb_ctx, output, x, y);
248 cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER);
252 /* We have no information about the screen sizes/positions, so we just
253 * place the unlock indicator in the middle of the X root window and
254 * hope for the best. */
255 int x = (last_resolution[0] / 2);
256 int y = (last_resolution[1] / 2);
257 cairo_set_source_surface(xcb_ctx, output, x, y);
258 cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER);
262 cairo_surface_destroy(xcb_output);
263 cairo_surface_destroy(output);
265 cairo_destroy(xcb_ctx);
271 * Calls draw_image on a new pixmap and swaps that with the current pixmap
274 void redraw_screen(void) {
275 xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
276 xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
277 /* XXX: Possible optimization: Only update the area in the middle of the
278 * screen instead of the whole screen. */
279 xcb_clear_area(conn, 0, win, 0, 0, last_resolution[0], last_resolution[1]);
280 xcb_free_pixmap(conn, bg_pixmap);
285 * Hides the unlock indicator completely when there is no content in the
289 static void clear_indicator(EV_P_ ev_timer *w, int revents) {
290 if (input_position == 0) {
291 unlock_state = STATE_STARTED;
292 } else unlock_state = STATE_KEY_PRESSED;
295 ev_timer_stop(main_loop, clear_indicator_timeout);
296 free(clear_indicator_timeout);
297 clear_indicator_timeout = NULL;
301 * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
302 * after an unsuccessful authentication attempt.
305 void start_clear_indicator_timeout(void) {
306 if (clear_indicator_timeout) {
307 ev_timer_stop(main_loop, clear_indicator_timeout);
308 ev_timer_set(clear_indicator_timeout, 1.0, 0.);
309 ev_timer_start(main_loop, clear_indicator_timeout);
311 /* When there is no memory, we just don’t have a timeout. We cannot
312 * exit() here, since that would effectively unlock the screen. */
313 if (!(clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1)))
315 ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
316 ev_timer_start(main_loop, clear_indicator_timeout);
321 * Stops the clear_indicator timeout.
324 void stop_clear_indicator_timeout(void) {
325 if (clear_indicator_timeout) {
326 ev_timer_stop(main_loop, clear_indicator_timeout);
327 free(clear_indicator_timeout);
328 clear_indicator_timeout = NULL;