Debian Codegraph @

hyprtester/clients/pointer-scroll.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 #include <fstream>
10 #include <utility>
11
12 #include <wayland-client.h>
13 #include <wayland.hpp>
14 #include <xdg-shell.hpp>
15 #include <pointer-warp-v1.hpp>
16
17 #include <hyprutils/memory/SharedPtr.hpp>
18 #include <hyprutils/math/Vector2D.hpp>
19 #include <hyprutils/os/FileDescriptor.hpp>
20
21 using Hyprutils::Math::Vector2D;
22 using namespace Hyprutils::Memory;
23
24 struct SWlState {
25 wl_display* display;
26 CSharedPointer<CCWlRegistry> registry;
27
28 // protocols
29 CSharedPointer<CCWlCompositor> wlCompositor;
30 CSharedPointer<CCWlSeat> wlSeat;
31 CSharedPointer<CCWlShm> wlShm;
32 CSharedPointer<CCXdgWmBase> xdgShell;
33
34 // shm/buffer stuff
35 CSharedPointer<CCWlShmPool> shmPool;
36 CSharedPointer<CCWlBuffer> shmBuf;
37 int shmFd;
38 size_t shmBufSize;
39 bool xrgb8888_support = false;
40
41 // surface/toplevel stuff
42 CSharedPointer<CCWlSurface> surf;
43 CSharedPointer<CCXdgSurface> xdgSurf;
44 CSharedPointer<CCXdgToplevel> xdgToplevel;
45 Vector2D geom;
46
47 // pointer
48 CSharedPointer<CCWlPointer> pointer;
49 uint32_t enterSerial;
50
51 // last delta
52 float lastScrollDelta = -1.F;
53 bool writeDelta = false;
54 };
55
56 static std::ofstream logfile;
57
58 static bool debug, started, shouldExit;
59
60 template <typename... Args>
61 //NOLINTNEXTLINE
62 static void clientLog(std::format_string<Args...> fmt, Args&&... args) {
63 std::string text = std::format(fmt, std::forward<Args>(args)...);
64 std::println("{}", text);
65 logfile << text << std::endl;
66 std::fflush(stdout);
67 }
68
69 template <typename... Args>
70 //NOLINTNEXTLINE
71 static void debugLog(std::format_string<Args...> fmt, Args&&... args) {
72 std::string text = std::format(fmt, std::forward<Args>(args)...);
73 logfile << text << std::endl;
74 if (!debug)
75 return;
76 std::println("{}", text);
77 std::fflush(stdout);
78 }
79
80 static bool bindRegistry(SWlState& state) {
81 state.registry = makeShared<CCWlRegistry>((wl_proxy*)wl_display_get_registry(state.display));
82
83 state.registry->setGlobal([&](CCWlRegistry* r, uint32_t id, const char* name, uint32_t version) {
84 const std::string NAME = name;
85 if (NAME == "wl_compositor") {
86 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
87 state.wlCompositor = makeShared<CCWlCompositor>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_compositor_interface, 6));
88 } else if (NAME == "wl_shm") {
89 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
90 state.wlShm = makeShared<CCWlShm>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_shm_interface, 1));
91 } else if (NAME == "wl_seat") {
92 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
93 state.wlSeat = makeShared<CCWlSeat>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_seat_interface, 9));
94 } else if (NAME == "xdg_wm_base") {
95 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
96 state.xdgShell = makeShared<CCXdgWmBase>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &xdg_wm_base_interface, 1));
97 }
98 });
99 state.registry->setGlobalRemove([](CCWlRegistry* r, uint32_t id) { debugLog("Global {} removed", id); });
100
101 wl_display_roundtrip(state.display);
102
103 if (!state.wlCompositor || !state.wlShm || !state.wlSeat || !state.xdgShell) {
104 clientLog("Failed to get protocols from Hyprland");
105 return false;
106 }
107
108 return true;
109 }
110
111 static bool createShm(SWlState& state, Vector2D geom) {
112 if (!state.xrgb8888_support)
113 return false;
114
115 size_t stride = geom.x * 4;
116 size_t size = geom.y * stride;
117 if (!state.shmPool) {
118 const char* name = "/wl-shm-pointer-scroll";
119 state.shmFd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
120 if (state.shmFd < 0)
121 return false;
122
123 if (shm_unlink(name) < 0 || ftruncate(state.shmFd, size) < 0) {
124 close(state.shmFd);
125 return false;
126 }
127
128 state.shmPool = makeShared<CCWlShmPool>(state.wlShm->sendCreatePool(state.shmFd, size));
129 if (!state.shmPool->resource()) {
130 close(state.shmFd);
131 state.shmFd = -1;
132 state.shmPool.reset();
133 return false;
134 }
135 state.shmBufSize = size;
136 } else if (size > state.shmBufSize) {
137 if (ftruncate(state.shmFd, size) < 0) {
138 close(state.shmFd);
139 state.shmFd = -1;
140 state.shmPool.reset();
141 return false;
142 }
143
144 state.shmPool->sendResize(size);
145 state.shmBufSize = size;
146 }
147
148 auto buf = makeShared<CCWlBuffer>(state.shmPool->sendCreateBuffer(0, geom.x, geom.y, stride, WL_SHM_FORMAT_XRGB8888));
149 if (!buf->resource())
150 return false;
151
152 if (state.shmBuf) {
153 state.shmBuf->sendDestroy();
154 state.shmBuf.reset();
155 }
156
157 state.shmBuf = buf;
158
159 return true;
160 }
161
162 static bool setupToplevel(SWlState& state) {
163 state.wlShm->setFormat([&](CCWlShm* p, uint32_t format) {
164 if (format == WL_SHM_FORMAT_XRGB8888)
165 state.xrgb8888_support = true;
166 });
167
168 state.xdgShell->setPing([&](CCXdgWmBase* p, uint32_t serial) { state.xdgShell->sendPong(serial); });
169
170 state.surf = makeShared<CCWlSurface>(state.wlCompositor->sendCreateSurface());
171 if (!state.surf->resource())
172 return false;
173
174 state.xdgSurf = makeShared<CCXdgSurface>(state.xdgShell->sendGetXdgSurface(state.surf->resource()));
175 if (!state.xdgSurf->resource())
176 return false;
177
178 state.xdgToplevel = makeShared<CCXdgToplevel>(state.xdgSurf->sendGetToplevel());
179 if (!state.xdgToplevel->resource())
180 return false;
181
182 state.xdgToplevel->setClose([&](CCXdgToplevel* p) { exit(0); });
183
184 state.xdgToplevel->setConfigure([&](CCXdgToplevel* p, int32_t w, int32_t h, wl_array* arr) {
185 state.geom = {1280, 720};
186
187 if (!createShm(state, state.geom))
188 exit(-1);
189 });
190
191 state.xdgSurf->setConfigure([&](CCXdgSurface* p, uint32_t serial) {
192 if (!state.shmBuf)
193 debugLog("xdgSurf configure but no buf made yet?");
194
195 state.xdgSurf->sendSetWindowGeometry(0, 0, state.geom.x, state.geom.y);
196 state.surf->sendAttach(state.shmBuf.get(), 0, 0);
197 state.surf->sendCommit();
198
199 state.xdgSurf->sendAckConfigure(serial);
200
201 if (!started) {
202 started = true;
203 clientLog("started");
204 }
205 });
206
207 state.xdgToplevel->sendSetTitle("pointer-scroll test client");
208 state.xdgToplevel->sendSetAppId("pointer-scroll");
209
210 state.surf->sendAttach(nullptr, 0, 0);
211 state.surf->sendCommit();
212
213 return true;
214 }
215
216 static bool setupSeat(SWlState& state) {
217 state.pointer = makeShared<CCWlPointer>(state.wlSeat->sendGetPointer());
218 if (!state.pointer->resource())
219 return false;
220
221 state.pointer->setEnter([&](CCWlPointer* p, uint32_t serial, wl_proxy* surf, wl_fixed_t x, wl_fixed_t y) {
222 debugLog("Got pointer enter event, serial {}, x {}, y {}", serial, x, y);
223 state.enterSerial = serial;
224 });
225
226 state.pointer->setAxis([&](CCWlPointer* p, uint32_t time, wl_pointer_axis axis, wl_fixed_t delta) {
227 debugLog("axis: ax {} delta {}", (int)axis, wl_fixed_to_double(delta));
228
229 if (state.writeDelta) {
230 clientLog("{:.2f}", wl_fixed_to_double(delta));
231 state.writeDelta = false;
232 state.lastScrollDelta = -1;
233 return;
234 }
235
236 state.lastScrollDelta = wl_fixed_to_double(delta);
237 state.writeDelta = true;
238 });
239
240 state.pointer->setLeave([&](CCWlPointer* p, uint32_t serial, wl_proxy* surf) { debugLog("Got pointer leave event, serial {}", serial); });
241
242 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); });
243
244 return true;
245 }
246
247 // return last delta after axis
248 static void parseRequest(SWlState& state, std::string req) {
249 if (!state.writeDelta) {
250 state.writeDelta = true;
251 return;
252 }
253
254 clientLog("{:.2f}", state.lastScrollDelta);
255 state.writeDelta = false;
256 state.lastScrollDelta = -1;
257 }
258
259 int main(int argc, char** argv) {
260 logfile.open("pointer-scroll.txt", std::ios::trunc);
261
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 logfile.flush();
318 logfile.close();
319 return 0;
320 }