| 1 |
#include <cerrno> |
| 2 |
#include <cstring> |
| 3 |
#include <algorithm> |
| 4 |
#include <fcntl.h> |
| 5 |
#include <format> |
| 6 |
#include <iostream> |
| 7 |
#include <string> |
| 8 |
#include <sys/mman.h> |
| 9 |
#include <sys/poll.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
#include <wayland-client.h> |
| 13 |
#include <wayland.hpp> |
| 14 |
#include <xdg-shell.hpp> |
| 15 |
#include <fractional-scale-v1.hpp> |
| 16 |
|
| 17 |
#include <hyprutils/memory/SharedPtr.hpp> |
| 18 |
|
| 19 |
using namespace Hyprutils::Memory; |
| 20 |
|
| 21 |
struct SSurfaceStats { |
| 22 |
int rootScaleCount = 0; |
| 23 |
int rootTransformCount = 0; |
| 24 |
int rootFractionCount = 0; |
| 25 |
int childScaleCount = 0; |
| 26 |
int childTransformCount = 0; |
| 27 |
int childFractionCount = 0; |
| 28 |
|
| 29 |
int rootScale = -1; |
| 30 |
int rootTransform = -1; |
| 31 |
int rootFraction = -1; |
| 32 |
int childScale = -1; |
| 33 |
int childTransform = -1; |
| 34 |
int childFraction = -1; |
| 35 |
}; |
| 36 |
|
| 37 |
struct SWlState { |
| 38 |
wl_display* display = nullptr; |
| 39 |
|
| 40 |
CSharedPointer<CCWlRegistry> registry; |
| 41 |
CSharedPointer<CCWlCompositor> compositor; |
| 42 |
CSharedPointer<CCWlSubcompositor> subcompositor; |
| 43 |
CSharedPointer<CCWlShm> shm; |
| 44 |
CSharedPointer<CCXdgWmBase> xdgShell; |
| 45 |
CSharedPointer<CCWpFractionalScaleManagerV1> fractional; |
| 46 |
|
| 47 |
CSharedPointer<CCWlSurface> surface; |
| 48 |
CSharedPointer<CCXdgSurface> xdgSurface; |
| 49 |
CSharedPointer<CCXdgToplevel> xdgToplevel; |
| 50 |
CSharedPointer<CCWpFractionalScaleV1> rootFractional; |
| 51 |
CSharedPointer<CCWlBuffer> rootBuffer; |
| 52 |
|
| 53 |
CSharedPointer<CCWlSurface> childSurface; |
| 54 |
CSharedPointer<CCWlSubsurface> childSubsurface; |
| 55 |
CSharedPointer<CCWpFractionalScaleV1> childFractional; |
| 56 |
CSharedPointer<CCWlBuffer> childBuffer; |
| 57 |
|
| 58 |
SSurfaceStats stats; |
| 59 |
bool xrgb8888 = false; |
| 60 |
bool configured = false; |
| 61 |
std::string setupError; |
| 62 |
}; |
| 63 |
|
| 64 |
static void sendLine(const std::string& line) { |
| 65 |
std::cout << line << std::endl; |
| 66 |
} |
| 67 |
|
| 68 |
static bool pollDisplay(SWlState& state, int timeoutMs) { |
| 69 |
while (wl_display_prepare_read(state.display) != 0) { |
| 70 |
if (wl_display_dispatch_pending(state.display) == -1) |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
if (wl_display_flush(state.display) == -1 && errno != EAGAIN) { |
| 75 |
wl_display_cancel_read(state.display); |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
pollfd fd = {.fd = wl_display_get_fd(state.display), .events = POLLIN, .revents = 0}; |
| 80 |
|
| 81 |
int ret = 0; |
| 82 |
do { |
| 83 |
ret = poll(&fd, 1, timeoutMs); |
| 84 |
} while (ret == -1 && errno == EINTR); |
| 85 |
|
| 86 |
if (ret == -1) { |
| 87 |
wl_display_cancel_read(state.display); |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
if (ret == 0) { |
| 92 |
wl_display_cancel_read(state.display); |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
if (fd.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
| 97 |
wl_display_cancel_read(state.display); |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
if (!(fd.revents & POLLIN)) { |
| 102 |
wl_display_cancel_read(state.display); |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
if (wl_display_read_events(state.display) == -1) |
| 107 |
return false; |
| 108 |
|
| 109 |
return wl_display_dispatch_pending(state.display) != -1; |
| 110 |
} |
| 111 |
|
| 112 |
template <typename F> |
| 113 |
static bool waitForDisplayState(SWlState& state, F&& predicate, int attempts, int timeoutMs) { |
| 114 |
for (int i = 0; i < attempts && !predicate(); ++i) { |
| 115 |
if (!pollDisplay(state, timeoutMs)) |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
return predicate(); |
| 120 |
} |
| 121 |
|
| 122 |
static std::string bindError(const SWlState& state) { |
| 123 |
std::string ret; |
| 124 |
|
| 125 |
if (!state.compositor) |
| 126 |
ret += " wl_compositor"; |
| 127 |
if (!state.subcompositor) |
| 128 |
ret += " wl_subcompositor"; |
| 129 |
if (!state.shm) |
| 130 |
ret += " wl_shm"; |
| 131 |
if (!state.xdgShell) |
| 132 |
ret += " xdg_wm_base"; |
| 133 |
if (!state.fractional) |
| 134 |
ret += " wp_fractional_scale_manager_v1"; |
| 135 |
if (state.shm && !state.xrgb8888) |
| 136 |
ret += " xrgb8888"; |
| 137 |
|
| 138 |
return ret.empty() ? "dispatch" : ret.substr(1); |
| 139 |
} |
| 140 |
|
| 141 |
static CSharedPointer<CCWlBuffer> createBuffer(SWlState& state, int width, int height) { |
| 142 |
if (!state.xrgb8888) |
| 143 |
return nullptr; |
| 144 |
|
| 145 |
const int stride = width * 4; |
| 146 |
const int size = stride * height; |
| 147 |
const int fd = memfd_create("hyprtester-surface-scale", MFD_CLOEXEC); |
| 148 |
if (fd < 0) |
| 149 |
return nullptr; |
| 150 |
|
| 151 |
if (ftruncate(fd, size) < 0) { |
| 152 |
close(fd); |
| 153 |
return nullptr; |
| 154 |
} |
| 155 |
|
| 156 |
void* data = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 157 |
if (data == MAP_FAILED) { |
| 158 |
close(fd); |
| 159 |
return nullptr; |
| 160 |
} |
| 161 |
|
| 162 |
std::memset(data, 0x33, size); |
| 163 |
munmap(data, size); |
| 164 |
|
| 165 |
auto pool = makeShared<CCWlShmPool>(state.shm->sendCreatePool(fd, size)); |
| 166 |
auto buffer = makeShared<CCWlBuffer>(pool->sendCreateBuffer(0, width, height, stride, WL_SHM_FORMAT_XRGB8888)); |
| 167 |
pool->sendDestroy(); |
| 168 |
close(fd); |
| 169 |
|
| 170 |
return buffer; |
| 171 |
} |
| 172 |
|
| 173 |
static void setSurfaceListeners(CSharedPointer<CCWlSurface> surface, SSurfaceStats& stats, bool child) { |
| 174 |
surface->setPreferredBufferScale([&stats, child](CCWlSurface*, int32_t scale) { |
| 175 |
if (child) { |
| 176 |
stats.childScale = scale; |
| 177 |
stats.childScaleCount++; |
| 178 |
} else { |
| 179 |
stats.rootScale = scale; |
| 180 |
stats.rootScaleCount++; |
| 181 |
} |
| 182 |
}); |
| 183 |
|
| 184 |
surface->setPreferredBufferTransform([&stats, child](CCWlSurface*, uint32_t transform) { |
| 185 |
if (child) { |
| 186 |
stats.childTransform = transform; |
| 187 |
stats.childTransformCount++; |
| 188 |
} else { |
| 189 |
stats.rootTransform = transform; |
| 190 |
stats.rootTransformCount++; |
| 191 |
} |
| 192 |
}); |
| 193 |
} |
| 194 |
|
| 195 |
static void setFractionalListener(CSharedPointer<CCWpFractionalScaleV1> fractional, SSurfaceStats& stats, bool child) { |
| 196 |
fractional->setPreferredScale([&stats, child](CCWpFractionalScaleV1*, uint32_t scale) { |
| 197 |
if (child) { |
| 198 |
stats.childFraction = scale; |
| 199 |
stats.childFractionCount++; |
| 200 |
} else { |
| 201 |
stats.rootFraction = scale; |
| 202 |
stats.rootFractionCount++; |
| 203 |
} |
| 204 |
}); |
| 205 |
} |
| 206 |
|
| 207 |
static bool bindRegistry(SWlState& state) { |
| 208 |
state.registry = makeShared<CCWlRegistry>((wl_proxy*)wl_display_get_registry(state.display)); |
| 209 |
if (!state.registry) |
| 210 |
return false; |
| 211 |
|
| 212 |
state.registry->setGlobal([&](CCWlRegistry*, uint32_t id, const char* name, uint32_t version) { |
| 213 |
const std::string NAME = name; |
| 214 |
if (NAME == "wl_compositor") |
| 215 |
state.compositor = |
| 216 |
makeShared<CCWlCompositor>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_compositor_interface, std::min<uint32_t>(version, 6))); |
| 217 |
else if (NAME == "wl_subcompositor") |
| 218 |
state.subcompositor = makeShared<CCWlSubcompositor>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_subcompositor_interface, 1)); |
| 219 |
else if (NAME == "wl_shm") { |
| 220 |
state.shm = makeShared<CCWlShm>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wl_shm_interface, 1)); |
| 221 |
state.shm->setFormat([&](CCWlShm*, uint32_t format) { |
| 222 |
if (format == WL_SHM_FORMAT_XRGB8888) |
| 223 |
state.xrgb8888 = true; |
| 224 |
}); |
| 225 |
} else if (NAME == "xdg_wm_base") |
| 226 |
state.xdgShell = makeShared<CCXdgWmBase>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &xdg_wm_base_interface, 1)); |
| 227 |
else if (NAME == "wp_fractional_scale_manager_v1") |
| 228 |
state.fractional = |
| 229 |
makeShared<CCWpFractionalScaleManagerV1>((wl_proxy*)wl_registry_bind((wl_registry*)state.registry->resource(), id, &wp_fractional_scale_manager_v1_interface, 1)); |
| 230 |
}); |
| 231 |
|
| 232 |
state.registry->setGlobalRemove([](CCWlRegistry*, uint32_t) {}); |
| 233 |
return waitForDisplayState( |
| 234 |
state, [&state]() { return state.compositor && state.subcompositor && state.shm && state.xdgShell && state.fractional && state.xrgb8888; }, 100, 100); |
| 235 |
} |
| 236 |
|
| 237 |
static bool setupToplevel(SWlState& state) { |
| 238 |
state.xdgShell->setPing([&](CCXdgWmBase*, uint32_t serial) { state.xdgShell->sendPong(serial); }); |
| 239 |
|
| 240 |
state.surface = makeShared<CCWlSurface>(state.compositor->sendCreateSurface()); |
| 241 |
if (!state.surface) { |
| 242 |
state.setupError = "surface"; |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
setSurfaceListeners(state.surface, state.stats, false); |
| 247 |
|
| 248 |
state.rootFractional = makeShared<CCWpFractionalScaleV1>(state.fractional->sendGetFractionalScale(state.surface->resource())); |
| 249 |
if (!state.rootFractional) { |
| 250 |
state.setupError = "fractional"; |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
setFractionalListener(state.rootFractional, state.stats, false); |
| 255 |
|
| 256 |
state.xdgSurface = makeShared<CCXdgSurface>(state.xdgShell->sendGetXdgSurface(state.surface->resource())); |
| 257 |
if (!state.xdgSurface) { |
| 258 |
state.setupError = "xdg-surface"; |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
state.xdgToplevel = makeShared<CCXdgToplevel>(state.xdgSurface->sendGetToplevel()); |
| 263 |
if (!state.xdgToplevel) { |
| 264 |
state.setupError = "xdg"; |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
state.xdgToplevel->setClose([](CCXdgToplevel*) { exit(0); }); |
| 269 |
state.xdgToplevel->setConfigure([](CCXdgToplevel*, int32_t, int32_t, wl_array*) {}); |
| 270 |
state.xdgSurface->setConfigure([&](CCXdgSurface*, uint32_t serial) { |
| 271 |
state.xdgSurface->sendAckConfigure(serial); |
| 272 |
state.configured = true; |
| 273 |
if (!state.rootBuffer) |
| 274 |
state.rootBuffer = createBuffer(state, 320, 240); |
| 275 |
state.surface->sendAttach(state.rootBuffer.get(), 0, 0); |
| 276 |
state.surface->sendCommit(); |
| 277 |
}); |
| 278 |
|
| 279 |
state.xdgToplevel->sendSetTitle("surface scale transform test"); |
| 280 |
state.xdgToplevel->sendSetAppId("surface-scale-transform"); |
| 281 |
state.surface->sendCommit(); |
| 282 |
|
| 283 |
if (!waitForDisplayState(state, [&state]() { return state.configured; }, 100, 100)) { |
| 284 |
state.setupError = "timeout"; |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
if (!waitForDisplayState(state, [&state]() { return state.stats.rootScale != -1 && state.stats.rootTransform != -1 && state.stats.rootFraction != -1; }, 100, 100)) { |
| 289 |
state.setupError = "surface-events"; |
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
if (!state.rootBuffer) |
| 294 |
state.setupError = "buffer"; |
| 295 |
|
| 296 |
return state.rootBuffer != nullptr; |
| 297 |
} |
| 298 |
|
| 299 |
static bool createChild(SWlState& state) { |
| 300 |
if (state.childSurface) |
| 301 |
return true; |
| 302 |
|
| 303 |
state.childSurface = makeShared<CCWlSurface>(state.compositor->sendCreateSurface()); |
| 304 |
setSurfaceListeners(state.childSurface, state.stats, true); |
| 305 |
|
| 306 |
state.childFractional = makeShared<CCWpFractionalScaleV1>(state.fractional->sendGetFractionalScale(state.childSurface->resource())); |
| 307 |
setFractionalListener(state.childFractional, state.stats, true); |
| 308 |
|
| 309 |
state.childSubsurface = makeShared<CCWlSubsurface>(state.subcompositor->sendGetSubsurface(state.childSurface.get(), state.surface.get())); |
| 310 |
state.childSubsurface->sendSetPosition(10, 10); |
| 311 |
|
| 312 |
state.childBuffer = createBuffer(state, 64, 64); |
| 313 |
if (!state.childBuffer) |
| 314 |
return false; |
| 315 |
|
| 316 |
state.childSurface->sendAttach(state.childBuffer.get(), 0, 0); |
| 317 |
state.childSurface->sendCommit(); |
| 318 |
state.surface->sendCommit(); |
| 319 |
|
| 320 |
return waitForDisplayState(state, [&state]() { return state.stats.childScale != -1 && state.stats.childTransform != -1 && state.stats.childFraction != -1; }, 100, 100); |
| 321 |
} |
| 322 |
|
| 323 |
static bool remapRoot(SWlState& state) { |
| 324 |
state.surface->sendAttach(state.rootBuffer.get(), 0, 0); |
| 325 |
state.surface->sendCommit(); |
| 326 |
|
| 327 |
return pollDisplay(state, 100); |
| 328 |
} |
| 329 |
|
| 330 |
static std::string report(const SSurfaceStats& stats) { |
| 331 |
return std::format("root_scale={} root_scale_count={} root_transform={} root_transform_count={} root_fraction={} root_fraction_count={} child_scale={} child_scale_count={} " |
| 332 |
"child_transform={} child_transform_count={} child_fraction={} child_fraction_count={}", |
| 333 |
stats.rootScale, stats.rootScaleCount, stats.rootTransform, stats.rootTransformCount, stats.rootFraction, stats.rootFractionCount, stats.childScale, |
| 334 |
stats.childScaleCount, stats.childTransform, stats.childTransformCount, stats.childFraction, stats.childFractionCount); |
| 335 |
} |
| 336 |
|
| 337 |
static void handleCommand(SWlState& state, const std::string& command) { |
| 338 |
pollDisplay(state, 100); |
| 339 |
|
| 340 |
if (command == "report") |
| 341 |
sendLine(report(state.stats)); |
| 342 |
else if (command == "unmap") { |
| 343 |
state.surface->sendAttach(nullptr, 0, 0); |
| 344 |
state.surface->sendCommit(); |
| 345 |
state.configured = false; |
| 346 |
pollDisplay(state, 100); |
| 347 |
sendLine("ok"); |
| 348 |
} else if (command == "remap") { |
| 349 |
sendLine(remapRoot(state) ? "ok" : "error"); |
| 350 |
} else if (command == "subsurface") |
| 351 |
sendLine(createChild(state) ? "ok" : "error"); |
| 352 |
else if (command == "exit") |
| 353 |
exit(0); |
| 354 |
else |
| 355 |
sendLine("unknown"); |
| 356 |
} |
| 357 |
|
| 358 |
int main() { |
| 359 |
SWlState state; |
| 360 |
state.display = wl_display_connect(nullptr); |
| 361 |
if (!state.display) { |
| 362 |
sendLine("error connect"); |
| 363 |
return 1; |
| 364 |
} |
| 365 |
|
| 366 |
if (!bindRegistry(state)) { |
| 367 |
sendLine(std::format("error bind {}", bindError(state))); |
| 368 |
return 1; |
| 369 |
} |
| 370 |
|
| 371 |
if (!setupToplevel(state)) { |
| 372 |
sendLine(std::format("error setup {}", state.setupError)); |
| 373 |
return 1; |
| 374 |
} |
| 375 |
|
| 376 |
sendLine("started"); |
| 377 |
|
| 378 |
std::string command; |
| 379 |
while (std::getline(std::cin, command)) |
| 380 |
handleCommand(state, command); |
| 381 |
|
| 382 |
return 0; |
| 383 |
} |