-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
275 lines (251 loc) · 8.09 KB
/
Copy pathwindow.cpp
File metadata and controls
275 lines (251 loc) · 8.09 KB
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
275
#include "glad/glad.h"
#include "../window.hpp"
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/shape.h>
#include <X11/extensions/Xrandr.h>
#include <vector>
#include <mutex>
#include <algorithm>
namespace lizard::platform {
namespace {
Display *g_display = nullptr;
::Window g_root = 0;
::Window g_overlay = 0;
std::mutex g_display_mutex;
std::once_flag g_xlib_init_once;
float compute_dpi(Display *dpy) {
int screen = DefaultScreen(dpy);
int width_px = DisplayWidth(dpy, screen);
int width_mm = DisplayWidthMM(dpy, screen);
float dpi = static_cast<float>(width_px) / static_cast<float>(width_mm) * 25.4f;
return dpi / 96.0f;
}
} // namespace
void init_xlib_threads() {
std::call_once(g_xlib_init_once, []() { XInitThreads(); });
}
Window create_overlay_window(const WindowDesc &desc) {
init_xlib_threads();
Window result{};
std::lock_guard<std::mutex> lock(g_display_mutex);
g_display = XOpenDisplay(nullptr);
if (!g_display) {
return result;
}
int screen = DefaultScreen(g_display);
g_root = RootWindow(g_display, screen);
XSetWindowAttributes attrs{};
attrs.override_redirect = True;
attrs.event_mask = StructureNotifyMask;
attrs.background_pixel = 0;
::Window win = XCreateWindow(g_display, g_root, desc.x, desc.y, desc.width, desc.height, 0,
CopyFromParent, InputOutput, CopyFromParent,
CWOverrideRedirect | CWEventMask | CWBackPixel, &attrs);
g_overlay = win;
XMapRaised(g_display, win);
// Click-through using shape extension
XserverRegion region = XFixesCreateRegion(g_display, nullptr, 0);
XFixesSetWindowShapeRegion(g_display, win, ShapeInput, 0, 0, region);
XFixesDestroyRegion(g_display, region);
GLXFBConfig fb = nullptr;
int nfb = 0;
int attrsList[] = {GLX_RENDER_TYPE,
GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE,
GLX_WINDOW_BIT,
GLX_DOUBLEBUFFER,
True,
GLX_RED_SIZE,
8,
GLX_GREEN_SIZE,
8,
GLX_BLUE_SIZE,
8,
GLX_ALPHA_SIZE,
8,
None};
GLXFBConfig *configs = glXChooseFBConfig(g_display, screen, attrsList, &nfb);
if (configs && nfb > 0) {
fb = configs[0];
XFree(configs);
}
using CreateContext = GLXContext (*)(Display *, GLXFBConfig, GLXContext, Bool, const int *);
auto createContext =
(CreateContext)glXGetProcAddress((const GLubyte *)"glXCreateContextAttribsARB");
int ctxAttr[] = {GLX_CONTEXT_MAJOR_VERSION_ARB,
3,
GLX_CONTEXT_MINOR_VERSION_ARB,
3,
GLX_CONTEXT_PROFILE_MASK_ARB,
GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
None};
GLXContext ctx = nullptr;
if (createContext) {
ctx = createContext(g_display, fb, nullptr, True, ctxAttr);
}
glXMakeCurrent(g_display, win, ctx);
gladLoadGL();
result.native = (void *)win;
result.dpiScale = compute_dpi(g_display);
result.glContext = ctx;
return result;
}
void destroy_window(Window &window) {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (g_display && window.native) {
glXMakeCurrent(g_display, None, nullptr);
if (window.glContext) {
glXDestroyContext(g_display, window.glContext);
window.glContext = nullptr;
}
XDestroyWindow(g_display, (::Window)window.native);
XCloseDisplay(g_display);
g_display = nullptr;
}
window.native = nullptr;
}
void poll_events(Window &window) {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (!g_display || !window.native) {
return;
}
while (XPending(g_display)) {
XEvent ev;
XNextEvent(g_display, &ev);
}
}
std::pair<float, float> cursor_pos() {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (!g_display) {
return {0.5f, 0.5f};
}
::Window root_return, child;
int root_x = 0, root_y = 0;
int win_x = 0, win_y = 0;
unsigned int mask = 0;
if (!XQueryPointer(g_display, g_root, &root_return, &child, &root_x, &root_y, &win_x, &win_y,
&mask)) {
return {0.5f, 0.5f};
}
int nmon = 0;
XRRMonitorInfo *mons = XRRGetMonitors(g_display, g_root, True, &nmon);
int min_x = 0, min_y = 0, max_x = 0, max_y = 0;
if (mons && nmon > 0) {
min_x = mons[0].x;
min_y = mons[0].y;
max_x = mons[0].x + mons[0].width;
max_y = mons[0].y + mons[0].height;
for (int i = 1; i < nmon; ++i) {
min_x = std::min(min_x, mons[i].x);
min_y = std::min(min_y, mons[i].y);
max_x = std::max(max_x, mons[i].x + mons[i].width);
max_y = std::max(max_y, mons[i].y + mons[i].height);
}
} else {
int screen = DefaultScreen(g_display);
min_x = 0;
min_y = 0;
max_x = DisplayWidth(g_display, screen);
max_y = DisplayHeight(g_display, screen);
}
if (mons)
XRRFreeMonitors(mons);
float w = static_cast<float>(max_x - min_x);
float h = static_cast<float>(max_y - min_y);
float x = w > 0.0f ? static_cast<float>(root_x - min_x) / w : 0.5f;
float y = h > 0.0f ? static_cast<float>(root_y - min_y) / h : 0.5f;
x = std::clamp(x, 0.0f, 1.0f);
y = std::clamp(y, 0.0f, 1.0f);
return {x, y};
}
bool fullscreen_window_present() {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (!g_display) {
return false;
}
Atom listAtom = XInternAtom(g_display, "_NET_CLIENT_LIST_STACKING", False);
Atom type;
int format;
unsigned long nitems, bytes;
unsigned char *data = nullptr;
if (XGetWindowProperty(g_display, g_root, listAtom, 0, ~0L, False, XA_WINDOW, &type, &format,
&nitems, &bytes, &data) != Success ||
!data) {
if (data)
XFree(data);
return false;
}
std::vector<::Window> stack(reinterpret_cast<::Window *>(data),
reinterpret_cast<::Window *>(data) + nitems);
XFree(data);
int nmon = 0;
XRRMonitorInfo *mons = XRRGetMonitors(g_display, g_root, True, &nmon);
struct Mon {
int x, y, w, h;
};
std::vector<Mon> monitors;
std::vector<bool> seen;
if (mons && nmon > 0) {
for (int i = 0; i < nmon; ++i) {
monitors.push_back({mons[i].x, mons[i].y, mons[i].width, mons[i].height});
}
seen.resize(monitors.size(), false);
} else {
int screen = DefaultScreen(g_display);
monitors.push_back({0, 0, DisplayWidth(g_display, screen), DisplayHeight(g_display, screen)});
seen.resize(1, false);
}
if (mons)
XRRFreeMonitors(mons);
bool full = false;
for (auto it = stack.rbegin(); it != stack.rend() && !full; ++it) {
::Window win = *it;
if (win == g_overlay) {
continue;
}
XWindowAttributes attrs;
if (!XGetWindowAttributes(g_display, win, &attrs) || attrs.map_state != IsViewable) {
continue;
}
int wx = 0, wy = 0;
::Window child;
XTranslateCoordinates(g_display, win, g_root, 0, 0, &wx, &wy, &child);
for (std::size_t i = 0; i < monitors.size(); ++i) {
if (seen[i]) {
continue;
}
auto &m = monitors[i];
if (wx <= m.x && wy <= m.y && wx + attrs.width >= m.x + m.w &&
wy + attrs.height >= m.y + m.h) {
full = true;
break;
}
if (wx < m.x + m.w && wx + attrs.width > m.x && wy < m.y + m.h && wy + attrs.height > m.y) {
seen[i] = true;
}
}
}
return full;
}
void make_context_current(Window &window) {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (g_display && window.native && window.glContext) {
glXMakeCurrent(g_display, static_cast<GLXDrawable>(reinterpret_cast<::Window>(window.native)),
window.glContext);
}
}
void clear_current_context(Window &) {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (g_display) {
glXMakeCurrent(g_display, None, nullptr);
}
}
void swap_buffers(Window &window) {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (g_display && window.native) {
glXSwapBuffers(g_display, static_cast<GLXDrawable>(reinterpret_cast<::Window>(window.native)));
}
}
} // namespace lizard::platform