]> git.lizzy.rs Git - bspwm.git/blob - ewmh.c
Move a few functions from tree to desktop/monitor
[bspwm.git] / ewmh.c
1 #include <string.h>
2 #include <unistd.h>
3 #include "bspwm.h"
4 #include "settings.h"
5 #include "tree.h"
6 #include "ewmh.h"
7
8 void ewmh_init(void)
9 {
10     ewmh = malloc(sizeof(xcb_ewmh_connection_t));
11     if (xcb_ewmh_init_atoms_replies(ewmh, xcb_ewmh_init_atoms(dpy, ewmh), NULL) == 0)
12         err("Can't initialize EWMH atoms.\n");
13 }
14
15 void ewmh_update_active_window(void)
16 {
17     xcb_window_t win = (mon->desk->focus == NULL ? XCB_NONE : mon->desk->focus->client->window);
18     xcb_ewmh_set_active_window(ewmh, default_screen, win);
19 }
20
21 void ewmh_update_number_of_desktops(void)
22 {
23     xcb_ewmh_set_number_of_desktops(ewmh, default_screen, num_desktops);
24 }
25
26 uint32_t ewmh_get_desktop_index(desktop_t *d)
27 {
28     uint32_t i = 0;
29     for (monitor_t *m = mon_head; m != NULL; m = m->next)
30         for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next, i++)
31             if (d == cd)
32                 return i;
33     return 0;
34 }
35
36 bool ewmh_locate_desktop(uint32_t i, coordinates_t *loc)
37 {
38     for (monitor_t *m = mon_head; m != NULL; m = m->next)
39         for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
40             if (i == 0) {
41                 loc->monitor = m;
42                 loc->desktop = d;
43                 loc->node = NULL;
44                 return true;
45             }
46     return false;
47 }
48
49 void ewmh_update_current_desktop(void)
50 {
51     uint32_t i = ewmh_get_desktop_index(mon->desk);
52     xcb_ewmh_set_current_desktop(ewmh, default_screen, i);
53 }
54
55 void ewmh_set_wm_desktop(node_t *n, desktop_t *d)
56 {
57     uint32_t i = ewmh_get_desktop_index(d);
58     xcb_ewmh_set_wm_desktop(ewmh, n->client->window, i);
59 }
60
61 void ewmh_update_wm_desktops(void)
62 {
63     for (monitor_t *m = mon_head; m != NULL; m = m->next)
64         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
65             uint32_t i = ewmh_get_desktop_index(d);
66             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
67                 xcb_ewmh_set_wm_desktop(ewmh, n->client->window, i);
68         }
69 }
70
71 void ewmh_update_desktop_names(void)
72 {
73     char names[MAXLEN];
74     unsigned int pos, i;
75     pos = i = 0;
76
77     for (monitor_t *m = mon_head; m != NULL; m = m->next)
78         for (desktop_t *d = m->desk_head; d != NULL && i < num_desktops; d = d->next) {
79             for (unsigned int j = 0; j < strlen(d->name); j++)
80                 names[pos + j] = d->name[j];
81             pos += strlen(d->name);
82             names[pos] = '\0';
83             pos++, i++;
84         }
85
86     if (i != num_desktops)
87         return;
88     pos--;
89
90     xcb_ewmh_set_desktop_names(ewmh, default_screen, pos, names);
91 }
92
93 void ewmh_update_client_list(void)
94 {
95     if (num_clients == 0) {
96         xcb_ewmh_set_client_list(ewmh, default_screen, 0, NULL);
97         xcb_ewmh_set_client_list_stacking(ewmh, default_screen, 0, NULL);
98         return;
99     }
100
101     xcb_window_t wins[num_clients];
102     unsigned int i = 0;
103
104     for (monitor_t *m = mon_head; m != NULL; m = m->next)
105         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
106             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
107                 wins[i++] = n->client->window;
108
109     if (i != num_clients)
110         return;
111
112     xcb_ewmh_set_client_list(ewmh, default_screen, num_clients, wins);
113     xcb_ewmh_set_client_list_stacking(ewmh, default_screen, num_clients, wins);
114 }
115
116 void ewmh_set_supporting(xcb_window_t win)
117 {
118     pid_t wm_pid = getpid();
119     xcb_ewmh_set_supporting_wm_check(ewmh, root, win);
120     xcb_ewmh_set_supporting_wm_check(ewmh, win, win);
121     xcb_ewmh_set_wm_name(ewmh, win, strlen(WM_NAME), WM_NAME);
122     xcb_ewmh_set_wm_pid(ewmh, win, wm_pid);
123 }