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