Debian Codegraph @

hyprtester/clients/shortcut-inhibitor.cpp

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