]> git.sur5r.net Git - i3/i3/blob - font.c
Implement keybindings, adjust CMDMODE grammar, update DEPENDS
[i3/i3] / font.c
1 /*
2  * Handles font loading
3  *
4  */
5 #include <string.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <xcb/xcb.h>
9
10 #include "data.h"
11
12 /* TODO: This is just here to be somewhere. Move it somewhere else. */
13 void check_error(xcb_connection_t *connection, xcb_void_cookie_t cookie, char *errMessage) {
14         xcb_generic_error_t *error = xcb_request_check (connection, cookie);
15         if (error != NULL) {
16                 fprintf(stderr, "ERROR: %s : %d\n", errMessage , error->error_code);
17                 xcb_disconnect(connection);
18                 exit(-1);
19         }
20 }
21
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 }