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 #include <utility>
11
12 #include <wayland-client.h>
13 #include <wayland.hpp>
14 #include <xdg-shell.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 CSharedPointer<CCWlCompositor> wlCompositor;
28 CSharedPointer<CCWlSeat> wlSeat;
29 CSharedPointer<CCWlShm> wlShm;
30 CSharedPointer<CCXdgWmBase> xdgShell;
31
32 CSharedPointer<CCWlShmPool> shmPool;
33 CSharedPointer<CCWlBuffer> shmBuf;
34 int shmFd;
35 size_t shmBufSize;
36 bool xrgb8888_support = false;
37
38 CSharedPointer<CCWlSurface> surf;
39 CSharedPointer<CCXdgSurface> xdgSurf;
40 CSharedPointer<CCXdgToplevel> xdgToplevel;
41 Vector2D geom;
42
43 CSharedPointer<CCWlKeyboard> keyboard;
44 uint32_t lastLocked = 0;
45 };
46
47 static std::ofstream logfile;
48 static bool debug, started, shouldExit;
49
50 template <typename... Args>
51 static void clientLog(std::format_string<Args...> fmt, Args&&... args) {
52 std::string text = std::format(fmt, std::forward<Args>(args)...);
53 std::println("{}", text);
54 logfile << text << std::endl;
55 std::fflush(stdout);
56 }
57
58 template <typename... Args>
59 static void debugLog(std::format_string<Args...> fmt, Args&&... args) {
60 std::string text = std::format(fmt, std::forward<Args>(args)...);
61 logfile << text << std::endl;
62 if (!debug)
63 return;
64 std::println("{}", text);
65 std::fflush(stdout);
66 }
67
68 static bool bindRegistry(SWlState& state) {
69 state.registry = makeShared<CCWlRegistry>((wl_proxy*)wl_display_get_registry(state.display));
70
71 state.registry->setGlobal([&](CCWlRegistry* r, uint32_t id, const char* name, uint32_t version) {
72 const std::string NAME = name;
73 if (NAME == "wl_compositor") {
74 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
75 state.wlCompositor = makeShared<CCWlCompositor>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_compositor_interface, 6));
76 } else if (NAME == "wl_shm") {
77 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
78 state.wlShm = makeShared<CCWlShm>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_shm_interface, 1));
79 } else if (NAME == "wl_seat") {
80 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
81 state.wlSeat = makeShared<CCWlSeat>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_seat_interface, 9));
82 } else if (NAME == "xdg_wm_base") {
83 debugLog(" > binding to global: {} (version {}) with id {}", name, version, id);
84 state.xdgShell = makeShared<CCXdgWmBase>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &xdg_wm_base_interface, 1));
85 }
86 });
87 state.registry->setGlobalRemove([](CCWlRegistry* r, uint32_t id) { debugLog("Global {} removed", id); });
88
89 wl_display_roundtrip(state.display);
90
91 if (!state.wlCompositor || !state.wlShm || !state.wlSeat || !state.xdgShell) {
92 clientLog("Failed to get protocols from Hyprland");
93 return false;
94 }
95
96 return true;
97 }
98
99 static bool createShm(SWlState& state, Vector2D geom) {
100 if (!state.xrgb8888_support)
101 return false;
102
103 size_t stride = geom.x * 4;
104 size_t size = geom.y * stride;
105 if (!state.shmPool) {
106 const char* name = "/wl-shm-kb-mods";
107 state.shmFd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
108 if (state.shmFd < 0)
109 return false;
110
111 if (shm_unlink(name) < 0 || ftruncate(state.shmFd, size) < 0) {
112 close(state.shmFd);
113 return false;
114 }
115
116 state.shmPool = makeShared<CCWlShmPool>(state.wlShm->sendCreatePool(state.shmFd, size));
117 if (!state.shmPool->resource()) {
118 close(state.shmFd);
119 state.shmFd = -1;
120 state.shmPool.reset();
121 return false;
122 }
123 state.shmBufSize = size;
124 } else if (size > state.shmBufSize) {
125 if (ftruncate(state.shmFd, size) < 0) {
126 close(state.shmFd);
127 state.shmFd = -1;
128 state.shmPool.reset();
129 return false;
130 }
131
132 state.shmPool->sendResize(size);
133 state.shmBufSize = size;
134 }
135
136 auto buf = makeShared<CCWlBuffer>(state.shmPool->sendCreateBuffer(0, geom.x, geom.y, stride, WL_SHM_FORMAT_XRGB8888));
137 if (!buf->resource())
138 return false;
139
140 if (state.shmBuf) {
141 state.shmBuf->sendDestroy();
142 state.shmBuf.reset();
143 }
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("keyboard-modifiers test client");
196 state.xdgToplevel->sendSetAppId("keyboard-modifiers");
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.keyboard = makeShared<CCWlKeyboard>(state.wlSeat->sendGetKeyboard());
206 if (!state.keyboard->resource())
207 return false;
208
209 state.keyboard->setModifiers([&](CCWlKeyboard* p, uint32_t serial, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group) {
210 debugLog("modifiers: depressed={} latched={} locked={} group={}", depressed, latched, locked, group);
211 state.lastLocked = locked;
212 });
213
214 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); });
215
216 return true;
217 }
218
219 static void parseRequest(SWlState& state, std::string req) {
220 if (req.starts_with("get"))
221 clientLog("{}", state.lastLocked);
222 else if (req.starts_with("exit"))
223 shouldExit = true;
224 }
225
226 int main(int argc, char** argv) {
227 logfile.open("keyboard-modifiers.txt", std::ios::trunc);
228
229 if (argc != 1 && argc != 2)
230 clientLog("Only the \"--debug\" switch is allowed, it turns on debug logs.");
231
232 if (argc == 2 && std::string{argv[1]} == "--debug")
233 debug = true;
234
235 SWlState state;
236
237 state.display = wl_display_connect(nullptr);
238 if (!state.display) {
239 clientLog("Failed to connect to wayland display");
240 return -1;
241 }
242
243 if (!bindRegistry(state) || !setupSeat(state) || !setupToplevel(state))
244 return -1;
245
246 std::array<char, 1024> readBuf;
247 readBuf.fill(0);
248
249 wl_display_flush(state.display);
250
251 struct pollfd fds[2] = {{.fd = wl_display_get_fd(state.display), .events = POLLIN | POLLOUT}, {.fd = STDIN_FILENO, .events = POLLIN}};
252 while (!shouldExit && poll(fds, 2, 0) != -1) {
253 if (fds[0].revents & POLLIN) {
254 wl_display_flush(state.display);
255
256 if (wl_display_prepare_read(state.display) == 0) {
257 wl_display_read_events(state.display);
258 wl_display_dispatch_pending(state.display);
259 } else
260 wl_display_dispatch(state.display);
261
262 int ret = 0;
263 do {
264 ret = wl_display_dispatch_pending(state.display);
265 wl_display_flush(state.display);
266 } while (ret > 0);
267 }
268
269 if (fds[1].revents & POLLIN) {
270 ssize_t bytesRead = read(fds[1].fd, readBuf.data(), 1023);
271 if (bytesRead == -1)
272 continue;
273 readBuf[bytesRead] = 0;
274
275 parseRequest(state, std::string{readBuf.data()});
276 }
277 }
278
279 wl_display* display = state.display;
280 state = {};
281
282 wl_display_disconnect(display);
283 logfile.flush();
284 logfile.close();
285 return 0;
286 }