]> git.sur5r.net Git - i3/i3/commitdiff
cache text_width for named workspaces, fix memory leak
authorMichael Stapelberg <michael@stapelberg.de>
Sun, 2 Aug 2009 19:32:35 +0000 (21:32 +0200)
committerMichael Stapelberg <michael@stapelberg.de>
Sun, 2 Aug 2009 19:32:35 +0000 (21:32 +0200)
include/data.h
src/layout.c
src/workspace.c
src/xcb.c

index 5bca3ae7a8f1a56d3b504c68efad0d9a476b4806..4ced21f800518165fa108a0028d6db09afb997cf 100644 (file)
@@ -171,6 +171,9 @@ struct Workspace {
         /** Length of the workspace’s name (in glyphs) */
         int name_len;
 
+        /** Width of the workspace’s name (in pixels) rendered in config.font */
+        int text_width;
+
         /** x, y, width, height */
         Rect rect;
 
index 2dbcf71cdd53f3c2429901a36fdee694cfffa105..d094ca42145ac06c3ee65a78fb9cad831f6a2e2e 100644 (file)
@@ -436,21 +436,18 @@ static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int wid
                                              &(config.bar.unfocused));
                 Workspace *ws = &workspaces[c];
 
-                /* Calculate the length of a string in a given font */
-                int text_width = predict_text_width(conn, config.font, ws->name, ws->name_len);
-
                 /* Draw the outer rect */
                 xcb_draw_rect(conn, screen->bar, screen->bargc, color->border,
                               drawn,              /* x */
                               1,                  /* y */
-                              text_width + 5 + 5, /* width = text width + 5 px left + 5px right */
+                              ws->text_width + 5 + 5, /* width = text width + 5 px left + 5px right */
                               height - 2          /* height = max. height - 1 px upper and 1 px bottom border */);
 
                 /* Draw the background of this rect */
                 xcb_draw_rect(conn, screen->bar, screen->bargc, color->background,
                               drawn + 1,
                               2,
-                              text_width + 4 + 4,
+                              ws->text_width + 4 + 4,
                               height - 4);
 
                 xcb_change_gc_single(conn, screen->bargc, XCB_GC_FOREGROUND, color->text);
@@ -458,7 +455,7 @@ static void render_internal_bar(xcb_connection_t *conn, Workspace *r_ws, int wid
                 xcb_image_text_16(conn, ws->name_len, screen->bar, screen->bargc, drawn + 5 /* X */,
                                   font->height + 1 /* Y = baseline of font */,
                                   (xcb_char2b_t*)ws->name);
-                drawn += text_width + 12;
+                drawn += ws->text_width + 12;
         }
 
         LOG("done rendering internal\n");
index 40a5692a724c17643945ea1a69a72086225bc4d7..d3de73e45e560b4087033b31dae9af977c2e518a 100644 (file)
@@ -16,6 +16,9 @@
 
 #include "util.h"
 #include "data.h"
+#include "i3.h"
+#include "config.h"
+#include "xcb.h"
 
 /*
  * Sets the name (or just its number) for the given workspace. This has to
@@ -38,6 +41,7 @@ void workspace_set_name(Workspace *ws, const char *name) {
         FREE(ws->name);
 
         ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
+        ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
 
         free(label);
 }
index ff0c32d00291e021aec4e776a2ca0a4df0672716..3f5d42800254679b6cb07e36ea1f8211aa36a4ec 100644 (file)
--- a/src/xcb.c
+++ b/src/xcb.c
@@ -357,5 +357,7 @@ int predict_text_width(xcb_connection_t *conn, const char *font_pattern, char *t
                 width += info->character_width;
         }
 
+        free(font_info);
+
         return width;
 }