1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/types.h>
34 xcb_ewmh_connection_t *ewmh;
38 ewmh = calloc(1, sizeof(xcb_ewmh_connection_t));
39 if (xcb_ewmh_init_atoms_replies(ewmh, xcb_ewmh_init_atoms(dpy, ewmh), NULL) == 0) {
40 err("Can't initialize EWMH atoms.\n");
44 void ewmh_update_active_window(void)
46 xcb_window_t win = ((mon->desk->focus == NULL || mon->desk->focus->client == NULL) ? XCB_NONE : mon->desk->focus->id);
47 xcb_ewmh_set_active_window(ewmh, default_screen, win);
50 void ewmh_update_number_of_desktops(void)
52 uint32_t desktops_count = 0;
54 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
55 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
60 xcb_ewmh_set_number_of_desktops(ewmh, default_screen, desktops_count);
63 uint32_t ewmh_get_desktop_index(desktop_t *d)
66 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
67 for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next, i++) {
76 bool ewmh_locate_desktop(uint32_t i, coordinates_t *loc)
78 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
79 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--) {
91 void ewmh_update_current_desktop(void)
96 uint32_t i = ewmh_get_desktop_index(mon->desk);
97 xcb_ewmh_set_current_desktop(ewmh, default_screen, i);
100 void ewmh_set_wm_desktop(node_t *n, desktop_t *d)
102 uint32_t i = ewmh_get_desktop_index(d);
103 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
104 if (f->client == NULL) {
107 xcb_ewmh_set_wm_desktop(ewmh, f->id, i);
111 void ewmh_update_wm_desktops(void)
113 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
114 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
115 uint32_t i = ewmh_get_desktop_index(d);
116 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
117 if (n->client == NULL) {
120 xcb_ewmh_set_wm_desktop(ewmh, n->id, i);
126 void ewmh_update_desktop_names(void)
133 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
134 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
135 for (j = 0; d->name[j] != '\0' && (i + j) < sizeof(names); j++) {
136 names[i + j] = d->name[j];
139 if (i < sizeof(names)) {
146 xcb_ewmh_set_desktop_names(ewmh, default_screen, 0, NULL);
151 xcb_ewmh_set_desktop_names(ewmh, default_screen, names_len, names);
154 void ewmh_update_desktop_viewport(void)
156 uint32_t desktops_count = 0;
157 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
158 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
162 if (desktops_count == 0) {
163 xcb_ewmh_set_desktop_viewport(ewmh, default_screen, 0, NULL);
166 xcb_ewmh_coordinates_t coords[desktops_count];
167 uint16_t desktop = 0;
168 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
169 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
170 coords[desktop++] = (xcb_ewmh_coordinates_t){m->rectangle.x, m->rectangle.y};
173 xcb_ewmh_set_desktop_viewport(ewmh, default_screen, desktop, coords);
176 bool ewmh_handle_struts(xcb_window_t win)
178 xcb_ewmh_wm_strut_partial_t struts;
179 bool changed = false;
180 if (xcb_ewmh_get_wm_strut_partial_reply(ewmh, xcb_ewmh_get_wm_strut_partial(ewmh, win), &struts, NULL) == 1) {
181 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
182 xcb_rectangle_t rect = m->rectangle;
183 if (rect.x < (int16_t) struts.left &&
184 (int16_t) struts.left < (rect.x + rect.width - 1) &&
185 (int16_t) struts.left_end_y >= rect.y &&
186 (int16_t) struts.left_start_y < (rect.y + rect.height)) {
187 int dx = struts.left - rect.x;
188 if (m->padding.left < 0) {
189 m->padding.left += dx;
191 m->padding.left = MAX(dx, m->padding.left);
195 if ((rect.x + rect.width) > (int16_t) (screen_width - struts.right) &&
196 (int16_t) (screen_width - struts.right) > rect.x &&
197 (int16_t) struts.right_end_y >= rect.y &&
198 (int16_t) struts.right_start_y < (rect.y + rect.height)) {
199 int dx = (rect.x + rect.width) - screen_width + struts.right;
200 if (m->padding.right < 0) {
201 m->padding.right += dx;
203 m->padding.right = MAX(dx, m->padding.right);
207 if (rect.y < (int16_t) struts.top &&
208 (int16_t) struts.top < (rect.y + rect.height - 1) &&
209 (int16_t) struts.top_end_x >= rect.x &&
210 (int16_t) struts.top_start_x < (rect.x + rect.width)) {
211 int dy = struts.top - rect.y;
212 if (m->padding.top < 0) {
213 m->padding.top += dy;
215 m->padding.top = MAX(dy, m->padding.top);
219 if ((rect.y + rect.height) > (int16_t) (screen_height - struts.bottom) &&
220 (int16_t) (screen_height - struts.bottom) > rect.y &&
221 (int16_t) struts.bottom_end_x >= rect.x &&
222 (int16_t) struts.bottom_start_x < (rect.x + rect.width)) {
223 int dy = (rect.y + rect.height) - screen_height + struts.bottom;
224 if (m->padding.bottom < 0) {
225 m->padding.bottom += dy;
227 m->padding.bottom = MAX(dy, m->padding.bottom);
236 void ewmh_update_client_list(bool stacking)
238 if (clients_count == 0) {
239 xcb_ewmh_set_client_list(ewmh, default_screen, 0, NULL);
240 xcb_ewmh_set_client_list_stacking(ewmh, default_screen, 0, NULL);
244 xcb_window_t wins[clients_count];
248 for (stacking_list_t *s = stack_head; s != NULL; s = s->next) {
249 wins[i++] = s->node->id;
251 xcb_ewmh_set_client_list_stacking(ewmh, default_screen, clients_count, wins);
253 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
254 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
255 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
256 if (n->client == NULL) {
263 xcb_ewmh_set_client_list(ewmh, default_screen, clients_count, wins);
267 void ewmh_wm_state_update(node_t *n)
269 client_t *c = n->client;
272 #define HANDLE_WM_STATE(s) \
273 if (WM_FLAG_##s & c->wm_flags) { \
274 values[count++] = ewmh->_NET_WM_STATE_##s; \
276 HANDLE_WM_STATE(MODAL)
277 HANDLE_WM_STATE(STICKY)
278 HANDLE_WM_STATE(MAXIMIZED_VERT)
279 HANDLE_WM_STATE(MAXIMIZED_HORZ)
280 HANDLE_WM_STATE(SHADED)
281 HANDLE_WM_STATE(SKIP_TASKBAR)
282 HANDLE_WM_STATE(SKIP_PAGER)
283 HANDLE_WM_STATE(HIDDEN)
284 HANDLE_WM_STATE(FULLSCREEN)
285 HANDLE_WM_STATE(ABOVE)
286 HANDLE_WM_STATE(BELOW)
287 HANDLE_WM_STATE(DEMANDS_ATTENTION)
288 #undef HANDLE_WM_STATE
289 xcb_ewmh_set_wm_state(ewmh, n->id, count, values);
292 void ewmh_set_supporting(xcb_window_t win)
294 pid_t wm_pid = getpid();
295 xcb_ewmh_set_supporting_wm_check(ewmh, root, win);
296 xcb_ewmh_set_supporting_wm_check(ewmh, win, win);
297 xcb_ewmh_set_wm_name(ewmh, win, strlen(WM_NAME), WM_NAME);
298 xcb_ewmh_set_wm_pid(ewmh, win, wm_pid);