Debian Codegraph @

hyprtester/clients/keyboard-modifiers.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
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 CSharedPointer<CCWlCompositor> wlCompositor;
27 CSharedPointer<CCWlSeat> wlSeat;
28 CSharedPointer<CCWlShm> wlShm;
29 CSharedPointer<CCXdgWmBase> xdgShell;
30
31 CSharedPointer<CCWlShmPool> shmPool;
32 CSharedPointer<CCWlBuffer> shmBuf;
33 int shmFd;
34 size_t shmBufSize;
35 bool xrgb8888_support = false;
36
37 CSharedPointer<CCWlSurface> surf;
38 CSharedPointer<CCXdgSurface> xdgSurf;
39 CSharedPointer<CCXdgToplevel> xdgToplevel;
40 Vector2D geom;
41
42 CSharedPointer<CCWlKeyboard> keyboard;
43 uint32_t lastLocked = 0;
44 };
45
46 static std::ofstream logfile;
47 static bool debug, started, shouldExit;
48
49 template <typename... Args>
50 static void clientLog(std::format_string<Args...> fmt, Args&&... args) {
51 std::string text = std::vformat(fmt.get(), std::make_format_args(args...));
52 std::println("{}", text);
53 logfile << text << std::endl;
54 std::fflush(stdout);
55 }
56
57 template <typename... Args>
58 static void debugLog(std::format_string<Args...> fmt, Args&&... args) {
59 std::string text = std::vformat(fmt.get(), std::make_format_args(args...));
60 logfile << text << std::endl;
61 if (!debug)
62 return;
63 std::println("{}", text);
64 std::fflush(stdout);
65 }
66
67 static bool bindRegistry(SWlState& state) {
68 state.registry = makeShared<CCWlRegistry>((wl_proxy*)wl_display_get_registry(state.display));
69
70 state.registry->setGlobal([&](CCWlRegistry* r, uint32_t id, const char* name, uint32_t version) {
71 const std::string NAME = name;
72 if (NAME == "wl_compositor") {
73 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
74 state.wlCompositor = makeShared<CCWlCompositor>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_compositor_interface, 6));
75 } else if (NAME == "wl_shm") {
76 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
77 state.wlShm = makeShared<CCWlShm>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_shm_interface, 1));
78 } else if (NAME == "wl_seat") {
79 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
80 state.wlSeat = makeShared<CCWlSeat>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_seat_interface, 9));
81 } else if (NAME == "xdg_wm_base") {
82 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
83 state.xdgShell = makeShared<CCXdgWmBase>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &xdg_wm_base_interface, 1));
84 }
85 });
86 state.registry->setGlobalRemove([](CCWlRegistry* r, uint32_t id) { debugLog("Global {} removed", id); });
87
88 wl_display_roundtrip(state.display);
89
90 if (!state.wlCompositor || !state.wlShm || !state.wlSeat || !state.xdgShell) {
91 clientLog("Failed to get protocols from Hyprland");
92 return false;
93 }
94
95 return true;
96 }
97
98 static bool createShm(SWlState& state, Vector2D geom) {
99 if (!state.xrgb8888_support)
100 return false;
101
102 size_t stride = geom.x * 4;
103 size_t size = geom.y * stride;
104 if (!state.shmPool) {
105 const char* name = "/wl-shm-kb-mods";
106 state.shmFd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
107 if (state.shmFd < 0)
108 return false;
109
110 if (shm_unlink(name) < 0 || ftruncate(state.shmFd, size) < 0) {
111 close(state.shmFd);
112 return false;
113 }
114
115 state.shmPool = makeShared<CCWlShmPool>(state.wlShm->sendCreatePool(state.shmFd, size));
116 if (!state.shmPool->resource()) {
117 close(state.shmFd);
118 state.shmFd = -1;
119 state.shmPool.reset();
120 return false;
121 }
122 state.shmBufSize = size;
123 } else if (size > state.shmBufSize) {
124 if (ftruncate(state.shmFd, size) < 0) {
125 close(state.shmFd);
126 state.shmFd = -1;
127 state.shmPool.reset();
128 return false;
129 }
130
131 state.shmPool->sendResize(size);
132 state.shmBufSize = size;
133 }
134
135 auto buf = makeShared<CCWlBuffer>(state.shmPool->sendCreateBuffer(0, geom.x, geom.y, stride, WL_SHM_FORMAT_XRGB8888));
136 if (!buf->resource())
137 return false;
138
139 if (state.shmBuf) {
140 state.shmBuf->sendDestroy();
141 state.shmBuf.reset();
142 }
143
144 state.shmBuf = buf;
145
146 return true;
147 }
148
149 static bool setupToplevel(SWlState& state) {
150 state.wlShm->setFormat([&](CCWlShm* p, uint32_t format) {
151 if (format == WL_SHM_FORMAT_XRGB8888)
152 state.xrgb8888_support = true;
153 });
154
155 state.xdgShell->setPing([&](CCXdgWmBase* p, uint32_t serial) { state.xdgShell->sendPong(serial); });
156
157 state.surf = makeShared<CCWlSurface>(state.wlCompositor->sendCreateSurface());
158 if (!state.surf->resource())
159 return false;
160
161 state.xdgSurf = makeShared<CCXdgSurface>(state.xdgShell->sendGetXdgSurface(state.surf->resource()));
162 if (!state.xdgSurf->resource())
163 return false;
164
165 state.xdgToplevel = makeShared<CCXdgToplevel>(state.xdgSurf->sendGetToplevel());
166 if (!state.xdgToplevel->resource())
167 return false;
168
169 state.xdgToplevel->setClose([&](CCXdgToplevel* p) { exit(0); });
170
171 state.xdgToplevel->setConfigure([&](CCXdgToplevel* p, int32_t w, int32_t h, wl_array* arr) {
172 state.geom = {1280, 720};
173
174 if (!createShm(state, state.geom))
175 exit(-1);
176 });
177
178 state.xdgSurf->setConfigure([&](CCXdgSurface* p, uint32_t serial) {
179 if (!state.shmBuf)
180 debugLog("xdgSurf configure but no buf made yet?");
181
182 state.xdgSurf->sendSetWindowGeometry(0, 0, state.geom.x, state.geom.y);
183 state.surf->sendAttach(state.shmBuf.get(), 0, 0);
184 state.surf->sendCommit();
185
186 state.xdgSurf->sendAckConfigure(serial);
187
188 if (!started) {
189 started = true;
190 clientLog("started");
191 }
192 });
193
194 state.xdgToplevel->sendSetTitle("keyboard-modifiers test client");
195 state.xdgToplevel->sendSetAppId("keyboard-modifiers");
196
197 state.surf->sendAttach(nullptr, 0, 0);
198 state.surf->sendCommit();
199
200 return true;
201 }
202
203 static bool setupSeat(SWlState& state) {
204 state.keyboard = makeShared<CCWlKeyboard>(state.wlSeat->sendGetKeyboard());
205 if (!state.keyboard->resource())
206 return false;
207
208 state.keyboard->setModifiers([&](CCWlKeyboard* p, uint32_t serial, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group) {
209 debugLog("modifiers: depressed={} latched={} locked={} group={}", depressed, latched, locked, group);
210 state.lastLocked = locked;
211 });
212
213 state.keyboard->setKey([&](CCWlKeyboard* p, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { debugLog("Got key event: key={} state={}", key, state); });
214
215 return true;
216 }
217
218 static void parseRequest(SWlState& state, std::string req) {
219 if (req.starts_with("get"))
220 clientLog("{}", state.lastLocked);
221 else if (req.starts_with("exit"))
222 shouldExit = true;
223 }
224
225 int main(int argc, char** argv) {
226 logfile.open("keyboard-modifiers.txt", std::ios::trunc);
227
228 if (argc != 1 && argc != 2)
229 clientLog("Only the \"--debug\" switch is allowed, it turns on debug logs.");
230
231 if (argc == 2 && std::string{argv[1]} == "--debug")
232 debug = true;
233
234 SWlState state;
235
236 state.display = wl_display_connect(nullptr);
237 if (!state.display) {
238 clientLog("Failed to connect to wayland display");
239 return -1;
240 }
241
242 if (!bindRegistry(state) || !setupSeat(state) || !setupToplevel(state))
243 return -1;
244
245 std::array<char, 1024> readBuf;
246 readBuf.fill(0);
247
248 wl_display_flush(state.display);
249
250 struct pollfd fds[2] = {{.fd = wl_display_get_fd(state.display), .events = POLLIN | POLLOUT}, {.fd = STDIN_FILENO, .events = POLLIN}};
251 while (!shouldExit && poll(fds, 2, 0) != -1) {
252 if (fds[0].revents & POLLIN) {
253 wl_display_flush(state.display);
254
255 if (wl_display_prepare_read(state.display) == 0) {
256 wl_display_read_events(state.display);
257 wl_display_dispatch_pending(state.display);
258 } else
259 wl_display_dispatch(state.display);
260
261 int ret = 0;
262 do {
263 ret = wl_display_dispatch_pending(state.display);
264 wl_display_flush(state.display);
265 } while (ret > 0);
266 }
267
268 if (fds[1].revents & POLLIN) {
269 ssize_t bytesRead = read(fds[1].fd, readBuf.data(), 1023);
270 if (bytesRead == -1)
271 continue;
272 readBuf[bytesRead] = 0;
273
274 parseRequest(state, std::string{readBuf.data()});
275 }
276 }
277
278 wl_display* display = state.display;
279 state = {};
280
281 wl_display_disconnect(display);
282 logfile.flush();
283 logfile.close();
284 return 0;
285 }