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