wlroots-hyprland/xwayland/xwm.c

1362 lines
38 KiB
C
Raw Normal View History

2017-09-29 15:57:21 +02:00
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#include <stdlib.h>
2017-10-26 16:39:36 +02:00
#include <unistd.h>
#include <xcb/composite.h>
2017-10-15 20:50:21 +02:00
#include <xcb/xfixes.h>
2017-11-02 00:03:55 +01:00
#include <xcb/xcb_image.h>
2017-11-02 15:52:02 +01:00
#include <xcb/render.h>
#include "wlr/util/log.h"
#include "wlr/types/wlr_surface.h"
#include "wlr/xwayland.h"
2017-11-02 15:52:02 +01:00
#include "wlr/xcursor.h"
#include "xwm.h"
2017-10-05 12:32:12 +02:00
#ifdef HAS_XCB_ICCCM
#include <xcb/xcb_icccm.h>
#endif
2017-09-29 00:47:20 +02:00
const char *atom_map[ATOM_LAST] = {
"WL_SURFACE_ID",
"WM_DELETE_WINDOW",
2017-09-29 00:47:20 +02:00
"WM_PROTOCOLS",
"WM_HINTS",
2017-10-02 17:34:55 +02:00
"WM_NORMAL_HINTS",
"WM_SIZE_HINTS",
"_MOTIF_WM_HINTS",
"UTF8_STRING",
2017-09-29 00:47:20 +02:00
"WM_S0",
"_NET_SUPPORTED",
"_NET_WM_S0",
2017-09-29 22:43:14 +02:00
"_NET_WM_PID",
2017-09-29 15:57:21 +02:00
"_NET_WM_NAME",
2017-09-29 00:47:20 +02:00
"_NET_WM_STATE",
2017-09-29 23:03:01 +02:00
"_NET_WM_WINDOW_TYPE",
2017-09-29 00:47:20 +02:00
"WM_TAKE_FOCUS",
2017-10-24 17:00:41 +02:00
"WINDOW",
"_NET_ACTIVE_WINDOW",
2017-10-26 15:01:53 +02:00
"_NET_WM_MOVERESIZE",
2017-10-26 16:39:36 +02:00
"_NET_WM_NAME",
"_NET_SUPPORTING_WM_CHECK",
"_NET_WM_STATE_FULLSCREEN",
"_NET_WM_STATE_MAXIMIZED_VERT",
"_NET_WM_STATE_MAXIMIZED_HORZ",
2017-11-19 23:55:02 +01:00
"WM_STATE",
2017-09-29 00:47:20 +02:00
};
/* General helpers */
// TODO: replace this with hash table?
2017-10-24 20:37:18 +02:00
static struct wlr_xwayland_surface *lookup_surface(struct wlr_xwm *xwm,
xcb_window_t window_id) {
struct wlr_xwayland_surface *surface;
wl_list_for_each(surface, &xwm->surfaces, link) {
2017-10-24 20:37:18 +02:00
if (surface->window_id == window_id) {
return surface;
}
}
return NULL;
}
static struct wlr_xwayland_surface *wlr_xwayland_surface_create(
struct wlr_xwm *xwm, xcb_window_t window_id, int16_t x, int16_t y,
uint16_t width, uint16_t height, bool override_redirect) {
struct wlr_xwayland_surface *surface =
calloc(1, sizeof(struct wlr_xwayland_surface));
if (!surface) {
wlr_log(L_ERROR, "Could not allocate wlr xwayland surface");
return NULL;
}
2017-10-24 19:24:26 +02:00
xcb_get_geometry_cookie_t geometry_cookie =
xcb_get_geometry(xwm->xcb_conn, window_id);
2017-10-24 19:24:26 +02:00
uint32_t values[1];
values[0] =
XCB_EVENT_MASK_FOCUS_CHANGE |
XCB_EVENT_MASK_PROPERTY_CHANGE;
xcb_change_window_attributes(xwm->xcb_conn, window_id,
XCB_CW_EVENT_MASK, &values);
surface->xwm = xwm;
surface->window_id = window_id;
surface->x = x;
surface->y = y;
surface->width = width;
surface->height = height;
surface->override_redirect = override_redirect;
wl_list_insert(&xwm->surfaces, &surface->link);
wl_signal_init(&surface->events.destroy);
wl_signal_init(&surface->events.request_configure);
2017-10-27 16:48:09 +02:00
wl_signal_init(&surface->events.request_move);
wl_signal_init(&surface->events.request_resize);
2017-10-27 19:05:14 +02:00
wl_signal_init(&surface->events.request_maximize);
wl_signal_init(&surface->events.request_fullscreen);
2017-10-25 03:07:46 +02:00
wl_signal_init(&surface->events.map_notify);
wl_signal_init(&surface->events.unmap_notify);
wl_signal_init(&surface->events.set_class);
wl_signal_init(&surface->events.set_title);
2017-09-29 20:22:35 +02:00
wl_signal_init(&surface->events.set_parent);
2017-09-29 23:03:01 +02:00
wl_signal_init(&surface->events.set_pid);
wl_signal_init(&surface->events.set_window_type);
xcb_get_geometry_reply_t *geometry_reply =
xcb_get_geometry_reply(xwm->xcb_conn, geometry_cookie, NULL);
if (geometry_reply != NULL) {
surface->has_alpha = geometry_reply->depth == 32;
}
free(geometry_reply);
return surface;
}
static void xwm_set_net_active_window(struct wlr_xwm *xwm,
xcb_window_t window) {
xcb_change_property(xwm->xcb_conn, XCB_PROP_MODE_REPLACE,
xwm->screen->root, xwm->atoms[_NET_ACTIVE_WINDOW],
xwm->atoms[WINDOW], 32, 1, &window);
}
static void xwm_send_focus_window(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface) {
2017-10-29 15:40:16 +01:00
if (!xsurface) {
xcb_set_input_focus_checked(xwm->xcb_conn,
XCB_INPUT_FOCUS_POINTER_ROOT,
XCB_NONE, XCB_CURRENT_TIME);
2017-10-29 15:40:16 +01:00
return;
} else if (xsurface->override_redirect) {
return;
}
2017-10-29 15:40:16 +01:00
xcb_client_message_event_t client_message;
client_message.response_type = XCB_CLIENT_MESSAGE;
client_message.format = 32;
client_message.window = xsurface->window_id;
client_message.type = xwm->atoms[WM_PROTOCOLS];
client_message.data.data32[0] = xwm->atoms[WM_TAKE_FOCUS];
client_message.data.data32[1] = XCB_TIME_CURRENT_TIME;
xcb_send_event(xwm->xcb_conn, 0, xsurface->window_id,
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (char*)&client_message);
xcb_set_input_focus(xwm->xcb_conn, XCB_INPUT_FOCUS_POINTER_ROOT,
xsurface->window_id, XCB_CURRENT_TIME);
uint32_t values[1];
values[0] = XCB_STACK_MODE_ABOVE;
xcb_configure_window(xwm->xcb_conn, xsurface->window_id,
XCB_CONFIG_WINDOW_STACK_MODE, values);
}
void xwm_surface_activate(struct wlr_xwm *xwm,
struct wlr_xwayland_surface *xsurface) {
if (xwm->focus_surface == xsurface ||
(xsurface && xsurface->override_redirect)) {
return;
}
if (xsurface) {
xwm_set_net_active_window(xwm, xsurface->window_id);
} else {
xwm_set_net_active_window(xwm, XCB_WINDOW_NONE);
}
xwm_send_focus_window(xwm, xsurface);
xwm->focus_surface = xsurface;
xcb_flush(xwm->xcb_conn);
}
static void xsurface_set_net_wm_state(struct wlr_xwayland_surface *xsurface) {
struct wlr_xwm *xwm = xsurface->xwm;
uint32_t property[3];
int i;
i = 0;
if (xsurface->fullscreen) {
property[i++] = xwm->atoms[_NET_WM_STATE_FULLSCREEN];
}
if (xsurface->maximized_vert) {
property[i++] = xwm->atoms[_NET_WM_STATE_MAXIMIZED_VERT];
}
if (xsurface->maximized_horz) {
property[i++] = xwm->atoms[_NET_WM_STATE_MAXIMIZED_HORZ];
}
xcb_change_property(xwm->xcb_conn,
XCB_PROP_MODE_REPLACE,
xsurface->window_id,
xwm->atoms[NET_WM_STATE],
XCB_ATOM_ATOM,
32, // format
i, property);
}
2017-10-29 15:31:01 +01:00
static void wlr_xwayland_surface_destroy(
struct wlr_xwayland_surface *xsurface) {
wl_signal_emit(&xsurface->events.destroy, xsurface);
2017-10-24 20:37:18 +02:00
2017-10-29 15:31:01 +01:00
if (xsurface == xsurface->xwm->focus_surface) {
xwm_surface_activate(xsurface->xwm, NULL);
}
2017-10-29 15:31:01 +01:00
wl_list_remove(&xsurface->link);
2017-10-24 20:37:18 +02:00
2017-10-29 15:31:01 +01:00
if (xsurface->surface_id) {
wl_list_remove(&xsurface->unpaired_link);
2017-10-24 20:37:18 +02:00
}
2017-10-29 15:31:01 +01:00
if (xsurface->surface) {
wl_list_remove(&xsurface->surface_destroy.link);
wl_list_remove(&xsurface->surface_commit.link);
}
2017-10-29 15:31:01 +01:00
free(xsurface->title);
free(xsurface->class);
free(xsurface->instance);
free(xsurface->window_type);
free(xsurface->protocols);
free(xsurface->hints);
free(xsurface->size_hints);
free(xsurface);
}
static void read_surface_class(struct wlr_xwm *xwm,
struct wlr_xwayland_surface *surface, xcb_get_property_reply_t *reply) {
if (reply->type != XCB_ATOM_STRING &&
reply->type != xwm->atoms[UTF8_STRING]) {
return;
}
size_t len = xcb_get_property_value_length(reply);
char *class = xcb_get_property_value(reply);
// Unpack two sequentially stored strings: instance, class
size_t instance_len = strnlen(class, len);
free(surface->instance);
if (len > 0 && instance_len < len) {
surface->instance = strndup(class, instance_len);
class += instance_len + 1;
} else {
surface->instance = NULL;
}
free(surface->class);
if (len > 0) {
surface->class = strndup(class, len);
} else {
surface->class = NULL;
}
2017-10-25 01:18:31 +02:00
wlr_log(L_DEBUG, "XCB_ATOM_WM_CLASS: %s %s", surface->instance,
surface->class);
wl_signal_emit(&surface->events.set_class, surface);
}
static void read_surface_title(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
if (reply->type != XCB_ATOM_STRING &&
reply->type != xwm->atoms[UTF8_STRING]) {
return;
}
// TODO: if reply->type == XCB_ATOM_STRING, uses latin1 encoding
// if reply->type == xwm->atoms[UTF8_STRING], uses utf8 encoding
size_t len = xcb_get_property_value_length(reply);
char *title = xcb_get_property_value(reply);
2017-10-29 15:31:01 +01:00
free(xsurface->title);
if (len > 0) {
2017-10-29 15:31:01 +01:00
xsurface->title = strndup(title, len);
} else {
2017-10-29 15:31:01 +01:00
xsurface->title = NULL;
}
2017-10-29 15:31:01 +01:00
wlr_log(L_DEBUG, "XCB_ATOM_WM_NAME: %s", xsurface->title);
wl_signal_emit(&xsurface->events.set_title, xsurface);
}
static void read_surface_parent(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
if (reply->type != XCB_ATOM_WINDOW) {
return;
}
xcb_window_t *xid = xcb_get_property_value(reply);
if (xid != NULL) {
2017-10-29 15:31:01 +01:00
xsurface->parent = lookup_surface(xwm, *xid);
} else {
2017-10-29 15:31:01 +01:00
xsurface->parent = NULL;
}
wlr_log(L_DEBUG, "XCB_ATOM_WM_TRANSIENT_FOR: %p", xid);
2017-10-29 15:31:01 +01:00
wl_signal_emit(&xsurface->events.set_parent, xsurface);
}
2017-09-29 22:43:14 +02:00
static void read_surface_pid(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
2017-09-29 22:43:14 +02:00
if (reply->type != XCB_ATOM_CARDINAL) {
return;
}
2017-09-29 23:03:01 +02:00
pid_t *pid = xcb_get_property_value(reply);
2017-10-29 15:31:01 +01:00
xsurface->pid = *pid;
wlr_log(L_DEBUG, "NET_WM_PID %d", xsurface->pid);
wl_signal_emit(&xsurface->events.set_pid, xsurface);
2017-09-29 23:03:01 +02:00
}
static void read_surface_window_type(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
2017-09-29 23:03:01 +02:00
if (reply->type != XCB_ATOM_ATOM) {
return;
}
xcb_atom_t *atoms = xcb_get_property_value(reply);
size_t atoms_len = reply->value_len;
size_t atoms_size = sizeof(xcb_atom_t) * atoms_len;
2017-10-29 15:31:01 +01:00
free(xsurface->window_type);
xsurface->window_type = malloc(atoms_size);
if (xsurface->window_type == NULL) {
2017-09-29 23:03:01 +02:00
return;
}
2017-10-29 15:31:01 +01:00
memcpy(xsurface->window_type, atoms, atoms_size);
xsurface->window_type_len = atoms_len;
2017-09-29 23:03:01 +02:00
wlr_log(L_DEBUG, "NET_WM_WINDOW_TYPE (%zu)", atoms_len);
2017-10-29 15:31:01 +01:00
wl_signal_emit(&xsurface->events.set_window_type, xsurface);
2017-09-29 22:43:14 +02:00
}
static void read_surface_protocols(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
if (reply->type != XCB_ATOM_ATOM) {
return;
}
xcb_atom_t *atoms = xcb_get_property_value(reply);
size_t atoms_len = reply->value_len;
size_t atoms_size = sizeof(xcb_atom_t) * atoms_len;
2017-10-29 15:31:01 +01:00
free(xsurface->protocols);
xsurface->protocols = malloc(atoms_size);
if (xsurface->protocols == NULL) {
return;
}
2017-10-29 15:31:01 +01:00
memcpy(xsurface->protocols, atoms, atoms_size);
xsurface->protocols_len = atoms_len;
wlr_log(L_DEBUG, "WM_PROTOCOLS (%zu)", atoms_len);
}
2017-10-05 16:24:45 +02:00
#ifdef HAS_XCB_ICCCM
static void read_surface_hints(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
2017-10-05 16:24:45 +02:00
// According to the docs, reply->type == xwm->atoms[WM_HINTS]
// In practice, reply->type == XCB_ATOM_ATOM
if (reply->value_len == 0) {
return;
}
2017-10-05 22:23:37 +02:00
xcb_icccm_wm_hints_t hints;
xcb_icccm_get_wm_hints_from_reply(&hints, reply);
2017-10-29 15:31:01 +01:00
free(xsurface->hints);
xsurface->hints = calloc(1, sizeof(struct wlr_xwayland_surface_hints));
if (xsurface->hints == NULL) {
return;
}
2017-10-29 15:31:01 +01:00
memcpy(xsurface->hints, &hints, sizeof(struct wlr_xwayland_surface_hints));
xsurface->hints_urgency = xcb_icccm_wm_hints_get_urgency(&hints);
2017-10-05 16:24:45 +02:00
wlr_log(L_DEBUG, "WM_HINTS (%d)", reply->value_len);
}
#else
static void read_surface_hints(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
2017-10-05 16:24:45 +02:00
// Do nothing
}
#endif
2017-10-05 12:32:12 +02:00
#ifdef HAS_XCB_ICCCM
2017-10-02 17:34:55 +02:00
static void read_surface_normal_hints(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
2017-10-05 16:24:45 +02:00
if (reply->type != xwm->atoms[WM_SIZE_HINTS] || reply->value_len == 0) {
2017-10-02 17:34:55 +02:00
return;
}
2017-10-05 22:23:37 +02:00
xcb_size_hints_t size_hints;
xcb_icccm_get_wm_size_hints_from_reply(&size_hints, reply);
2017-10-29 15:31:01 +01:00
free(xsurface->size_hints);
xsurface->size_hints =
2017-10-05 22:23:37 +02:00
calloc(1, sizeof(struct wlr_xwayland_surface_size_hints));
2017-10-29 15:31:01 +01:00
if (xsurface->size_hints == NULL) {
return;
}
2017-10-29 15:31:01 +01:00
memcpy(xsurface->size_hints, &size_hints,
2017-10-05 22:23:37 +02:00
sizeof(struct wlr_xwayland_surface_size_hints));
2017-10-02 17:34:55 +02:00
wlr_log(L_DEBUG, "WM_NORMAL_HINTS (%d)", reply->value_len);
}
2017-10-05 12:32:12 +02:00
#else
static void read_surface_normal_hints(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
2017-10-05 12:32:12 +02:00
// Do nothing
}
#endif
2017-10-02 17:34:55 +02:00
2017-10-05 16:49:47 +02:00
#define MWM_HINTS_FLAGS_FIELD 0
#define MWM_HINTS_DECORATIONS_FIELD 2
#define MWM_HINTS_DECORATIONS (1 << 1)
#define MWM_DECOR_ALL (1 << 0)
#define MWM_DECOR_BORDER (1 << 1)
#define MWM_DECOR_TITLE (1 << 3)
2017-10-02 17:34:55 +02:00
static void read_surface_motif_hints(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
if (reply->value_len < 5) {
return;
}
uint32_t *motif_hints = xcb_get_property_value(reply);
2017-10-05 16:49:47 +02:00
if (motif_hints[MWM_HINTS_FLAGS_FIELD] & MWM_HINTS_DECORATIONS) {
2017-10-29 15:31:01 +01:00
xsurface->decorations = WLR_XWAYLAND_SURFACE_DECORATIONS_ALL;
2017-10-05 16:49:47 +02:00
uint32_t decorations = motif_hints[MWM_HINTS_DECORATIONS_FIELD];
if ((decorations & MWM_DECOR_ALL) == 0) {
if ((decorations & MWM_DECOR_BORDER) == 0) {
2017-10-29 15:31:01 +01:00
xsurface->decorations |=
2017-10-05 16:49:47 +02:00
WLR_XWAYLAND_SURFACE_DECORATIONS_NO_BORDER;
}
if ((decorations & MWM_DECOR_TITLE) == 0) {
2017-10-29 15:31:01 +01:00
xsurface->decorations |=
2017-10-05 16:49:47 +02:00
WLR_XWAYLAND_SURFACE_DECORATIONS_NO_TITLE;
}
}
}
2017-10-02 17:34:55 +02:00
wlr_log(L_DEBUG, "MOTIF_WM_HINTS (%d)", reply->value_len);
}
2017-10-27 19:05:14 +02:00
static void read_surface_net_wm_state(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface,
xcb_get_property_reply_t *reply) {
2017-10-27 19:05:14 +02:00
xsurface->fullscreen = 0;
xcb_atom_t *atom = xcb_get_property_value(reply);
for (uint32_t i = 0; i < reply->value_len; i++) {
if (atom[i] == xwm->atoms[_NET_WM_STATE_FULLSCREEN])
xsurface->fullscreen = true;
if (atom[i] == xwm->atoms[_NET_WM_STATE_MAXIMIZED_VERT])
xsurface->maximized_vert = true;
if (atom[i] == xwm->atoms[_NET_WM_STATE_MAXIMIZED_HORZ])
xsurface->maximized_horz = true;
}
}
2017-09-29 15:57:21 +02:00
static void read_surface_property(struct wlr_xwm *xwm,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface, xcb_atom_t property) {
2017-09-29 15:57:21 +02:00
xcb_get_property_cookie_t cookie = xcb_get_property(xwm->xcb_conn, 0,
2017-10-29 15:31:01 +01:00
xsurface->window_id, property, XCB_ATOM_ANY, 0, 2048);
2017-09-29 15:57:21 +02:00
xcb_get_property_reply_t *reply = xcb_get_property_reply(xwm->xcb_conn,
cookie, NULL);
if (reply == NULL) {
return;
}
if (property == XCB_ATOM_WM_CLASS) {
2017-10-29 15:31:01 +01:00
read_surface_class(xwm, xsurface, reply);
2017-09-29 15:57:21 +02:00
} else if (property == XCB_ATOM_WM_NAME ||
property == xwm->atoms[NET_WM_NAME]) {
2017-10-29 15:31:01 +01:00
read_surface_title(xwm, xsurface, reply);
} else if (property == XCB_ATOM_WM_TRANSIENT_FOR) {
2017-10-29 15:31:01 +01:00
read_surface_parent(xwm, xsurface, reply);
2017-09-29 22:43:14 +02:00
} else if (property == xwm->atoms[NET_WM_PID]) {
2017-10-29 15:31:01 +01:00
read_surface_pid(xwm, xsurface, reply);
2017-09-29 23:03:01 +02:00
} else if (property == xwm->atoms[NET_WM_WINDOW_TYPE]) {
2017-10-29 15:31:01 +01:00
read_surface_window_type(xwm, xsurface, reply);
} else if (property == xwm->atoms[WM_PROTOCOLS]) {
2017-10-29 15:31:01 +01:00
read_surface_protocols(xwm, xsurface, reply);
2017-09-29 22:26:03 +02:00
} else if (property == xwm->atoms[NET_WM_STATE]) {
2017-10-29 15:31:01 +01:00
read_surface_net_wm_state(xwm, xsurface, reply);
2017-10-05 16:24:45 +02:00
} else if (property == xwm->atoms[WM_HINTS]) {
2017-10-29 15:31:01 +01:00
read_surface_hints(xwm, xsurface, reply);
2017-10-02 17:34:55 +02:00
} else if (property == xwm->atoms[WM_NORMAL_HINTS]) {
2017-10-29 15:31:01 +01:00
read_surface_normal_hints(xwm, xsurface, reply);
2017-10-02 17:34:55 +02:00
} else if (property == xwm->atoms[MOTIF_WM_HINTS]) {
2017-10-29 15:31:01 +01:00
read_surface_motif_hints(xwm, xsurface, reply);
2017-09-29 15:57:21 +02:00
} else {
wlr_log(L_DEBUG, "unhandled x11 property %u", property);
}
free(reply);
}
static void handle_surface_commit(struct wl_listener *listener, void *data) {
struct wlr_xwayland_surface *xsurface =
wl_container_of(listener, xsurface, surface_commit);
if (!xsurface->added &&
wlr_surface_has_buffer(xsurface->surface) &&
xsurface->mapped) {
wl_signal_emit(&xsurface->xwm->xwayland->events.new_surface, xsurface);
xsurface->added = true;
}
}
static void handle_surface_destroy(struct wl_listener *listener, void *data) {
struct wlr_xwayland_surface *xsurface =
wl_container_of(listener, xsurface, surface_destroy);
xsurface->surface = NULL;
// TODO destroy xwayland surface?
}
2017-10-29 15:31:01 +01:00
static void xwm_map_shell_surface(struct wlr_xwm *xwm,
struct wlr_xwayland_surface *xsurface,
struct wlr_surface *surface) {
xsurface->surface = surface;
2017-09-29 15:57:21 +02:00
// read all surface properties
const xcb_atom_t props[] = {
XCB_ATOM_WM_CLASS,
XCB_ATOM_WM_NAME,
XCB_ATOM_WM_TRANSIENT_FOR,
xwm->atoms[WM_PROTOCOLS],
2017-10-05 16:24:45 +02:00
xwm->atoms[WM_HINTS],
2017-10-02 17:34:55 +02:00
xwm->atoms[WM_NORMAL_HINTS],
xwm->atoms[MOTIF_WM_HINTS],
2017-09-29 22:26:03 +02:00
xwm->atoms[NET_WM_STATE],
2017-09-29 23:03:01 +02:00
xwm->atoms[NET_WM_WINDOW_TYPE],
2017-09-29 22:26:03 +02:00
xwm->atoms[NET_WM_NAME],
2017-09-29 22:43:14 +02:00
xwm->atoms[NET_WM_PID],
2017-09-29 15:57:21 +02:00
};
for (size_t i = 0; i < sizeof(props)/sizeof(xcb_atom_t); i++) {
read_surface_property(xwm, xsurface, props[i]);
2017-09-29 15:57:21 +02:00
}
xsurface->surface_commit.notify = handle_surface_commit;
wl_signal_add(&surface->events.commit, &xsurface->surface_commit);
xsurface->surface_destroy.notify = handle_surface_destroy;
wl_signal_add(&surface->events.destroy, &xsurface->surface_destroy);
xsurface->mapped = true;
2017-10-25 03:07:46 +02:00
wl_signal_emit(&xsurface->events.map_notify, xsurface);
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_create_notify(struct wlr_xwm *xwm,
xcb_create_notify_event_t *ev) {
wlr_log(L_DEBUG, "XCB_CREATE_NOTIFY (%u)", ev->window);
wlr_xwayland_surface_create(xwm, ev->window, ev->x, ev->y,
2017-08-23 23:19:15 +02:00
ev->width, ev->height, ev->override_redirect);
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_destroy_notify(struct wlr_xwm *xwm,
xcb_destroy_notify_event_t *ev) {
wlr_log(L_DEBUG, "XCB_DESTROY_NOTIFY (%u)", ev->window);
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface = lookup_surface(xwm, ev->window);
if (xsurface == NULL) {
return;
}
2017-10-29 15:31:01 +01:00
wlr_xwayland_surface_destroy(xsurface);
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_configure_request(struct wlr_xwm *xwm,
xcb_configure_request_event_t *ev) {
wlr_log(L_DEBUG, "XCB_CONFIGURE_REQUEST (%u) [%ux%u+%d,%d]", ev->window,
2017-08-23 23:19:15 +02:00
ev->width, ev->height, ev->x, ev->y);
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface = lookup_surface(xwm, ev->window);
if (xsurface == NULL) {
return;
}
// TODO: handle ev->{parent,sibling}?
2017-08-20 14:18:43 +02:00
2017-10-29 15:31:01 +01:00
if (xsurface->surface == NULL) {
// Surface has not been mapped yet
2017-10-29 15:31:01 +01:00
wlr_xwayland_surface_configure(xwm->xwayland, xsurface, ev->x, ev->y,
ev->width, ev->height);
} else {
struct wlr_xwayland_surface_configure_event *wlr_event =
calloc(1, sizeof(struct wlr_xwayland_surface_configure_event));
if (wlr_event == NULL) {
return;
}
2017-10-29 15:31:01 +01:00
wlr_event->surface = xsurface;
wlr_event->x = ev->x;
wlr_event->y = ev->y;
wlr_event->width = ev->width;
wlr_event->height = ev->height;
2017-10-29 15:31:01 +01:00
wl_signal_emit(&xsurface->events.request_configure, wlr_event);
free(wlr_event);
}
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_configure_notify(struct wlr_xwm *xwm,
2017-10-24 19:57:10 +02:00
xcb_configure_notify_event_t *ev) {
struct wlr_xwayland_surface *xsurface =
lookup_surface(xwm, ev->window);
2017-10-24 19:57:10 +02:00
if (!xsurface) {
return;
}
xsurface->x = ev->x;
xsurface->y = ev->y;
xsurface->width = ev->width;
xsurface->height = ev->height;
}
2017-10-26 17:39:08 +02:00
#define ICCCM_WITHDRAWN_STATE 0
#define ICCCM_NORMAL_STATE 1
#define ICCCM_ICONIC_STATE 3
static void xsurface_set_wm_state(struct wlr_xwayland_surface *xsurface,
int32_t state) {
struct wlr_xwm *xwm = xsurface->xwm;
uint32_t property[2];
property[0] = state;
property[1] = XCB_WINDOW_NONE;
xcb_change_property(xwm->xcb_conn,
XCB_PROP_MODE_REPLACE,
xsurface->window_id,
2017-11-19 23:55:02 +01:00
xwm->atoms[WM_STATE],
xwm->atoms[WM_STATE],
2017-10-26 17:39:08 +02:00
32, // format
2, property);
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_map_request(struct wlr_xwm *xwm,
xcb_map_request_event_t *ev) {
wlr_log(L_DEBUG, "XCB_MAP_REQUEST (%u)", ev->window);
2017-10-26 17:39:08 +02:00
struct wlr_xwayland_surface *xsurface = lookup_surface(xwm, ev->window);
if (!xsurface) {
return;
}
xsurface_set_wm_state(xsurface, ICCCM_NORMAL_STATE);
xsurface_set_net_wm_state(xsurface);
2017-10-24 19:24:26 +02:00
xcb_map_window(xwm->xcb_conn, ev->window);
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_map_notify(struct wlr_xwm *xwm,
xcb_map_notify_event_t *ev) {
wlr_log(L_DEBUG, "XCB_MAP_NOTIFY (%u)", ev->window);
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_unmap_notify(struct wlr_xwm *xwm,
xcb_unmap_notify_event_t *ev) {
wlr_log(L_DEBUG, "XCB_UNMAP_NOTIFY (%u)", ev->window);
struct wlr_xwayland_surface *xsurface = lookup_surface(xwm, ev->window);
if (xsurface == NULL) {
return;
}
if (xsurface->surface_id) {
// Make sure we're not on the unpaired surface list or we
// could be assigned a surface during surface creation that
// was mapped before this unmap request.
wl_list_remove(&xsurface->unpaired_link);
xsurface->surface_id = 0;
}
if (xsurface->surface) {
wl_list_remove(&xsurface->surface_commit.link);
wl_list_remove(&xsurface->surface_destroy.link);
}
2017-10-25 03:07:46 +02:00
xsurface->surface = NULL;
2017-10-25 13:12:25 +02:00
if (xsurface->mapped) {
xsurface->mapped = false;
wl_signal_emit(&xsurface->events.unmap_notify, xsurface);
}
2017-10-26 17:39:08 +02:00
xsurface_set_wm_state(xsurface, ICCCM_WITHDRAWN_STATE);
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_property_notify(struct wlr_xwm *xwm,
xcb_property_notify_event_t *ev) {
wlr_log(L_DEBUG, "XCB_PROPERTY_NOTIFY (%u)", ev->window);
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface = lookup_surface(xwm, ev->window);
if (xsurface == NULL) {
2017-09-29 15:57:21 +02:00
return;
}
2017-10-29 15:31:01 +01:00
read_surface_property(xwm, xsurface, ev->atom);
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_surface_id_message(struct wlr_xwm *xwm,
2017-10-26 15:01:53 +02:00
xcb_client_message_event_t *ev) {
struct wlr_xwayland_surface *xsurface = lookup_surface(xwm, ev->window);
if (xsurface == NULL) {
wlr_log(L_DEBUG,
"client message WL_SURFACE_ID but no new window %u ?",
ev->window);
return;
}
/* Check if we got notified after wayland surface create event */
uint32_t id = ev->data.data32[0];
struct wl_resource *resource =
wl_client_get_object(xwm->xwayland->client, id);
if (resource) {
struct wlr_surface *surface = wl_resource_get_user_data(resource);
xsurface->surface_id = 0;
2017-10-29 15:31:01 +01:00
xwm_map_shell_surface(xwm, xsurface, surface);
2017-10-26 15:01:53 +02:00
} else {
xsurface->surface_id = id;
wl_list_insert(&xwm->unpaired_surfaces, &xsurface->unpaired_link);
}
}
2017-10-27 16:48:09 +02:00
#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
#define _NET_WM_MOVERESIZE_SIZE_TOP 1
#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
#define _NET_WM_MOVERESIZE_MOVE 8 // movement only
#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 // size via keyboard
#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 // move via keyboard
#define _NET_WM_MOVERESIZE_CANCEL 11 // cancel operation
2017-10-29 15:31:01 +01:00
static void xwm_handle_net_wm_moveresize_message(struct wlr_xwm *xwm,
2017-10-26 15:01:53 +02:00
xcb_client_message_event_t *ev) {
2017-10-27 16:48:09 +02:00
// same as xdg-toplevel-v6
// TODO need a common enum for this
static const int map[] = {
5, 1, 9, 8, 10, 2, 6, 4
};
struct wlr_xwayland_surface *xsurface = lookup_surface(xwm, ev->window);
if (!xsurface) {
return;
}
// TODO: we should probably add input or seat info to this but we would just
// be guessing
struct wlr_xwayland_resize_event resize_event;
struct wlr_xwayland_move_event move_event;
int detail = ev->data.data32[2];
switch (detail) {
case _NET_WM_MOVERESIZE_MOVE:
move_event.surface = xsurface;
wl_signal_emit(&xsurface->events.request_move, &move_event);
break;
case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
case _NET_WM_MOVERESIZE_SIZE_TOP:
case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
case _NET_WM_MOVERESIZE_SIZE_RIGHT:
case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
case _NET_WM_MOVERESIZE_SIZE_LEFT:
resize_event.surface = xsurface;
resize_event.edges = map[detail];
wl_signal_emit(&xsurface->events.request_resize, &resize_event);
break;
case _NET_WM_MOVERESIZE_CANCEL:
2017-10-29 15:31:01 +01:00
// handled by the compositor
2017-10-27 16:48:09 +02:00
break;
}
2017-10-26 15:01:53 +02:00
}
2017-10-27 19:05:14 +02:00
#define _NET_WM_STATE_REMOVE 0
#define _NET_WM_STATE_ADD 1
#define _NET_WM_STATE_TOGGLE 2
static bool update_state(int action, bool *state) {
int new_state, changed;
switch (action) {
case _NET_WM_STATE_REMOVE:
new_state = false;
break;
case _NET_WM_STATE_ADD:
new_state = true;
break;
case _NET_WM_STATE_TOGGLE:
new_state = !*state;
break;
default:
return false;
}
changed = (*state != new_state);
*state = new_state;
return changed;
}
2017-10-29 15:31:01 +01:00
static inline bool xsurface_is_maximized(
struct wlr_xwayland_surface *xsurface) {
2017-10-27 19:05:14 +02:00
return xsurface->maximized_horz && xsurface->maximized_vert;
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_net_wm_state_message(struct wlr_xwm *xwm,
2017-10-27 19:05:14 +02:00
xcb_client_message_event_t *client_message) {
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface =
lookup_surface(xwm, client_message->window);
2017-10-27 19:05:14 +02:00
if (!xsurface) {
return;
}
2017-11-09 11:21:55 +01:00
if (client_message->format != 32) {
return;
}
2017-10-27 19:05:14 +02:00
2017-11-09 11:21:55 +01:00
bool fullscreen = xsurface->fullscreen;
bool maximized = xsurface_is_maximized(xsurface);
2017-10-27 19:05:14 +02:00
uint32_t action = client_message->data.data32[0];
2017-11-09 11:21:55 +01:00
for (size_t i = 0; i < 2; ++i) {
uint32_t property = client_message->data.data32[1 + i];
2017-10-27 19:05:14 +02:00
2017-11-09 11:21:55 +01:00
if (property == xwm->atoms[_NET_WM_STATE_FULLSCREEN] &&
update_state(action, &xsurface->fullscreen)) {
xsurface_set_net_wm_state(xsurface);
} else if (property == xwm->atoms[_NET_WM_STATE_MAXIMIZED_VERT] &&
update_state(action, &xsurface->maximized_vert)) {
xsurface_set_net_wm_state(xsurface);
} else if (property == xwm->atoms[_NET_WM_STATE_MAXIMIZED_HORZ] &&
update_state(action, &xsurface->maximized_horz)) {
xsurface_set_net_wm_state(xsurface);
}
}
// client_message->data.data32[3] is the source indication
// all other values are set to 0
2017-10-27 19:05:14 +02:00
2017-11-09 11:21:55 +01:00
if (fullscreen != xsurface->fullscreen) {
2017-10-27 19:05:14 +02:00
if (xsurface->fullscreen) {
xsurface->saved_width = xsurface->width;
xsurface->saved_height = xsurface->height;
}
wl_signal_emit(&xsurface->events.request_fullscreen, xsurface);
2017-11-09 11:21:55 +01:00
}
2017-10-27 19:05:14 +02:00
2017-11-09 11:21:55 +01:00
if (maximized != xsurface_is_maximized(xsurface)) {
if (xsurface_is_maximized(xsurface)) {
xsurface->saved_width = xsurface->width;
xsurface->saved_height = xsurface->height;
2017-10-27 19:05:14 +02:00
}
2017-11-09 11:21:55 +01:00
wl_signal_emit(&xsurface->events.request_maximize, xsurface);
2017-10-27 19:05:14 +02:00
}
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_client_message(struct wlr_xwm *xwm,
xcb_client_message_event_t *ev) {
wlr_log(L_DEBUG, "XCB_CLIENT_MESSAGE (%u)", ev->window);
if (ev->type == xwm->atoms[WL_SURFACE_ID]) {
2017-10-29 15:31:01 +01:00
xwm_handle_surface_id_message(xwm, ev);
2017-09-29 22:26:03 +02:00
} else if (ev->type == xwm->atoms[NET_WM_STATE]) {
2017-10-29 15:31:01 +01:00
xwm_handle_net_wm_state_message(xwm, ev);
2017-10-26 15:01:53 +02:00
} else if (ev->type == xwm->atoms[_NET_WM_MOVERESIZE]) {
2017-10-29 15:31:01 +01:00
xwm_handle_net_wm_moveresize_message(xwm, ev);
2017-09-29 15:57:21 +02:00
} else {
wlr_log(L_DEBUG, "unhandled x11 client message %u", ev->type);
}
}
2017-10-29 15:31:01 +01:00
static void xwm_handle_focus_in(struct wlr_xwm *xwm,
2017-10-26 17:54:39 +02:00
xcb_focus_in_event_t *ev) {
// Do not interfere with grabs
if (ev->mode == XCB_NOTIFY_MODE_GRAB ||
ev->mode == XCB_NOTIFY_MODE_UNGRAB) {
return;
}
// Do not let X clients change the focus behind the compositor's
// back. Reset the focus to the old one if it changed.
if (!xwm->focus_surface || ev->event != xwm->focus_surface->window_id) {
xwm_send_focus_window(xwm, xwm->focus_surface);
}
}
/* This is in xcb/xcb_event.h, but pulling xcb-util just for a constant
* others redefine anyway is meh
*/
#define XCB_EVENT_RESPONSE_TYPE_MASK (0x7f)
static int x11_event_handler(int fd, uint32_t mask, void *data) {
int count = 0;
xcb_generic_event_t *event;
struct wlr_xwm *xwm = data;
while ((event = xcb_poll_for_event(xwm->xcb_conn))) {
count++;
switch (event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK) {
case XCB_CREATE_NOTIFY:
2017-10-29 15:31:01 +01:00
xwm_handle_create_notify(xwm, (xcb_create_notify_event_t *)event);
break;
case XCB_DESTROY_NOTIFY:
2017-10-29 15:31:01 +01:00
xwm_handle_destroy_notify(xwm, (xcb_destroy_notify_event_t *)event);
break;
case XCB_CONFIGURE_REQUEST:
2017-10-29 15:31:01 +01:00
xwm_handle_configure_request(xwm,
2017-10-25 01:18:31 +02:00
(xcb_configure_request_event_t *)event);
break;
2017-10-24 19:57:10 +02:00
case XCB_CONFIGURE_NOTIFY:
2017-10-29 15:31:01 +01:00
xwm_handle_configure_notify(xwm,
(xcb_configure_notify_event_t *)event);
2017-10-24 19:57:10 +02:00
break;
case XCB_MAP_REQUEST:
2017-10-29 15:31:01 +01:00
xwm_handle_map_request(xwm, (xcb_map_request_event_t *)event);
break;
case XCB_MAP_NOTIFY:
2017-10-29 15:31:01 +01:00
xwm_handle_map_notify(xwm, (xcb_map_notify_event_t *)event);
break;
case XCB_UNMAP_NOTIFY:
2017-10-29 15:31:01 +01:00
xwm_handle_unmap_notify(xwm, (xcb_unmap_notify_event_t *)event);
break;
case XCB_PROPERTY_NOTIFY:
2017-10-29 15:31:01 +01:00
xwm_handle_property_notify(xwm,
(xcb_property_notify_event_t *)event);
break;
case XCB_CLIENT_MESSAGE:
2017-10-29 15:31:01 +01:00
xwm_handle_client_message(xwm, (xcb_client_message_event_t *)event);
break;
2017-10-26 17:54:39 +02:00
case XCB_FOCUS_IN:
2017-10-29 15:31:01 +01:00
xwm_handle_focus_in(xwm, (xcb_focus_in_event_t *)event);
2017-10-26 17:54:39 +02:00
break;
default:
wlr_log(L_DEBUG, "X11 event: %d",
2017-08-20 19:50:28 +02:00
event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK);
break;
}
2017-08-27 22:30:37 +02:00
free(event);
}
2017-10-26 00:51:15 +02:00
if (count) {
xcb_flush(xwm->xcb_conn);
}
return count;
}
2017-10-25 01:18:31 +02:00
static void handle_compositor_surface_create(struct wl_listener *listener,
void *data) {
struct wlr_surface *surface = data;
2017-10-24 16:04:33 +02:00
struct wlr_xwm *xwm =
wl_container_of(listener, xwm, compositor_surface_create);
if (wl_resource_get_client(surface->resource) != xwm->xwayland->client) {
return;
}
wlr_log(L_DEBUG, "New xwayland surface: %p", surface);
uint32_t surface_id = wl_resource_get_id(surface->resource);
2017-10-25 03:07:46 +02:00
struct wlr_xwayland_surface *xsurface;
wl_list_for_each(xsurface, &xwm->unpaired_surfaces, unpaired_link) {
if (xsurface->surface_id == surface_id) {
2017-10-29 15:31:01 +01:00
xwm_map_shell_surface(xwm, xsurface, surface);
2017-10-25 03:07:46 +02:00
xsurface->surface_id = 0;
wl_list_remove(&xsurface->unpaired_link);
xcb_flush(xwm->xcb_conn);
return;
}
}
}
2017-10-24 16:04:33 +02:00
void wlr_xwayland_surface_activate(struct wlr_xwayland *wlr_xwayland,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface, bool activated) {
struct wlr_xwayland_surface *focused = wlr_xwayland->xwm->focus_surface;
if (activated) {
2017-10-29 15:31:01 +01:00
xwm_surface_activate(wlr_xwayland->xwm, xsurface);
} else if (focused == xsurface) {
xwm_surface_activate(wlr_xwayland->xwm, NULL);
2017-10-24 16:04:33 +02:00
}
2017-09-28 15:11:16 +02:00
}
void wlr_xwayland_surface_configure(struct wlr_xwayland *wlr_xwayland,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface, int16_t x, int16_t y,
uint16_t width, uint16_t height) {
2017-10-29 15:31:01 +01:00
xsurface->x = x;
xsurface->y = y;
xsurface->width = width;
xsurface->height = height;
struct wlr_xwm *xwm = wlr_xwayland->xwm;
uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_BORDER_WIDTH;
uint32_t values[] = {x, y, width, height, 0};
2017-10-29 15:31:01 +01:00
xcb_configure_window(xwm->xcb_conn, xsurface->window_id, mask, values);
xcb_flush(xwm->xcb_conn);
}
void wlr_xwayland_surface_close(struct wlr_xwayland *wlr_xwayland,
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface) {
struct wlr_xwm *xwm = wlr_xwayland->xwm;
bool supports_delete = false;
2017-10-29 15:31:01 +01:00
for (size_t i = 0; i < xsurface->protocols_len; i++) {
if (xsurface->protocols[i] == xwm->atoms[WM_DELETE_WINDOW]) {
supports_delete = true;
break;
}
}
if (supports_delete) {
xcb_client_message_event_t ev = {0};
ev.response_type = XCB_CLIENT_MESSAGE;
2017-10-29 15:31:01 +01:00
ev.window = xsurface->window_id;
ev.format = 32;
ev.sequence = 0;
ev.type = xwm->atoms[WM_PROTOCOLS];
ev.data.data32[0] = xwm->atoms[WM_DELETE_WINDOW];
ev.data.data32[1] = XCB_CURRENT_TIME;
2017-10-29 15:36:16 +01:00
xcb_send_event(xwm->xcb_conn, 0,
xsurface->window_id,
XCB_EVENT_MASK_NO_EVENT,
(char *)&ev);
} else {
2017-10-29 15:36:16 +01:00
xcb_kill_client(xwm->xcb_conn, xsurface->window_id);
}
xcb_flush(xwm->xcb_conn);
}
void xwm_destroy(struct wlr_xwm *xwm) {
2017-08-23 23:19:15 +02:00
if (!xwm) {
return;
}
2017-11-02 11:09:14 +01:00
if (xwm->cursor) {
xcb_free_cursor(xwm->xcb_conn, xwm->cursor);
}
if (xwm->event_source) {
wl_event_source_remove(xwm->event_source);
}
2017-10-29 15:31:01 +01:00
struct wlr_xwayland_surface *xsurface, *tmp;
wl_list_for_each_safe(xsurface, tmp, &xwm->surfaces, link) {
wlr_xwayland_surface_destroy(xsurface);
2017-08-23 23:39:16 +02:00
}
wl_list_remove(&xwm->compositor_surface_create.link);
xcb_disconnect(xwm->xcb_conn);
free(xwm);
}
2017-10-24 14:49:10 +02:00
static void xwm_get_resources(struct wlr_xwm *xwm) {
2017-10-26 16:39:36 +02:00
xcb_prefetch_extension_data(xwm->xcb_conn, &xcb_xfixes_id);
xcb_prefetch_extension_data(xwm->xcb_conn, &xcb_composite_id);
2017-10-24 14:49:10 +02:00
size_t i;
xcb_intern_atom_cookie_t cookies[ATOM_LAST];
for (i = 0; i < ATOM_LAST; i++) {
2017-10-25 01:18:31 +02:00
cookies[i] =
xcb_intern_atom(xwm->xcb_conn, 0, strlen(atom_map[i]), atom_map[i]);
2017-10-24 14:49:10 +02:00
}
for (i = 0; i < ATOM_LAST; i++) {
xcb_intern_atom_reply_t *reply;
xcb_generic_error_t *error;
reply = xcb_intern_atom_reply(xwm->xcb_conn, cookies[i], &error);
if (reply && !error) {
xwm->atoms[i] = reply->atom;
}
2017-10-24 15:21:11 +02:00
free(reply);
2017-10-24 14:49:10 +02:00
if (error) {
wlr_log(L_ERROR, "could not resolve atom %s, x11 error code %d",
atom_map[i], error->error_code);
free(error);
return;
}
}
2017-10-26 16:39:36 +02:00
xwm->xfixes = xcb_get_extension_data(xwm->xcb_conn, &xcb_xfixes_id);
if (!xwm->xfixes || !xwm->xfixes->present) {
wlr_log(L_DEBUG, "xfixes not available");
}
xcb_xfixes_query_version_cookie_t xfixes_cookie;
xcb_xfixes_query_version_reply_t *xfixes_reply;
xfixes_cookie =
xcb_xfixes_query_version(xwm->xcb_conn, XCB_XFIXES_MAJOR_VERSION,
XCB_XFIXES_MINOR_VERSION);
xfixes_reply =
xcb_xfixes_query_version_reply(xwm->xcb_conn, xfixes_cookie, NULL);
wlr_log(L_DEBUG, "xfixes version: %d.%d",
xfixes_reply->major_version, xfixes_reply->minor_version);
free(xfixes_reply);
2017-10-24 14:49:10 +02:00
}
2017-10-26 16:39:36 +02:00
static void xwm_create_wm_window(struct wlr_xwm *xwm) {
static const char name[] = "wlroots wm";
xwm->window = xcb_generate_id(xwm->xcb_conn);
xcb_create_window(xwm->xcb_conn,
XCB_COPY_FROM_PARENT,
xwm->window,
xwm->screen->root,
0, 0,
10, 10,
0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
xwm->screen->root_visual,
0, NULL);
xcb_change_property(xwm->xcb_conn,
XCB_PROP_MODE_REPLACE,
xwm->window,
xwm->atoms[_NET_WM_NAME],
xwm->atoms[UTF8_STRING],
8, // format
strlen(name), name);
xcb_change_property(xwm->xcb_conn,
XCB_PROP_MODE_REPLACE,
xwm->screen->root,
xwm->atoms[_NET_SUPPORTING_WM_CHECK],
XCB_ATOM_WINDOW,
32, // format
1, &xwm->window);
2017-10-27 16:48:09 +02:00
xcb_change_property(xwm->xcb_conn,
XCB_PROP_MODE_REPLACE,
xwm->window,
xwm->atoms[_NET_SUPPORTING_WM_CHECK],
XCB_ATOM_WINDOW,
32, // format
1, &xwm->window);
2017-10-26 16:39:36 +02:00
xcb_set_selection_owner(xwm->xcb_conn,
xwm->window,
xwm->atoms[WM_S0],
XCB_CURRENT_TIME);
xcb_set_selection_owner(xwm->xcb_conn,
xwm->window,
xwm->atoms[NET_WM_S0],
XCB_CURRENT_TIME);
}
2017-10-24 14:49:10 +02:00
// TODO use me to support 32 bit color somehow
static void xwm_get_visual_and_colormap(struct wlr_xwm *xwm) {
xcb_depth_iterator_t d_iter;
xcb_visualtype_iterator_t vt_iter;
xcb_visualtype_t *visualtype;
d_iter = xcb_screen_allowed_depths_iterator(xwm->screen);
visualtype = NULL;
while (d_iter.rem > 0) {
if (d_iter.data->depth == 32) {
vt_iter = xcb_depth_visuals_iterator(d_iter.data);
visualtype = vt_iter.data;
break;
}
xcb_depth_next(&d_iter);
}
if (visualtype == NULL) {
wlr_log(L_DEBUG, "No 32 bit visualtype\n");
return;
}
xwm->visual_id = visualtype->visual_id;
xwm->colormap = xcb_generate_id(xwm->xcb_conn);
2017-10-29 15:36:16 +01:00
xcb_create_colormap(xwm->xcb_conn,
XCB_COLORMAP_ALLOC_NONE,
xwm->colormap,
xwm->screen->root,
xwm->visual_id);
}
static void xwm_get_render_format(struct wlr_xwm *xwm) {
2017-11-02 15:52:02 +01:00
xcb_render_query_pict_formats_cookie_t cookie =
xcb_render_query_pict_formats(xwm->xcb_conn);
xcb_render_query_pict_formats_reply_t *reply =
xcb_render_query_pict_formats_reply(xwm->xcb_conn, cookie, NULL);
xcb_render_pictforminfo_iterator_t iter =
xcb_render_query_pict_formats_formats_iterator(reply);
2017-11-02 15:52:02 +01:00
xcb_render_pictforminfo_t *format = NULL;
while (iter.rem > 0) {
if (iter.data->depth == 32) {
format = iter.data;
2017-11-02 15:52:02 +01:00
break;
}
xcb_render_pictforminfo_next(&iter);
2017-11-02 15:52:02 +01:00
}
2017-11-02 15:52:02 +01:00
if (format == NULL) {
wlr_log(L_DEBUG, "No 32 bit render format");
return;
}
xwm->render_format_id = format->id;
}
void xwm_set_cursor(struct wlr_xwm *xwm, const uint8_t *pixels, uint32_t stride,
uint32_t width, uint32_t height, int32_t hotspot_x, int32_t hotspot_y) {
if (!xwm->render_format_id) {
wlr_log(L_ERROR, "Cannot set xwm cursor: no render format available");
2017-11-02 15:52:02 +01:00
return;
}
if (xwm->cursor) {
xcb_free_cursor(xwm->xcb_conn, xwm->cursor);
}
stride *= 4;
int depth = 32;
xcb_pixmap_t pix = xcb_generate_id(xwm->xcb_conn);
xcb_create_pixmap(xwm->xcb_conn, depth, pix, xwm->screen->root, width,
height);
2017-11-02 15:52:02 +01:00
xcb_render_picture_t pic = xcb_generate_id(xwm->xcb_conn);
xcb_render_create_picture(xwm->xcb_conn, pic, pix, xwm->render_format_id,
0, 0);
2017-11-02 15:52:02 +01:00
xcb_gcontext_t gc = xcb_generate_id(xwm->xcb_conn);
xcb_create_gc(xwm->xcb_conn, gc, pix, 0, NULL);
xcb_put_image(xwm->xcb_conn, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc,
width, height, 0, 0, 0, depth, stride * height * sizeof(uint8_t),
pixels);
xcb_free_gc(xwm->xcb_conn, gc);
xwm->cursor = xcb_generate_id(xwm->xcb_conn);
xcb_render_create_cursor(xwm->xcb_conn, xwm->cursor, pic, hotspot_x,
hotspot_y);
xcb_free_pixmap(xwm->xcb_conn, pix);
uint32_t values[] = {xwm->cursor};
xcb_change_window_attributes(xwm->xcb_conn, xwm->screen->root,
XCB_CW_CURSOR, values);
2017-11-02 16:49:22 +01:00
xcb_flush(xwm->xcb_conn);
2017-11-02 15:52:02 +01:00
}
struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) {
struct wlr_xwm *xwm = calloc(1, sizeof(struct wlr_xwm));
if (xwm == NULL) {
return NULL;
}
xwm->xwayland = wlr_xwayland;
wl_list_init(&xwm->surfaces);
wl_list_init(&xwm->unpaired_surfaces);
xwm->xcb_conn = xcb_connect_to_fd(wlr_xwayland->wm_fd[0], NULL);
int rc = xcb_connection_has_error(xwm->xcb_conn);
if (rc) {
wlr_log(L_ERROR, "xcb connect failed: %d", rc);
2017-10-26 16:39:36 +02:00
close(wlr_xwayland->wm_fd[0]);
free(xwm);
return NULL;
}
2017-10-26 16:39:36 +02:00
xcb_screen_iterator_t screen_iterator =
xcb_setup_roots_iterator(xcb_get_setup(xwm->xcb_conn));
xwm->screen = screen_iterator.data;
struct wl_event_loop *event_loop = wl_display_get_event_loop(
wlr_xwayland->wl_display);
2017-10-26 16:39:36 +02:00
xwm->event_source =
wl_event_loop_add_fd(event_loop,
wlr_xwayland->wm_fd[0],
WL_EVENT_READABLE,
x11_event_handler,
xwm);
2017-10-24 14:49:10 +02:00
wl_event_source_check(xwm->event_source);
xwm_get_resources(xwm);
xwm_get_visual_and_colormap(xwm);
xwm_get_render_format(xwm);
2017-10-24 14:49:10 +02:00
2017-11-02 00:03:55 +01:00
uint32_t values[] = {
2017-10-26 16:39:36 +02:00
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
2017-11-02 00:03:55 +01:00
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
XCB_EVENT_MASK_PROPERTY_CHANGE,
};
2017-10-26 16:39:36 +02:00
xcb_change_window_attributes(xwm->xcb_conn,
xwm->screen->root,
2017-11-02 15:52:02 +01:00
XCB_CW_EVENT_MASK,
2017-10-26 16:39:36 +02:00
values);
2017-10-29 15:36:16 +01:00
xcb_composite_redirect_subwindows(xwm->xcb_conn,
xwm->screen->root,
2017-10-26 16:39:36 +02:00
XCB_COMPOSITE_REDIRECT_MANUAL);
2017-10-24 14:49:10 +02:00
xcb_atom_t supported[] = {
xwm->atoms[NET_WM_STATE],
xwm->atoms[_NET_ACTIVE_WINDOW],
2017-10-26 16:39:36 +02:00
xwm->atoms[_NET_WM_MOVERESIZE],
2017-10-27 19:05:14 +02:00
xwm->atoms[_NET_WM_STATE_FULLSCREEN],
xwm->atoms[_NET_WM_STATE_MAXIMIZED_VERT],
2017-11-09 11:21:55 +01:00
xwm->atoms[_NET_WM_STATE_MAXIMIZED_HORZ],
2017-10-24 14:49:10 +02:00
};
2017-10-26 16:39:36 +02:00
xcb_change_property(xwm->xcb_conn,
XCB_PROP_MODE_REPLACE,
xwm->screen->root,
xwm->atoms[NET_SUPPORTED],
XCB_ATOM_ATOM,
32,
sizeof(supported)/sizeof(*supported),
supported);
2017-10-24 14:49:10 +02:00
xcb_flush(xwm->xcb_conn);
2017-10-26 16:39:36 +02:00
xwm_set_net_active_window(xwm, XCB_WINDOW_NONE);
2017-10-15 20:50:21 +02:00
xwm->compositor_surface_create.notify = handle_compositor_surface_create;
2017-08-20 19:50:28 +02:00
wl_signal_add(&wlr_xwayland->compositor->events.create_surface,
&xwm->compositor_surface_create);
2017-10-26 16:39:36 +02:00
xwm_create_wm_window(xwm);
xcb_flush(xwm->xcb_conn);
return xwm;
}
2017-10-27 19:05:14 +02:00
void wlr_xwayland_surface_set_maximized(struct wlr_xwayland *wlr_xwayland,
struct wlr_xwayland_surface *surface, bool maximized) {
if (xsurface_is_maximized(surface) != maximized) {
surface->maximized_horz = maximized;
surface->maximized_vert = maximized;
xsurface_set_net_wm_state(surface);
}
}
void wlr_xwayland_surface_set_fullscreen(struct wlr_xwayland *wlr_xwayland,
struct wlr_xwayland_surface *surface, bool fullscreen) {
if (surface->fullscreen != fullscreen) {
surface->fullscreen = fullscreen;
xsurface_set_net_wm_state(surface);
}
}