1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
#include "mock-server.h"
#include "linux/input.h"
typedef enum
{
SURFACE_ROLE_NONE = 0,
SURFACE_ROLE_XDG_TOPLEVEL,
SURFACE_ROLE_XDG_POPUP,
SURFACE_ROLE_LAYER,
} SurfaceRole;
typedef struct SurfaceData SurfaceData;
struct SurfaceData
{
SurfaceRole role;
struct wl_resource* surface;
struct wl_resource* pending_frame;
struct wl_resource* pending_buffer; // The attached but not committed buffer
char buffer_cleared; // If the buffer has been explicitly cleared since the last commit
struct wl_resource* xdg_toplevel;
struct wl_resource* xdg_popup;
struct wl_resource* xdg_surface;
struct wl_resource* layer_surface;
char has_committed_buffer; // This surface has a non-null committed buffer
char initial_commit_for_role; // Set to 1 when a role is created for a surface, and cleared after the first commit
char layer_send_configure; // If to send a layer surface configure on the next commit
int layer_set_w; // The width to configure the layer surface with
int layer_set_h; // The height to configure the layer surface with
uint32_t layer_anchor; // The layer surface's anchor
SurfaceData* most_recent_popup; // Start of the popup linked list
SurfaceData* previous_popup_sibling; // Forms a linked list of popups
SurfaceData* popup_parent;
};
static struct wl_resource* seat_global = NULL;
static struct wl_resource* pointer_global = NULL;
static struct wl_resource* output_global = NULL;
// Needs to be called before any role objects are assigned
static void surface_data_set_role(SurfaceData* data, SurfaceRole role)
{
if (data->role != SURFACE_ROLE_NONE) {
ASSERT_EQ(data->role, role, "%u");
}
char is_xdg_role = (role == SURFACE_ROLE_XDG_TOPLEVEL || role == SURFACE_ROLE_XDG_POPUP);
ASSERT_EQ(data->xdg_surface != NULL, is_xdg_role, "%d");
ASSERT(!data->xdg_toplevel);
ASSERT(!data->xdg_popup);
ASSERT(!data->layer_surface);
ASSERT(!data->has_committed_buffer);
data->role = role;
data->initial_commit_for_role = 1;
}
static void surface_data_unmap(SurfaceData* data) {
SurfaceData* popup = data->most_recent_popup;
while (popup) {
// Popups must be unmapped before their parents
ASSERT(!popup->surface);
ASSERT(!popup->layer_surface);
ASSERT(!popup->xdg_popup);
ASSERT(!popup->xdg_toplevel);
ASSERT(!popup->xdg_surface);
popup = popup->previous_popup_sibling;
}
}
static void surface_data_add_pupup(SurfaceData* parent, SurfaceData* popup) {
ASSERT(!popup->previous_popup_sibling);
popup->previous_popup_sibling = parent->most_recent_popup;
parent->most_recent_popup = popup;
popup->popup_parent = parent;
}
static void wl_surface_frame(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
NEW_ID_ARG(callback, 0);
SurfaceData* data = wl_resource_get_user_data(resource);
ASSERT(!data->pending_frame);
data->pending_frame = wl_resource_create(
wl_resource_get_client(resource),
&wl_callback_interface,
wl_resource_get_version(resource),
callback);
}
static void wl_surface_attach(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
RESOURCE_ARG(wl_buffer, buffer, 0);
SurfaceData* data = wl_resource_get_user_data(resource);
data->pending_buffer = buffer;
data->buffer_cleared = buffer == NULL;
}
static void wl_surface_commit(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
SurfaceData* data = wl_resource_get_user_data(resource);
if (data->buffer_cleared)
{
data->has_committed_buffer = 0;
data->buffer_cleared = 0;
}
else if (data->pending_buffer)
{
data->has_committed_buffer = 1;
}
if (data->pending_buffer)
{
wl_buffer_send_release(data->pending_buffer);
data->pending_buffer = NULL;
}
if (data->pending_frame)
{
wl_callback_send_done(data->pending_frame, 0);
wl_resource_destroy(data->pending_frame);
data->pending_frame = NULL;
}
if (data->initial_commit_for_role)
{
ASSERT(!data->has_committed_buffer);
data->initial_commit_for_role = 0;
}
if (data->layer_surface && data->layer_send_configure)
{
char horiz = (
(data->layer_anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) &&
(data->layer_anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT));
char vert = (
(data->layer_anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) &&
(data->layer_anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM));
int width = data->layer_set_w;
int height = data->layer_set_h;
if (width == 0 && !horiz)
FATAL("not horizontally stretched and no width given");
if (height == 0 && !vert)
FATAL("not horizontally stretched and no width given");
if (horiz)
width = DEFAULT_OUTPUT_WIDTH;
if (vert)
height = DEFAULT_OUTPUT_HEIGHT;
zwlr_layer_surface_v1_send_configure(data->layer_surface, wl_display_next_serial(display), width, height);
data->layer_send_configure = 0;
}
}
static void wl_surface_destroy(struct wl_resource* resource, const struct wl_message* message, union wl_argument* args)
{
SurfaceData* data = wl_resource_get_user_data(resource);
ASSERT(!data->xdg_popup);
ASSERT(!data->xdg_toplevel);
ASSERT(!data->xdg_surface);
ASSERT(!data->layer_surface);
data->surface = NULL;
// Don't free surfaces to guarantee traversing popups is always safe
// We're employing the missile memory management pattern here https://x.com/pomeranian99/status/858856994438094848
}
static void wl_compositor_create_surface(struct wl_resource* resource, const struct wl_message* message, union wl_argument* args)
{
NEW_ID_ARG(id, 0);
struct wl_resource* surface = wl_resource_create(
wl_resource_get_client(resource),
&wl_surface_interface,
wl_resource_get_version(resource),
id);
SurfaceData* data = ALLOC_STRUCT(SurfaceData);
data->surface = surface;
use_default_impl(surface);
wl_resource_set_user_data(surface, data);
}
void wl_seat_bind(struct wl_client* client, void* data, uint32_t version, uint32_t id)
{
ASSERT(!seat_global);
seat_global = wl_resource_create(client, &wl_seat_interface, version, id);
use_default_impl(seat_global);
wl_seat_send_capabilities(seat_global, WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_KEYBOARD);
};
void wl_output_bind(struct wl_client* client, void* data, uint32_t version, uint32_t id)
{
ASSERT(!output_global);
output_global = wl_resource_create(client, &wl_output_interface, version, id);
use_default_impl(output_global);
wl_output_send_done(output_global);
};
static void wl_seat_get_pointer(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
NEW_ID_ARG(id, 0);
ASSERT(!pointer_global);
pointer_global = wl_resource_create(
wl_resource_get_client(resource),
&wl_pointer_interface,
wl_resource_get_version(resource),
id);
use_default_impl(pointer_global);
}
static void xdg_wm_base_get_xdg_surface(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
NEW_ID_ARG(id, 0);
RESOURCE_ARG(wl_surface, surface, 1);
struct wl_resource* xdg_surface = wl_resource_create(
wl_resource_get_client(resource),
&xdg_surface_interface,
wl_resource_get_version(resource),
id);
use_default_impl(xdg_surface);
SurfaceData* data = wl_resource_get_user_data(surface);
wl_resource_set_user_data(xdg_surface, data);
data->xdg_surface = xdg_surface;
}
static void xdg_surface_destroy(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
SurfaceData* data = wl_resource_get_user_data(resource);
ASSERT(!data->xdg_toplevel);
ASSERT(!data->xdg_popup);
data->xdg_surface = NULL;
}
static void xdg_surface_get_toplevel(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
NEW_ID_ARG(id, 0);
struct wl_resource* toplevel = wl_resource_create(
wl_resource_get_client(resource),
&xdg_toplevel_interface,
wl_resource_get_version(resource),
id);
use_default_impl(toplevel);
struct wl_array states;
wl_array_init(&states);
xdg_toplevel_send_configure(toplevel, 0, 0, &states);
wl_array_release(&states);
xdg_surface_send_configure(resource, wl_display_next_serial(display));
SurfaceData* data = wl_resource_get_user_data(resource);
surface_data_set_role(data, SURFACE_ROLE_XDG_TOPLEVEL);
wl_resource_set_user_data(toplevel, data);
data->xdg_toplevel = toplevel;
}
static void xdg_toplevel_destroy(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
SurfaceData* data = wl_resource_get_user_data(resource);
ASSERT(data->xdg_surface);
data->xdg_toplevel = NULL;
surface_data_unmap(data);
}
static void xdg_surface_get_popup(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
NEW_ID_ARG(id, 0);
RESOURCE_ARG(xdg_surface, parent, 1);
struct wl_resource* popup = wl_resource_create(
wl_resource_get_client(resource),
&xdg_popup_interface,
wl_resource_get_version(resource),
id);
use_default_impl(popup);
xdg_popup_send_configure(popup, 0, 0, 100, 100);
xdg_surface_send_configure(resource, wl_display_next_serial(display));
SurfaceData* data = wl_resource_get_user_data(resource);
surface_data_set_role(data, SURFACE_ROLE_XDG_POPUP);
wl_resource_set_user_data(popup, data);
data->xdg_popup = popup;
if (parent) {
SurfaceData* parent_data = wl_resource_get_user_data(parent);
surface_data_add_pupup(parent_data, data);
}
}
static void xdg_popup_grab(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
SurfaceData* data = wl_resource_get_user_data(resource);
RESOURCE_ARG(wl_seat, seat, 0);
ASSERT_EQ(seat, seat_global, "%p");
ASSERT(data->popup_parent);
}
static void xdg_popup_destroy(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
SurfaceData* data = wl_resource_get_user_data(resource);
ASSERT(data->xdg_surface);
data->xdg_popup = NULL;
surface_data_unmap(data);
}
static void zwlr_layer_surface_v1_set_anchor(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
UINT_ARG(anchor, 0);
SurfaceData* data = wl_resource_get_user_data(resource);
data->layer_send_configure = 1;
data->layer_anchor = anchor;
}
static void zwlr_layer_surface_v1_set_size(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
UINT_ARG(width, 0);
UINT_ARG(height, 1);
SurfaceData* data = wl_resource_get_user_data(resource);
data->layer_send_configure = 1;
data->layer_set_w = width;
data->layer_set_h = height;
}
static void zwlr_layer_surface_v1_get_popup(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
RESOURCE_ARG(xdg_popup, popup, 0);
SurfaceData* data = wl_resource_get_user_data(resource);
SurfaceData* popup_data = wl_resource_get_user_data(popup);
ASSERT(!popup_data->popup_parent);
surface_data_add_pupup(data, popup_data);
}
static void zwlr_layer_shell_v1_get_layer_surface(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
NEW_ID_ARG(id, 0);
RESOURCE_ARG(wl_surface, surface, 1);
struct wl_resource* layer_surface = wl_resource_create(
wl_resource_get_client(resource),
&zwlr_layer_surface_v1_interface,
wl_resource_get_version(resource),
id);
use_default_impl(layer_surface);
SurfaceData* data = wl_resource_get_user_data(surface);
surface_data_set_role(data, SURFACE_ROLE_LAYER);
wl_resource_set_user_data(layer_surface, data);
data->layer_send_configure = 1;
data->layer_surface = layer_surface;
}
static void zwlr_layer_surface_v1_destroy(struct wl_resource *resource, const struct wl_message* message, union wl_argument* args)
{
SurfaceData* data = wl_resource_get_user_data(resource);
data->layer_surface = NULL;
surface_data_unmap(data);
}
void init()
{
OVERRIDE_REQUEST(wl_surface, commit);
OVERRIDE_REQUEST(wl_surface, frame);
OVERRIDE_REQUEST(wl_surface, attach);
OVERRIDE_REQUEST(wl_surface, destroy);
OVERRIDE_REQUEST(wl_compositor, create_surface);
OVERRIDE_REQUEST(wl_seat, get_pointer);
OVERRIDE_REQUEST(xdg_wm_base, get_xdg_surface);
OVERRIDE_REQUEST(xdg_surface, destroy);
OVERRIDE_REQUEST(xdg_surface, get_toplevel);
OVERRIDE_REQUEST(xdg_toplevel, destroy);
OVERRIDE_REQUEST(xdg_surface, get_popup);
OVERRIDE_REQUEST(xdg_popup, grab);
OVERRIDE_REQUEST(xdg_popup, destroy);
OVERRIDE_REQUEST(zwlr_layer_shell_v1, get_layer_surface);
OVERRIDE_REQUEST(zwlr_layer_surface_v1, set_anchor);
OVERRIDE_REQUEST(zwlr_layer_surface_v1, set_size);
OVERRIDE_REQUEST(zwlr_layer_surface_v1, get_popup);
OVERRIDE_REQUEST(zwlr_layer_surface_v1, destroy);
wl_global_create(display, &wl_seat_interface, 6, NULL, wl_seat_bind);
wl_global_create(display, &wl_output_interface, 2, NULL, wl_output_bind);
default_global_create(display, &wl_shm_interface, 1);
default_global_create(display, &wl_data_device_manager_interface, 2);
default_global_create(display, &wl_compositor_interface, 4);
default_global_create(display, &wl_subcompositor_interface, 1);
default_global_create(display, &xdg_wm_base_interface, 2);
default_global_create(display, &zwlr_layer_shell_v1_interface, 4);
default_global_create(display, &xdg_wm_dialog_v1_interface, 1);
}
|