windowmaker-wl/src/backend/wayland/wl_client.c

634 lines
24 KiB
C

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <wayland-server-core.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/xwayland.h>
#include <wlr/interfaces/wlr_buffer.h>
#include "../../screen.h"
#include "wl_types.h"
#include "../../window.h"
#include <WINGs/WINGsP.h>
/* Forward declarations from sibling modules. */
extern const struct wlr_buffer_impl frame_buf_impl;
void fb_attach_scene_buf_listener(struct wl_frame_buf *fb);
struct wl_frame_buf *frame_buf_find(WNativeWindow id);
struct wl_toplevel_view *wl_find_view_by_id(WNativeWindow id);
/* ------------------------------------------------------------------ */
/* Client */
int
wl_window_get_client_attrs(WNativeWindow win, WClientAttributes *out)
{
/* return real map_state and geometry from XDG surface.
* XWayland path uses xw_surface->mapped and width/height. */
struct wl_toplevel_view *v = wl_find_view_by_id(win);
memset(out, 0, sizeof(*out));
if (v && v->xw_surface) {
if (v->xw_surface->surface) {
out->map_state = WCLIENT_VIEWABLE;
out->width = v->xw_surface->width >= 64 ? v->xw_surface->width : 640;
out->height = v->xw_surface->height >= 64 ? v->xw_surface->height : 480;
} else {
out->map_state = WCLIENT_UNMAPPED;
}
} else if (v && v->xdg_surface && v->xdg_surface->surface) {
out->map_state = WCLIENT_VIEWABLE;
struct wlr_box geo = v->xdg_surface->geometry;
if (geo.width > 0 && geo.height > 0) {
out->width = geo.width;
out->height = geo.height;
} else {
out->width = v->xdg_surface->surface->current.width;
out->height = v->xdg_surface->surface->current.height;
}
} else {
out->map_state = WCLIENT_UNMAPPED;
}
return 1;
}
void
wl_client_get_visual_attrs(WNativeWindow win, WClientVisual *out)
{
(void)win;
out->depth = 24; out->visual = NULL; out->colormap = 0;
}
void
wl_client_adopt_attributes(WNativeWindow win, unsigned long event_mask)
{ (void)win; (void)event_mask; }
void
wl_client_reparent(WNativeWindow client, WNativeWindow new_parent, int x, int y)
{
/* Toplevel view reparented into a frame_buf — record the association
* so wl_window_move can keep them in sync. */
struct wl_toplevel_view *v = wl_find_view_by_id(client);
if (v) {
v->frame_id = new_parent;
v->frame_rel_x = x;
v->frame_rel_y = y;
/* Position the client surface relative to the frame */
struct wl_frame_buf *parent_fb = frame_buf_find(new_parent);
if (parent_fb) {
v->x = parent_fb->x + x;
v->y = parent_fb->y + y;
if (v->scene_node) {
/* Move client's scene tree under the frame's tree */
if (parent_fb->tree)
wlr_scene_node_reparent(&v->scene_node->node, parent_fb->tree);
/* Position at relative offset within the tree */
wlr_scene_node_set_position(&v->scene_node->node, x, y);
}
}
return;
}
/* Reparent to root = dialog teardown: destroy the child frame_buf */
if (client >= 0x20000000 && new_parent == (WNativeWindow)wScreenWithNumber(0)->root_win) {
struct wl_frame_buf *fb = frame_buf_find(client);
if (fb) {
extern void W_UnregisterBacking(unsigned long id);
W_UnregisterBacking(fb->id);
if (fb->scene_buf)
wlr_scene_node_destroy(&fb->scene_buf->node);
wl_list_remove(&fb->link);
wlr_buffer_drop(&fb->base);
}
return;
}
/* For WINGs internal windows: create a child frame_buf so the dialog
* content is visible in the scene graph. */
if (client >= 0x20000000 && new_parent >= 0x10000000) {
/* client is a WINGs view ID, new_parent is a frame_buf */
struct wl_frame_buf *parent_fb = frame_buf_find(new_parent);
if (!parent_fb) return;
/* Check if we already have a frame_buf for this client */
struct wl_frame_buf *existing = frame_buf_find(client);
if (existing) {
/* Update parent, position, and ensure backing is current */
existing->parent_id = new_parent;
existing->rel_x = x;
existing->rel_y = y;
existing->x = parent_fb->x + x;
existing->y = parent_fb->y + y;
/* Resize backing image if the view size changed */
extern W_View *W_GetViewForWindow(unsigned long id);
W_View *view = W_GetViewForWindow((unsigned long)client);
int w = view ? view->size.width : existing->width;
int h = view ? view->size.height : existing->height;
if (w <= 0) w = 1;
if (h <= 0) h = 1;
if (w != existing->width || h != existing->height) {
pixman_image_unref(existing->image);
existing->image = pixman_image_create_bits(PIXMAN_a8r8g8b8, w, h, NULL, 0);
existing->width = w;
existing->height = h;
existing->base.width = w;
existing->base.height = h;
if (existing->scene_buf)
wlr_scene_buffer_set_dest_size(existing->scene_buf, w, h);
}
/* Re-link the view's backing to this frame_buf's image */
extern void W_SetViewBacking(unsigned long id, void *img);
W_SetViewBacking((unsigned long)client, existing->image);
/* Recreate scene_buf if it was destroyed (cascade from parent tree) */
if (!existing->scene_buf && wl_state.scene) {
struct wlr_scene_tree *_wp = parent_fb->tree
? parent_fb->tree
: (wl_state.level_trees[WL_LAYER_FLOATING]
? wl_state.level_trees[WL_LAYER_FLOATING]
: &wl_state.scene->tree);
existing->scene_buf = wlr_scene_buffer_create(_wp, &existing->base);
if (existing->scene_buf)
fb_attach_scene_buf_listener(existing);
}
if (existing->scene_buf) {
if (parent_fb->tree &&
existing->scene_buf->node.parent != parent_fb->tree) {
wlr_scene_node_reparent(&existing->scene_buf->node,
parent_fb->tree);
}
if (parent_fb->tree)
wlr_scene_node_set_position(&existing->scene_buf->node, x, y);
else
wlr_scene_node_set_position(&existing->scene_buf->node,
existing->x, existing->y);
wlr_scene_node_set_enabled(&existing->scene_buf->node, false);
}
return;
}
/* Create a new frame_buf for this WINGs view.
* Get size from the view itself. */
extern W_View *W_GetViewForWindow(unsigned long id);
W_View *view = W_GetViewForWindow((unsigned long)client);
int w = view ? view->size.width : parent_fb->width;
int h = view ? view->size.height : (parent_fb->height - y);
if (w <= 0) w = 1;
if (h <= 0) h = 1;
struct wl_frame_buf *fb = wmalloc(sizeof(*fb));
memset(fb, 0, sizeof(*fb));
fb->image = pixman_image_create_bits(PIXMAN_a8r8g8b8, w, h, NULL, 0);
if (!fb->image) { wfree(fb); return; }
wlr_buffer_init(&fb->base, &frame_buf_impl, w, h);
fb->id = client; /* use the WINGs view ID directly */
fb->parent_id = new_parent;
fb->rel_x = x;
fb->rel_y = y;
fb->x = parent_fb->x + x;
fb->y = parent_fb->y + y;
fb->width = w;
fb->height = h;
if (wl_state.scene) {
/* Place the WINGs content under the parent frame's tree so it
* moves with the WM-managed dialog decorations. */
struct wlr_scene_tree *_wp = parent_fb->tree
? parent_fb->tree
: (wl_state.level_trees[WL_LAYER_FLOATING]
? wl_state.level_trees[WL_LAYER_FLOATING]
: &wl_state.scene->tree);
fb->scene_buf = wlr_scene_buffer_create(_wp, &fb->base);
if (fb->scene_buf) {
fb_attach_scene_buf_listener(fb);
/* If under the frame tree, use relative coords; otherwise absolute. */
if (parent_fb->tree)
wlr_scene_node_set_position(&fb->scene_buf->node, x, y);
else
wlr_scene_node_set_position(&fb->scene_buf->node, fb->x, fb->y);
wlr_scene_node_set_enabled(&fb->scene_buf->node, false);
}
}
fb->canary = 0xDEADBEEF;
wl_list_insert(&wl_state.frame_list, &fb->link);
/* Connect the WINGs view's backing store to this frame_buf's image */
extern void W_SetViewBacking(unsigned long id, void *img);
W_SetViewBacking((unsigned long)client, fb->image);
}
/* Reparent an existing frame_buf into a new parent */
else if (client >= 0x10000000 && new_parent >= 0x10000000) {
struct wl_frame_buf *fb = frame_buf_find(client);
struct wl_frame_buf *parent_fb = frame_buf_find(new_parent);
if (fb && parent_fb) {
fb->parent_id = new_parent;
fb->rel_x = x;
fb->rel_y = y;
fb->x = parent_fb->x + x;
fb->y = parent_fb->y + y;
if (fb->tree) {
/* This frame_buf has a tree (e.g. fake_leader): reparent
* the whole tree so all children move with it. */
if (parent_fb->tree)
wlr_scene_node_reparent(&fb->tree->node, parent_fb->tree);
wlr_scene_node_set_position(&fb->tree->node, x, y);
} else if (fb->scene_buf) {
/* Plain frame_buf (no tree): reparent scene_buf directly. */
if (parent_fb->tree)
wlr_scene_node_reparent(&fb->scene_buf->node, parent_fb->tree);
wlr_scene_node_set_position(&fb->scene_buf->node, x, y);
}
/* Update absolute tracking recursively for all descendants */
frame_buf_update_children_abs(client);
}
}
}
/* Called by WINGs after a view repaints — commit its frame_buf to scene graph */
void wl_view_commit_backing_impl(unsigned long id)
{
struct wl_frame_buf *fb = frame_buf_find((WNativeWindow)id);
WNativeWindow saved_parent_id = 0;
int saved_rel_x = 0, saved_rel_y = 0;
/* If the view was resized, the old frame_buf's image is stale. */
if (fb) {
extern void *W_GetViewBacking(unsigned long id);
void *current = W_GetViewBacking((unsigned long)id);
if (current && current != (void *)fb->image && !fb->parent_id) {
/* Save parent relation before destroying — the new frame_buf
* must be positioned within the same parent tree so dialog
* content reparented via wl_client_reparent stays visible. */
saved_parent_id = fb->parent_id;
saved_rel_x = fb->rel_x;
saved_rel_y = fb->rel_y;
wlr_scene_node_destroy(&fb->scene_buf->node);
wl_list_remove(&fb->link);
wlr_buffer_drop(&fb->base);
wfree(fb);
fb = NULL;
}
}
if (!fb) {
extern W_View *W_GetViewForWindow(unsigned long id);
extern void *W_GetViewBacking(unsigned long id);
W_View *view = W_GetViewForWindow((unsigned long)id);
if (view && view->flags.topLevel) {
fb = wmalloc(sizeof(*fb));
memset(fb, 0, sizeof(*fb));
fb->id = id;
fb->parent_id = saved_parent_id;
fb->rel_x = saved_rel_x;
fb->rel_y = saved_rel_y;
fb->x = view->pos.x;
fb->y = view->pos.y;
fb->width = view->size.width;
fb->height = view->size.height;
fb->image = (pixman_image_t *)W_GetViewBacking(id);
if (fb->image) {
pixman_image_ref(fb->image);
wlr_buffer_init(&fb->base, &frame_buf_impl,
pixman_image_get_width(fb->image),
pixman_image_get_height(fb->image));
if (wl_state.scene) {
struct wlr_scene_tree *parent = NULL;
/* Re-parent under the saved parent's tree so
* dialogs reparented via wl_client_reparent stay
* correctly positioned within their frame. */
if (saved_parent_id) {
struct wl_frame_buf *pfb =
frame_buf_find(saved_parent_id);
if (pfb && pfb->tree)
parent = pfb->tree;
}
if (!parent)
parent = wl_state.level_trees[WL_LAYER_FLOATING]
? wl_state.level_trees[WL_LAYER_FLOATING]
: &wl_state.scene->tree;
fb->scene_buf = wlr_scene_buffer_create(parent, &fb->base);
if (fb->scene_buf) {
fb_attach_scene_buf_listener(fb);
/* Position relative to parent's tree or absolute */
if (parent != &wl_state.scene->tree && saved_parent_id)
wlr_scene_node_set_position(&fb->scene_buf->node,
saved_rel_x, saved_rel_y);
else
wlr_scene_node_set_position(&fb->scene_buf->node,
fb->x, fb->y);
wlr_scene_node_set_enabled(&fb->scene_buf->node, false);
}
}
fb->canary = 0xDEADBEEF;
wl_list_insert(&wl_state.frame_list, &fb->link);
} else {
wfree(fb);
fb = NULL;
}
}
}
if (fb && fb->scene_buf) {
/* Sync position from the WINGs view — it may have moved since
* the frame_buf was created (e.g. switch panel is positioned
* after widget creation via WMMoveWidget).
* When reparented (parent_id set), use the relative offset
* instead of the view's absolute position. */
extern W_View *W_GetViewForWindow(unsigned long id);
W_View *v = W_GetViewForWindow((unsigned long)id);
if (v && !fb->parent_id) {
if (fb->x != v->pos.x || fb->y != v->pos.y) {
fb->x = v->pos.x;
fb->y = v->pos.y;
wlr_scene_node_set_position(&fb->scene_buf->node,
fb->x, fb->y);
}
}
/* Only enable the scene node if the view is still mapped.
* If the view was unmapped (e.g. move ended), wl_unmap disables
* it via W_ViewSceneNodeSetEnabledFn — don't re-enable here. */
if (!v || v->flags.mapped) {
wlr_scene_buffer_set_buffer_with_damage(fb->scene_buf, &fb->base, NULL);
wlr_scene_node_set_enabled(&fb->scene_buf->node, true);
}
}
}
extern uint32_t wlc_top_get_surface_id(WNativeWindow id);
void
wl_view_scene_node_set_enabled(unsigned long id, int enabled)
{
struct wl_frame_buf *fb = frame_buf_find((WNativeWindow)id);
if (fb && fb->scene_buf) {
wlr_scene_node_set_enabled(&fb->scene_buf->node, !!enabled);
return;
}
/* WINGs client toplevels: match client proxy ID to compositor
* wlr_surface resource ID for scene node enable/disable. */
uint32_t proxy_id = wlc_top_get_surface_id((WNativeWindow)id);
if (!proxy_id) {
return;
}
struct wl_toplevel_view *v;
wl_list_for_each(v, &wl_state.toplevel_list, link) {
if (v->scene_node && v->xdg_surface
&& v->xdg_surface->surface
&& v->xdg_surface->surface->resource
&& v->xdg_surface->surface->resource->object.id == proxy_id) {
wlr_scene_node_set_enabled(&v->scene_node->node, !!enabled);
wwarning("set_enabled: matched view 0x%lx for id=0x%lx en=%d",
(unsigned long)v->id, id, enabled);
return;
}
}
wwarning("set_enabled: NO MATCH for id=0x%lx proxy=%u", id, proxy_id);
}
void wl_view_destroy_backing_impl(unsigned long id)
{
/* NULL out last_hovered_view if it points to this dying view.
* Avoids UAF when the motion handler reads the stale pointer. */
if (wl_state.last_hovered_view
&& (unsigned long)wl_state.last_hovered_view->window == id)
wl_state.last_hovered_view = NULL;
struct wl_frame_buf *fb = frame_buf_find((WNativeWindow)id);
if (!fb) return;
wl_list_remove(&fb->link);
/* fb->scene_buf may have been auto-destroyed by wlroots already if a
* parent tree was destroyed earlier (e.g. wl_frame_destroy on the
* surrounding WMaker frame). The destroy listener NULLs scene_buf
* in that case. */
if (fb->scene_buf)
wlr_scene_node_destroy(&fb->scene_buf->node);
wlr_buffer_drop(&fb->base);
}
void
wl_client_synth_configure(WNativeWindow client_win,
int x, int y, int width, int height,
int border_width, WNativeWindow above)
{
/* send an XDG configure with the new size to the client.
* also record the WM-assigned position in the view so
* pointer hit-testing (view_surface_at) uses current coordinates.
* XWayland uses wlr_xwayland_surface_configure instead. */
(void)border_width; (void)above;
struct wl_toplevel_view *v = wl_find_view_by_id(client_win);
if (!v) return;
v->x = x;
v->y = y;
/* Update relative offset so frame_configure keeps us in sync. */
if (v->frame_id) {
struct wl_frame_buf *pfb = frame_buf_find(v->frame_id);
if (pfb) {
v->frame_rel_x = x - pfb->x;
v->frame_rel_y = y - pfb->y;
}
/* Scene node is child of frame tree — use relative coords */
if (v->scene_node)
wlr_scene_node_set_position(&v->scene_node->node,
v->frame_rel_x, v->frame_rel_y);
} else {
/* No frame — use absolute coords */
if (v->scene_node)
wlr_scene_node_set_position(&v->scene_node->node, x, y);
}
if (v->xw_surface) {
wlr_xwayland_surface_configure(v->xw_surface,
(int16_t)x, (int16_t)y,
width > 0 ? (uint16_t)width : v->xw_surface->width,
height > 0 ? (uint16_t)height : v->xw_surface->height);
return;
}
if (!v->xdg_surface || !v->xdg_surface->toplevel) return;
if (width > 0 && height > 0)
wlr_xdg_toplevel_set_size(v->xdg_surface->toplevel, (uint32_t)width, (uint32_t)height);
wlr_xdg_surface_schedule_configure(v->xdg_surface);
}
void wl_client_add_to_save_set(WNativeWindow win) { (void)win; }
void wl_client_remove_from_save_set(WNativeWindow win) { (void)win; }
void
wl_client_close(WNativeWindow win)
{
/* ask the surface to close.
* XWayland uses wlr_xwayland_surface_close. */
struct wl_toplevel_view *v = wl_find_view_by_id(win);
if (!v) return;
if (v->xw_surface) {
wlr_xwayland_surface_close(v->xw_surface);
} else if (v->xdg_surface && v->xdg_surface->toplevel) {
wlr_xdg_toplevel_send_close(v->xdg_surface->toplevel);
/* Flush so the in-process client receives the close event,
* then dispatch so it processes it on this iteration. */
wl_display_flush_clients(wl_state.display);
}
}
void wl_client_query_shaped(WNativeWindow win, int *shaped_out)
{ (void)win; *shaped_out = 0; }
int
wl_client_get_gnustep_attrs(WNativeWindow win, GNUstepWMAttributes **attr_out)
{ (void)win; *attr_out = NULL; return 0; }
int wl_client_get_pid(WNativeWindow win)
{
struct wl_toplevel_view *v = wl_find_view_by_id(win);
if (!v) return 0;
if (v->xw_surface)
return (int)v->xw_surface->pid;
if (!v->xdg_surface || !v->xdg_surface->resource)
return 0;
struct wl_client *client = wl_resource_get_client(v->xdg_surface->resource);
if (!client) return 0;
pid_t pid = 0; uid_t uid = 0; gid_t gid = 0;
wl_client_get_credentials(client, &pid, &uid, &gid);
return (int)pid;
}
RImage *
wl_client_snapshot(WScreen *scr, WNativeWindow client_win, WNativeWindow frame_win)
{
(void)frame_win;
struct wl_toplevel_view *v = wl_find_view_by_id(client_win);
if (!v) return NULL;
/* XWayland: capture via wl_xwayland_capture_snapshot */
if (v->xw_surface) {
return wl_xwayland_capture_snapshot(scr, v->xw_surface);
}
/* Native Wayland: read pixels from last committed surface buffer */
if (!v->xdg_surface || !v->xdg_surface->surface) return NULL;
struct wlr_surface *s = v->xdg_surface->surface;
if (!s->buffer) return NULL;
struct wlr_buffer *buf = &s->buffer->base;
uint32_t fmt;
void *data;
size_t stride;
if (!wlr_buffer_begin_data_ptr_access(buf, WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &fmt, &stride))
return NULL;
int w = s->current.width;
int h = s->current.height;
if (w <= 0 || h <= 0) {
wlr_buffer_end_data_ptr_access(buf);
return NULL;
}
/* Create RImage and copy pixels (ARGB32 → RGBA byte order swap) */
RImage *res = RCreateImage(w, h, True);
if (res) {
unsigned char *src = (unsigned char *)data;
unsigned char *dst = res->data;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int si = y * stride + x * 4;
int di = (y * w + x) * 4;
dst[di + 0] = src[si + 2]; /* B → R */
dst[di + 1] = src[si + 1]; /* G → G */
dst[di + 2] = src[si + 0]; /* R → B */
dst[di + 3] = src[si + 3]; /* A → A */
}
}
}
wlr_buffer_end_data_ptr_access(buf);
return res;
}
void wl_client_set_state(WWindow *wwin, int state, WNativeWindow icon_win)
{
(void)icon_win;
struct wl_toplevel_view *v = wl_find_view_by_id(wwin->client_win);
if (!v)
return;
switch (state) {
case 3: /* WM_STATE_ICONIC */
if (v->scene_node)
wlr_scene_node_set_enabled(&v->scene_node->node, false);
break;
case 1: /* WM_STATE_NORMAL */
if (v->scene_node)
wlr_scene_node_set_enabled(&v->scene_node->node, true);
break;
}
}
void wl_client_set_maximized(WNativeWindow win, int maximized)
{
struct wl_toplevel_view *v = wl_find_view_by_id(win);
if (!v) return;
if (v->xdg_surface && v->xdg_surface->toplevel)
wlr_xdg_toplevel_set_maximized(v->xdg_surface->toplevel, maximized);
}
void wl_client_set_fullscreen(WNativeWindow win, int fullscreen)
{
struct wl_toplevel_view *v = wl_find_view_by_id(win);
if (!v) return;
if (v->xdg_surface && v->xdg_surface->toplevel)
wlr_xdg_toplevel_set_fullscreen(v->xdg_surface->toplevel, fullscreen);
}
void wl_client_send_protocol(WWindow *wwin, unsigned long protocol,
unsigned long timestamp)
{
(void)protocol; (void)timestamp;
if (wwin->flags.internal_window) {
wUnmanageWindow(wwin, False, False);
return;
}
wm_backend->client_close(wwin->client_win);
}
void wl_client_kill_impl(WWindow *wwin)
{
wm_backend->client_close(wwin->client_win);
}
#if USE_XWAYLAND
void wl_client_message_forward(WMEvent *event, WNativeWindow target_win)
{
(void)event; (void)target_win;
}
void wl_client_send_key(WNativeWindow win, unsigned int keycode,
unsigned int modifier_mask, int press)
{
(void)win; (void)keycode; (void)modifier_mask; (void)press;
}
#endif
char *wl_get_command_for_window(WNativeWindow win, int elements)
{
char **argv, *command = NULL;
int argc;
if (!wm_backend->icccm_get_command(win, &argv, &argc))
return NULL;
if (argc > 0 && argv != NULL) {
if (elements == 0)
elements = argc;
command = wtokenjoin(argv, WMIN(argc, elements));
if (command[0] == 0) {
wfree(command);
command = NULL;
}
}
wm_backend->icccm_free_command(argv);
return command;
}