]> git.sur5r.net Git - i3/i3lock/blob - unlock_indicator.c
Properly free timeouts, move one of the timeouts to unlock_indicator.c
[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 <xcb/xcb_keysyms.h>
14 #include <ev.h>
15
16 #ifndef NOLIBCAIRO
17 #include <cairo.h>
18 #include <cairo/cairo-xcb.h>
19 #endif
20
21 #include "xcb.h"
22 #include "unlock_indicator.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 (5 * 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 /* A Cairo surface containing the specified image (-i), if any. */
50 extern cairo_surface_t *img;
51 /* Whether the image should be tiled. */
52 extern bool tile;
53 /* The background color to use (in hex). */
54 extern char color[7];
55
56 /*******************************************************************************
57  * Local variables.
58  ******************************************************************************/
59
60 static struct ev_timer *clear_indicator_timeout;
61
62 /* Cache the screen’s visual, necessary for creating a Cairo context. */
63 static xcb_visualtype_t *vistype;
64
65 /* Maintain the current unlock/PAM state to draw the appropriate unlock
66  * indicator. */
67 unlock_state_t unlock_state;
68 pam_state_t pam_state;
69
70 /*
71  * Draws global image with fill color onto a pixmap with the given
72  * resolution and returns it.
73  *
74  */
75 xcb_pixmap_t draw_image(uint32_t *resolution) {
76     xcb_pixmap_t bg_pixmap = XCB_NONE;
77
78 #ifndef NOLIBCAIRO
79     if (!vistype)
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);
87     if (img) {
88         if (!tile) {
89             cairo_set_source_surface(ctx, img, 0, 0);
90             cairo_paint(ctx);
91         } else {
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]);
98             cairo_fill(ctx);
99             cairo_pattern_destroy(pattern);
100         }
101     }
102
103     if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
104         cairo_pattern_t *outer_pat = NULL;
105
106         outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
107         switch (pam_state) {
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);
111                 break;
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);
115                 break;
116             case STATE_PAM_IDLE:
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);
119                 break;
120         }
121
122         /* Draw a (centered) circle with transparent background. */
123         cairo_set_line_width(ctx, 10.0);
124         cairo_arc(ctx,
125                   (resolution[0] / 2) /* x */,
126                   (resolution[1] / 2) /* y */,
127                   BUTTON_RADIUS /* radius */,
128                   0 /* start */,
129                   2 * M_PI /* end */);
130
131         /* Use the appropriate color for the different PAM states
132          * (currently verifying, wrong password, or default) */
133         switch (pam_state) {
134             case STATE_PAM_VERIFY:
135                 cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
136                 break;
137             case STATE_PAM_WRONG:
138                 cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
139                 break;
140             default:
141                 cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
142                 break;
143         }
144         cairo_fill_preserve(ctx);
145         cairo_set_source(ctx, outer_pat);
146         cairo_stroke(ctx);
147
148         /* Draw an inner seperator line. */
149         cairo_set_source_rgb(ctx, 0, 0, 0);
150         cairo_set_line_width(ctx, 2.0);
151         cairo_arc(ctx,
152                   (resolution[0] / 2) /* x */,
153                   (resolution[1] / 2) /* y */,
154                   BUTTON_RADIUS - 5 /* radius */,
155                   0,
156                   2 * M_PI);
157         cairo_stroke(ctx);
158
159         cairo_set_line_width(ctx, 10.0);
160
161         /* Display a (centered) text of the current PAM state. */
162         char *text = NULL;
163         switch (pam_state) {
164             case STATE_PAM_VERIFY:
165                 text = "verifying…";
166                 break;
167             case STATE_PAM_WRONG:
168                 text = "wrong!";
169                 break;
170             default:
171                 break;
172         }
173
174         if (text) {
175             cairo_text_extents_t extents;
176             double x, y;
177
178             cairo_set_source_rgb(ctx, 0, 0, 0);
179             cairo_set_font_size(ctx, 28.0);
180
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);
184
185             cairo_move_to(ctx, x, y);
186             cairo_show_text(ctx, text);
187             cairo_close_path(ctx);
188         }
189
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
192          * keypress. */
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);
205             } else {
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);
210             }
211             cairo_set_source(ctx, outer_pat);
212             cairo_stroke(ctx);
213
214             /* Draw two little separators for the highlighted part of the
215              * unlock indicator. */
216             cairo_set_source_rgb(ctx, 0, 0, 0);
217             cairo_arc(ctx,
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 */);
223             cairo_stroke(ctx);
224             cairo_arc(ctx,
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 */);
230             cairo_stroke(ctx);
231         }
232     }
233
234     cairo_surface_destroy(output);
235     cairo_destroy(ctx);
236 #endif
237     return bg_pixmap;
238 }
239
240 /*
241  * Calls draw_image on a new pixmap and swaps that with the current pixmap
242  *
243  */
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);
251     xcb_flush(conn);
252 }
253
254 /*
255  * Hides the unlock indicator completely when there is no content in the
256  * password buffer.
257  *
258  */
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;
263     redraw_screen();
264
265     ev_timer_stop(main_loop, clear_indicator_timeout);
266     free(clear_indicator_timeout);
267     clear_indicator_timeout = NULL;
268 }
269
270 /*
271  * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
272  * after an unsuccessful authentication attempt.
273  *
274  */
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);
280     } else {
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)))
284             return;
285         ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
286         ev_timer_start(main_loop, clear_indicator_timeout);
287     }
288 }
289
290 /*
291  * Stops the clear_indicator timeout.
292  *
293  */
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;
299     }
300 }