gri3-wm/font.c

50 lines
1.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Handles font loading
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <xcb/xcb.h>
#include "data.h"
/* TODO: This is just here to be somewhere. Move it somewhere else. */
void check_error(xcb_connection_t *connection, xcb_void_cookie_t cookie, char *errMessage) {
xcb_generic_error_t *error = xcb_request_check (connection, cookie);
if (error != NULL) {
fprintf(stderr, "ERROR: %s : %d\n", errMessage , error->error_code);
xcb_disconnect(connection);
exit(-1);
}
}
i3Font *load_font(xcb_connection_t *c, const char *pattern) {
/* TODO: this function should be caching */
i3Font *new = malloc(sizeof(i3Font));
xcb_list_fonts_with_info_cookie_t cookie = xcb_list_fonts_with_info(c, 1, strlen(pattern), pattern);
xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(c, cookie, NULL);
if (!reply) {
printf("Could not load font\n");
exit(1);
}
/* Oh my, this is so ugly :-(. Why cant they just return a null-terminated
* string? Thats what abstraction layers are for. */
char buffer[xcb_list_fonts_with_info_name_length(reply)+1];
memset(buffer, 0, sizeof(buffer));
memcpy(buffer, xcb_list_fonts_with_info_name(reply), sizeof(buffer)-1);
new->name = strdup(buffer);
new->pattern = strdup(pattern);
new->height = reply->font_ascent + reply->font_descent;
/* Actually load the font */
new->id = xcb_generate_id(c);
xcb_void_cookie_t font_cookie = xcb_open_font_checked(c, new->id, strlen(pattern), pattern);
check_error(c, font_cookie, "Could not open font");
return new;
}