]> git.sur5r.net Git - i3/i3/blob - src/font.c
Use c99
[i3/i3] / src / font.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * (c) 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 /*
12  * Handles font loading
13  *
14  */
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <xcb/xcb.h>
19
20 #include "data.h"
21 #include "util.h"
22
23 i3Font *load_font(xcb_connection_t *c, const char *pattern) {
24         /* TODO: this function should be caching */
25         i3Font *new = malloc(sizeof(i3Font));
26
27         xcb_list_fonts_with_info_cookie_t cookie = xcb_list_fonts_with_info(c, 1, strlen(pattern), pattern);
28         xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(c, cookie, NULL);
29         if (!reply) {
30                 printf("Could not load font\n");
31                 exit(1);
32         }
33
34         /* Oh my, this is so ugly :-(. Why can’t they just return a null-terminated
35          * string? That’s what abstraction layers are for. */
36         char buffer[xcb_list_fonts_with_info_name_length(reply)+1];
37         memset(buffer, 0, sizeof(buffer));
38         memcpy(buffer, xcb_list_fonts_with_info_name(reply), sizeof(buffer)-1);
39         new->name = strdup(buffer);
40         new->pattern = strdup(pattern);
41         new->height = reply->font_ascent + reply->font_descent;
42
43         /* Actually load the font */
44         new->id = xcb_generate_id(c);
45         xcb_void_cookie_t font_cookie = xcb_open_font_checked(c, new->id, strlen(pattern), pattern);
46         check_error(c, font_cookie, "Could not open font");
47
48         return new;
49 }