From 49a823d4c6b41723a9978c19d426e8ca4f5418d7 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 17:59:31 +0200 Subject: [PATCH 01/17] Xwayland: first draft, just start server for now --- include/wlr/xwayland.h | 17 ++++ include/xwayland/internals.h | 6 ++ meson.build | 2 + xwayland/meson.build | 5 ++ xwayland/sockets.c | 144 +++++++++++++++++++++++++++++ xwayland/xwayland.c | 170 +++++++++++++++++++++++++++++++++++ 6 files changed, 344 insertions(+) create mode 100644 include/wlr/xwayland.h create mode 100644 include/xwayland/internals.h create mode 100644 xwayland/meson.build create mode 100644 xwayland/sockets.c create mode 100644 xwayland/xwayland.c diff --git a/include/wlr/xwayland.h b/include/wlr/xwayland.h new file mode 100644 index 00000000..ef9cc887 --- /dev/null +++ b/include/wlr/xwayland.h @@ -0,0 +1,17 @@ +#ifndef _WLR_XWAYLAND_H +#define _WLR_XWAYLAND_H + +struct wlr_xwayland { + pid_t pid; + int display; + int x_fd[2], wl_fd[2], wm_fd[2]; + struct wl_client *client; + struct wl_display *wl_display; + time_t server_start; +}; + +void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland); +bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, + struct wl_display *wl_display); + +#endif diff --git a/include/xwayland/internals.h b/include/xwayland/internals.h new file mode 100644 index 00000000..b91f7930 --- /dev/null +++ b/include/xwayland/internals.h @@ -0,0 +1,6 @@ +#ifndef XWAYLAND_INTERNALS_H +#define XWAYLAND_INTERNALS_H + +void unlink_sockets(int display); +int open_display_sockets(int socks[2]); +#endif diff --git a/meson.build b/meson.build index 4c449f53..b3706dbc 100644 --- a/meson.build +++ b/meson.build @@ -50,6 +50,7 @@ subdir('render') subdir('types') subdir('util') subdir('xcursor') +subdir('xwayland') wlr_deps = [ wayland_server, @@ -77,6 +78,7 @@ lib_wlr = library('wlroots', files('dummy.c'), lib_wlr_types, lib_wlr_util, lib_wlr_xcursor, + lib_wlr_xwayland, ], dependencies: wlr_deps, include_directories: wlr_inc) diff --git a/xwayland/meson.build b/xwayland/meson.build new file mode 100644 index 00000000..d43ace9b --- /dev/null +++ b/xwayland/meson.build @@ -0,0 +1,5 @@ +lib_wlr_xwayland = static_library('wlr_xwayland', files( + 'sockets.c', + 'xwayland.c', + ), + include_directories: wlr_inc) diff --git a/xwayland/sockets.c b/xwayland/sockets.c new file mode 100644 index 00000000..868d6ada --- /dev/null +++ b/xwayland/sockets.c @@ -0,0 +1,144 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "wlr/util/log.h" +#include "xwayland/internals.h" + +static const char *lock_fmt = "/tmp/.X%d-lock"; +static const char *socket_dir = "/tmp/.X11-unix"; +static const char *socket_fmt = "/tmp/.X11-unix/X%d"; + +static int open_socket(struct sockaddr_un *addr, size_t path_size) { + int fd, rc; + socklen_t size = offsetof(struct sockaddr_un, sun_path) + path_size + 1; + + fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd < 0) { + wlr_log_errno(L_ERROR, "Failed to create socket %s", addr->sun_path); + return -1; + } + + if (addr->sun_path[0]) { + unlink(addr->sun_path); + } + if (bind(fd, (struct sockaddr*)addr, size) < 0) { + rc = errno; + wlr_log_errno(L_ERROR, "Failed to bind socket %s", addr->sun_path); + goto cleanup; + } + if (listen(fd, 1) < 0) { + rc = errno; + wlr_log_errno(L_ERROR, "Failed to listen to socket %s", addr->sun_path); + goto cleanup; + } + + return fd; + +cleanup: + close(fd); + if (addr->sun_path[0]) { + unlink(addr->sun_path); + } + errno = rc; + return -1; +} + +static bool open_sockets(int socks[2], int display) { + struct sockaddr_un addr = { .sun_family = AF_LOCAL }; + size_t path_size; + + mkdir(socket_dir, 0777); + + // TODO: non-linux apparently want another format + addr.sun_path[0] = 0; + path_size = snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1, socket_fmt, display); + socks[0] = open_socket(&addr, path_size); + if (socks[0] < 0) { + return false; + } + + path_size = snprintf(addr.sun_path, sizeof(addr.sun_path), socket_fmt, display); + socks[1] = open_socket(&addr, path_size); + if (socks[1] < 0) { + close(socks[0]); + socks[0] = -1; + return false; + } + + return true; +} + +void unlink_sockets(int display) { + char sun_path[64]; + + snprintf(sun_path, sizeof(sun_path), socket_fmt, display); + unlink(sun_path); +} + +int open_display_sockets(int socks[2]) { + int lock_fd, display; + char lock_name[64]; + + for (display = 0; display <= 32; display++) { + snprintf(lock_name, sizeof(lock_name), lock_fmt, display); + if ((lock_fd = open(lock_name, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0444)) >= 0) { + if (!open_sockets(socks, display)) { + unlink(lock_name); + close(lock_fd); + continue; + } + char pid[12]; + snprintf(pid, sizeof(pid), "%10d", getpid()); + if (write(lock_fd, pid, sizeof(pid) - 1) != sizeof(pid) - 1) { + unlink(lock_name); + close(lock_fd); + continue; + } + close(lock_fd); + break; + } + + if ((lock_fd = open(lock_name, O_RDONLY | O_CLOEXEC)) < 0) { + continue; + } + + char pid[12] = { 0 }, *end_pid; + ssize_t bytes = read(lock_fd, pid, sizeof(pid) - 1); + close(lock_fd); + + if (bytes != sizeof(pid) - 1) { + continue; + } + long int read_pid; + read_pid = strtol(pid, &end_pid, 10); + if (read_pid < 0 || read_pid > INT32_MAX || end_pid != pid + sizeof(pid)) { + continue; + } + errno = 0; + if (kill((pid_t)read_pid, 0) != 0 && errno == ESRCH) { + if (unlink(lock_name) != 0) { + continue; + } + // retry + display--; + continue; + } + } + + if (display > 32) { + wlr_log(L_ERROR, "No display available in the first 33"); + return -1; + } + + return display; +} diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c new file mode 100644 index 00000000..49173ace --- /dev/null +++ b/xwayland/xwayland.c @@ -0,0 +1,170 @@ +#define _XOPEN_SOURCE 700 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "wlr/util/log.h" +#include "wlr/xwayland.h" +#include "xwayland/internals.h" + +static void safe_close(int fd) { + if (fd >= 0) { + close(fd); + } +} + +static int unset_cloexec(int fd) { + if (fcntl(fd, F_SETFD, 0) != 0) { + wlr_log_errno(L_ERROR, "fcntl() failed on fd %d", fd); + return -1; + } + return 0; +} + +static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { + if (unset_cloexec(wlr_xwayland->x_fd[0]) || + unset_cloexec(wlr_xwayland->x_fd[1]) || + unset_cloexec(wlr_xwayland->wm_fd[1]) || + unset_cloexec(wlr_xwayland->wl_fd[1])) { + exit(EXIT_FAILURE); + } + + char *argv[11] = { 0 }; + argv[0] = "Xwayland"; + if (asprintf(&argv[1], ":%d", wlr_xwayland->display) < 0) { + wlr_log_errno(L_ERROR, "asprintf failed"); + exit(EXIT_FAILURE); + } + argv[2] = "-rootless"; + argv[3] = "-terminate"; + argv[4] = "-listen"; + if (asprintf(&argv[5], "%d", wlr_xwayland->x_fd[0]) < 0) { + wlr_log_errno(L_ERROR, "asprintf failed"); + exit(EXIT_FAILURE); + } + argv[6] = "-listen"; + if (asprintf(&argv[7], "%d", wlr_xwayland->x_fd[1]) < 0) { + wlr_log_errno(L_ERROR, "asprintf failed"); + exit(EXIT_FAILURE); + } + argv[8] = "-wm"; + if (asprintf(&argv[9], "%d", wlr_xwayland->wm_fd[1]) < 0) { + wlr_log_errno(L_ERROR, "asprintf failed"); + exit(EXIT_FAILURE); + } + + const char *xdg_runtime = getenv("XDG_RUNTIME_DIR"); + if (!xdg_runtime) { + wlr_log(L_ERROR, "XDG_RUNTIME_DIR is not set"); + exit(EXIT_FAILURE); + } + + char *envp[3] = { 0 }; + if (asprintf(&envp[0], "XDG_RUNTIME_DIR=%s", xdg_runtime) < 0 || + asprintf(&envp[1], "WAYLAND_SOCKET=%d", wlr_xwayland->wl_fd[1]) < 0) { + wlr_log_errno(L_ERROR, "asprintf failed"); + exit(EXIT_FAILURE); + } + + wlr_log(L_INFO, "Xwayland :%d -rootless -terminate -listen %d -listen %d -wm %d", + wlr_xwayland->display, wlr_xwayland->x_fd[0], wlr_xwayland->x_fd[1], + wlr_xwayland->wm_fd[1]); + + execvpe("Xwayland", argv, envp); +} + +static void xwayland_destroy_event(struct wl_listener *listener, void *data) { + struct wl_client *client = data; + struct wlr_xwayland *wlr_xwayland = wl_container_of(client, wlr_xwayland, client); + + /* don't call client destroy */ + wlr_xwayland->client = NULL; + wlr_xwayland_finish(wlr_xwayland); + + if (wlr_xwayland->server_start - time(NULL) > 5) { + wlr_xwayland_init(wlr_xwayland, wlr_xwayland->wl_display); + } +} + +static struct wl_listener xwayland_destroy_listener = { + .notify = xwayland_destroy_event, +}; + +void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { + + if (wlr_xwayland->client) { + wl_list_remove(&xwayland_destroy_listener.link); + wl_client_destroy(wlr_xwayland->client); + } + + safe_close(wlr_xwayland->x_fd[0]); + safe_close(wlr_xwayland->x_fd[1]); + safe_close(wlr_xwayland->wl_fd[0]); + safe_close(wlr_xwayland->wl_fd[1]); + safe_close(wlr_xwayland->wm_fd[0]); + safe_close(wlr_xwayland->wm_fd[1]); + + unlink_sockets(wlr_xwayland->display); + unsetenv("DISPLAY"); + /* kill Xwayland process? */ +} + +bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, + struct wl_display *wl_display) { + wlr_xwayland->wl_display = wl_display; + wlr_xwayland->x_fd[0] = wlr_xwayland->x_fd[1] = -1; + wlr_xwayland->wl_fd[0] = wlr_xwayland->wl_fd[1] = -1; + wlr_xwayland->wm_fd[0] = wlr_xwayland->wm_fd[1] = -1; + + wlr_xwayland->display = open_display_sockets(wlr_xwayland->x_fd); + if (wlr_xwayland->display < 0) { + wlr_xwayland_finish(wlr_xwayland); + return false; + } + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wlr_xwayland->wl_fd) != 0 || + socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wlr_xwayland->wm_fd) != 0) { + wlr_log_errno(L_ERROR, "failed to create socketpair"); + wlr_xwayland_finish(wlr_xwayland); + return false; + } + + if ((wlr_xwayland->pid = fork()) == 0) { + exec_xwayland(wlr_xwayland); + wlr_log_errno(L_ERROR, "execvpe failed"); + exit(EXIT_FAILURE); + } + + if (wlr_xwayland->pid < 0) { + wlr_log_errno(L_ERROR, "fork failed"); + wlr_xwayland_finish(wlr_xwayland); + return false; + } + + /* close child fds */ + close(wlr_xwayland->x_fd[0]); + close(wlr_xwayland->x_fd[1]); + close(wlr_xwayland->wl_fd[1]); + close(wlr_xwayland->wm_fd[1]); + wlr_xwayland->x_fd[0] = wlr_xwayland->x_fd[1] = -1; + wlr_xwayland->wl_fd[1] = wlr_xwayland->wm_fd[1] = -1; + + char display_name[16]; + snprintf(display_name, sizeof(display_name), ":%d", wlr_xwayland->display); + setenv("DISPLAY", display_name, true); + wlr_xwayland->server_start = time(NULL); + + if (!(wlr_xwayland->client = wl_client_create(wl_display, wlr_xwayland->wl_fd[0]))) { + wlr_log_errno(L_ERROR, "wl_client_create failed"); + wlr_xwayland_finish(wlr_xwayland); + } + + wl_client_add_destroy_listener(wlr_xwayland->client, &xwayland_destroy_listener); + + return true; +} From 60451521bd6bd7d03255a206e35b0f88d5328311 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 18:06:58 +0200 Subject: [PATCH 02/17] start xwayland in example compositor --- examples/compositor.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/compositor.c b/examples/compositor.c index fb00032a..be9fc5ca 100644 --- a/examples/compositor.c +++ b/examples/compositor.c @@ -19,6 +19,7 @@ #include #include #include "wlr/types/wlr_compositor.h" +#include #include #include #include "shared.h" @@ -35,6 +36,7 @@ struct sample_state { struct wlr_data_device_manager *data_device_manager; struct wl_resource *focus; struct wl_listener keyboard_bound; + struct wlr_xwayland wlr_xwayland; int keymap_fd; size_t keymap_size; uint32_t serial; @@ -176,11 +178,13 @@ int main() { free(keymap); break; } + wlr_xwayland_init(&state.wlr_xwayland, compositor.display); compositor.keyboard_key_cb = handle_keyboard_key; wl_display_run(compositor.display); + wlr_xwayland_finish(&state.wlr_xwayland); close(state.keymap_fd); wlr_seat_destroy(state.wl_seat); wlr_data_device_manager_destroy(state.data_device_manager); From b2bab1af5c34b785a9b08db04af1330e61dd210c Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 21:25:26 +0200 Subject: [PATCH 03/17] xwayland: split xwm structure out, initial xwm.c --- examples/compositor.c | 2 +- include/wlr/xwayland.h | 11 +++++- include/xwayland/internals.h | 16 +++++++++ meson.build | 2 ++ xwayland/meson.build | 4 ++- xwayland/xwayland.c | 18 ++++++++-- xwayland/xwm.c | 69 ++++++++++++++++++++++++++++++++++++ 7 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 xwayland/xwm.c diff --git a/examples/compositor.c b/examples/compositor.c index be9fc5ca..e143099c 100644 --- a/examples/compositor.c +++ b/examples/compositor.c @@ -178,7 +178,7 @@ int main() { free(keymap); break; } - wlr_xwayland_init(&state.wlr_xwayland, compositor.display); + wlr_xwayland_init(&state.wlr_xwayland, compositor.display, state.wlr_compositor); compositor.keyboard_key_cb = handle_keyboard_key; diff --git a/include/wlr/xwayland.h b/include/wlr/xwayland.h index ef9cc887..f61a90bd 100644 --- a/include/wlr/xwayland.h +++ b/include/wlr/xwayland.h @@ -1,5 +1,11 @@ #ifndef _WLR_XWAYLAND_H #define _WLR_XWAYLAND_H +#include +#include +#include +#include + +struct wlr_xwm; struct wlr_xwayland { pid_t pid; @@ -7,11 +13,14 @@ struct wlr_xwayland { int x_fd[2], wl_fd[2], wm_fd[2]; struct wl_client *client; struct wl_display *wl_display; + struct wlr_compositor *compositor; time_t server_start; + + struct wlr_xwm *xwm; }; void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland); bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, - struct wl_display *wl_display); + struct wl_display *wl_display, struct wlr_compositor *compositor); #endif diff --git a/include/xwayland/internals.h b/include/xwayland/internals.h index b91f7930..46158bd0 100644 --- a/include/xwayland/internals.h +++ b/include/xwayland/internals.h @@ -1,6 +1,22 @@ #ifndef XWAYLAND_INTERNALS_H #define XWAYLAND_INTERNALS_H +#include +#include +#include + +struct wlr_xwm { + struct wlr_xwayland *xwayland; + struct wl_event_source *event_source; + struct wl_listener surface_listener; + + xcb_connection_t *xcb_connection; + xcb_screen_t *xcb_screen; +}; void unlink_sockets(int display); int open_display_sockets(int socks[2]); + +void xwm_destroy(struct wlr_xwm *xwm); +struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland); + #endif diff --git a/meson.build b/meson.build index b3706dbc..a7baacf9 100644 --- a/meson.build +++ b/meson.build @@ -32,6 +32,7 @@ libinput = dependency('libinput') xkbcommon = dependency('xkbcommon') udev = dependency('libudev') pixman = dependency('pixman-1') +xcb = dependency('xcb') libcap = dependency('libcap', required: false) systemd = dependency('libsystemd', required: false) math = cc.find_library('m', required: false) @@ -65,6 +66,7 @@ wlr_deps = [ xkbcommon, udev, pixman, + xcb, libcap, systemd, math, diff --git a/xwayland/meson.build b/xwayland/meson.build index d43ace9b..6f75e23a 100644 --- a/xwayland/meson.build +++ b/xwayland/meson.build @@ -1,5 +1,7 @@ lib_wlr_xwayland = static_library('wlr_xwayland', files( 'sockets.c', 'xwayland.c', + 'xwm.c', ), - include_directories: wlr_inc) + include_directories: wlr_inc, + dependencies: [wayland_server, xcb, pixman]) diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index 49173ace..e9bdfcc5 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -76,6 +76,8 @@ static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { wlr_xwayland->display, wlr_xwayland->x_fd[0], wlr_xwayland->x_fd[1], wlr_xwayland->wm_fd[1]); + // TODO: close stdout/err depending on log level + execvpe("Xwayland", argv, envp); } @@ -88,7 +90,8 @@ static void xwayland_destroy_event(struct wl_listener *listener, void *data) { wlr_xwayland_finish(wlr_xwayland); if (wlr_xwayland->server_start - time(NULL) > 5) { - wlr_xwayland_init(wlr_xwayland, wlr_xwayland->wl_display); + wlr_xwayland_init(wlr_xwayland, wlr_xwayland->wl_display, + wlr_xwayland->compositor); } } @@ -103,6 +106,8 @@ void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { wl_client_destroy(wlr_xwayland->client); } + xwm_destroy(wlr_xwayland->xwm); + safe_close(wlr_xwayland->x_fd[0]); safe_close(wlr_xwayland->x_fd[1]); safe_close(wlr_xwayland->wl_fd[0]); @@ -116,8 +121,10 @@ void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { } bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, - struct wl_display *wl_display) { + struct wl_display *wl_display, struct wlr_compositor *compositor) { + memset(wlr_xwayland, 0, sizeof(struct wlr_xwayland)); wlr_xwayland->wl_display = wl_display; + wlr_xwayland->compositor = compositor; wlr_xwayland->x_fd[0] = wlr_xwayland->x_fd[1] = -1; wlr_xwayland->wl_fd[0] = wlr_xwayland->wl_fd[1] = -1; wlr_xwayland->wm_fd[0] = wlr_xwayland->wm_fd[1] = -1; @@ -162,9 +169,16 @@ bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, if (!(wlr_xwayland->client = wl_client_create(wl_display, wlr_xwayland->wl_fd[0]))) { wlr_log_errno(L_ERROR, "wl_client_create failed"); wlr_xwayland_finish(wlr_xwayland); + return false; } wl_client_add_destroy_listener(wlr_xwayland->client, &xwayland_destroy_listener); + wlr_xwayland->xwm = xwm_create(wlr_xwayland); + if (!wlr_xwayland->xwm) { + wlr_xwayland_finish(wlr_xwayland); + return false; + } + return true; } diff --git a/xwayland/xwm.c b/xwayland/xwm.c new file mode 100644 index 00000000..d39527c5 --- /dev/null +++ b/xwayland/xwm.c @@ -0,0 +1,69 @@ +#include +#include "wlr/util/log.h" +#include "wlr/types/wlr_surface.h" +#include "wlr/xwayland.h" +#include "xwayland/internals.h" + +#define SEND_EVENT_MASK 0x80 +static int x11_event_handler(int fd, uint32_t mask, void *data) { + int count = 0; + xcb_generic_event_t *event; + struct wlr_xwayland *wlr_xwayland = data; + struct wlr_xwm *xwm = wlr_xwayland->xwm; + + while ((event = xcb_poll_for_event(xwm->xcb_connection))) { + wlr_log(L_DEBUG, "X11 event: %d", event->response_type & ~SEND_EVENT_MASK); + count++; + // TODO: actually do stuff! + } + return count; +} + +static void create_surface_handler(struct wl_listener *listener, void *data) +{ + struct wlr_surface *surface = data; + struct wlr_xwm *xwm = wl_container_of(listener, xwm, surface_listener); + + if (wl_resource_get_client(surface->resource) != xwm->xwayland->client) { + return; + } + + wlr_log(L_DEBUG, "new x11 surface: %p", surface); + + // TODO: look for unpaired window, and assign +} + +void xwm_destroy(struct wlr_xwm *xwm) { + if (xwm->event_source) { + wl_event_source_remove(xwm->event_source); + } + + free(xwm); +} + +struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) { + struct wlr_xwm *xwm = calloc(1, sizeof(struct wlr_xwm)); + + xwm->xwayland = wlr_xwayland; + + xwm->xcb_connection = xcb_connect_to_fd(wlr_xwayland->wm_fd[0], NULL); + if (xcb_connection_has_error(xwm->xcb_connection)) { + return NULL; + } + + // TODO more xcb init + // xcb_prefetch_extension_data(xwm->xcb_connection, &xcb_composite_id); + + struct wl_event_loop *event_loop = wl_display_get_event_loop(wlr_xwayland->wl_display); + xwm->event_source = wl_event_loop_add_fd(event_loop, wlr_xwayland->wm_fd[0], + WL_EVENT_READABLE, x11_event_handler, wlr_xwayland); + // probably not needed + // wl_event_source_check(xwm->event_source); + + + xwm->surface_listener.notify = create_surface_handler; + //wl_signal_add(&wlr_xwayland->compositor->create_surface_signal, + // &xwm->surface_listener); + + return xwm; +} From 3cc5b1acc6014ef424630c2ac2ae5b024c2b57e8 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 22:14:51 +0200 Subject: [PATCH 04/17] wlr_compositor: add signal for create_surface --- include/wlr/types/wlr_compositor.h | 2 ++ types/wlr_compositor.c | 2 ++ xwayland/xwm.c | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/wlr/types/wlr_compositor.h b/include/wlr/types/wlr_compositor.h index 1b2f890f..d8a2506f 100644 --- a/include/wlr/types/wlr_compositor.h +++ b/include/wlr/types/wlr_compositor.h @@ -9,6 +9,8 @@ struct wlr_compositor { struct wlr_renderer *renderer; struct wl_list surfaces; struct wl_listener destroy_surface_listener; + + struct wl_signal create_surface_signal; }; void wlr_compositor_destroy(struct wlr_compositor *wlr_compositor); diff --git a/types/wlr_compositor.c b/types/wlr_compositor.c index c153f525..6b6312d1 100644 --- a/types/wlr_compositor.c +++ b/types/wlr_compositor.c @@ -22,6 +22,7 @@ static void wl_compositor_create_surface(struct wl_client *client, wl_resource_add_destroy_listener(surface_resource, &surface->compositor_listener); wl_list_insert(&compositor->surfaces, wl_resource_get_link(surface_resource)); + wl_signal_emit(&compositor->create_surface_signal, surface); } static void wl_compositor_create_region(struct wl_client *client, @@ -80,5 +81,6 @@ struct wlr_compositor *wlr_compositor_create(struct wl_display *display, compositor->renderer = renderer; wl_list_init(&compositor->wl_resources); wl_list_init(&compositor->surfaces); + wl_signal_init(&compositor->create_surface_signal); return compositor; } diff --git a/xwayland/xwm.c b/xwayland/xwm.c index d39527c5..3b1d3b7b 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -62,8 +62,8 @@ struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) { xwm->surface_listener.notify = create_surface_handler; - //wl_signal_add(&wlr_xwayland->compositor->create_surface_signal, - // &xwm->surface_listener); + wl_signal_add(&wlr_xwayland->compositor->create_surface_signal, + &xwm->surface_listener); return xwm; } From 6eb20fb8d28749e0b710a54daa1b916080cf4ea1 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 23:09:59 +0200 Subject: [PATCH 05/17] init xwm only once Xwayland server is ready --- include/wlr/xwayland.h | 1 + xwayland/sockets.c | 5 ++++- xwayland/xwayland.c | 35 +++++++++++++++++++++++++++-------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/include/wlr/xwayland.h b/include/wlr/xwayland.h index f61a90bd..bbb315e2 100644 --- a/include/wlr/xwayland.h +++ b/include/wlr/xwayland.h @@ -16,6 +16,7 @@ struct wlr_xwayland { struct wlr_compositor *compositor; time_t server_start; + struct wl_event_source *sigusr1_source; struct wlr_xwm *xwm; }; diff --git a/xwayland/sockets.c b/xwayland/sockets.c index 868d6ada..714969fe 100644 --- a/xwayland/sockets.c +++ b/xwayland/sockets.c @@ -83,6 +83,9 @@ void unlink_sockets(int display) { snprintf(sun_path, sizeof(sun_path), socket_fmt, display); unlink(sun_path); + + snprintf(sun_path, sizeof(sun_path), lock_fmt, display); + unlink(sun_path); } int open_display_sockets(int socks[2]) { @@ -121,7 +124,7 @@ int open_display_sockets(int socks[2]) { } long int read_pid; read_pid = strtol(pid, &end_pid, 10); - if (read_pid < 0 || read_pid > INT32_MAX || end_pid != pid + sizeof(pid)) { + if (read_pid < 0 || read_pid > INT32_MAX || end_pid != pid + sizeof(pid) - 2) { continue; } errno = 0; diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index e9bdfcc5..e998db28 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -35,6 +35,9 @@ static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { exit(EXIT_FAILURE); } + /* Make Xwayland signal us when it's ready */ + signal(SIGUSR1, SIG_IGN); + char *argv[11] = { 0 }; argv[0] = "Xwayland"; if (asprintf(&argv[1], ":%d", wlr_xwayland->display) < 0) { @@ -106,6 +109,10 @@ void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { wl_client_destroy(wlr_xwayland->client); } + if (wlr_xwayland->sigusr1_source) { + wl_event_source_remove(wlr_xwayland->sigusr1_source); + } + xwm_destroy(wlr_xwayland->xwm); safe_close(wlr_xwayland->x_fd[0]); @@ -120,6 +127,24 @@ void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { /* kill Xwayland process? */ } +static int xserver_handle_ready(int signal_number, void *data) { + struct wlr_xwayland *wlr_xwayland = data; + + wlr_log(L_DEBUG, "Xserver is ready"); + + wlr_xwayland->xwm = xwm_create(wlr_xwayland); + if (!wlr_xwayland->xwm) { + wlr_xwayland_finish(wlr_xwayland); + return 1; + } + + char display_name[16]; + snprintf(display_name, sizeof(display_name), ":%d", wlr_xwayland->display); + setenv("DISPLAY", display_name, true); + + return 1; +} + bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, struct wl_display *wl_display, struct wlr_compositor *compositor) { memset(wlr_xwayland, 0, sizeof(struct wlr_xwayland)); @@ -161,9 +186,6 @@ bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, wlr_xwayland->x_fd[0] = wlr_xwayland->x_fd[1] = -1; wlr_xwayland->wl_fd[1] = wlr_xwayland->wm_fd[1] = -1; - char display_name[16]; - snprintf(display_name, sizeof(display_name), ":%d", wlr_xwayland->display); - setenv("DISPLAY", display_name, true); wlr_xwayland->server_start = time(NULL); if (!(wlr_xwayland->client = wl_client_create(wl_display, wlr_xwayland->wl_fd[0]))) { @@ -174,11 +196,8 @@ bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, wl_client_add_destroy_listener(wlr_xwayland->client, &xwayland_destroy_listener); - wlr_xwayland->xwm = xwm_create(wlr_xwayland); - if (!wlr_xwayland->xwm) { - wlr_xwayland_finish(wlr_xwayland); - return false; - } + struct wl_event_loop *loop = wl_display_get_event_loop(wl_display); + wlr_xwayland->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1, xserver_handle_ready, wlr_xwayland); return true; } From fa0e1015c6f06f08fe368f89af511ef646f0bc99 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 07:47:38 +0200 Subject: [PATCH 06/17] xwm: fix minimal init, this gets us some events on window creations --- include/xwayland/internals.h | 68 ++++++++++++++++++++++++- meson.build | 2 + xwayland/meson.build | 2 +- xwayland/xwayland.c | 3 ++ xwayland/xwm.c | 98 +++++++++++++++++++++++++++++++++--- 5 files changed, 162 insertions(+), 11 deletions(-) diff --git a/include/xwayland/internals.h b/include/xwayland/internals.h index 46158bd0..97c7e31a 100644 --- a/include/xwayland/internals.h +++ b/include/xwayland/internals.h @@ -4,13 +4,77 @@ #include #include +/* wlc's atom list: + WL_SURFACE_ID, + WM_DELETE_WINDOW, + WM_TAKE_FOCUS, + WM_PROTOCOLS, + WM_NORMAL_HINTS, + MOTIF_WM_HINTS, + TEXT, + UTF8_STRING, + CLIPBOARD, + CLIPBOARD_MANAGER, + TARGETS, + PRIMARY, + WM_S0, + STRING, + WLC_SELECTION, + NET_WM_S0, + NET_WM_PID, + NET_WM_NAME, + NET_WM_STATE, + NET_WM_STATE_FULLSCREEN, + NET_WM_STATE_MODAL, + NET_WM_STATE_ABOVE, + NET_SUPPORTED, + NET_SUPPORTING_WM_CHECK, + NET_WM_WINDOW_TYPE, + NET_WM_WINDOW_TYPE_DESKTOP, + NET_WM_WINDOW_TYPE_DOCK, + NET_WM_WINDOW_TYPE_TOOLBAR, + NET_WM_WINDOW_TYPE_MENU, + NET_WM_WINDOW_TYPE_UTILITY, + NET_WM_WINDOW_TYPE_SPLASH, + NET_WM_WINDOW_TYPE_DIALOG, + NET_WM_WINDOW_TYPE_DROPDOWN_MENU, + NET_WM_WINDOW_TYPE_POPUP_MENU, + NET_WM_WINDOW_TYPE_TOOLTIP, + NET_WM_WINDOW_TYPE_NOTIFICATION, + NET_WM_WINDOW_TYPE_COMBO, + NET_WM_WINDOW_TYPE_DND, + NET_WM_WINDOW_TYPE_NORMAL, + */ + +enum atom_name { + WL_SURFACE_ID, + WM_PROTOCOLS, + WM_S0, + NET_SUPPORTED, + NET_WM_S0, + NET_WM_STATE, + ATOM_LAST +}; + +static const char * const atom_map[ATOM_LAST] = { + "WL_SURFACE_ID", + "WM_PROTOCOLS", + "WM_S0", + "_NET_SUPPORTED", + "_NET_WM_S0", + "_NET_WM_STATE", +}; + + struct wlr_xwm { struct wlr_xwayland *xwayland; struct wl_event_source *event_source; struct wl_listener surface_listener; - xcb_connection_t *xcb_connection; - xcb_screen_t *xcb_screen; + xcb_atom_t atoms[ATOM_LAST]; + xcb_connection_t *xcb_conn; + xcb_screen_t *screen; + xcb_window_t window; }; void unlink_sockets(int display); diff --git a/meson.build b/meson.build index a7baacf9..9a9574fd 100644 --- a/meson.build +++ b/meson.build @@ -33,6 +33,7 @@ xkbcommon = dependency('xkbcommon') udev = dependency('libudev') pixman = dependency('pixman-1') xcb = dependency('xcb') +xcb_composite = dependency('xcb-composite') libcap = dependency('libcap', required: false) systemd = dependency('libsystemd', required: false) math = cc.find_library('m', required: false) @@ -67,6 +68,7 @@ wlr_deps = [ udev, pixman, xcb, + xcb_composite, libcap, systemd, math, diff --git a/xwayland/meson.build b/xwayland/meson.build index 6f75e23a..dbad27bb 100644 --- a/xwayland/meson.build +++ b/xwayland/meson.build @@ -4,4 +4,4 @@ lib_wlr_xwayland = static_library('wlr_xwayland', files( 'xwm.c', ), include_directories: wlr_inc, - dependencies: [wayland_server, xcb, pixman]) + dependencies: [wayland_server, xcb, xcb_composite, pixman]) diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index e998db28..a7047369 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -138,6 +138,9 @@ static int xserver_handle_ready(int signal_number, void *data) { return 1; } + wl_event_source_remove(wlr_xwayland->sigusr1_source); + wlr_xwayland->sigusr1_source = NULL; + char display_name[16]; snprintf(display_name, sizeof(display_name), ":%d", wlr_xwayland->display); setenv("DISPLAY", display_name, true); diff --git a/xwayland/xwm.c b/xwayland/xwm.c index 3b1d3b7b..af21c78d 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -1,20 +1,34 @@ #include +#include +#include #include "wlr/util/log.h" #include "wlr/types/wlr_surface.h" #include "wlr/xwayland.h" #include "xwayland/internals.h" -#define SEND_EVENT_MASK 0x80 + + + static int x11_event_handler(int fd, uint32_t mask, void *data) { int count = 0; xcb_generic_event_t *event; struct wlr_xwayland *wlr_xwayland = data; struct wlr_xwm *xwm = wlr_xwayland->xwm; - while ((event = xcb_poll_for_event(xwm->xcb_connection))) { - wlr_log(L_DEBUG, "X11 event: %d", event->response_type & ~SEND_EVENT_MASK); + while ((event = xcb_poll_for_event(xwm->xcb_conn))) { count++; - // TODO: actually do stuff! + switch (event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK) { + case XCB_CREATE_NOTIFY: + break; + case XCB_CONFIGURE_REQUEST: + break; + case XCB_MAP_REQUEST: + break; + case XCB_DESTROY_NOTIFY: + break; + default: + wlr_log(L_DEBUG, "X11 event: %d", event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK); + } } return count; } @@ -33,11 +47,76 @@ static void create_surface_handler(struct wl_listener *listener, void *data) // TODO: look for unpaired window, and assign } +static void xcb_get_resources(struct wlr_xwm *xwm) { + int i; + + for (i = 0; i < ATOM_LAST; i++) { + xcb_intern_atom_cookie_t cookie; + xcb_intern_atom_reply_t *reply; + xcb_generic_error_t *error; + + cookie = xcb_intern_atom(xwm->xcb_conn, 0, strlen(atom_map[i]), atom_map[i]); + reply = xcb_intern_atom_reply(xwm->xcb_conn, cookie, &error); + + if (reply && !error) { + xwm->atoms[i] = reply->atom; + } + if (reply) { + free(reply); + } + if (error) { + wlr_log(L_ERROR, "could not resolve atom %s, x11 error code %d", + atom_map[i], error->error_code); + free(error); + return; + } + } +} + +static bool xcb_call(struct wlr_xwm *xwm, const char *func, uint32_t line, xcb_void_cookie_t cookie) { + xcb_generic_error_t *error; + if (!(error = xcb_request_check(xwm->xcb_conn, cookie))) { + return true; + } + + wlr_log(L_ERROR, "xcb call failed in %s:%u, x11 error code %d", func, line, error->error_code); + free(error); + return false; +} +#define XCB_CALL(xwm, x) xcb_call(xwm, __PRETTY_FUNCTION__, __LINE__, x) + +static void xcb_init_wm(struct wlr_xwm *xwm) { + xcb_screen_iterator_t screen_iterator; + screen_iterator = xcb_setup_roots_iterator(xcb_get_setup(xwm->xcb_conn)); + xwm->screen = screen_iterator.data; + + xwm->window = xcb_generate_id(xwm->xcb_conn); + + uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_PROPERTY_CHANGE /* , xwm->cursor */ }; + XCB_CALL(xwm, xcb_change_window_attributes_checked(xwm->xcb_conn, xwm->screen->root, + XCB_CW_EVENT_MASK /* | XCB_CW_CURSOR */, values)); + XCB_CALL(xwm, xcb_composite_redirect_subwindows_checked(xwm->xcb_conn, xwm->screen->root, XCB_COMPOSITE_REDIRECT_MANUAL)); + + XCB_CALL(xwm, xcb_create_window_checked(xwm->xcb_conn, XCB_COPY_FROM_PARENT, xwm->window, xwm->screen->root, + 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, xwm->screen->root_visual, + XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_PROPERTY_CHANGE})); + xcb_atom_t supported[] = { + xwm->atoms[NET_WM_STATE], + }; + XCB_CALL(xwm, xcb_change_property_checked(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, xwm->screen->root, xwm->atoms[NET_SUPPORTED], XCB_ATOM_ATOM, 32, sizeof(supported)/sizeof(*supported), supported)); + + XCB_CALL(xwm, xcb_set_selection_owner_checked(xwm->xcb_conn, xwm->window, xwm->atoms[WM_S0], XCB_CURRENT_TIME)); + XCB_CALL(xwm, xcb_set_selection_owner_checked(xwm->xcb_conn, xwm->window, xwm->atoms[NET_WM_S0], XCB_CURRENT_TIME)); + xcb_flush(xwm->xcb_conn); +} + void xwm_destroy(struct wlr_xwm *xwm) { if (xwm->event_source) { wl_event_source_remove(xwm->event_source); } + xcb_disconnect(xwm->xcb_conn); + free(xwm); } @@ -46,13 +125,12 @@ struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) { xwm->xwayland = wlr_xwayland; - xwm->xcb_connection = xcb_connect_to_fd(wlr_xwayland->wm_fd[0], NULL); - if (xcb_connection_has_error(xwm->xcb_connection)) { + xwm->xcb_conn = xcb_connect_to_fd(wlr_xwayland->wm_fd[0], NULL); + if (xcb_connection_has_error(xwm->xcb_conn)) { + free(xwm); return NULL; } - // TODO more xcb init - // xcb_prefetch_extension_data(xwm->xcb_connection, &xcb_composite_id); struct wl_event_loop *event_loop = wl_display_get_event_loop(wlr_xwayland->wl_display); xwm->event_source = wl_event_loop_add_fd(event_loop, wlr_xwayland->wm_fd[0], @@ -60,6 +138,10 @@ struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) { // probably not needed // wl_event_source_check(xwm->event_source); + // TODO more xcb init + // xcb_prefetch_extension_data(xwm->xcb_conn, &xcb_composite_id); + xcb_get_resources(xwm); + xcb_init_wm(xwm); xwm->surface_listener.notify = create_surface_handler; wl_signal_add(&wlr_xwayland->compositor->create_surface_signal, From eb5b9cc6daa39f6e1bcafae074f832f07b5d0a19 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 07:59:03 +0200 Subject: [PATCH 07/17] xwayland: move & split internal header file --- examples/compositor.c | 6 ++-- include/wlr/xwayland.h | 6 ++-- xwayland/sockets.c | 4 +-- xwayland/sockets.h | 7 +++++ xwayland/xwayland.c | 28 ++++++++++++++++--- xwayland/xwm.c | 4 +-- .../xwayland/internals.h => xwayland/xwm.h | 3 -- 7 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 xwayland/sockets.h rename include/xwayland/internals.h => xwayland/xwm.h (95%) diff --git a/examples/compositor.c b/examples/compositor.c index e143099c..e0bafe60 100644 --- a/examples/compositor.c +++ b/examples/compositor.c @@ -36,7 +36,7 @@ struct sample_state { struct wlr_data_device_manager *data_device_manager; struct wl_resource *focus; struct wl_listener keyboard_bound; - struct wlr_xwayland wlr_xwayland; + struct wlr_xwayland *wlr_xwayland; int keymap_fd; size_t keymap_size; uint32_t serial; @@ -178,13 +178,13 @@ int main() { free(keymap); break; } - wlr_xwayland_init(&state.wlr_xwayland, compositor.display, state.wlr_compositor); + state.wlr_xwayland = wlr_xwayland_create(compositor.display, state.wlr_compositor); compositor.keyboard_key_cb = handle_keyboard_key; wl_display_run(compositor.display); - wlr_xwayland_finish(&state.wlr_xwayland); + wlr_xwayland_destroy(state.wlr_xwayland); close(state.keymap_fd); wlr_seat_destroy(state.wl_seat); wlr_data_device_manager_destroy(state.data_device_manager); diff --git a/include/wlr/xwayland.h b/include/wlr/xwayland.h index bbb315e2..61386700 100644 --- a/include/wlr/xwayland.h +++ b/include/wlr/xwayland.h @@ -20,8 +20,8 @@ struct wlr_xwayland { struct wlr_xwm *xwm; }; -void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland); -bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, - struct wl_display *wl_display, struct wlr_compositor *compositor); +void wlr_xwayland_destroy(struct wlr_xwayland *wlr_xwayland); +struct wlr_xwayland *wlr_xwayland_create(struct wl_display *wl_display, + struct wlr_compositor *compositor); #endif diff --git a/xwayland/sockets.c b/xwayland/sockets.c index 714969fe..ca46ec9f 100644 --- a/xwayland/sockets.c +++ b/xwayland/sockets.c @@ -12,7 +12,7 @@ #include #include #include "wlr/util/log.h" -#include "xwayland/internals.h" +#include "sockets.h" static const char *lock_fmt = "/tmp/.X%d-lock"; static const char *socket_dir = "/tmp/.X11-unix"; @@ -78,7 +78,7 @@ static bool open_sockets(int socks[2], int display) { return true; } -void unlink_sockets(int display) { +void unlink_display_sockets(int display) { char sun_path[64]; snprintf(sun_path, sizeof(sun_path), socket_fmt, display); diff --git a/xwayland/sockets.h b/xwayland/sockets.h new file mode 100644 index 00000000..73eb36e0 --- /dev/null +++ b/xwayland/sockets.h @@ -0,0 +1,7 @@ +#ifndef XWAYLAND_SOCKETS_H +#define XWAYLAND_SOCKETS_H + +void unlink_display_sockets(int display); +int open_display_sockets(int socks[2]); + +#endif diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index a7047369..764c0135 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -11,7 +11,8 @@ #include #include "wlr/util/log.h" #include "wlr/xwayland.h" -#include "xwayland/internals.h" +#include "sockets.h" +#include "xwm.h" static void safe_close(int fd) { if (fd >= 0) { @@ -84,6 +85,10 @@ static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { execvpe("Xwayland", argv, envp); } +static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, + struct wl_display *wl_display, struct wlr_compositor *compositor); +static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland); + static void xwayland_destroy_event(struct wl_listener *listener, void *data) { struct wl_client *client = data; struct wlr_xwayland *wlr_xwayland = wl_container_of(client, wlr_xwayland, client); @@ -102,7 +107,7 @@ static struct wl_listener xwayland_destroy_listener = { .notify = xwayland_destroy_event, }; -void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { +static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { if (wlr_xwayland->client) { wl_list_remove(&xwayland_destroy_listener.link); @@ -122,7 +127,7 @@ void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { safe_close(wlr_xwayland->wm_fd[0]); safe_close(wlr_xwayland->wm_fd[1]); - unlink_sockets(wlr_xwayland->display); + unlink_display_sockets(wlr_xwayland->display); unsetenv("DISPLAY"); /* kill Xwayland process? */ } @@ -148,7 +153,7 @@ static int xserver_handle_ready(int signal_number, void *data) { return 1; } -bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, +static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, struct wl_display *wl_display, struct wlr_compositor *compositor) { memset(wlr_xwayland, 0, sizeof(struct wlr_xwayland)); wlr_xwayland->wl_display = wl_display; @@ -204,3 +209,18 @@ bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, return true; } + +void wlr_xwayland_destroy(struct wlr_xwayland *wlr_xwayland) { + wlr_xwayland_finish(wlr_xwayland); + free(wlr_xwayland); +} + +struct wlr_xwayland *wlr_xwayland_create(struct wl_display *wl_display, + struct wlr_compositor *compositor) { + struct wlr_xwayland *wlr_xwayland = calloc(1, sizeof(struct wlr_xwayland)); + if (wlr_xwayland_init(wlr_xwayland, wl_display, compositor)) { + return wlr_xwayland; + } + free(wlr_xwayland); + return NULL; +} diff --git a/xwayland/xwm.c b/xwayland/xwm.c index af21c78d..26d86fb3 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -4,9 +4,7 @@ #include "wlr/util/log.h" #include "wlr/types/wlr_surface.h" #include "wlr/xwayland.h" -#include "xwayland/internals.h" - - +#include "xwm.h" static int x11_event_handler(int fd, uint32_t mask, void *data) { diff --git a/include/xwayland/internals.h b/xwayland/xwm.h similarity index 95% rename from include/xwayland/internals.h rename to xwayland/xwm.h index 97c7e31a..cfdd95f5 100644 --- a/include/xwayland/internals.h +++ b/xwayland/xwm.h @@ -77,9 +77,6 @@ struct wlr_xwm { xcb_window_t window; }; -void unlink_sockets(int display); -int open_display_sockets(int socks[2]); - void xwm_destroy(struct wlr_xwm *xwm); struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland); From ea906ea3e217c578aca76825c1d24a542b487380 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 13:41:15 +0200 Subject: [PATCH 08/17] wlr surface/shell types: add message on creation Not sure if we really want to keep that, would be helpful to get more logging levels at some point --- types/wlr_surface.c | 1 + types/wlr_wl_shell.c | 1 + types/wlr_xdg_shell_v6.c | 1 + 3 files changed, 3 insertions(+) diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 2f68b134..e733c544 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -386,6 +386,7 @@ struct wlr_surface *wlr_surface_create(struct wl_resource *res, wl_resource_post_no_memory(res); return NULL; } + wlr_log(L_DEBUG, "New wlr_surface %p (res %p)", surface, res); surface->renderer = renderer; surface->texture = wlr_render_texture_create(renderer); surface->resource = res; diff --git a/types/wlr_wl_shell.c b/types/wlr_wl_shell.c index 378758e8..84b40fc3 100644 --- a/types/wlr_wl_shell.c +++ b/types/wlr_wl_shell.c @@ -88,6 +88,7 @@ static void wl_shell_get_shell_surface(struct wl_client *client, state->surface = surface; struct wl_resource *shell_surface_resource = wl_resource_create(client, &wl_shell_surface_interface, wl_resource_get_version(resource), id); + wlr_log(L_DEBUG, "New wl_shell %p (res %p)", state, shell_surface_resource); wl_resource_set_implementation(shell_surface_resource, &shell_surface_interface, state, destroy_shell_surface); wl_list_insert(&wlr_wl_shell->surfaces, &state->link); diff --git a/types/wlr_xdg_shell_v6.c b/types/wlr_xdg_shell_v6.c index 484571b3..b5df9bd8 100644 --- a/types/wlr_xdg_shell_v6.c +++ b/types/wlr_xdg_shell_v6.c @@ -149,6 +149,7 @@ static void xdg_shell_get_xdg_surface(struct wl_client *client, surface->surface = _surface; surface->resource = wl_resource_create(client, &zxdg_surface_v6_interface, wl_resource_get_version(_xdg_shell), id); + wlr_log(L_DEBUG, "new xdg_surface %p (res %p)", surface, surface->resource); wl_resource_set_implementation(surface->resource, &zxdg_surface_v6_implementation, surface, xdg_surface_destroy); wl_list_insert(&xdg_shell->surfaces, &surface->link); From f912316d9b1ed39352efb07e773128b6e4f554aa Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 13:43:04 +0200 Subject: [PATCH 09/17] xwm: handle some more events handle map/unmap_notify, property_notify (just logging for now), and client_message for wl_surface_id. We almost can display an X window now, just need the compositor to iterate over them --- xwayland/xwm.c | 259 ++++++++++++++++++++++++++++++++++++++++++------- xwayland/xwm.h | 17 +++- 2 files changed, 239 insertions(+), 37 deletions(-) diff --git a/xwayland/xwm.c b/xwayland/xwm.c index 26d86fb3..b9653f14 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -6,55 +6,243 @@ #include "wlr/xwayland.h" #include "xwm.h" +/* General helpers */ +// TODO: replace this with hash table? +static struct wlr_x11_window *lookup_window(struct wl_list *list, xcb_window_t window_id) { + struct wlr_x11_window *window; + wl_list_for_each(window, list, link) { + if (window->window_id == window_id) { + return window; + } + } + return NULL; +} +static struct wlr_x11_window *lookup_window_any(struct wlr_xwm *xwm, xcb_window_t window_id) { + struct wlr_x11_window *window; + if ((window = lookup_window(&xwm->paired_windows, window_id)) || + (window = lookup_window(&xwm->unpaired_windows, window_id)) || + (window = lookup_window(&xwm->new_windows, window_id))) { + return window; + } + return NULL; +} + +static struct wlr_x11_window *wlr_x11_window_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_x11_window *window; + + window = calloc(1, sizeof(struct wlr_x11_window)); + if (!window) { + wlr_log(L_ERROR, "Could not allocate wlr x11 window"); + return NULL; + } + window->window_id = window_id; + window->x = x; + window->y = y; + window->width = width; + window->height = height; + window->override_redirect = override_redirect; + wl_list_insert(&xwm->new_windows, &window->link); + return window; +} + +static void wlr_x11_window_destroy(struct wlr_x11_window *window) { + wl_list_remove(&window->link); + free(window); +} + +/* xcb helpers */ +#define XCB_CALL(xwm, x) xcb_call(xwm, __PRETTY_FUNCTION__, __LINE__, x) +static bool xcb_call(struct wlr_xwm *xwm, const char *func, uint32_t line, + xcb_void_cookie_t cookie) { + xcb_generic_error_t *error; + if (!(error = xcb_request_check(xwm->xcb_conn, cookie))) { + return true; + } + + wlr_log(L_ERROR, "xcb call failed in %s:%u, x11 error code %d", + func, line, error->error_code); + free(error); + return false; +} + +static void map_shell_surface(struct wlr_xwm *xwm, struct wlr_x11_window *window, + struct wlr_surface *surface) { + + // get xcb geometry for depth = alpha channel + // TODO link to compositor somehow + + wl_list_remove(&window->link); + wl_list_insert(&xwm->paired_windows, &window->link); +} + +/* xcb event handlers */ +static void handle_create_notify(struct wlr_xwm *xwm, xcb_create_notify_event_t *ev) { + wlr_log(L_DEBUG, "XCB_CREATE_NOTIFY (%u)", ev->window); + wlr_x11_window_create(xwm, ev->window, ev->x, ev->y, + ev->width, ev->height, ev->override_redirect); +} + +static void handle_destroy_notify(struct wlr_xwm *xwm, xcb_destroy_notify_event_t *ev) { + struct wlr_x11_window *window; + wlr_log(L_DEBUG, "XCB_DESTROY_NOTIFY (%u)", ev->window); + if (!(window = lookup_window_any(xwm, ev->window))) { + return; + } + wlr_x11_window_destroy(window); +} + +static void handle_configure_request(struct wlr_xwm *xwm, xcb_configure_request_event_t *ev) { + struct wlr_x11_window *window; + wlr_log(L_DEBUG, "XCB_CONFIGURE_REQUEST (%u) [%ux%u+%d,%d]", ev->window, + ev->width, ev->height, ev->x, ev->y); + if (!(window = lookup_window_any(xwm, ev->window))) { + return; + } + + window->x = ev->x; + window->y = ev->y; + window->width = ev->width; + window->height = ev->height; + // handle parent/sibling? +} + +static void handle_map_request(struct wlr_xwm *xwm, xcb_map_request_event_t *ev) { + wlr_log(L_DEBUG, "XCB_MAP_REQUEST (%u)", ev->window); + XCB_CALL(xwm, xcb_change_window_attributes_checked(xwm->xcb_conn, + ev->window, XCB_CW_EVENT_MASK, + &(uint32_t){XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_PROPERTY_CHANGE})); + XCB_CALL(xwm, xcb_map_window_checked(xwm->xcb_conn, ev->window)); +} + +static void handle_map_notify(struct wlr_xwm *xwm, xcb_map_notify_event_t *ev) { + struct wlr_x11_window *window; + wlr_log(L_DEBUG, "XCB_MAP_NOTIFY (%u)", ev->window); + if ((window = lookup_window_any(xwm, ev->window))) { + window->override_redirect = ev->override_redirect; + } else { + wlr_x11_window_create(xwm, ev->window, 0, 0, 1, 1, ev->override_redirect); + } +} + +static void handle_unmap_notify(struct wlr_xwm *xwm, xcb_unmap_notify_event_t *ev) { + struct wlr_x11_window *window; + wlr_log(L_DEBUG, "XCB_UNMAP_NOTIFY (%u)", ev->window); + if (!(window = lookup_window_any(xwm, ev->window))) { + return; + } + // remove pointer to surface only? + wlr_x11_window_destroy(window); +} + +static void handle_property_notify(struct wlr_xwm *xwm, xcb_property_notify_event_t *ev) { + wlr_log(L_DEBUG, "XCB_PROPERTY_NOTIFY (%u)", ev->window); + // TODO lookup window & get properties +} + +static void 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]) { + struct wlr_x11_window *window; + struct wl_resource *resource; + window = lookup_window(&xwm->new_windows, ev->window); + if (!window) { + wlr_log(L_DEBUG, "client message WL_SURFACE_ID but no new window %u ?", + ev->window); + return; + } + window->surface_id = ev->data.data32[0]; + /* Check if we got notified after wayland surface create event */ + resource = wl_client_get_object(xwm->xwayland->client, window->surface_id); + if (resource) { + map_shell_surface(xwm, window, wl_resource_get_user_data(resource)); + } else { + wl_list_remove(&window->link); + wl_list_insert(&xwm->unpaired_windows, &window->link); + } + } + wlr_log(L_DEBUG, "unhandled client message %u", ev->type); +} static int x11_event_handler(int fd, uint32_t mask, void *data) { int count = 0; xcb_generic_event_t *event; - struct wlr_xwayland *wlr_xwayland = data; - struct wlr_xwm *xwm = wlr_xwayland->xwm; + 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: + handle_create_notify(xwm, (xcb_create_notify_event_t *)event); + break; + case XCB_DESTROY_NOTIFY: + handle_destroy_notify(xwm, (xcb_destroy_notify_event_t *)event); break; case XCB_CONFIGURE_REQUEST: + handle_configure_request(xwm, (xcb_configure_request_event_t *)event); break; case XCB_MAP_REQUEST: + handle_map_request(xwm, (xcb_map_request_event_t *)event); break; - case XCB_DESTROY_NOTIFY: + case XCB_MAP_NOTIFY: + handle_map_notify(xwm, (xcb_map_notify_event_t *)event); + break; + case XCB_UNMAP_NOTIFY: + handle_unmap_notify(xwm, (xcb_unmap_notify_event_t *)event); + break; + case XCB_PROPERTY_NOTIFY: + handle_property_notify(xwm, (xcb_property_notify_event_t *)event); + break; + case XCB_CLIENT_MESSAGE: + handle_client_message(xwm, (xcb_client_message_event_t *)event); break; default: - wlr_log(L_DEBUG, "X11 event: %d", event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK); + wlr_log(L_DEBUG, "X11 event: %d", + event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK); } } + + xcb_flush(xwm->xcb_conn); return count; } static void create_surface_handler(struct wl_listener *listener, void *data) { struct wlr_surface *surface = data; - struct wlr_xwm *xwm = wl_container_of(listener, xwm, surface_listener); + struct wlr_xwm *xwm = wl_container_of(listener, xwm, surface_create_listener); + struct wlr_x11_window *window; + uint32_t surface_id; if (wl_resource_get_client(surface->resource) != xwm->xwayland->client) { return; } - wlr_log(L_DEBUG, "new x11 surface: %p", surface); + wlr_log(L_DEBUG, "New x11 surface: %p", surface); - // TODO: look for unpaired window, and assign + surface_id = wl_resource_get_id(surface->resource); + wl_list_for_each(window, &xwm->unpaired_windows, link) { + if (window->surface_id == surface_id) { + map_shell_surface(xwm, window, surface); + xcb_flush(xwm->xcb_conn); + return; + } + } } static void xcb_get_resources(struct wlr_xwm *xwm) { int i; + xcb_intern_atom_cookie_t cookies[ATOM_LAST]; for (i = 0; i < ATOM_LAST; i++) { - xcb_intern_atom_cookie_t cookie; + cookies[i] = xcb_intern_atom(xwm->xcb_conn, 0, strlen(atom_map[i]), atom_map[i]); + } + for (i = 0; i < ATOM_LAST; i++) { xcb_intern_atom_reply_t *reply; xcb_generic_error_t *error; - cookie = xcb_intern_atom(xwm->xcb_conn, 0, strlen(atom_map[i]), atom_map[i]); - reply = xcb_intern_atom_reply(xwm->xcb_conn, cookie, &error); + reply = xcb_intern_atom_reply(xwm->xcb_conn, cookies[i], &error); if (reply && !error) { xwm->atoms[i] = reply->atom; @@ -71,18 +259,6 @@ static void xcb_get_resources(struct wlr_xwm *xwm) { } } -static bool xcb_call(struct wlr_xwm *xwm, const char *func, uint32_t line, xcb_void_cookie_t cookie) { - xcb_generic_error_t *error; - if (!(error = xcb_request_check(xwm->xcb_conn, cookie))) { - return true; - } - - wlr_log(L_ERROR, "xcb call failed in %s:%u, x11 error code %d", func, line, error->error_code); - free(error); - return false; -} -#define XCB_CALL(xwm, x) xcb_call(xwm, __PRETTY_FUNCTION__, __LINE__, x) - static void xcb_init_wm(struct wlr_xwm *xwm) { xcb_screen_iterator_t screen_iterator; screen_iterator = xcb_setup_roots_iterator(xcb_get_setup(xwm->xcb_conn)); @@ -90,21 +266,28 @@ static void xcb_init_wm(struct wlr_xwm *xwm) { xwm->window = xcb_generate_id(xwm->xcb_conn); - uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_PROPERTY_CHANGE /* , xwm->cursor */ }; + uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_PROPERTY_CHANGE, + /* , xwm->cursor */ }; XCB_CALL(xwm, xcb_change_window_attributes_checked(xwm->xcb_conn, xwm->screen->root, - XCB_CW_EVENT_MASK /* | XCB_CW_CURSOR */, values)); - XCB_CALL(xwm, xcb_composite_redirect_subwindows_checked(xwm->xcb_conn, xwm->screen->root, XCB_COMPOSITE_REDIRECT_MANUAL)); + XCB_CW_EVENT_MASK /* | XCB_CW_CURSOR */, values)); + XCB_CALL(xwm, xcb_composite_redirect_subwindows_checked(xwm->xcb_conn, + xwm->screen->root, XCB_COMPOSITE_REDIRECT_MANUAL)); - XCB_CALL(xwm, xcb_create_window_checked(xwm->xcb_conn, XCB_COPY_FROM_PARENT, xwm->window, xwm->screen->root, - 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, xwm->screen->root_visual, - XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_PROPERTY_CHANGE})); + XCB_CALL(xwm, xcb_create_window_checked(xwm->xcb_conn, XCB_COPY_FROM_PARENT, + xwm->window, xwm->screen->root, 0, 0, 1, 1, 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, xwm->screen->root_visual, + XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_PROPERTY_CHANGE})); xcb_atom_t supported[] = { xwm->atoms[NET_WM_STATE], }; - XCB_CALL(xwm, xcb_change_property_checked(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, xwm->screen->root, xwm->atoms[NET_SUPPORTED], XCB_ATOM_ATOM, 32, sizeof(supported)/sizeof(*supported), supported)); + XCB_CALL(xwm, xcb_change_property_checked(xwm->xcb_conn, XCB_PROP_MODE_REPLACE, + xwm->screen->root, xwm->atoms[NET_SUPPORTED], XCB_ATOM_ATOM, + 32, sizeof(supported)/sizeof(*supported), supported)); - XCB_CALL(xwm, xcb_set_selection_owner_checked(xwm->xcb_conn, xwm->window, xwm->atoms[WM_S0], XCB_CURRENT_TIME)); - XCB_CALL(xwm, xcb_set_selection_owner_checked(xwm->xcb_conn, xwm->window, xwm->atoms[NET_WM_S0], XCB_CURRENT_TIME)); + XCB_CALL(xwm, xcb_set_selection_owner_checked(xwm->xcb_conn, xwm->window, + xwm->atoms[WM_S0], XCB_CURRENT_TIME)); + XCB_CALL(xwm, xcb_set_selection_owner_checked(xwm->xcb_conn, xwm->window, + xwm->atoms[NET_WM_S0], XCB_CURRENT_TIME)); xcb_flush(xwm->xcb_conn); } @@ -120,19 +303,23 @@ void xwm_destroy(struct wlr_xwm *xwm) { struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) { struct wlr_xwm *xwm = calloc(1, sizeof(struct wlr_xwm)); + int rc; xwm->xwayland = wlr_xwayland; + wl_list_init(&xwm->new_windows); + wl_list_init(&xwm->unpaired_windows); + wl_list_init(&xwm->paired_windows); xwm->xcb_conn = xcb_connect_to_fd(wlr_xwayland->wm_fd[0], NULL); - if (xcb_connection_has_error(xwm->xcb_conn)) { + if ((rc = xcb_connection_has_error(xwm->xcb_conn))) { + wlr_log(L_ERROR, "xcb connect failed: %d", rc); free(xwm); return NULL; } - struct wl_event_loop *event_loop = wl_display_get_event_loop(wlr_xwayland->wl_display); xwm->event_source = wl_event_loop_add_fd(event_loop, wlr_xwayland->wm_fd[0], - WL_EVENT_READABLE, x11_event_handler, wlr_xwayland); + WL_EVENT_READABLE, x11_event_handler, xwm); // probably not needed // wl_event_source_check(xwm->event_source); @@ -141,9 +328,9 @@ struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) { xcb_get_resources(xwm); xcb_init_wm(xwm); - xwm->surface_listener.notify = create_surface_handler; + xwm->surface_create_listener.notify = create_surface_handler; wl_signal_add(&wlr_xwayland->compositor->create_surface_signal, - &xwm->surface_listener); + &xwm->surface_create_listener); return xwm; } diff --git a/xwayland/xwm.h b/xwayland/xwm.h index cfdd95f5..51d60395 100644 --- a/xwayland/xwm.h +++ b/xwayland/xwm.h @@ -65,16 +65,31 @@ static const char * const atom_map[ATOM_LAST] = { "_NET_WM_STATE", }; +struct wlr_x11_window { + xcb_window_t window_id; + uint32_t surface_id; + struct wl_list link; + + struct wl_resource *surface; + struct wl_listener surface_destroy_listener; + int16_t x, y; + uint16_t width, height; + bool override_redirect; +}; struct wlr_xwm { struct wlr_xwayland *xwayland; struct wl_event_source *event_source; - struct wl_listener surface_listener; + struct wl_listener surface_create_listener; xcb_atom_t atoms[ATOM_LAST]; xcb_connection_t *xcb_conn; xcb_screen_t *screen; xcb_window_t window; + + struct wl_list new_windows; + struct wl_list unpaired_windows; + struct wl_list paired_windows; }; void xwm_destroy(struct wlr_xwm *xwm); From 1458a95e65248fede646051d2607c4553e8dcb0c Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 14:18:29 +0200 Subject: [PATCH 10/17] example compositor: loop through xwayland surfaces --- examples/compositor.c | 10 +++++++--- include/wlr/xwayland.h | 22 ++++++++++++++++++---- xwayland/xwayland.c | 1 + xwayland/xwm.c | 13 ++++++------- xwayland/xwm.h | 14 -------------- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/examples/compositor.c b/examples/compositor.c index e0bafe60..024324cd 100644 --- a/examples/compositor.c +++ b/examples/compositor.c @@ -36,7 +36,7 @@ struct sample_state { struct wlr_data_device_manager *data_device_manager; struct wl_resource *focus; struct wl_listener keyboard_bound; - struct wlr_xwayland *wlr_xwayland; + struct wlr_xwayland *xwayland; int keymap_fd; size_t keymap_size; uint32_t serial; @@ -85,6 +85,10 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wl_list_for_each(xdg_surface, &sample->xdg_shell->surfaces, link) { output_frame_handle_surface(sample, wlr_output, ts, xdg_surface->surface); } + struct wlr_x11_window *x11_window; + wl_list_for_each(x11_window, &sample->xwayland->displayable_windows, link) { + output_frame_handle_surface(sample, wlr_output, ts, x11_window->surface); + } wlr_renderer_end(sample->renderer); wlr_output_swap_buffers(wlr_output); @@ -178,13 +182,13 @@ int main() { free(keymap); break; } - state.wlr_xwayland = wlr_xwayland_create(compositor.display, state.wlr_compositor); + state.xwayland = wlr_xwayland_create(compositor.display, state.wlr_compositor); compositor.keyboard_key_cb = handle_keyboard_key; wl_display_run(compositor.display); - wlr_xwayland_destroy(state.wlr_xwayland); + wlr_xwayland_destroy(state.xwayland); close(state.keymap_fd); wlr_seat_destroy(state.wl_seat); wlr_data_device_manager_destroy(state.data_device_manager); diff --git a/include/wlr/xwayland.h b/include/wlr/xwayland.h index 61386700..4d1a726d 100644 --- a/include/wlr/xwayland.h +++ b/include/wlr/xwayland.h @@ -4,20 +4,34 @@ #include #include #include +#include struct wlr_xwm; struct wlr_xwayland { - pid_t pid; - int display; - int x_fd[2], wl_fd[2], wm_fd[2]; - struct wl_client *client; + pid_t pid; + int display; + int x_fd[2], wl_fd[2], wm_fd[2]; + struct wl_client *client; struct wl_display *wl_display; struct wlr_compositor *compositor; time_t server_start; struct wl_event_source *sigusr1_source; struct wlr_xwm *xwm; + struct wl_list displayable_windows; +}; + +struct wlr_x11_window { + xcb_window_t window_id; + uint32_t surface_id; + struct wl_list link; + + struct wl_resource *surface; + struct wl_listener surface_destroy_listener; + int16_t x, y; + uint16_t width, height; + bool override_redirect; }; void wlr_xwayland_destroy(struct wlr_xwayland *wlr_xwayland); diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index 764c0135..aa1c3f96 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -161,6 +161,7 @@ static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, wlr_xwayland->x_fd[0] = wlr_xwayland->x_fd[1] = -1; wlr_xwayland->wl_fd[0] = wlr_xwayland->wl_fd[1] = -1; wlr_xwayland->wm_fd[0] = wlr_xwayland->wm_fd[1] = -1; + wl_list_init(&wlr_xwayland->displayable_windows); wlr_xwayland->display = open_display_sockets(wlr_xwayland->x_fd); if (wlr_xwayland->display < 0) { diff --git a/xwayland/xwm.c b/xwayland/xwm.c index b9653f14..bcc5ea05 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -19,7 +19,7 @@ static struct wlr_x11_window *lookup_window(struct wl_list *list, xcb_window_t w } static struct wlr_x11_window *lookup_window_any(struct wlr_xwm *xwm, xcb_window_t window_id) { struct wlr_x11_window *window; - if ((window = lookup_window(&xwm->paired_windows, window_id)) || + if ((window = lookup_window(&xwm->xwayland->displayable_windows, window_id)) || (window = lookup_window(&xwm->unpaired_windows, window_id)) || (window = lookup_window(&xwm->new_windows, window_id))) { return window; @@ -71,10 +71,10 @@ static void map_shell_surface(struct wlr_xwm *xwm, struct wlr_x11_window *window struct wlr_surface *surface) { // get xcb geometry for depth = alpha channel - // TODO link to compositor somehow + window->surface = surface->resource; wl_list_remove(&window->link); - wl_list_insert(&xwm->paired_windows, &window->link); + wl_list_insert(&xwm->xwayland->displayable_windows, &window->link); } /* xcb event handlers */ @@ -96,7 +96,7 @@ static void handle_destroy_notify(struct wlr_xwm *xwm, xcb_destroy_notify_event_ static void handle_configure_request(struct wlr_xwm *xwm, xcb_configure_request_event_t *ev) { struct wlr_x11_window *window; wlr_log(L_DEBUG, "XCB_CONFIGURE_REQUEST (%u) [%ux%u+%d,%d]", ev->window, - ev->width, ev->height, ev->x, ev->y); + ev->width, ev->height, ev->x, ev->y); if (!(window = lookup_window_any(xwm, ev->window))) { return; } @@ -211,7 +211,7 @@ static int x11_event_handler(int fd, uint32_t mask, void *data) { static void create_surface_handler(struct wl_listener *listener, void *data) { struct wlr_surface *surface = data; - struct wlr_xwm *xwm = wl_container_of(listener, xwm, surface_create_listener); + struct wlr_xwm *xwm = wl_container_of(listener, xwm, surface_create_listener); struct wlr_x11_window *window; uint32_t surface_id; @@ -308,7 +308,6 @@ struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) { xwm->xwayland = wlr_xwayland; wl_list_init(&xwm->new_windows); wl_list_init(&xwm->unpaired_windows); - wl_list_init(&xwm->paired_windows); xwm->xcb_conn = xcb_connect_to_fd(wlr_xwayland->wm_fd[0], NULL); if ((rc = xcb_connection_has_error(xwm->xcb_conn))) { @@ -317,7 +316,7 @@ struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) { return NULL; } - struct wl_event_loop *event_loop = wl_display_get_event_loop(wlr_xwayland->wl_display); + struct wl_event_loop *event_loop = wl_display_get_event_loop(wlr_xwayland->wl_display); xwm->event_source = wl_event_loop_add_fd(event_loop, wlr_xwayland->wm_fd[0], WL_EVENT_READABLE, x11_event_handler, xwm); // probably not needed diff --git a/xwayland/xwm.h b/xwayland/xwm.h index 51d60395..235145b9 100644 --- a/xwayland/xwm.h +++ b/xwayland/xwm.h @@ -1,6 +1,5 @@ #ifndef XWAYLAND_INTERNALS_H #define XWAYLAND_INTERNALS_H -#include #include #include @@ -65,18 +64,6 @@ static const char * const atom_map[ATOM_LAST] = { "_NET_WM_STATE", }; -struct wlr_x11_window { - xcb_window_t window_id; - uint32_t surface_id; - struct wl_list link; - - struct wl_resource *surface; - struct wl_listener surface_destroy_listener; - int16_t x, y; - uint16_t width, height; - bool override_redirect; -}; - struct wlr_xwm { struct wlr_xwayland *xwayland; struct wl_event_source *event_source; @@ -89,7 +76,6 @@ struct wlr_xwm { struct wl_list new_windows; struct wl_list unpaired_windows; - struct wl_list paired_windows; }; void xwm_destroy(struct wlr_xwm *xwm); From a39b091bd9575b3dcca9bd3f0b952b078ca287a6 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 14:18:43 +0200 Subject: [PATCH 11/17] xwm: reply to configure_requests --- xwayland/xwm.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/xwayland/xwm.c b/xwayland/xwm.c index bcc5ea05..843b296b 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -106,6 +106,12 @@ static void handle_configure_request(struct wlr_xwm *xwm, xcb_configure_request_ window->width = ev->width; window->height = ev->height; // handle parent/sibling? + + uint32_t values[] = { ev->x, ev->y, ev->width, ev->height, 0 }; + uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | + XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT | + XCB_CONFIG_WINDOW_BORDER_WIDTH; + xcb_configure_window(xwm->xcb_conn, ev->window, mask, values); } static void handle_map_request(struct wlr_xwm *xwm, xcb_map_request_event_t *ev) { From 3391e5b634ebabf53dac16eee332068ea4d578f3 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 19:50:28 +0200 Subject: [PATCH 12/17] xwayland: fix style issues --- include/wlr/types/wlr_compositor.h | 4 +- types/wlr_compositor.c | 4 +- xwayland/xwayland.c | 81 +++++++++++++++++++----------- xwayland/xwm.c | 17 ++++--- 4 files changed, 66 insertions(+), 40 deletions(-) diff --git a/include/wlr/types/wlr_compositor.h b/include/wlr/types/wlr_compositor.h index d8a2506f..58a93760 100644 --- a/include/wlr/types/wlr_compositor.h +++ b/include/wlr/types/wlr_compositor.h @@ -10,7 +10,9 @@ struct wlr_compositor { struct wl_list surfaces; struct wl_listener destroy_surface_listener; - struct wl_signal create_surface_signal; + struct { + struct wl_signal create_surface; + } events; }; void wlr_compositor_destroy(struct wlr_compositor *wlr_compositor); diff --git a/types/wlr_compositor.c b/types/wlr_compositor.c index 6b6312d1..0658f65e 100644 --- a/types/wlr_compositor.c +++ b/types/wlr_compositor.c @@ -22,7 +22,7 @@ static void wl_compositor_create_surface(struct wl_client *client, wl_resource_add_destroy_listener(surface_resource, &surface->compositor_listener); wl_list_insert(&compositor->surfaces, wl_resource_get_link(surface_resource)); - wl_signal_emit(&compositor->create_surface_signal, surface); + wl_signal_emit(&compositor->events.create_surface, surface); } static void wl_compositor_create_region(struct wl_client *client, @@ -81,6 +81,6 @@ struct wlr_compositor *wlr_compositor_create(struct wl_display *display, compositor->renderer = renderer; wl_list_init(&compositor->wl_resources); wl_list_init(&compositor->surfaces); - wl_signal_init(&compositor->create_surface_signal); + wl_signal_init(&compositor->events.create_surface); return compositor; } diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index aa1c3f96..a28073d8 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -1,5 +1,5 @@ #define _XOPEN_SOURCE 700 -#define _GNU_SOURCE +#define _DEFAULT_SOURCE #include #include #include @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include "wlr/util/log.h" @@ -28,6 +29,27 @@ static int unset_cloexec(int fd) { return 0; } +static int fill_arg(char ***argv, const char *fmt, ...) { + int len; + char **cur_arg = *argv; + va_list args; + va_start(args, fmt); + len = vsnprintf(NULL, 0, fmt, args) + 1; + va_end(args); + while (*cur_arg) { + cur_arg++; + } + *cur_arg = malloc(len); + if (!*cur_arg) { + return -1; + } + *argv = cur_arg; + va_start(args, fmt); + len = vsnprintf(*cur_arg, len, fmt, args); + va_end(args); + return len; +} + static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { if (unset_cloexec(wlr_xwayland->x_fd[0]) || unset_cloexec(wlr_xwayland->x_fd[1]) || @@ -39,27 +61,20 @@ static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { /* Make Xwayland signal us when it's ready */ signal(SIGUSR1, SIG_IGN); - char *argv[11] = { 0 }; - argv[0] = "Xwayland"; - if (asprintf(&argv[1], ":%d", wlr_xwayland->display) < 0) { - wlr_log_errno(L_ERROR, "asprintf failed"); - exit(EXIT_FAILURE); - } - argv[2] = "-rootless"; - argv[3] = "-terminate"; - argv[4] = "-listen"; - if (asprintf(&argv[5], "%d", wlr_xwayland->x_fd[0]) < 0) { - wlr_log_errno(L_ERROR, "asprintf failed"); - exit(EXIT_FAILURE); - } - argv[6] = "-listen"; - if (asprintf(&argv[7], "%d", wlr_xwayland->x_fd[1]) < 0) { - wlr_log_errno(L_ERROR, "asprintf failed"); - exit(EXIT_FAILURE); - } - argv[8] = "-wm"; - if (asprintf(&argv[9], "%d", wlr_xwayland->wm_fd[1]) < 0) { - wlr_log_errno(L_ERROR, "asprintf failed"); + char *argv[] = { + "Xwayland", NULL /* display, e.g. :1 */, + "-rootless", "-terminate", + "-listen", NULL /* x_fd[0] */, + "-listen", NULL /* x_fd[1] */, + "-wm", NULL /* wm_fd[1] */, + NULL }; + char **cur_arg = argv; + + if (fill_arg(&cur_arg, ":%d", wlr_xwayland->display) < 0 || + fill_arg(&cur_arg, "%d", wlr_xwayland->x_fd[0]) < 0 || + fill_arg(&cur_arg, "%d", wlr_xwayland->x_fd[1]) < 0 || + fill_arg(&cur_arg, "%d", wlr_xwayland->wm_fd[1]) < 0) { + wlr_log_errno(L_ERROR, "alloc/print failure"); exit(EXIT_FAILURE); } @@ -69,12 +84,14 @@ static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { exit(EXIT_FAILURE); } - char *envp[3] = { 0 }; - if (asprintf(&envp[0], "XDG_RUNTIME_DIR=%s", xdg_runtime) < 0 || - asprintf(&envp[1], "WAYLAND_SOCKET=%d", wlr_xwayland->wl_fd[1]) < 0) { - wlr_log_errno(L_ERROR, "asprintf failed"); + if (clearenv()) { + wlr_log_errno(L_ERROR, "clearenv failed"); exit(EXIT_FAILURE); } + setenv("XDG_RUNTIME_DIR", xdg_runtime, true); + char wayland_socket_str[16]; + snprintf(wayland_socket_str, sizeof(wayland_socket_str), "%d", wlr_xwayland->wl_fd[1]); + setenv("WAYLAND_SOCKET", wayland_socket_str, true); wlr_log(L_INFO, "Xwayland :%d -rootless -terminate -listen %d -listen %d -wm %d", wlr_xwayland->display, wlr_xwayland->x_fd[0], wlr_xwayland->x_fd[1], @@ -82,7 +99,7 @@ static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { // TODO: close stdout/err depending on log level - execvpe("Xwayland", argv, envp); + execvp("Xwayland", argv); } static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, @@ -108,7 +125,6 @@ static struct wl_listener xwayland_destroy_listener = { }; static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { - if (wlr_xwayland->client) { wl_list_remove(&xwayland_destroy_listener.link); wl_client_destroy(wlr_xwayland->client); @@ -118,6 +134,9 @@ static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { wl_event_source_remove(wlr_xwayland->sigusr1_source); } + // TODO: destroy all these windows, for now just cleanup + wl_list_init(&wlr_xwayland->displayable_windows); + xwm_destroy(wlr_xwayland->xwm); safe_close(wlr_xwayland->x_fd[0]); @@ -129,7 +148,9 @@ static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { unlink_display_sockets(wlr_xwayland->display); unsetenv("DISPLAY"); - /* kill Xwayland process? */ + /* We do not kill the Xwayland process, it dies because of broken pipe + * after we close our side of the wm/wl fds. This is more reliable than + * trying to kill something that might no longer be Xwayland */ } static int xserver_handle_ready(int signal_number, void *data) { @@ -150,7 +171,7 @@ static int xserver_handle_ready(int signal_number, void *data) { snprintf(display_name, sizeof(display_name), ":%d", wlr_xwayland->display); setenv("DISPLAY", display_name, true); - return 1; + return 1; /* wayland event loop dispatcher's count */ } static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, diff --git a/xwayland/xwm.c b/xwayland/xwm.c index 843b296b..ff22b089 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -69,7 +69,6 @@ static bool xcb_call(struct wlr_xwm *xwm, const char *func, uint32_t line, static void map_shell_surface(struct wlr_xwm *xwm, struct wlr_x11_window *window, struct wlr_surface *surface) { - // get xcb geometry for depth = alpha channel window->surface = surface->resource; @@ -206,7 +205,8 @@ static int x11_event_handler(int fd, uint32_t mask, void *data) { break; default: wlr_log(L_DEBUG, "X11 event: %d", - event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK); + event->response_type & XCB_EVENT_RESPONSE_TYPE_MASK); + break; } } @@ -214,8 +214,7 @@ static int x11_event_handler(int fd, uint32_t mask, void *data) { return count; } -static void create_surface_handler(struct wl_listener *listener, void *data) -{ +static void create_surface_handler(struct wl_listener *listener, void *data) { struct wlr_surface *surface = data; struct wlr_xwm *xwm = wl_container_of(listener, xwm, surface_create_listener); struct wlr_x11_window *window; @@ -272,8 +271,12 @@ static void xcb_init_wm(struct wlr_xwm *xwm) { xwm->window = xcb_generate_id(xwm->xcb_conn); - uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_PROPERTY_CHANGE, - /* , xwm->cursor */ }; + uint32_t values[] = { + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | + XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | + XCB_EVENT_MASK_PROPERTY_CHANGE, + /* xwm->cursor, */ + }; XCB_CALL(xwm, xcb_change_window_attributes_checked(xwm->xcb_conn, xwm->screen->root, XCB_CW_EVENT_MASK /* | XCB_CW_CURSOR */, values)); XCB_CALL(xwm, xcb_composite_redirect_subwindows_checked(xwm->xcb_conn, @@ -334,7 +337,7 @@ struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland) { xcb_init_wm(xwm); xwm->surface_create_listener.notify = create_surface_handler; - wl_signal_add(&wlr_xwayland->compositor->create_surface_signal, + wl_signal_add(&wlr_xwayland->compositor->events.create_surface, &xwm->surface_create_listener); return xwm; From 0196284331fc249293a9b6a74ba9f71ccdc681dd Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 23:06:13 +0200 Subject: [PATCH 13/17] xwm: remove xcb-util dependency This hardcodes a define, but other projects (wlc/weston) either use the raw number or just redefine it with another name anyway... This should fix travis build. --- xwayland/xwm.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xwayland/xwm.c b/xwayland/xwm.c index ff22b089..02794e72 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -1,5 +1,4 @@ #include -#include #include #include "wlr/util/log.h" #include "wlr/types/wlr_surface.h" @@ -171,6 +170,10 @@ static void handle_client_message(struct wlr_xwm *xwm, xcb_client_message_event_ wlr_log(L_DEBUG, "unhandled client message %u", ev->type); } +/* 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; From fd3ad3b9e42d9f2d1196a5c36b07763283729053 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Wed, 23 Aug 2017 23:11:44 +0200 Subject: [PATCH 14/17] xwayland: fix shutdown caused by Xwayland/client stop --- include/wlr/xwayland.h | 1 + xwayland/xwayland.c | 12 ++++-------- xwayland/xwm.c | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/include/wlr/xwayland.h b/include/wlr/xwayland.h index 4d1a726d..804e9962 100644 --- a/include/wlr/xwayland.h +++ b/include/wlr/xwayland.h @@ -18,6 +18,7 @@ struct wlr_xwayland { time_t server_start; struct wl_event_source *sigusr1_source; + struct wl_listener destroy_listener; struct wlr_xwm *xwm; struct wl_list displayable_windows; }; diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index a28073d8..9c0b1839 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -107,8 +107,7 @@ static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland); static void xwayland_destroy_event(struct wl_listener *listener, void *data) { - struct wl_client *client = data; - struct wlr_xwayland *wlr_xwayland = wl_container_of(client, wlr_xwayland, client); + struct wlr_xwayland *wlr_xwayland = wl_container_of(listener, wlr_xwayland, destroy_listener); /* don't call client destroy */ wlr_xwayland->client = NULL; @@ -120,13 +119,9 @@ static void xwayland_destroy_event(struct wl_listener *listener, void *data) { } } -static struct wl_listener xwayland_destroy_listener = { - .notify = xwayland_destroy_event, -}; - static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { if (wlr_xwayland->client) { - wl_list_remove(&xwayland_destroy_listener.link); + wl_list_remove(&wlr_xwayland->destroy_listener.link); wl_client_destroy(wlr_xwayland->client); } @@ -224,7 +219,8 @@ static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, return false; } - wl_client_add_destroy_listener(wlr_xwayland->client, &xwayland_destroy_listener); + wlr_xwayland->destroy_listener.notify = xwayland_destroy_event; + wl_client_add_destroy_listener(wlr_xwayland->client, &wlr_xwayland->destroy_listener); struct wl_event_loop *loop = wl_display_get_event_loop(wl_display); wlr_xwayland->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1, xserver_handle_ready, wlr_xwayland); diff --git a/xwayland/xwm.c b/xwayland/xwm.c index 02794e72..0e161330 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -240,7 +240,7 @@ static void create_surface_handler(struct wl_listener *listener, void *data) { } static void xcb_get_resources(struct wlr_xwm *xwm) { - int i; + size_t i; xcb_intern_atom_cookie_t cookies[ATOM_LAST]; for (i = 0; i < ATOM_LAST; i++) { From e3143b50b6363bb7408e30aa64acdbdcd9b249d9 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Wed, 23 Aug 2017 23:19:15 +0200 Subject: [PATCH 15/17] xwayland: fix some shutdown cases --- xwayland/sockets.c | 12 +++++++++--- xwayland/xwayland.c | 26 +++++++++++++++++--------- xwayland/xwm.c | 15 +++++++++------ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/xwayland/sockets.c b/xwayland/sockets.c index ca46ec9f..48bcc822 100644 --- a/xwayland/sockets.c +++ b/xwayland/sockets.c @@ -24,7 +24,9 @@ static int open_socket(struct sockaddr_un *addr, size_t path_size) { fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); if (fd < 0) { - wlr_log_errno(L_ERROR, "Failed to create socket %s", addr->sun_path); + wlr_log_errno(L_DEBUG, "Failed to create socket %c%s", + addr->sun_path[0] ? addr->sun_path[0] : '@', + addr->sun_path + 1); return -1; } @@ -33,12 +35,16 @@ static int open_socket(struct sockaddr_un *addr, size_t path_size) { } if (bind(fd, (struct sockaddr*)addr, size) < 0) { rc = errno; - wlr_log_errno(L_ERROR, "Failed to bind socket %s", addr->sun_path); + wlr_log_errno(L_DEBUG, "Failed to bind socket %c%s", + addr->sun_path[0] ? addr->sun_path[0] : '@', + addr->sun_path + 1); goto cleanup; } if (listen(fd, 1) < 0) { rc = errno; - wlr_log_errno(L_ERROR, "Failed to listen to socket %s", addr->sun_path); + wlr_log_errno(L_DEBUG, "Failed to listen to socket %c%s", + addr->sun_path[0] ? addr->sun_path[0] : '@', + addr->sun_path + 1); goto cleanup; } diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index 9c0b1839..9ac94e72 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include @@ -93,9 +95,9 @@ static void exec_xwayland(struct wlr_xwayland *wlr_xwayland) { snprintf(wayland_socket_str, sizeof(wayland_socket_str), "%d", wlr_xwayland->wl_fd[1]); setenv("WAYLAND_SOCKET", wayland_socket_str, true); - wlr_log(L_INFO, "Xwayland :%d -rootless -terminate -listen %d -listen %d -wm %d", - wlr_xwayland->display, wlr_xwayland->x_fd[0], wlr_xwayland->x_fd[1], - wlr_xwayland->wm_fd[1]); + wlr_log(L_INFO, "WAYLAND_SOCKET=%d Xwayland :%d -rootless -terminate -listen %d -listen %d -wm %d", + wlr_xwayland->wl_fd[1], wlr_xwayland->display, wlr_xwayland->x_fd[0], + wlr_xwayland->x_fd[1], wlr_xwayland->wm_fd[1]); // TODO: close stdout/err depending on log level @@ -111,20 +113,23 @@ static void xwayland_destroy_event(struct wl_listener *listener, void *data) { /* don't call client destroy */ wlr_xwayland->client = NULL; + wl_list_remove(&wlr_xwayland->destroy_listener.link); wlr_xwayland_finish(wlr_xwayland); - if (wlr_xwayland->server_start - time(NULL) > 5) { + if (time(NULL) - wlr_xwayland->server_start > 5) { wlr_xwayland_init(wlr_xwayland, wlr_xwayland->wl_display, - wlr_xwayland->compositor); + wlr_xwayland->compositor); } } static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { + if (!wlr_xwayland || wlr_xwayland->display == -1) { + return; + } if (wlr_xwayland->client) { wl_list_remove(&wlr_xwayland->destroy_listener.link); wl_client_destroy(wlr_xwayland->client); } - if (wlr_xwayland->sigusr1_source) { wl_event_source_remove(wlr_xwayland->sigusr1_source); } @@ -142,10 +147,13 @@ static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { safe_close(wlr_xwayland->wm_fd[1]); unlink_display_sockets(wlr_xwayland->display); + wlr_xwayland->display = -1; unsetenv("DISPLAY"); - /* We do not kill the Xwayland process, it dies because of broken pipe - * after we close our side of the wm/wl fds. This is more reliable than - * trying to kill something that might no longer be Xwayland */ + /* We do not kill the Xwayland process, it dies to broken pipe + * after we close our side of the wm/wl fds. This is more reliable + * than trying to kill something that might no longer be Xwayland. + */ + // TODO: figure how to wait for dying process though. Probably handle SIGCHILD } static int xserver_handle_ready(int signal_number, void *data) { diff --git a/xwayland/xwm.c b/xwayland/xwm.c index 0e161330..3cd5b22b 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -61,7 +61,7 @@ static bool xcb_call(struct wlr_xwm *xwm, const char *func, uint32_t line, } wlr_log(L_ERROR, "xcb call failed in %s:%u, x11 error code %d", - func, line, error->error_code); + func, line, error->error_code); free(error); return false; } @@ -79,7 +79,7 @@ static void map_shell_surface(struct wlr_xwm *xwm, struct wlr_x11_window *window static void handle_create_notify(struct wlr_xwm *xwm, xcb_create_notify_event_t *ev) { wlr_log(L_DEBUG, "XCB_CREATE_NOTIFY (%u)", ev->window); wlr_x11_window_create(xwm, ev->window, ev->x, ev->y, - ev->width, ev->height, ev->override_redirect); + ev->width, ev->height, ev->override_redirect); } static void handle_destroy_notify(struct wlr_xwm *xwm, xcb_destroy_notify_event_t *ev) { @@ -94,7 +94,7 @@ static void handle_destroy_notify(struct wlr_xwm *xwm, xcb_destroy_notify_event_ static void handle_configure_request(struct wlr_xwm *xwm, xcb_configure_request_event_t *ev) { struct wlr_x11_window *window; wlr_log(L_DEBUG, "XCB_CONFIGURE_REQUEST (%u) [%ux%u+%d,%d]", ev->window, - ev->width, ev->height, ev->x, ev->y); + ev->width, ev->height, ev->x, ev->y); if (!(window = lookup_window_any(xwm, ev->window))) { return; } @@ -154,7 +154,7 @@ static void handle_client_message(struct wlr_xwm *xwm, xcb_client_message_event_ window = lookup_window(&xwm->new_windows, ev->window); if (!window) { wlr_log(L_DEBUG, "client message WL_SURFACE_ID but no new window %u ?", - ev->window); + ev->window); return; } window->surface_id = ev->data.data32[0]; @@ -260,7 +260,7 @@ static void xcb_get_resources(struct wlr_xwm *xwm) { } if (error) { wlr_log(L_ERROR, "could not resolve atom %s, x11 error code %d", - atom_map[i], error->error_code); + atom_map[i], error->error_code); free(error); return; } @@ -304,10 +304,13 @@ static void xcb_init_wm(struct wlr_xwm *xwm) { } void xwm_destroy(struct wlr_xwm *xwm) { + if (!xwm) { + return; + } if (xwm->event_source) { wl_event_source_remove(xwm->event_source); } - + wl_list_remove(&xwm->surface_create_listener.link); xcb_disconnect(xwm->xcb_conn); free(xwm); From 4a288fdacbfec518b1caa3b413b869cd389d54d4 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Wed, 23 Aug 2017 23:39:16 +0200 Subject: [PATCH 16/17] xwm: free x11 windows on finish --- xwayland/xwayland.c | 3 --- xwayland/xwm.c | 10 ++++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index 9ac94e72..f30b18d0 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -134,9 +134,6 @@ static void wlr_xwayland_finish(struct wlr_xwayland *wlr_xwayland) { wl_event_source_remove(wlr_xwayland->sigusr1_source); } - // TODO: destroy all these windows, for now just cleanup - wl_list_init(&wlr_xwayland->displayable_windows); - xwm_destroy(wlr_xwayland->xwm); safe_close(wlr_xwayland->x_fd[0]); diff --git a/xwayland/xwm.c b/xwayland/xwm.c index 3cd5b22b..3fa6cb98 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -310,6 +310,16 @@ void xwm_destroy(struct wlr_xwm *xwm) { if (xwm->event_source) { wl_event_source_remove(xwm->event_source); } + struct wlr_x11_window *window, *tmp; + wl_list_for_each_safe(window, tmp, &xwm->xwayland->displayable_windows, link) { + wlr_x11_window_destroy(window); + } + wl_list_for_each_safe(window, tmp, &xwm->new_windows, link) { + wlr_x11_window_destroy(window); + } + wl_list_for_each_safe(window, tmp, &xwm->unpaired_windows, link) { + wlr_x11_window_destroy(window); + } wl_list_remove(&xwm->surface_create_listener.link); xcb_disconnect(xwm->xcb_conn); From b29c7d01b19fc8c6bda70af016bc536cc72fe4f9 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Thu, 24 Aug 2017 07:47:43 +0200 Subject: [PATCH 17/17] xwayland: relinquish wl_fd[0] after creating client wayland WILL close this fd during wl_client_destroy, after our handler if we close it as well this will close some of the fd we reopened --- xwayland/xwayland.c | 1 + 1 file changed, 1 insertion(+) diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index f30b18d0..211e2a04 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -223,6 +223,7 @@ static bool wlr_xwayland_init(struct wlr_xwayland *wlr_xwayland, wlr_xwayland_finish(wlr_xwayland); return false; } + wlr_xwayland->wl_fd[0] = -1; /* not ours anymore */ wlr_xwayland->destroy_listener.notify = xwayland_destroy_event; wl_client_add_destroy_listener(wlr_xwayland->client, &wlr_xwayland->destroy_listener);