]> git.sur5r.net Git - i3/i3/blob - src/window.c
Merge branch 'i3bar-cleanup' into next
[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("WM_NAME changed to \"%s\"\n", new_name);
127     LOG("Using legacy window title. Note that in order to get Unicode window "
128         "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
129
130     FREE(win->name_x);
131     FREE(win->name_json);
132     win->name_x = new_name;
133     win->name_json = sstrdup(new_name);
134     win->name_len = strlen(new_name);
135     win->name_x_changed = true;
136
137     if (before_mgmt) {
138         free(prop);
139         return;
140     }
141
142     run_assignments(win);
143
144     free(prop);
145 }
146
147 /*
148  * Updates the CLIENT_LEADER (logical parent window).
149  *
150  */
151 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
152     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
153         DLOG("prop == NULL\n");
154         FREE(prop);
155         return;
156     }
157
158     xcb_window_t *leader = xcb_get_property_value(prop);
159     if (leader == NULL) {
160         free(prop);
161         return;
162     }
163
164     DLOG("Client leader changed to %08x\n", *leader);
165
166     win->leader = *leader;
167
168     free(prop);
169 }
170
171 /*
172  * Updates the TRANSIENT_FOR (logical parent window).
173  *
174  */
175 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
176     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
177         DLOG("prop == NULL\n");
178         FREE(prop);
179         return;
180     }
181
182     xcb_window_t transient_for;
183     if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
184         free(prop);
185         return;
186     }
187
188     DLOG("Transient for changed to %08x\n", transient_for);
189
190     win->transient_for = transient_for;
191
192     free(prop);
193 }
194
195 /*
196  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
197  *
198  */
199 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
200     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
201         DLOG("prop == NULL\n");
202         FREE(prop);
203         return;
204     }
205
206     uint32_t *strut;
207     if (!(strut = xcb_get_property_value(prop))) {
208         free(prop);
209         return;
210     }
211
212     DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
213          strut[0], strut[1], strut[2], strut[3]);
214
215     win->reserved = (struct reservedpx){ strut[0], strut[1], strut[2], strut[3] };
216
217     free(prop);
218 }
219
220 /*
221  * Updates the WM_WINDOW_ROLE
222  *
223  */
224 void window_update_role(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
225     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
226         DLOG("prop == NULL\n");
227         FREE(prop);
228         return;
229     }
230
231     char *new_role;
232     if (asprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
233                  (char*)xcb_get_property_value(prop)) == -1) {
234         perror("asprintf()");
235         DLOG("Could not get WM_WINDOW_ROLE\n");
236         free(prop);
237         return;
238     }
239     FREE(win->role);
240     win->role = new_role;
241     LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
242
243     if (before_mgmt) {
244         free(prop);
245         return;
246     }
247
248     run_assignments(win);
249
250     free(prop);
251 }