-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwintools.cpp
208 lines (173 loc) · 4.96 KB
/
wintools.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
#include "wintools.h"
#include <QList>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xos.h>
#include <X11/extensions/shape.h>
#ifndef NO_I18N
#include <X11/Xlocale.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <locale.h>
#include <glib.h>
#define MAXSTR 1000
#define MAX_PROPERTY_VALUE_LEN 4096
class Instance {
public:
inline ~Instance() {
XCloseDisplay(display);
}
Display *display;
unsigned long window;
unsigned char *prop;
int screen = 0;
bool loaded = false;
};
static Instance *instance = new Instance;
void load()
{
instance->display = XOpenDisplay(nullptr);
}
static bool check_status(int status, unsigned long window)
{
if (status != Success) {
printf("XGetWindowProperty failed!");
return true;
}
if (status == BadWindow) {
printf("window id # 0x%lx does not exists!", window);
}
return false;
}
static unsigned char* get_string_property(char* property_name)
{
Atom actual_type, filter_atom;
int actual_format, status;
unsigned long nitems, bytes_after;
filter_atom = XInternAtom(instance->display, property_name, True);
status = XGetWindowProperty(instance->display, instance->window, filter_atom, 0, MAXSTR, False, AnyPropertyType,
&actual_type, &actual_format, &nitems, &bytes_after, &instance->prop);
check_status(status, instance->window);
return instance->prop;
}
static unsigned long get_long_property(char* property_name)
{
get_string_property(property_name);
unsigned long long_property = instance->prop[0] + (instance->prop[1]<<8) + (instance->prop[2]<<16) + (instance->prop[3]<<24);
return long_property;
}
static Window *winlist (Display *disp, unsigned long *len) {
Atom prop = XInternAtom(disp,"_NET_CLIENT_LIST",False), type;
int form;
unsigned long remain;
unsigned char *list;
errno = 0;
if (XGetWindowProperty(disp,XDefaultRootWindow(disp),prop,0,1024,False,XA_WINDOW,
&type,&form,len,&remain,&list) != Success) {
perror("winlist() -- GetWinProp");
return nullptr;
}
return (Window*)list;
}
static char *get_property(Display *disp, Window win, Atom xa_prop_type, char *prop_name, unsigned long *size)
{
Atom xa_prop_name;
Atom xa_ret_type;
int ret_format;
unsigned long ret_nitems;
unsigned long ret_bytes_after;
unsigned long tmp_size;
unsigned char *ret_prop;
char *ret;
xa_prop_name = XInternAtom(disp, prop_name, False);
/* MAX_PROPERTY_VALUE_LEN / 4 explanation (XGetWindowProperty manpage):
*
* long_length = Specifies the length in 32-bit multiples of the
* data to be retrieved.
*/
if (XGetWindowProperty(disp, win, xa_prop_name, 0, MAX_PROPERTY_VALUE_LEN / 4, False,
xa_prop_type, &xa_ret_type, &ret_format,
&ret_nitems, &ret_bytes_after, &ret_prop) != Success) {
return nullptr;
}
if (xa_ret_type != xa_prop_type) {
XFree(ret_prop);
return nullptr;
}
/* null terminate the result to make string handling easier */
tmp_size = (ret_format / (32 / sizeof(long))) * ret_nitems;
gsize sized = tmp_size+1;
ret = (char *)g_malloc(sized);
memcpy((void*)ret, ret_prop, tmp_size);
ret[tmp_size] = '\0';
if (size) {
*size = tmp_size;
}
XFree(ret_prop);
return ret;
}
static char *winame(int winID)
{
Atom prop = XInternAtom(instance->display,"WM_NAME",False), type;
int form;
unsigned long remain, len;
unsigned char *list;
errno = 0;
if (XGetWindowProperty(instance->display, instance->window, prop, 0, 1024, False, XA_STRING,
&type,&form,&len,&remain,&list) != Success) {
perror("winlist() -- GetWinProp");
return nullptr;
}
return (char*)list;
}
LDA_BEGIN_NAMESPACE
bool isFromPID(int win, int pid)
{
if (!instance->loaded) {
load();
}
unsigned long len;
Window *list;
if (!instance->display) {
puts("no display!");
return false;
}
list = (Window*)winlist(instance->display,&len);
std::cout << "Total of windows: " << (int)len;
instance->window = win;
if (((int)get_long_property((char *)"_NET_WM_PID")) == pid) {
return true;
}
XFree(list);
return false;
}
QList<int> pidToWins(int pid)
{
if (!instance->loaded) {
load();
}
QList<int> out;
int i;
unsigned long len;
Window *list;
if (!instance->display) {
puts("no display!");
return out;
}
list = (Window*)winlist(instance->display,&len);
std::cout << "Total of windows: " << (int)len;
for (i=0;i<(int)len;i++) {
instance->window = list[i];
if (((int)get_long_property((char *)"_NET_WM_PID")) == pid) {
out << (int)instance->window;
}
}
XFree(list);
return out;
}
LDA_END_NAMESPACE