tests/wayland-client-utils: Add simple toplevel helper

Add a helper to create a toplevel painted with a given color, size and
title. It's meant to be "dumb" and have a default size, but respect any
configured size.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2770>
This commit is contained in:
Jonas Ådahl 2022-11-15 10:48:57 +01:00 committed by Marge Bot
parent d97c160122
commit 474c093801
2 changed files with 112 additions and 0 deletions

View File

@ -381,6 +381,94 @@ draw_surface (WaylandDisplay *display,
wl_surface_attach (surface, buffer, 0, 0); wl_surface_attach (surface, buffer, 0, 0);
} }
static void
handle_xdg_toplevel_configure (void *data,
struct xdg_toplevel *xdg_toplevel,
int32_t width,
int32_t height,
struct wl_array *state)
{
WaylandSurface *surface = data;
if (width == 0)
surface->width = surface->default_width;
else
surface->width = width;
if (height == 0)
surface->height = surface->default_height;
else
surface->height = height;
}
static void
handle_xdg_toplevel_close (void *data,
struct xdg_toplevel *xdg_toplevel)
{
g_assert_not_reached ();
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
handle_xdg_toplevel_configure,
handle_xdg_toplevel_close,
};
static void
handle_xdg_surface_configure (void *data,
struct xdg_surface *xdg_surface,
uint32_t serial)
{
WaylandSurface *surface = data;
draw_surface (surface->display,
surface->wl_surface,
surface->width, surface->height,
surface->color);
xdg_surface_ack_configure (xdg_surface, serial);
wl_surface_commit (surface->wl_surface);
}
static const struct xdg_surface_listener xdg_surface_listener = {
handle_xdg_surface_configure,
};
WaylandSurface *
wayland_surface_new (WaylandDisplay *display,
const char *title,
int default_width,
int default_height,
uint32_t color)
{
WaylandSurface *surface;
surface = g_new0 (WaylandSurface, 1);
surface->display = display;
surface->default_width = default_width;
surface->default_height = default_height;
surface->color = color;
surface->wl_surface = wl_compositor_create_surface (display->compositor);
surface->xdg_surface = xdg_wm_base_get_xdg_surface (display->xdg_wm_base,
surface->wl_surface);
xdg_surface_add_listener (surface->xdg_surface, &xdg_surface_listener,
surface);
surface->xdg_toplevel = xdg_surface_get_toplevel (surface->xdg_surface);
xdg_toplevel_add_listener (surface->xdg_toplevel, &xdg_toplevel_listener,
surface);
xdg_toplevel_set_title (surface->xdg_toplevel, title);
return surface;
}
void
wayland_surface_free (WaylandSurface *surface)
{
g_clear_pointer (&surface->xdg_toplevel, xdg_toplevel_destroy);
g_clear_pointer (&surface->xdg_surface, xdg_surface_destroy);
g_clear_pointer (&surface->wl_surface, wl_surface_destroy);
g_free (surface);
}
const char * const char *
lookup_property_value (WaylandDisplay *display, lookup_property_value (WaylandDisplay *display,
const char *name) const char *name)

View File

@ -36,6 +36,22 @@ typedef struct _WaylandDisplay
GHashTable *properties; GHashTable *properties;
} WaylandDisplay; } WaylandDisplay;
typedef struct _WaylandSurface
{
WaylandDisplay *display;
struct wl_surface *wl_surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
int default_width;
int default_height;
int width;
int height;
uint32_t color;
} WaylandSurface;
G_DECLARE_FINAL_TYPE (WaylandDisplay, wayland_display, G_DECLARE_FINAL_TYPE (WaylandDisplay, wayland_display,
WAYLAND, DISPLAY, WAYLAND, DISPLAY,
GObject) GObject)
@ -44,6 +60,14 @@ int create_anonymous_file (off_t size);
WaylandDisplay * wayland_display_new (WaylandDisplayCapabilities capabilities); WaylandDisplay * wayland_display_new (WaylandDisplayCapabilities capabilities);
WaylandSurface * wayland_surface_new (WaylandDisplay *display,
const char *title,
int default_width,
int default_height,
uint32_t color);
void wayland_surface_free (WaylandSurface *surface);
gboolean create_shm_buffer (WaylandDisplay *display, gboolean create_shm_buffer (WaylandDisplay *display,
int width, int width,
int height, int height,