]> git.sur5r.net Git - i3/i3lock/blob - unlock_indicator.c
use libxkbcommon for input handling
[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
15 #ifndef NOLIBCAIRO
16 #include <cairo.h>
17 #include <cairo/cairo-xcb.h>
18 #endif
19
20 #include "xcb.h"
21 #include "unlock_indicator.h"
22 #include "xinerama.h"
23
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)
28
29 /*******************************************************************************
30  * Variables defined in i3lock.c.
31  ******************************************************************************/
32
33 /* The current position in the input buffer. Useful to determine if any
34  * characters of the password have already been entered or not. */
35 int input_position;
36
37 /* The ev main loop. */
38 struct ev_loop *main_loop;
39
40 /* The lock window. */
41 extern xcb_window_t win;
42
43 /* The current resolution of the X11 root window. */
44 extern uint32_t last_resolution[2];
45
46 /* Whether the unlock indicator is enabled (defaults to true). */
47 extern bool unlock_indicator;
48
49 #ifndef NOLIBCAIRO
50 /* A Cairo surface containing the specified image (-i), if any. */
51 extern cairo_surface_t *img;
52 #endif
53
54 /* Whether the image should be tiled. */
55 extern bool tile;
56 /* The background color to use (in hex). */
57 extern char color[7];
58
59 /*******************************************************************************
60  * Local variables.
61  ******************************************************************************/
62
63 static struct ev_timer *clear_indicator_timeout;
64
65 /* Cache the screen’s visual, necessary for creating a Cairo context. */
66 static xcb_visualtype_t *vistype;
67
68 /* Maintain the current unlock/PAM state to draw the appropriate unlock
69  * indicator. */
70 unlock_state_t unlock_state;
71 pam_state_t pam_state;
72
73 /*
74  * Draws global image with fill color onto a pixmap with the given
75  * resolution and returns it.
76  *
77  */
78 xcb_pixmap_t draw_image(uint32_t *resolution) {
79     xcb_pixmap_t bg_pixmap = XCB_NONE;
80
81 #ifndef NOLIBCAIRO
82     if (!vistype)
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);
90
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);
93
94     if (img) {
95         if (!tile) {
96             cairo_set_source_surface(xcb_ctx, img, 0, 0);
97             cairo_paint(xcb_ctx);
98         } else {
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]);
105             cairo_fill(xcb_ctx);
106             cairo_pattern_destroy(pattern);
107         }
108     } else {
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]);
117         cairo_fill(xcb_ctx);
118     }
119
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);
123         cairo_arc(ctx,
124                   BUTTON_CENTER /* x */,
125                   BUTTON_CENTER /* y */,
126                   BUTTON_RADIUS /* radius */,
127                   0 /* start */,
128                   2 * M_PI /* end */);
129
130         /* Use the appropriate color for the different PAM states
131          * (currently verifying, wrong password, or default) */
132         switch (pam_state) {
133             case STATE_PAM_VERIFY:
134                 cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
135                 break;
136             case STATE_PAM_WRONG:
137                 cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
138                 break;
139             default:
140                 cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
141                 break;
142         }
143         cairo_fill_preserve(ctx);
144
145         switch (pam_state) {
146             case STATE_PAM_VERIFY:
147                 cairo_set_source_rgb(ctx, 51.0/255, 0, 250.0/255);
148                 break;
149             case STATE_PAM_WRONG:
150                 cairo_set_source_rgb(ctx, 125.0/255, 51.0/255, 0);
151                 break;
152             case STATE_PAM_IDLE:
153                 cairo_set_source_rgb(ctx, 51.0/255, 125.0/255, 0);
154                 break;
155         }
156         cairo_stroke(ctx);
157
158         /* Draw an inner seperator line. */
159         cairo_set_source_rgb(ctx, 0, 0, 0);
160         cairo_set_line_width(ctx, 2.0);
161         cairo_arc(ctx,
162                   BUTTON_CENTER /* x */,
163                   BUTTON_CENTER /* y */,
164                   BUTTON_RADIUS - 5 /* radius */,
165                   0,
166                   2 * M_PI);
167         cairo_stroke(ctx);
168
169         cairo_set_line_width(ctx, 10.0);
170
171         /* Display a (centered) text of the current PAM state. */
172         char *text = NULL;
173         switch (pam_state) {
174             case STATE_PAM_VERIFY:
175                 text = "verifying…";
176                 break;
177             case STATE_PAM_WRONG:
178                 text = "wrong!";
179                 break;
180             default:
181                 break;
182         }
183
184         if (text) {
185             cairo_text_extents_t extents;
186             double x, y;
187
188             cairo_set_source_rgb(ctx, 0, 0, 0);
189             cairo_set_font_size(ctx, 28.0);
190
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);
194
195             cairo_move_to(ctx, x, y);
196             cairo_show_text(ctx, text);
197             cairo_close_path(ctx);
198         }
199
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
202          * keypress. */
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;
207             cairo_arc(ctx,
208                       BUTTON_CENTER /* x */,
209                       BUTTON_CENTER /* y */,
210                       BUTTON_RADIUS /* radius */,
211                       highlight_start,
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);
216             } else {
217                 /* For backspace, we use red. */
218                 cairo_set_source_rgb(ctx, 219.0/255, 51.0/255, 0);
219             }
220             cairo_stroke(ctx);
221
222             /* Draw two little separators for the highlighted part of the
223              * unlock indicator. */
224             cairo_set_source_rgb(ctx, 0, 0, 0);
225             cairo_arc(ctx,
226                       BUTTON_CENTER /* x */,
227                       BUTTON_CENTER /* y */,
228                       BUTTON_RADIUS /* radius */,
229                       highlight_start /* start */,
230                       highlight_start + (M_PI / 128.0) /* end */);
231             cairo_stroke(ctx);
232             cairo_arc(ctx,
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 */);
238             cairo_stroke(ctx);
239         }
240     }
241
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);
249             cairo_fill(xcb_ctx);
250         }
251     } else {
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);
259         cairo_fill(xcb_ctx);
260     }
261
262     cairo_surface_destroy(xcb_output);
263     cairo_surface_destroy(output);
264     cairo_destroy(ctx);
265     cairo_destroy(xcb_ctx);
266 #endif
267     return bg_pixmap;
268 }
269
270 /*
271  * Calls draw_image on a new pixmap and swaps that with the current pixmap
272  *
273  */
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);
281     xcb_flush(conn);
282 }
283
284 /*
285  * Hides the unlock indicator completely when there is no content in the
286  * password buffer.
287  *
288  */
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;
293     redraw_screen();
294
295     ev_timer_stop(main_loop, clear_indicator_timeout);
296     free(clear_indicator_timeout);
297     clear_indicator_timeout = NULL;
298 }
299
300 /*
301  * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
302  * after an unsuccessful authentication attempt.
303  *
304  */
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);
310     } else {
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)))
314             return;
315         ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
316         ev_timer_start(main_loop, clear_indicator_timeout);
317     }
318 }
319
320 /*
321  * Stops the clear_indicator timeout.
322  *
323  */
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;
329     }
330 }