Make colorpixels independent from clients

This commit is contained in:
Michael Stapelberg 2009-03-05 02:58:12 +01:00
parent e4627bcaed
commit 56393c823e
5 changed files with 34 additions and 56 deletions

View File

@ -236,11 +236,6 @@ struct Client {
xcb_gcontext_t titlegc; /* The titlebars graphic context inside the frame */ xcb_gcontext_t titlegc; /* The titlebars graphic context inside the frame */
xcb_window_t child; /* The clients window */ xcb_window_t child; /* The clients window */
/* Cache of colorpixels for this client */
/* TODO: Couldnt we move them outside here, as they should only depend on the
root window? */
SLIST_HEAD(colorpixel_head, Colorpixel) colorpixels;
/* The following entry provides the necessary list pointers to use Client with LIST_* macros */ /* The following entry provides the necessary list pointers to use Client with LIST_* macros */
CIRCLEQ_ENTRY(Client) clients; CIRCLEQ_ENTRY(Client) clients;
SLIST_ENTRY(Client) dock_clients; SLIST_ENTRY(Client) dock_clients;

View File

@ -35,7 +35,7 @@ enum { _NET_SUPPORTED = 0,
}; };
i3Font *load_font(xcb_connection_t *conn, const char *pattern); i3Font *load_font(xcb_connection_t *conn, const char *pattern);
uint32_t get_colorpixel(xcb_connection_t *conn, Client *client, xcb_window_t window, char *hex); uint32_t get_colorpixel(xcb_connection_t *conn, char *hex);
xcb_window_t create_window(xcb_connection_t *conn, Rect r, uint16_t window_class, int cursor, xcb_window_t create_window(xcb_connection_t *conn, Rect r, uint16_t window_class, int cursor,
uint32_t mask, uint32_t *values); uint32_t mask, uint32_t *values);
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

@ -242,7 +242,7 @@ int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_
XCB_CURSOR_SB_V_DOUBLE_ARROW : XCB_CURSOR_SB_V_DOUBLE_ARROW :
XCB_CURSOR_SB_H_DOUBLE_ARROW), 0, NULL); XCB_CURSOR_SB_H_DOUBLE_ARROW), 0, NULL);
uint32_t values[1] = {get_colorpixel(conn, NULL, helpwin, "#4c7899")}; uint32_t values[1] = {get_colorpixel(conn, "#4c7899")};
xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(conn, helpwin, XCB_CW_BACK_PIXEL, values); xcb_void_cookie_t cookie = xcb_change_window_attributes_checked(conn, helpwin, XCB_CW_BACK_PIXEL, values);
check_error(conn, cookie, "Could not change window attributes (background color)"); check_error(conn, cookie, "Could not change window attributes (background color)");
@ -421,15 +421,6 @@ int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_noti
return 0; return 0;
} }
/* Free the clients colorpixel cache */
struct Colorpixel *colorpixel;
while (!SLIST_EMPTY(&(client->colorpixels))) {
colorpixel = SLIST_FIRST(&(client->colorpixels));
SLIST_REMOVE_HEAD(&(client->colorpixels), colorpixels);
free(colorpixel->hex);
free(colorpixel);
}
if (client->name != NULL) if (client->name != NULL)
free(client->name); free(client->name);
@ -534,7 +525,7 @@ int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *
decorate_window(conn, client, client->frame, client->titlegc, 0); decorate_window(conn, client, client->frame, client->titlegc, 0);
else { else {
xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND,
get_colorpixel(conn, client, client->frame, "#285577")); get_colorpixel(conn, "#285577"));
xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height}; xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect); xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);

View File

@ -114,16 +114,16 @@ void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t draw
if (client->container->currently_focused == client) { if (client->container->currently_focused == client) {
/* Distinguish if the window is currently focused… */ /* Distinguish if the window is currently focused… */
if (CUR_CELL->currently_focused == client) if (CUR_CELL->currently_focused == client)
background_color = get_colorpixel(conn, client, client->frame, "#285577"); background_color = get_colorpixel(conn, "#285577");
/* …or if it is the focused window in a not focused container */ /* …or if it is the focused window in a not focused container */
else background_color = get_colorpixel(conn, client, client->frame, "#555555"); else background_color = get_colorpixel(conn, "#555555");
text_color = get_colorpixel(conn, client, client->frame, "#ffffff"); text_color = get_colorpixel(conn, "#ffffff");
border_color = get_colorpixel(conn, client, client->frame, "#4c7899"); border_color = get_colorpixel(conn, "#4c7899");
} else { } else {
background_color = get_colorpixel(conn, client, client->frame, "#222222"); background_color = get_colorpixel(conn, "#222222");
text_color = get_colorpixel(conn, client, client->frame, "#888888"); text_color = get_colorpixel(conn, "#888888");
border_color = get_colorpixel(conn, client, client->frame, "#333333"); border_color = get_colorpixel(conn, "#333333");
} }
/* Our plan is the following: /* Our plan is the following:
@ -360,15 +360,15 @@ static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int wid
black; black;
char label[3]; char label[3];
black = get_colorpixel(conn, NULL, screen->bar, "#000000"); black = get_colorpixel(conn, "#000000");
background_color[SET_NORMAL] = get_colorpixel(conn, NULL, screen->bar, "#222222"); background_color[SET_NORMAL] = get_colorpixel(conn, "#222222");
text_color[SET_NORMAL] = get_colorpixel(conn, NULL, screen->bar, "#888888"); text_color[SET_NORMAL] = get_colorpixel(conn, "#888888");
border_color[SET_NORMAL] = get_colorpixel(conn, NULL, screen->bar, "#333333"); border_color[SET_NORMAL] = get_colorpixel(conn, "#333333");
background_color[SET_FOCUSED] = get_colorpixel(conn, NULL, screen->bar, "#285577"); background_color[SET_FOCUSED] = get_colorpixel(conn, "#285577");
text_color[SET_FOCUSED] = get_colorpixel(conn, NULL, screen->bar, "#ffffff"); text_color[SET_FOCUSED] = get_colorpixel(conn, "#ffffff");
border_color[SET_FOCUSED] = get_colorpixel(conn, NULL, screen->bar, "#4c7899"); border_color[SET_FOCUSED] = get_colorpixel(conn, "#4c7899");
/* Fill the whole bar in black */ /* Fill the whole bar in black */
xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, black); xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, black);

