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