From: Michael Stapelberg Date: Sun, 23 Oct 2011 16:38:21 +0000 (+0100) Subject: Move get_colorpixel to libi3, use it everywhere else X-Git-Tag: 4.1~68 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=cb9bbcfccff7b27e4a2cbe5e9b2bdbd6e5260bb2;p=i3%2Fi3 Move get_colorpixel to libi3, use it everywhere else --- diff --git a/i3-config-wizard/main.c b/i3-config-wizard/main.c index d3f9bcbf..2355750f 100644 --- a/i3-config-wizard/main.c +++ b/i3-config-wizard/main.c @@ -137,7 +137,7 @@ static char *resolve_tilde(const char *path) { static int handle_expose() { /* re-draw the background */ xcb_rectangle_t border = {0, 0, 300, (15*font_height) + 8}; - xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000")); + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#000000")); xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border); xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id); @@ -146,7 +146,7 @@ static int handle_expose() { if (current_step == STEP_WELCOME) { /* restore font color */ - xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF")); + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#FFFFFF")); txt(10, 2, "You have not configured i3 yet."); txt(10, 3, "Do you want me to generate ~/.i3/config?"); @@ -154,16 +154,16 @@ static int handle_expose() { txt(85, 7, "No, I will use the defaults"); /* green */ - xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#00FF00")); + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#00FF00")); txt(25, 5, ""); /* red */ - xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000")); + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#FF0000")); txt(31, 7, ""); } if (current_step == STEP_GENERATE) { - xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF")); + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#FFFFFF")); txt(10, 2, "Please choose either:"); txt(85, 4, "Win as default modifier"); @@ -185,13 +185,13 @@ static int handle_expose() { /* green */ uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_FONT; - uint32_t values[] = { get_colorpixel(conn, "#00FF00"), font_id }; + uint32_t values[] = { get_colorpixel("#00FF00"), font_id }; xcb_change_gc(conn, pixmap_gc, mask, values); txt(25, 9, ""); /* red */ - xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000")); + xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel("#FF0000")); txt(31, 10, ""); } diff --git a/i3-config-wizard/xcb.c b/i3-config-wizard/xcb.c index 461dfd52..438c6507 100644 --- a/i3-config-wizard/xcb.c +++ b/i3-config-wizard/xcb.c @@ -32,26 +32,6 @@ void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t ma xcb_change_gc(conn, gc, mask, &value); } -/* - * Returns the colorpixel to use for the given hex color (think of HTML). - * - * The hex_color has to start with #, for example #FF00FF. - * - * NOTE that get_colorpixel() does _NOT_ check the given color code for validity. - * This has to be done by the caller. - * - */ -uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) { - char strgroups[3][3] = {{hex[1], hex[2], '\0'}, - {hex[3], hex[4], '\0'}, - {hex[5], hex[6], '\0'}}; - uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)), - (strtol(strgroups[1], NULL, 16)), - (strtol(strgroups[2], NULL, 16))}; - - return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2]; -} - /* * Returns the mask for Mode_switch (to be used for looking up keysymbols by * keycode). diff --git a/i3-config-wizard/xcb.h b/i3-config-wizard/xcb.h index 40aeec88..748fb1db 100644 --- a/i3-config-wizard/xcb.h +++ b/i3-config-wizard/xcb.h @@ -11,7 +11,6 @@ extern unsigned int xcb_numlock_mask; void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value); -uint32_t get_colorpixel(xcb_connection_t *conn, char *hex); uint32_t get_mod_mask(xcb_connection_t *conn, uint32_t keycode); xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height); int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height); diff --git a/i3-input/i3-input.h b/i3-input/i3-input.h index 934ada03..300cb335 100644 --- a/i3-input/i3-input.h +++ b/i3-input/i3-input.h @@ -16,7 +16,6 @@ extern xcb_window_t root; char *convert_ucs_to_utf8(char *input); char *convert_utf8_to_ucs2(char *input, int *real_strlen); -uint32_t get_colorpixel(xcb_connection_t *conn, char *hex); uint32_t get_mod_mask(xcb_connection_t *conn, uint32_t keycode); xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height); int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height); diff --git a/i3-input/main.c b/i3-input/main.c index 931afc2b..bd4592e7 100644 --- a/i3-input/main.c +++ b/i3-input/main.c @@ -92,13 +92,13 @@ static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t /* re-draw the background */ xcb_rectangle_t border = {0, 0, 500, font_height + 8}, inner = {2, 2, 496, font_height + 8 - 4}; - xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel(conn, "#FF0000") }); + xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#FF0000") }); xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border); - xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel(conn, "#000000") }); + xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#000000") }); xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner); /* restore font color */ - xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel(conn, "#FFFFFF") }); + xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#FFFFFF") }); uint8_t *con = concat_strings(glyphs_ucs, input_position); char *full_text = (char*)con; if (prompt != NULL) { diff --git a/i3-input/xcb.c b/i3-input/xcb.c index 0c7e9434..ae2dcd1b 100644 --- a/i3-input/xcb.c +++ b/i3-input/xcb.c @@ -20,26 +20,6 @@ #include "i3-input.h" -/* - * Returns the colorpixel to use for the given hex color (think of HTML). - * - * The hex_color has to start with #, for example #FF00FF. - * - * NOTE that get_colorpixel() does _NOT_ check the given color code for validity. - * This has to be done by the caller. - * - */ -uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) { - char strgroups[3][3] = {{hex[1], hex[2], '\0'}, - {hex[3], hex[4], '\0'}, - {hex[5], hex[6], '\0'}}; - uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)), - (strtol(strgroups[1], NULL, 16)), - (strtol(strgroups[2], NULL, 16))}; - - return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2]; -} - /* * Returns the mask for Mode_switch (to be used for looking up keysymbols by * keycode). diff --git a/i3-nagbar/Makefile b/i3-nagbar/Makefile index fed223db..05a5b911 100644 --- a/i3-nagbar/Makefile +++ b/i3-nagbar/Makefile @@ -3,6 +3,8 @@ TOPDIR=.. include $(TOPDIR)/common.mk +CPPFLAGS += -I$(TOPDIR)/include + # Depend on the object files of all source-files in src/*.c and on all header files FILES=$(patsubst %.c,%.o,$(wildcard *.c)) HEADERS=$(wildcard *.h) @@ -14,9 +16,12 @@ HEADERS=$(wildcard *.h) all: i3-nagbar -i3-nagbar: ${FILES} +i3-nagbar: $(TOPDIR)/libi3/libi3.a ${FILES} echo "[i3-nagbar] LINK i3-nagbar" - $(CC) $(LDFLAGS) -o $@ ${FILES} $(LIBS) + $(CC) $(LDFLAGS) -o $@ $(filter-out libi3/libi3.a,$^) $(LIBS) + +$(TOPDIR)/libi3/%.a: $(TOPDIR)/libi3/*.c + $(MAKE) -C $(TOPDIR)/libi3 install: all echo "[i3-nagbar] INSTALL" diff --git a/i3-nagbar/i3-nagbar.h b/i3-nagbar/i3-nagbar.h index 2fbe3cbb..04131e4b 100644 --- a/i3-nagbar/i3-nagbar.h +++ b/i3-nagbar/i3-nagbar.h @@ -18,7 +18,6 @@ while (0) extern xcb_window_t root; -uint32_t get_colorpixel(xcb_connection_t *conn, char *hex); xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height); int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height); void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value); diff --git a/i3-nagbar/main.c b/i3-nagbar/main.c index 254aedfb..0a3e5150 100644 --- a/i3-nagbar/main.c +++ b/i3-nagbar/main.c @@ -28,6 +28,7 @@ #include #include +#include "libi3.h" #include "i3-nagbar.h" typedef struct { @@ -287,18 +288,18 @@ int main(int argc, char *argv[]) { if (bar_type == TYPE_ERROR) { /* Red theme for error messages */ - color_button_background = get_colorpixel(conn, "#680a0a"); - color_background = get_colorpixel(conn, "#900000"); - color_text = get_colorpixel(conn, "#ffffff"); - color_border = get_colorpixel(conn, "#d92424"); - color_border_bottom = get_colorpixel(conn, "#470909"); + color_button_background = get_colorpixel("#680a0a"); + color_background = get_colorpixel("#900000"); + color_text = get_colorpixel("#ffffff"); + color_border = get_colorpixel("#d92424"); + color_border_bottom = get_colorpixel("#470909"); } else { /* Yellowish theme for warnings */ - color_button_background = get_colorpixel(conn, "#ffc100"); - color_background = get_colorpixel(conn, "#ffa8000"); - color_text = get_colorpixel(conn, "#000000"); - color_border = get_colorpixel(conn, "#ab7100"); - color_border_bottom = get_colorpixel(conn, "#ab7100"); + color_button_background = get_colorpixel("#ffc100"); + color_background = get_colorpixel("#ffa8000"); + color_text = get_colorpixel("#000000"); + color_border = get_colorpixel("#ab7100"); + color_border_bottom = get_colorpixel("#ab7100"); } uint32_t font_id = get_font_id(conn, pattern, &font_height); diff --git a/i3-nagbar/xcb.c b/i3-nagbar/xcb.c index ed1bfd89..7148133d 100644 --- a/i3-nagbar/xcb.c +++ b/i3-nagbar/xcb.c @@ -28,26 +28,6 @@ void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t ma xcb_change_gc(conn, gc, mask, &value); } -/* - * Returns the colorpixel to use for the given hex color (think of HTML). - * - * The hex_color has to start with #, for example #FF00FF. - * - * NOTE that get_colorpixel() does _NOT_ check the given color code for validity. - * This has to be done by the caller. - * - */ -uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) { - char strgroups[3][3] = {{hex[1], hex[2], '\0'}, - {hex[3], hex[4], '\0'}, - {hex[5], hex[6], '\0'}}; - uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)), - (strtol(strgroups[1], NULL, 16)), - (strtol(strgroups[2], NULL, 16))}; - - return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2]; -} - /* * Opens the window we use for input/output and maps it * diff --git a/i3bar/src/xcb.c b/i3bar/src/xcb.c index b5b79ae2..076fe791 100644 --- a/i3bar/src/xcb.c +++ b/i3bar/src/xcb.c @@ -194,21 +194,6 @@ void draw_text(xcb_drawable_t drawable, xcb_gcontext_t ctx, int16_t x, int16_t y } } -/* - * Converts a colorstring to a colorpixel as expected from xcb_change_gc. - * s is assumed to be in the format "rrggbb" - * - */ -uint32_t get_colorpixel(const char *s) { - char strings[3][3] = { { s[0], s[1], '\0'} , - { s[2], s[3], '\0'} , - { s[4], s[5], '\0'} }; - uint8_t r = strtol(strings[0], NULL, 16); - uint8_t g = strtol(strings[1], NULL, 16); - uint8_t b = strtol(strings[2], NULL, 16); - return (r << 16 | g << 8 | b); -} - /* * Redraws the statusline to the buffer * diff --git a/include/libi3.h b/include/libi3.h index 9e9c4012..797ba424 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -93,4 +93,20 @@ int ipc_recv_message(int sockfd, uint32_t message_type, */ void fake_configure_notify(xcb_connection_t *conn, xcb_rectangle_t r, xcb_window_t window, int border_width); +/** + * Returns the colorpixel to use for the given hex color (think of HTML). Only + * works for true-color (vast majority of cases) at the moment, avoiding a + * roundtrip to X11. + * + * The hex_color has to start with #, for example #FF00FF. + * + * NOTE that get_colorpixel() does _NOT_ check the given color code for validity. + * This has to be done by the caller. + * + * NOTE that this function may in the future rely on a global xcb_connection_t + * variable called 'conn' to be present. + * + */ +uint32_t get_colorpixel(const char *hex) __attribute__((const)); + #endif diff --git a/include/xcb.h b/include/xcb.h index 65e4e6c3..ef6ed197 100644 --- a/include/xcb.h +++ b/include/xcb.h @@ -60,17 +60,6 @@ extern unsigned int xcb_numlock_mask; */ i3Font load_font(const char *pattern, bool fallback); -/** - * Returns the colorpixel to use for the given hex color (think of HTML). - * - * The hex_color has to start with #, for example #FF00FF. - * - * NOTE that get_colorpixel() does _NOT_ check the given color code for - * validity. This has to be done by the caller. - * - */ -uint32_t get_colorpixel(char *hex); - /** * Convenience wrapper around xcb_create_window which takes care of depth, * generating an ID and checking for errors. diff --git a/libi3/get_colorpixel.c b/libi3/get_colorpixel.c new file mode 100644 index 00000000..bdd9f5a5 --- /dev/null +++ b/libi3/get_colorpixel.c @@ -0,0 +1,39 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3 - an improved dynamic tiling window manager + * + * © 2009-2011 Michael Stapelberg and contributors + * + * See file LICENSE for license information. + * + */ +#include +#include + +#include "libi3.h" + +/* + * Returns the colorpixel to use for the given hex color (think of HTML). Only + * works for true-color (vast majority of cases) at the moment, avoiding a + * roundtrip to X11. + * + * The hex_color has to start with #, for example #FF00FF. + * + * NOTE that get_colorpixel() does _NOT_ check the given color code for validity. + * This has to be done by the caller. + * + * NOTE that this function may in the future rely on a global xcb_connection_t + * variable called 'conn' to be present. + * + */ +uint32_t get_colorpixel(const char *hex) { + char strgroups[3][3] = {{hex[1], hex[2], '\0'}, + {hex[3], hex[4], '\0'}, + {hex[5], hex[6], '\0'}}; + uint8_t r = strtol(strgroups[0], NULL, 16); + uint8_t g = strtol(strgroups[1], NULL, 16); + uint8_t b = strtol(strgroups[2], NULL, 16); + + return (r << 16 | g << 8 | b); +} diff --git a/libi3/get_socket_path.c b/libi3/get_socket_path.c index 9e02ec51..839bcd42 100644 --- a/libi3/get_socket_path.c +++ b/libi3/get_socket_path.c @@ -16,6 +16,8 @@ #include #include +#include "libi3.h" + /* * Try to get the socket path from X11 and return NULL if it doesn’t work. * diff --git a/libi3/ipc_connect.c b/libi3/ipc_connect.c index 2c41fcc2..d19d2fab 100644 --- a/libi3/ipc_connect.c +++ b/libi3/ipc_connect.c @@ -15,6 +15,8 @@ #include #include +#include "libi3.h" + /* * Connects to the i3 IPC socket and returns the file descriptor for the * socket. die()s if anything goes wrong. diff --git a/libi3/ipc_send_message.c b/libi3/ipc_send_message.c index ff395ada..05b18747 100644 --- a/libi3/ipc_send_message.c +++ b/libi3/ipc_send_message.c @@ -16,6 +16,8 @@ #include +#include "libi3.h" + /* * Formats a message (payload) of the given size and type and sends it to i3 via * the given socket file descriptor. diff --git a/libi3/safewrappers.c b/libi3/safewrappers.c index 2403578d..ec62aa34 100644 --- a/libi3/safewrappers.c +++ b/libi3/safewrappers.c @@ -14,6 +14,7 @@ #include #include +#include "libi3.h" /* * The s* functions (safe) are wrappers around malloc, strdup, …, which exits if one of diff --git a/src/xcb.c b/src/xcb.c index 858e4985..577c023d 100644 --- a/src/xcb.c +++ b/src/xcb.c @@ -63,26 +63,6 @@ i3Font load_font(const char *pattern, bool fallback) { return new; } -/* - * Returns the colorpixel to use for the given hex color (think of HTML). - * - * The hex_color has to start with #, for example #FF00FF. - * - * NOTE that get_colorpixel() does _NOT_ check the given color code for validity. - * This has to be done by the caller. - * - */ -uint32_t get_colorpixel(char *hex) { - char strgroups[3][3] = {{hex[1], hex[2], '\0'}, - {hex[3], hex[4], '\0'}, - {hex[5], hex[6], '\0'}}; - uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)), - (strtol(strgroups[1], NULL, 16)), - (strtol(strgroups[2], NULL, 16))}; - - return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2]; -} - /* * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking * for errors.