From fd8735a6fdab6760cce712e7e127b1b2196dd841 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 13 Apr 2010 17:22:34 +0200 Subject: [PATCH] correctly update/display window title/class --- Makefile | 2 +- include/all.h | 1 + include/data.h | 7 +++- include/handlers.h | 3 +- include/i3.h | 1 + include/window.h | 7 ++++ src/con.c | 14 +++++-- src/handlers.c | 91 +++++++++------------------------------------- src/manage.c | 16 ++++---- src/nc.c | 7 ++++ src/util.c | 4 +- src/window.c | 46 +++++++++++++++++++++++ src/x.c | 10 ++--- 13 files changed, 114 insertions(+), 95 deletions(-) create mode 100644 include/window.h create mode 100644 src/window.c diff --git a/Makefile b/Makefile index 28a78064..810a7863 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ include $(TOPDIR)/common.mk # Depend on the object files of all source-files in src/*.c and on all header files AUTOGENERATED:=src/cfgparse.tab.c src/cfgparse.yy.c -FILES:=src/ipc.c src/nc.c src/log.c src/util.c src/tree.c src/xcb.c src/manage.c src/workspace.c src/x.c src/floating.c src/click.c src/config.c src/handlers.c src/randr.c src/xinerama.c src/con.c src/load_layout.c src/render.c +FILES:=src/ipc.c src/nc.c src/log.c src/util.c src/tree.c src/xcb.c src/manage.c src/workspace.c src/x.c src/floating.c src/click.c src/config.c src/handlers.c src/randr.c src/xinerama.c src/con.c src/load_layout.c src/render.c src/window.c FILES:=$(FILES:.c=.o) HEADERS:=$(filter-out include/loglevels.h,$(wildcard include/*.h)) diff --git a/include/all.h b/include/all.h index a86bd5d2..84e4a2f9 100644 --- a/include/all.h +++ b/include/all.h @@ -48,5 +48,6 @@ #include "con.h" #include "load_layout.h" #include "render.h" +#include "window.h" #endif diff --git a/include/data.h b/include/data.h index c034d115..154c0828 100644 --- a/include/data.h +++ b/include/data.h @@ -247,7 +247,12 @@ struct xoutput { struct Window { xcb_window_t id; - const char *class; + const char *class_class; + const char *class_instance; + const char *name_ucs2; + const char *name_utf8; + int name_len; + bool uses_net_wm_name; }; struct Match { diff --git a/include/handlers.h b/include/handlers.h index b92b59a4..ecfa6a53 100644 --- a/include/handlers.h +++ b/include/handlers.h @@ -107,6 +107,7 @@ int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_noti int handle_destroy_notify_event(void *data, xcb_connection_t *conn, xcb_destroy_notify_event_t *event); +#endif /** * Called when a window changes its title * @@ -114,7 +115,7 @@ int handle_destroy_notify_event(void *data, xcb_connection_t *conn, int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop); - +#if 0 /** * We handle legacy window names (titles) which are in COMPOUND_TEXT * encoding. However, we just pass them along, so when containing non-ASCII diff --git a/include/i3.h b/include/i3.h index 562c557d..e437943e 100644 --- a/include/i3.h +++ b/include/i3.h @@ -33,6 +33,7 @@ extern TAILQ_HEAD(autostarts_head, Autostart) autostarts; extern TAILQ_HEAD(assignments_head, Assignment) assignments; extern SLIST_HEAD(stack_wins_head, Stack_Window) stack_wins; extern xcb_event_handlers_t evenths; +extern xcb_property_handlers_t prophs; extern uint8_t root_depth; extern bool xkb_supported; extern xcb_atom_t atoms[NUM_ATOMS]; diff --git a/include/window.h b/include/window.h new file mode 100644 index 00000000..d2b3e9dc --- /dev/null +++ b/include/window.h @@ -0,0 +1,7 @@ +#ifndef _WINDOW_H +#define _WINDOW_H + +void window_update_class(i3Window *win, xcb_get_property_reply_t *prop); +void window_update_name(i3Window *win, xcb_get_property_reply_t *prop); + +#endif diff --git a/src/con.c b/src/con.c index 00001d4c..528a0f38 100644 --- a/src/con.c +++ b/src/con.c @@ -184,17 +184,23 @@ Con *con_by_frame_id(xcb_window_t frame) { static bool match_matches_window(Match *match, i3Window *window) { /* TODO: pcre, full matching, … */ - if (match->class != NULL && strcasecmp(match->class, window->class) == 0) { - LOG("match made by window class (%s)\n", window->class); + if (match->class != NULL && strcasecmp(match->class, window->class_class) == 0) { + LOG("match made by window class (%s)\n", window->class_class); return true; } + if (match->instance != NULL && strcasecmp(match->instance, window->class_instance) == 0) { + LOG("match made by window instance (%s)\n", window->class_instance); + return true; + } + + if (match->id != XCB_NONE && window->id == match->id) { LOG("match made by window id (%d)\n", window->id); return true; } - LOG("window %d (%s) could not be matched\n", window->id, window->class); + LOG("window %d (%s) could not be matched\n", window->id, window->class_class); return false; } @@ -208,7 +214,7 @@ Con *con_for_window(i3Window *window, Match **store_match) { Con *con; Match *match; LOG("searching con for window %p\n", window); - LOG("class == %s\n", window->class); + LOG("class == %s\n", window->class_class); TAILQ_FOREACH(con, &all_cons, all_cons) TAILQ_FOREACH(match, &(con->swallow_head), matches) { diff --git a/src/handlers.c b/src/handlers.c index c679da81..2f89afe9 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -2,10 +2,7 @@ * vim:ts=4:sw=4:expandtab * * i3 - an improved dynamic tiling window manager - * - * © 2009-2010 Michael Stapelberg and contributors - * - * See file LICENSE for license information. + * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE) * */ #include @@ -568,61 +565,24 @@ int handle_destroy_notify_event(void *data, xcb_connection_t *conn, xcb_destroy_ return handle_unmap_notify_event(NULL, conn, &unmap); } - +#endif /* * Called when a window changes its title * */ int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) { - if (prop == NULL || xcb_get_property_value_length(prop) == 0) { - DLOG("_NET_WM_NAME not specified, not changing\n"); - return 1; - } - Client *client = table_get(&by_child, window); - if (client == NULL) - return 1; - - /* Save the old pointer to make the update atomic */ - char *new_name; - int new_len; - asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)); - /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */ - char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len); - LOG("_NET_WM_NAME changed to \"%s\"\n", new_name); - free(new_name); - - /* Check if they are the same and don’t update if so. - Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes. - Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes, - but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */ - if ((new_len == client->name_len) && - (client->name != NULL) && - (memcmp(client->name, ucs2_name, new_len * 2) == 0)) { - free(ucs2_name); - return 1; - } - - char *old_name = client->name; - client->name = ucs2_name; - client->name_len = new_len; - client->uses_net_wm_name = true; - - FREE(old_name); - - /* If the client is a dock window, we don’t need to render anything */ - if (client->dock) - return 1; - - int mode = container_mode(client->container, true); - if (mode == MODE_STACK || mode == MODE_TABBED) - render_container(conn, client->container); - else decorate_window(conn, client, client->frame, client->titlegc, 0, 0); - xcb_flush(conn); - + Con *con; + if ((con = con_by_window_id(window)) == NULL || con->window == NULL) return 1; -} + window_update_name(con->window, prop); + + x_push_changes(croot); + + return 1; +} +#if 0 /* * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we * just pass them along, so when containing non-ASCII characters, those will be rendering @@ -689,6 +649,7 @@ int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t return 1; } +#endif /* * Updates the client’s WM_CLASS property @@ -696,32 +657,16 @@ int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t */ int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) { - if (prop == NULL || xcb_get_property_value_length(prop) == 0) { - DLOG("prop == NULL\n"); - return 1; - } - Client *client = table_get(&by_child, window); - if (client == NULL) - return 1; + Con *con; + if ((con = con_by_window_id(window)) == NULL || con->window == NULL) + return 1; - /* We cannot use asprintf here since this property contains two - * null-terminated strings (for compatibility reasons). Instead, we - * use strdup() on both strings */ - char *new_class = xcb_get_property_value(prop); + window_update_class(con->window, prop); - FREE(client->window_class_instance); - FREE(client->window_class_class); - - client->window_class_instance = strdup(new_class); - if ((strlen(new_class) + 1) < xcb_get_property_value_length(prop)) - client->window_class_class = strdup(new_class + strlen(new_class) + 1); - else client->window_class_class = NULL; - LOG("WM_CLASS changed to %s (instance), %s (class)\n", - client->window_class_instance, client->window_class_class); - - return 0; + return 0; } +#if 0 /* * Expose event means we should redraw our windows (= title bar) * diff --git a/src/manage.c b/src/manage.c index e15e802a..7ffcd61e 100644 --- a/src/manage.c +++ b/src/manage.c @@ -37,7 +37,6 @@ void manage_existing_windows(xcb_window_t root) { for (i = 0; i < len; ++i) manage_window(children[i], cookies[i], true); - free(reply); free(cookies); } @@ -119,16 +118,18 @@ void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cooki goto out; LOG("reparenting!\n"); + uint32_t mask = 0; + uint32_t values[1]; + mask = XCB_CW_EVENT_MASK; + values[0] = CHILD_EVENT_MASK; + xcb_change_window_attributes(conn, window, mask, values); i3Window *cwindow = scalloc(sizeof(i3Window)); cwindow->id = window; - class_cookie = xcb_get_any_property_unchecked(conn, false, window, WM_CLASS, 128); - xcb_get_property_reply_t *preply; - preply = xcb_get_property_reply(conn, class_cookie, NULL); - if (preply == NULL || xcb_get_property_value_length(preply) == 0) { - LOG("cannot get wm_class\n"); - } else cwindow->class = strdup(xcb_get_property_value(preply)); + /* update as much information as possible so far (some replies may be NULL) */ + window_update_class(cwindow, xcb_get_property_reply(conn, class_cookie, NULL)); + window_update_name(cwindow, xcb_get_property_reply(conn, utf8_title_cookie, NULL)); Con *nc; Match *match; @@ -176,7 +177,6 @@ void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cooki geom->border_width); #endif - /* Generate callback events for every property we watch */ free(geom); out: free(attr); diff --git a/src/nc.c b/src/nc.c index f5843d55..7b2b126a 100644 --- a/src/nc.c +++ b/src/nc.c @@ -14,6 +14,7 @@ char **start_argv; xcb_connection_t *conn; xcb_event_handlers_t evenths; +xcb_property_handlers_t prophs; xcb_atom_t atoms[NUM_ATOMS]; xcb_window_t root; @@ -295,8 +296,11 @@ int main(int argc, char *argv[]) { REQUEST_ATOM(_NET_ACTIVE_WINDOW); REQUEST_ATOM(_NET_WORKAREA); + memset(&evenths, 0, sizeof(xcb_event_handlers_t)); + memset(&prophs, 0, sizeof(xcb_property_handlers_t)); xcb_event_handlers_init(conn, &evenths); + xcb_property_handlers_init(&prophs, &evenths); xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL); xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL); @@ -308,6 +312,7 @@ int main(int argc, char *argv[]) { xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL); + /* Setup NetWM atoms */ #define GET_ATOM(name) \ do { \ @@ -342,6 +347,8 @@ int main(int argc, char *argv[]) { GET_ATOM(_NET_ACTIVE_WINDOW); GET_ATOM(_NET_WORKAREA); + xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL); + keysyms = xcb_key_symbols_alloc(conn); xcb_get_numlock_mask(conn); diff --git a/src/util.c b/src/util.c index e381aa52..24f99d6a 100644 --- a/src/util.c +++ b/src/util.c @@ -21,7 +21,7 @@ #include "all.h" -//static iconv_t conversion_descriptor = 0; +static iconv_t conversion_descriptor = 0; struct keyvalue_table_head by_parent = TAILQ_HEAD_INITIALIZER(by_parent); struct keyvalue_table_head by_child = TAILQ_HEAD_INITIALIZER(by_child); @@ -158,7 +158,6 @@ void check_error(xcb_connection_t *conn, xcb_void_cookie_t cookie, char *err_mes } } -#if 0 /* * Converts the given string to UCS-2 big endian for use with * xcb_image_text_16(). The amount of real glyphs is stored in real_strlen, @@ -202,6 +201,7 @@ char *convert_utf8_to_ucs2(char *input, int *real_strlen) { return buffer; } +#if 0 /* * Returns the client which comes next in focus stack (= was selected before) for diff --git a/src/window.c b/src/window.c new file mode 100644 index 00000000..ac6f45a0 --- /dev/null +++ b/src/window.c @@ -0,0 +1,46 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3 - an improved dynamic tiling window manager + * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE) + * + */ +#include "all.h" + +void window_update_class(i3Window *win, xcb_get_property_reply_t *prop) { + if (prop == NULL || xcb_get_property_value_length(prop) == 0) { + DLOG("empty property, not updating\n"); + return; + } + + /* We cannot use asprintf here since this property contains two + * null-terminated strings (for compatibility reasons). Instead, we + * use strdup() on both strings */ + char *new_class = xcb_get_property_value(prop); + + FREE(win->class_instance); + FREE(win->class_class); + + win->class_instance = strdup(new_class); + if ((strlen(new_class) + 1) < xcb_get_property_value_length(prop)) + win->class_class = strdup(new_class + strlen(new_class) + 1); + else win->class_class = NULL; + LOG("WM_CLASS changed to %s (instance), %s (class)\n", + win->class_instance, win->class_class); +} + +void window_update_name(i3Window *win, xcb_get_property_reply_t *prop) { + if (prop == NULL || xcb_get_property_value_length(prop) == 0) { + DLOG("_NET_WM_NAME not specified, not changing\n"); + return 1; + } + + /* Save the old pointer to make the update atomic */ + int new_len; + asprintf(&win->name_utf8, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)); + /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */ + win->name_ucs2 = convert_utf8_to_ucs2(win->name_utf8, &win->name_len); + LOG("_NET_WM_NAME changed to \"%s\"\n", win->name_utf8); + + win->uses_net_wm_name = true; +} diff --git a/src/x.c b/src/x.c index 2ed8fd11..ee0bd07d 100644 --- a/src/x.c +++ b/src/x.c @@ -157,23 +157,23 @@ void x_draw_decoration(Con *con) { return; } - if (con->window->class == NULL) { + if (con->window->name_ucs2 == NULL) { LOG("not rendering decoration, not yet known\n"); return; } - LOG("should render text %s onto %p / %s\n", con->window->class, parent, parent->name); + LOG("should render text %s onto %p / %s\n", con->window->name_utf8, parent, parent->name); xcb_change_gc_single(conn, parent->gc, XCB_GC_FOREGROUND, get_colorpixel("#FFFFFF")); - xcb_image_text_8( + xcb_image_text_16( conn, - strlen(con->window->class), + con->window->name_len, parent->frame, parent->gc, con->deco_rect.x, con->deco_rect.y + 14, - con->window->class + (xcb_char2b_t*)con->window->name_ucs2 ); }