More documentation, cleanups, and a cache for get_colorpixel()

This commit is contained in:
Michael Stapelberg
2009-02-24 14:18:08 +01:00
parent 2dcf4ad62f
commit aa18ca0889
7 changed files with 147 additions and 68 deletions

View File

@ -204,7 +204,7 @@ int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_
}
xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT, 0, NULL);
uint32_t values[1] = {get_colorpixel(conn, helpwin, "#4c7899")};
uint32_t values[1] = {get_colorpixel(conn, NULL, helpwin, "#4c7899")};
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)");

View File

@ -37,17 +37,13 @@ int get_unoccupied_x(Workspace *workspace, int row) {
for (int cols = 0; cols < workspace->cols;) {
Container *con = workspace->table[cols][row];
printf("oh hai. wf[%d][%d] = %f\n", cols, row, con->width_factor);
printf("colspan = %d\n", con->colspan);
printf("width_factor[%d][%d] = %f, colspan = %d\n", cols, row, con->width_factor, con->colspan);
if (con->width_factor == 0)
unoccupied -= workspace->rect.width * default_factor * con->colspan;
cols += con->colspan;
}
/* TODO: Check (as soon as the whole implementation is done) if this case does ever occur,
because this function only gets called when at least one client was resized */
if (unoccupied == 0)
unoccupied = workspace->rect.width;
assert(unoccupied != 0);
printf("unoccupied space: %d\n", unoccupied);
return unoccupied;
@ -62,24 +58,21 @@ int get_unoccupied_y(Workspace *workspace, int col) {
for (int rows = 0; rows < workspace->rows;) {
Container *con = workspace->table[col][rows];
printf("oh hai. wf[%d][%d] = %f\n", col, rows, con->height_factor);
printf("rowspan = %d\n", con->rowspan);
printf("height_factor[%d][%d] = %f, rowspan %d\n", col, rows, con->height_factor, con->rowspan);
if (con->height_factor == 0)
unoccupied -= workspace->rect.height * default_factor * con->rowspan;
rows += con->rowspan;
}
/* TODO: Check (as soon as the whole implementation is done) if this case does ever occur,
because this function only gets called when at least one client was resized */
if (unoccupied == 0)
unoccupied = workspace->rect.height;
assert(unoccupied != 0);
printf("unoccupied space: %d\n", unoccupied);
return unoccupied;
}
/*
* (Re-)draws window decorations for a given Client
* (Re-)draws window decorations for a given Client onto the given drawable/graphic context.
* When in stacking mode, the window decorations are drawn onto an own window.
*
*/
void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
@ -93,13 +86,13 @@ void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t draw
return;
if (client->container->currently_focused == client) {
background_color = get_colorpixel(conn, client->frame, "#285577");
text_color = get_colorpixel(conn, client->frame, "#ffffff");
border_color = get_colorpixel(conn, client->frame, "#4c7899");
background_color = get_colorpixel(conn, client, client->frame, "#285577");
text_color = get_colorpixel(conn, client, client->frame, "#ffffff");
border_color = get_colorpixel(conn, client, client->frame, "#4c7899");
} else {
background_color = get_colorpixel(conn, client->frame, "#222222");
text_color = get_colorpixel(conn, client->frame, "#888888");
border_color = get_colorpixel(conn, client->frame, "#333333");
background_color = get_colorpixel(conn, client, client->frame, "#222222");
text_color = get_colorpixel(conn, client, client->frame, "#888888");
border_color = get_colorpixel(conn, client, client->frame, "#333333");
}
/* Our plan is the following:
@ -110,7 +103,6 @@ void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t draw
/* Draw a green rectangle around the window */
xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
printf("drawing at offset %d\n", offset);
xcb_rectangle_t rect = {0, offset, client->rect.width, offset + client->rect.height};
xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
@ -128,7 +120,6 @@ void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t draw
/* TODO: utf8? */
char *label;
asprintf(&label, "(%08x) %.*s", client->frame, client->name_len, client->name);
printf("label is %s\n", label);
xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(conn, strlen(label), drawable,
gc, 3 /* X */, offset + font->height /* Y = baseline of font */, label);
check_error(conn, text_cookie, "Could not draw client's title");

View File

@ -58,6 +58,12 @@ void *smalloc(size_t size) {
return result;
}
void *scalloc(size_t size) {
void *result = calloc(size, 1);
exit_if_null(result, "Too less memory for calloc(%d)\n", size);
return result;
}
char *sstrdup(const char *str) {
char *result = strdup(str);
exit_if_null(result, "Too less memory for strdup()\n");
@ -157,13 +163,14 @@ void set_focus(xcb_connection_t *conn, Client *client) {
void switch_layout_mode(xcb_connection_t *conn, Container *container, int mode) {
if (mode == MODE_STACK) {
/* When entering stacking mode, we need to open a window on which we can draw the
title bars of the clients */
Rect rect = {container->x, container->y, container->width, 15 /* TODO: exact */ };
title bars of the clients, it has height 1 because we dont bother here with
calculating the correct height - it will be adjusted when rendering anyways. */
Rect rect = {container->x, container->y, container->width, 1 };
/* Dont generate events for our new window, it should *not* be managed */
uint32_t mask = 0;
uint32_t values[2];
/* Dont generate events for our new window, it should *not* be managed */
mask |= XCB_CW_OVERRIDE_REDIRECT;
values[0] = 1;
@ -250,14 +257,15 @@ void toggle_fullscreen(xcb_connection_t *conn, Client *client) {
values[0], values[1], values[2], values[3]);
/* Raise the window */
xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, client->frame);
values[0] = XCB_STACK_MODE_ABOVE;
xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
xcb_configure_window(conn, client->frame, mask, values);
xcb_configure_window(conn, client->child, mask, values);
xcb_flush(conn);
} else {
printf("left fullscreen\n");
printf("leaving fullscreen mode\n");
/* Because the coordinates of the window havent changed, it would not be
re-configured if we dont set the following flag */
client->force_reconfigure = true;

View File

@ -13,6 +13,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
@ -23,12 +24,20 @@
*
* 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.
* This has to be done by the caller.
*
*/
uint32_t get_colorpixel(xcb_connection_t *conn, xcb_window_t window, char *hex) {
/* TODO: We need to store the colorpixels per child to remove these unnecessary requests every time */
uint32_t get_colorpixel(xcb_connection_t *conn, Client *client, xcb_window_t window, char *hex) {
/* Lookup this colorpixel in the cache if a client was specified */
if (client != NULL) {
struct Colorpixel *pixel;
SLIST_FOREACH(pixel, &(client->colorpixels), colorpixels)
if (strcmp(pixel->hex, hex) == 0)
return pixel->pixel;
}
#define RGB_8_TO_16(i) (65535 * ((i) & 0xFF) / 255)
char strgroups[3][3] = {{hex[1], hex[2], '\0'},
{hex[3], hex[4], '\0'},
@ -54,6 +63,16 @@ uint32_t get_colorpixel(xcb_connection_t *conn, xcb_window_t window, char *hex)
uint32_t pixel = reply->pixel;
free(reply);
xcb_free_colormap(conn, colormap_id);
/* Store the result in the cache if a client was specified */
if (client != NULL) {
struct Colorpixel *cache_pixel = scalloc(sizeof(struct Colorpixel));
cache_pixel->hex = sstrdup(hex);
cache_pixel->pixel = pixel;
SLIST_INSERT_HEAD(&(client->colorpixels), cache_pixel, colorpixels);
}
return pixel;
}