]> git.sur5r.net Git - i3/i3/blob - src/window.c
explicitly set filenames to $(basename __FILE__)
[i3/i3] / src / window.c
1 #line 2 "window.c"
2 /*
3  * vim:ts=4:sw=4:expandtab
4  *
5  * i3 - an improved dynamic tiling window manager
6  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
7  *
8  * window.c: Updates window attributes (X11 hints/properties).
9  *
10  */
11 #include "all.h"
12
13 /*
14  * Updates the WM_CLASS (consisting of the class and instance) for the
15  * given window.
16  *
17  */
18 void window_update_class(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
19     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
20         DLOG("WM_CLASS not set.\n");
21         FREE(prop);
22         return;
23     }
24
25     /* We cannot use asprintf here since this property contains two
26      * null-terminated strings (for compatibility reasons). Instead, we
27      * use strdup() on both strings */
28     char *new_class = xcb_get_property_value(prop);
29
30     FREE(win->class_instance);
31     FREE(win->class_class);
32
33     win->class_instance = sstrdup(new_class);
34     if ((strlen(new_class) + 1) < xcb_get_property_value_length(prop))
35         win->class_class = sstrdup(new_class + strlen(new_class) + 1);
36     else win->class_class = NULL;
37     LOG("WM_CLASS changed to %s (instance), %s (class)\n",
38         win->class_instance, win->class_class);
39
40     if (before_mgmt) {
41         free(prop);
42         return;
43     }
44
45     run_assignments(win);
46
47     free(prop);
48 }
49
50 /*
51  * Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
52  * window. Further updates using window_update_name_legacy will be ignored.
53  *
54  */
55 void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
56     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
57         DLOG("_NET_WM_NAME not specified, not changing\n");
58         FREE(prop);
59         return;
60     }
61
62     /* Save the old pointer to make the update atomic */
63     char *new_name;
64     if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop),
65                  (char*)xcb_get_property_value(prop)) == -1) {
66         perror("asprintf()");
67         DLOG("Could not get window name\n");
68         free(prop);
69         return;
70     }
71     /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
72     size_t len;
73     xcb_char2b_t *ucs2_name = convert_utf8_to_ucs2(new_name, &len);
74     if (ucs2_name == NULL) {
75         LOG("Could not convert _NET_WM_NAME to UCS-2, ignoring new hint\n");
76         FREE(new_name);
77         free(prop);
78         return;
79     }
80     FREE(win->name_x);
81     FREE(win->name_json);
82     win->name_json = new_name;
83     win->name_x = (char*)ucs2_name;
84     win->name_len = len;
85     win->name_x_changed = true;
86     LOG("_NET_WM_NAME changed to \"%s\"\n", win->name_json);
87
88     win->uses_net_wm_name = true;
89
90     if (before_mgmt) {
91         free(prop);
92         return;
93     }
94
95     run_assignments(win);
96
97     free(prop);
98 }
99
100 /*
101  * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
102  * touch what the client sends us but pass it to xcb_image_text_8. To get
103  * proper unicode rendering, the application has to use _NET_WM_NAME (see
104  * window_update_name()).
105  *
106  */
107 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
108     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
109         DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
110         FREE(prop);
111         return;
112     }
113
114     /* ignore update when the window is known to already have a UTF-8 name */
115     if (win->uses_net_wm_name) {
116         free(prop);
117         return;
118     }
119
120     char *new_name;
121     if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop),
122                  (char*)xcb_get_property_value(prop)) == -1) {
123         perror("asprintf()");
124         DLOG("Could not get legacy window name\n");
125         free(prop);
126         return;
127     }
128
129     LOG("WM_NAME changed to \"%s\"\n", new_name);
130     LOG("Using legacy window title. Note that in order to get Unicode window "
131         "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
132
133     FREE(win->name_x);
134     FREE(win->name_json);
135     win->name_x = new_name;
136     win->name_json = sstrdup(new_name);
137     win->name_len = strlen(new_name);
138     win->name_x_changed = true;
139
140     if (before_mgmt) {
141         free(prop);
142         return;
143     }
144
145     run_assignments(win);
146
147     free(prop);
148 }
149
150 /*
151  * Updates the CLIENT_LEADER (logical parent window).
152  *
153  */
154 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
155     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
156         DLOG("CLIENT_LEADER not set.\n");
157         FREE(prop);
158         return;
159     }
160
161     xcb_window_t *leader = xcb_get_property_value(prop);
162     if (leader == NULL) {
163         free(prop);
164         return;
165     }
166
167     DLOG("Client leader changed to %08x\n", *leader);
168
169     win->leader = *leader;
170
171     free(prop);
172 }
173
174 /*
175  * Updates the TRANSIENT_FOR (logical parent window).
176  *
177  */
178 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
179     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
180         DLOG("TRANSIENT_FOR not set.\n");
181         FREE(prop);
182         return;
183     }
184
185     xcb_window_t transient_for;
186     if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
187         free(prop);
188         return;
189     }
190
191     DLOG("Transient for changed to %08x\n", transient_for);
192
193     win->transient_for = transient_for;
194
195     free(prop);
196 }
197
198 /*
199  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
200  *
201  */
202 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
203     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
204         DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
205         FREE(prop);
206         return;
207     }
208
209     uint32_t *strut;
210     if (!(strut = xcb_get_property_value(prop))) {
211         free(prop);
212         return;
213     }
214
215     DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
216          strut[0], strut[1], strut[2], strut[3]);
217
218     win->reserved = (struct reservedpx){ strut[0], strut[1], strut[2], strut[3] };
219
220     free(prop);
221 }
222
223 /*
224  * Updates the WM_WINDOW_ROLE
225  *
226  */
227 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
228     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
229         DLOG("WM_WINDOW_ROLE not set.\n");
230         FREE(prop);
231         return;
232     }
233
234     char *new_role;
235     if (asprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
236                  (char*)xcb_get_property_value(prop)) == -1) {
237         perror("asprintf()");
238         DLOG("Could not get WM_WINDOW_ROLE\n");
239         free(prop);
240         return;
241     }
242     FREE(win->role);
243     win->role = new_role;
244     LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
245
246     if (before_mgmt) {
247         free(prop);
248         return;
249     }
250
251     run_assignments(win);
252
253     free(prop);
254 }
255
256 /*
257  * Updates the WM_HINTS (we only care about the input focus handling part).
258  *
259  */
260 void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop) {
261     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
262         DLOG("WM_HINTS not set.\n");
263         FREE(prop);
264         return;
265     }
266
267     xcb_icccm_wm_hints_t hints;
268
269     if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
270         DLOG("Could not get WM_HINTS\n");
271         free(prop);
272         return;
273     }
274
275     win->doesnt_accept_focus = !hints.input;
276     LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
277
278     free(prop);
279 }