]> git.sur5r.net Git - i3/i3/blobdiff - libi3/draw_util.c
Make comment style more consistent
[i3/i3] / libi3 / draw_util.c
index 90b1f26fd66a55e8678b47315f85d0f6771e177c..f88360dc32ecab74a146a6e7e35789c9c35efc0b 100644 (file)
@@ -6,57 +6,76 @@
  * draw.c: Utility for drawing.
  *
  */
+#include "libi3.h"
+
 #include <stdlib.h>
 #include <err.h>
 #include <string.h>
 #include <xcb/xcb.h>
 #include <xcb/xcb_aux.h>
-#ifdef CAIRO_SUPPORT
 #include <cairo/cairo-xcb.h>
-#endif
-
-#include "libi3.h"
 
-xcb_connection_t *xcb_connection;
+/* The default visual_type to use if none is specified when creating the surface. Must be defined globally. */
 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)
+
 /*
  * Initialize the surface to represent the given drawable.
  *
  */
-void draw_util_surface_init(surface_t *surface, xcb_drawable_t drawable, int width, int height) {
+void draw_util_surface_init(xcb_connection_t *conn, surface_t *surface, xcb_drawable_t drawable,
+                            xcb_visualtype_t *visual, int width, int height) {
     surface->id = drawable;
+    surface->visual_type = ((visual == NULL) ? visual_type : visual);
     surface->width = width;
     surface->height = height;
 
-    surface->gc = xcb_generate_id(xcb_connection);
-    xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(xcb_connection, surface->gc, surface->id, 0, NULL);
+    surface->gc = xcb_generate_id(conn);
+    xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(conn, surface->gc, surface->id, 0, NULL);
 
-    xcb_generic_error_t *error = xcb_request_check(xcb_connection, gc_cookie);
+    xcb_generic_error_t *error = xcb_request_check(conn, gc_cookie);
     if (error != NULL) {
-        ELOG("Could not create graphical context. Error code: %d\n", error->error_code);
-        exit(EXIT_FAILURE);
+        ELOG("Could not create graphical context. Error code: %d. Please report this bug.\n", error->error_code);
     }
 
-#ifdef CAIRO_SUPPORT
-    surface->surface = cairo_xcb_surface_create(xcb_connection, surface->id, visual_type, width, height);
+    surface->surface = cairo_xcb_surface_create(conn, surface->id, surface->visual_type, width, height);
     surface->cr = cairo_create(surface->surface);
-#endif
 }
 
 /*
  * Destroys the surface.
  *
  */
-void draw_util_surface_free(surface_t *surface) {
-    xcb_free_gc(xcb_connection, surface->gc);
-#ifdef CAIRO_SUPPORT
+void draw_util_surface_free(xcb_connection_t *conn, surface_t *surface) {
+    xcb_free_gc(conn, surface->gc);
     cairo_surface_destroy(surface->surface);
     cairo_destroy(surface->cr);
-#endif
+
+    /* We need to explicitly set these to NULL to avoid assertion errors in
+     * cairo when calling this multiple times. This can happen, for example,
+     * when setting the border of a window to none and then closing it. */
+    surface->surface = NULL;
+    surface->cr = NULL;
+}
+
+/*
+ * Resize the surface to the given size.
+ *
+ */
+void draw_util_surface_set_size(surface_t *surface, int width, int height) {
+    surface->width = width;
+    surface->height = height;
+    cairo_xcb_surface_set_size(surface->surface, width, height);
 }
 
 /*
@@ -65,15 +84,30 @@ void draw_util_surface_free(surface_t *surface) {
  *
  */
 color_t draw_util_hex_to_color(const char *color) {
-    char groups[3][3] = {
+    if (strlen(color) < 6 || color[0] != '#') {
+        ELOG("Could not parse color: %s\n", color);
+        return draw_util_hex_to_color("#A9A9A9");
+    }
+
+    char alpha[2];
+    if (strlen(color) == strlen("#rrggbbaa")) {
+        alpha[0] = color[7];
+        alpha[1] = color[8];
+    } else {
+        alpha[0] = alpha[1] = 'F';
+    }
+
+    char groups[4][3] = {
         {color[1], color[2], '\0'},
         {color[3], color[4], '\0'},
-        {color[5], color[6], '\0'}};
+        {color[5], color[6], '\0'},
+        {alpha[0], alpha[1], '\0'}};
 
     return (color_t){
         .red = strtol(groups[0], NULL, 16) / 255.0,
         .green = strtol(groups[1], NULL, 16) / 255.0,
         .blue = strtol(groups[2], NULL, 16) / 255.0,
+        .alpha = strtol(groups[3], NULL, 16) / 255.0,
         .colorpixel = get_colorpixel(color)};
 }
 
@@ -82,44 +116,39 @@ color_t draw_util_hex_to_color(const char *color) {
  *
  */
 static void draw_util_set_source_color(surface_t *surface, color_t color) {
-#ifdef CAIRO_SUPPORT
-    cairo_set_source_rgb(surface->cr, color.red, color.green, color.blue);
-#else
-    uint32_t colorpixel = color.colorpixel;
-    xcb_change_gc(xcb_connection, surface->gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
-                  (uint32_t[]){colorpixel, colorpixel});
-#endif
+    RETURN_UNLESS_SURFACE_INITIALIZED(surface);
+
+    cairo_set_source_rgba(surface->cr, color.red, color.green, color.blue, color.alpha);
 }
 
-/**
+/*
  * Draw the given text using libi3.
  * This function also marks the surface dirty which is needed if other means of
  * drawing are used. This will be the case when using XCB to draw text.
  *
  */
 void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) {
-#ifdef CAIRO_SUPPORT
+    RETURN_UNLESS_SURFACE_INITIALIZED(surface);
+
     /* Flush any changes before we draw the text as this might use XCB directly. */
     CAIRO_SURFACE_FLUSH(surface->surface);
-#endif
 
-    set_font_colors(surface->gc, fg_color.colorpixel, bg_color.colorpixel);
-    draw_text(text, surface->id, surface->gc, visual_type, x, y, max_width);
+    set_font_colors(surface->gc, fg_color, bg_color);
+    draw_text(text, surface->id, surface->gc, surface->visual_type, x, y, max_width);
 
-#ifdef CAIRO_SUPPORT
     /* Notify cairo that we (possibly) used another way to draw on the surface. */
     cairo_surface_mark_dirty(surface->surface);
-#endif
 }
 
-/**
+/*
  * Draws a filled rectangle.
  * This function is a convenience wrapper and takes care of flushing the
  * surface as well as restoring the cairo state.
  *
  */
 void draw_util_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h) {
-#ifdef CAIRO_SUPPORT
+    RETURN_UNLESS_SURFACE_INITIALIZED(surface);
+
     cairo_save(surface->cr);
 
     /* Using the SOURCE operator will copy both color and alpha information directly
@@ -136,20 +165,15 @@ void draw_util_rectangle(surface_t *surface, color_t color, double x, double y,
     CAIRO_SURFACE_FLUSH(surface->surface);
 
     cairo_restore(surface->cr);
-#else
-    draw_util_set_source_color(surface, color);
-
-    xcb_rectangle_t rect = {x, y, w, h};
-    xcb_poly_fill_rectangle(xcb_connection, surface->id, surface->gc, 1, &rect);
-#endif
 }
 
-/**
+/*
  * Clears a surface with the given color.
  *
  */
 void draw_util_clear_surface(surface_t *surface, color_t color) {
-#ifdef CAIRO_SUPPORT
+    RETURN_UNLESS_SURFACE_INITIALIZED(surface);
+
     cairo_save(surface->cr);
 
     /* Using the SOURCE operator will copy both color and alpha information directly
@@ -165,21 +189,17 @@ void draw_util_clear_surface(surface_t *surface, color_t color) {
     CAIRO_SURFACE_FLUSH(surface->surface);
 
     cairo_restore(surface->cr);
-#else
-    draw_util_set_source_color(surface, color);
-
-    xcb_rectangle_t rect = {0, 0, surface->width, surface->height};
-    xcb_poly_fill_rectangle(xcb_connection, surface->id, surface->gc, 1, &rect);
-#endif
 }
 
-/**
+/*
  * Copies a surface onto another surface.
  *
  */
 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) {
-#ifdef CAIRO_SUPPORT
+    RETURN_UNLESS_SURFACE_INITIALIZED(src);
+    RETURN_UNLESS_SURFACE_INITIALIZED(dest);
+
     cairo_save(dest->cr);
 
     /* Using the SOURCE operator will copy both color and alpha information directly
@@ -197,8 +217,4 @@ void draw_util_copy_surface(surface_t *src, surface_t *dest, double src_x, doubl
     CAIRO_SURFACE_FLUSH(dest->surface);
 
     cairo_restore(dest->cr);
-#else
-    xcb_copy_area(xcb_connection, src->id, dest->id, dest->gc, (int16_t)src_x, (int16_t)src_y,
-                  (int16_t)dest_x, (int16_t)dest_y, (uint16_t)width, (uint16_t)height);
-#endif
 }