]> git.sur5r.net Git - i3/i3lock/blob - unlock_indicator.c
Properly free the reply of xcb_get_geometry
[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
15 #ifndef NOLIBCAIRO
16 #include <cairo.h>
17 #include <cairo/cairo-xcb.h>
18 #endif
19
20 #include "xcb.h"
21 #include "unlock_indicator.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 (5 * BUTTON_SPACE)
27
28 /*******************************************************************************
29  * Variables defined in i3lock.c.
30  ******************************************************************************/
31
32 /* The lock window. */
33 extern xcb_window_t win;
34
35 /* The current resolution of the X11 root window. */
36 extern uint32_t last_resolution[2];
37
38 /* Whether the unlock indicator is enabled (defaults to true). */
39 extern bool unlock_indicator;
40
41 /* A Cairo surface containing the specified image (-i), if any. */
42 extern cairo_surface_t *img;
43 /* Whether the image should be tiled. */
44 extern bool tile;
45 /* The background color to use (in hex). */
46 extern char color[7];
47
48 /*******************************************************************************
49  * Local variables.
50  ******************************************************************************/
51
52 /* Cache the screen’s visual, necessary for creating a Cairo context. */
53 static xcb_visualtype_t *vistype;
54
55 /* Maintain the current unlock/PAM state to draw the appropriate unlock
56  * indicator. */
57 unlock_state_t unlock_state;
58 pam_state_t pam_state;
59
60 /*
61  * Draws global image with fill color onto a pixmap with the given
62  * resolution and returns it.
63  *
64  */
65 xcb_pixmap_t draw_image(uint32_t *resolution) {
66     xcb_pixmap_t bg_pixmap = XCB_NONE;
67
68 #ifndef NOLIBCAIRO
69     if (!vistype)
70         vistype = get_root_visual_type(screen);
71     bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
72     /* Initialize cairo */
73     cairo_surface_t *output;
74     output = cairo_xcb_surface_create(conn, bg_pixmap, vistype,
75              resolution[0], resolution[1]);
76     cairo_t *ctx = cairo_create(output);
77     if (img) {
78         if (!tile) {
79             cairo_set_source_surface(ctx, img, 0, 0);
80             cairo_paint(ctx);
81         } else {
82             /* create a pattern and fill a rectangle as big as the screen */
83             cairo_pattern_t *pattern;
84             pattern = cairo_pattern_create_for_surface(img);
85             cairo_set_source(ctx, pattern);
86             cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
87             cairo_rectangle(ctx, 0, 0, resolution[0], resolution[1]);
88             cairo_fill(ctx);
89             cairo_pattern_destroy(pattern);
90         }
91     }
92
93     if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
94         cairo_pattern_t *outer_pat = NULL;
95
96         outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
97         switch (pam_state) {
98             case STATE_PAM_VERIFY:
99                 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 0, 250.0/255);
100                 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 0, 250.0/255);
101                 break;
102             case STATE_PAM_WRONG:
103                 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 255.0/250, 139.0/255, 0);
104                 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 125.0/255, 51.0/255, 0);
105                 break;
106             case STATE_PAM_IDLE:
107                 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 125.0/255, 0);
108                 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 125.0/255, 0);
109                 break;
110         }
111
112         /* Draw a (centered) circle with transparent background. */
113         cairo_set_line_width(ctx, 10.0);
114         cairo_arc(ctx,
115                   (resolution[0] / 2) /* x */,
116                   (resolution[1] / 2) /* y */,
117                   BUTTON_RADIUS /* radius */,
118                   0 /* start */,
119                   2 * M_PI /* end */);
120
121         /* Use the appropriate color for the different PAM states
122          * (currently verifying, wrong password, or default) */
123         switch (pam_state) {
124             case STATE_PAM_VERIFY:
125                 cairo_set_source_rgba(ctx, 0, 114.0/255, 255.0/255, 0.75);
126                 break;
127             case STATE_PAM_WRONG:
128                 cairo_set_source_rgba(ctx, 250.0/255, 0, 0, 0.75);
129                 break;
130             default:
131                 cairo_set_source_rgba(ctx, 0, 0, 0, 0.75);
132                 break;
133         }
134         cairo_fill_preserve(ctx);
135         cairo_set_source(ctx, outer_pat);
136         cairo_stroke(ctx);
137
138         /* Draw an inner seperator line. */
139         cairo_set_source_rgb(ctx, 0, 0, 0);
140         cairo_set_line_width(ctx, 2.0);
141         cairo_arc(ctx,
142                   (resolution[0] / 2) /* x */,
143                   (resolution[1] / 2) /* y */,
144                   BUTTON_RADIUS - 5 /* radius */,
145                   0,
146                   2 * M_PI);
147         cairo_stroke(ctx);
148
149         cairo_set_line_width(ctx, 10.0);
150
151         /* Display a (centered) text of the current PAM state. */
152         char *text = NULL;
153         switch (pam_state) {
154             case STATE_PAM_VERIFY:
155                 text = "verifying…";
156                 break;
157             case STATE_PAM_WRONG:
158                 text = "wrong!";
159                 break;
160             default:
161                 break;
162         }
163
164         if (text) {
165             cairo_text_extents_t extents;
166             double x, y;
167
168             cairo_set_source_rgb(ctx, 0, 0, 0);
169             cairo_set_font_size(ctx, 28.0);
170
171             cairo_text_extents(ctx, text, &extents);
172             x = (resolution[0] / 2.0) - ((extents.width / 2) + extents.x_bearing);
173             y = (resolution[1] / 2.0) - ((extents.height / 2) + extents.y_bearing);
174
175             cairo_move_to(ctx, x, y);
176             cairo_show_text(ctx, text);
177             cairo_close_path(ctx);
178         }
179
180         /* After the user pressed any valid key or the backspace key, we
181          * highlight a random part of the unlock indicator to confirm this
182          * keypress. */
183         if (unlock_state == STATE_KEY_ACTIVE ||
184             unlock_state == STATE_BACKSPACE_ACTIVE) {
185             cairo_new_sub_path(ctx);
186             double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0;
187             cairo_arc(ctx, resolution[0] / 2 /* x */, resolution[1] / 2 /* y */,
188                       BUTTON_RADIUS /* radius */, highlight_start,
189                       highlight_start + (M_PI / 3.0));
190             if (unlock_state == STATE_KEY_ACTIVE) {
191                 /* For normal keys, we use a lighter green. */
192                 outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
193                 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 139.0/255, 219.0/255, 0);
194                 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 51.0/255, 219.0/255, 0);
195             } else {
196                 /* For backspace, we use red. */
197                 outer_pat = cairo_pattern_create_linear(0, 0, 0, BUTTON_DIAMETER);
198                 cairo_pattern_add_color_stop_rgb(outer_pat, 0, 219.0/255, 139.0/255, 0);
199                 cairo_pattern_add_color_stop_rgb(outer_pat, 1, 219.0/255, 51.0/255, 0);
200             }
201             cairo_set_source(ctx, outer_pat);
202             cairo_stroke(ctx);
203
204             /* Draw two little separators for the highlighted part of the
205              * unlock indicator. */
206             cairo_set_source_rgb(ctx, 0, 0, 0);
207             cairo_arc(ctx,
208                       (resolution[0] / 2) /* x */,
209                       (resolution[1] / 2) /* y */,
210                       BUTTON_RADIUS /* radius */,
211                       highlight_start /* start */,
212                       highlight_start + (M_PI / 128.0) /* end */);
213             cairo_stroke(ctx);
214             cairo_arc(ctx,
215                       (resolution[0] / 2) /* x */,
216                       (resolution[1] / 2) /* y */,
217                       BUTTON_RADIUS /* radius */,
218                       highlight_start + (M_PI / 3.0) /* start */,
219                       (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */);
220             cairo_stroke(ctx);
221         }
222     }
223
224     cairo_surface_destroy(output);
225     cairo_destroy(ctx);
226 #endif
227     return bg_pixmap;
228 }
229
230 /*
231  * Calls draw_image on a new pixmap and swaps that with the current pixmap
232  *
233  */
234 void redraw_screen() {
235     xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
236     xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){ bg_pixmap });
237     /* XXX: Possible optimization: Only update the area in the middle of the
238      * screen instead of the whole screen. */
239     xcb_clear_area(conn, 0, win, 0, 0, screen->width_in_pixels, screen->height_in_pixels);
240     xcb_free_pixmap(conn, bg_pixmap);
241     xcb_flush(conn);
242 }