You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
322 lines
10 KiB
322 lines
10 KiB
7 years ago
|
# From: https://github.com/i3/i3/compare/4.14...mickael9:window-icons.diff
|
||
8 years ago
|
|
||
7 years ago
|
# Add support for window icons in the i3 title bar.
|
||
|
# This makes it easier to distinguish windows without reading their names,
|
||
|
# and makes it possible to use browsers like qutebrowser in tabbed mode.
|
||
|
|
||
7 years ago
|
+++ include/atoms_rest.xmacro
|
||
8 years ago
|
@@ -1,6 +1,7 @@
|
||
|
xmacro(_NET_WM_USER_TIME)
|
||
|
xmacro(_NET_STARTUP_ID)
|
||
|
xmacro(_NET_WORKAREA)
|
||
|
+xmacro(_NET_WM_ICON)
|
||
|
xmacro(WM_PROTOCOLS)
|
||
|
xmacro(WM_DELETE_WINDOW)
|
||
|
xmacro(UTF8_STRING)
|
||
7 years ago
|
+++ include/data.h
|
||
|
@@ -470,6 +470,11 @@
|
||
8 years ago
|
|
||
|
/* aspect ratio from WM_NORMAL_HINTS (MPlayer uses this for example) */
|
||
|
double aspect_ratio;
|
||
|
+
|
||
8 years ago
|
+ /** Window icon, as array of ARGB pixels */
|
||
8 years ago
|
+ uint32_t* icon;
|
||
8 years ago
|
+ int icon_width;
|
||
|
+ int icon_height;
|
||
8 years ago
|
};
|
||
|
|
||
|
/**
|
||
7 years ago
|
+++ include/libi3.h
|
||
|
@@ -602,6 +602,11 @@
|
||
8 years ago
|
void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width);
|
||
|
|
||
7 years ago
|
/**
|
||
8 years ago
|
+ * Draw the given image using libi3.
|
||
|
+ */
|
||
8 years ago
|
+void draw_util_image(unsigned char *src, int src_width, int src_height, surface_t *surface, int x, int y, int width, int height);
|
||
8 years ago
|
+
|
||
7 years ago
|
+/**
|
||
8 years ago
|
* Draws a filled rectangle.
|
||
|
* This function is a convenience wrapper and takes care of flushing the
|
||
7 years ago
|
* surface as well as restoring the cairo state.
|
||
|
+++ include/window.h
|
||
|
@@ -89,3 +89,9 @@
|
||
8 years ago
|
*
|
||
|
*/
|
||
|
void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style);
|
||
|
+
|
||
|
+/**
|
||
|
+ * Updates the _NET_WM_ICON
|
||
|
+ *
|
||
|
+ */
|
||
|
+void window_update_icon(i3Window *win, xcb_get_property_reply_t *prop);
|
||
7 years ago
|
+++ libi3/draw_util.c
|
||
|
@@ -140,6 +140,42 @@
|
||
8 years ago
|
cairo_surface_mark_dirty(surface->surface);
|
||
8 years ago
|
}
|
||
|
|
||
|
+
|
||
|
+/**
|
||
|
+ * Draw the given image using libi3.
|
||
|
+ * This function is a convenience wrapper and takes care of flushing the
|
||
|
+ * surface as well as restoring the cairo state.
|
||
|
+ *
|
||
|
+ */
|
||
8 years ago
|
+void draw_util_image(unsigned char *src, int src_width, int src_height, surface_t *surface, int x, int y, int width, int height) {
|
||
8 years ago
|
+ RETURN_UNLESS_SURFACE_INITIALIZED(surface);
|
||
|
+
|
||
8 years ago
|
+ double scale;
|
||
|
+
|
||
|
+ cairo_save(surface->cr);
|
||
8 years ago
|
+
|
||
8 years ago
|
+ cairo_surface_t *image;
|
||
|
+
|
||
8 years ago
|
+ image = cairo_image_surface_create_for_data(
|
||
8 years ago
|
+ src,
|
||
8 years ago
|
+ CAIRO_FORMAT_ARGB32,
|
||
8 years ago
|
+ src_width,
|
||
|
+ src_height,
|
||
|
+ src_width * 4);
|
||
|
+
|
||
|
+ cairo_translate(surface->cr, x, y);
|
||
|
+
|
||
|
+ scale = MIN((double)width / src_width, (double)height / src_height);
|
||
|
+ cairo_scale(surface->cr, scale, scale);
|
||
8 years ago
|
+
|
||
8 years ago
|
+ cairo_set_source_surface(surface->cr, image, 0, 0);
|
||
|
+ cairo_paint(surface->cr);
|
||
8 years ago
|
+
|
||
|
+ cairo_surface_destroy(image);
|
||
|
+
|
||
|
+ cairo_restore(surface->cr);
|
||
|
+}
|
||
|
+
|
||
|
/**
|
||
|
* Draws a filled rectangle.
|
||
|
* This function is a convenience wrapper and takes care of flushing the
|
||
7 years ago
|
+++ src/handlers.c
|
||
|
@@ -1402,6 +1402,19 @@
|
||
8 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
+static bool handle_windowicon_change(void *data, xcb_connection_t *conn, uint8_t state,
|
||
|
+ xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
|
||
|
+ Con *con;
|
||
|
+ if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
|
||
|
+ return false;
|
||
|
+
|
||
|
+ window_update_icon(con->window, prop);
|
||
|
+
|
||
|
+ x_push_changes(croot);
|
||
|
+
|
||
|
+ return true;
|
||
|
+}
|
||
|
+
|
||
|
/* Returns false if the event could not be processed (e.g. the window could not
|
||
|
* be found), true otherwise */
|
||
|
typedef bool (*cb_property_handler_t)(void *data, xcb_connection_t *c, uint8_t state, xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *property);
|
||
7 years ago
|
@@ -1423,7 +1436,8 @@
|
||
8 years ago
|
{0, 128, handle_class_change},
|
||
|
{0, UINT_MAX, handle_strut_partial_change},
|
||
8 years ago
|
{0, UINT_MAX, handle_window_type},
|
||
|
- {0, 5 * sizeof(uint64_t), handle_motif_hints_change}};
|
||
|
+ {0, 5 * sizeof(uint64_t), handle_motif_hints_change},
|
||
8 years ago
|
+ {0, UINT_MAX, handle_windowicon_change}};
|
||
|
#define NUM_HANDLERS (sizeof(property_handlers) / sizeof(struct property_handler_t))
|
||
|
|
||
|
/*
|
||
7 years ago
|
@@ -1445,6 +1459,7 @@
|
||
8 years ago
|
property_handlers[8].atom = A__NET_WM_STRUT_PARTIAL;
|
||
|
property_handlers[9].atom = A__NET_WM_WINDOW_TYPE;
|
||
8 years ago
|
property_handlers[10].atom = A__MOTIF_WM_HINTS;
|
||
|
+ property_handlers[11].atom = A__NET_WM_ICON;
|
||
8 years ago
|
}
|
||
|
|
||
|
static void property_notify(uint8_t state, xcb_window_t window, xcb_atom_t atom) {
|
||
7 years ago
|
+++ src/manage.c
|
||
|
@@ -91,6 +91,8 @@
|
||
8 years ago
|
role_cookie, startup_id_cookie, wm_hints_cookie,
|
||
|
wm_normal_hints_cookie, motif_wm_hints_cookie, wm_user_time_cookie, wm_desktop_cookie;
|
||
|
|
||
|
+ xcb_get_property_cookie_t wm_icon_cookie;
|
||
|
+
|
||
|
geomc = xcb_get_geometry(conn, d);
|
||
|
|
||
|
/* Check if the window is mapped (it could be not mapped when intializing and
|
||
7 years ago
|
@@ -162,6 +164,7 @@
|
||
8 years ago
|
motif_wm_hints_cookie = GET_PROPERTY(A__MOTIF_WM_HINTS, 5 * sizeof(uint64_t));
|
||
|
wm_user_time_cookie = GET_PROPERTY(A__NET_WM_USER_TIME, UINT32_MAX);
|
||
|
wm_desktop_cookie = GET_PROPERTY(A__NET_WM_DESKTOP, UINT32_MAX);
|
||
|
+ wm_icon_cookie = GET_PROPERTY(A__NET_WM_ICON, UINT32_MAX);
|
||
|
|
||
|
DLOG("Managing window 0x%08x\n", window);
|
||
|
|
||
7 years ago
|
@@ -177,6 +180,7 @@
|
||
8 years ago
|
window_update_class(cwindow, xcb_get_property_reply(conn, class_cookie, NULL), true);
|
||
|
window_update_name_legacy(cwindow, xcb_get_property_reply(conn, title_cookie, NULL), true);
|
||
|
window_update_name(cwindow, xcb_get_property_reply(conn, utf8_title_cookie, NULL), true);
|
||
|
+ window_update_icon(cwindow, xcb_get_property_reply(conn, wm_icon_cookie, NULL));
|
||
|
window_update_leader(cwindow, xcb_get_property_reply(conn, leader_cookie, NULL));
|
||
|
window_update_transient_for(cwindow, xcb_get_property_reply(conn, transient_cookie, NULL));
|
||
|
window_update_strut_partial(cwindow, xcb_get_property_reply(conn, strut_cookie, NULL));
|
||
7 years ago
|
@@ -185,6 +189,8 @@
|
||
8 years ago
|
window_update_hints(cwindow, xcb_get_property_reply(conn, wm_hints_cookie, NULL), &urgency_hint);
|
||
|
border_style_t motif_border_style = BS_NORMAL;
|
||
|
window_update_motif_hints(cwindow, xcb_get_property_reply(conn, motif_wm_hints_cookie, NULL), &motif_border_style);
|
||
|
+
|
||
|
+
|
||
|
xcb_size_hints_t wm_size_hints;
|
||
|
if (!xcb_icccm_get_wm_size_hints_reply(conn, wm_normal_hints_cookie, &wm_size_hints, NULL))
|
||
|
memset(&wm_size_hints, '\0', sizeof(xcb_size_hints_t));
|
||
7 years ago
|
+++ src/render.c
|
||
|
@@ -125,6 +125,10 @@
|
||
8 years ago
|
/* find the height for the decorations */
|
||
|
params.deco_height = render_deco_height();
|
||
|
|
||
|
+ /* minimum decoration height to allow icon to fit
|
||
8 years ago
|
+ * not actually required, icon would be cropped otherwise */
|
||
8 years ago
|
+ params.deco_height = (params.deco_height < 16) ? 16 : params.deco_height;
|
||
|
+
|
||
|
/* precalculate the sizes to be able to correct rounding errors */
|
||
|
params.sizes = precalculate_sizes(con, ¶ms);
|
||
|
|
||
7 years ago
|
+++ src/window.c
|
||
|
@@ -17,6 +17,7 @@
|
||
8 years ago
|
FREE(win->class_class);
|
||
|
FREE(win->class_instance);
|
||
|
i3string_free(win->name);
|
||
|
+ FREE(win->icon);
|
||
|
FREE(win->ran_assignments);
|
||
|
FREE(win);
|
||
|
}
|
||
7 years ago
|
@@ -365,3 +366,75 @@
|
||
8 years ago
|
#undef MWM_DECOR_BORDER
|
||
|
#undef MWM_DECOR_TITLE
|
||
|
}
|
||
|
+
|
||
|
+void window_update_icon(i3Window *win, xcb_get_property_reply_t *prop)
|
||
|
+{
|
||
|
+ uint32_t *data = NULL;
|
||
|
+ uint64_t len = 0;
|
||
|
+
|
||
8 years ago
|
+ if (!prop || prop->type != XCB_ATOM_CARDINAL || prop->format != 32) {
|
||
8 years ago
|
+ DLOG("_NET_WM_ICON is not set\n");
|
||
|
+ FREE(prop);
|
||
|
+ return;
|
||
|
+ }
|
||
|
+
|
||
|
+ uint32_t prop_value_len = xcb_get_property_value_length(prop);
|
||
|
+ uint32_t *prop_value = (uint32_t *) xcb_get_property_value(prop);
|
||
|
+
|
||
|
+ /* Find the number of icons in the reply. */
|
||
|
+ while (prop_value_len > (sizeof(uint32_t) * 2) && prop_value &&
|
||
|
+ prop_value[0] && prop_value[1])
|
||
|
+ {
|
||
|
+ /* Check that the property is as long as it should be (in bytes),
|
||
|
+ handling integer overflow. "+2" to handle the width and height
|
||
|
+ fields. */
|
||
|
+ const uint64_t crt_len = prop_value[0] * (uint64_t) prop_value[1];
|
||
|
+ const uint64_t expected_len = (crt_len + 2) * 4;
|
||
|
+
|
||
|
+ if (expected_len > prop_value_len) {
|
||
|
+ break;
|
||
|
+ }
|
||
|
+
|
||
|
+ if (len == 0 || (crt_len >= 16*16 && crt_len < len)) {
|
||
|
+ len = crt_len;
|
||
8 years ago
|
+ data = prop_value;
|
||
8 years ago
|
+ }
|
||
|
+ if (len == 16*16) {
|
||
8 years ago
|
+ break; /* found 16 pixels icon */
|
||
8 years ago
|
+ }
|
||
|
+
|
||
|
+ /* Find pointer to next icon in the reply. */
|
||
|
+ prop_value_len -= expected_len;
|
||
|
+ prop_value = (uint32_t *) (((uint8_t *) prop_value) + expected_len);
|
||
|
+ }
|
||
|
+
|
||
|
+ if (!data) {
|
||
|
+ DLOG("Could not get _NET_WM_ICON\n");
|
||
|
+ FREE(prop);
|
||
|
+ return;
|
||
|
+ }
|
||
|
+
|
||
|
+ LOG("Got _NET_WM_ICON of size: (%d,%d)\n", data[0], data[1]);
|
||
8 years ago
|
+ win->name_x_changed = true; /* trigger a redraw */
|
||
8 years ago
|
+
|
||
8 years ago
|
+ win->icon_width = data[0];
|
||
|
+ win->icon_height = data[1];
|
||
8 years ago
|
+ win->icon = srealloc(win->icon, len * 4);
|
||
8 years ago
|
+
|
||
8 years ago
|
+ for (uint64_t i = 0; i < len; i++) {
|
||
|
+ uint8_t r, g, b, a;
|
||
|
+ a = (data[2 + i] >> 24) & 0xff;
|
||
|
+ r = (data[2 + i] >> 16) & 0xff;
|
||
|
+ g = (data[2 + i] >> 8) & 0xff;
|
||
|
+ b = (data[2 + i] >> 0) & 0xff;
|
||
|
+
|
||
|
+ /* Cairo uses premultiplied alpha */
|
||
|
+ r = (r * a) / 0xff;
|
||
|
+ g = (g * a) / 0xff;
|
||
|
+ b = (b * a) / 0xff;
|
||
|
+
|
||
|
+ win->icon[i] = (a << 24) | (r << 16) | (g << 8) | b;
|
||
|
+ }
|
||
8 years ago
|
+
|
||
|
+ FREE(prop);
|
||
|
+}
|
||
7 years ago
|
+++ src/x.c
|
||
|
@@ -546,6 +546,7 @@
|
||
8 years ago
|
|
||
8 years ago
|
/* 6: draw the title */
|
||
|
int text_offset_y = (con->deco_rect.height - config.font.height) / 2;
|
||
|
+ int text_offset_x = 0;
|
||
|
|
||
|
struct Window *win = con->window;
|
||
|
if (win == NULL) {
|
||
7 years ago
|
@@ -572,6 +573,9 @@
|
||
7 years ago
|
goto after_title;
|
||
|
}
|
||
8 years ago
|
|
||
|
+ if (win->icon)
|
||
|
+ text_offset_x = 18;
|
||
8 years ago
|
+
|
||
|
int mark_width = 0;
|
||
|
if (config.show_marks && !TAILQ_EMPTY(&(con->marks_head))) {
|
||
|
char *formatted_mark = sstrdup("");
|
||
7 years ago
|
@@ -611,14 +615,32 @@
|
||
7 years ago
|
|
||
8 years ago
|
draw_util_text(title, &(parent->frame_buffer),
|
||
|
p->color->text, p->color->background,
|
||
|
- con->deco_rect.x + logical_px(2),
|
||
|
+ con->deco_rect.x + text_offset_x + logical_px(2),
|
||
|
con->deco_rect.y + text_offset_y,
|
||
|
- con->deco_rect.width - mark_width - 2 * logical_px(2));
|
||
|
+ con->deco_rect.width - text_offset_x - mark_width - 2 * logical_px(2));
|
||
7 years ago
|
|
||
|
if (con->title_format != NULL) {
|
||
8 years ago
|
I3STRING_FREE(title);
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
|
+ /* Draw the icon */
|
||
|
+ if (win->icon) {
|
||
|
+ uint16_t width = 16;
|
||
|
+ uint16_t height = 16;
|
||
|
+
|
||
|
+ int icon_offset_y = (con->deco_rect.height - height) / 2;
|
||
|
+
|
||
|
+ draw_util_image(
|
||
8 years ago
|
+ (unsigned char *)win->icon,
|
||
8 years ago
|
+ win->icon_width,
|
||
|
+ win->icon_height,
|
||
8 years ago
|
+ &(parent->frame_buffer),
|
||
8 years ago
|
+ con->deco_rect.x + logical_px(2),
|
||
8 years ago
|
+ con->deco_rect.y + icon_offset_y,
|
||
|
+ width,
|
||
|
+ height);
|
||
|
+ }
|
||
|
+
|
||
|
after_title:
|
||
|
x_draw_decoration_after_title(con, p);
|
||
|
copy_pixmaps:
|