| 1 |
#include <print> |
| 2 |
#include <poll.h> |
| 3 |
#include <sys/mman.h> |
| 4 |
#include <fcntl.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include <wayland-client.h> |
| 9 |
#include <wayland.hpp> |
| 10 |
#include <xdg-shell.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 |
|
| 28 |
// shm/buffer stuff |
| 29 |
CSharedPointer<CCWlShmPool> shmPool; |
| 30 |
CSharedPointer<CCWlBuffer> shmBuf; |
| 31 |
CSharedPointer<CCWlBuffer> shmBuf2; |
| 32 |
int shmFd = 0; |
| 33 |
size_t shmBufSize = 0; |
| 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 = 0; |
| 45 |
}; |
| 46 |
|
| 47 |
bool debug, shouldExit, started; |
| 48 |
|
| 49 |
template <typename... Args> |
| 50 |
//NOLINTNEXTLINE |
| 51 |
static void clientLog(std::format_string<Args...> fmt, Args&&... args) { |
| 52 |
std::string text = std::vformat(fmt.get(), std::make_format_args(args...)); |
| 53 |
std::println("{}", text); |
| 54 |
std::fflush(stdout); |
| 55 |
} |
| 56 |
|
| 57 |
template <typename... Args> |
| 58 |
//NOLINTNEXTLINE |
| 59 |
static void debugLog(std::format_string<Args...> fmt, Args&&... args) { |
| 60 |
std::string text = std::vformat(fmt.get(), std::make_format_args(args...)); |
| 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-pointer-warp"; |
| 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 * 2) < 0) { |
| 111 |
close(state.shmFd); |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
state.shmPool = makeShared<CCWlShmPool>(state.wlShm->sendCreatePool(state.shmFd, size * 2)); |
| 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 * 2); |
| 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 |
state.shmBuf = buf; |
| 144 |
|
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
static bool setupToplevel(SWlState& state) { |
| 149 |
state.wlShm->setFormat([&](CCWlShm* p, uint32_t format) { |
| 150 |
if (format == WL_SHM_FORMAT_XRGB8888) |
| 151 |
state.xrgb8888_support = true; |
| 152 |
}); |
| 153 |
|
| 154 |
state.xdgShell->setPing([&](CCXdgWmBase* p, uint32_t serial) { state.xdgShell->sendPong(serial); }); |
| 155 |
|
| 156 |
state.surf = makeShared<CCWlSurface>(state.wlCompositor->sendCreateSurface()); |
| 157 |
if (!state.surf->resource()) |
| 158 |
return false; |
| 159 |
|
| 160 |
state.xdgSurf = makeShared<CCXdgSurface>(state.xdgShell->sendGetXdgSurface(state.surf->resource())); |
| 161 |
if (!state.xdgSurf->resource()) |
| 162 |
return false; |
| 163 |
|
| 164 |
state.xdgToplevel = makeShared<CCXdgToplevel>(state.xdgSurf->sendGetToplevel()); |
| 165 |
if (!state.xdgToplevel->resource()) |
| 166 |
return false; |
| 167 |
|
| 168 |
state.xdgToplevel->setClose([&](CCXdgToplevel* p) { exit(0); }); |
| 169 |
|
| 170 |
state.xdgToplevel->setConfigure([&](CCXdgToplevel* p, int32_t w, int32_t h, wl_array* arr) { |
| 171 |
state.geom = {1280, 720}; |
| 172 |
|
| 173 |
if (!createShm(state, state.geom)) |
| 174 |
exit(-1); |
| 175 |
}); |
| 176 |
|
| 177 |
state.xdgSurf->setConfigure([&](CCXdgSurface* p, uint32_t serial) { |
| 178 |
if (!state.shmBuf) |
| 179 |
debugLog("xdgSurf configure but no buf made yet?"); |
| 180 |
|
| 181 |
state.xdgSurf->sendSetWindowGeometry(0, 0, state.geom.x, state.geom.y); |
| 182 |
state.surf->sendAttach(state.shmBuf.get(), 0, 0); |
| 183 |
state.surf->sendCommit(); |
| 184 |
|
| 185 |
state.xdgSurf->sendAckConfigure(serial); |
| 186 |
|
| 187 |
if (!started) { |
| 188 |
started = true; |
| 189 |
clientLog("started"); |
| 190 |
} |
| 191 |
}); |
| 192 |
|
| 193 |
state.xdgToplevel->sendSetTitle("child-test parent"); |
| 194 |
state.xdgToplevel->sendSetAppId("child-test-parent"); |
| 195 |
|
| 196 |
state.surf->sendAttach(nullptr, 0, 0); |
| 197 |
state.surf->sendCommit(); |
| 198 |
|
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
static bool setupSeat(SWlState& state) { |
| 203 |
state.pointer = makeShared<CCWlPointer>(state.wlSeat->sendGetPointer()); |
| 204 |
if (!state.pointer->resource()) |
| 205 |
return false; |
| 206 |
|
| 207 |
state.pointer->setEnter([&](CCWlPointer* p, uint32_t serial, wl_proxy* surf, wl_fixed_t x, wl_fixed_t y) { |
| 208 |
debugLog("Got pointer enter event, serial {}, x {}, y {}", serial, x, y); |
| 209 |
state.enterSerial = serial; |
| 210 |
}); |
| 211 |
|
| 212 |
state.pointer->setLeave([&](CCWlPointer* p, uint32_t serial, wl_proxy* surf) { debugLog("Got pointer leave event, serial {}", serial); }); |
| 213 |
|
| 214 |
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); }); |
| 215 |
|
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
struct SChildWindow { |
| 220 |
CSharedPointer<CCWlSurface> surface; |
| 221 |
CSharedPointer<CCXdgSurface> xSurface; |
| 222 |
CSharedPointer<CCXdgToplevel> toplevel; |
| 223 |
}; |
| 224 |
|
| 225 |
static void parseRequest(SWlState& state, std::string str, SChildWindow& window) { |
| 226 |
if (str.starts_with("exit")) { |
| 227 |
shouldExit = true; |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
size_t index = str.find_first_of('\n'); |
| 232 |
str = str.substr(0, index); |
| 233 |
|
| 234 |
if (str == "toplevel") { |
| 235 |
window.surface = makeShared<CCWlSurface>(state.wlCompositor->sendCreateSurface()); |
| 236 |
window.xSurface = makeShared<CCXdgSurface>(state.xdgShell->sendGetXdgSurface(window.surface->resource())); |
| 237 |
|
| 238 |
window.xSurface->setConfigure([&](CCXdgSurface* p, uint32_t serial) { |
| 239 |
if (!state.shmBuf) |
| 240 |
debugLog("xdgSurf configure but no buf made yet?"); |
| 241 |
|
| 242 |
window.xSurface->sendSetWindowGeometry(0, 0, state.geom.x, state.geom.y); |
| 243 |
window.surface->sendAttach(state.shmBuf2.get(), 0, 0); |
| 244 |
window.surface->sendCommit(); |
| 245 |
|
| 246 |
window.xSurface->sendAckConfigure(serial); |
| 247 |
}); |
| 248 |
|
| 249 |
window.toplevel = makeShared<CCXdgToplevel>(window.xSurface->sendGetToplevel()); |
| 250 |
|
| 251 |
window.toplevel->setConfigure([&](CCXdgToplevel* p, int32_t w, int32_t h, wl_array* arr) { |
| 252 |
size_t stride = 1280 * 4; |
| 253 |
size_t size = 720 * stride; |
| 254 |
|
| 255 |
auto buf = makeShared<CCWlBuffer>(state.shmPool->sendCreateBuffer(size, state.geom.x, state.geom.y, stride, WL_SHM_FORMAT_XRGB8888)); |
| 256 |
if (!buf->resource()) |
| 257 |
clientLog("Failed to create child buffer"); |
| 258 |
|
| 259 |
if (state.shmBuf2) { |
| 260 |
state.shmBuf2->sendDestroy(); |
| 261 |
state.shmBuf2.reset(); |
| 262 |
} |
| 263 |
state.shmBuf2 = buf; |
| 264 |
}); |
| 265 |
|
| 266 |
window.toplevel->sendSetTitle("child-test child"); |
| 267 |
window.toplevel->sendSetAppId("child-test-child"); |
| 268 |
window.toplevel->sendSetParent(state.xdgToplevel.get()); |
| 269 |
|
| 270 |
window.surface->sendAttach(nullptr, 0, 0); |
| 271 |
window.surface->sendCommit(); |
| 272 |
clientLog("child started"); |
| 273 |
return; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
int main(int argc, char** argv) { |
| 278 |
if (argc != 1 && argc != 2) |
| 279 |
clientLog("Only the \"--debug\" switch is allowed, it turns on debug logs."); |
| 280 |
|
| 281 |
if (argc == 2 && std::string{argv[1]} == "--debug") |
| 282 |
debug = true; |
| 283 |
|
| 284 |
SWlState state; |
| 285 |
SChildWindow window; |
| 286 |
|
| 287 |
// WAYLAND_DISPLAY env should be set to the correct one |
| 288 |
state.display = wl_display_connect(nullptr); |
| 289 |
if (!state.display) { |
| 290 |
clientLog("Failed to connect to wayland display"); |
| 291 |
return -1; |
| 292 |
} |
| 293 |
|
| 294 |
if (!bindRegistry(state) || !setupSeat(state) || !setupToplevel(state)) |
| 295 |
return -1; |
| 296 |
|
| 297 |
std::array<char, 1024> readBuf; |
| 298 |
readBuf.fill(0); |
| 299 |
|
| 300 |
wl_display_flush(state.display); |
| 301 |
|
| 302 |
struct pollfd fds[2] = {{.fd = wl_display_get_fd(state.display), .events = POLLIN | POLLOUT}, {.fd = STDIN_FILENO, .events = POLLIN}}; |
| 303 |
while (!shouldExit && poll(fds, 2, 0) != -1) { |
| 304 |
if (fds[0].revents & POLLIN) { |
| 305 |
wl_display_flush(state.display); |
| 306 |
|
| 307 |
if (wl_display_prepare_read(state.display) == 0) { |
| 308 |
wl_display_read_events(state.display); |
| 309 |
wl_display_dispatch_pending(state.display); |
| 310 |
} else |
| 311 |
wl_display_dispatch(state.display); |
| 312 |
|
| 313 |
int ret = 0; |
| 314 |
do { |
| 315 |
ret = wl_display_dispatch_pending(state.display); |
| 316 |
wl_display_flush(state.display); |
| 317 |
} while (ret > 0); |
| 318 |
} |
| 319 |
|
| 320 |
if (fds[1].revents & POLLIN) { |
| 321 |
ssize_t bytesRead = read(fds[1].fd, readBuf.data(), 1023); |
| 322 |
if (bytesRead == -1) |
| 323 |
continue; |
| 324 |
readBuf[bytesRead] = 0; |
| 325 |
|
| 326 |
parseRequest(state, std::string{readBuf.data()}, window); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
wl_display* display = state.display; |
| 331 |
state = {}; |
| 332 |
window = {}; |
| 333 |
|
| 334 |
wl_display_disconnect(display); |
| 335 |
return 0; |
| 336 |
} |