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