-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathewmh.c
More file actions
404 lines (348 loc) · 11.2 KB
/
Copy pathewmh.c
File metadata and controls
404 lines (348 loc) · 11.2 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
Interface with EWMH-compatible window managers.
Note: _WIN fallbacks are not part of EWMH or ICCCM, but kept here anyway.
Copyright 2017-2019 Alexander Kulak.
This file is part of alttab program.
alttab is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
alttab is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with alttab. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xft/Xft.h>
#include "alttab.h"
#include "util.h"
extern Globals g;
extern Display *dpy;
extern int scr;
extern Window root;
// PRIVATE
static unsigned long ewmh_getCurrentDesktop();
static unsigned long ewmh_getDesktopOfWindow(Window w);
static bool ewmh_skipWindowInTaskbar(Window w);
//
// returns ptr to EWMH client list and client_list_size
// or NULL
//
static Window *ewmh_get_client_list(unsigned long *client_list_size)
{
Window *client_list;
if (g.ewmh.try_stacking_list_first) {
if ((client_list =
(Window *) get_x_property(root, XA_WINDOW,
"_NET_CLIENT_LIST_STACKING",
client_list_size)) != NULL) {
msg(1, "ewmh found stacking window list\n");
return client_list;
} else {
g.ewmh.try_stacking_list_first = false;
}
}
client_list =
(Window *) get_x_property_alt(root,
XA_WINDOW, "_NET_CLIENT_LIST",
XA_CARDINAL, "_WIN_CLIENT_LIST",
client_list_size);
return client_list;
}
static int ewmh_send_wm_evt(Window w, char *atom, unsigned long edata[])
{
XEvent evt;
long rn_mask = SubstructureRedirectMask | SubstructureNotifyMask;
evt.xclient.window = w;
evt.xclient.type = ClientMessage;
evt.xclient.message_type = XInternAtom(dpy, atom, False);
evt.xclient.serial = 0;
evt.xclient.send_event = True;
evt.xclient.format = 32;
//memset (&(evt.xclient.data.l[1]), 0, 4*sizeof(evt.xclient.data.l[1]));
int ei;
for (ei = 0; ei < 5; ei++)
evt.xclient.data.l[ei] = edata[ei];
if (!XSendEvent(dpy, root, False, rn_mask, &evt)) {
msg(-1, "can't send %s xevent\n", atom);
return 0;
}
return 1;
}
static int ewmh_switch_desktop(unsigned long desktop)
{
int evr, elapsed;
unsigned long edata[] = { desktop, CurrentTime, 0, 0, 0 };
if (desktop == -1 && g.ewmh.minus1_desktop_unusable)
return 0;
msg(1, "ewmh switching desktop to %ld\n", desktop);
evr = ewmh_send_wm_evt(root, "_NET_CURRENT_DESKTOP", edata);
if (evr == 0)
return 0;
// wait for WM (#45)
#define WM_POLL_TIMEOUT 200000 // 200 ms
#define WM_POLL_INTERVAL 10000
for (elapsed = 0;
elapsed < WM_POLL_TIMEOUT
&& ewmh_getCurrentDesktop() != desktop; elapsed += WM_POLL_INTERVAL) {
msg(1, "usleep %d\n", WM_POLL_INTERVAL);
usleep(WM_POLL_INTERVAL);
}
return evr;
}
static int ewmh_switch_window(unsigned long window)
{
unsigned long edata[] = { 2, CurrentTime, 0, 0, 0 };
msg(1, "ewmh switching window to 0x%lx\n", window);
return ewmh_send_wm_evt(window, "_NET_ACTIVE_WINDOW", edata);
}
//
// initialize EwmhFeatures
// return true if usable at all
//
static bool ewmh_detectFeatures(EwmhFeatures * e)
{
Window *chld_win;
char *r;
Atom utf8string;
char *default_wm_name = "unknown_ewmh_compatible";
unsigned long client_list_size;
Window *client_list;
// This function is used in alttab for detection of EWMH compatibility.
// But there are WM (dwm) that support EWMH subset required for alttab
// except of _NET_SUPPORTING_WM_CHECK/_WIN_SUPPORTING_WM_CHECK detection.
// We detect those WM as EWMH-compatible and return their name
// as "unknown_ewmh_compatible".
bzero(e, sizeof(EwmhFeatures));
e->try_stacking_list_first = true;
// first, detect necessary feature: client list
// also, this resets try_stacking_list_first if necessary
client_list = ewmh_get_client_list(&client_list_size);
if (client_list == NULL) {
// WM is not usable in EWMH mode
return false;
}
free(client_list);
// then, guess/devise WM name
chld_win = (Window *)get_x_property_alt(root,
XA_WINDOW, "_NET_SUPPORTING_WM_CHECK",
XA_CARDINAL, "_WIN_SUPPORTING_WM_CHECK",
NULL);
if (!chld_win) {
e->wmname = default_wm_name;
return true;
}
utf8string = XInternAtom(dpy, "UTF8_STRING", False);
r = get_x_property_alt(*chld_win,
utf8string, "_NET_WM_NAME",
XA_STRING, "_NET_WM_NAME", NULL);
free(chld_win);
e->wmname = (r != NULL) ? r : default_wm_name;
// special workarounds
if (strncmp(e->wmname, "CWM", 4) == 0)
e->minus1_desktop_unusable = true;
return true;
}
//
// active window or 0
//
static Window ewmh_getActiveWindow()
{
Window w = (Window) 0;
char *awp;
unsigned long sz;
if ((awp = get_x_property(root, XA_WINDOW, "_NET_ACTIVE_WINDOW", &sz))) {
w = *((Window *) awp);
free(awp);
} else {
msg(0, "can't obtain _NET_ACTIVE_WINDOW\n");
}
return w;
}
//
// initialize winlist, correcting sortlist
// return 1 if ok
//
static int ewmh_initWinlist()
{
Window *client_list;
unsigned long client_list_size;
int i;
Window aw;
char *title;
unsigned long current_desktop, window_desktop;
current_desktop = ewmh_getCurrentDesktop();
aw = ewmh_getActiveWindow();
if (!aw) {
msg(0, "can't obtain _NET_ACTIVE_WINDOW\n");
// continue anyway
}
client_list = ewmh_get_client_list(&client_list_size);
if (client_list == NULL) {
msg(-1, "can't get client list\n");
return 0;
}
for (i = 0; i < client_list_size / sizeof(Window); i++) {
Window w = client_list[i];
if (ewmh_skipWindowInTaskbar(w))
continue;
window_desktop = ewmh_getDesktopOfWindow(w);
if (common_skipWindow(w, current_desktop, window_desktop))
continue;
// build title
char *wmn1 = get_x_property(w, XA_STRING, "WM_NAME", NULL);
Atom utf8str = XInternAtom(dpy, "UTF8_STRING", False);
char *wmn2 = get_x_property(w, utf8str, "_NET_WM_NAME", NULL);
title = wmn2 ? strdup(wmn2) : (wmn1 ? strdup(wmn1) : NULL);
free(wmn1);
free(wmn2);
addWindowInfo(w, 0, 0, window_desktop, title);
if (w == aw) {
addToSortlist(w, true, true); // pull to head
}
free(title);
}
// TODO: BUG? sometimes i3 returns previous active window,
// which breaks sortlist
// no more startNdx, can't output this
//msg(1, "ewmh active window: %lu name: %s\n",
// aw, (g.winlist ? g.winlist[g.startNdx].name : "null"));
free(client_list);
return 1;
}
static unsigned long ewmh_getDesktopFromProp(Window w, char *prop1, char *prop2)
{
unsigned long *d;
unsigned long propsize;
unsigned long ret = DESKTOP_UNKNOWN;
d = (unsigned long *)get_x_property_alt(w,
XA_CARDINAL, prop1,
XA_CARDINAL, prop2, &propsize);
if (d && (propsize > 0))
ret = *d;
free(d);
return ret;
}
//
// get current desktop in EWMH WM
//
static unsigned long ewmh_getCurrentDesktop()
{
return ewmh_getDesktopFromProp(root, "_NET_CURRENT_DESKTOP", "_WIN_WORKSPACE");
}
//
// get desktop of window w in EWMH WM
//
static unsigned long ewmh_getDesktopOfWindow(Window w)
{
return ewmh_getDesktopFromProp(w, "_NET_WM_DESKTOP", "_WIN_WORKSPACE");
}
//
// does window have _NET_WM_STATE_SKIP_TASKBAR
//
static bool ewmh_skipWindowInTaskbar(Window w)
{
Atom *state;
long unsigned int state_propsize;
Atom a_skip_tb;
int i;
bool ret = false;
state =
(Atom *) get_x_property(w, XA_ATOM, "_NET_WM_STATE", &state_propsize);
if (state == NULL || state_propsize == 0) {
msg(1, "%lx: no _NET_WM_STATE property\n", w);
goto out;
}
a_skip_tb = XInternAtom(dpy, "_NET_WM_STATE_SKIP_TASKBAR", True);
for (i = 0; i < state_propsize / sizeof(Atom); i++) {
if (state[i] == a_skip_tb) {
msg(1, "%lx: _NET_WM_STATE_SKIP_TASKBAR found\n", w);
ret = true;
goto out;
}
}
out:
free(state);
return ret;
}
static bool ewmhProbe(void)
{
return ewmh_detectFeatures(&g.ewmh);
}
static int ewmhStartup(void)
{
long rootevmask = 0;
// root: watching for _NET_ACTIVE_WINDOW
g.naw = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", true);
rootevmask |= PropertyChangeMask;
// warning: this overwrites any previous value.
// note: x_setCommonPropertiesForAnyWindow does the similar thing
// for any window other than root and uiwin
XSelectInput(dpy, root, rootevmask);
return 1;
}
static int ewmhSetFocus(int winNdx)
{
int r;
XWindowAttributes att;
r = ewmh_setFocus(winNdx, 0);
// XSetInputFocus stuff.
// skippy-xd does it and notes that "order is important".
// fixes #28.
// it must be protected by testing IsViewable in the same way
// as in x.c, or BadMatch happens after switching desktops.
XGetWindowAttributes(dpy, g.winlist[winNdx].id, &att);
if (att.map_state == IsViewable)
XSetInputFocus(dpy, g.winlist[winNdx].id, RevertToParent,
CurrentTime);
return r;
}
static bool ewmhSkipFocusChangeEvent(void)
{
return true;
}
static long ewmhEventMask(Window w)
{
msg(0, "using direct focus tracking for 0x%lx\n", w);
return FocusChangeMask;
}
// PUBLIC
//
// focus window in EWMH WM
// fwin used if non-zero, winNdx otherwise
//
int ewmh_setFocus(int winNdx, Window fwin)
{
Window win = (fwin != 0) ? fwin : g.winlist[winNdx].id;
msg(1, "ewmh_setFocus win 0x%lx\n", win);
if (fwin == 0 && g.option_desktop != DESK_CURRENT) {
unsigned long wdesk = g.winlist[winNdx].desktop;
unsigned long cdesk = ewmh_getCurrentDesktop();
msg(1, "ewmh_setFocus fwin 0x%lx opt %d wdesk %lu cdesk %lu\n",
fwin, g.option_desktop, wdesk, cdesk);
if (cdesk != wdesk && wdesk != DESKTOP_UNKNOWN) {
ewmh_switch_desktop(wdesk);
}
}
ewmh_switch_window(win);
XMapRaised(dpy, win);
return 1;
}
struct WmOps WmEwmhOps = {
.probe = ewmhProbe,
.startup = ewmhStartup,
.winlist = ewmh_initWinlist,
.setFocus = ewmhSetFocus,
.getActiveWindow = ewmh_getActiveWindow,
.skipWindowInTaskbar = ewmh_skipWindowInTaskbar,
.skipFocusChangeEvent = ewmhSkipFocusChangeEvent,
.eventMask = ewmhEventMask,
};