]> git.sur5r.net Git - i3/i3lock/blob - unlock_indicator.c
remove support for NOLIBCAIRO, cairo-xcb is widespread by now
[i3/i3lock] / unlock_indicator.c
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 <ev.h>
14 #include <cairo.h>
15 #include <cairo/cairo-xcb.h>
16
17 #include "xcb.h"
18 #include "unlock_indicator.h"
19 #include "xinerama.h"
20
21 #define BUTTON_RADIUS 90
22 #define BUTTON_SPACE (BUTTON_RADIUS + 5)
23 #define BUTTON_CENTER (BUTTON_RADIUS + 5)
24 #define BUTTON_DIAMETER (2 * BUTTON_SPACE)
25
26 /*******************************************************************************
27  * Variables defined in i3lock.c.
28  ******************************************************************************/
29
30 /* The current position in the input buffer. Useful to determine if any
31  * characters of the password have already been entered or not. */
32 int input_position;
33
34 /* The ev main loop. */
35 struct ev_loop *main_loop;
36
37 /* The lock window. */
38 extern xcb_window_t win;
39
40 /* The current resolution of the X11 root window. */
41 extern uint32_t last_resolution[2];
42
43 /* Whether the unlock indicator is enabled (defaults to true). */
44 extern bool unlock_indicator;
45
46 /* A Cairo surface containing the specified image (-i), if any. */
47 extern cairo_surface_t *img;
48
49 /* Whether the image should be tiled. */
50 extern bool tile;
51 /* The background color to use (in hex). */
52 extern char color[7];
53
54 /*******************************************************************************
55  * Local variables.
56  ******************************************************************************/
57
58 static struct ev_timer *clear_indicator_timeout;
59
60 /* Cache the screen’s visual, necessary for creating a Cairo context. */
61 static xcb_visualtype_t *vistype;
62
63 /* Maintain the current unlock/PAM state to draw the appropriate unlock
64  * indicator. */
65 unlock_state_t unlock_state;
66 pam_state_t pam_state;
67
68 /*
69  * Draws global image with fill color onto a pixmap with the given
70  * resolution and returns it.
71  *
72  */
73 xcb_pixmap_t draw_image(uint32_t *resolution) {
74     xcb_pixmap_t bg_pixmap = XCB_NONE;
75
76     if (!vistype)
77         vistype = get_root_visual_type(screen);
78     bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
79     /* Initialize cairo: Create one in-memory surface to render the unlock
80      * indicator on, create one XCB surface to actually draw (one or more,
81      * depending on the amount of screens) unlock indicators on. */
82     cairo_surface_t *output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, BUTTON_DIAMETER, BUTTON_DIAMETER);
83     cairo_t *ctx = cairo_create(output);
84
85     cairo_surface_t *xcb_output = cairo_xcb_surface_create(conn, bg_pixmap, vistype, resolution[0], resolution[1]);
86     cairo_t *xcb_ctx = cairo_create(xcb_output);
87
88     if (img) {
89         if (!tile) {
90             cairo_set_source_surface(xcb_ctx, img, 0, 0);
91             cairo_paint(xcb_ctx);
92         } else {
93             /* create a pattern and fill a rectangle as big as the screen */
94             cairo_pattern_t *pattern;
95             pattern = cairo_pattern_create_for_surface(img);
96             cairo_set_source(xcb_ctx, pattern);
97             cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
98             cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
99             cairo_fill(xcb_ctx);
100             cairo_pattern_destroy(pattern);
101         }
102     } else {
103         char strgroups[3][3] = {{color[0], color[1], '\0'},
104                                 {color[2], color[3], '\0'},
105                                 {color[4], color[5], '\0'}};
106         uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
107                              (strtol(strgroups[1], NULL, 16)),
108                              (strtol(strgroups[2], NULL, 16))};
109         cairo_set_source_rgb(xcb_ctx, rgb16[0] / 255.0, rgb16[1] / 255.0, rgb16[2] / 255.0);
110         cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
111         cairo_fill(xcb_ctx);
112     }
113
114     if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
115         /* Draw a (centered) circle with transparent background. */
116         cairo_set_line_width(ctx, 10.0);
117         cairo_arc(ctx,
118                   BUTTON_CENTER /* x */,
119                   BUTTON_CENTER /* y */,
120                   BUTTON_RADIUS /* radius */,
121                   0 /* start */,
122                   2 * M_PI /* end */);
123
124         /* Use the appropriate color for the different PAM states
125          * (currently verifying, wrong password, or default) */
126         switch (pam_state) {
127             case STATE_PAM_VERIFY:
128                 cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
129                 break;
130             case STATE_PAM_WRONG:
131                 cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
132                 break;
133             default:
134                 cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
135                 break;
136         }
137         cairo_fill_preserve(ctx);
138
139         switch (pam_state) {
140             case STATE_PAM_VERIFY:
141                 cairo_set_source_rgb(ctx, 51.0/255, 0, 250.0/255);
142                 break;
143             case STATE_PAM_WRONG:
144                 cairo_set_source_rgb(ctx, 125.0/255, 51.0/255, 0);
145                 break;
146             case STATE_PAM_IDLE:
147                 cairo_set_source_rgb(ctx, 51.0/255, 125.0/255, 0);
148                 break;
149         }
150         cairo_stroke(ctx);
151
152         /* Draw an inner seperator line. */
153         cairo_set_source_rgb(ctx, 0, 0, 0);
154         cairo_set_line_width(ctx, 2.0);
155         cairo_arc(ctx,
156                   BUTTON_CENTER /* x */,
157                   BUTTON_CENTER /* y */,
158                   BUTTON_RADIUS - 5 /* radius */,
159                   0,
160                   2 * M_PI);
161         cairo_stroke(ctx);
162
163         cairo_set_line_width(ctx, 10.0);
164
165         /* Display a (centered) text of the current PAM state. */
166         char *text = NULL;
167         switch (pam_state) {
168             case STATE_PAM_VERIFY:
169                 text = "verifying…";
170                 break;
171             case STATE_PAM_WRONG:
172                 text = "wrong!";
173                 break;
174             default:
175                 break;
176         }
177
178         if (text) {
179             cairo_text_extents_t extents;
180             double x, y;
181
182             cairo_set_source_rgb(ctx, 0, 0, 0);
183             cairo_set_font_size(ctx, 28.0);
184
185             cairo_text_extents(ctx, text, &extents);
186             x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing);
187             y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing);
188
189             cairo_move_to(ctx, x, y);
190             cairo_show_text(ctx, text);
191             cairo_close_path(ctx);
192         }
193
194         /* After the user pressed any valid key or the backspace key, we
195          * highlight a random part of the unlock indicator to confirm this
196          * keypress. */
197         if (unlock_state == STATE_KEY_ACTIVE ||
198             unlock_state == STATE_BACKSPACE_ACTIVE) {
199             cairo_new_sub_path(ctx);
200             double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
201             cairo_arc(ctx,
202                       BUTTON_CENTER /* x */,
203                       BUTTON_CENTER /* y */,
204                       BUTTON_RADIUS /* radius */,
205                       highlight_start,
206                       highlight_start + (M_PI / 3.0));
207             if (unlock_state == STATE_KEY_ACTIVE) {
208                 /* For normal keys, we use a lighter green. */
209                 cairo_set_source_rgb(ctx, 51.0/255, 219.0/255, 0);
210             } else {
211                 /* For backspace, we use red. */
212                 cairo_set_source_rgb(ctx, 219.0/255, 51.0/255, 0);
213             }
214             cairo_stroke(ctx);
215
216             /* Draw two little separators for the highlighted part of the
217              * unlock indicator. */
218             cairo_set_source_rgb(ctx, 0, 0, 0);
219             cairo_arc(ctx,
220                       BUTTON_CENTER /* x */,
221                       BUTTON_CENTER /* y */,
222                       BUTTON_RADIUS /* radius */,
223                       highlight_start /* start */,
224                       highlight_start + (M_PI / 128.0) /* end */);
225             cairo_stroke(ctx);
226             cairo_arc(ctx,
227                       BUTTON_CENTER /* x */,
228                       BUTTON_CENTER /* y */,
229                       BUTTON_RADIUS /* radius */,
230                       highlight_start + (M_PI / 3.0) /* start */,
231                       (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */);
232             cairo_stroke(ctx);
233         }
234     }
235
236     if (xr_screens > 0) {
237         /* Composite the unlock indicator in the middle of each screen. */
238         for (int screen = 0; screen < xr_screens; screen++) {
239             int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (BUTTON_DIAMETER / 2)));
240             int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (BUTTON_DIAMETER / 2)));
241             cairo_set_source_surface(xcb_ctx, output, x, y);
242             cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER);
243             cairo_fill(xcb_ctx);
244         }
245     } else {
246         /* We have no information about the screen sizes/positions, so we just
247          * place the unlock indicator in the middle of the X root window and
248          * hope for the best. */
249         int x = (last_resolution[0] / 2);
250         int y = (last_resolution[1] / 2);
251         cairo_set_source_surface(xcb_ctx, output, x, y);
252         cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER);
253         cairo_fill(xcb_ctx);
254     }
255
256     cairo_surface_destroy(xcb_output);
257     cairo_surface_destroy(output);
258     cairo_destroy(ctx);
259     cairo_destroy(xcb_ctx);
260     return bg_pixmap;
261 }
262
263 /*
264  * Calls draw_image on a new pixmap and swaps that with the current pixmap
265  *
266  */
267 void redraw_screen(void) {
268     xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
269     xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
270     /* XXX: Possible optimization: Only update the area in the middle of the
271      * screen instead of the whole screen. */
272     xcb_clear_area(conn, 0, win, 0, 0, last_resolution[0], last_resolution[1]);
273     xcb_free_pixmap(conn, bg_pixmap);
274     xcb_flush(conn);
275 }
276
277 /*
278  * Hides the unlock indicator completely when there is no content in the
279  * password buffer.
280  *
281  */
282 static void clear_indicator(EV_P_ ev_timer *w, int revents) {
283     if (input_position == 0) {
284         unlock_state = STATE_STARTED;
285     } else unlock_state = STATE_KEY_PRESSED;
286     redraw_screen();
287
288     ev_timer_stop(main_loop, clear_indicator_timeout);
289     free(clear_indicator_timeout);
290     clear_indicator_timeout = NULL;
291 }
292
293 /*
294  * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
295  * after an unsuccessful authentication attempt.
296  *
297  */
298 void start_clear_indicator_timeout(void) {
299     if (clear_indicator_timeout) {
300         ev_timer_stop(main_loop, clear_indicator_timeout);
301         ev_timer_set(clear_indicator_timeout, 1.0, 0.);
302         ev_timer_start(main_loop, clear_indicator_timeout);
303     } else {
304         /* When there is no memory, we just don’t have a timeout. We cannot
305          * exit() here, since that would effectively unlock the screen. */
306         if (!(clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1)))
307             return;
308         ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
309         ev_timer_start(main_loop, clear_indicator_timeout);
310     }
311 }
312
313 /*
314  * Stops the clear_indicator timeout.
315  *
316  */
317 void stop_clear_indicator_timeout(void) {
318     if (clear_indicator_timeout) {
319         ev_timer_stop(main_loop, clear_indicator_timeout);
320         free(clear_indicator_timeout);
321         clear_indicator_timeout = NULL;
322     }
323 }