forked from sagb/alttab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx.c
173 lines (152 loc) · 4.86 KB
/
x.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
/*
Interface with foreign windows in raw X11.
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 <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xft/Xft.h>
#include <X11/Xatom.h>
#include <stdbool.h>
#include <stdio.h>
#include "alttab.h"
#include "util.h"
extern Globals g;
extern Display *dpy;
extern int scr;
extern Window root;
// PRIVATE
//
// get window group leader
// to be used later. rewritten, not tested.
//
Window x_get_leader(Window win)
{
Window *retprop;
XWMHints *h;
Window leader = None;
retprop =
(Window *) get_x_property(win, XA_WINDOW, "WM_CLIENT_LEADER", NULL);
if (retprop != NULL) {
leader = retprop[0];
XFree(retprop);
} else {
if (!(h = XGetWMHints(dpy, win)))
return None;
if (h->flags & WindowGroupHint)
leader = h->window_group;
}
return leader;
}
// PUBLIC
//
// set winlist,maxNdx recursively using raw Xlib
// first call should be (win=root, reclevel=0)
//
int x_initWindowsInfoRecursive(Window win, int reclevel)
{
Window root, parent;
Window *children;
unsigned int nchildren, i;
// Window leader;
XWindowAttributes wa;
char *winname;
// check if window is "leader" or no prop, skip otherwise
// caveat: in rp, gvim leader has no file name and icon
// this doesn't work in raw X: leader may be not viewable.
// doesn't work in twm either: qutebrowser has meaningless leader.
/*
leader = 0;
if (g.option_wm == WM_TWM) {
leader = x_get_leader (win);
msg(1, "win: 0x%lx leader: 0x%lx\n", win, leader);
}
*/
// in non-twm, add viewable only
// caveat: in rp, skips anything except of visible window
// probably add an option for this in WMs too?
wa.map_state = 0;
if (g.option_wm != WM_TWM)
XGetWindowAttributes(dpy, win, &wa);
// in twm-like, add only windows with a name
winname = NULL;
if (g.option_wm == WM_TWM) {
winname = get_x_property(win, XA_STRING, "WM_NAME", NULL);
}
// insert detailed window data in window list
if ((g.option_wm == WM_TWM || wa.map_state == IsViewable)
&& reclevel != 0 && (g.option_wm != WM_TWM || winname != NULL)
// && (g.option_wm != WM_TWM || leader == win)
&& !common_skipWindow(win, DESKTOP_UNKNOWN, DESKTOP_UNKNOWN)
) {
addWindowInfo(win, reclevel, 0, DESKTOP_UNKNOWN, winname);
}
// skip children if max recursion level reached
if (g.option_max_reclevel != -1 && reclevel >= g.option_max_reclevel)
return 1;
// recursion
if (XQueryTree(dpy, win, &root, &parent, &children, &nchildren) == 0) {
msg(0, "can't get window tree for 0x%lx\n", win);
return 0;
}
for (i = 0; i < nchildren; ++i) {
x_initWindowsInfoRecursive(children[i], reclevel + 1);
}
if (nchildren > 0 && children) {
XFree(children);
}
return 1;
}
//
// set window focus in raw X
//
int x_setFocus(int wndx)
{
Window w = g.winlist[wndx].id;
// 1. XWarpPointer
// If such WMs would be discovered that prevent our focus
// AND set their own focus via the pointer only.
// 2. XRaiseWindow required, but doesn't make window "Viewable"
XRaiseWindow(dpy, w);
// 3. XSetInputFocus
// "The specified focus window must be viewable at the time
// XSetInputFocus is called, or a BadMatch error results."
// This check is redundant: non-viewable windows isn't added to winlist in raw X anyway
XWindowAttributes att;
XGetWindowAttributes(dpy, w, &att);
if (att.map_state == IsViewable)
XSetInputFocus(dpy, w, RevertToParent, CurrentTime);
return 1;
}
//
// this is where alttab is supposed to set properties or
// register interest in event for ANY foreign window encountered.
// warning: this is called only on addition to sortlist.
//
void x_setCommonPropertiesForAnyWindow(Window win)
{
long evmask = 0;
// root and our window are treated elsewhere
if (win == root || win == getUiwin())
return;
// for delete notification
evmask |= StructureNotifyMask;
// for focusIn notification
if (g.option_wm != WM_EWMH) {
msg(0, "using direct focus tracking for 0x%lx\n", win);
evmask |= FocusChangeMask;
}
// warning: this overwrites previous value
if (evmask != 0)
XSelectInput(dpy, win, evmask);
}