diff options
Diffstat (limited to 'sway/desktop/xwayland.c')
-rw-r--r-- | sway/desktop/xwayland.c | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c new file mode 100644 index 00000000..04ec118d --- /dev/null +++ b/sway/desktop/xwayland.c | |||
@@ -0,0 +1,134 @@ | |||
1 | #define _POSIX_C_SOURCE 199309L | ||
2 | #include <stdbool.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <wayland-server.h> | ||
5 | #include <wlr/xwayland.h> | ||
6 | #include "sway/container.h" | ||
7 | #include "sway/layout.h" | ||
8 | #include "sway/server.h" | ||
9 | #include "sway/view.h" | ||
10 | #include "log.h" | ||
11 | |||
12 | static bool assert_xwayland(struct sway_view *view) { | ||
13 | return sway_assert(view->type == SWAY_XWAYLAND_VIEW, | ||
14 | "Expected xwayland view!"); | ||
15 | } | ||
16 | |||
17 | static const char *get_prop(struct sway_view *view, enum sway_view_prop prop) { | ||
18 | if (!assert_xwayland(view)) { | ||
19 | return NULL; | ||
20 | } | ||
21 | switch (prop) { | ||
22 | case VIEW_PROP_TITLE: | ||
23 | return view->wlr_xwayland_surface->title; | ||
24 | case VIEW_PROP_CLASS: | ||
25 | return view->wlr_xwayland_surface->class; | ||
26 | default: | ||
27 | return NULL; | ||
28 | } | ||
29 | } | ||
30 | |||
31 | static void set_size(struct sway_view *view, int width, int height) { | ||
32 | if (!assert_xwayland(view)) { | ||
33 | return; | ||
34 | } | ||
35 | view->sway_xwayland_surface->pending_width = width; | ||
36 | view->sway_xwayland_surface->pending_height = height; | ||
37 | |||
38 | struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; | ||
39 | wlr_xwayland_surface_configure(xsurface, view->swayc->x, view->swayc->y, | ||
40 | width, height); | ||
41 | } | ||
42 | |||
43 | static void handle_commit(struct wl_listener *listener, void *data) { | ||
44 | struct sway_xwayland_surface *sway_surface = | ||
45 | wl_container_of(listener, sway_surface, commit); | ||
46 | struct sway_view *view = sway_surface->view; | ||
47 | sway_log(L_DEBUG, "xwayland surface commit %dx%d", | ||
48 | sway_surface->pending_width, sway_surface->pending_height); | ||
49 | // NOTE: We intentionally discard the view's desired width here | ||
50 | // TODO: Let floating views do whatever | ||
51 | view->width = sway_surface->pending_width; | ||
52 | view->height = sway_surface->pending_height; | ||
53 | } | ||
54 | |||
55 | static void handle_destroy(struct wl_listener *listener, void *data) { | ||
56 | struct sway_xwayland_surface *sway_surface = | ||
57 | wl_container_of(listener, sway_surface, destroy); | ||
58 | wl_list_remove(&sway_surface->commit.link); | ||
59 | wl_list_remove(&sway_surface->destroy.link); | ||
60 | wl_list_remove(&sway_surface->request_configure.link); | ||
61 | swayc_t *parent = destroy_view(sway_surface->view->swayc); | ||
62 | free(sway_surface->view); | ||
63 | free(sway_surface); | ||
64 | arrange_windows(parent, -1, -1); | ||
65 | } | ||
66 | |||
67 | static void handle_configure_request(struct wl_listener *listener, void *data) { | ||
68 | struct sway_xwayland_surface *sway_surface = | ||
69 | wl_container_of(listener, sway_surface, request_configure); | ||
70 | struct wlr_xwayland_surface_configure_event *ev = data; | ||
71 | struct sway_view *view = sway_surface->view; | ||
72 | struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; | ||
73 | // TODO: floating windows are allowed to move around like this, but make | ||
74 | // sure tiling windows always stay in place. | ||
75 | wlr_xwayland_surface_configure(xsurface, ev->x, ev->y, | ||
76 | ev->width, ev->height); | ||
77 | } | ||
78 | |||
79 | void handle_xwayland_surface(struct wl_listener *listener, void *data) { | ||
80 | struct sway_server *server = wl_container_of( | ||
81 | listener, server, xwayland_surface); | ||
82 | struct wlr_xwayland_surface *xsurface = data; | ||
83 | |||
84 | if (xsurface->override_redirect) { | ||
85 | // TODO: floating popups | ||
86 | return; | ||
87 | } | ||
88 | |||
89 | sway_log(L_DEBUG, "New xwayland surface title='%s' class='%s'", | ||
90 | xsurface->title, xsurface->class); | ||
91 | |||
92 | struct sway_xwayland_surface *sway_surface = | ||
93 | calloc(1, sizeof(struct sway_xwayland_surface)); | ||
94 | if (!sway_assert(sway_surface, "Failed to allocate surface!")) { | ||
95 | return; | ||
96 | } | ||
97 | |||
98 | struct sway_view *sway_view = calloc(1, sizeof(struct sway_view)); | ||
99 | if (!sway_assert(sway_view, "Failed to allocate view!")) { | ||
100 | return; | ||
101 | } | ||
102 | sway_view->type = SWAY_XWAYLAND_VIEW; | ||
103 | sway_view->iface.get_prop = get_prop; | ||
104 | sway_view->iface.set_size = set_size; | ||
105 | sway_view->wlr_xwayland_surface = xsurface; | ||
106 | sway_view->sway_xwayland_surface = sway_surface; | ||
107 | // TODO remove from the tree when the surface goes away (unmapped) | ||
108 | sway_view->surface = xsurface->surface; | ||
109 | sway_surface->view = sway_view; | ||
110 | |||
111 | // TODO: | ||
112 | // - Wire up listeners | ||
113 | // - Handle popups | ||
114 | // - Look up pid and open on appropriate workspace | ||
115 | // - Set new view to maximized so it behaves nicely | ||
116 | // - Criteria | ||
117 | |||
118 | sway_surface->commit.notify = handle_commit; | ||
119 | wl_signal_add(&xsurface->surface->events.commit, &sway_surface->commit); | ||
120 | sway_surface->destroy.notify = handle_destroy; | ||
121 | wl_signal_add(&xsurface->events.destroy, &sway_surface->destroy); | ||
122 | sway_surface->request_configure.notify = handle_configure_request; | ||
123 | wl_signal_add(&xsurface->events.request_configure, | ||
124 | &sway_surface->request_configure); | ||
125 | |||
126 | // TODO: actual focus semantics | ||
127 | swayc_t *parent = root_container.children->items[0]; | ||
128 | parent = parent->children->items[0]; // workspace | ||
129 | |||
130 | swayc_t *cont = new_view(parent, sway_view); | ||
131 | sway_view->swayc = cont; | ||
132 | |||
133 | arrange_windows(cont->parent, -1, -1); | ||
134 | } | ||