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