Debian Codegraph @

hyprtester/clients/child-window.cpp

raw
1 #include <print>
2 #include <poll.h>
3 #include <sys/mman.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <unistd.h>
7
8 #include <wayland-client.h>
9 #include <wayland.hpp>
10 #include <xdg-shell.hpp>
11
12 #include <hyprutils/memory/SharedPtr.hpp>
13 #include <hyprutils/math/Vector2D.hpp>
14
15 #include <utility>
16
17 using Hyprutils::Math::Vector2D;
18 using namespace Hyprutils::Memory;
19
20 struct SWlState {
21 wl_display* display;
22 CSharedPointer<CCWlRegistry> registry;
23
24 // protocols
25 CSharedPointer<CCWlCompositor> wlCompositor;
26 CSharedPointer<CCWlSeat> wlSeat;
27 CSharedPointer<CCWlShm> wlShm;
28 CSharedPointer<CCXdgWmBase> xdgShell;
29
30 // shm/buffer stuff
31 CSharedPointer<CCWlShmPool> shmPool;
32 CSharedPointer<CCWlBuffer> shmBuf;
33 CSharedPointer<CCWlBuffer> shmBuf2;
34 int shmFd = 0;
35 size_t shmBufSize = 0;
36 bool xrgb8888_support = false;
37
38 // surface/toplevel stuff
39 CSharedPointer<CCWlSurface> surf;
40 CSharedPointer<CCXdgSurface> xdgSurf;
41 CSharedPointer<CCXdgToplevel> xdgToplevel;
42 Vector2D geom;
43
44 // pointer
45 CSharedPointer<CCWlPointer> pointer;
46 uint32_t enterSerial = 0;
47 };
48
49 bool debug, shouldExit, started;
50
51 template <typename... Args>
52 //NOLINTNEXTLINE
53 static void clientLog(std::format_string<Args...> fmt, Args&&... args) {
54 std::string text = std::format(fmt, std::forward<Args>(args)...);
55 std::println("{}", text);
56 std::fflush(stdout);
57 }
58
59 template <typename... Args>
60 //NOLINTNEXTLINE
61 static void debugLog(std::format_string<Args...> fmt, Args&&... args) {
62 std::string text = std::format(fmt, std::forward<Args>(args)...);
63 if (!debug)
64 return;
65 std::println("{}", text);
66 std::fflush(stdout);
67 }
68
69 static bool bindRegistry(SWlState& state) {
70 state.registry = makeShared<CCWlRegistry>((wl_proxy*)wl_display_get_registry(state.display));
71
72 state.registry->setGlobal([&](CCWlRegistry* r, uint32_t id, const char* name, uint32_t version) {
73 const std::string NAME = name;
74 if (NAME == "wl_compositor") {
75 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
76 state.wlCompositor = makeShared<CCWlCompositor>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_compositor_interface, 6));
77 } else if (NAME == "wl_shm") {
78 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
79 state.wlShm = makeShared<CCWlShm>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_shm_interface, 1));
80 } else if (NAME == "wl_seat") {
81 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
82 state.wlSeat = makeShared<CCWlSeat>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_seat_interface, 9));
83 } else if (NAME == "xdg_wm_base") {
84 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
85 state.xdgShell = makeShared<CCXdgWmBase>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &xdg_wm_base_interface, 1));
86 }
87 });
88 state.registry->setGlobalRemove([](CCWlRegistry* r, uint32_t id) { debugLog("Global {} removed", id); });
89
90 wl_display_roundtrip(state.display);
91
92 if (!state.wlCompositor || !state.wlShm || !state.wlSeat || !state.xdgShell) {
93 clientLog("Failed to get protocols from Hyprland");
94 return false;
95 }
96
97 return true;
98 }
99
100 static bool createShm(SWlState& state, Vector2D geom) {
101 if (!state.xrgb8888_support)
102 return false;
103
104 size_t stride = geom.x * 4;
105 size_t size = geom.y * stride;
106 if (!state.shmPool) {
107 const char* name = "/wl-shm-pointer-warp";
108 state.shmFd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
109 if (state.shmFd < 0)
110 return false;
111
112 if (shm_unlink(name) < 0 || ftruncate(state.shmFd, size * 2) < 0) {
113 close(state.shmFd);
114 return false;
115 }
116
117 state.shmPool = makeShared<CCWlShmPool>(state.wlShm->sendCreatePool(state.shmFd, size * 2));
118 if (!state.shmPool->resource()) {
119 close(state.shmFd);
120 state.shmFd = -1;
121 state.shmPool.reset();
122 return false;
123 }
124 state.shmBufSize = size;
125 } else if (size > state.shmBufSize) {
126 if (ftruncate(state.shmFd, size) < 0) {
127 close(state.shmFd);
128 state.shmFd = -1;
129 state.shmPool.reset();
130 return false;
131 }
132
133 state.shmPool->sendResize(size * 2);
134 state.shmBufSize = size;
135 }
136
137 auto buf = makeShared<CCWlBuffer>(state.shmPool->sendCreateBuffer(0, geom.x, geom.y, stride, WL_SHM_FORMAT_XRGB8888));
138 if (!buf->resource())
139 return false;
140
141 if (state.shmBuf) {
142 state.shmBuf->sendDestroy();
143 state.shmBuf.reset();
144 }
145 state.shmBuf = buf;
146
147 return true;
148 }
149
150 static bool setupToplevel(SWlState& state) {
151 state.wlShm->setFormat([&](CCWlShm* p, uint32_t format) {
152 if (format == WL_SHM_FORMAT_XRGB8888)
153 state.xrgb8888_support = true;
154 });
155
156 state.xdgShell->setPing([&](CCXdgWmBase* p, uint32_t serial) { state.xdgShell->sendPong(serial); });
157
158 state.surf = makeShared<CCWlSurface>(state.wlCompositor->sendCreateSurface());
159 if (!state.surf->resource())
160 return false;
161
162 state.xdgSurf = makeShared<CCXdgSurface>(state.xdgShell->sendGetXdgSurface(state.surf->resource()));
163 if (!state.xdgSurf->resource())
164 return false;
165
166 state.xdgToplevel = makeShared<CCXdgToplevel>(state.xdgSurf->sendGetToplevel());
167 if (!state.xdgToplevel->resource())
168 return false;
169
170 state.xdgToplevel->setClose([&](CCXdgToplevel* p) { exit(0); });
171
172 state.xdgToplevel->setConfigure([&](CCXdgToplevel* p, int32_t w, int32_t h, wl_array* arr) {
173 state.geom = {1280, 720};
174
175 if (!createShm(state, state.geom))
176 exit(-1);
177 });
178
179 state.xdgSurf->setConfigure([&](CCXdgSurface* p, uint32_t serial) {
180 if (!state.shmBuf)
181 debugLog("xdgSurf configure but no buf made yet?");
182
183 state.xdgSurf->sendSetWindowGeometry(0, 0, state.geom.x, state.geom.y);
184 state.surf->sendAttach(state.shmBuf.get(), 0, 0);
185 state.surf->sendCommit();
186
187 state.xdgSurf->sendAckConfigure(serial);
188
189 if (!started) {
190 started = true;
191 clientLog("started");
192 }
193 });
194
195 state.xdgToplevel->sendSetTitle("child-test parent");
196 state.xdgToplevel->sendSetAppId("child-test-parent");
197
198 state.surf->sendAttach(nullptr, 0, 0);
199 state.surf->sendCommit();
200
201 return true;
202 }
203
204 static bool setupSeat(SWlState& state) {
205 state.pointer = makeShared<CCWlPointer>(state.wlSeat->sendGetPointer());
206 if (!state.pointer->resource())
207 return false;
208
209 state.pointer->setEnter([&](CCWlPointer* p, uint32_t serial, wl_proxy* surf, wl_fixed_t x, wl_fixed_t y) {
210 debugLog("Got pointer enter event, serial {}, x {}, y {}", serial, x, y);
211 state.enterSerial = serial;
212 });
213
214 state.pointer->setLeave([&](CCWlPointer* p, uint32_t serial, wl_proxy* surf) { debugLog("Got pointer leave event, serial {}", serial); });
215
216 state.pointer->setMotion([&](CCWlPointer* p, uint32_t serial, wl_fixed_t x, wl_fixed_t y) { debugLog("Got pointer motion event, serial {}, x {}, y {}", serial, x, y); });
217
218 return true;
219 }
220
221 struct SChildWindow {
222 CSharedPointer<CCWlSurface> surface;
223 CSharedPointer<CCXdgSurface> xSurface;
224 CSharedPointer<CCXdgToplevel> toplevel;
225 };
226
227 static void parseRequest(SWlState& state, std::string str, SChildWindow& window) {
228 if (str.starts_with("exit")) {
229 shouldExit = true;
230 return;
231 }
232
233 size_t index = str.find_first_of('\n');
234 str = str.substr(0, index);
235
236 if (str == "toplevel") {
237 window.surface = makeShared<CCWlSurface>(state.wlCompositor->sendCreateSurface());
238 window.xSurface = makeShared<CCXdgSurface>(state.xdgShell->sendGetXdgSurface(window.surface->resource()));
239
240 window.xSurface->setConfigure([&](CCXdgSurface* p, uint32_t serial) {
241 if (!state.shmBuf)
242 debugLog("xdgSurf configure but no buf made yet?");
243
244 window.xSurface->sendSetWindowGeometry(0, 0, state.geom.x, state.geom.y);
245 window.surface->sendAttach(state.shmBuf2.get(), 0, 0);
246 window.surface->sendCommit();
247
248 window.xSurface->sendAckConfigure(serial);
249 });
250
251 window.toplevel = makeShared<CCXdgToplevel>(window.xSurface->sendGetToplevel());
252
253 window.toplevel->setConfigure([&](CCXdgToplevel* p, int32_t w, int32_t h, wl_array* arr) {
254 size_t stride = 1280 * 4;
255 size_t size = 720 * stride;
256
257 auto buf = makeShared<CCWlBuffer>(state.shmPool->sendCreateBuffer(size, state.geom.x, state.geom.y, stride, WL_SHM_FORMAT_XRGB8888));
258 if (!buf->resource())
259 clientLog("Failed to create child buffer");
260
261 if (state.shmBuf2) {
262 state.shmBuf2->sendDestroy();
263 state.shmBuf2.reset();
264 }
265 state.shmBuf2 = buf;
266 });
267
268 window.toplevel->sendSetTitle("child-test child");
269 window.toplevel->sendSetAppId("child-test-child");
270 window.toplevel->sendSetParent(state.xdgToplevel.get());
271
272 window.surface->sendAttach(nullptr, 0, 0);
273 window.surface->sendCommit();
274 clientLog("child started");
275 return;
276 }
277 }
278
279 int main(int argc, char** argv) {
280 if (argc != 1 && argc != 2)
281 clientLog("Only the \"--debug\" switch is allowed, it turns on debug logs.");
282
283 if (argc == 2 && std::string{argv[1]} == "--debug")
284 debug = true;
285
286 SWlState state;
287 SChildWindow window;
288
289 // WAYLAND_DISPLAY env should be set to the correct one
290 state.display = wl_display_connect(nullptr);
291 if (!state.display) {
292 clientLog("Failed to connect to wayland display");
293 return -1;
294 }
295
296 if (!bindRegistry(state) || !setupSeat(state) || !setupToplevel(state))
297 return -1;
298
299 std::array<char, 1024> readBuf;
300 readBuf.fill(0);
301
302 wl_display_flush(state.display);
303
304 struct pollfd fds[2] = {{.fd = wl_display_get_fd(state.display), .events = POLLIN | POLLOUT}, {.fd = STDIN_FILENO, .events = POLLIN}};
305 while (!shouldExit && poll(fds, 2, 0) != -1) {
306 if (fds[0].revents & POLLIN) {
307 wl_display_flush(state.display);
308
309 if (wl_display_prepare_read(state.display) == 0) {
310 wl_display_read_events(state.display);
311 wl_display_dispatch_pending(state.display);
312 } else
313 wl_display_dispatch(state.display);
314
315 int ret = 0;
316 do {
317 ret = wl_display_dispatch_pending(state.display);
318 wl_display_flush(state.display);
319 } while (ret > 0);
320 }
321
322 if (fds[1].revents & POLLIN) {
323 ssize_t bytesRead = read(fds[1].fd, readBuf.data(), 1023);
324 if (bytesRead == -1)
325 continue;
326 readBuf[bytesRead] = 0;
327
328 parseRequest(state, std::string{readBuf.data()}, window);
329 }
330 }
331
332 wl_display* display = state.display;
333 state = {};
334 window = {};
335
336 wl_display_disconnect(display);
337 return 0;
338 }