]> git.sur5r.net Git - i3/i3/commitdiff
Handle legacy window titles by rendering them not unicode-compatible.
authorMichael Stapelberg <michael+x200@stapelberg.de>
Wed, 11 Mar 2009 20:31:54 +0000 (21:31 +0100)
committerMichael Stapelberg <michael+x200@stapelberg.de>
Wed, 11 Mar 2009 20:31:54 +0000 (21:31 +0100)
include/data.h
include/handlers.h
src/handlers.c
src/layout.c
src/mainx.c

index 3ed605a612ea9c1cc5d196895448a451d7c67e2f..5c4bfb0adab4313391d846b3f239675a8ec0a09c 100644 (file)
@@ -223,6 +223,10 @@ struct Client {
 
         /* Name (= window title) */
         char *name;
+        /* name_len stores the real string length (glyphs) of the window title if the client uses
+           _NET_WM_NAME. Otherwise, it is set to -1 to indicate that name should be just passed
+           to X as 8-bit string and therefore will not be rendered correctly. This behaviour is
+           to support legacy applications which do not set _NET_WM_NAME */
         int name_len;
 
         /* fullscreen is pretty obvious */
index ac6209aea248cd601b2e84dc0e9a4da375ef1e6a..e10a00894f87820e76d0b77da4d06f618369cea8 100644 (file)
@@ -21,6 +21,8 @@ int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure
 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event);
 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);
+int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
+                                xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop);
 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event);
 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event);
 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
index 0a7b520b6c1b2b06962bcbd80999a8650aa37297..8ab8ac4dabdb4b5121e46826e71f528b7fff3f10 100644 (file)
@@ -584,8 +584,8 @@ int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_noti
 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) {
         LOG("window's name changed.\n");
-        if (prop == NULL) {
-                LOG("prop == NULL\n");
+        if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
+                LOG("_NET_WM_NAME not specified, not changing\n");
                 return 1;
         }
         Client *client = table_get(byChild, window);
@@ -632,6 +632,66 @@ int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
         return 1;
 }
 
+/*
+ * 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
+ * incorrectly. In order to correctly render unicode window titles in i3, an application
+ * has to set _NET_WM_NAME, which is in UTF-8 encoding.
+ *
+ * On every update, a message is put out to the user, so he may improve the situation and
+ * update applications which display filenames in their title to correctly use
+ * _NET_WM_NAME and therefore support unicode.
+ *
+ */
+int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
+                                xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
+        LOG("window's name changed (legacy).\n");
+        if (prop == NULL) {
+                LOG("prop == NULL\n");
+                return 1;
+        }
+        Client *client = table_get(byChild, 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 */
+        LOG("Name should change to \"%s\"\n", new_name);
+
+        /* Check if they are the same and don’t update if so. */
+        if (client->name != NULL &&
+            strlen(new_name) == strlen(client->name) &&
+            strcmp(client->name, new_name) == 0) {
+                LOG("Name did not change, not updating\n");
+                free(new_name);
+                return 1;
+        }
+
+        LOG("Using legacy window title. Note that in order to get Unicode window titles in i3,"
+            "the application has to set _NET_WM_NAME which is in UTF-8 encoding.\n");
+
+        char *old_name = client->name;
+        client->name = new_name;
+        client->name_len = -1;
+
+        if (old_name != NULL)
+                free(old_name);
+
+        /* If the client is a dock window, we don’t need to render anything */
+        if (client->dock)
+                return 1;
+
+        if (client->container->mode == MODE_STACK)
+                render_container(conn, client->container);
+        else decorate_window(conn, client, client->frame, client->titlegc, 0);
+        xcb_flush(conn);
+
+        return 1;
+}
+
 /*
  * Expose event means we should redraw our windows (= title bar)
  *
index 3783e9bb48bf64be649381fea33e16356d30c657..547b7c52f79870885fbd476a16c6f685a59ff34b 100644 (file)
@@ -169,8 +169,15 @@ void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t draw
                 uint32_t values[] = { text_color, background_color, font->id };
                 xcb_change_gc(conn, gc, mask, values);
 
-                xcb_image_text_16(conn, client->name_len, drawable, gc, 3 /* X */,
-                                  offset + font->height /* Y = baseline of font */, (xcb_char2b_t*)client->name);
+                /* name_len == -1 means this is a legacy application which does not specify _NET_WM_NAME,
+                   and we don’t handle the old window name (COMPOUND_TEXT) but only _NET_WM_NAME, which
+                   is UTF-8 */
+                if (client->name_len == -1)
+                        xcb_image_text_8(conn, strlen(client->name), drawable, gc, 3 /* X */,
+                                         offset + font->height /* Y = baseline of font */, client->name);
+                else
+                        xcb_image_text_16(conn, client->name_len, drawable, gc, 3 /* X */,
+                                          offset + font->height /* Y = baseline of font */, (xcb_char2b_t*)client->name);
         }
 }
 
index d47c89f139a90f12f0a57423c05a427d8e9feaa1..43511b0db92d4be583765b7d273004362e5d0b72 100644 (file)
@@ -105,6 +105,7 @@ void manage_window(xcb_property_handlers_t *prophs, xcb_connection_t *conn, xcb_
         if (attr && geom) {
                 reparent_window(conn, window, attr->visual, geom->root, geom->depth,
                                 geom->x, geom->y, geom->width, geom->height);
+                xcb_property_changed(prophs, XCB_PROPERTY_NEW_VALUE, window, WM_NAME);
                 xcb_property_changed(prophs, XCB_PROPERTY_NEW_VALUE, window, atoms[_NET_WM_NAME]);
         }
 
@@ -467,6 +468,9 @@ int main(int argc, char *argv[], char *env[]) {
         /* Watch _NET_WM_NAME (= title of the window in UTF-8) property */
         xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
 
+        /* Watch WM_NAME (= title of the window in compound text) property for legacy applications */
+        xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
+
         /* Set up the atoms we support */
         check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
                        ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");