]> git.sur5r.net Git - i3/i3lock/blob - unlock_indicator.c
display error when backspace is pressed without any input (#172)
[i3/i3lock] / unlock_indicator.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * © 2010 Michael Stapelberg
5  *
6  * See LICENSE for licensing information
7  *
8  */
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <math.h>
14 #include <xcb/xcb.h>
15 #include <ev.h>
16 #include <cairo.h>
17 #include <cairo/cairo-xcb.h>
18
19 #include "i3lock.h"
20 #include "xcb.h"
21 #include "unlock_indicator.h"
22 #include "randr.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 extern bool debug_mode;
34
35 /* The current position in the input buffer. Useful to determine if any
36  * characters of the password have already been entered or not. */
37 int input_position;
38
39 /* The lock window. */
40 extern xcb_window_t win;
41
42 /* The current resolution of the X11 root window. */
43 extern uint32_t last_resolution[2];
44
45 /* Whether the unlock indicator is enabled (defaults to true). */
46 extern bool unlock_indicator;
47
48 /* List of pressed modifiers, or NULL if none are pressed. */
49 extern char *modifier_string;
50
51 /* A Cairo surface containing the specified image (-i), if any. */
52 extern cairo_surface_t *img;
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 /* Whether the failed attempts should be displayed. */
60 extern bool show_failed_attempts;
61 /* Number of failed unlock attempts. */
62 extern int failed_attempts;
63
64 /*******************************************************************************
65  * Variables defined in xcb.c.
66  ******************************************************************************/
67
68 /* The root screen, to determine the DPI. */
69 extern xcb_screen_t *screen;
70
71 /*******************************************************************************
72  * Local variables.
73  ******************************************************************************/
74
75 /* Cache the screen’s visual, necessary for creating a Cairo context. */
76 static xcb_visualtype_t *vistype;
77
78 /* Maintain the current unlock/PAM state to draw the appropriate unlock
79  * indicator. */
80 unlock_state_t unlock_state;
81 auth_state_t auth_state;
82
83 /*
84  * Returns the scaling factor of the current screen. E.g., on a 227 DPI MacBook
85  * Pro 13" Retina screen, the scaling factor is 227/96 = 2.36.
86  *
87  */
88 static double scaling_factor(void) {
89     const int dpi = (double)screen->height_in_pixels * 25.4 /
90                     (double)screen->height_in_millimeters;
91     return (dpi / 96.0);
92 }
93
94 /*
95  * Draws global image with fill color onto a pixmap with the given
96  * resolution and returns it.
97  *
98  */
99 xcb_pixmap_t draw_image(uint32_t *resolution) {
100     xcb_pixmap_t bg_pixmap = XCB_NONE;
101     int button_diameter_physical = ceil(scaling_factor() * BUTTON_DIAMETER);
102     DEBUG("scaling_factor is %.f, physical diameter is %d px\n",
103           scaling_factor(), button_diameter_physical);
104
105     if (!vistype)
106         vistype = get_root_visual_type(screen);
107     bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
108     /* Initialize cairo: Create one in-memory surface to render the unlock
109      * indicator on, create one XCB surface to actually draw (one or more,
110      * depending on the amount of screens) unlock indicators on. */
111     cairo_surface_t *output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, button_diameter_physical, button_diameter_physical);
112     cairo_t *ctx = cairo_create(output);
113
114     cairo_surface_t *xcb_output = cairo_xcb_surface_create(conn, bg_pixmap, vistype, resolution[0], resolution[1]);
115     cairo_t *xcb_ctx = cairo_create(xcb_output);
116
117     if (img) {
118         if (!tile) {
119             cairo_set_source_surface(xcb_ctx, img, 0, 0);
120             cairo_paint(xcb_ctx);
121         } else {
122             /* create a pattern and fill a rectangle as big as the screen */
123             cairo_pattern_t *pattern;
124             pattern = cairo_pattern_create_for_surface(img);
125             cairo_set_source(xcb_ctx, pattern);
126             cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
127             cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
128             cairo_fill(xcb_ctx);
129             cairo_pattern_destroy(pattern);
130         }
131     } else {
132         char strgroups[3][3] = {{color[0], color[1], '\0'},
133                                 {color[2], color[3], '\0'},
134                                 {color[4], color[5], '\0'}};
135         uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
136                              (strtol(strgroups[1], NULL, 16)),
137                              (strtol(strgroups[2], NULL, 16))};
138         cairo_set_source_rgb(xcb_ctx, rgb16[0] / 255.0, rgb16[1] / 255.0, rgb16[2] / 255.0);
139         cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
140         cairo_fill(xcb_ctx);
141     }
142
143     if (unlock_indicator &&
144         (unlock_state >= STATE_KEY_PRESSED || auth_state > STATE_AUTH_IDLE)) {
145         cairo_scale(ctx, scaling_factor(), scaling_factor());
146         /* Draw a (centered) circle with transparent background. */
147         cairo_set_line_width(ctx, 10.0);
148         cairo_arc(ctx,
149                   BUTTON_CENTER /* x */,
150                   BUTTON_CENTER /* y */,
151                   BUTTON_RADIUS /* radius */,
152                   0 /* start */,
153                   2 * M_PI /* end */);
154
155         /* Use the appropriate color for the different PAM states
156          * (currently verifying, wrong password, or default) */
157         switch (auth_state) {
158             case STATE_AUTH_VERIFY:
159             case STATE_AUTH_LOCK:
160                 cairo_set_source_rgba(ctx, 0, 114.0 / 255, 255.0 / 255, 0.75);
161                 break;
162             case STATE_AUTH_WRONG:
163             case STATE_I3LOCK_LOCK_FAILED:
164                 cairo_set_source_rgba(ctx, 250.0 / 255, 0, 0, 0.75);
165                 break;
166             default:
167                 if (unlock_state == STATE_NOTHING_TO_DELETE) {
168                     cairo_set_source_rgba(ctx, 250.0 / 255, 0, 0, 0.75);
169                     break;
170                 }
171                 cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
172                 break;
173         }
174         cairo_fill_preserve(ctx);
175
176         switch (auth_state) {
177             case STATE_AUTH_VERIFY:
178             case STATE_AUTH_LOCK:
179                 cairo_set_source_rgb(ctx, 51.0 / 255, 0, 250.0 / 255);
180                 break;
181             case STATE_AUTH_WRONG:
182             case STATE_I3LOCK_LOCK_FAILED:
183                 cairo_set_source_rgb(ctx, 125.0 / 255, 51.0 / 255, 0);
184                 break;
185             case STATE_AUTH_IDLE:
186                 if (unlock_state == STATE_NOTHING_TO_DELETE) {
187                     cairo_set_source_rgb(ctx, 125.0 / 255, 51.0 / 255, 0);
188                     break;
189                 }
190
191                 cairo_set_source_rgb(ctx, 51.0 / 255, 125.0 / 255, 0);
192                 break;
193         }
194         cairo_stroke(ctx);
195
196         /* Draw an inner seperator line. */
197         cairo_set_source_rgb(ctx, 0, 0, 0);
198         cairo_set_line_width(ctx, 2.0);
199         cairo_arc(ctx,
200                   BUTTON_CENTER /* x */,
201                   BUTTON_CENTER /* y */,
202                   BUTTON_RADIUS - 5 /* radius */,
203                   0,
204                   2 * M_PI);
205         cairo_stroke(ctx);
206
207         cairo_set_line_width(ctx, 10.0);
208
209         /* Display a (centered) text of the current PAM state. */
210         char *text = NULL;
211         /* We don't want to show more than a 3-digit number. */
212         char buf[4];
213
214         cairo_set_source_rgb(ctx, 0, 0, 0);
215         cairo_select_font_face(ctx, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
216         cairo_set_font_size(ctx, 28.0);
217         switch (auth_state) {
218             case STATE_AUTH_VERIFY:
219                 text = "verifying…";
220                 break;
221             case STATE_AUTH_LOCK:
222                 text = "locking…";
223                 break;
224             case STATE_AUTH_WRONG:
225                 text = "wrong!";
226                 break;
227             case STATE_I3LOCK_LOCK_FAILED:
228                 text = "lock failed!";
229                 break;
230             default:
231                 if (unlock_state == STATE_NOTHING_TO_DELETE) {
232                     text = "no input";
233                 }
234                 if (show_failed_attempts && failed_attempts > 0) {
235                     if (failed_attempts > 999) {
236                         text = "> 999";
237                     } else {
238                         snprintf(buf, sizeof(buf), "%d", failed_attempts);
239                         text = buf;
240                     }
241                     cairo_set_source_rgb(ctx, 1, 0, 0);
242                     cairo_set_font_size(ctx, 32.0);
243                 }
244                 break;
245         }
246
247         if (text) {
248             cairo_text_extents_t extents;
249             double x, y;
250
251             cairo_text_extents(ctx, text, &extents);
252             x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing);
253             y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing);
254
255             cairo_move_to(ctx, x, y);
256             cairo_show_text(ctx, text);
257             cairo_close_path(ctx);
258         }
259
260         if (auth_state == STATE_AUTH_WRONG && (modifier_string != NULL)) {
261             cairo_text_extents_t extents;
262             double x, y;
263
264             cairo_set_font_size(ctx, 14.0);
265
266             cairo_text_extents(ctx, modifier_string, &extents);
267             x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing);
268             y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing) + 28.0;
269
270             cairo_move_to(ctx, x, y);
271             cairo_show_text(ctx, modifier_string);
272             cairo_close_path(ctx);
273         }
274
275         /* After the user pressed any valid key or the backspace key, we
276          * highlight a random part of the unlock indicator to confirm this
277          * keypress. */
278         if (unlock_state == STATE_KEY_ACTIVE ||
279             unlock_state == STATE_BACKSPACE_ACTIVE) {
280             cairo_new_sub_path(ctx);
281             double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
282             cairo_arc(ctx,
283                       BUTTON_CENTER /* x */,
284                       BUTTON_CENTER /* y */,
285                       BUTTON_RADIUS /* radius */,
286                       highlight_start,
287                       highlight_start + (M_PI / 3.0));
288             if (unlock_state == STATE_KEY_ACTIVE) {
289                 /* For normal keys, we use a lighter green. */
290                 cairo_set_source_rgb(ctx, 51.0 / 255, 219.0 / 255, 0);
291             } else {
292                 /* For backspace, we use red. */
293                 cairo_set_source_rgb(ctx, 219.0 / 255, 51.0 / 255, 0);
294             }
295             cairo_stroke(ctx);
296
297             /* Draw two little separators for the highlighted part of the
298              * unlock indicator. */
299             cairo_set_source_rgb(ctx, 0, 0, 0);
300             cairo_arc(ctx,
301                       BUTTON_CENTER /* x */,
302                       BUTTON_CENTER /* y */,
303                       BUTTON_RADIUS /* radius */,
304                       highlight_start /* start */,
305                       highlight_start + (M_PI / 128.0) /* end */);
306             cairo_stroke(ctx);
307             cairo_arc(ctx,
308                       BUTTON_CENTER /* x */,
309                       BUTTON_CENTER /* y */,
310                       BUTTON_RADIUS /* radius */,
311                       (highlight_start + (M_PI / 3.0)) - (M_PI / 128.0) /* start */,
312                       highlight_start + (M_PI / 3.0) /* end */);
313             cairo_stroke(ctx);
314         }
315     }
316
317     if (xr_screens > 0) {
318         /* Composite the unlock indicator in the middle of each screen. */
319         for (int screen = 0; screen < xr_screens; screen++) {
320             int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (button_diameter_physical / 2)));
321             int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (button_diameter_physical / 2)));
322             cairo_set_source_surface(xcb_ctx, output, x, y);
323             cairo_rectangle(xcb_ctx, x, y, button_diameter_physical, button_diameter_physical);
324             cairo_fill(xcb_ctx);
325         }
326     } else {
327         /* We have no information about the screen sizes/positions, so we just
328          * place the unlock indicator in the middle of the X root window and
329          * hope for the best. */
330         int x = (last_resolution[0] / 2) - (button_diameter_physical / 2);
331         int y = (last_resolution[1] / 2) - (button_diameter_physical / 2);
332         cairo_set_source_surface(xcb_ctx, output, x, y);
333         cairo_rectangle(xcb_ctx, x, y, button_diameter_physical, button_diameter_physical);
334         cairo_fill(xcb_ctx);
335     }
336
337     cairo_surface_destroy(xcb_output);
338     cairo_surface_destroy(output);
339     cairo_destroy(ctx);
340     cairo_destroy(xcb_ctx);
341     return bg_pixmap;
342 }
343
344 /*
345  * Calls draw_image on a new pixmap and swaps that with the current pixmap
346  *
347  */
348 void redraw_screen(void) {
349     DEBUG("redraw_screen(unlock_state = %d, auth_state = %d)\n", unlock_state, auth_state);
350     xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
351     xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){bg_pixmap});
352     /* XXX: Possible optimization: Only update the area in the middle of the
353      * screen instead of the whole screen. */
354     xcb_clear_area(conn, 0, win, 0, 0, last_resolution[0], last_resolution[1]);
355     xcb_free_pixmap(conn, bg_pixmap);
356     xcb_flush(conn);
357 }
358
359 /*
360  * Hides the unlock indicator completely when there is no content in the
361  * password buffer.
362  *
363  */
364 void clear_indicator(void) {
365     if (input_position == 0) {
366         unlock_state = STATE_STARTED;
367     } else
368         unlock_state = STATE_KEY_PRESSED;
369     redraw_screen();
370 }