summarylogtreecommitdiffstats
path: root/iconsupport.patch
blob: ac726ce1db4deef019aa5d38646136eb36ef0f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
diff -rupN i3-4.11-original/common.mk i3-4.11-patched/common.mk
--- i3-4.11-original/common.mk	2015-09-30 02:55:10.000000000 -0400
+++ i3-4.11-patched/common.mk	2015-10-25 23:59:59.028232946 -0400
@@ -1,6 +1,7 @@
 UNAME=$(shell uname)
 DEBUG=1
 INSTALL=install
+USE_ICONS=1
 LN=ln
 PKG_CONFIG=pkg-config
 ifndef PREFIX
@@ -59,6 +60,9 @@ I3_CPPFLAGS += -DMINOR_VERSION=${MINOR_V
 I3_CPPFLAGS += -DPATCH_VERSION=${PATCH_VERSION}
 I3_CPPFLAGS += -DSYSCONFDIR=\"${SYSCONFDIR}\"
 I3_CPPFLAGS += -DI3__FILE__=__FILE__
+ifeq ($(USE_ICONS),1)
+I3_CPPFLAGS += -DUSE_ICONS
+endif
 
 
 ## Libraries flags
@@ -108,6 +112,10 @@ XCB_WM_CFLAGS += $(call cflags_for_lib,
 XCB_WM_LIBS   := $(call ldflags_for_lib, xcb-icccm,xcb-icccm)
 XCB_WM_LIBS   += $(call ldflags_for_lib, xcb-xinerama,xcb-xinerama)
 XCB_WM_LIBS   += $(call ldflags_for_lib, xcb-randr,xcb-randr)
+ifeq ($(USE_ICONS),1)
+XCB_WM_LIBS   += $(call ldflags_for_lib, xcb-image,xcb-image)
+endif
+
 
 XKB_COMMON_CFLAGS := $(call cflags_for_lib, xkbcommon,xkbcommon)
 XKB_COMMON_LIBS := $(call ldflags_for_lib, xkbcommon,xkbcommon)
diff -rupN i3-4.11-original/include/atoms.xmacro i3-4.11-patched/include/atoms.xmacro
--- i3-4.11-original/include/atoms.xmacro	2015-09-30 02:55:10.000000000 -0400
+++ i3-4.11-patched/include/atoms.xmacro	2015-10-26 00:01:01.186402376 -0400
@@ -32,6 +32,7 @@ xmacro(_NET_ACTIVE_WINDOW)
 xmacro(_NET_CLOSE_WINDOW)
 xmacro(_NET_STARTUP_ID)
 xmacro(_NET_WORKAREA)
+xmacro(_NET_WM_ICON)
 xmacro(WM_PROTOCOLS)
 xmacro(WM_DELETE_WINDOW)
 xmacro(UTF8_STRING)
diff -rupN i3-4.11-original/include/data.h i3-4.11-patched/include/data.h
--- i3-4.11-original/include/data.h	2015-09-30 02:55:10.000000000 -0400
+++ i3-4.11-patched/include/data.h	2015-10-26 00:02:36.543594088 -0400
@@ -421,6 +421,12 @@ struct Window {
 
     /* aspect ratio from WM_NORMAL_HINTS (MPlayer uses this for example) */
     double aspect_ratio;
+
+#ifdef USE_ICONS
+    /* Window icon, array of size 16x16 containing the ARGB pixels */
+    uint32_t* icon;
+#endif
+
 };
 
 /**
diff -rupN i3-4.11-original/include/window.h i3-4.11-patched/include/window.h
--- i3-4.11-original/include/window.h	2015-09-30 02:55:10.000000000 -0400
+++ i3-4.11-patched/include/window.h	2015-10-26 00:04:24.660410026 -0400
@@ -69,6 +69,12 @@ void window_update_type(i3Window *window
 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint);
 
 /**
+ * Updates the _NET_WM_ICON
+ *
+ */
+void window_update_icon(i3Window *win, xcb_get_property_reply_t *prop);
+
+/**
  * Updates the MOTIF_WM_HINTS. The container's border style should be set to
  * `motif_border_style' if border style is not BS_NORMAL.
  *
diff -rupN i3-4.11-original/src/manage.c i3-4.11-patched/src/manage.c
--- i3-4.11-original/src/manage.c	2015-09-30 02:55:10.000000000 -0400
+++ i3-4.11-patched/src/manage.c	2015-10-26 00:07:52.240963404 -0400
@@ -92,6 +92,10 @@ void manage_window(xcb_window_t window,
         role_cookie, startup_id_cookie, wm_hints_cookie,
         wm_normal_hints_cookie, motif_wm_hints_cookie;
 
+    #ifdef USE_ICONS
+    xcb_get_property_cookie_t wm_icon_cookie;
+    #endif
+
     geomc = xcb_get_geometry(conn, d);
 
     /* Check if the window is mapped (it could be not mapped when intializing and
@@ -161,6 +165,9 @@ void manage_window(xcb_window_t window,
     wm_hints_cookie = xcb_icccm_get_wm_hints(conn, window);
     wm_normal_hints_cookie = xcb_icccm_get_wm_normal_hints(conn, window);
     motif_wm_hints_cookie = GET_PROPERTY(A__MOTIF_WM_HINTS, 5 * sizeof(uint64_t));
+    #ifdef USE_ICONS
+    wm_icon_cookie = xcb_get_property_unchecked(conn, false, window, A__NET_WM_ICON, XCB_ATOM_CARDINAL, 0, UINT32_MAX);
+    #endif
 
     DLOG("Managing window 0x%08x\n", window);
 
@@ -187,6 +194,9 @@ void manage_window(xcb_window_t window,
     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);
+    #ifdef USE_ICONS
+    window_update_icon(cwindow, xcb_get_property_reply(conn, wm_icon_cookie, NULL));
+    #endif
     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));
diff -rupN i3-4.11-original/src/render.c i3-4.11-patched/src/render.c
--- i3-4.11-original/src/render.c	2015-09-30 02:55:10.000000000 -0400
+++ i3-4.11-patched/src/render.c	2015-10-26 00:09:08.002065558 -0400
@@ -223,6 +223,11 @@ void render_con(Con *con, bool render_fu
 
     /* find the height for the decorations */
     int deco_height = render_deco_height();
+    #ifdef USE_ICONS
+    /* minimum decoration height to allow icon to fit
+     * not actuuly required, icon would be cropped otherwise */
+    deco_height = deco_height<16 ? 16 : deco_height;
+    #endif
 
     /* precalculate the sizes to be able to correct rounding errors */
     int sizes[children];
diff -rupN i3-4.11-original/src/tree.c i3-4.11-patched/src/tree.c
--- i3-4.11-original/src/tree.c	2015-09-30 02:55:10.000000000 -0400
+++ i3-4.11-patched/src/tree.c	2015-10-26 00:10:18.246663507 -0400
@@ -268,6 +268,9 @@ bool tree_close(Con *con, kill_window_t
         FREE(con->window->class_class);
         FREE(con->window->class_instance);
         i3string_free(con->window->name);
+        #ifdef USE_ICONS
+        FREE(con->window->icon);
+        #endif
         FREE(con->window->ran_assignments);
         FREE(con->window);
     }
diff -rupN i3-4.11-original/src/window.c i3-4.11-patched/src/window.c
--- i3-4.11-original/src/window.c	2015-09-30 02:55:10.000000000 -0400
+++ i3-4.11-patched/src/window.c	2015-10-26 00:12:58.141954559 -0400
@@ -334,6 +334,85 @@ void window_update_motif_hints(i3Window
 #undef MWM_DECOR_TITLE
 }
 
+#ifdef USE_ICONS
+/*
+ * Copy and resize icon if needed
+ */
+void copy_icon_with_resize(uint32_t *dst, int width, int height, uint32_t* src, int s_width, int s_height)
+{
+    int i, j;
+    if (width==s_width && height==s_height) {
+        /*  easy case, same dimensions, just copy data */
+        memcpy(dst, src, width*height*sizeof(uint32_t));
+    }
+    else {
+        uint32_t* row = src;
+        int xstep = s_width/width;
+        int ystep = s_height/height*s_width;
+
+        for(i=0; i < height; ++i) {
+            uint32_t* ptr = row;
+            for(j=0; j < width; ++j) {
+                *dst++ = *ptr;
+                ptr+=xstep;
+            }
+            row += ystep;
+        }
+    }
+}
+
+void window_update_icon(i3Window *win, xcb_get_property_reply_t *prop)
+{
+    uint32_t *data = NULL;
+    uint64_t len = 0;
+
+    if(!prop || prop->type != XCB_ATOM_CARDINAL || prop->format != 32) {
+       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;
+               data  = prop_value;
+       }
+       if (len==16*16) break; // found 16 pixels icon
+
+       /* 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]);
+
+    FREE(win->icon);
+    win->icon = malloc(16*16*sizeof(uint32_t));
+    copy_icon_with_resize(win->icon, 16, 16, data+2, data[0], data[1]);
+
+    free(prop);
+}
+#endif /* USE_ICONS */
+
 /*
  * Returns the window title considering the current title format.
  * If no format is set, this will simply return the window's name.
diff -rupN i3-4.11-original/src/x.c i3-4.11-patched/src/x.c
--- i3-4.11-original/src/x.c	2015-09-30 02:55:10.000000000 -0400
+++ i3-4.11-patched/src/x.c	2015-10-26 00:18:12.942683615 -0400
@@ -11,6 +11,9 @@
  *
  */
 #include "all.h"
+#ifdef USE_ICONS
+#include <xcb/xcb_image.h>
+#endif
 
 xcb_window_t ewmh_window;
 
@@ -312,6 +315,43 @@ void x_window_kill(xcb_window_t window,
     free(event);
 }
 
+#ifdef USE_ICONS
+static inline uint32_t pixel_blend(uint32_t d, uint32_t s)
+{
+       const uint32_t a     = (s >> 24) + 1;
+
+       const uint32_t dstrb = d & 0xFF00FF;
+       const uint32_t dstg  = d & 0xFF00;
+
+       const uint32_t srcrb = s & 0xFF00FF;
+       const uint32_t srcg  = s & 0xFF00;
+
+       uint32_t drb = srcrb - dstrb;
+       uint32_t dg  =  srcg - dstg;
+
+       drb *= a;
+       dg  *= a;
+       drb >>= 8;
+       dg  >>= 8;
+
+       uint32_t rb = (drb + dstrb) & 0xFF00FF;
+       uint32_t g  = (dg  + dstg) & 0xFF00;
+
+       return rb | g;
+}
+
+/*
+ * Copy icon pixels, blend with background
+ */
+void copy_with_pixel_blend(uint32_t *dst, uint32_t* src, uint32_t background)
+{
+    int i;
+    for(i=0; i < 16*16; ++i) {
+       *dst++ = pixel_blend(background,*src++);
+    }
+}
+#endif
+
 /*
  * Draws the decoration of the given container onto its parent.
  *
@@ -543,6 +583,9 @@ void x_draw_decoration(Con *con) {
     }
     //DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
     int indent_px = (indent_level * 5) * indent_mult;
+    #ifdef USE_ICONS
+        if (win->icon) indent_px += 18;
+    #endif
 
     int mark_width = 0;
     if (config.show_marks && con->mark != NULL && (con->mark)[0] != '_') {
@@ -564,6 +607,40 @@ void x_draw_decoration(Con *con) {
               parent->pixmap, parent->pm_gc,
               con->deco_rect.x + logical_px(2) + indent_px, con->deco_rect.y + text_offset_y,
               con->deco_rect.width - logical_px(2) - indent_px - mark_width - logical_px(2));
+    #ifdef USE_ICONS
+    /* Draw the icon */
+    if (win->icon) {
+        xcb_image_t* icon;
+
+        uint16_t width = 16;
+        uint16_t height = 16;
+        uint32_t icon_pixels[width*height];
+
+        copy_with_pixel_blend(icon_pixels, win->icon, p->color->background);
+
+        icon = xcb_image_create_native( conn,
+                width, height,
+                XCB_IMAGE_FORMAT_Z_PIXMAP,
+                root_depth,
+                NULL,
+                width*height*4,
+                (uint8_t*)icon_pixels
+                );
+
+        if (icon) {
+            int icon_offset_y = (con->deco_rect.height - 16) / 2;
+
+            xcb_image_put(conn, parent->pixmap, parent->pm_gc,
+                    icon, con->deco_rect.x + indent_px - 16 , con->deco_rect.y + icon_offset_y, 0);
+
+            xcb_image_destroy(icon);
+        }
+        else {
+            ELOG("Error creating XCB image\n");
+        }
+    }
+    #endif
+
     if (win->title_format != NULL)
         I3STRING_FREE(title);