]> git.sur5r.net Git - i3/i3/blob - src/window.c
nagbar: use less / vi as fallbacks for PAGER / EDITOR
[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         return;
19     }
20
21     /* We cannot use asprintf here since this property contains two
22      * null-terminated strings (for compatibility reasons). Instead, we
23      * use strdup() on both strings */
24     char *new_class = xcb_get_property_value(prop);
25
26     FREE(win->class_instance);
27     FREE(win->class_class);
28
29     win->class_instance = sstrdup(new_class);
30     if ((strlen(new_class) + 1) < xcb_get_property_value_length(prop))
31         win->class_class = sstrdup(new_class + strlen(new_class) + 1);
32     else win->class_class = NULL;
33     LOG("WM_CLASS changed to %s (instance), %s (class)\n",
34         win->class_instance, win->class_class);
35
36     if (before_mgmt)
37         return;
38
39     run_assignments(win);
40 }
41
42 /*
43  * Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
44  * window. Further updates using window_update_name_legacy will be ignored.
45  *
46  */
47 void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
48     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
49         DLOG("_NET_WM_NAME not specified, not changing\n");
50         return;
51     }
52
53     /* Save the old pointer to make the update atomic */
54     char *new_name;
55     if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop),
56                  (char*)xcb_get_property_value(prop)) == -1) {
57         perror("asprintf()");
58         DLOG("Could not get window name\n");
59         return;
60     }
61     /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
62     int len;
63     char *ucs2_name = convert_utf8_to_ucs2(new_name, &len);
64     if (ucs2_name == NULL) {
65         LOG("Could not convert _NET_WM_NAME to UCS-2, ignoring new hint\n");
66         FREE(new_name);
67         return;
68     }
69     FREE(win->name_x);
70     FREE(win->name_json);
71     win->name_json = new_name;
72     win->name_x = ucs2_name;
73     win->name_len = len;
74     win->name_x_changed = true;
75     LOG("_NET_WM_NAME changed to \"%s\"\n", win->name_json);
76
77     win->uses_net_wm_name = true;
78
79     if (before_mgmt)
80         return;
81
82     run_assignments(win);
83 }
84
85 /*
86  * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
87  * touch what the client sends us but pass it to xcb_image_text_8. To get
88  * proper unicode rendering, the application has to use _NET_WM_NAME (see
89  * window_update_name()).
90  *
91  */
92 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop, bool before_mgmt) {
93     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
94         DLOG("prop == NULL\n");
95         return;
96     }
97
98     /* ignore update when the window is known to already have a UTF-8 name */
99     if (win->uses_net_wm_name)
100         return;
101
102     char *new_name;
103     if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop),
104                  (char*)xcb_get_property_value(prop)) == -1) {
105         perror("asprintf()");
106         DLOG("Could not get legacy window name\n");
107         return;
108     }
109
110     LOG("Using legacy window title. Note that in order to get Unicode window "
111         "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
112
113     FREE(win->name_x);
114     FREE(win->name_json);
115     win->name_x = new_name;
116     win->name_json = sstrdup(new_name);
117     win->name_len = strlen(new_name);
118     win->name_x_changed = true;
119
120     if (before_mgmt)
121         return;
122
123     run_assignments(win);
124 }
125
126 /*
127  * Updates the CLIENT_LEADER (logical parent window).
128  *
129  */
130 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
131     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
132         DLOG("prop == NULL\n");
133         return;
134     }
135
136     xcb_window_t *leader = xcb_get_property_value(prop);
137     if (leader == NULL)
138         return;
139
140     DLOG("Client leader changed to %08x\n", *leader);
141
142     win->leader = *leader;
143 }
144
145 /*
146  * Updates the TRANSIENT_FOR (logical parent window).
147  *
148  */
149 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
150     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
151         DLOG("prop == NULL\n");
152         return;
153     }
154
155     xcb_window_t transient_for;
156     if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop))
157         return;
158
159     DLOG("Transient for changed to %08x\n", transient_for);
160
161     win->transient_for = transient_for;
162 }
163
164 /*
165  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
166  *
167  */
168 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
169     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
170         DLOG("prop == NULL\n");
171         return;
172     }
173
174     uint32_t *strut;
175     if (!(strut = xcb_get_property_value(prop)))
176         return;
177
178     DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
179          strut[0], strut[1], strut[2], strut[3]);
180
181     win->reserved = (struct reservedpx){ strut[0], strut[1], strut[2], strut[3] };
182 }