]> git.lizzy.rs Git - bspwm.git/blob - ewmh.c
New setting: `history_aware_focus`
[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     unsigned int pos, i;
74     pos = i = 0;
75
76     for (monitor_t *m = mon_head; m != NULL; m = m->next)
77         for (desktop_t *d = m->desk_head; d != NULL && i < num_desktops; d = d->next) {
78             for (unsigned int j = 0; j < strlen(d->name); j++)
79                 names[pos + j] = d->name[j];
80             pos += strlen(d->name);
81             names[pos] = '\0';
82             pos++, i++;
83         }
84
85     if (i != num_desktops)
86         return;
87     pos--;
88
89     xcb_ewmh_set_desktop_names(ewmh, default_screen, pos, names);
90 }
91
92 void ewmh_update_client_list(void)
93 {
94     if (num_clients == 0) {
95         xcb_ewmh_set_client_list(ewmh, default_screen, 0, NULL);
96         return;
97     }
98
99     xcb_window_t wins[num_clients];
100     unsigned int i = 0;
101
102     for (monitor_t *m = mon_head; m != NULL; m = m->next)
103         for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
104             for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
105                 wins[i++] = n->client->window;
106
107     if (i != num_clients)
108         return;
109
110     xcb_ewmh_set_client_list(ewmh, default_screen, num_clients, wins);
111     xcb_ewmh_set_client_list_stacking(ewmh, default_screen, num_clients, wins);
112 }