wlroots-hyprland/xwayland/xwayland.c

181 lines
4.8 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
2018-02-12 21:29:23 +01:00
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
2018-02-12 21:29:23 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <sys/socket.h>
2017-08-23 23:19:15 +02:00
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
2018-02-12 21:29:23 +01:00
#include <unistd.h>
#include <wlr/types/wlr_seat.h>
2018-02-12 09:12:31 +01:00
#include <wlr/util/log.h>
#include <wlr/xwayland/shell.h>
#include <wlr/xwayland/xwayland.h>
#include "sockets.h"
#include "xwayland/xwm.h"
2017-11-02 16:49:22 +01:00
struct wlr_xwayland_cursor {
uint8_t *pixels;
uint32_t stride;
uint32_t width;
uint32_t height;
int32_t hotspot_x;
int32_t hotspot_y;
};
static void handle_server_destroy(struct wl_listener *listener, void *data) {
struct wlr_xwayland *xwayland =
wl_container_of(listener, xwayland, server_destroy);
// Server is being destroyed so avoid destroying it once again.
xwayland->server = NULL;
wlr_xwayland_destroy(xwayland);
}
static void handle_server_start(struct wl_listener *listener, void *data) {
struct wlr_xwayland *xwayland =
wl_container_of(listener, xwayland, server_start);
wlr_xwayland_shell_v1_set_client(xwayland->shell_v1, xwayland->server->client);
}
static void handle_server_ready(struct wl_listener *listener, void *data) {
struct wlr_xwayland *xwayland =
wl_container_of(listener, xwayland, server_ready);
struct wlr_xwayland_server_ready_event *event = data;
xwayland->xwm = xwm_create(xwayland, event->wm_fd);
if (!xwayland->xwm) {
return;
}
if (xwayland->seat) {
xwm_set_seat(xwayland->xwm, xwayland->seat);
2017-11-22 14:23:23 +01:00
}
if (xwayland->cursor != NULL) {
struct wlr_xwayland_cursor *cur = xwayland->cursor;
xwm_set_cursor(xwayland->xwm, cur->pixels, cur->stride, cur->width,
2017-11-02 16:49:22 +01:00
cur->height, cur->hotspot_x, cur->hotspot_y);
}
2022-08-18 13:16:16 +02:00
wl_signal_emit_mutable(&xwayland->events.ready, NULL);
}
void wlr_xwayland_destroy(struct wlr_xwayland *xwayland) {
if (!xwayland) {
2018-09-08 13:00:56 +02:00
return;
}
wl_list_remove(&xwayland->server_destroy.link);
wl_list_remove(&xwayland->server_start.link);
wl_list_remove(&xwayland->server_ready.link);
free(xwayland->cursor);
wlr_xwayland_set_seat(xwayland, NULL);
wlr_xwayland_server_destroy(xwayland->server);
xwayland->server = NULL;
wlr_xwayland_shell_v1_destroy(xwayland->shell_v1);
free(xwayland);
}
struct wlr_xwayland *wlr_xwayland_create(struct wl_display *wl_display,
struct wlr_compositor *compositor, bool lazy) {
struct wlr_xwayland *xwayland = calloc(1, sizeof(struct wlr_xwayland));
if (!xwayland) {
return NULL;
}
xwayland->wl_display = wl_display;
xwayland->compositor = compositor;
wl_signal_init(&xwayland->events.new_surface);
wl_signal_init(&xwayland->events.ready);
wl_signal_init(&xwayland->events.remove_startup_info);
xwayland->shell_v1 = wlr_xwayland_shell_v1_create(wl_display, 1);
if (xwayland->shell_v1 == NULL) {
free(xwayland);
return NULL;
}
2020-05-19 17:28:23 +02:00
struct wlr_xwayland_server_options options = {
.lazy = lazy,
.enable_wm = true,
#if HAVE_XCB_XFIXES_SET_CLIENT_DISCONNECT_MODE
.terminate_delay = lazy ? 10 : 0,
#endif
2020-05-19 17:28:23 +02:00
};
xwayland->server = wlr_xwayland_server_create(wl_display, &options);
if (xwayland->server == NULL) {
wlr_xwayland_shell_v1_destroy(xwayland->shell_v1);
2020-06-04 16:23:51 +02:00
free(xwayland);
return NULL;
}
xwayland->display_name = xwayland->server->display_name;
xwayland->server_destroy.notify = handle_server_destroy;
wl_signal_add(&xwayland->server->events.destroy, &xwayland->server_destroy);
xwayland->server_start.notify = handle_server_start;
wl_signal_add(&xwayland->server->events.start, &xwayland->server_start);
xwayland->server_ready.notify = handle_server_ready;
wl_signal_add(&xwayland->server->events.ready, &xwayland->server_ready);
return xwayland;
}
2017-11-02 16:49:22 +01:00
void wlr_xwayland_set_cursor(struct wlr_xwayland *xwayland,
2017-11-02 16:49:22 +01:00
uint8_t *pixels, uint32_t stride, uint32_t width, uint32_t height,
int32_t hotspot_x, int32_t hotspot_y) {
if (xwayland->xwm != NULL) {
xwm_set_cursor(xwayland->xwm, pixels, stride, width, height,
2017-11-02 16:49:22 +01:00
hotspot_x, hotspot_y);
return;
}
free(xwayland->cursor);
2017-11-02 16:49:22 +01:00
xwayland->cursor = calloc(1, sizeof(struct wlr_xwayland_cursor));
if (xwayland->cursor == NULL) {
2017-11-02 16:49:22 +01:00
return;
}
xwayland->cursor->pixels = pixels;
xwayland->cursor->stride = stride;
xwayland->cursor->width = width;
xwayland->cursor->height = height;
xwayland->cursor->hotspot_x = hotspot_x;
xwayland->cursor->hotspot_y = hotspot_y;
2017-11-02 16:49:22 +01:00
}
2017-11-22 14:10:06 +01:00
static void xwayland_handle_seat_destroy(struct wl_listener *listener,
void *data) {
struct wlr_xwayland *xwayland =
wl_container_of(listener, xwayland, seat_destroy);
wlr_xwayland_set_seat(xwayland, NULL);
}
2017-11-22 14:10:06 +01:00
void wlr_xwayland_set_seat(struct wlr_xwayland *xwayland,
struct wlr_seat *seat) {
if (xwayland->seat) {
wl_list_remove(&xwayland->seat_destroy.link);
}
2017-11-22 14:23:23 +01:00
xwayland->seat = seat;
if (xwayland->xwm) {
xwm_set_seat(xwayland->xwm, seat);
}
if (seat == NULL) {
return;
}
xwayland->seat_destroy.notify = xwayland_handle_seat_destroy;
wl_signal_add(&seat->events.destroy, &xwayland->seat_destroy);
2017-11-22 14:10:06 +01:00
}