-
Notifications
You must be signed in to change notification settings - Fork 0
/
xinstance.cpp
210 lines (194 loc) · 5.79 KB
/
xinstance.cpp
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
/**
* Sigstoped
* Copyright (C) 2020 Carl Klemm
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "xinstance.h"
#include <X11/Xutil.h>
#include <iostream>
#include <limits.h>
#include <cstring>
#include <unistd.h>
#include "debug.h"
unsigned long XInstance::readProparty(Window wid, Atom atom, unsigned char** prop, int* format)
{
Atom returnedAtom;
unsigned long nitems;
unsigned long bytes_after;
XLockDisplay(display);
int ret = XGetWindowProperty(
display,
wid,
atom,
0, XInstance::MAX_BYTES/4, false,
AnyPropertyType,
&returnedAtom,
format,
&nitems,
&bytes_after,
prop);
XUnlockDisplay(display);
if (ret != Success)
{
std::cerr<<"XGetWindowProperty failed!\n";
return 0;
}
else return std::min((*format)/8*nitems, XInstance::MAX_BYTES);
}
Atom XInstance::getAtom(const std::string& atomName)
{
return XInternAtom(display, atomName.c_str(), true);;
}
bool XInstance::open(const std::string& xDisplayName)
{
XInitThreads();
display = XOpenDisplay(xDisplayName.c_str());
if (display == nullptr)
{
std::cerr<<"Can not open display "<<xDisplayName<<'\n';
return false;
}
screen = XDefaultScreen(display);
atoms.netActiveWindow = getAtom("_NET_ACTIVE_WINDOW");
if(atoms.netActiveWindow == 0)
{
std::cerr<<"_NET_ACTIVE_WINDOW is required\n";
return false;
}
atoms.netWmPid = getAtom("_NET_WM_PID");
if(atoms.netActiveWindow == 0)
{
std::cerr<<"_NET_WM_PID is required\n";
return false;
}
atoms.wmClientMachine = getAtom("WM_CLIENT_MACHINE");
if(atoms.netActiveWindow == 0)
{
std::cerr<<" is required\n";
return false;
}
return true;
}
Window XInstance::getActiveWindow()
{
unsigned char* data = nullptr;
int format;
unsigned long length = readProparty(RootWindow(display, screen), atoms.netActiveWindow, &data, &format);
Window wid = 0;
if(format == 32 && length == 4) wid = *reinterpret_cast<Window*>(data);
XLockDisplay(display);
XFree(data);
XUnlockDisplay(display);
return wid;
}
std::vector<Window> XInstance::getTopLevelWindows()
{
Window root_return;
Window parent_return;
Window* windows = nullptr;
unsigned int nwindows;
XLockDisplay(display);
XQueryTree(display, RootWindow(display, screen), &root_return, &parent_return, &windows, &nwindows);
XUnlockDisplay(display);
std::vector<Window> out;
out.reserve(nwindows);
for(unsigned int i = 0; i < nwindows; ++i)
{
out.push_back(windows[i]);
}
XLockDisplay(display);
if(windows != nullptr) XFree(windows);
XUnlockDisplay(display);
return out;
}
void XInstance::flush()
{
XLockDisplay(display);
XFlush(display);
XUnlockDisplay(display);
}
pid_t XInstance::getPid(Window wid)
{
defaultHandler = XSetErrorHandler(ignoreErrorHandler);
XTextProperty xWidHostNameTextProperty;
bool ret;
XLockDisplay(display);
ret = XGetTextProperty(display, wid, &xWidHostNameTextProperty, atoms.wmClientMachine);
XUnlockDisplay(display);
if (!ret)
{
char errorString[1024];
XGetErrorText(display, ret, errorString, 1024);
debug("XGetWMClientMachine failed! " + std::string(errorString));
if(!ignoreClientMachine)
{
XSetErrorHandler(defaultHandler);
return -1;
}
}
char** xWidHostNameStringList = nullptr;
int nStrings;
ret = XTextPropertyToStringList(&xWidHostNameTextProperty, &xWidHostNameStringList, &nStrings);
if (!ret || nStrings == 0)
{
char errorString[1024];
XGetErrorText(display, ret, errorString, 1024);
debug("XTextPropertyToStringList failed! " + std::string(errorString));
if(!ignoreClientMachine)
{
XSetErrorHandler(defaultHandler);
return -1;
}
}
char hostName[HOST_NAME_MAX+1]={0};
if(gethostname(hostName, HOST_NAME_MAX) != 0)
{
debug("Can't get host name");
if(!ignoreClientMachine)
{
XSetErrorHandler(defaultHandler);
return -1;
}
}
pid_t pid = -1;
if(ignoreClientMachine || strcmp(hostName, xWidHostNameStringList[0]) == 0 )
{
unsigned char* data = nullptr;
int format;
unsigned long length = readProparty(wid, atoms.netWmPid, &data, &format);
if(format == 32 && length == 4) pid = *reinterpret_cast<pid_t*>(data);
XFree(data);
}
else
{
debug("Window "+std::to_string(wid)+" is a remote window");
}
if(xWidHostNameStringList) XFreeStringList(xWidHostNameStringList);
XSetErrorHandler(defaultHandler);
return pid;
}
int XInstance::ignoreErrorHandler(Display* display, XErrorEvent* xerror)
{
std::cerr<<"Ignoring: error code"<<xerror->error_code<<" request code "<<xerror->request_code<<'\n'
<<"this error most likely occured because of a bug in your WM\n";
return 0;
}
XInstance::~XInstance()
{
XLockDisplay(display);
if(display) XCloseDisplay(display);
XUnlockDisplay(display);
}