]> git.sur5r.net Git - i3/i3/blob - src/window.c
Remove old code from randr.c and workspace.c
[i3/i3] / src / window.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2010 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) {
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
37 /*
38  * Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
39  * window. Further updates using window_update_name_legacy will be ignored.
40  *
41  */
42 void window_update_name(i3Window *win, xcb_get_property_reply_t *prop) {
43     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
44         DLOG("_NET_WM_NAME not specified, not changing\n");
45         return;
46     }
47
48     /* Save the old pointer to make the update atomic */
49     char *new_name;
50     if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop),
51                  (char*)xcb_get_property_value(prop)) == -1) {
52         perror("asprintf()");
53         DLOG("Could not get window name\n");
54         return;
55     }
56     /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
57     int len;
58     char *ucs2_name = convert_utf8_to_ucs2(new_name, &len);
59     if (ucs2_name == NULL) {
60         LOG("Could not convert _NET_WM_NAME to UCS-2, ignoring new hint\n");
61         FREE(new_name);
62         return;
63     }
64     FREE(win->name_x);
65     FREE(win->name_json);
66     win->name_json = new_name;
67     win->name_x = ucs2_name;
68     win->name_len = len;
69     win->name_x_changed = true;
70     LOG("_NET_WM_NAME changed to \"%s\"\n", win->name_json);
71
72     win->uses_net_wm_name = true;
73 }
74
75 /*
76  * Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
77  * touch what the client sends us but pass it to xcb_image_text_8. To get
78  * proper unicode rendering, the application has to use _NET_WM_NAME (see
79  * window_update_name()).
80  *
81  */
82 void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop) {
83     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
84         DLOG("prop == NULL\n");
85         return;
86     }
87
88     /* ignore update when the window is known to already have a UTF-8 name */
89     if (win->uses_net_wm_name)
90         return;
91
92     char *new_name;
93     if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop),
94                  (char*)xcb_get_property_value(prop)) == -1) {
95         perror("asprintf()");
96         DLOG("Could not get legacy window name\n");
97         return;
98     }
99
100     LOG("Using legacy window title. Note that in order to get Unicode window "
101         "titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
102
103     FREE(win->name_x);
104     FREE(win->name_json);
105     win->name_x = new_name;
106     win->name_json = sstrdup(new_name);
107     win->name_len = strlen(new_name);
108     win->name_x_changed = true;
109 }
110
111 /*
112  * Updates the CLIENT_LEADER (logical parent window).
113  *
114  */
115 void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
116     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
117         DLOG("prop == NULL\n");
118         return;
119     }
120
121     xcb_window_t *leader = xcb_get_property_value(prop);
122     if (leader == NULL)
123         return;
124
125     DLOG("Client leader changed to %08x\n", *leader);
126
127     win->leader = *leader;
128 }
129
130 /*
131  * Updates the TRANSIENT_FOR (logical parent window).
132  *
133  */
134 void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
135     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
136         DLOG("prop == NULL\n");
137         return;
138     }
139
140     xcb_window_t transient_for;
141     if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop))
142         return;
143
144     DLOG("Transient for changed to %08x\n", transient_for);
145
146     win->transient_for = transient_for;
147 }
148
149 /*
150  * Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
151  *
152  */
153 void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
154     if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
155         DLOG("prop == NULL\n");
156         return;
157     }
158
159     uint32_t *strut;
160     if (!(strut = xcb_get_property_value(prop)))
161         return;
162
163     DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
164          strut[0], strut[1], strut[2], strut[3]);
165
166     win->reserved = (struct reservedpx){ strut[0], strut[1], strut[2], strut[3] };
167 }