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