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