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