-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxutil.hpp
234 lines (192 loc) · 7.42 KB
/
xutil.hpp
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
/*
* WARNING
* ChatGPT basically wrote this entire file. I have no interest in working with a window protocol from 1984, although more recently X11 is from 2012.
* Regardless, it's not like im using ChatGPT for everything. Just a little less pain with legacy software. This really isn't fun.
*
*
* October 2024: After learning the protocol a bit more; finding window information isn't really that bad. There is still some C related nonsense that
* should not still be around in our current year.
*/
#include <X11/Xos.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xdbe.h>
#include <unistd.h>
#include <climits>
#include <cstring>
#include <string>
#define BASIC_EVENT_MASK (StructureNotifyMask|ExposureMask|PropertyChangeMask|EnterWindowMask|LeaveWindowMask|KeyPressMask|KeyReleaseMask|KeymapStateMask)
#define NOT_PROPAGATE_MASK (KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask|PointerMotionMask|ButtonMotionMask)
// Create a XColor from 3 byte tuple (0 - 255, 0 - 255, 0 - 255).
inline XColor createXColorFromRGBA(short red, short green, short blue, short alpha, Display* d, int screen) {
XColor color;
// m_color.red = red * 65535 / 255;
color.red = (red * 0xFFFF) / 0xFF;
color.green = (green * 0xFFFF) / 0xFF;
color.blue = (blue * 0xFFFF) / 0xFF;
color.flags = DoRed | DoGreen | DoBlue;
if (!XAllocColor(d, DefaultColormap(d, screen), &color)) {
printf("createXColorFromRGB: Cannot create color\n");
}
*(&color.pixel) = ((*(&color.pixel)) & 0x00ffffff) | (alpha << 24);
return color;
}
// Create a XColor from 3 byte tuple (0 - 255, 0 - 255, 0 - 255).
inline XColor createXColorFromRGB(short red, short green, short blue, Display* d, int screen) {
return createXColorFromRGBA(red, green, blue, 255, d, screen);
}
//chatgpt wrote this
inline void getWindowPosition(Display* d, Window win, int& x, int& y) {
XWindowAttributes attr;
if (XGetWindowAttributes(d, win, &attr)) {
x = attr.x;
y = attr.y;
}
}
//chatgpt wrote this
inline bool isKeyDown(Display* d, int key) {
char keys[32];
XQueryKeymap(d, keys);
KeyCode keyCode = XKeysymToKeycode(d, key);
return (keys[keyCode / 8] & (1 << (keyCode % 8))) != 0;
}
//chatgpt assisted
inline bool isMouseDown(Display* d, int mouse) {
Window root = DefaultRootWindow(d);
XEvent event;
XQueryPointer(d, root, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
if (event.xbutton.state & mouse) {
return true;
}
else {
return false;
}
}
//Halts the program until that pid is in focus, yes, the only purpose is to get the Window object of the game
//chatgpt assisted
inline XWindowAttributes getWindowAttributesFromPid(Display *display, pid_t pid) {
XWindowAttributes windowAttr;
Window root = DefaultRootWindow(display);
Atom actualType;
int format;
unsigned long nItems, bytesAfter;
unsigned char *propData = NULL;
Window root_return, parent_return;
Window *children;
unsigned int nchildren;
if (!XQueryTree(display, root, &root_return, &parent_return, &children, &nchildren)) return windowAttr;
Atom prop = XInternAtom(display, "_NET_WM_PID", False);
for (unsigned int i = 0; i < nchildren; ++i) {
Window window = children[i];
if (XGetWindowProperty(display, window, prop, 0, LONG_MAX / 4, False, AnyPropertyType,
&actualType, &format, &nItems, &bytesAfter, &propData) == Success) {
if (propData != NULL) {
pid_t *pidArray = reinterpret_cast<pid_t *>(propData);
pid_t windowPid = pidArray[0];
if (windowPid == pid) {
XFree(propData);
XGetWindowAttributes(display, window, &windowAttr);
return windowAttr;
}
}
}
}
return windowAttr;
}
inline void getDefaultDisplayResolution(int resolution[2]) {
FILE* fp = popen("xrandr | grep \"$(xrandr --listmonitors | grep \'*\' | awk {\'print $4\'})\" | awk {\'print $4\'} | grep -Eo \'[0-9x0-9]+\' | awk \'NR==1{print $1}\'", "r");
char buffer[1024];
int x, y;
if (fp == NULL) {
resolution = 0;
return;
}
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
std::string num_buffer = "";
for (unsigned int i = 0; i < strlen(buffer); ++i) {
if (buffer[i] == 'x') { x = atoi(num_buffer.c_str()); num_buffer = ""; continue; }
num_buffer += buffer[i];
}
y = atoi(num_buffer.c_str());
}
pclose(fp);
resolution[0] = x;
resolution[1] = y;
}
//chatgpt wrote this
inline Window getFocusedWindow(Display* d) {
Window focusedWindow;
int revert;
XGetInputFocus(d, &focusedWindow, &revert);
return focusedWindow;
}
//Get pid from window
//chatgpt wrote this
inline pid_t getPidByWindow(Display *display, Window window) {
Atom prop = XInternAtom(display, "_NET_WM_PID", False);
Atom actualType;
int format;
unsigned long nItems, bytesAfter;
unsigned char *propData = NULL;
if (XGetWindowProperty(display, window, prop, 0, LONG_MAX / 4, False, AnyPropertyType,
&actualType, &format, &nItems, &bytesAfter, &propData) == Success) {
if (propData != NULL) {
pid_t *pidArray = reinterpret_cast<pid_t *>(propData);
pid_t pid = pidArray[0];
XFree(propData);
return pid;
}
}
return -1; // Unable to retrieve PID
}
//double buffering examples: https://github.com/tsoding/x11-double-buffering/blob/master/db_xdbe.c
inline void db_clear(XdbeBackBuffer back_buffer, Display* d, GC gc)
{
XSetForeground(d, gc, 0);
XFillRectangle(d, back_buffer, gc, 0, 0, 1920, 1080);
}
inline void db_rect(XdbeBackBuffer back_buffer, Display* d, GC gc, int x, int y, unsigned int w, unsigned int h)
{
XDrawRectangle(d, back_buffer, gc, x, y, w, h);
}
inline void db_fillrect(XdbeBackBuffer back_buffer, Display* d, GC gc, int x, int y, unsigned int w, unsigned int h)
{
XFillRectangle(d, back_buffer, gc, x, y, w, h);
}
inline void db_line(XdbeBackBuffer back_buffer, Display* d, GC gc, int x1, int y1, int x2, int y2)
{
XDrawLine(d, back_buffer, gc, x1, y1, x2, y2);
}
//chatgipidy assisted
inline void db_thickline(XdbeBackBuffer back_buffer, Display* d, GC gc, int x1, int y1, int x2, int y2, int thickness, int distance, float scale_factor) {
float slope = (y2-y1+1)/(x2-x1+1);
float slope_inverse = -(1/slope);
if (scale_factor > 0) {
for (int i = 1; i <= thickness; ++i) {
if (y1 == y2) {
XDrawLine(d, back_buffer, gc, x1, y1+(i*((distance*scale_factor)/distance)), x2, y2+(i*((distance*scale_factor)/distance)));
XDrawLine(d, back_buffer, gc, x1, y1-(i*((distance*scale_factor)/distance)), x2, y2-(i*((distance*scale_factor)/distance)));
} else {
XDrawLine(d, back_buffer, gc, x1+(i*((distance*scale_factor)/distance)), y1, x2+(i*((distance*scale_factor)/distance)), y2);
XDrawLine(d, back_buffer, gc, x1-(i*((distance*scale_factor)/distance)), y1, x2-(i*((distance*scale_factor)/distance)), y2);
}
}
} else {
for (int i = 0; i <= thickness; ++i) {
XDrawLine(d, back_buffer, gc, x1+i+slope, y1+i+slope, x2-i-slope, y2-i-slope);
XDrawLine(d, back_buffer, gc, x1-i-slope, y1-i-slope, x2+i+slope, y2+i+slope);
}
}
}
inline void db_swap_buffers(Display* d, Window win)
{
XdbeSwapInfo swap_info;
swap_info.swap_window = win;
swap_info.swap_action = 0;
XdbeSwapBuffers(d, &swap_info, 1);
}