View File

@ -21,6 +21,7 @@
#include "xcb.h" #include "xcb.h"
TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached_fonts); TAILQ_HEAD(cached_fonts_head, Font) cached_fonts = TAILQ_HEAD_INITIALIZER(cached_fonts);
SLIST_HEAD(colorpixel_head, Colorpixel) colorpixels;
/* /*
* Loads a font for usage, getting its height. This function is used very often, so it * Loads a font for usage, getting its height. This function is used very often, so it
@ -66,20 +67,17 @@ i3Font *load_font(xcb_connection_t *conn, const char *pattern) {
* *
* The hex_color has to start with #, for example #FF00FF. * The hex_color has to start with #, for example #FF00FF.
* *
* The client argument is optional. If it is given, the colorpixel will be cached.
*
* NOTE that get_colorpixel() does _NOT_ check the given color code for validity. * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
* This has to be done by the caller. * This has to be done by the caller.
* *
*/ */
uint32_t get_colorpixel(xcb_connection_t *conn, Client *client, xcb_window_t window, char *hex) { uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) {
/* Lookup this colorpixel in the cache if a client was specified */ /* Lookup this colorpixel in the cache */
if (client != NULL) { struct Colorpixel *colorpixel;
struct Colorpixel *pixel; SLIST_FOREACH(colorpixel, &(colorpixels), colorpixels)
SLIST_FOREACH(pixel, &(client->colorpixels), colorpixels) if (strcmp(colorpixel->hex, hex) == 0)
if (strcmp(pixel->hex, hex) == 0) return colorpixel->pixel;
return pixel->pixel;
}
#define RGB_8_TO_16(i) (65535 * ((i) & 0xFF) / 255) #define RGB_8_TO_16(i) (65535 * ((i) & 0xFF) / 255)
char strgroups[3][3] = {{hex[1], hex[2], '\0'}, char strgroups[3][3] = {{hex[1], hex[2], '\0'},
{hex[3], hex[4], '\0'}, {hex[3], hex[4], '\0'},
@ -89,13 +87,10 @@ uint32_t get_colorpixel(xcb_connection_t *conn, Client *client, xcb_window_t win
RGB_8_TO_16(strtol(strgroups[2], NULL, 16))}; RGB_8_TO_16(strtol(strgroups[2], NULL, 16))};
xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data; xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
xcb_alloc_color_reply_t *reply;
xcb_colormap_t colormap_id = xcb_generate_id(conn); reply = xcb_alloc_color_reply(conn, xcb_alloc_color(conn, root_screen->default_colormap,
xcb_void_cookie_t cookie = xcb_create_colormap_checked(conn, XCB_COLORMAP_ALLOC_NONE, rgb16[0], rgb16[1], rgb16[2]), NULL);
colormap_id, window, root_screen->root_visual);
check_error(conn, cookie, "Could not create colormap");
xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(conn,
xcb_alloc_color(conn, colormap_id, rgb16[0], rgb16[1], rgb16[2]), NULL);
if (!reply) { if (!reply) {
printf("Could not allocate color\n"); printf("Could not allocate color\n");
@ -104,16 +99,13 @@ uint32_t get_colorpixel(xcb_connection_t *conn, Client *client, xcb_window_t win
uint32_t pixel = reply->pixel; uint32_t pixel = reply->pixel;
free(reply); free(reply);
xcb_free_colormap(conn, colormap_id);
/* Store the result in the cache if a client was specified */ /* Store the result in the cache */
if (client != NULL) {
struct Colorpixel *cache_pixel = scalloc(sizeof(struct Colorpixel)); struct Colorpixel *cache_pixel = scalloc(sizeof(struct Colorpixel));
cache_pixel->hex = sstrdup(hex); cache_pixel->hex = sstrdup(hex);
cache_pixel->pixel = pixel; cache_pixel->pixel = pixel;
SLIST_INSERT_HEAD(&(client->colorpixels), cache_pixel, colorpixels); SLIST_INSERT_HEAD(&(colorpixels), cache_pixel, colorpixels);
}
return pixel; return pixel;
} }