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