From 05747c4a1c8709924e02209be4d7a321112b637c Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 4 Mar 2009 12:09:43 +0100 Subject: [PATCH] Implement an internal bar which displays the workspaces --- include/data.h | 4 +++ include/xcb.h | 2 ++ src/layout.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++--- src/xcb.c | 16 ++++++++++++ src/xinerama.c | 45 +++++++++++++++++++++++++++------- 5 files changed, 120 insertions(+), 13 deletions(-) diff --git a/include/data.h b/include/data.h index 9b82a859..605d5bf7 100644 --- a/include/data.h +++ b/include/data.h @@ -293,6 +293,10 @@ struct Screen { /* x, y, width, height */ Rect rect; + /* The bar window */ + xcb_window_t bar; + xcb_gcontext_t bargc; + TAILQ_ENTRY(Screen) screens; }; diff --git a/include/xcb.h b/include/xcb.h index bf5daa3e..cf2863ae 100644 --- a/include/xcb.h +++ b/include/xcb.h @@ -32,5 +32,7 @@ xcb_window_t create_window(xcb_connection_t *conn, Rect r, uint16_t window_class void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value); void xcb_draw_line(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext_t gc, uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t to_x, uint32_t to_y); +void xcb_draw_rect(xcb_connection_t *connection, xcb_drawable_t drawable, xcb_gcontext_t gc, + uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t width, uint32_t height); #endif diff --git a/src/layout.c b/src/layout.c index 8b3f6e98..c122f313 100644 --- a/src/layout.c +++ b/src/layout.c @@ -320,12 +320,12 @@ void render_container(xcb_connection_t *connection, Container *container) { } } -static void render_bars(xcb_connection_t *connection, Workspace *r_ws, int width, int height) { +static void render_bars(xcb_connection_t *connection, Workspace *r_ws, int width, int *height) { Client *client; SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) { if (client->force_reconfigure | HAS_CHANGED(old_value_1, client->rect.x, 0) | - HAS_CHANGED(old_value_2, client->rect.y, height)) + HAS_CHANGED(old_value_2, client->rect.y, *height)) reposition_client(connection, client); if (client->force_reconfigure | @@ -334,12 +334,66 @@ static void render_bars(xcb_connection_t *connection, Workspace *r_ws, int width resize_client(connection, client); client->force_reconfigure = false; - height += client->desired_height; + *height += client->desired_height; } } +static void render_internal_bar(xcb_connection_t *connection, Workspace *r_ws, int width, int height) { + printf("Rendering internal bar\n"); + i3Font *font = load_font(connection, config.font); + i3Screen *screen = r_ws->screen; + enum { SET_NORMAL = 0, SET_FOCUSED = 1 }; + uint32_t background_color[2], + text_color[2], + border_color[2], + black; + char label[3]; + + black = get_colorpixel(connection, NULL, screen->bar, "#000000"); + + background_color[SET_NORMAL] = get_colorpixel(connection, NULL, screen->bar, "#222222"); + text_color[SET_NORMAL] = get_colorpixel(connection, NULL, screen->bar, "#888888"); + border_color[SET_NORMAL] = get_colorpixel(connection, NULL, screen->bar, "#333333"); + + background_color[SET_FOCUSED] = get_colorpixel(connection, NULL, screen->bar, "#285577"); + text_color[SET_FOCUSED] = get_colorpixel(connection, NULL, screen->bar, "#ffffff"); + border_color[SET_FOCUSED] = get_colorpixel(connection, NULL, screen->bar, "#4c7899"); + + /* Fill the whole bar in black */ + xcb_change_gc_single(connection, screen->bargc, XCB_GC_FOREGROUND, black); + xcb_rectangle_t rect = {0, 0, width, height}; + xcb_poly_fill_rectangle(connection, screen->bar, screen->bargc, 1, &rect); + + /* Set font */ + xcb_change_gc_single(connection, screen->bargc, XCB_GC_FONT, font->id); + + int drawn = 0; + for (int c = 0; c < 10; c++) { + if (workspaces[c].screen == screen) { + int set = (screen->current_workspace == c ? SET_FOCUSED : SET_NORMAL); + + xcb_draw_rect(connection, screen->bar, screen->bargc, border_color[set], + drawn * height, 1, height - 2, height - 2); + xcb_draw_rect(connection, screen->bar, screen->bargc, background_color[set], + drawn * height + 1, 2, height - 4, height - 4); + + snprintf(label, sizeof(label), "%d", c+1); + xcb_change_gc_single(connection, screen->bargc, XCB_GC_FOREGROUND, text_color[set]); + xcb_change_gc_single(connection, screen->bargc, XCB_GC_BACKGROUND, background_color[set]); + xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(connection, strlen(label), screen->bar, + screen->bargc, drawn * height + 5 /* X */, + font->height + 1 /* Y = baseline of font */, label); + check_error(connection, text_cookie, "Could not draw workspace title"); + drawn++; + } + } + + printf("done rendering internal\n"); +} + void render_layout(xcb_connection_t *connection) { i3Screen *screen; + i3Font *font = load_font(connection, config.font); TAILQ_FOREACH(screen, virtual_screens, screens) { /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */ @@ -358,6 +412,9 @@ void render_layout(xcb_connection_t *connection) { SLIST_FOREACH(client, &(r_ws->dock_clients), dock_clients) height -= client->desired_height; + /* Space for the internal bar */ + height -= (font->height + 6); + printf("got %d rows and %d cols\n", r_ws->rows, r_ws->cols); int xoffset[r_ws->rows]; @@ -399,7 +456,8 @@ void render_layout(xcb_connection_t *connection) { printf("==========\n"); } - render_bars(connection, r_ws, width, height); + render_bars(connection, r_ws, width, &height); + render_internal_bar(connection, r_ws, width, 18); } xcb_flush(connection); diff --git a/src/xcb.c b/src/xcb.c index b2101dcf..2f60ba70 100644 --- a/src/xcb.c +++ b/src/xcb.c @@ -76,6 +76,11 @@ uint32_t get_colorpixel(xcb_connection_t *conn, Client *client, xcb_window_t win return pixel; } +/* + * Convenience wrapper around xcb_create_window which takes care of depth, generating an ID and checking + * for errors. + * + */ xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class, uint32_t mask, uint32_t *values) { xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root; xcb_window_t result = xcb_generate_id(conn); @@ -120,3 +125,14 @@ void xcb_draw_line(xcb_connection_t *conn, xcb_drawable_t drawable, xcb_gcontext xcb_point_t points[] = {{x, y}, {to_x, to_y}}; xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, drawable, gc, 2, points); } + +/* + * Draws a rectangle from x,y with width,height using the given color + * + */ +void xcb_draw_rect(xcb_connection_t *connection, xcb_drawable_t drawable, xcb_gcontext_t gc, + uint32_t colorpixel, uint32_t x, uint32_t y, uint32_t width, uint32_t height) { + xcb_change_gc_single(connection, gc, XCB_GC_FOREGROUND, colorpixel); + xcb_rectangle_t rect = {x, y, width, height}; + xcb_poly_fill_rectangle(connection, drawable, gc, 1, &rect); +} diff --git a/src/xinerama.c b/src/xinerama.c index 8e7f7918..1d16eb9b 100644 --- a/src/xinerama.c +++ b/src/xinerama.c @@ -23,6 +23,9 @@ #include "util.h" #include "xinerama.h" #include "layout.h" +#include "xcb.h" +#include "font.h" +#include "config.h" /* This TAILQ of i3Screens stores the virtual screens, used for handling overlapping screens * (xrandr --same-as) */ @@ -125,6 +128,29 @@ static void query_screens(xcb_connection_t *connection, struct screens_head *scr free(reply); } +static void initialize_screen(xcb_connection_t *connection, i3Screen *screen, Workspace *workspace) { + i3Font *font = load_font(connection, config.font); + + workspace->screen = screen; + screen->current_workspace = workspace->num; + + /* Create a bar for each screen */ + Rect bar_rect = {screen->rect.x, + screen->rect.height - (font->height + 6), + screen->rect.x + screen->rect.width, + font->height + 6}; + uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK; + uint32_t values[] = {1, XCB_EVENT_MASK_EXPOSURE}; + screen->bar = create_window(connection, bar_rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, mask, values); + screen->bargc = xcb_generate_id(connection); + xcb_create_gc(connection, screen->bargc, screen->bar, 0, 0); + + /* Copy dimensions */ + memcpy(&(workspace->rect), &(screen->rect), sizeof(Rect)); + printf("that is virtual screen at %d x %d with %d x %d\n", + screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height); +} + /* * We have just established a connection to the X server and need the initial Xinerama * information to setup workspaces for each screen. @@ -153,11 +179,8 @@ void initialize_xinerama(xcb_connection_t *connection) { /* Just go through each workspace and associate as many screens as we can. */ TAILQ_FOREACH(s, virtual_screens, screens) { s->num = num_screens; - s->current_workspace = num_screens; - workspaces[num_screens].screen = s; - memcpy(&(workspaces[num_screens++].rect), &(s->rect), sizeof(Rect)); - printf("that is virtual screen at %d x %d with %d x %d\n", - s->rect.x, s->rect.y, s->rect.width, s->rect.height); + initialize_screen(connection, s, &(workspaces[num_screens])); + num_screens++; } } @@ -198,6 +221,10 @@ void xinerama_requery_screens(xcb_connection_t *connection) { if (screen->current_workspace == -1) screen->current_workspace = c; + /* Re-use the old bar window */ + screen->bar = workspaces[c].screen->bar; + screen->bargc = workspaces[c].screen->bargc; + /* Update the dimensions */ memcpy(&(workspaces[c].rect), &(screen->rect), sizeof(Rect)); workspaces[c].screen = screen; @@ -207,10 +234,7 @@ void xinerama_requery_screens(xcb_connection_t *connection) { for (int c = 0; c < 10; c++) if (workspaces[c].screen == NULL) { printf("fix: initializing new workspace, setting num to %d\n", c); - workspaces[c].screen = screen; - screen->current_workspace = c; - /* Copy the dimensions from the virtual screen */ - memcpy(&(workspaces[c].rect), &(screen->rect), sizeof(Rect)); + initialize_screen(connection, screen, &(workspaces[c])); break; } } @@ -221,6 +245,9 @@ void xinerama_requery_screens(xcb_connection_t *connection) { for (int c = 0; c < 10; c++) if ((workspaces[c].screen != NULL) && (workspaces[c].screen->num >= num_screens)) { + printf("Closing bar window\n"); + xcb_destroy_window(connection, workspaces[c].screen->bar); + printf("Workspace %d's screen out of bounds, assigning to first screen\n", c+1); workspaces[c].screen = first; memcpy(&(workspaces[c].rect), &(first->rect), sizeof(Rect)); -- 2.39.5