From 742cef2881c9b1b6ad8338144ea794655a48d131 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 15 Feb 2009 02:52:52 +0100 Subject: [PATCH] Finalize font.c --- include/util.h | 4 ++++ src/font.c | 34 +++++++++++++++------------------- src/util.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/include/util.h b/include/util.h index c05b91cf..a5be61b3 100644 --- a/include/util.h +++ b/include/util.h @@ -17,6 +17,10 @@ int min(int a, int b); int max(int a, int b); +void exit_if_null(void *pointer, char *fmt, ...); +void die(char *fmt, ...); +void *smalloc(size_t size); +char *sstrdup(const char *str); void start_application(const char *command); void check_error(xcb_connection_t *connection, xcb_void_cookie_t cookie, char *err_message); void set_focus(xcb_connection_t *conn, Client *client); diff --git a/src/font.c b/src/font.c index ec65eda7..7c7fe934 100644 --- a/src/font.c +++ b/src/font.c @@ -3,13 +3,11 @@ * * i3 - an improved dynamic tiling window manager * - * (c) 2009 Michael Stapelberg and contributors + * © 2009 Michael Stapelberg and contributors * * See file LICENSE for license information. * - */ -/* - * Handles font loading + * font.c: Handles font loading (with caching, with height information) * */ #include @@ -27,32 +25,30 @@ TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached * maintains a cache. * */ -i3Font *load_font(xcb_connection_t *c, const char *pattern) { +i3Font *load_font(xcb_connection_t *connection, const char *pattern) { /* Check if we got the font cached */ i3Font *font; TAILQ_FOREACH(font, &cached_fonts, fonts) if (strcmp(font->pattern, pattern) == 0) return font; - i3Font *new = malloc(sizeof(i3Font)); + i3Font *new = smalloc(sizeof(i3Font)); /* Send all our requests first */ - new->id = xcb_generate_id(c); - xcb_void_cookie_t font_cookie = xcb_open_font_checked(c, new->id, strlen(pattern), pattern); - xcb_list_fonts_with_info_cookie_t cookie = xcb_list_fonts_with_info(c, 1, strlen(pattern), pattern); + new->id = xcb_generate_id(connection); + xcb_void_cookie_t font_cookie = xcb_open_font_checked(connection, new->id, strlen(pattern), pattern); + xcb_list_fonts_with_info_cookie_t cookie = xcb_list_fonts_with_info(connection, 1, strlen(pattern), pattern); - check_error(c, font_cookie, "Could not open font"); + check_error(connection, font_cookie, "Could not open font"); /* Get information (height/name) for this font */ - xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(c, cookie, NULL); - if (reply == NULL) { - printf("Could not load font\n"); - exit(1); - } - - asprintf(&(new->name), "%.*s", xcb_list_fonts_with_info_name_length(reply), - xcb_list_fonts_with_info_name(reply)); - new->pattern = strdup(pattern); + xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(connection, cookie, NULL); + exit_if_null(reply, "Could not load font \"%s\"\n", pattern); + + if (asprintf(&(new->name), "%.*s", xcb_list_fonts_with_info_name_length(reply), + xcb_list_fonts_with_info_name(reply)) == -1) + die("asprintf() failed\n"); + new->pattern = sstrdup(pattern); new->height = reply->font_ascent + reply->font_descent; /* Insert into cache */ diff --git a/src/util.c b/src/util.c index 7ce4d55f..76abe2aa 100644 --- a/src/util.c +++ b/src/util.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "i3.h" #include "data.h" @@ -27,6 +28,55 @@ int max(int a, int b) { return (a > b ? a : b); } +/* + * Checks if pointer is NULL and exits the whole program, printing a message to stdout + * before with the given format (see printf()) + * + */ +void exit_if_null(void *pointer, char *fmt, ...) { + va_list args; + + if (pointer != NULL) + return; + + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + + exit(EXIT_FAILURE); +} + +/* + * Prints the message (see printf()) to stderr, then exits the program. + * + */ +void die(char *fmt, ...) { + va_list args; + + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + + exit(EXIT_FAILURE); +} + +/* + * The s* functions (safe) are wrappers around malloc, strdup, …, which exits if one of + * the called functions returns NULL, meaning that there is no more memory available + * + */ +void *smalloc(size_t size) { + void *result = malloc(size); + exit_if_null(result, "Too less memory for malloc(%d)\n", size); + return result; +} + +char *sstrdup(const char *str) { + char *result = strdup(str); + exit_if_null(result, "Too less memory for strdup()\n"); + return result; +} + /* * Starts the given application by passing it through a shell. We use double fork * to avoid zombie processes. As the started application’s parent exits (immediately), -- 2.39.5