From 49a823d4c6b41723a9978c19d426e8ca4f5418d7 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 17:59:31 +0200 Subject: Xwayland: first draft, just start server for now --- include/wlr/xwayland.h | 17 +++++++++++++++++ include/xwayland/internals.h | 6 ++++++ 2 files changed, 23 insertions(+) create mode 100644 include/wlr/xwayland.h create mode 100644 include/xwayland/internals.h (limited to 'include') 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 -- cgit v1.2.3 From b2bab1af5c34b785a9b08db04af1330e61dd210c Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 21:25:26 +0200 Subject: 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 (limited to 'include') 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; +} -- cgit v1.2.3 From 3cc5b1acc6014ef424630c2ac2ae5b024c2b57e8 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 22:14:51 +0200 Subject: 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(-) (limited to 'include') 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; } -- cgit v1.2.3 From 6eb20fb8d28749e0b710a54daa1b916080cf4ea1 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sat, 19 Aug 2017 23:09:59 +0200 Subject: 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(-) (limited to 'include') 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; } -- cgit v1.2.3 From fa0e1015c6f06f08fe368f89af511ef646f0bc99 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 07:47:38 +0200 Subject: 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(-) (limited to 'include') 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, -- cgit v1.2.3 From eb5b9cc6daa39f6e1bcafae074f832f07b5d0a19 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 07:59:03 +0200 Subject: xwayland: move & split internal header file --- examples/compositor.c | 6 ++-- include/wlr/xwayland.h | 6 ++-- include/xwayland/internals.h | 86 -------------------------------------------- xwayland/sockets.c | 4 +-- xwayland/sockets.h | 7 ++++ xwayland/xwayland.c | 28 ++++++++++++--- xwayland/xwm.c | 4 +-- xwayland/xwm.h | 83 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 123 insertions(+), 101 deletions(-) delete mode 100644 include/xwayland/internals.h create mode 100644 xwayland/sockets.h create mode 100644 xwayland/xwm.h (limited to 'include') 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/include/xwayland/internals.h b/include/xwayland/internals.h deleted file mode 100644 index 97c7e31a..00000000 --- a/include/xwayland/internals.h +++ /dev/null @@ -1,86 +0,0 @@ -#ifndef XWAYLAND_INTERNALS_H -#define XWAYLAND_INTERNALS_H -#include -#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_atom_t atoms[ATOM_LAST]; - xcb_connection_t *xcb_conn; - xcb_screen_t *screen; - 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); - -#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/xwayland/xwm.h b/xwayland/xwm.h new file mode 100644 index 00000000..cfdd95f5 --- /dev/null +++ b/xwayland/xwm.h @@ -0,0 +1,83 @@ +#ifndef XWAYLAND_INTERNALS_H +#define XWAYLAND_INTERNALS_H +#include +#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_atom_t atoms[ATOM_LAST]; + xcb_connection_t *xcb_conn; + xcb_screen_t *screen; + xcb_window_t window; +}; + +void xwm_destroy(struct wlr_xwm *xwm); +struct wlr_xwm *xwm_create(struct wlr_xwayland *wlr_xwayland); + +#endif -- cgit v1.2.3 From 1458a95e65248fede646051d2607c4553e8dcb0c Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 14:18:29 +0200 Subject: 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(-) (limited to 'include') 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); -- cgit v1.2.3 From 3391e5b634ebabf53dac16eee332068ea4d578f3 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 20 Aug 2017 19:50:28 +0200 Subject: 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(-) (limited to 'include') 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; -- cgit v1.2.3 From fd3ad3b9e42d9f2d1196a5c36b07763283729053 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Wed, 23 Aug 2017 23:11:44 +0200 Subject: 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(-) (limited to 'include') 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++) { -- cgit v1.2.3