-
Notifications
You must be signed in to change notification settings - Fork 8
/
ewmh.c
382 lines (343 loc) · 9.69 KB
/
ewmh.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* EWMH atom support. initial implementation borrowed from
* awesome wm, then partially reworked.
*
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
* Copyright © 2008 Alexander Polakov <polachok@gmail.com>
*
*/
#include <regex.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/Xft/Xft.h>
#include "echinus.h"
#include "config.h"
Atom atom[NATOMS];
/* keep in sync with enum in echinus.h */
const char *atomnames[NATOMS][1] = {
{ "_NET_CLIENT_LIST" },
{ "_NET_ACTIVE_WINDOW" },
{ "_NET_WM_DESKTOP" },
{ "_NET_NUMBER_OF_DESKTOPS" },
{ "_NET_DESKTOP_NAMES" },
{ "_NET_CURRENT_DESKTOP" },
{ "_ECHINUS_LAYOUT" },
{ "_NET_WORKAREA" },
{ "_NET_CLIENT_LIST_STACKING" },
{ "_NET_WM_WINDOW_OPACITY" },
{ "_NET_WM_WINDOW_TYPE" },
{ "_NET_WM_WINDOW_TYPE_DESKTOP" },
{ "_NET_WM_WINDOW_TYPE_DOCK" },
{ "_NET_WM_WINDOW_TYPE_DIALOG" },
{ "_NET_WM_STRUT_PARTIAL" },
{ "_NET_WM_STRUT" },
{ "_ECHINUS_SELTAGS" },
{ "_NET_WM_NAME" },
{ "_NET_WM_STATE" },
{ "_NET_WM_STATE_FULLSCREEN" },
{ "_NET_WM_STATE_MODAL" },
{ "_NET_WM_STATE_HIDDEN" },
{ "_NET_SUPPORTING_WM_CHECK" },
{ "_NET_CLOSE_WINDOW" },
{ "UTF8_STRING" },
{ "_NET_SUPPORTED" },
{ "WM_PROTOCOLS" },
{ "WM_DELETE_WINDOW" },
{ "WM_NAME" },
{ "WM_STATE" },
{ "WM_CHANGE_STATE" },
{ "WM_TAKE_FOCUS" },
{ "_MOTIF_WM_HINTS" },
};
#define _NET_WM_STATE_REMOVE 0
#define _NET_WM_STATE_ADD 1
#define _NET_WM_STATE_TOGGLE 2
void
initewmh(void) {
int i;
char name[] = "echinus";
XSetWindowAttributes wa;
Window win;
for (i = 0; i < NATOMS; i++)
atom[i] = XInternAtom(dpy, atomnames[i][0], False);
XChangeProperty(dpy, root,
atom[Supported], XA_ATOM, 32,
PropModeReplace, (unsigned char *) atom, NATOMS);
wa.override_redirect = True;
win = XCreateWindow(dpy, root, -100, 0, 1, 1,
0, DefaultDepth(dpy, screen), CopyFromParent,
DefaultVisual(dpy, screen), CWOverrideRedirect, &wa);
XChangeProperty(dpy, win, atom[WindowName], atom[Utf8String], 8,
PropModeReplace, (unsigned char*)name, strlen(name));
XChangeProperty(dpy, root, atom[WMCheck], XA_WINDOW, 32,
PropModeReplace, (unsigned char*)&win, 1);
}
void
update_echinus_layout_name(void *p) {
XChangeProperty(dpy, root, atom[ELayout],
XA_STRING, 8, PropModeReplace,
(const unsigned char *) &views[curmontag].layout->symbol, 1L);
}
void
ewmh_update_net_client_list(void *p) {
Window *wins = NULL;
Client *c;
int i, n = 0;
for (c = stack; c; c = c->snext)
n++;
if (!n) {
XChangeProperty(dpy, root, atom[ClientList], XA_WINDOW, 32,
PropModeReplace, (unsigned char *) wins, n);
XChangeProperty(dpy, root, atom[ClientListStacking], XA_WINDOW,
32, PropModeReplace, (unsigned char *) wins, n);
return;
}
wins = malloc(sizeof(Window) * n);
for (i = 0, c = stack; c; c = c->snext)
wins[i++] = c->win;
XChangeProperty(dpy, root,
atom[ClientListStacking], XA_WINDOW, 32, PropModeReplace,
(unsigned char *) wins, n);
for (i = 0, c = clients; c; c = c->next)
wins[i++] = c->win;
XChangeProperty(dpy, root,
atom[ClientList], XA_WINDOW, 32, PropModeReplace, (unsigned char *) wins, n);
free(wins);
XFlush(dpy);
}
void
ewmh_update_net_number_of_desktops(void *p) {
XChangeProperty(dpy, root,
atom[NumberOfDesk], XA_CARDINAL, 32, PropModeReplace,
(unsigned char *) &ntags, 1);
}
void
ewmh_update_net_current_desktop(void *p) {
Monitor *m;
unsigned long *seltags;
unsigned int i;
seltags = emallocz(ntags * sizeof(unsigned long));
for (m = monitors; m != NULL; m = m->next) {
for (i = 0; i < ntags; i++)
seltags[i] |= m->seltags[i];
}
XChangeProperty(dpy, root,
atom[ESelTags], XA_CARDINAL, 32, PropModeReplace,
(unsigned char *) seltags, ntags);
XChangeProperty(dpy, root, atom[CurDesk], XA_CARDINAL, 32,
PropModeReplace, (unsigned char *) &curmontag, 1);
update_echinus_layout_name(NULL);
free(seltags);
}
void
ewmh_update_net_window_desktop(void *p) {
unsigned long i;
Client *c = (Client *)p;
for (i = 0; i < ntags && !c->tags[i]; i++);
XChangeProperty(dpy, c->win,
atom[WindowDesk], XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &i, 1);
}
void
ewmh_update_net_work_area(void *p) {
unsigned long *geoms;
Monitor *m = monitors;
int i, x, y, w, h;
geoms = malloc(sizeof(unsigned long)*4*ntags);
x = m->wax - m->sx;
y = m->way - m->sy;
w = m->waw;
h = m->wah;
for (m = m->next; m != NULL; m = m->next) {
x = max(x, m->wax - m->sx);
y = max(y, m->way - m->sy);
w = min(w, m->waw);
h = min(h, m->wah);
}
for (i = 0; i < ntags; i++) {
geoms[i*4] = x;
geoms[i*4+1] = y;
geoms[i*4+2] = w;
geoms[i*4+3] = h;
}
XChangeProperty(dpy, root,
atom[WorkArea], XA_CARDINAL, 32, PropModeReplace, (unsigned char *) geoms, ntags*4);
free(geoms);
}
void
ewmh_update_net_desktop_names(void *p) {
char buf[1024], *pos;
unsigned int i;
int len = 0;
pos = buf;
for (i = 0; i < ntags; i++) {
snprintf(pos, strlen(tags[i]) + 1, "%s", tags[i]);
pos += (strlen(tags[i]) + 1);
}
len = pos - buf;
XChangeProperty(dpy, root,
atom[DeskNames], atom[Utf8String], 8, PropModeReplace,
(unsigned char *) buf, len);
}
void
ewmh_update_net_active_window(void *p) {
Window win;
win = sel ? sel->win : None;
XChangeProperty(dpy, root,
atom[ActiveWindow], XA_WINDOW, 32, PropModeReplace,
(unsigned char *) &win, 1);
}
void
mwm_process_atom(Client *c) {
Atom real;
int format;
unsigned char *data = NULL;
CARD32 *hint;
unsigned long n, extra;
#define MWM_HINTS_ELEMENTS 5
#define MWM_DECOR_ALL(x) ((x) & (1L << 0))
#define MWM_DECOR_TITLE(x) ((x) & (1L << 3))
#define MWM_DECOR_BORDER(x) ((x) & (1L << 1))
#define MWM_HINTS_DECOR(x) ((x) & (1L << 1))
if (XGetWindowProperty(dpy, c->win, atom[MWMHints], 0L, 20L, False,
atom[MWMHints], &real, &format, &n, &extra,
(unsigned char **) &data) == Success && n >= MWM_HINTS_ELEMENTS) {
hint = (CARD32 *) data;
if (MWM_HINTS_DECOR(hint[0]) && !(MWM_DECOR_ALL(hint[2]))) {
c->title = MWM_DECOR_TITLE(hint[2]) ? root : (Window) NULL;
c->border = MWM_DECOR_BORDER(hint[2]) ? style.border : 0;
}
}
XFree(data);
}
void
ewmh_process_state_atom(Client *c, Atom state, int set) {
CARD32 data[2];
data[1] = None;
if (state == atom[WindowStateFs]) {
focus(c);
if ((set == _NET_WM_STATE_ADD || set == _NET_WM_STATE_TOGGLE)
&& !c->ismax) {
c->wasfloating = c->isfloating;
if (!c->isfloating)
togglefloating(NULL);
togglemax(NULL);
data[0] = state;
} else if ((set == _NET_WM_STATE_REMOVE ||
set == _NET_WM_STATE_TOGGLE) && c->ismax) {
togglemax(NULL);
if (!c->wasfloating)
togglefloating(NULL);
data[0] = None;
}
XChangeProperty(dpy, c->win, atom[WindowState], XA_ATOM, 32,
PropModeReplace, (unsigned char *) data, 2);
DPRINT;
arrange(curmonitor());
DPRINTF("%s: x%d y%d w%d h%d\n", c->name, c->x, c->y, c->w, c->h);
}
if (state == atom[WindowStateModal])
focus(c);
}
void
clientmessage(XEvent *e) {
XClientMessageEvent *ev = &e->xclient;
Client *c;
if (ev->message_type == atom[CloseWindow]) {
if ((c = getclient(ev->window, clients, ClientWindow)))
killclient(c);
}
else if (ev->message_type == atom[ActiveWindow]) {
if ((c = getclient(ev->window, clients, ClientWindow))) {
c->isicon = False;
focus(c);
arrange(curmonitor());
}
} else if (ev->message_type == atom[CurDesk]) {
view(tags[ev->data.l[0]]);
} else if (ev->message_type == atom[WindowState]) {
if ((c = getclient(ev->window, clients, ClientWindow))) {
ewmh_process_state_atom(c, (Atom) ev->data.l[1], ev->data.l[0]);
if (ev->data.l[2])
ewmh_process_state_atom(c,
(Atom) ev->data.l[2], ev->data.l[0]);
}
} else if (ev->message_type == atom[WMChangeState]) {
if ((c = getclient(ev->window, clients, ClientWindow))) {
if (ev->data.l[0] == IconicState) {
focus(c);
iconify(NULL);
}
}
}
}
void
setopacity(Client *c, unsigned int opacity) {
if (opacity == OPAQUE) {
XDeleteProperty(dpy, c->win, atom[WindowOpacity]);
XDeleteProperty(dpy, c->frame, atom[WindowOpacity]);
} else {
XChangeProperty(dpy, c->win, atom[WindowOpacity],
XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &opacity, 1L);
XChangeProperty(dpy, c->frame, atom[WindowOpacity],
XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &opacity, 1L);
}
}
void *
getatom(Window win, Atom atom, unsigned long *nitems) {
int format, status;
unsigned char *ret = NULL;
unsigned long extra;
Atom real;
status = XGetWindowProperty(dpy, win, atom, 0L, 64L, False, AnyPropertyType,
&real, &format, nitems, &extra, (unsigned char **)&ret);
if (status != Success) {
*nitems = 0;
return NULL;
}
return ret;
}
Bool
checkatom(Window win, Atom bigatom, Atom smallatom) {
Atom *state;
unsigned long i, n;
Bool ret = False;
state = (Atom*)getatom(win, bigatom, &n);
for (i = 0; i < n; i++) {
if (state[i] == smallatom)
ret = True;
}
XFree(state);
return ret;
}
int
getstrut(Client *c, Atom strut) {
unsigned long *state;
int ret = 0;
Monitor *m;
unsigned long i, n;
if (!(m = clientmonitor(c)))
return ret;
state = (unsigned long*)getatom(c->win, strut, &n);
if (n) {
for (i = LeftStrut; i < LastStrut; i++)
m->struts[i] = max(state[i], m->struts[i]);
ret = 1;
}
XFree(state);
return ret;
}
int getstruts(Client *c) {
return (getstrut(c, atom[StrutPartial]) || getstrut(c, atom[Strut]));
}
void (*updateatom[]) (void *) = {
[ClientList] = ewmh_update_net_client_list,
[ActiveWindow] = ewmh_update_net_active_window,
[WindowDesk] = ewmh_update_net_window_desktop,
[NumberOfDesk] = ewmh_update_net_number_of_desktops,
[DeskNames] = ewmh_update_net_desktop_names,
[CurDesk] = ewmh_update_net_current_desktop,
[ELayout] = update_echinus_layout_name,
[WorkArea] = ewmh_update_net_work_area,
};