]> git.sur5r.net Git - i3/i3/blob - libi3/load_font.c
Merge branch 'fix-take-focus'
[i3/i3] / libi3 / load_font.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdbool.h>
12 #include <err.h>
13
14 #include "libi3.h"
15
16 extern xcb_connection_t *conn;
17
18 /*
19  * Loads a font for usage, also getting its height. If fallback is true,
20  * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting.
21  *
22  */
23 i3Font load_font(const char *pattern, bool fallback) {
24     i3Font font;
25     xcb_void_cookie_t font_cookie;
26     xcb_list_fonts_with_info_cookie_t info_cookie;
27     xcb_list_fonts_with_info_reply_t *info_reply;
28     xcb_generic_error_t *error;
29
30     /* Send all our requests first */
31     font.id = xcb_generate_id(conn);
32     font_cookie = xcb_open_font_checked(conn, font.id, strlen(pattern), pattern);
33     info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
34
35     /* Check for errors. If errors, fall back to default font. */
36     error = xcb_request_check(conn, font_cookie);
37
38     /* If we fail to open font, fall back to 'fixed' */
39     if (fallback && error != NULL) {
40         ELOG("Could not open font %s (X error %d). Trying fallback to 'fixed'.\n",
41              pattern, error->error_code);
42         pattern = "fixed";
43         font_cookie = xcb_open_font_checked(conn, font.id, strlen(pattern), pattern);
44         info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
45
46         /* Check if we managed to open 'fixed' */
47         error = xcb_request_check(conn, font_cookie);
48
49         /* Fall back to '-misc-*' if opening 'fixed' fails. */
50         if (error != NULL) {
51             ELOG("Could not open fallback font 'fixed', trying with '-misc-*'.\n");
52             pattern = "-misc-*";
53             font_cookie = xcb_open_font_checked(conn, font.id, strlen(pattern), pattern);
54             info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
55
56             if ((error = xcb_request_check(conn, font_cookie)) != NULL)
57                 errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks "
58                      "(fixed or -misc-*): X11 error %d", error->error_code);
59         }
60     }
61
62     /* Get information (height/name) for this font */
63     if (!(info_reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL)))
64         errx(EXIT_FAILURE, "Could not load font \"%s\"", pattern);
65
66     font.height = info_reply->font_ascent + info_reply->font_descent;
67
68     free(info_reply);
69
70     return font;
71 }