Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 3 | * |
Bryce Harrington | 0a007dd | 2015-06-11 16:22:34 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 11 | * |
Bryce Harrington | 0a007dd | 2015-06-11 16:22:34 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 24 | */ |
| 25 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 26 | #include "config.h" |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 27 | |
| 28 | #include <stdlib.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 29 | #include <stdint.h> |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/un.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <errno.h> |
| 36 | #include <unistd.h> |
| 37 | #include <signal.h> |
| 38 | |
| 39 | #include "xwayland.h" |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 40 | #include "xwayland-api.h" |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 41 | #include "shared/helpers.h" |
Bryce Harrington | 25a2bdd | 2016-08-03 17:40:52 -0700 | [diff] [blame] | 42 | #include "shared/string-helpers.h" |
Pekka Paalanen | 58f98c9 | 2016-06-03 16:45:21 +0300 | [diff] [blame] | 43 | #include "compositor/weston.h" |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 44 | |
| 45 | static int |
| 46 | weston_xserver_handle_event(int listen_fd, uint32_t mask, void *data) |
| 47 | { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 48 | struct weston_xserver *wxs = data; |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 49 | char display[8]; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 50 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 51 | snprintf(display, sizeof display, ":%d", wxs->display); |
| 52 | |
| 53 | wxs->pid = wxs->spawn_func(wxs->user_data, display, wxs->abstract_fd, wxs->unix_fd); |
| 54 | if (wxs->pid == -1) { |
| 55 | weston_log("Failed to spawn the Xwayland server\n"); |
Kristian Høgsberg | 757d8af | 2014-03-17 22:33:29 -0700 | [diff] [blame] | 56 | return 1; |
| 57 | } |
| 58 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 59 | weston_log("Spawned Xwayland server, pid %d\n", wxs->pid); |
| 60 | wl_event_source_remove(wxs->abstract_source); |
| 61 | wl_event_source_remove(wxs->unix_source); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 62 | |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | weston_xserver_shutdown(struct weston_xserver *wxs) |
| 68 | { |
| 69 | char path[256]; |
| 70 | |
| 71 | snprintf(path, sizeof path, "/tmp/.X%d-lock", wxs->display); |
| 72 | unlink(path); |
| 73 | snprintf(path, sizeof path, "/tmp/.X11-unix/X%d", wxs->display); |
| 74 | unlink(path); |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 75 | if (wxs->pid == 0) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 76 | wl_event_source_remove(wxs->abstract_source); |
| 77 | wl_event_source_remove(wxs->unix_source); |
| 78 | } |
| 79 | close(wxs->abstract_fd); |
| 80 | close(wxs->unix_fd); |
Dima Ryazanov | 434cc23 | 2014-06-19 01:03:31 -0700 | [diff] [blame] | 81 | if (wxs->wm) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 82 | weston_wm_destroy(wxs->wm); |
Dima Ryazanov | 434cc23 | 2014-06-19 01:03:31 -0700 | [diff] [blame] | 83 | wxs->wm = NULL; |
| 84 | } |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 85 | wxs->loop = NULL; |
| 86 | } |
| 87 | |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 88 | static int |
| 89 | bind_to_abstract_socket(int display) |
| 90 | { |
| 91 | struct sockaddr_un addr; |
| 92 | socklen_t size, name_size; |
| 93 | int fd; |
| 94 | |
| 95 | fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 96 | if (fd < 0) |
| 97 | return -1; |
| 98 | |
| 99 | addr.sun_family = AF_LOCAL; |
| 100 | name_size = snprintf(addr.sun_path, sizeof addr.sun_path, |
| 101 | "%c/tmp/.X11-unix/X%d", 0, display); |
| 102 | size = offsetof(struct sockaddr_un, sun_path) + name_size; |
| 103 | if (bind(fd, (struct sockaddr *) &addr, size) < 0) { |
Jasper St. Pierre | 4783739 | 2014-04-01 19:55:26 -0400 | [diff] [blame] | 104 | weston_log("failed to bind to @%s: %m\n", addr.sun_path + 1); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 105 | close(fd); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | if (listen(fd, 1) < 0) { |
| 110 | close(fd); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | return fd; |
| 115 | } |
| 116 | |
| 117 | static int |
| 118 | bind_to_unix_socket(int display) |
| 119 | { |
| 120 | struct sockaddr_un addr; |
| 121 | socklen_t size, name_size; |
| 122 | int fd; |
| 123 | |
| 124 | fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 125 | if (fd < 0) |
| 126 | return -1; |
| 127 | |
| 128 | addr.sun_family = AF_LOCAL; |
| 129 | name_size = snprintf(addr.sun_path, sizeof addr.sun_path, |
| 130 | "/tmp/.X11-unix/X%d", display) + 1; |
| 131 | size = offsetof(struct sockaddr_un, sun_path) + name_size; |
| 132 | unlink(addr.sun_path); |
| 133 | if (bind(fd, (struct sockaddr *) &addr, size) < 0) { |
Jasper St. Pierre | 4783739 | 2014-04-01 19:55:26 -0400 | [diff] [blame] | 134 | weston_log("failed to bind to %s: %m\n", addr.sun_path); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 135 | close(fd); |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | if (listen(fd, 1) < 0) { |
| 140 | unlink(addr.sun_path); |
| 141 | close(fd); |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | return fd; |
| 146 | } |
| 147 | |
| 148 | static int |
| 149 | create_lockfile(int display, char *lockfile, size_t lsize) |
| 150 | { |
Daniel Stone | 08a35d3 | 2016-11-16 16:37:05 +0000 | [diff] [blame] | 151 | /* 10 decimal characters, trailing LF and NUL byte; see comment |
| 152 | * at end of function. */ |
| 153 | char pid[11]; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 154 | int fd, size; |
| 155 | pid_t other; |
| 156 | |
| 157 | snprintf(lockfile, lsize, "/tmp/.X%d-lock", display); |
| 158 | fd = open(lockfile, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444); |
| 159 | if (fd < 0 && errno == EEXIST) { |
Kristian Høgsberg | 5e647ca | 2014-02-01 20:44:54 -0800 | [diff] [blame] | 160 | fd = open(lockfile, O_CLOEXEC | O_RDONLY); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 161 | if (fd < 0 || read(fd, pid, 11) != 11) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 162 | weston_log("can't read lock file %s: %s\n", |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 163 | lockfile, strerror(errno)); |
Rob Bradford | ef94085 | 2012-12-05 18:47:07 +0000 | [diff] [blame] | 164 | if (fd >= 0) |
| 165 | close (fd); |
| 166 | |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 167 | errno = EEXIST; |
| 168 | return -1; |
| 169 | } |
| 170 | |
Daniel Stone | 08a35d3 | 2016-11-16 16:37:05 +0000 | [diff] [blame] | 171 | /* Trim the trailing LF, or at least ensure it's NULL. */ |
Pekka Paalanen | 61beda6 | 2016-11-14 15:05:28 +0200 | [diff] [blame] | 172 | pid[10] = '\0'; |
| 173 | |
Bryce Harrington | 25a2bdd | 2016-08-03 17:40:52 -0700 | [diff] [blame] | 174 | if (!safe_strtoint(pid, &other)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 175 | weston_log("can't parse lock file %s\n", |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 176 | lockfile); |
| 177 | close(fd); |
| 178 | errno = EEXIST; |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | if (kill(other, 0) < 0 && errno == ESRCH) { |
| 183 | /* stale lock file; unlink and try again */ |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 184 | weston_log("unlinking stale lock file %s\n", lockfile); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 185 | close(fd); |
| 186 | if (unlink(lockfile)) |
| 187 | /* If we fail to unlink, return EEXIST |
| 188 | so we try the next display number.*/ |
| 189 | errno = EEXIST; |
| 190 | else |
| 191 | errno = EAGAIN; |
| 192 | return -1; |
| 193 | } |
| 194 | |
Martin Olsson | 1972141 | 2012-07-08 03:03:45 +0200 | [diff] [blame] | 195 | close(fd); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 196 | errno = EEXIST; |
| 197 | return -1; |
| 198 | } else if (fd < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 199 | weston_log("failed to create lock file %s: %s\n", |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 200 | lockfile, strerror(errno)); |
| 201 | return -1; |
| 202 | } |
| 203 | |
Daniel Stone | 08a35d3 | 2016-11-16 16:37:05 +0000 | [diff] [blame] | 204 | /* Subtle detail: we use the pid of the wayland compositor, not the |
| 205 | * xserver in the lock file. |
| 206 | * Also subtle is that we don't emit a trailing NUL to the file, so |
| 207 | * our size here is 11 rather than 12. */ |
| 208 | size = dprintf(fd, "%10d\n", getpid()); |
| 209 | if (size != 11) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 210 | unlink(lockfile); |
| 211 | close(fd); |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | close(fd); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | weston_xserver_destroy(struct wl_listener *l, void *data) |
| 222 | { |
| 223 | struct weston_xserver *wxs = |
| 224 | container_of(l, struct weston_xserver, destroy_listener); |
| 225 | |
| 226 | if (!wxs) |
| 227 | return; |
| 228 | |
| 229 | if (wxs->loop) |
| 230 | weston_xserver_shutdown(wxs); |
| 231 | |
| 232 | free(wxs); |
| 233 | } |
| 234 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 235 | static struct weston_xwayland * |
| 236 | weston_xwayland_get(struct weston_compositor *compositor) |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 237 | { |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 238 | struct wl_listener *listener; |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 239 | struct weston_xserver *wxs; |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 240 | |
| 241 | listener = wl_signal_get(&compositor->destroy_signal, |
| 242 | weston_xserver_destroy); |
| 243 | if (!listener) |
| 244 | return NULL; |
| 245 | |
| 246 | wxs = wl_container_of(listener, wxs, destroy_listener); |
| 247 | return (struct weston_xwayland *)wxs; |
| 248 | } |
| 249 | |
| 250 | static int |
| 251 | weston_xwayland_listen(struct weston_xwayland *xwayland, void *user_data, |
| 252 | weston_xwayland_spawn_xserver_func_t spawn_func) |
| 253 | { |
| 254 | struct weston_xserver *wxs = (struct weston_xserver *)xwayland; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 255 | char lockfile[256], display_name[8]; |
| 256 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 257 | wxs->user_data = user_data; |
| 258 | wxs->spawn_func = spawn_func; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 259 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 260 | retry: |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 261 | if (create_lockfile(wxs->display, lockfile, sizeof lockfile) < 0) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 262 | if (errno == EAGAIN) { |
| 263 | goto retry; |
| 264 | } else if (errno == EEXIST) { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 265 | wxs->display++; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 266 | goto retry; |
| 267 | } else { |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 268 | free(wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 269 | return -1; |
| 270 | } |
| 271 | } |
| 272 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 273 | wxs->abstract_fd = bind_to_abstract_socket(wxs->display); |
| 274 | if (wxs->abstract_fd < 0 && errno == EADDRINUSE) { |
| 275 | wxs->display++; |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 276 | unlink(lockfile); |
| 277 | goto retry; |
| 278 | } |
| 279 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 280 | wxs->unix_fd = bind_to_unix_socket(wxs->display); |
| 281 | if (wxs->unix_fd < 0) { |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 282 | unlink(lockfile); |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 283 | close(wxs->abstract_fd); |
| 284 | free(wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 288 | snprintf(display_name, sizeof display_name, ":%d", wxs->display); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 289 | weston_log("xserver listening on display %s\n", display_name); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 290 | setenv("DISPLAY", display_name, 1); |
| 291 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 292 | wxs->loop = wl_display_get_event_loop(wxs->wl_display); |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 293 | wxs->abstract_source = |
| 294 | wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 295 | WL_EVENT_READABLE, |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 296 | weston_xserver_handle_event, wxs); |
| 297 | wxs->unix_source = |
| 298 | wl_event_loop_add_fd(wxs->loop, wxs->unix_fd, |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 299 | WL_EVENT_READABLE, |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 300 | weston_xserver_handle_event, wxs); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 301 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static void |
| 306 | weston_xwayland_xserver_loaded(struct weston_xwayland *xwayland, |
| 307 | struct wl_client *client, int wm_fd) |
| 308 | { |
| 309 | struct weston_xserver *wxs = (struct weston_xserver *)xwayland; |
| 310 | wxs->wm = weston_wm_create(wxs, wm_fd); |
| 311 | wxs->client = client; |
| 312 | } |
| 313 | |
| 314 | static void |
| 315 | weston_xwayland_xserver_exited(struct weston_xwayland *xwayland, |
| 316 | int exit_status) |
| 317 | { |
| 318 | struct weston_xserver *wxs = (struct weston_xserver *)xwayland; |
| 319 | |
| 320 | wxs->pid = 0; |
| 321 | wxs->client = NULL; |
| 322 | |
| 323 | wxs->abstract_source = |
| 324 | wl_event_loop_add_fd(wxs->loop, wxs->abstract_fd, |
| 325 | WL_EVENT_READABLE, |
| 326 | weston_xserver_handle_event, wxs); |
| 327 | wxs->unix_source = |
| 328 | wl_event_loop_add_fd(wxs->loop, wxs->unix_fd, |
| 329 | WL_EVENT_READABLE, |
| 330 | weston_xserver_handle_event, wxs); |
| 331 | |
| 332 | if (wxs->wm) { |
| 333 | weston_log("xserver exited, code %d\n", exit_status); |
| 334 | weston_wm_destroy(wxs->wm); |
| 335 | wxs->wm = NULL; |
| 336 | } else { |
| 337 | /* If the X server crashes before it binds to the |
| 338 | * xserver interface, shut down and don't try |
| 339 | * again. */ |
| 340 | weston_log("xserver crashing too fast: %d\n", exit_status); |
| 341 | weston_xserver_shutdown(wxs); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | const struct weston_xwayland_api api = { |
| 346 | weston_xwayland_get, |
| 347 | weston_xwayland_listen, |
| 348 | weston_xwayland_xserver_loaded, |
| 349 | weston_xwayland_xserver_exited, |
| 350 | }; |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 351 | extern const struct weston_xwayland_surface_api surface_api; |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 352 | |
| 353 | WL_EXPORT int |
Quentin Glidic | 3d7ca3b | 2016-12-02 14:20:35 +0100 | [diff] [blame] | 354 | weston_module_init(struct weston_compositor *compositor) |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 355 | |
| 356 | { |
| 357 | struct wl_display *display = compositor->wl_display; |
| 358 | struct weston_xserver *wxs; |
| 359 | int ret; |
| 360 | |
| 361 | wxs = zalloc(sizeof *wxs); |
| 362 | if (wxs == NULL) |
| 363 | return -1; |
| 364 | wxs->wl_display = display; |
| 365 | wxs->compositor = compositor; |
| 366 | |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 367 | if (weston_xwayland_get_api(compositor) != NULL || |
| 368 | weston_xwayland_surface_get_api(compositor) != NULL) { |
| 369 | weston_log("The xwayland module APIs are already loaded.\n"); |
| 370 | free(wxs); |
| 371 | return -1; |
| 372 | } |
| 373 | |
Giulio Camuffo | 9c764df | 2016-06-29 11:54:27 +0200 | [diff] [blame] | 374 | ret = weston_plugin_api_register(compositor, WESTON_XWAYLAND_API_NAME, |
| 375 | &api, sizeof(api)); |
| 376 | if (ret < 0) { |
| 377 | weston_log("Failed to register the xwayland module API.\n"); |
| 378 | free(wxs); |
| 379 | return -1; |
| 380 | } |
| 381 | |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 382 | ret = weston_plugin_api_register(compositor, |
| 383 | WESTON_XWAYLAND_SURFACE_API_NAME, |
| 384 | &surface_api, sizeof(surface_api)); |
| 385 | if (ret < 0) { |
| 386 | weston_log("Failed to register the xwayland surface API.\n"); |
| 387 | free(wxs); |
| 388 | return -1; |
| 389 | } |
| 390 | |
Tiago Vignatti | f268446 | 2012-11-30 17:19:56 -0200 | [diff] [blame] | 391 | wxs->destroy_listener.notify = weston_xserver_destroy; |
| 392 | wl_signal_add(&compositor->destroy_signal, &wxs->destroy_listener); |
Kristian Høgsberg | e10b124 | 2012-05-21 16:48:05 -0400 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | } |