Move get_colorpixel to libi3, use it everywhere else

This commit is contained in:
Michael Stapelberg 2011-10-23 17:38:21 +01:00
parent 0086bcb2b4
commit cb9bbcfccf
19 changed files with 90 additions and 131 deletions

View File

@ -137,7 +137,7 @@ static char *resolve_tilde(const char *path) {
static int handle_expose() { static int handle_expose() {
/* re-draw the background */ /* re-draw the background */
xcb_rectangle_t border = {0, 0, 300, (15*font_height) + 8}; 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_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font_id); 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) { if (current_step == STEP_WELCOME) {
/* restore font color */ /* 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, 2, "You have not configured i3 yet.");
txt(10, 3, "Do you want me to generate ~/.i3/config?"); 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"); txt(85, 7, "No, I will use the defaults");
/* green */ /* 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, "<Enter>"); txt(25, 5, "<Enter>");
/* red */ /* 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, "<ESC>"); txt(31, 7, "<ESC>");
} }
if (current_step == STEP_GENERATE) { 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(10, 2, "Please choose either:");
txt(85, 4, "Win as default modifier"); txt(85, 4, "Win as default modifier");
@ -185,13 +185,13 @@ static int handle_expose() {
/* green */ /* green */
uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_FONT; 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); xcb_change_gc(conn, pixmap_gc, mask, values);
txt(25, 9, "<Enter>"); txt(25, 9, "<Enter>");
/* red */ /* 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, "<ESC>"); txt(31, 10, "<ESC>");
} }

View File

@ -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); 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 * Returns the mask for Mode_switch (to be used for looking up keysymbols by
* keycode). * keycode).

View File

@ -11,7 +11,6 @@
extern unsigned int xcb_numlock_mask; 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); 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); 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); 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); int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height);

View File

@ -16,7 +16,6 @@ extern xcb_window_t root;
char *convert_ucs_to_utf8(char *input); char *convert_ucs_to_utf8(char *input);
char *convert_utf8_to_ucs2(char *input, int *real_strlen); 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); 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); 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); int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height);

View File

@ -92,13 +92,13 @@ static int handle_expose(void *data, xcb_connection_t *conn, xcb_expose_event_t
/* re-draw the background */ /* re-draw the background */
xcb_rectangle_t border = {0, 0, 500, font_height + 8}, inner = {2, 2, 496, font_height + 8 - 4}; 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_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); xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
/* restore font color */ /* 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); uint8_t *con = concat_strings(glyphs_ucs, input_position);
char *full_text = (char*)con; char *full_text = (char*)con;
if (prompt != NULL) { if (prompt != NULL) {

View File

@ -20,26 +20,6 @@
#include "i3-input.h" #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 * Returns the mask for Mode_switch (to be used for looking up keysymbols by
* keycode). * keycode).

View File

@ -3,6 +3,8 @@ TOPDIR=..
include $(TOPDIR)/common.mk 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 # Depend on the object files of all source-files in src/*.c and on all header files
FILES=$(patsubst %.c,%.o,$(wildcard *.c)) FILES=$(patsubst %.c,%.o,$(wildcard *.c))
HEADERS=$(wildcard *.h) HEADERS=$(wildcard *.h)
@ -14,9 +16,12 @@ HEADERS=$(wildcard *.h)
all: i3-nagbar all: i3-nagbar
i3-nagbar: ${FILES} i3-nagbar: $(TOPDIR)/libi3/libi3.a ${FILES}
echo "[i3-nagbar] LINK i3-nagbar" 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 install: all
echo "[i3-nagbar] INSTALL" echo "[i3-nagbar] INSTALL"

View File

@ -18,7 +18,6 @@ while (0)
extern xcb_window_t root; 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); 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); 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); void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value);

View File

@ -28,6 +28,7 @@
#include <xcb/xcb_aux.h> #include <xcb/xcb_aux.h>
#include <xcb/xcb_event.h> #include <xcb/xcb_event.h>
#include "libi3.h"
#include "i3-nagbar.h" #include "i3-nagbar.h"
typedef struct { typedef struct {
@ -287,18 +288,18 @@ int main(int argc, char *argv[]) {
if (bar_type == TYPE_ERROR) { if (bar_type == TYPE_ERROR) {
/* Red theme for error messages */ /* Red theme for error messages */
color_button_background = get_colorpixel(conn, "#680a0a"); color_button_background = get_colorpixel("#680a0a");
color_background = get_colorpixel(conn, "#900000"); color_background = get_colorpixel("#900000");
color_text = get_colorpixel(conn, "#ffffff"); color_text = get_colorpixel("#ffffff");
color_border = get_colorpixel(conn, "#d92424"); color_border = get_colorpixel("#d92424");
color_border_bottom = get_colorpixel(conn, "#470909"); color_border_bottom = get_colorpixel("#470909");
} else { } else {
/* Yellowish theme for warnings */ /* Yellowish theme for warnings */
color_button_background = get_colorpixel(conn, "#ffc100"); color_button_background = get_colorpixel("#ffc100");
color_background = get_colorpixel(conn, "#ffa8000"); color_background = get_colorpixel("#ffa8000");
color_text = get_colorpixel(conn, "#000000"); color_text = get_colorpixel("#000000");
color_border = get_colorpixel(conn, "#ab7100"); color_border = get_colorpixel("#ab7100");
color_border_bottom = get_colorpixel(conn, "#ab7100"); color_border_bottom = get_colorpixel("#ab7100");
} }
uint32_t font_id = get_font_id(conn, pattern, &font_height); uint32_t font_id = get_font_id(conn, pattern, &font_height);

View File

@ -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); 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 * Opens the window we use for input/output and maps it
* *

View File

@ -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 * Redraws the statusline to the buffer
* *

View File

@ -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); 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 #endif

View File

@ -60,17 +60,6 @@ extern unsigned int xcb_numlock_mask;
*/ */
i3Font load_font(const char *pattern, bool fallback); 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, * Convenience wrapper around xcb_create_window which takes care of depth,
* generating an ID and checking for errors. * generating an ID and checking for errors.

39
libi3/get_colorpixel.c Normal file
View File

@ -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 <stdlib.h>
#include <stdint.h>
#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);
}

View File

@ -16,6 +16,8 @@
#include <xcb/xcb.h> #include <xcb/xcb.h>
#include <xcb/xcb_aux.h> #include <xcb/xcb_aux.h>
#include "libi3.h"
/* /*
* Try to get the socket path from X11 and return NULL if it doesnt work. * Try to get the socket path from X11 and return NULL if it doesnt work.
* *

View File

@ -15,6 +15,8 @@
#include <err.h> #include <err.h>
#include <stdlib.h> #include <stdlib.h>
#include "libi3.h"
/* /*
* Connects to the i3 IPC socket and returns the file descriptor for the * Connects to the i3 IPC socket and returns the file descriptor for the
* socket. die()s if anything goes wrong. * socket. die()s if anything goes wrong.

View File

@ -16,6 +16,8 @@
#include <i3/ipc.h> #include <i3/ipc.h>
#include "libi3.h"
/* /*
* Formats a message (payload) of the given size and type and sends it to i3 via * Formats a message (payload) of the given size and type and sends it to i3 via
* the given socket file descriptor. * the given socket file descriptor.

View File

@ -14,6 +14,7 @@
#include <stdio.h> #include <stdio.h>
#include <err.h> #include <err.h>
#include "libi3.h"
/* /*
* The s* functions (safe) are wrappers around malloc, strdup, , which exits if one of * The s* functions (safe) are wrappers around malloc, strdup, , which exits if one of

View File

@ -63,26 +63,6 @@ i3Font load_font(const char *pattern, bool fallback) {
return new; 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 * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking
* for errors. * for errors.