From 6e6af01b7a1f2ff1a35f7751c07e51277d4414e2 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Thu, 3 Nov 2022 23:02:19 +0100 Subject: [PATCH] draw_util: refactor surface_initialized macro into function This makes it possible to set a breakpoint in gdb on a line in the function and get a backtrace of un-initialized surface access. --- libi3/draw_util.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/libi3/draw_util.c b/libi3/draw_util.c index 30eae656..903e3536 100644 --- a/libi3/draw_util.c +++ b/libi3/draw_util.c @@ -20,13 +20,13 @@ extern xcb_visualtype_t *visual_type; /* Forward declarations */ static void draw_util_set_source_color(surface_t *surface, color_t color); -#define RETURN_UNLESS_SURFACE_INITIALIZED(surface) \ - do { \ - if ((surface)->id == XCB_NONE) { \ - ELOG("Surface %p is not initialized, skipping drawing.\n", surface); \ - return; \ - } \ - } while (0) +static bool surface_initialized(surface_t *surface) { + if (surface->id == XCB_NONE) { + ELOG("Surface %p is not initialized, skipping drawing.\n", surface); + return false; + } + return true; +} /* * Initialize the surface to represent the given drawable. @@ -131,7 +131,9 @@ color_t draw_util_hex_to_color(const char *color) { * */ static void draw_util_set_source_color(surface_t *surface, color_t color) { - RETURN_UNLESS_SURFACE_INITIALIZED(surface); + if (!surface_initialized(surface)) { + return; + } cairo_set_source_rgba(surface->cr, color.red, color.green, color.blue, color.alpha); } @@ -143,7 +145,9 @@ static void draw_util_set_source_color(surface_t *surface, color_t color) { * */ void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) { - RETURN_UNLESS_SURFACE_INITIALIZED(surface); + if (!surface_initialized(surface)) { + return; + } /* Flush any changes before we draw the text as this might use XCB directly. */ CAIRO_SURFACE_FLUSH(surface->surface); @@ -162,7 +166,9 @@ void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_ * */ void draw_util_image(cairo_surface_t *image, surface_t *surface, int x, int y, int width, int height) { - RETURN_UNLESS_SURFACE_INITIALIZED(surface); + if (!surface_initialized(surface)) { + return; + } cairo_save(surface->cr); @@ -186,7 +192,9 @@ void draw_util_image(cairo_surface_t *image, surface_t *surface, int x, int y, i * */ void draw_util_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h) { - RETURN_UNLESS_SURFACE_INITIALIZED(surface); + if (!surface_initialized(surface)) { + return; + } cairo_save(surface->cr); @@ -211,7 +219,9 @@ void draw_util_rectangle(surface_t *surface, color_t color, double x, double y, * */ void draw_util_clear_surface(surface_t *surface, color_t color) { - RETURN_UNLESS_SURFACE_INITIALIZED(surface); + if (!surface_initialized(surface)) { + return; + } cairo_save(surface->cr); @@ -236,8 +246,10 @@ void draw_util_clear_surface(surface_t *surface, color_t color) { */ void draw_util_copy_surface(surface_t *src, surface_t *dest, double src_x, double src_y, double dest_x, double dest_y, double width, double height) { - RETURN_UNLESS_SURFACE_INITIALIZED(src); - RETURN_UNLESS_SURFACE_INITIALIZED(dest); + if (!surface_initialized(src) || + !surface_initialized(dest)) { + return; + } cairo_save(dest->cr);