-
Notifications
You must be signed in to change notification settings - Fork 45
/
ewmh.c
275 lines (239 loc) · 7.35 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
/*
Interface with EWMH-compatible window managers.
Note: _WIN fallbacks are not part of EWMH or ICCCM, but kept here anyway.
Copyright 2017-2018 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
// this constant can't be 0, 1, -1, because WMs set it to these values incoherently
#define DESKTOP_UNKNOWN 0xdead
//
// returns ptr to EWMH client list and client_list_size
// or NULL
//
Window *ewmh_get_client_list(unsigned long *client_list_size)
{
Window *client_list;
if ((client_list =
(Window *) get_x_property(root, XA_WINDOW, "_NET_CLIENT_LIST",
client_list_size)) == NULL) {
if ((client_list =
(Window *) get_x_property(root, XA_CARDINAL,
"_WIN_CLIENT_LIST",
client_list_size)) == NULL) {
return 0;
}
}
return client_list;
}
// PUBLIC
//
// return the name of EWMH-compatible WM
// or NULL if not found
//
char *ewmh_getWmName()
{
Window *chld_win;
char *r;
Atom utf8string;
char *default_wm_name = "unknown_ewmh_compatible";
unsigned long client_list_size;
// 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".
// first, detect necessary feature: client list
if (ewmh_get_client_list(&client_list_size) == NULL) {
// WM is not usable in EWMH mode
return (char *)NULL;
}
// then, guess/devise WM name
chld_win = (Window *) NULL;
if (! (chld_win = (Window *) get_x_property(root, XA_WINDOW, "_NET_SUPPORTING_WM_CHECK", NULL))) {
if (! (chld_win = (Window *) get_x_property(root, XA_CARDINAL, "_WIN_SUPPORTING_WM_CHECK", NULL))) {
return default_wm_name;
}
}
r = (char *)NULL;
utf8string = XInternAtom(dpy, "UTF8_STRING", False);
if (!
(r =
get_x_property(*chld_win, utf8string, "_NET_WM_NAME",
NULL))) {
(r =
get_x_property(*chld_win, XA_STRING, "_NET_WM_NAME",
NULL));
}
if (chld_win!=NULL)
free(chld_win);
return (r!=NULL) ? r : default_wm_name;
}
//
// initialize winlist/startNdx
// return 1 if ok
//
int ewmh_initWinlist()
{
Window *client_list;
unsigned long client_list_size;
int i;
Window aw;
char *awp;
unsigned long sz;
char *title;
unsigned long current_desktop, window_desktop;
aw = (Window) 0;
current_desktop = ewmh_getCurrentDesktop();
if ((awp = get_x_property(root, XA_WINDOW, "_NET_ACTIVE_WINDOW", &sz))) {
aw = *((Window *) awp);
free(awp);
} else {
if (g.debug > 0)
fprintf(stderr, "can't obtain _NET_ACTIVE_WINDOW\n");
// not mandatory
}
if ((client_list = ewmh_get_client_list(&client_list_size)) == NULL) {
fprintf(stderr, "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)) {
if (g.debug > 1) {
fprintf (stderr, "window %lx has \"skip on taskbar\" property, skipped\n", w);
}
continue;
}
window_desktop = ewmh_getDesktopOfWindow(w);
if (current_desktop != window_desktop
&& current_desktop != DESKTOP_UNKNOWN
&& window_desktop != DESKTOP_UNKNOWN) {
if (g.debug > 1) {
fprintf (stderr, "window not on active desktop, skipped (window's %ld, current %ld)\n", window_desktop, current_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, title);
if (w == aw) {
g.startNdx = g.maxNdx-1;
}
}
if (g.debug > 1) {
fprintf(stderr, "ewmh active window: %lu index: %d name: %s\n",
aw, g.startNdx, (g.winlist ? g.winlist[g.startNdx].name : "null"));
}
return 1;
}
//
// 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;
if (g.debug>1) {
fprintf(stderr, "ewmh_setFocus %lx\n", win);
}
XEvent evt;
long rn_mask = SubstructureRedirectMask | SubstructureNotifyMask;
evt.xclient.window = win;
evt.xclient.type = ClientMessage;
evt.xclient.message_type =
XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
evt.xclient.serial = 0;
evt.xclient.send_event = True;
evt.xclient.format = 32;
memset (&(evt.xclient.data.l[0]), 0, 5*sizeof(evt.xclient.data.l[0]));
if (!XSendEvent(dpy, root, False, rn_mask, &evt)) {
fprintf(stderr, "ewmh_activate_window: can't send xevent\n");
}
XMapRaised(dpy, win);
return 1;
}
//
// get current desktop in EWMH WM
//
unsigned long ewmh_getCurrentDesktop()
{
unsigned long *cd;
unsigned long propsize;
cd = (unsigned long*)get_x_property (root, XA_CARDINAL,
"_NET_CURRENT_DESKTOP", &propsize);
if (!cd)
cd = (unsigned long*)get_x_property (root, XA_CARDINAL,
"_WIN_WORKSPACE", &propsize);
return (cd && (propsize>0) && ((signed long)(*cd) >=0 )) ? *cd : DESKTOP_UNKNOWN;
}
//
// get desktop of window w in EWMH WM
//
unsigned long ewmh_getDesktopOfWindow(Window w)
{
unsigned long *d;
d = (unsigned long*)get_x_property (w, XA_CARDINAL,
"_NET_WM_DESKTOP", NULL);
if (!d)
d = (unsigned long*)get_x_property (w, XA_CARDINAL,
"_WIN_WORKSPACE", NULL);
return (d && ((signed long)(*d) >= 0)) ? *d : DESKTOP_UNKNOWN;
}
//
// does window have _NET_WM_STATE_SKIP_TASKBAR
//
bool ewmh_skipWindowInTaskbar(Window w)
{
Atom *state;
long unsigned int state_propsize;
Atom a_skip_tb;
int i;
state = (Atom*)get_x_property(w, XA_ATOM, "_NET_WM_STATE", &state_propsize);
if (state == NULL || state_propsize == 0) {
if (g.debug>1) {
fprintf (stderr, "%lx: no _NET_WM_STATE property\n", w);
}
return false;
}
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) {
if (g.debug>1) {
fprintf (stderr, "%lx: _NET_WM_STATE_SKIP_TASKBAR found\n", w);
}
return true;
}
}
return false;
}