]> git.sur5r.net Git - i3/i3lock/blob - i3lock.c
Properly free the reply of xcb_get_geometry
[i3/i3lock] / i3lock.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 <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <xcb/xcb.h>
16 #include <xcb/dpms.h>
17 #include <xcb/xcb_keysyms.h>
18 #include <err.h>
19 #include <assert.h>
20 #include <security/pam_appl.h>
21 /* FIXME: can we get rid of this header? */
22 #include <X11/keysym.h>
23 #include <getopt.h>
24 #include <string.h>
25 #include <ev.h>
26 #include <sys/mman.h>
27
28
29 #ifndef NOLIBCAIRO
30 #include <cairo.h>
31 #include <cairo/cairo-xcb.h>
32 #endif
33
34 #include "keysym2ucs.h"
35 #include "ucs2_to_utf8.h"
36 #include "xcb.h"
37 #include "cursors.h"
38 #include "unlock_indicator.h"
39
40 char color[7] = "ffffff";
41 uint32_t last_resolution[2];
42 xcb_window_t win;
43 static xcb_cursor_t cursor;
44 static xcb_key_symbols_t *symbols;
45 static pam_handle_t *pam_handle;
46 static int input_position = 0;
47 /* Holds the password you enter (in UTF-8). */
48 static char password[512];
49 static bool modeswitch_active = false;
50 static bool iso_level3_shift_active = false;
51 static bool iso_level5_shift_active = false;
52 static int modeswitchmask;
53 static int numlockmask;
54 static bool beep = false;
55 static bool debug_mode = false;
56 static bool dpms = false;
57 bool unlock_indicator = true;
58 static bool dont_fork = false;
59 static struct ev_loop *main_loop;
60 static struct ev_timer *clear_pam_wrong_timeout;
61 static struct ev_timer *clear_indicator_timeout;
62 extern unlock_state_t unlock_state;
63 extern pam_state_t pam_state;
64
65 #define DEBUG(fmt, ...) do { \
66     if (debug_mode) \
67         printf("[i3lock-debug] " fmt, ##__VA_ARGS__); \
68 } while (0)
69
70 #ifndef NOLIBCAIRO
71 cairo_surface_t *img = NULL;
72 bool tile = false;
73 #endif
74
75 /*
76  * Clears the memory which stored the password to be a bit safer against
77  * cold-boot attacks.
78  *
79  */
80 static void clear_password_memory() {
81     /* A volatile pointer to the password buffer to prevent the compiler from
82      * optimizing this out. */
83     volatile char *vpassword = password;
84     for (int c = 0; c < sizeof(password); c++)
85         /* We store a non-random pattern which consists of the (irrelevant)
86          * index plus (!) the value of the beep variable. This prevents the
87          * compiler from optimizing the calls away, since the value of 'beep'
88          * is not known at compile-time. */
89         vpassword[c] = c + (int)beep;
90 }
91
92
93 /*
94  * Resets pam_state to STATE_PAM_IDLE 2 seconds after an unsuccesful
95  * authentication event.
96  *
97  */
98 static void clear_pam_wrong(EV_P_ ev_timer *w, int revents) {
99     DEBUG("clearing pam wrong\n");
100     pam_state = STATE_PAM_IDLE;
101     unlock_state = STATE_STARTED;
102     redraw_screen();
103 }
104
105 /*
106  * Hides the unlock indicator completely when there is no content in the
107  * password buffer.
108  *
109  */
110 static void clear_indicator(EV_P_ ev_timer *w, int revents) {
111     if (input_position == 0) {
112         DEBUG("Clear indicator\n");
113         unlock_state = STATE_STARTED;
114     } else unlock_state = STATE_KEY_PRESSED;
115     redraw_screen();
116 }
117
118 /*
119  * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
120  * after an unsuccessful authentication attempt.
121  *
122  */
123 static void start_clear_indicator_timeout() {
124     if (clear_indicator_timeout) {
125         ev_timer_stop(main_loop, clear_indicator_timeout);
126         ev_timer_set(clear_indicator_timeout, 1.0, 0.);
127         ev_timer_start(main_loop, clear_indicator_timeout);
128     } else {
129         clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1);
130         ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
131         ev_timer_start(main_loop, clear_indicator_timeout);
132     }
133 }
134
135 static void input_done() {
136     if (input_position == 0)
137         return;
138
139     if (clear_pam_wrong_timeout) {
140         ev_timer_stop(main_loop, clear_pam_wrong_timeout);
141         clear_pam_wrong_timeout = NULL;
142     }
143
144     pam_state = STATE_PAM_VERIFY;
145     redraw_screen();
146
147     if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
148         printf("successfully authenticated\n");
149         clear_password_memory();
150         exit(0);
151     }
152
153     fprintf(stderr, "Authentication failure\n");
154
155     pam_state = STATE_PAM_WRONG;
156     redraw_screen();
157
158     /* Clear this state after 2 seconds (unless the user enters another
159      * password during that time). */
160     ev_now_update(main_loop);
161     clear_pam_wrong_timeout = calloc(sizeof(struct ev_timer), 1);
162     ev_timer_init(clear_pam_wrong_timeout, clear_pam_wrong, 2.0, 0.);
163     ev_timer_start(main_loop, clear_pam_wrong_timeout);
164
165     /* Cancel the clear_indicator_timeout, it would hide the unlock indicator
166      * too early. */
167     if (clear_indicator_timeout) {
168         ev_timer_stop(main_loop, clear_indicator_timeout);
169         clear_indicator_timeout = NULL;
170     }
171
172     /* beep on authentication failure, if enabled */
173     if (beep) {
174         xcb_bell(conn, 100);
175         xcb_flush(conn);
176     }
177 }
178
179 /*
180  * Called when the user releases a key. We need to leave the Mode_switch
181  * state when the user releases the Mode_switch key.
182  *
183  */
184 static void handle_key_release(xcb_key_release_event_t *event) {
185     DEBUG("releasing key %d, state raw = %d, modeswitch_active = %d, iso_level3_shift_active = %d, iso_level5_shift_active = %d\n",
186           event->detail, event->state, modeswitch_active, iso_level3_shift_active, iso_level5_shift_active);
187
188     /* We don’t care about the column here and just use the first symbol. Since
189      * we only check for Mode_switch and ISO_Level3_Shift, this *should* work.
190      * Also, if we would use the current column, we would look in the wrong
191      * place. */
192     xcb_keysym_t sym = xcb_key_press_lookup_keysym(symbols, event, 0);
193     if (sym == XK_Mode_switch) {
194         //printf("Mode switch disabled\n");
195         modeswitch_active = false;
196     } else if (sym == XK_ISO_Level3_Shift) {
197         iso_level3_shift_active = false;
198     } else if (sym == XK_ISO_Level5_Shift) {
199         iso_level5_shift_active = false;
200     }
201     DEBUG("release done. modeswitch_active = %d, iso_level3_shift_active = %d, iso_level5_shift_active = %d\n",
202           modeswitch_active, iso_level3_shift_active, iso_level5_shift_active);
203 }
204
205 static void redraw_timeout(EV_P_ ev_timer *w, int revents) {
206     redraw_screen();
207 }
208
209 /*
210  * Handle key presses. Fixes state, then looks up the key symbol for the
211  * given keycode, then looks up the key symbol (as UCS-2), converts it to
212  * UTF-8 and stores it in the password array.
213  *
214  */
215 static void handle_key_press(xcb_key_press_event_t *event) {
216     DEBUG("keypress %d, state raw = %d, modeswitch_active = %d, iso_level3_shift_active = %d\n",
217           event->detail, event->state, modeswitch_active, iso_level3_shift_active);
218
219     xcb_keysym_t sym0, sym1, sym;
220     /* For each keycode, there is a list of symbols. The list could look like this:
221      * $ xmodmap -pke | grep 'keycode  38'
222      * keycode  38 = a A adiaeresis Adiaeresis o O
223      * In non-X11 terminology, the symbols for the keycode 38 (the key labeled
224      * with "a" on my keyboard) are "a A ä Ä o O".
225      * Another form to display the same information is using xkbcomp:
226      * $ xkbcomp $DISPLAY /tmp/xkb.dump
227      * Then open /tmp/xkb.dump and search for '\<a\>' (in VIM regexp-language):
228      *
229      * symbols[Group1]= [               a,               A,               o,               O ],
230      * symbols[Group2]= [      adiaeresis,      Adiaeresis ]
231      *
232      * So there are two *groups*, one containing 'a A' and one containing 'ä
233      * Ä'. You can use Mode_switch to switch between these groups. You can use
234      * ISO_Level3_Shift to reach the 'o O' part of the first group (it’s the
235      * same group, just an even higher shift level).
236      *
237      * So, using the "logical" XKB information, the following lookup will be
238      * performed:
239      *
240      * Neither Mode_switch nor ISO_Level3_Shift active: group 1, column 0 and 1
241      * Mode_switch active: group 2, column 0 and 1
242      * ISO_Level3_Shift active: group 1, column 2 and 3
243      *
244      * Using the column index which xcb_key_press_lookup_keysym uses (and
245      * xmodmap prints out), the following lookup will be performed:
246      *
247      * Neither Mode_switch nor ISO_Level3_Shift active: column 0 and 1
248      * Mode_switch active: column 2 and 3
249      * ISO_Level3_Shift active: column 4 and 5
250      */
251     int base_column = 0;
252     if (modeswitch_active)
253         base_column = 2;
254     if (iso_level3_shift_active)
255         base_column = 4;
256     if (iso_level5_shift_active)
257         base_column = 6;
258     sym0 = xcb_key_press_lookup_keysym(symbols, event, base_column);
259     sym1 = xcb_key_press_lookup_keysym(symbols, event, base_column + 1);
260     switch (sym0) {
261     case XK_Mode_switch:
262         DEBUG("Mode switch enabled\n");
263         modeswitch_active = true;
264         return;
265     case XK_ISO_Level3_Shift:
266         DEBUG("ISO_Level3_Shift enabled\n");
267         iso_level3_shift_active = true;
268         return;
269     case XK_ISO_Level5_Shift:
270         DEBUG("ISO_Level5_Shift enabled\n");
271         iso_level5_shift_active = true;
272         return;
273     case XK_Return:
274     case XK_KP_Enter:
275         input_done();
276     case XK_Escape:
277         input_position = 0;
278         clear_password_memory();
279         password[input_position] = '\0';
280         return;
281
282     case XK_BackSpace:
283         if (input_position == 0)
284             return;
285
286         /* decrement input_position to point to the previous glyph */
287         u8_dec(password, &input_position);
288         password[input_position] = '\0';
289
290         /* Clear this state after 2 seconds (unless the user enters another
291          * password during that time). */
292         start_clear_indicator_timeout();
293         unlock_state = STATE_BACKSPACE_ACTIVE;
294         redraw_screen();
295         unlock_state = STATE_KEY_PRESSED;
296         //printf("new input position = %d, new password = %s\n", input_position, password);
297         return;
298     }
299
300     if ((input_position + 8) >= sizeof(password))
301         return;
302
303     if ((event->state & numlockmask) && xcb_is_keypad_key(sym1)) {
304         /* this key was a keypad key */
305         if ((event->state & XCB_MOD_MASK_SHIFT))
306             sym = sym0;
307         else sym = sym1;
308     } else {
309         if ((event->state & XCB_MOD_MASK_SHIFT))
310             sym = sym1;
311         else sym = sym0;
312     }
313
314 #if 0
315     /* FIXME: handle all of these? */
316     printf("is_keypad_key = %d\n", xcb_is_keypad_key(sym));
317     printf("is_private_keypad_key = %d\n", xcb_is_private_keypad_key(sym));
318     printf("xcb_is_cursor_key = %d\n", xcb_is_cursor_key(sym));
319     printf("xcb_is_pf_key = %d\n", xcb_is_pf_key(sym));
320     printf("xcb_is_function_key = %d\n", xcb_is_function_key(sym));
321     printf("xcb_is_misc_function_key = %d\n", xcb_is_misc_function_key(sym));
322     printf("xcb_is_modifier_key = %d\n", xcb_is_modifier_key(sym));
323 #endif
324
325     if (xcb_is_modifier_key(sym) || xcb_is_cursor_key(sym))
326         return;
327
328     DEBUG("resolved to keysym = %c (%d)\n", sym, sym);
329
330     /* convert the keysym to UCS */
331     uint16_t ucs = keysym2ucs(sym);
332     if ((int16_t)ucs == -1) {
333         fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
334         return;
335     }
336
337     /* store the UCS in a string to convert it */
338     uint8_t inp[3] = {(ucs & 0xFF00) >> 8, (ucs & 0xFF), 0};
339     DEBUG("input part = %s\n", inp);
340
341     /* store it in the password array as UTF-8 */
342     input_position += convert_ucs_to_utf8((char*)inp, password + input_position);
343     password[input_position] = '\0';
344     DEBUG("current password = %s\n", password);
345
346     unlock_state = STATE_KEY_ACTIVE;
347     redraw_screen();
348     unlock_state = STATE_KEY_PRESSED;
349
350     struct ev_timer *timeout = calloc(sizeof(struct ev_timer), 1);
351     ev_timer_init(timeout, redraw_timeout, 0.25, 0.);
352     ev_timer_start(main_loop, timeout);
353
354     if (clear_indicator_timeout) {
355         ev_timer_stop(main_loop, clear_indicator_timeout);
356         clear_indicator_timeout = NULL;
357     }
358 }
359
360 /*
361  * A visibility notify event will be received when the visibility (= can the
362  * user view the complete window) changes, so for example when a popup overlays
363  * some area of the i3lock window.
364  *
365  * In this case, we raise our window on top so that the popup (or whatever is
366  * hiding us) gets hidden.
367  *
368  */
369 static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
370     if (event->state != XCB_VISIBILITY_UNOBSCURED) {
371         uint32_t values[] = { XCB_STACK_MODE_ABOVE };
372         xcb_configure_window(conn, event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
373         xcb_flush(conn);
374     }
375 }
376
377 /*
378  * Called when the keyboard mapping changes. We update our symbols.
379  *
380  */
381 static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
382     xcb_refresh_keyboard_mapping(symbols, event);
383
384     modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
385     numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
386 }
387
388 /*
389  * Called when the properties on the root window change, e.g. when the screen
390  * resolution changes. If so we update the window to cover the whole screen
391  * and also redraw the image, if any.
392  *
393  */
394 void handle_screen_resize() {
395     xcb_get_geometry_cookie_t geomc;
396     xcb_get_geometry_reply_t *geom;
397     geomc = xcb_get_geometry(conn, screen->root);
398     if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL)
399         return;
400
401     if (last_resolution[0] == geom->width &&
402         last_resolution[1] == geom->height) {
403         free(geom);
404         return;
405     }
406
407     last_resolution[0] = geom->width;
408     last_resolution[1] = geom->height;
409
410     free(geom);
411
412 #ifndef NOLIBCAIRO
413     redraw_screen();
414 #endif
415
416     uint32_t mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
417     xcb_configure_window(conn, win, mask, last_resolution);
418     xcb_flush(conn);
419 }
420
421 /*
422  * Callback function for PAM. We only react on password request callbacks.
423  *
424  */
425 static int conv_callback(int num_msg, const struct pam_message **msg,
426                          struct pam_response **resp, void *appdata_ptr)
427 {
428     if (num_msg == 0)
429         return 1;
430
431     /* PAM expects an array of responses, one for each message */
432     if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
433         perror("calloc");
434         return 1;
435     }
436
437     for (int c = 0; c < num_msg; c++) {
438         if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
439             msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
440             continue;
441
442         /* return code is currently not used but should be set to zero */
443         resp[c]->resp_retcode = 0;
444         if ((resp[c]->resp = strdup(password)) == NULL) {
445             perror("strdup");
446             return 1;
447         }
448     }
449
450     return 0;
451 }
452
453 /*
454  * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
455  * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
456  *
457  */
458 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
459     /* empty, because xcb_prepare_cb and xcb_check_cb are used */
460 }
461
462 /*
463  * Flush before blocking (and waiting for new events)
464  *
465  */
466 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
467     xcb_flush(conn);
468 }
469
470 /*
471  * Instead of polling the X connection socket we leave this to
472  * xcb_poll_for_event() which knows better than we can ever know.
473  *
474  */
475 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
476     xcb_generic_event_t *event;
477
478     while ((event = xcb_poll_for_event(conn)) != NULL) {
479         if (event->response_type == 0) {
480             xcb_generic_error_t *error = (xcb_generic_error_t*)event;
481             fprintf(stderr, "X11 Error received! sequence 0x%x, error_code = %d\n",
482                     error->sequence, error->error_code);
483             free(event);
484             continue;
485         }
486
487         /* Strip off the highest bit (set if the event is generated) */
488         int type = (event->response_type & 0x7F);
489
490         if (type == XCB_KEY_PRESS) {
491             handle_key_press((xcb_key_press_event_t*)event);
492             continue;
493         }
494
495         if (type == XCB_KEY_RELEASE) {
496             handle_key_release((xcb_key_release_event_t*)event);
497
498             /* If this was the backspace or escape key we are back at an
499              * empty input, so turn off the screen if DPMS is enabled */
500             if (dpms && input_position == 0)
501                 dpms_turn_off_screen(conn);
502
503             continue;
504         }
505
506         if (type == XCB_VISIBILITY_NOTIFY) {
507             handle_visibility_notify((xcb_visibility_notify_event_t*)event);
508             continue;
509         }
510
511         if (type == XCB_MAP_NOTIFY) {
512             if (!dont_fork) {
513                 /* After the first MapNotify, we never fork again. We don’t
514                  * expect to get another MapNotify, but better be sure… */
515                 dont_fork = true;
516
517                 /* In the parent process, we exit */
518                 if (fork() != 0)
519                     exit(0);
520             }
521             continue;
522         }
523
524         if (type == XCB_MAPPING_NOTIFY) {
525             handle_mapping_notify((xcb_mapping_notify_event_t*)event);
526             continue;
527         }
528
529         if (type == XCB_CONFIGURE_NOTIFY) {
530             handle_screen_resize();
531             continue;
532         }
533
534         printf("WARNING: unhandled event of type %d\n", type);
535
536         free(event);
537     }
538 }
539
540 int main(int argc, char *argv[]) {
541     char *username;
542 #ifndef NOLIBCAIRO
543     char *image_path = NULL;
544 #endif
545     int ret;
546     struct pam_conv conv = {conv_callback, NULL};
547     int nscreen;
548     int curs_choice = CURS_NONE;
549     char o;
550     int optind = 0;
551     struct option longopts[] = {
552         {"version", no_argument, NULL, 'v'},
553         {"nofork", no_argument, NULL, 'n'},
554         {"beep", no_argument, NULL, 'b'},
555         {"dpms", no_argument, NULL, 'd'},
556         {"color", required_argument, NULL, 'c'},
557         {"pointer", required_argument, NULL , 'p'},
558         {"debug", no_argument, NULL, 0},
559         {"help", no_argument, NULL, 'h'},
560         {"no-unlock-indicator", no_argument, NULL, 'u'},
561 #ifndef NOLIBCAIRO
562         {"image", required_argument, NULL, 'i'},
563         {"tiling", no_argument, NULL, 't'},
564 #endif
565         {NULL, no_argument, NULL, 0}
566     };
567
568     if ((username = getenv("USER")) == NULL)
569         errx(1, "USER environment variable not set, please set it.\n");
570
571     while ((o = getopt_long(argc, argv, "hvnbdc:p:u"
572 #ifndef NOLIBCAIRO
573         "i:t"
574 #endif
575         , longopts, &optind)) != -1) {
576         switch (o) {
577         case 'v':
578             errx(EXIT_SUCCESS, "version " VERSION " © 2010-2012 Michael Stapelberg");
579         case 'n':
580             dont_fork = true;
581             break;
582         case 'b':
583             beep = true;
584             break;
585         case 'd':
586             dpms = true;
587             break;
588         case 'c': {
589             char *arg = optarg;
590
591             /* Skip # if present */
592             if (arg[0] == '#')
593                 arg++;
594
595             if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
596                 errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
597
598             break;
599         }
600         case 'u':
601             unlock_indicator = false;
602             break;
603 #ifndef NOLIBCAIRO
604         case 'i':
605             image_path = strdup(optarg);
606             break;
607         case 't':
608             tile = true;
609             break;
610 #endif
611         case 'p':
612             if (!strcmp(optarg, "win")) {
613                 curs_choice = CURS_WIN;
614             } else if (!strcmp(optarg, "default")) {
615                 curs_choice = CURS_DEFAULT;
616             } else {
617                 errx(1, "i3lock: Invalid pointer type given. Expected one of \"win\" or \"default\".\n");
618             }
619             break;
620         case 0:
621             if (strcmp(longopts[optind].name, "debug") == 0)
622                 debug_mode = true;
623             break;
624         default:
625             errx(1, "Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-u] [-p win|default]"
626 #ifndef NOLIBCAIRO
627             " [-i image.png] [-t]"
628 #else
629             " (compiled with NOLIBCAIRO)"
630 #endif
631             );
632         }
633     }
634
635     /* We need (relatively) random numbers for highlighting a random part of
636      * the unlock indicator upon keypresses. */
637     srand(time(NULL));
638
639     /* Initialize PAM */
640     ret = pam_start("i3lock", username, &conv, &pam_handle);
641     if (ret != PAM_SUCCESS)
642         errx(EXIT_FAILURE, "PAM: %s", pam_strerror(pam_handle, ret));
643
644     /* Lock the area where we store the password in memory, we don’t want it to
645      * be swapped to disk. Since Linux 2.6.9, this does not require any
646      * privileges, just enough bytes in the RLIMIT_MEMLOCK limit. */
647     if (mlock(password, sizeof(password)) != 0)
648         err(EXIT_FAILURE, "Could not lock page in memory, check RLIMIT_MEMLOCK");
649
650     /* Initialize connection to X11 */
651     if ((conn = xcb_connect(NULL, &nscreen)) == NULL ||
652         xcb_connection_has_error(conn))
653         errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
654
655     /* if DPMS is enabled, check if the X server really supports it */
656     if (dpms) {
657         xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
658         xcb_dpms_capable_reply_t *dpmsr;
659         if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
660             fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
661             dpms = false;
662         }
663     }
664
665     screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
666
667     last_resolution[0] = screen->width_in_pixels;
668     last_resolution[1] = screen->height_in_pixels;
669
670
671 #ifndef NOLIBCAIRO
672     if (image_path) {
673         /* Create a pixmap to render on, fill it with the background color */
674         img = cairo_image_surface_create_from_png(image_path);
675     }
676 #endif
677
678     /* Pixmap on which the image is rendered to (if any) */
679     xcb_pixmap_t bg_pixmap = draw_image(last_resolution);
680
681     /* open the fullscreen window, already with the correct pixmap in place */
682     win = open_fullscreen_window(conn, screen, color, bg_pixmap);
683     xcb_free_pixmap(conn, bg_pixmap);
684
685     cursor = create_cursor(conn, screen, win, curs_choice);
686
687     grab_pointer_and_keyboard(conn, screen, cursor);
688
689     symbols = xcb_key_symbols_alloc(conn);
690     modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
691     numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
692
693     if (dpms)
694         dpms_turn_off_screen(conn);
695
696     /* Initialize the libev event loop. */
697     main_loop = EV_DEFAULT;
698     if (main_loop == NULL)
699         errx(EXIT_FAILURE, "Could not initialize libev. Bad LIBEV_FLAGS?\n");
700
701     struct ev_io *xcb_watcher = calloc(sizeof(struct ev_io), 1);
702     struct ev_check *xcb_check = calloc(sizeof(struct ev_check), 1);
703     struct ev_prepare *xcb_prepare = calloc(sizeof(struct ev_prepare), 1);
704
705     ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
706     ev_io_start(main_loop, xcb_watcher);
707
708     ev_check_init(xcb_check, xcb_check_cb);
709     ev_check_start(main_loop, xcb_check);
710
711     ev_prepare_init(xcb_prepare, xcb_prepare_cb);
712     ev_prepare_start(main_loop, xcb_prepare);
713
714     xcb_flush(conn);
715     ev_loop(main_loop, 0);
716 }