Debian Codegraph @

hyprtester/clients/pointer-warp.cpp

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