[PATCH weston 3/6] Introduce libweston-desktop

Quentin Glidic sardemff7+wayland at sardemff7.net
Fri Aug 12 08:41:34 UTC 2016


From: Quentin Glidic <sardemff7+git at sardemff7.net>

libweston-desktop is an abstraction library for compositors wanting to
support desktop-like shells.

The API is designed from xdg_shell features, as it will eventually be
the recommended shell for modern applications to use.
In the future, adding new shell protocols support will be easier, as
limited to libweston-desktop.

The library versioning is the same as libweston. If one of them break
ABI compatibility, the other will too.

The compositor will only ever see toplevel surfaces (“windows”), with
all the other being internal implementation details.
Thus, popups and associated grabs are handled entirely in
libweston-desktop.
Xwayland special surfaces (override-redirect) are special-cased to a
dedicated layer, as the compositor should not know about them.

All the shell error checking is taken care of too, as well as some
specification rules (e.g. sizes constraint for maximized and fullscreen
surfaces).

All the compositor has to do is define a few callbacks in the interface
struct, and manage toplevel surfaces.

Signed-off-by: Quentin Glidic <sardemff7+git at sardemff7.net>
Reviewed-by: Jonas Ådahl <jadahl at gmail.com>
Acked-by: Giulio Camuffo <giulio.camuffo at kdab.com>

Differential Revision: https://phabricator.freedesktop.org/D1207
---

Added a line for versioning and ABI compatibility, not sure about this
one.

 Makefile.am                               |  31 +-
 configure.ac                              |   1 +
 libweston-desktop/client.c                | 212 ++++++++
 libweston-desktop/internal.h              | 236 +++++++++
 libweston-desktop/libweston-desktop.c     | 234 +++++++++
 libweston-desktop/libweston-desktop.h     | 166 ++++++
 libweston-desktop/libweston-desktop.pc.in |  12 +
 libweston-desktop/seat.c                  | 368 ++++++++++++++
 libweston-desktop/surface.c               | 818 ++++++++++++++++++++++++++++++
 libweston-desktop/wl-shell.c              | 464 +++++++++++++++++
 libweston-desktop/xdg-shell-v5.c          | 798 +++++++++++++++++++++++++++++
 libweston-desktop/xwayland.c              | 360 +++++++++++++
 libweston/compositor.c                    |  37 ++
 libweston/compositor.h                    |  16 +
 14 files changed, 3752 insertions(+), 1 deletion(-)
 create mode 100644 libweston-desktop/client.c
 create mode 100644 libweston-desktop/internal.h
 create mode 100644 libweston-desktop/libweston-desktop.c
 create mode 100644 libweston-desktop/libweston-desktop.h
 create mode 100644 libweston-desktop/libweston-desktop.pc.in
 create mode 100644 libweston-desktop/seat.c
 create mode 100644 libweston-desktop/surface.c
 create mode 100644 libweston-desktop/wl-shell.c
 create mode 100644 libweston-desktop/xdg-shell-v5.c
 create mode 100644 libweston-desktop/xwayland.c

diff --git a/Makefile.am b/Makefile.am
index 32627f5..217d21f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -105,6 +105,29 @@ libweston_ at LIBWESTON_MAJOR@_la_SOURCES =			\
 	shared/platform.h				\
 	libweston/weston-egl-ext.h
 
+lib_LTLIBRARIES += libweston-desktop- at LIBWESTON_MAJOR@.la
+libweston_desktop_ at LIBWESTON_MAJOR@_la_CPPFLAGS = $(AM_CPPFLAGS) -DIN_WESTON
+libweston_desktop_ at LIBWESTON_MAJOR@_la_CFLAGS = $(AM_CFLAGS) $(COMPOSITOR_CFLAGS)
+libweston_desktop_ at LIBWESTON_MAJOR@_la_LIBADD = libweston- at LIBWESTON_MAJOR@.la $(COMPOSITOR_LIBS)
+libweston_desktop_ at LIBWESTON_MAJOR@_la_LDFLAGS = -version-info $(LT_VERSION_INFO)
+
+libweston_desktop_ at LIBWESTON_MAJOR@_la_SOURCES =	\
+	libweston-desktop/client.c			\
+	libweston-desktop/internal.h			\
+	libweston-desktop/libweston-desktop.c		\
+	libweston-desktop/libweston-desktop.h		\
+	libweston-desktop/seat.c			\
+	libweston-desktop/surface.c			\
+	libweston-desktop/wl-shell.c			\
+	libweston-desktop/xdg-shell-v5.c		\
+	libweston-desktop/xwayland.c
+
+nodist_libweston_desktop_ at LIBWESTON_MAJOR@_la_SOURCES =		\
+	protocol/xdg-shell-unstable-v5-protocol.c		\
+	protocol/xdg-shell-unstable-v5-server-protocol.h
+
+libweston-desktop- at LIBWESTON_MAJOR@.la libweston-desktop/libweston_desktop_ at LIBWESTON_MAJOR@_la-xdg-shell-v5.lo: protocol/xdg-shell-unstable-v5-server-protocol.h
+
 if SYSTEMD_NOTIFY_SUPPORT
 module_LTLIBRARIES += systemd-notify.la
 systemd_notify_la_LDFLAGS = -module -avoid-version
@@ -230,7 +253,10 @@ endif
 endif # BUILD_WESTON_LAUNCH
 
 pkgconfigdir = $(libdir)/pkgconfig
-pkgconfig_DATA = compositor/weston.pc libweston/libweston-${LIBWESTON_MAJOR}.pc
+pkgconfig_DATA = \
+	libweston/libweston-${LIBWESTON_MAJOR}.pc \
+	libweston-desktop/libweston-desktop-${LIBWESTON_MAJOR}.pc \
+	compositor/weston.pc
 
 wayland_sessiondir = $(datadir)/wayland-sessions
 dist_wayland_session_DATA = compositor/weston.desktop
@@ -251,6 +277,9 @@ libwestoninclude_HEADERS =			\
 	shared/config-parser.h			\
 	shared/zalloc.h
 
+libwestoninclude_HEADERS +=			\
+	libweston-desktop/libweston-desktop.h
+
 westonincludedir = $(includedir)/weston
 westoninclude_HEADERS = compositor/weston.h
 
diff --git a/configure.ac b/configure.ac
index 0090d71..b45d0e2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -646,6 +646,7 @@ AC_CONFIG_FILES([Makefile libweston/version.h compositor/weston.pc])
 # AC_CONFIG_FILES needs the full name when running autoconf, so we need to use
 # libweston_abi_version here, and outside [] because of m4 quoting rules
 AC_CONFIG_FILES([libweston/libweston-]libweston_major_version[.pc:libweston/libweston.pc.in])
+AC_CONFIG_FILES([libweston-desktop/libweston-desktop-]libweston_major_version[.pc:libweston-desktop/libweston-desktop.pc.in])
 
 AM_CONDITIONAL([HAVE_GIT_REPO], [test -f $srcdir/.git/logs/HEAD])
 
diff --git a/libweston-desktop/client.c b/libweston-desktop/client.c
new file mode 100644
index 0000000..29c3c98
--- /dev/null
+++ b/libweston-desktop/client.c
@@ -0,0 +1,212 @@
+/*
+ * Copyright © 2016 Quentin "Sardem FF7" Glidic
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <wayland-server.h>
+
+#include "compositor.h"
+#include "zalloc.h"
+
+#include "libweston-desktop.h"
+#include "internal.h"
+
+struct weston_desktop_client {
+	struct weston_desktop *desktop;
+	struct wl_client *client;
+	struct wl_resource *resource;
+	struct wl_list surface_list;
+	uint32_t ping_serial;
+	struct wl_event_source *ping_timer;
+	struct wl_signal destroy_signal;
+};
+
+void
+weston_desktop_client_add_destroy_listener(struct weston_desktop_client *client,
+					   struct wl_listener *listener)
+{
+	wl_signal_add(&client->destroy_signal, listener);
+}
+
+static void
+weston_desktop_client_destroy(struct wl_resource *resource)
+{
+	struct weston_desktop_client *client =
+		wl_resource_get_user_data(resource);
+	struct wl_list *list = &client->surface_list;
+	struct wl_list *link, *tmp;
+
+	wl_signal_emit(&client->destroy_signal, client);
+
+	for (link = list->next, tmp = link->next;
+	     link != list;
+	     link = tmp, tmp = link->next) {
+		wl_list_remove(link);
+		wl_list_init(link);
+	}
+
+	if (client->ping_timer != NULL)
+		wl_event_source_remove(client->ping_timer);
+
+	free(client);
+}
+
+static int
+weston_desktop_client_ping_timeout(void *user_data)
+{
+	struct weston_desktop_client *client = user_data;
+
+	weston_desktop_api_ping_timeout(client->desktop, client);
+	return 1;
+}
+
+struct weston_desktop_client *
+weston_desktop_client_create(struct weston_desktop *desktop,
+			     struct wl_client *wl_client,
+			     wl_dispatcher_func_t dispatcher,
+			     const struct wl_interface *interface,
+			     const void *implementation, uint32_t version,
+			     uint32_t id)
+{
+	struct weston_desktop_client *client;
+	struct wl_display *display;
+	struct wl_event_loop *loop;
+
+	client = zalloc(sizeof(struct weston_desktop_client));
+	if (client == NULL) {
+		if (wl_client != NULL)
+			wl_client_post_no_memory(wl_client);
+		return NULL;
+	}
+
+	client->desktop = desktop;
+	client->client = wl_client;
+
+	wl_list_init(&client->surface_list);
+	wl_signal_init(&client->destroy_signal);
+
+	if (wl_client == NULL)
+		return client;
+
+	client->resource = wl_resource_create(wl_client, interface, version, id);
+	if (client->resource == NULL) {
+		wl_client_post_no_memory(wl_client);
+		free(client);
+		return NULL;
+	}
+
+	if (dispatcher != NULL)
+		wl_resource_set_dispatcher(client->resource, dispatcher,
+					   weston_desktop_client_destroy, client,
+					   weston_desktop_client_destroy);
+	else
+		wl_resource_set_implementation(client->resource, implementation,
+					       client,
+					       weston_desktop_client_destroy);
+
+
+	display = wl_client_get_display(client->client);
+	loop = wl_display_get_event_loop(display);
+	client->ping_timer =
+		wl_event_loop_add_timer(loop,
+					weston_desktop_client_ping_timeout,
+					client);
+	if (client->ping_timer == NULL)
+		wl_client_post_no_memory(wl_client);
+
+	return client;
+}
+
+struct weston_desktop *
+weston_desktop_client_get_desktop(struct weston_desktop_client *client)
+{
+	return client->desktop;
+}
+
+struct wl_resource *
+weston_desktop_client_get_resource(struct weston_desktop_client *client)
+{
+	return client->resource;
+}
+
+struct wl_list *
+weston_desktop_client_get_surface_list(struct weston_desktop_client *client)
+{
+	return &client->surface_list;
+}
+
+WL_EXPORT struct wl_client *
+weston_desktop_client_get_client(struct weston_desktop_client *client)
+{
+	return client->client;
+}
+
+WL_EXPORT void
+weston_desktop_client_for_each_surface(struct weston_desktop_client *client,
+				       void (*callback)(struct weston_desktop_surface *surface, void *user_data),
+				       void *user_data)
+{
+	struct wl_list *list = &client->surface_list;
+	struct wl_list *link;
+
+	for (link = list->next; link != list; link = link->next)
+		callback(weston_desktop_surface_from_client_link(link),
+			 user_data);
+}
+
+WL_EXPORT int
+weston_desktop_client_ping(struct weston_desktop_client *client)
+{
+	struct weston_desktop_surface *surface =
+		weston_desktop_surface_from_client_link(client->surface_list.next);
+	const struct weston_desktop_surface_implementation *implementation =
+		weston_desktop_surface_get_implementation(surface);
+	void *implementation_data =
+		weston_desktop_surface_get_implementation_data(surface);
+
+	if (implementation->ping == NULL)
+		return -1;
+
+	if (client->ping_serial != 0)
+		return 1;
+
+	client->ping_serial =
+		wl_display_next_serial(wl_client_get_display(client->client));
+	wl_event_source_timer_update(client->ping_timer, 10000);
+
+	implementation->ping(surface, client->ping_serial, implementation_data);
+
+	return 0;
+}
+
+void
+weston_desktop_client_pong(struct weston_desktop_client *client, uint32_t serial)
+{
+	if (client->ping_serial != serial)
+		return;
+
+	weston_desktop_api_pong(client->desktop, client);
+
+	wl_event_source_timer_update(client->ping_timer, 0);
+	client->ping_serial = 0;
+}
diff --git a/libweston-desktop/internal.h b/libweston-desktop/internal.h
new file mode 100644
index 0000000..21181a6
--- /dev/null
+++ b/libweston-desktop/internal.h
@@ -0,0 +1,236 @@
+/*
+ * Copyright © 2016 Quentin "Sardem FF7" Glidic
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef WESTON_DESKTOP_INTERNAL_H
+#define WESTON_DESKTOP_INTERNAL_H
+
+#include "compositor.h"
+
+struct weston_desktop_seat;
+struct weston_desktop_client;
+
+struct weston_compositor *
+weston_desktop_get_compositor(struct weston_desktop *desktop);
+struct wl_display *
+weston_desktop_get_display(struct weston_desktop *desktop);
+
+void
+weston_desktop_api_ping_timeout(struct weston_desktop *desktop,
+				struct weston_desktop_client *client);
+void
+weston_desktop_api_pong(struct weston_desktop *desktop,
+			struct weston_desktop_client *client);
+void
+weston_desktop_api_surface_added(struct weston_desktop *desktop,
+				 struct weston_desktop_surface *surface);
+void
+weston_desktop_api_surface_removed(struct weston_desktop *desktop,
+				   struct weston_desktop_surface *surface);
+void
+weston_desktop_api_committed(struct weston_desktop *desktop,
+			     struct weston_desktop_surface *surface,
+			     int32_t sx, int32_t sy);
+void
+weston_desktop_api_show_window_menu(struct weston_desktop *desktop,
+				    struct weston_desktop_surface *surface,
+				    struct weston_seat *seat,
+				    int32_t x, int32_t y);
+void
+weston_desktop_api_set_parent(struct weston_desktop *desktop,
+			      struct weston_desktop_surface *surface,
+			      struct weston_desktop_surface *parent);
+void
+weston_desktop_api_move(struct weston_desktop *desktop,
+			struct weston_desktop_surface *surface,
+			struct weston_seat *seat, uint32_t serial);
+void
+weston_desktop_api_resize(struct weston_desktop *desktop,
+			  struct weston_desktop_surface *surface,
+			  struct weston_seat *seat, uint32_t serial,
+			  enum weston_desktop_surface_edge edges);
+void
+weston_desktop_api_fullscreen_requested(struct weston_desktop *desktop,
+					struct weston_desktop_surface *surface,
+					bool fullscreen,
+					struct weston_output *output);
+void
+weston_desktop_api_maximized_requested(struct weston_desktop *desktop,
+				       struct weston_desktop_surface *surface,
+				       bool maximized);
+void
+weston_desktop_api_minimized_requested(struct weston_desktop *desktop,
+				       struct weston_desktop_surface *surface);
+
+struct weston_desktop_seat *
+weston_desktop_seat_from_seat(struct weston_seat *wseat);
+
+struct weston_desktop_surface_implementation {
+	void (*set_activated)(struct weston_desktop_surface *surface,
+			      void *user_data, bool activated);
+	void (*set_fullscreen)(struct weston_desktop_surface *surface,
+			       void *user_data, bool fullscreen);
+	void (*set_maximized)(struct weston_desktop_surface *surface,
+			      void *user_data, bool maximized);
+	void (*set_resizing)(struct weston_desktop_surface *surface,
+			     void *user_data, bool resizing);
+	void (*set_size)(struct weston_desktop_surface *surface,
+			 void *user_data, int32_t width, int32_t height);
+	void (*committed)(struct weston_desktop_surface *surface, void *user_data,
+		          bool new_buffer, int32_t sx, int32_t sy);
+	void (*update_position)(struct weston_desktop_surface *surface,
+				void *user_data);
+	void (*ping)(struct weston_desktop_surface *surface, uint32_t serial,
+		     void *user_data);
+	void (*close)(struct weston_desktop_surface *surface, void *user_data);
+
+	bool (*get_activated)(struct weston_desktop_surface *surface,
+			      void *user_data);
+	bool (*get_fullscreen)(struct weston_desktop_surface *surface,
+			       void *user_data);
+	bool (*get_maximized)(struct weston_desktop_surface *surface,
+			      void *user_data);
+	bool (*get_resizing)(struct weston_desktop_surface *surface,
+			     void *user_data);
+	struct weston_size
+	(*get_max_size)(struct weston_desktop_surface *surface,
+			void *user_data);
+	struct weston_size
+	(*get_min_size)(struct weston_desktop_surface *surface,
+			void *user_data);
+
+	void (*destroy)(struct weston_desktop_surface *surface,
+			void *user_data);
+};
+
+struct weston_desktop_client *
+weston_desktop_client_create(struct weston_desktop *desktop,
+			     struct wl_client *client,
+			     wl_dispatcher_func_t dispatcher,
+			     const struct wl_interface *interface,
+			     const void *implementation, uint32_t version,
+			     uint32_t id);
+
+void
+weston_desktop_client_add_destroy_listener(struct weston_desktop_client *client,
+					   struct wl_listener *listener);
+struct weston_desktop *
+weston_desktop_client_get_desktop(struct weston_desktop_client *client);
+struct wl_resource *
+weston_desktop_client_get_resource(struct weston_desktop_client *client);
+struct wl_list *
+weston_desktop_client_get_surface_list(struct weston_desktop_client *client);
+
+void
+weston_desktop_client_pong(struct weston_desktop_client *client,
+			   uint32_t serial);
+
+struct weston_desktop_surface *
+weston_desktop_surface_create(struct weston_desktop *desktop,
+			      struct weston_desktop_client *client,
+			      struct weston_surface *surface,
+			      const struct weston_desktop_surface_implementation *implementation,
+			      void *implementation_data);
+void
+weston_desktop_surface_destroy(struct weston_desktop_surface *surface);
+void
+weston_desktop_surface_resource_destroy(struct wl_resource *resource);
+struct wl_resource *
+weston_desktop_surface_add_resource(struct weston_desktop_surface *surface,
+				    const struct wl_interface *interface,
+				    const void *implementation, uint32_t id,
+				    wl_resource_destroy_func_t destroy);
+struct weston_desktop_surface *
+weston_desktop_surface_from_grab_link(struct wl_list *grab_link);
+
+struct wl_list *
+weston_desktop_surface_get_client_link(struct weston_desktop_surface *surface);
+struct weston_desktop_surface *
+weston_desktop_surface_from_client_link(struct wl_list *link);
+bool
+weston_desktop_surface_has_implementation(struct weston_desktop_surface *surface,
+					  const struct weston_desktop_surface_implementation *implementation);
+const struct weston_desktop_surface_implementation *
+weston_desktop_surface_get_implementation(struct weston_desktop_surface *surface);
+void *
+weston_desktop_surface_get_implementation_data(struct weston_desktop_surface *surface);
+struct weston_desktop_surface *
+weston_desktop_surface_get_parent(struct weston_desktop_surface *surface);
+bool
+weston_desktop_surface_get_grab(struct weston_desktop_surface *surface);
+
+void
+weston_desktop_surface_set_title(struct weston_desktop_surface *surface,
+				 const char *title);
+void
+weston_desktop_surface_set_app_id(struct weston_desktop_surface *surface,
+				  const char *app_id);
+void
+weston_desktop_surface_set_pid(struct weston_desktop_surface *surface,
+			       pid_t pid);
+void
+weston_desktop_surface_set_geometry(struct weston_desktop_surface *surface,
+				    struct weston_geometry geometry);
+void
+weston_desktop_surface_set_relative_to(struct weston_desktop_surface *surface,
+				       struct weston_desktop_surface *parent,
+				       int32_t x, int32_t y, bool use_geometry);
+void
+weston_desktop_surface_unset_relative_to(struct weston_desktop_surface *surface);
+void
+weston_desktop_surface_popup_grab(struct weston_desktop_surface *popup,
+				  struct weston_desktop_seat *seat,
+				  uint32_t serial);
+void
+weston_desktop_surface_popup_ungrab(struct weston_desktop_surface *popup,
+				    struct weston_desktop_seat *seat);
+void
+weston_desktop_surface_popup_dismiss(struct weston_desktop_surface *surface);
+
+struct weston_desktop_surface *
+weston_desktop_seat_popup_grab_get_topmost_surface(struct weston_desktop_seat *seat);
+bool
+weston_desktop_seat_popup_grab_start(struct weston_desktop_seat *seat,
+				     struct wl_client *client, uint32_t serial);
+void
+weston_desktop_seat_popup_grab_add_surface(struct weston_desktop_seat *seat,
+					   struct wl_list *link);
+void
+weston_desktop_seat_popup_grab_remove_surface(struct weston_desktop_seat *seat,
+					      struct wl_list *link);
+
+void
+weston_desktop_destroy_request(struct wl_client *client,
+			       struct wl_resource *resource);
+struct wl_global *
+weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop,
+				   struct wl_display *display);
+struct wl_global *
+weston_desktop_xdg_shell_v5_create(struct weston_desktop *desktop,
+				   struct wl_display *display);
+struct wl_global *
+weston_desktop_wl_shell_create(struct weston_desktop *desktop,
+			       struct wl_display *display);
+void
+weston_desktop_xwayland_init(struct weston_desktop *desktop);
+
+#endif /* WESTON_DESKTOP_INTERNAL_H */
diff --git a/libweston-desktop/libweston-desktop.c b/libweston-desktop/libweston-desktop.c
new file mode 100644
index 0000000..4583ede
--- /dev/null
+++ b/libweston-desktop/libweston-desktop.c
@@ -0,0 +1,234 @@
+/*
+ * Copyright © 2016 Quentin "Sardem FF7" Glidic
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include <wayland-server.h>
+#include <assert.h>
+
+#include "compositor.h"
+#include "zalloc.h"
+#include "helpers.h"
+
+#include "libweston-desktop.h"
+#include "internal.h"
+
+
+struct weston_desktop {
+	struct weston_compositor *compositor;
+	struct weston_desktop_api api;
+	void *user_data;
+	struct wl_global *xdg_shell_v5;
+	struct wl_global *wl_shell;
+};
+
+void
+weston_desktop_destroy_request(struct wl_client *client,
+			       struct wl_resource *resource)
+{
+	wl_resource_destroy(resource);
+}
+
+WL_EXPORT struct weston_desktop *
+weston_desktop_create(struct weston_compositor *compositor,
+		      const struct weston_desktop_api *api, void *user_data)
+{
+	struct weston_desktop *desktop;
+	struct wl_display *display = compositor->wl_display;
+
+	assert(api->surface_added);
+	assert(api->surface_removed);
+
+	desktop = zalloc(sizeof(struct weston_desktop));
+	desktop->compositor = compositor;
+	desktop->user_data = user_data;
+
+	desktop->api.struct_size =
+		MIN(sizeof(struct weston_desktop_api), api->struct_size);
+	memcpy(&desktop->api, api, desktop->api.struct_size);
+
+	desktop->xdg_shell_v5 =
+		weston_desktop_xdg_shell_v5_create(desktop, display);
+	if (desktop->xdg_shell_v5 == NULL) {
+		weston_desktop_destroy(desktop);
+		return NULL;
+	}
+
+	desktop->wl_shell =
+		weston_desktop_wl_shell_create(desktop, display);
+	if (desktop->wl_shell == NULL) {
+		weston_desktop_destroy(desktop);
+		return NULL;
+	}
+
+	weston_desktop_xwayland_init(desktop);
+
+	return desktop;
+}
+
+WL_EXPORT void
+weston_desktop_destroy(struct weston_desktop *desktop)
+{
+	if (desktop == NULL)
+		return;
+
+	if (desktop->wl_shell != NULL)
+		wl_global_destroy(desktop->wl_shell);
+	if (desktop->xdg_shell_v5 != NULL)
+		wl_global_destroy(desktop->xdg_shell_v5);
+
+	free(desktop);
+}
+
+
+struct weston_compositor *
+weston_desktop_get_compositor(struct weston_desktop *desktop)
+{
+	return desktop->compositor;
+}
+
+struct wl_display *
+weston_desktop_get_display(struct weston_desktop *desktop)
+{
+	return desktop->compositor->wl_display;
+}
+
+void
+weston_desktop_api_ping_timeout(struct weston_desktop *desktop,
+				struct weston_desktop_client *client)
+{
+	if (desktop->api.ping_timeout != NULL)
+		desktop->api.ping_timeout(client, desktop->user_data);
+}
+
+void
+weston_desktop_api_pong(struct weston_desktop *desktop,
+			struct weston_desktop_client *client)
+{
+	if (desktop->api.pong != NULL)
+		desktop->api.pong(client, desktop->user_data);
+}
+
+void
+weston_desktop_api_surface_added(struct weston_desktop *desktop,
+				 struct weston_desktop_surface *surface)
+{
+	struct weston_desktop_client *client =
+		weston_desktop_surface_get_client(surface);
+	struct wl_list *list = weston_desktop_client_get_surface_list(client);
+	struct wl_list *link = weston_desktop_surface_get_client_link(surface);
+
+	desktop->api.surface_added(surface, desktop->user_data);
+	wl_list_insert(list, link);
+}
+
+void
+weston_desktop_api_surface_removed(struct weston_desktop *desktop,
+				   struct weston_desktop_surface *surface)
+{
+	struct wl_list *link = weston_desktop_surface_get_client_link(surface);
+
+	wl_list_remove(link);
+	wl_list_init(link);
+	desktop->api.surface_removed(surface, desktop->user_data);
+}
+
+void
+weston_desktop_api_committed(struct weston_desktop *desktop,
+			     struct weston_desktop_surface *surface,
+			     int32_t sx, int32_t sy)
+{
+	if (desktop->api.committed != NULL)
+		desktop->api.committed(surface, sx, sy, desktop->user_data);
+}
+
+void
+weston_desktop_api_show_window_menu(struct weston_desktop *desktop,
+				    struct weston_desktop_surface *surface,
+				    struct weston_seat *seat,
+				    int32_t x, int32_t y)
+{
+	if (desktop->api.show_window_menu != NULL)
+		desktop->api.show_window_menu(surface, seat, x, y,
+					      desktop->user_data);
+}
+
+void
+weston_desktop_api_set_parent(struct weston_desktop *desktop,
+			      struct weston_desktop_surface *surface,
+			      struct weston_desktop_surface *parent)
+{
+	if (desktop->api.set_parent != NULL)
+		desktop->api.set_parent(surface, parent, desktop->user_data);
+}
+
+void
+weston_desktop_api_move(struct weston_desktop *desktop,
+			struct weston_desktop_surface *surface,
+			struct weston_seat *seat, uint32_t serial)
+{
+	if (desktop->api.move != NULL)
+		desktop->api.move(surface, seat, serial, desktop->user_data);
+}
+
+void
+weston_desktop_api_resize(struct weston_desktop *desktop,
+			  struct weston_desktop_surface *surface,
+			  struct weston_seat *seat, uint32_t serial,
+			  enum weston_desktop_surface_edge edges)
+{
+	if (desktop->api.resize != NULL)
+		desktop->api.resize(surface, seat, serial, edges,
+				    desktop->user_data);
+}
+
+void
+weston_desktop_api_fullscreen_requested(struct weston_desktop *desktop,
+					struct weston_desktop_surface *surface,
+					bool fullscreen,
+					struct weston_output *output)
+{
+	if (desktop->api.fullscreen_requested != NULL)
+		desktop->api.fullscreen_requested(surface, fullscreen, output,
+						  desktop->user_data);
+}
+
+void
+weston_desktop_api_maximized_requested(struct weston_desktop *desktop,
+				       struct weston_desktop_surface *surface,
+				       bool maximized)
+{
+	if (desktop->api.maximized_requested != NULL)
+		desktop->api.maximized_requested(surface, maximized,
+						 desktop->user_data);
+}
+
+void
+weston_desktop_api_minimized_requested(struct weston_desktop *desktop,
+				       struct weston_desktop_surface *surface)
+{
+	if (desktop->api.minimized_requested != NULL)
+		desktop->api.minimized_requested(surface, desktop->user_data);
+}
diff --git a/libweston-desktop/libweston-desktop.h b/libweston-desktop/libweston-desktop.h
new file mode 100644
index 0000000..0f0e031
--- /dev/null
+++ b/libweston-desktop/libweston-desktop.h
@@ -0,0 +1,166 @@
+/*
+ * Copyright © 2016 Quentin "Sardem FF7" Glidic
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef WESTON_DESKTOP_H
+#define WESTON_DESKTOP_H
+
+#include "compositor.h"
+#include <pixman.h>
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum weston_desktop_surface_edge {
+	WESTON_DESKTOP_SURFACE_EDGE_NONE = 0,
+	WESTON_DESKTOP_SURFACE_EDGE_TOP = 1,
+	WESTON_DESKTOP_SURFACE_EDGE_BOTTOM = 2,
+	WESTON_DESKTOP_SURFACE_EDGE_LEFT = 4,
+	WESTON_DESKTOP_SURFACE_EDGE_TOP_LEFT = 5,
+	WESTON_DESKTOP_SURFACE_EDGE_BOTTOM_LEFT = 6,
+	WESTON_DESKTOP_SURFACE_EDGE_RIGHT = 8,
+	WESTON_DESKTOP_SURFACE_EDGE_TOP_RIGHT = 9,
+	WESTON_DESKTOP_SURFACE_EDGE_BOTTOM_RIGHT = 10,
+};
+
+struct weston_desktop;
+struct weston_desktop_client;
+struct weston_desktop_surface;
+
+struct weston_desktop_api {
+	size_t struct_size;
+	void (*ping_timeout)(struct weston_desktop_client *client,
+			     void *user_data);
+	void (*pong)(struct weston_desktop_client *client,
+		     void *user_data);
+
+	void (*surface_added)(struct weston_desktop_surface *surface,
+			      void *user_data);
+	void (*surface_removed)(struct weston_desktop_surface *surface,
+				void *user_data);
+	void (*committed)(struct weston_desktop_surface *surface,
+			  int32_t sx, int32_t sy, void *user_data);
+	void (*show_window_menu)(struct weston_desktop_surface *surface,
+				 struct weston_seat *seat, int32_t x, int32_t y,
+				 void *user_data);
+	void (*set_parent)(struct weston_desktop_surface *surface,
+			   struct weston_desktop_surface *parent,
+			   void *user_data);
+	void (*move)(struct weston_desktop_surface *surface,
+		     struct weston_seat *seat, uint32_t serial, void *user_data);
+	void (*resize)(struct weston_desktop_surface *surface,
+		       struct weston_seat *seat, uint32_t serial,
+		       enum weston_desktop_surface_edge edges, void *user_data);
+	void (*fullscreen_requested)(struct weston_desktop_surface *surface,
+				     bool fullscreen,
+				     struct weston_output *output,
+				     void *user_data);
+	void (*maximized_requested)(struct weston_desktop_surface *surface,
+				    bool maximized, void *user_data);
+	void (*minimized_requested)(struct weston_desktop_surface *surface,
+				    void *user_data);
+};
+
+void
+weston_seat_break_desktop_grabs(struct weston_seat *seat);
+
+struct weston_desktop *
+weston_desktop_create(struct weston_compositor *compositor,
+		      const struct weston_desktop_api *api, void *user_data);
+void
+weston_desktop_destroy(struct weston_desktop *desktop);
+
+struct wl_client *
+weston_desktop_client_get_client(struct weston_desktop_client *client);
+void
+weston_desktop_client_for_each_surface(struct weston_desktop_client *client,
+				       void (*callback)(struct weston_desktop_surface *surface, void *user_data),
+				       void *user_data);
+int
+weston_desktop_client_ping(struct weston_desktop_client *client);
+
+bool
+weston_surface_is_desktop_surface(struct weston_surface *surface);
+struct weston_desktop_surface *
+weston_surface_get_desktop_surface(struct weston_surface *surface);
+
+void
+weston_desktop_surface_set_user_data(struct weston_desktop_surface *self,
+				     void *user_data);
+struct weston_view *
+weston_desktop_surface_create_view(struct weston_desktop_surface *surface);
+void
+weston_desktop_surface_destroy_view(struct weston_view *view);
+void
+weston_desktop_surface_propagate_layer(struct weston_desktop_surface *surface);
+void
+weston_desktop_surface_set_activated(struct weston_desktop_surface *surface,
+				     bool activated);
+void
+weston_desktop_surface_set_fullscreen(struct weston_desktop_surface *surface,
+				      bool fullscreen);
+void
+weston_desktop_surface_set_maximized(struct weston_desktop_surface *surface,
+				     bool maximized);
+void
+weston_desktop_surface_set_resizing(struct weston_desktop_surface *surface,
+				    bool resized);
+void
+weston_desktop_surface_set_size(struct weston_desktop_surface *surface,
+				int32_t width, int32_t height);
+void
+weston_desktop_surface_close(struct weston_desktop_surface *surface);
+
+void *
+weston_desktop_surface_get_user_data(struct weston_desktop_surface *surface);
+struct weston_desktop_client *
+weston_desktop_surface_get_client(struct weston_desktop_surface *surface);
+struct weston_surface *
+weston_desktop_surface_get_surface(struct weston_desktop_surface *surface);
+const char *
+weston_desktop_surface_get_title(struct weston_desktop_surface *surface);
+const char *
+weston_desktop_surface_get_app_id(struct weston_desktop_surface *surface);
+pid_t
+weston_desktop_surface_get_pid(struct weston_desktop_surface *surface);
+bool
+weston_desktop_surface_get_activated(struct weston_desktop_surface *surface);
+bool
+weston_desktop_surface_get_maximized(struct weston_desktop_surface *surface);
+bool
+weston_desktop_surface_get_fullscreen(struct weston_desktop_surface *surface);
+bool
+weston_desktop_surface_get_resizing(struct weston_desktop_surface *surface);
+struct weston_geometry
+weston_desktop_surface_get_geometry(struct weston_desktop_surface *surface);
+struct weston_size
+weston_desktop_surface_get_max_size(struct weston_desktop_surface *surface);
+struct weston_size
+weston_desktop_surface_get_min_size(struct weston_desktop_surface *surface);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* WESTON_DESKTOP_H */
diff --git a/libweston-desktop/libweston-desktop.pc.in b/libweston-desktop/libweston-desktop.pc.in
new file mode 100644
index 0000000..c118b08
--- /dev/null
+++ b/libweston-desktop/libweston-desktop.pc.in
@@ -0,0 +1,12 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+pkgincludedir=${includedir}/libweston- at LIBWESTON_ABI_VERSION@
+
+Name: libweston-desktop
+Description: Desktop shells abstraction library for libweston compositors
+Version: @WESTON_VERSION@
+Requires.private: libweston- at LIBWESTON_ABI_VERSION@ wayland-server
+Cflags: -I${pkgincludedir}
+Libs: -L${libdir} -lweston-desktop- at LIBWESTON_ABI_VERSION@
diff --git a/libweston-desktop/seat.c b/libweston-desktop/seat.c
new file mode 100644
index 0000000..9630880
--- /dev/null
+++ b/libweston-desktop/seat.c
@@ -0,0 +1,368 @@
+/*
+ * Copyright © 2010-2012 Intel Corporation
+ * Copyright © 2011-2012 Collabora, Ltd.
+ * Copyright © 2013 Raspberry Pi Foundation
+ * Copyright © 2016 Quentin "Sardem FF7" Glidic
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <assert.h>
+
+#include <wayland-server.h>
+
+#include "compositor.h"
+#include "zalloc.h"
+
+#include "libweston-desktop.h"
+#include "internal.h"
+
+struct weston_desktop_seat {
+	struct wl_listener seat_destroy_listener;
+	struct weston_seat *seat;
+	struct {
+		struct weston_keyboard_grab keyboard;
+		struct weston_pointer_grab pointer;
+		struct weston_touch_grab touch;
+		bool initial_up;
+		struct wl_client *client;
+		struct wl_list surfaces;
+	} popup_grab;
+};
+
+static void weston_desktop_seat_popup_grab_end(struct weston_desktop_seat *seat);
+
+static void
+weston_desktop_seat_popup_grab_keyboard_key(struct weston_keyboard_grab *grab,
+					    uint32_t time, uint32_t key,
+					    enum wl_keyboard_key_state state)
+{
+	weston_keyboard_send_key(grab->keyboard, time, key, state);
+}
+
+static void
+weston_desktop_seat_popup_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
+						  uint32_t serial,
+						  uint32_t mods_depressed,
+						  uint32_t mods_latched,
+						  uint32_t mods_locked,
+						  uint32_t group)
+{
+	weston_keyboard_send_modifiers(grab->keyboard, serial, mods_depressed,
+				       mods_latched, mods_locked, group);
+}
+
+static void
+weston_desktop_seat_popup_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
+{
+	struct weston_desktop_seat *seat =
+		wl_container_of(grab, seat, popup_grab.keyboard);
+
+	weston_desktop_seat_popup_grab_end(seat);
+}
+
+static const struct weston_keyboard_grab_interface weston_desktop_seat_keyboard_popup_grab_interface = {
+   .key = weston_desktop_seat_popup_grab_keyboard_key,
+   .modifiers = weston_desktop_seat_popup_grab_keyboard_modifiers,
+   .cancel = weston_desktop_seat_popup_grab_keyboard_cancel,
+};
+
+static void
+weston_desktop_seat_popup_grab_pointer_focus(struct weston_pointer_grab *grab)
+{
+	struct weston_desktop_seat *seat =
+		wl_container_of(grab, seat, popup_grab.pointer);
+	struct weston_pointer *pointer = grab->pointer;
+	struct weston_view *view;
+	wl_fixed_t sx, sy;
+
+	view = weston_compositor_pick_view(pointer->seat->compositor,
+					   pointer->x, pointer->y, &sx, &sy);
+
+	if (view != NULL &&
+	    view->surface->resource != NULL &&
+	    wl_resource_get_client(view->surface->resource) == seat->popup_grab.client)
+		weston_pointer_set_focus(pointer, view, sx, sy);
+	else
+		weston_pointer_clear_focus(pointer);
+}
+
+static void
+weston_desktop_seat_popup_grab_pointer_motion(struct weston_pointer_grab *grab,
+					      uint32_t time,
+					      struct weston_pointer_motion_event *event)
+{
+	weston_pointer_send_motion(grab->pointer, time, event);
+}
+
+static void
+weston_desktop_seat_popup_grab_pointer_button(struct weston_pointer_grab *grab,
+					      uint32_t time, uint32_t button,
+					      enum wl_pointer_button_state state)
+{
+	struct weston_desktop_seat *seat =
+		wl_container_of(grab, seat, popup_grab.pointer);
+	struct weston_pointer *pointer = grab->pointer;
+	bool initial_up = seat->popup_grab.initial_up;
+
+	if (state == WL_POINTER_BUTTON_STATE_RELEASED)
+		seat->popup_grab.initial_up = true;
+
+	if (weston_pointer_has_focus_resource(pointer))
+		weston_pointer_send_button(pointer, time, button, state);
+	else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
+		 (initial_up || (time - grab->pointer->grab_time) > 500))
+		weston_desktop_seat_popup_grab_end(seat);
+}
+
+static void
+weston_desktop_seat_popup_grab_pointer_axis(struct weston_pointer_grab *grab,
+					    uint32_t time,
+					    struct weston_pointer_axis_event *event)
+{
+	weston_pointer_send_axis(grab->pointer, time, event);
+}
+
+static void
+weston_desktop_seat_popup_grab_pointer_axis_source(struct weston_pointer_grab *grab,
+						   uint32_t source)
+{
+	weston_pointer_send_axis_source(grab->pointer, source);
+}
+
+static void
+weston_desktop_seat_popup_grab_pointer_frame(struct weston_pointer_grab *grab)
+{
+	weston_pointer_send_frame(grab->pointer);
+}
+
+static void
+weston_desktop_seat_popup_grab_pointer_cancel(struct weston_pointer_grab *grab)
+{
+	struct weston_desktop_seat *seat =
+		wl_container_of(grab, seat, popup_grab.pointer);
+
+	weston_desktop_seat_popup_grab_end(seat);
+}
+
+static const struct weston_pointer_grab_interface weston_desktop_seat_pointer_popup_grab_interface = {
+   .focus = weston_desktop_seat_popup_grab_pointer_focus,
+   .motion = weston_desktop_seat_popup_grab_pointer_motion,
+   .button = weston_desktop_seat_popup_grab_pointer_button,
+   .axis = weston_desktop_seat_popup_grab_pointer_axis,
+   .axis_source = weston_desktop_seat_popup_grab_pointer_axis_source,
+   .frame = weston_desktop_seat_popup_grab_pointer_frame,
+   .cancel = weston_desktop_seat_popup_grab_pointer_cancel,
+};
+
+static void
+weston_desktop_seat_popup_grab_touch_down(struct weston_touch_grab *grab,
+					  uint32_t time, int touch_id,
+					  wl_fixed_t sx, wl_fixed_t sy)
+{
+	weston_touch_send_down(grab->touch, time, touch_id, sx, sy);
+}
+
+static void
+weston_desktop_seat_popup_grab_touch_up(struct weston_touch_grab *grab,
+					uint32_t time, int touch_id)
+{
+	weston_touch_send_up(grab->touch, time, touch_id);
+}
+
+static void
+weston_desktop_seat_popup_grab_touch_motion(struct weston_touch_grab *grab,
+					    uint32_t time, int touch_id,
+					    wl_fixed_t sx, wl_fixed_t sy)
+{
+	weston_touch_send_motion(grab->touch, time, touch_id, sx, sy);
+}
+
+static void
+weston_desktop_seat_popup_grab_touch_frame(struct weston_touch_grab *grab)
+{
+	weston_touch_send_frame(grab->touch);
+}
+
+static void
+weston_desktop_seat_popup_grab_touch_cancel(struct weston_touch_grab *grab)
+{
+	struct weston_desktop_seat *seat =
+		wl_container_of(grab, seat, popup_grab.touch);
+
+	weston_desktop_seat_popup_grab_end(seat);
+}
+
+static const struct weston_touch_grab_interface weston_desktop_seat_touch_popup_grab_interface = {
+   .down = weston_desktop_seat_popup_grab_touch_down,
+   .up = weston_desktop_seat_popup_grab_touch_up,
+   .motion = weston_desktop_seat_popup_grab_touch_motion,
+   .frame = weston_desktop_seat_popup_grab_touch_frame,
+   .cancel = weston_desktop_seat_popup_grab_touch_cancel,
+};
+
+static void
+weston_desktop_seat_destroy(struct wl_listener *listener, void *data)
+{
+	struct weston_desktop_seat *seat =
+		wl_container_of(listener, seat, seat_destroy_listener);
+
+	free(seat);
+}
+
+struct weston_desktop_seat *
+weston_desktop_seat_from_seat(struct weston_seat *wseat)
+{
+	struct wl_listener *listener;
+	struct weston_desktop_seat *seat;
+
+	listener = wl_signal_get(&wseat->destroy_signal,
+				 weston_desktop_seat_destroy);
+	if (listener != NULL)
+		return wl_container_of(listener, seat, seat_destroy_listener);
+
+	seat = zalloc(sizeof(struct weston_desktop_seat));
+	if (seat == NULL)
+		return NULL;
+
+	seat->seat = wseat;
+
+	seat->seat_destroy_listener.notify = weston_desktop_seat_destroy;
+	wl_signal_add(&wseat->destroy_signal, &seat->seat_destroy_listener);
+
+	seat->popup_grab.keyboard.interface =
+		&weston_desktop_seat_keyboard_popup_grab_interface;
+	seat->popup_grab.pointer.interface =
+		&weston_desktop_seat_pointer_popup_grab_interface;
+	seat->popup_grab.touch.interface =
+		&weston_desktop_seat_touch_popup_grab_interface;
+	wl_list_init(&seat->popup_grab.surfaces);
+
+	return seat;
+}
+
+struct weston_desktop_surface *
+weston_desktop_seat_popup_grab_get_topmost_surface(struct weston_desktop_seat *seat)
+{
+	if (wl_list_empty(&seat->popup_grab.surfaces))
+		return NULL;
+
+	struct wl_list *grab_link = seat->popup_grab.surfaces.next;
+
+	return weston_desktop_surface_from_grab_link(grab_link);
+}
+
+bool
+weston_desktop_seat_popup_grab_start(struct weston_desktop_seat *seat,
+				     struct wl_client *client, uint32_t serial)
+{
+	assert(seat->popup_grab.client == NULL || seat->popup_grab.client == client);
+
+	struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat->seat);
+	struct weston_pointer *pointer = weston_seat_get_pointer(seat->seat);
+	struct weston_touch *touch = weston_seat_get_touch(seat->seat);
+
+	if ((keyboard == NULL || keyboard->grab_serial != serial) &&
+	    (pointer == NULL || pointer->grab_serial != serial) &&
+	    (touch == NULL || touch->grab_serial != serial)) {
+		return false;
+	}
+
+	if (keyboard != NULL &&
+	    keyboard->grab->interface != &weston_desktop_seat_keyboard_popup_grab_interface)
+		weston_keyboard_start_grab(keyboard, &seat->popup_grab.keyboard);
+
+	if (pointer != NULL &&
+	    pointer->grab->interface != &weston_desktop_seat_pointer_popup_grab_interface)
+		weston_pointer_start_grab(pointer, &seat->popup_grab.pointer);
+
+	if (touch != NULL &&
+	    touch->grab->interface != &weston_desktop_seat_touch_popup_grab_interface)
+		weston_touch_start_grab(touch, &seat->popup_grab.touch);
+
+	seat->popup_grab.initial_up =
+		(pointer == NULL || pointer->button_count == 0);
+	seat->popup_grab.client = client;
+
+	return true;
+}
+
+static void
+weston_desktop_seat_popup_grab_end(struct weston_desktop_seat *seat)
+{
+	struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat->seat);
+	struct weston_pointer *pointer = weston_seat_get_pointer(seat->seat);
+	struct weston_touch *touch = weston_seat_get_touch(seat->seat);
+
+	while (!wl_list_empty(&seat->popup_grab.surfaces)) {
+		struct wl_list *link = seat->popup_grab.surfaces.prev;
+		struct weston_desktop_surface *surface =
+			weston_desktop_surface_from_grab_link(link);
+
+		wl_list_remove(link);
+		wl_list_init(link);
+		weston_desktop_surface_popup_dismiss(surface);
+	}
+
+	if (keyboard != NULL &&
+	    keyboard->grab->interface == &weston_desktop_seat_keyboard_popup_grab_interface)
+		weston_keyboard_end_grab(keyboard);
+
+	if (pointer != NULL &&
+	    pointer->grab->interface == &weston_desktop_seat_pointer_popup_grab_interface)
+		weston_pointer_end_grab(pointer);
+
+	if (touch != NULL &&
+	    touch->grab->interface == &weston_desktop_seat_touch_popup_grab_interface)
+		weston_touch_end_grab(touch);
+
+	seat->popup_grab.client = NULL;
+}
+
+void
+weston_desktop_seat_popup_grab_add_surface(struct weston_desktop_seat *seat,
+					   struct wl_list *link)
+{
+	assert(seat->popup_grab.client != NULL);
+
+	wl_list_insert(&seat->popup_grab.surfaces, link);
+}
+
+void
+weston_desktop_seat_popup_grab_remove_surface(struct weston_desktop_seat *seat,
+					      struct wl_list *link)
+{
+	assert(seat->popup_grab.client != NULL);
+
+	wl_list_remove(link);
+	wl_list_init(link);
+	if (wl_list_empty(&seat->popup_grab.surfaces))
+		weston_desktop_seat_popup_grab_end(seat);
+}
+
+WL_EXPORT void
+weston_seat_break_desktop_grabs(struct weston_seat *wseat)
+{
+	struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
+
+	weston_desktop_seat_popup_grab_end(seat);
+}
diff --git a/libweston-desktop/surface.c b/libweston-desktop/surface.c
new file mode 100644
index 0000000..7a2fc42
--- /dev/null
+++ b/libweston-desktop/surface.c
@@ -0,0 +1,818 @@
+/*
+ * Copyright © 2016 Quentin "Sardem FF7" Glidic
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <string.h>
+#include <assert.h>
+
+#include <wayland-server.h>
+
+#include "compositor.h"
+#include "zalloc.h"
+
+#include "libweston-desktop.h"
+#include "internal.h"
+
+struct weston_desktop_view {
+	struct wl_list link;
+	struct weston_view *view;
+	struct weston_desktop_view *parent;
+	struct wl_list children_list;
+	struct wl_list children_link;
+};
+
+struct weston_desktop_surface {
+	struct weston_desktop *desktop;
+	struct weston_desktop_client *client;
+	struct wl_list client_link;
+	const struct weston_desktop_surface_implementation *implementation;
+	void *implementation_data;
+	void *user_data;
+	struct weston_surface *surface;
+	struct wl_list view_list;
+	bool has_new_buffer;
+	struct wl_listener surface_commit_listener;
+	struct wl_listener surface_destroy_listener;
+	struct wl_listener client_destroy_listener;
+	struct wl_list children_list;
+
+	struct wl_list resource_list;
+	bool has_geometry;
+	struct weston_geometry geometry;
+	struct {
+		char *title;
+		char *app_id;
+		pid_t pid;
+	};
+	struct {
+		struct weston_desktop_surface *parent;
+		struct wl_list children_link;
+		struct weston_position position;
+		bool use_geometry;
+	};
+	struct {
+		struct wl_list grab_link;
+	};
+};
+
+static void
+weston_desktop_surface_update_view_position(struct weston_desktop_surface *surface)
+{
+	struct weston_desktop_view *view;
+	int32_t x, y;
+
+	x = surface->position.x;
+	y = surface->position.y;
+
+	if (surface->use_geometry) {
+		struct weston_desktop_surface *parent =
+			weston_desktop_surface_get_parent(surface);
+		struct weston_geometry geometry, parent_geometry;
+
+		geometry = weston_desktop_surface_get_geometry(surface);
+		parent_geometry = weston_desktop_surface_get_geometry(parent);
+
+		x += parent_geometry.x - geometry.x;
+		y += parent_geometry.y - geometry.y;
+	}
+	wl_list_for_each(view, &surface->view_list, link)
+		weston_view_set_position(view->view, x, y);
+}
+
+
+static void
+weston_desktop_view_propagate_layer(struct weston_desktop_view *view);
+
+static void
+weston_desktop_surface_committed_common(struct weston_desktop_surface *surface,
+					bool new_buffer, int32_t sx, int32_t sy)
+{
+	if (surface->implementation->committed != NULL)
+		surface->implementation->committed(surface,
+						   surface->implementation_data,
+						   new_buffer, sx, sy);
+
+	if (surface->parent != NULL) {
+		struct weston_desktop_view *view;
+
+		wl_list_for_each(view, &surface->view_list, link) {
+			weston_view_set_transform_parent(view->view,
+							 view->parent->view);
+			weston_desktop_view_propagate_layer(view->parent);
+		}
+		weston_desktop_surface_update_view_position(surface);
+	}
+
+	if (!wl_list_empty(&surface->children_list)) {
+		struct weston_desktop_surface *child;
+
+		wl_list_for_each(child, &surface->children_list, children_link)
+			weston_desktop_surface_update_view_position(child);
+	}
+}
+
+static void
+weston_desktop_view_destroy(struct weston_desktop_view *view)
+{
+	struct weston_desktop_view *child_view, *tmp;
+
+	wl_list_for_each_safe(child_view, tmp, &view->children_list, children_link)
+		weston_desktop_view_destroy(child_view);
+
+	wl_list_remove(&view->children_link);
+	wl_list_remove(&view->link);
+
+	weston_view_damage_below(view->view);
+	if (view->parent != NULL)
+		weston_view_destroy(view->view);
+
+	free(view);
+}
+
+void
+weston_desktop_surface_destroy(struct weston_desktop_surface *surface)
+{
+	struct weston_desktop_view *view, *next_view;
+	struct weston_desktop_surface *child, *next_child;
+
+	wl_list_remove(&surface->surface_commit_listener.link);
+	wl_list_remove(&surface->surface_destroy_listener.link);
+	wl_list_remove(&surface->client_destroy_listener.link);
+
+	if (!wl_list_empty(&surface->resource_list)) {
+		struct wl_resource *resource, *tmp;
+		wl_resource_for_each_safe(resource, tmp, &surface->resource_list) {
+			wl_resource_set_user_data(resource, NULL);
+			wl_list_remove(wl_resource_get_link(resource));
+		}
+	}
+
+	surface->implementation->destroy(surface, surface->implementation_data);
+
+	surface->surface->committed = NULL;
+	surface->surface->committed_private = NULL;
+
+	weston_desktop_surface_unset_relative_to(surface);
+	wl_list_remove(&surface->client_link);
+
+	wl_list_for_each_safe(child, next_child,
+			      &surface->children_list,
+			      children_link)
+		weston_desktop_surface_unset_relative_to(child);
+
+	wl_list_for_each_safe(view, next_view, &surface->view_list, link)
+		weston_desktop_view_destroy(view);
+
+	free(surface->title);
+	free(surface->app_id);
+
+	free(surface);
+}
+
+static void
+weston_desktop_surface_surface_committed(struct wl_listener *listener,
+					 void *data)
+{
+	struct weston_desktop_surface *surface =
+		wl_container_of(listener, surface, surface_commit_listener);
+
+	if (surface->has_new_buffer)
+		surface->has_new_buffer = false;
+	else
+		weston_desktop_surface_committed_common(surface, false, 0, 0);
+}
+
+static void
+weston_desktop_surface_surface_destroyed(struct wl_listener *listener,
+					 void *data)
+{
+	struct weston_desktop_surface *surface =
+		wl_container_of(listener, surface, surface_destroy_listener);
+
+	weston_desktop_surface_destroy(surface);
+}
+
+void
+weston_desktop_surface_resource_destroy(struct wl_resource *resource)
+{
+	struct weston_desktop_surface *surface =
+		wl_resource_get_user_data(resource);
+
+	if (surface != NULL)
+		weston_desktop_surface_destroy(surface);
+}
+
+static void
+weston_desktop_surface_committed(struct weston_surface *wsurface,
+				 int32_t sx, int32_t sy)
+{
+	struct weston_desktop_surface *surface = wsurface->committed_private;
+
+	weston_desktop_surface_committed_common(surface, true, sx, sy);
+	surface->has_new_buffer = true;
+}
+
+static void
+weston_desktop_surface_client_destroyed(struct wl_listener *listener,
+					void *data)
+{
+	struct weston_desktop_surface *surface =
+		wl_container_of(listener, surface, client_destroy_listener);
+
+	weston_desktop_surface_destroy(surface);
+}
+
+struct weston_desktop_surface *
+weston_desktop_surface_create(struct weston_desktop *desktop,
+			      struct weston_desktop_client *client,
+			      struct weston_surface *wsurface,
+			      const struct weston_desktop_surface_implementation *implementation,
+			      void *implementation_data)
+{
+	assert(implementation->destroy != NULL);
+
+	struct weston_desktop_surface *surface;
+
+	surface = zalloc(sizeof(struct weston_desktop_surface));
+	if (surface == NULL) {
+		if (client != NULL)
+			wl_client_post_no_memory(weston_desktop_client_get_client(client));
+		return NULL;
+	}
+
+	surface->desktop = desktop;
+	surface->implementation = implementation;
+	surface->implementation_data = implementation_data;
+	surface->surface = wsurface;
+
+	surface->client = client;
+	surface->client_destroy_listener.notify =
+		weston_desktop_surface_client_destroyed;
+	weston_desktop_client_add_destroy_listener(
+		client, &surface->client_destroy_listener);
+
+	wsurface->committed = weston_desktop_surface_committed;
+	wsurface->committed_private = surface;
+
+	surface->surface_commit_listener.notify =
+		weston_desktop_surface_surface_committed;
+	wl_signal_add(&surface->surface->commit_signal,
+		      &surface->surface_commit_listener);
+	surface->surface_destroy_listener.notify =
+		weston_desktop_surface_surface_destroyed;
+	wl_signal_add(&surface->surface->destroy_signal,
+		      &surface->surface_destroy_listener);
+
+	wl_list_init(&surface->client_link);
+	wl_list_init(&surface->resource_list);
+	wl_list_init(&surface->children_list);
+	wl_list_init(&surface->children_link);
+	wl_list_init(&surface->view_list);
+	wl_list_init(&surface->grab_link);
+
+	return surface;
+}
+
+struct wl_resource *
+weston_desktop_surface_add_resource(struct weston_desktop_surface *surface,
+				    const struct wl_interface *interface,
+				    const void *implementation, uint32_t id,
+				    wl_resource_destroy_func_t destroy)
+{
+	struct wl_resource *client_resource =
+		weston_desktop_client_get_resource(surface->client);
+	struct wl_client *wl_client  =
+		weston_desktop_client_get_client(surface->client);
+	struct wl_resource *resource;
+
+	resource = wl_resource_create(wl_client,
+				      interface,
+				      wl_resource_get_version(client_resource),
+				      id);
+	if (resource == NULL) {
+		wl_client_post_no_memory(wl_client);
+		weston_desktop_surface_destroy(surface);
+		return NULL;
+	}
+	if (destroy == NULL)
+		destroy = weston_desktop_surface_resource_destroy;
+	wl_resource_set_implementation(resource, implementation, surface, destroy);
+	wl_list_insert(&surface->resource_list, wl_resource_get_link(resource));
+
+	return resource;
+}
+
+struct weston_desktop_surface *
+weston_desktop_surface_from_grab_link(struct wl_list *grab_link)
+{
+	struct weston_desktop_surface *surface =
+		wl_container_of(grab_link, surface, grab_link);
+
+	return surface;
+}
+
+WL_EXPORT bool
+weston_surface_is_desktop_surface(struct weston_surface *wsurface)
+{
+	struct weston_desktop_surface *surface = wsurface->committed_private;
+	return wsurface->committed == weston_desktop_surface_committed &&
+	       surface->parent == NULL;
+}
+
+WL_EXPORT struct weston_desktop_surface *
+weston_surface_get_desktop_surface(struct weston_surface *wsurface)
+{
+	if (!weston_surface_is_desktop_surface(wsurface))
+		return NULL;
+	return wsurface->committed_private;
+}
+
+WL_EXPORT void
+weston_desktop_surface_set_user_data(struct weston_desktop_surface *surface,
+				     void *user_data)
+{
+	surface->user_data = user_data;
+}
+
+static struct weston_desktop_view *
+weston_desktop_surface_create_desktop_view(struct weston_desktop_surface *surface)
+{
+	struct wl_client *wl_client=
+		weston_desktop_client_get_client(surface->client);
+	struct weston_desktop_view *view, *child_view;
+	struct weston_view *wview;
+	struct weston_desktop_surface *child;
+
+	wview = weston_view_create(surface->surface);
+	if (wview == NULL) {
+		if (wl_client != NULL)
+			wl_client_post_no_memory(wl_client);
+		return NULL;
+	}
+
+	view = zalloc(sizeof(struct weston_desktop_view));
+	if (view == NULL) {
+		if (wl_client != NULL)
+			wl_client_post_no_memory(wl_client);
+		return NULL;
+	}
+
+	view->view = wview;
+	wl_list_init(&view->children_list);
+	wl_list_init(&view->children_link);
+	wl_list_insert(surface->view_list.prev, &view->link);
+
+	wl_list_for_each(child, &surface->children_list, children_link) {
+		child_view =
+			weston_desktop_surface_create_desktop_view(child);
+		if (child_view == NULL) {
+			weston_desktop_view_destroy(view);
+			return NULL;
+		}
+
+		child_view->parent = view;
+		wl_list_insert(view->children_list.prev,
+			       &child_view->children_link);
+	}
+
+	return view;
+}
+
+WL_EXPORT struct weston_view *
+weston_desktop_surface_create_view(struct weston_desktop_surface *surface)
+{
+	struct weston_desktop_view *view;
+
+	view = weston_desktop_surface_create_desktop_view(surface);
+	if (view == NULL)
+		return NULL;
+
+	return view->view;
+}
+
+WL_EXPORT void
+weston_desktop_surface_destroy_view(struct weston_view *wview)
+{
+	struct weston_desktop_surface *surface;
+	struct weston_desktop_view *view;
+
+	if (!weston_surface_is_desktop_surface(wview->surface))
+		return;
+
+	surface = weston_surface_get_desktop_surface(wview->surface);
+	wl_list_for_each(view, &surface->view_list, link) {
+		if (view->view == wview) {
+			weston_desktop_view_destroy(view);
+			return;
+		}
+	}
+}
+
+static void
+weston_desktop_view_propagate_layer(struct weston_desktop_view *view)
+{
+	struct weston_desktop_view *child;
+	struct wl_list *link = &view->view->layer_link.link;
+
+	wl_list_for_each_reverse(child, &view->children_list, children_link) {
+		struct weston_layer_entry *prev =
+			wl_container_of(link->prev, prev, link);
+
+		if (prev == &child->view->layer_link)
+			continue;
+
+		child->view->is_mapped = true;
+		weston_view_damage_below(child->view);
+		weston_view_geometry_dirty(child->view);
+		weston_layer_entry_remove(&child->view->layer_link);
+		weston_layer_entry_insert(prev, &child->view->layer_link);
+		weston_view_geometry_dirty(child->view);
+		weston_surface_damage(child->view->surface);
+		weston_view_update_transform(child->view);
+
+		weston_desktop_view_propagate_layer(child);
+	}
+}
+
+WL_EXPORT void
+weston_desktop_surface_propagate_layer(struct weston_desktop_surface *surface)
+{
+	struct weston_desktop_view *view;
+
+	wl_list_for_each(view, &surface->view_list, link)
+		weston_desktop_view_propagate_layer(view);
+}
+
+WL_EXPORT void
+weston_desktop_surface_set_activated(struct weston_desktop_surface *surface, bool activated)
+{
+	if (surface->implementation->set_activated != NULL)
+		surface->implementation->set_activated(surface,
+						       surface->implementation_data,
+						       activated);
+}
+
+WL_EXPORT void
+weston_desktop_surface_set_fullscreen(struct weston_desktop_surface *surface, bool fullscreen)
+{
+	if (surface->implementation->set_fullscreen != NULL)
+		surface->implementation->set_fullscreen(surface,
+							surface->implementation_data,
+							fullscreen);
+}
+
+WL_EXPORT void
+weston_desktop_surface_set_maximized(struct weston_desktop_surface *surface, bool maximized)
+{
+	if (surface->implementation->set_maximized != NULL)
+		surface->implementation->set_maximized(surface,
+						       surface->implementation_data,
+						       maximized);
+}
+
+WL_EXPORT void
+weston_desktop_surface_set_resizing(struct weston_desktop_surface *surface, bool resizing)
+{
+	if (surface->implementation->set_resizing != NULL)
+		surface->implementation->set_resizing(surface,
+						      surface->implementation_data,
+						      resizing);
+}
+
+WL_EXPORT void
+weston_desktop_surface_set_size(struct weston_desktop_surface *surface, int32_t width, int32_t height)
+{
+	if (surface->implementation->set_size != NULL)
+		surface->implementation->set_size(surface,
+						  surface->implementation_data,
+						  width, height);
+}
+
+WL_EXPORT void
+weston_desktop_surface_close(struct weston_desktop_surface *surface)
+{
+	if (surface->implementation->close != NULL)
+		surface->implementation->close(surface,
+					       surface->implementation_data);
+}
+
+struct weston_desktop_surface *
+weston_desktop_surface_from_client_link(struct wl_list *link)
+{
+	struct weston_desktop_surface *surface;
+
+	surface = wl_container_of(link, surface, client_link);
+	return surface;
+}
+
+struct wl_list *
+weston_desktop_surface_get_client_link(struct weston_desktop_surface *surface)
+{
+	return &surface->client_link;
+}
+
+bool
+weston_desktop_surface_has_implementation(struct weston_desktop_surface *surface,
+					  const struct weston_desktop_surface_implementation *implementation)
+{
+	return surface->implementation == implementation;
+}
+
+const struct weston_desktop_surface_implementation *
+weston_desktop_surface_get_implementation(struct weston_desktop_surface *surface)
+{
+	return surface->implementation;
+}
+
+void *
+weston_desktop_surface_get_implementation_data(struct weston_desktop_surface *surface)
+{
+	return surface->implementation_data;
+}
+
+struct weston_desktop_surface *
+weston_desktop_surface_get_parent(struct weston_desktop_surface *surface)
+{
+	return surface->parent;
+}
+
+bool
+weston_desktop_surface_get_grab(struct weston_desktop_surface *surface)
+{
+	return !wl_list_empty(&surface->grab_link);
+}
+
+WL_EXPORT struct weston_desktop_client *
+weston_desktop_surface_get_client(struct weston_desktop_surface *surface)
+{
+	return surface->client;
+}
+
+WL_EXPORT void *
+weston_desktop_surface_get_user_data(struct weston_desktop_surface *surface)
+{
+	return surface->user_data;
+}
+
+WL_EXPORT struct weston_surface *
+weston_desktop_surface_get_surface(struct weston_desktop_surface *surface)
+{
+	return surface->surface;
+}
+
+WL_EXPORT const char *
+weston_desktop_surface_get_title(struct weston_desktop_surface *surface)
+{
+	return surface->title;
+}
+
+WL_EXPORT const char *
+weston_desktop_surface_get_app_id(struct weston_desktop_surface *surface)
+{
+	return surface->app_id;
+}
+
+WL_EXPORT pid_t
+weston_desktop_surface_get_pid(struct weston_desktop_surface *surface)
+{
+	pid_t pid;
+
+	if (surface->pid != 0) {
+		pid = surface->pid;
+	} else {
+		struct weston_desktop_client *client =
+			weston_desktop_surface_get_client(surface);
+		struct wl_client *wl_client =
+			weston_desktop_client_get_client(client);
+
+		wl_client_get_credentials(wl_client, &pid, NULL, NULL);
+	}
+	return pid;
+}
+
+WL_EXPORT bool
+weston_desktop_surface_get_activated(struct weston_desktop_surface *surface)
+{
+	if (surface->implementation->get_activated == NULL)
+		return false;
+	return surface->implementation->get_activated(surface,
+						      surface->implementation_data);
+}
+
+WL_EXPORT bool
+weston_desktop_surface_get_resizing(struct weston_desktop_surface *surface)
+{
+	if (surface->implementation->get_resizing == NULL)
+		return false;
+	return surface->implementation->get_resizing(surface,
+						     surface->implementation_data);
+}
+
+WL_EXPORT bool
+weston_desktop_surface_get_maximized(struct weston_desktop_surface *surface)
+{
+	if (surface->implementation->get_maximized == NULL)
+		return false;
+	return surface->implementation->get_maximized(surface,
+						      surface->implementation_data);
+}
+
+WL_EXPORT bool
+weston_desktop_surface_get_fullscreen(struct weston_desktop_surface *surface)
+{
+	if (surface->implementation->get_fullscreen == NULL)
+		return false;
+	return surface->implementation->get_fullscreen(surface,
+						       surface->implementation_data);
+}
+
+WL_EXPORT struct weston_geometry
+weston_desktop_surface_get_geometry(struct weston_desktop_surface *surface)
+{
+	if (surface->has_geometry)
+		return surface->geometry;
+	return weston_surface_get_bounding_box(surface->surface);
+}
+
+WL_EXPORT struct weston_size
+weston_desktop_surface_get_max_size(struct weston_desktop_surface *surface)
+{
+	struct weston_size size = { 0, 0 };
+
+	if (surface->implementation->get_max_size == NULL)
+		return size;
+	return surface->implementation->get_max_size(surface,
+						     surface->implementation_data);
+}
+
+WL_EXPORT struct weston_size
+weston_desktop_surface_get_min_size(struct weston_desktop_surface *surface)
+{
+	struct weston_size size = { 0, 0 };
+
+	if (surface->implementation->get_min_size == NULL)
+		return size;
+	return surface->implementation->get_min_size(surface,
+						     surface->implementation_data);
+}
+
+void
+weston_desktop_surface_set_title(struct weston_desktop_surface *surface,
+				 const char *title)
+{
+	char *tmp;
+
+	tmp = strdup(title);
+	if (tmp == NULL)
+		return;
+
+	free(surface->title);
+	surface->title = tmp;
+}
+
+void
+weston_desktop_surface_set_app_id(struct weston_desktop_surface *surface,
+				  const char *app_id)
+{
+	char *tmp;
+
+	tmp = strdup(app_id);
+	if (tmp == NULL)
+		return;
+
+	free(surface->app_id);
+	surface->app_id = tmp;
+}
+
+void
+weston_desktop_surface_set_pid(struct weston_desktop_surface *surface,
+			       pid_t pid)
+{
+	surface->pid = pid;
+}
+
+void
+weston_desktop_surface_set_geometry(struct weston_desktop_surface *surface,
+				    struct weston_geometry geometry)
+{
+	surface->has_geometry = true;
+	surface->geometry = geometry;
+}
+
+void
+weston_desktop_surface_set_relative_to(struct weston_desktop_surface *surface,
+				       struct weston_desktop_surface *parent,
+				       int32_t x, int32_t y, bool use_geometry)
+{
+	struct weston_desktop_view *view, *parent_view;
+	struct wl_list *link, *tmp;
+
+	assert(parent);
+
+	surface->position.x = x;
+	surface->position.y = y;
+	surface->use_geometry = use_geometry;
+
+	if (surface->parent == parent)
+		return;
+
+	surface->parent = parent;
+	wl_list_remove(&surface->children_link);
+	wl_list_insert(surface->parent->children_list.prev,
+		       &surface->children_link);
+
+	link = surface->view_list.next;
+	tmp = link->next;
+	wl_list_for_each(parent_view, &parent->view_list, link) {
+		if (link == &surface->view_list) {
+			view = weston_desktop_surface_create_desktop_view(surface);
+			if (view == NULL)
+				return;
+			tmp = &surface->view_list;
+		} else {
+			view = wl_container_of(link, view, link);
+			wl_list_remove(&view->children_link);
+		}
+
+		view->parent = parent_view;
+		wl_list_insert(parent_view->children_list.prev,
+			       &view->children_link);
+		weston_desktop_view_propagate_layer(view);
+
+		link = tmp;
+		tmp = link->next;
+	}
+	for (; link != &surface->view_list; link = tmp, tmp = link->next) {
+		view = wl_container_of(link, view, link);
+		weston_desktop_view_destroy(view);
+	}
+}
+
+void
+weston_desktop_surface_unset_relative_to(struct weston_desktop_surface *surface)
+{
+	struct weston_desktop_view *view, *tmp;
+
+	if (surface->parent == NULL)
+		return;
+
+	surface->parent = NULL;
+	wl_list_remove(&surface->children_link);
+	wl_list_init(&surface->children_link);
+
+	wl_list_for_each_safe(view, tmp, &surface->view_list, link)
+		weston_desktop_view_destroy(view);
+}
+
+void
+weston_desktop_surface_popup_grab(struct weston_desktop_surface *surface,
+				  struct weston_desktop_seat *seat,
+				  uint32_t serial)
+{
+	struct wl_client *wl_client =
+		weston_desktop_client_get_client(surface->client);
+	if (weston_desktop_seat_popup_grab_start(seat, wl_client, serial))
+		weston_desktop_seat_popup_grab_add_surface(seat, &surface->grab_link);
+	else
+		weston_desktop_surface_popup_dismiss(surface);
+}
+
+void
+weston_desktop_surface_popup_ungrab(struct weston_desktop_surface *surface,
+				   struct weston_desktop_seat *seat)
+{
+	weston_desktop_seat_popup_grab_remove_surface(seat, &surface->grab_link);
+}
+
+void
+weston_desktop_surface_popup_dismiss(struct weston_desktop_surface *surface)
+{
+	struct weston_desktop_view *view, *tmp;
+
+	wl_list_for_each_safe(view, tmp, &surface->view_list, link)
+		weston_desktop_view_destroy(view);
+	wl_list_remove(&surface->grab_link);
+	wl_list_init(&surface->grab_link);
+	weston_desktop_surface_close(surface);
+}
diff --git a/libweston-desktop/wl-shell.c b/libweston-desktop/wl-shell.c
new file mode 100644
index 0000000..12409cd
--- /dev/null
+++ b/libweston-desktop/wl-shell.c
@@ -0,0 +1,464 @@
+/*
+ * Copyright © 2010-2012 Intel Corporation
+ * Copyright © 2011-2012 Collabora, Ltd.
+ * Copyright © 2013 Raspberry Pi Foundation
+ * Copyright © 2016 Quentin "Sardem FF7" Glidic
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <assert.h>
+
+#include <wayland-server.h>
+
+#include "compositor.h"
+#include "zalloc.h"
+
+#include "libweston-desktop.h"
+#include "internal.h"
+
+#define WD_WL_SHELL_PROTOCOL_VERSION 1
+
+enum weston_desktop_wl_shell_surface_state {
+	NONE,
+	TOPLEVEL,
+	MAXIMIZED,
+	FULLSCREEN,
+	TRANSIENT,
+	POPUP,
+};
+
+struct weston_desktop_wl_shell_surface {
+	struct wl_resource *resource;
+	struct weston_desktop *desktop;
+	struct wl_display *display;
+	struct weston_desktop_surface *surface;
+	struct weston_desktop_surface *parent;
+	bool added;
+	struct weston_desktop_seat *popup_seat;
+	enum weston_desktop_wl_shell_surface_state state;
+};
+
+static void
+weston_desktop_wl_shell_surface_set_size(struct weston_desktop_surface *dsurface,
+					 void *user_data,
+					 int32_t width, int32_t height)
+{
+	struct weston_desktop_wl_shell_surface *surface = user_data;
+	struct weston_surface *wsurface =
+		weston_desktop_surface_get_surface(surface->surface);
+
+	if (wsurface->width == width && wsurface->height == height)
+		return;
+
+	wl_shell_surface_send_configure(surface->resource,
+					WL_SHELL_SURFACE_RESIZE_NONE,
+					width, height);
+}
+
+static void
+weston_desktop_wl_shell_surface_maybe_ungrab(struct weston_desktop_wl_shell_surface *surface)
+{
+	if (surface->state != POPUP ||
+	    !weston_desktop_surface_get_grab(surface->surface))
+		return;
+
+	weston_desktop_surface_popup_ungrab(surface->surface,
+					    surface->popup_seat);
+	surface->popup_seat = NULL;
+}
+
+static void
+weston_desktop_wl_shell_surface_committed(struct weston_desktop_surface *dsurface,
+					  void *user_data, bool new_buffer,
+					  int32_t sx, int32_t sy)
+{
+	struct weston_desktop_wl_shell_surface *surface = user_data;
+	struct weston_surface *wsurface =
+		weston_desktop_surface_get_surface(dsurface);
+
+	if (wsurface->buffer_ref.buffer == NULL)
+		weston_desktop_wl_shell_surface_maybe_ungrab(surface);
+
+	if (surface->added)
+		weston_desktop_api_committed(surface->desktop, surface->surface,
+					     sx, sy);
+}
+
+static void
+weston_desktop_wl_shell_surface_ping(struct weston_desktop_surface *dsurface,
+				     uint32_t serial, void *user_data)
+{
+	struct weston_desktop_wl_shell_surface *surface = user_data;
+
+	wl_shell_surface_send_ping(surface->resource, serial);
+}
+
+static void
+weston_desktop_wl_shell_surface_close(struct weston_desktop_surface *dsurface,
+				      void *user_data)
+{
+	struct weston_desktop_wl_shell_surface *surface = user_data;
+
+	if (surface->state == POPUP)
+		wl_shell_surface_send_popup_done(surface->resource);
+}
+
+static bool
+weston_desktop_wl_shell_surface_get_maximized(struct weston_desktop_surface *dsurface,
+					      void *user_data)
+{
+	struct weston_desktop_wl_shell_surface *surface = user_data;
+
+	return surface->state == MAXIMIZED;
+}
+
+static bool
+weston_desktop_wl_shell_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
+					       void *user_data)
+{
+	struct weston_desktop_wl_shell_surface *surface = user_data;
+
+	return surface->state == FULLSCREEN;
+}
+
+static void
+weston_desktop_wl_shell_change_state(struct weston_desktop_wl_shell_surface *surface,
+				     enum weston_desktop_wl_shell_surface_state state,
+				     struct weston_desktop_surface *parent,
+				     int32_t x, int32_t y)
+{
+	bool to_add = (parent == NULL);
+
+	assert(state != NONE);
+
+	if (to_add && surface->added)
+		return;
+
+	if (surface->state != state) {
+		if (surface->state == POPUP)
+			weston_desktop_wl_shell_surface_maybe_ungrab(surface);
+
+		if (to_add) {
+			weston_desktop_surface_unset_relative_to(surface->surface);
+			weston_desktop_api_surface_added(surface->desktop,
+							 surface->surface);
+		} else if (surface->added) {
+			weston_desktop_api_surface_removed(surface->desktop,
+							   surface->surface);
+		}
+
+		surface->state = state;
+		surface->added = to_add;
+	}
+
+	if (parent != NULL)
+		weston_desktop_surface_set_relative_to(surface->surface, parent,
+						       x, y, false);
+}
+
+static void
+weston_desktop_wl_shell_surface_destroy(struct weston_desktop_surface *dsurface,
+					void *user_data)
+{
+	struct weston_desktop_wl_shell_surface *surface = user_data;
+
+	weston_desktop_wl_shell_surface_maybe_ungrab(surface);
+	weston_desktop_surface_unset_relative_to(surface->surface);
+	if (surface->added)
+		weston_desktop_api_surface_removed(surface->desktop,
+						   surface->surface);
+
+	free(surface);
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_pong(struct wl_client *wl_client,
+					      struct wl_resource *resource,
+					      uint32_t serial)
+{
+	struct weston_desktop_surface *surface = wl_resource_get_user_data(resource);
+
+	weston_desktop_client_pong(weston_desktop_surface_get_client(surface), serial);
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_move(struct wl_client *wl_client,
+					      struct wl_resource *resource,
+					      struct wl_resource *seat_resource,
+					      uint32_t serial)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_seat *seat =
+		wl_resource_get_user_data(seat_resource);
+	struct weston_desktop_wl_shell_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_api_move(surface->desktop, dsurface, seat, serial);
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_resize(struct wl_client *wl_client,
+						struct wl_resource *resource,
+						struct wl_resource *seat_resource,
+						uint32_t serial,
+						enum wl_shell_surface_resize edges)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
+	struct weston_desktop_wl_shell_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_api_resize(surface->desktop, dsurface, seat, serial, edges);
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_set_toplevel(struct wl_client *wl_client,
+						struct wl_resource *resource)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_wl_shell_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_wl_shell_change_state(surface, TOPLEVEL, NULL, 0, 0);
+	if (surface->parent == NULL)
+		return;
+	surface->parent = NULL;
+	weston_desktop_api_set_parent(surface->desktop, surface->surface, NULL);
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_set_transient(struct wl_client *wl_client,
+						       struct wl_resource *resource,
+						       struct wl_resource *parent_resource,
+						       int32_t x, int32_t y,
+						       enum wl_shell_surface_transient flags)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_surface *wparent =
+		wl_resource_get_user_data(parent_resource);
+	struct weston_desktop_surface *parent;
+	struct weston_desktop_wl_shell_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	if (!weston_surface_is_desktop_surface(wparent))
+		return;
+
+	parent = weston_surface_get_desktop_surface(wparent);
+	if (flags & WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
+		weston_desktop_wl_shell_change_state(surface, TRANSIENT, parent,
+						     x, y);
+	} else {
+		weston_desktop_wl_shell_change_state(surface, TOPLEVEL, NULL,
+						     0, 0);
+		surface->parent = parent;
+		weston_desktop_api_set_parent(surface->desktop,
+					      surface->surface, parent);
+	}
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_set_fullscreen(struct wl_client *wl_client,
+							struct wl_resource *resource,
+							enum wl_shell_surface_fullscreen_method method,
+							uint32_t framerate,
+							struct wl_resource *output_resource)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_wl_shell_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+	struct weston_output *output = NULL;
+
+	if (output_resource != NULL)
+		output = wl_resource_get_user_data(output_resource);
+
+	weston_desktop_wl_shell_change_state(surface, FULLSCREEN, NULL, 0, 0);
+	weston_desktop_api_fullscreen_requested(surface->desktop, dsurface,
+						true, output);
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_set_popup(struct wl_client *wl_client,
+						   struct wl_resource *resource,
+						   struct wl_resource *seat_resource,
+						   uint32_t serial,
+						   struct wl_resource *parent_resource,
+						   int32_t x, int32_t y,
+						   enum wl_shell_surface_transient flags)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
+	struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
+	struct weston_surface *parent =
+		wl_resource_get_user_data(parent_resource);
+	struct weston_desktop_surface *parent_surface;
+	struct weston_desktop_wl_shell_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	if (seat == NULL) {
+		wl_client_post_no_memory(wl_client);
+		return;
+	}
+
+	if (!weston_surface_is_desktop_surface(parent))
+		return;
+
+	parent_surface = weston_surface_get_desktop_surface(parent);
+
+	weston_desktop_wl_shell_change_state(surface, POPUP,
+					     parent_surface, x, y);
+	weston_desktop_surface_popup_grab(surface->surface, seat, serial);
+	surface->popup_seat = seat;
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_set_maximized(struct wl_client *wl_client,
+						       struct wl_resource *resource,
+						       struct wl_resource *output_resource)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_wl_shell_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_wl_shell_change_state(surface, MAXIMIZED, NULL, 0, 0);
+	weston_desktop_api_maximized_requested(surface->desktop, dsurface, true);
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_set_title(struct wl_client *wl_client,
+						   struct wl_resource *resource,
+						   const char *title)
+{
+	struct weston_desktop_surface *surface =
+		wl_resource_get_user_data(resource);
+
+	weston_desktop_surface_set_title(surface, title);
+}
+
+static void
+weston_desktop_wl_shell_surface_protocol_set_class(struct wl_client *wl_client,
+						   struct wl_resource *resource,
+						   const char *class_)
+{
+	struct weston_desktop_surface *surface =
+		wl_resource_get_user_data(resource);
+
+	weston_desktop_surface_set_app_id(surface, class_);
+}
+
+
+static const struct wl_shell_surface_interface weston_desktop_wl_shell_surface_implementation = {
+	.pong           = weston_desktop_wl_shell_surface_protocol_pong,
+	.move           = weston_desktop_wl_shell_surface_protocol_move,
+	.resize         = weston_desktop_wl_shell_surface_protocol_resize,
+	.set_toplevel   = weston_desktop_wl_shell_surface_protocol_set_toplevel,
+	.set_transient  = weston_desktop_wl_shell_surface_protocol_set_transient,
+	.set_fullscreen = weston_desktop_wl_shell_surface_protocol_set_fullscreen,
+	.set_popup      = weston_desktop_wl_shell_surface_protocol_set_popup,
+	.set_maximized  = weston_desktop_wl_shell_surface_protocol_set_maximized,
+	.set_title      = weston_desktop_wl_shell_surface_protocol_set_title,
+	.set_class      = weston_desktop_wl_shell_surface_protocol_set_class,
+};
+
+static const struct weston_desktop_surface_implementation weston_desktop_wl_shell_surface_internal_implementation = {
+	.set_size = weston_desktop_wl_shell_surface_set_size,
+	.committed = weston_desktop_wl_shell_surface_committed,
+	.ping = weston_desktop_wl_shell_surface_ping,
+	.close = weston_desktop_wl_shell_surface_close,
+
+	.get_maximized = weston_desktop_wl_shell_surface_get_maximized,
+	.get_fullscreen = weston_desktop_wl_shell_surface_get_fullscreen,
+
+	.destroy = weston_desktop_wl_shell_surface_destroy,
+};
+
+static void
+weston_desktop_wl_shell_protocol_get_shell_surface(struct wl_client *wl_client,
+						   struct wl_resource *resource,
+						   uint32_t id,
+						   struct wl_resource *surface_resource)
+{
+	struct weston_desktop_client *client = wl_resource_get_user_data(resource);
+	struct weston_surface *wsurface = wl_resource_get_user_data(surface_resource);
+	struct weston_desktop_wl_shell_surface *surface;
+
+
+	if (weston_surface_set_role(wsurface, "wl_shell_surface", resource, WL_SHELL_ERROR_ROLE) < 0)
+		return;
+
+	surface = zalloc(sizeof(struct weston_desktop_wl_shell_surface));
+	if (surface == NULL) {
+		wl_client_post_no_memory(wl_client);
+		return;
+	}
+
+	surface->desktop = weston_desktop_client_get_desktop(client);
+	surface->display = weston_desktop_get_display(surface->desktop);
+
+	surface->surface =
+		weston_desktop_surface_create(surface->desktop, client, wsurface,
+					      &weston_desktop_wl_shell_surface_internal_implementation,
+					      surface);
+	if (surface->surface == NULL) {
+		free(surface);
+		return;
+	}
+
+	surface->resource =
+		weston_desktop_surface_add_resource(surface->surface,
+						    &wl_shell_surface_interface,
+						    &weston_desktop_wl_shell_surface_implementation,
+						    id, NULL);
+}
+
+
+static const struct wl_shell_interface weston_desktop_wl_shell_implementation = {
+	.get_shell_surface = weston_desktop_wl_shell_protocol_get_shell_surface,
+};
+
+static void
+weston_desktop_wl_shell_bind(struct wl_client *client, void *data,
+			     uint32_t version, uint32_t id)
+{
+	struct weston_desktop *desktop = data;
+
+	weston_desktop_client_create(desktop, client, NULL, &wl_shell_interface,
+				     &weston_desktop_wl_shell_implementation,
+				     version, id);
+}
+
+struct wl_global *
+weston_desktop_wl_shell_create(struct weston_desktop *desktop,
+			       struct wl_display *display)
+{
+	return wl_global_create(display,
+				&wl_shell_interface,
+				WD_WL_SHELL_PROTOCOL_VERSION, desktop,
+				weston_desktop_wl_shell_bind);
+}
diff --git a/libweston-desktop/xdg-shell-v5.c b/libweston-desktop/xdg-shell-v5.c
new file mode 100644
index 0000000..8a1383d
--- /dev/null
+++ b/libweston-desktop/xdg-shell-v5.c
@@ -0,0 +1,798 @@
+/*
+ * Copyright © 2010-2012 Intel Corporation
+ * Copyright © 2011-2012 Collabora, Ltd.
+ * Copyright © 2013 Raspberry Pi Foundation
+ * Copyright © 2016 Quentin "Sardem FF7" Glidic
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <stdbool.h>
+
+#include <wayland-server.h>
+
+#include "compositor.h"
+#include "zalloc.h"
+#include "protocol/xdg-shell-unstable-v5-server-protocol.h"
+
+#include "libweston-desktop.h"
+#include "internal.h"
+
+#define WD_XDG_SHELL_PROTOCOL_VERSION 1
+
+struct weston_desktop_xdg_surface {
+	struct wl_resource *resource;
+	struct weston_desktop_surface *surface;
+	struct weston_desktop *desktop;
+	struct wl_event_source *add_idle;
+	struct wl_event_source *configure_idle;
+	uint32_t configure_serial;
+	struct weston_size requested_size;
+	struct {
+		bool maximized;
+		bool fullscreen;
+		bool resizing;
+		bool activated;
+	} requested_state, next_state, state;
+	bool has_next_geometry;
+	struct weston_geometry next_geometry;
+};
+
+struct weston_desktop_xdg_popup {
+	struct wl_resource *resource;
+	struct weston_desktop_surface *popup;
+	struct weston_desktop *desktop;
+	struct weston_desktop_seat *seat;
+	struct wl_display *display;
+};
+
+static void
+weston_desktop_xdg_surface_send_configure(void *data)
+{
+	struct weston_desktop_xdg_surface *surface = data;
+	uint32_t *s;
+	struct wl_array states;
+
+	surface->configure_idle = NULL;
+
+	surface->configure_serial =
+		wl_display_next_serial(weston_desktop_get_display(surface->desktop));
+
+	wl_array_init(&states);
+	if (surface->requested_state.maximized) {
+		s = wl_array_add(&states, sizeof(uint32_t));
+		*s = XDG_SURFACE_STATE_MAXIMIZED;
+	}
+	if (surface->requested_state.fullscreen) {
+		s = wl_array_add(&states, sizeof(uint32_t));
+		*s = XDG_SURFACE_STATE_FULLSCREEN;
+	}
+	if (surface->requested_state.resizing) {
+		s = wl_array_add(&states, sizeof(uint32_t));
+		*s = XDG_SURFACE_STATE_RESIZING;
+	}
+	if (surface->requested_state.activated) {
+		s = wl_array_add(&states, sizeof(uint32_t));
+		*s = XDG_SURFACE_STATE_ACTIVATED;
+	}
+
+	xdg_surface_send_configure(surface->resource,
+				   surface->requested_size.width,
+				   surface->requested_size.height,
+				   &states,
+				   surface->configure_serial);
+
+	wl_array_release(&states);
+};
+
+static void
+weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface)
+{
+	struct wl_display *display = weston_desktop_get_display(surface->desktop);
+	struct wl_event_loop *loop = wl_display_get_event_loop(display);
+
+	if (surface->configure_idle != NULL)
+		return;
+	surface->configure_idle =
+		wl_event_loop_add_idle(loop,
+				       weston_desktop_xdg_surface_send_configure,
+				       surface);
+}
+
+static void
+weston_desktop_xdg_surface_set_maximized(struct weston_desktop_surface *dsurface,
+					 void *user_data, bool maximized)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	if (surface->state.maximized == maximized)
+		return;
+
+	surface->requested_state.maximized = maximized;
+	weston_desktop_xdg_surface_schedule_configure(surface);
+}
+
+static void
+weston_desktop_xdg_surface_set_fullscreen(struct weston_desktop_surface *dsurface,
+					  void *user_data, bool fullscreen)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	if (surface->state.fullscreen == fullscreen)
+		return;
+
+	surface->requested_state.fullscreen = fullscreen;
+	weston_desktop_xdg_surface_schedule_configure(surface);
+}
+
+static void
+weston_desktop_xdg_surface_set_resizing(struct weston_desktop_surface *dsurface,
+					void *user_data, bool resizing)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	if (surface->state.resizing == resizing)
+		return;
+
+	surface->requested_state.resizing = resizing;
+	weston_desktop_xdg_surface_schedule_configure(surface);
+}
+
+static void
+weston_desktop_xdg_surface_set_activated(struct weston_desktop_surface *dsurface,
+					 void *user_data, bool activated)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	if (surface->state.activated == activated)
+		return;
+
+	surface->requested_state.activated = activated;
+	weston_desktop_xdg_surface_schedule_configure(surface);
+}
+
+static void
+weston_desktop_xdg_surface_set_size(struct weston_desktop_surface *dsurface,
+				    void *user_data,
+				    int32_t width, int32_t height)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+	struct weston_surface *wsurface = weston_desktop_surface_get_surface(surface->surface);
+
+	if (wsurface->width == width && wsurface->height == height)
+		return;
+
+	surface->requested_size.width = width;
+	surface->requested_size.height = height;
+	weston_desktop_xdg_surface_schedule_configure(surface);
+}
+
+static void
+weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
+				     void *user_data, bool new_buffer,
+				     int32_t sx, int32_t sy)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+	struct weston_surface *wsurface =
+		weston_desktop_surface_get_surface(surface->surface);
+	bool reconfigure = false;
+
+	if (surface->next_state.maximized || surface->next_state.fullscreen)
+		reconfigure = surface->requested_size.width != wsurface->width ||
+			      surface->requested_size.height != wsurface->height;
+
+	if (reconfigure) {
+		weston_desktop_xdg_surface_schedule_configure(surface);
+	} else {
+		surface->state = surface->next_state;
+		if (surface->has_next_geometry) {
+			surface->has_next_geometry = false;
+			weston_desktop_surface_set_geometry(surface->surface,
+							    surface->next_geometry);
+		}
+
+		if (surface->add_idle != NULL) {
+			wl_event_source_remove(surface->add_idle);
+			surface->add_idle = NULL;
+			weston_desktop_api_surface_added(surface->desktop,
+						       surface->surface);
+		}
+		weston_desktop_api_committed(surface->desktop, surface->surface,
+					     sx, sy);
+	}
+}
+
+static void
+weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
+				uint32_t serial, void *user_data)
+{
+	struct weston_desktop_client *client =
+		weston_desktop_surface_get_client(dsurface);
+
+	xdg_shell_send_ping(weston_desktop_client_get_resource(client),
+			    serial);
+}
+
+static void
+weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
+				 void *user_data)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	xdg_surface_send_close(surface->resource);
+}
+
+static bool
+weston_desktop_xdg_surface_get_maximized(struct weston_desktop_surface *dsurface,
+					 void *user_data)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	return surface->state.maximized;
+}
+
+static bool
+weston_desktop_xdg_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
+					  void *user_data)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	return surface->state.fullscreen;
+}
+
+static bool
+weston_desktop_xdg_surface_get_resizing(struct weston_desktop_surface *dsurface,
+					void *user_data)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	return surface->state.resizing;
+}
+
+static bool
+weston_desktop_xdg_surface_get_activated(struct weston_desktop_surface *dsurface,
+					 void *user_data)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	return surface->state.activated;
+}
+
+static void
+weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
+				   void *user_data)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+
+	weston_desktop_api_surface_removed(surface->desktop, surface->surface);
+
+	if (surface->add_idle != NULL)
+		wl_event_source_remove(surface->add_idle);
+
+	if (surface->configure_idle != NULL)
+		wl_event_source_remove(surface->configure_idle);
+
+	free(surface);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_set_parent(struct wl_client *wl_client,
+					       struct wl_resource *resource,
+					       struct wl_resource *parent_resource)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+	struct weston_desktop_surface *parent = NULL;
+
+	if (parent_resource != NULL)
+		parent = wl_resource_get_user_data(parent_resource);
+	weston_desktop_api_set_parent(surface->desktop, dsurface, parent);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_set_title(struct wl_client *wl_client,
+					      struct wl_resource *resource,
+					      const char *title)
+{
+	struct weston_desktop_surface *surface =
+		wl_resource_get_user_data(resource);
+
+	weston_desktop_surface_set_title(surface, title);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_set_app_id(struct wl_client *wl_client,
+					       struct wl_resource *resource,
+					       const char *app_id)
+{
+	struct weston_desktop_surface *surface =
+		wl_resource_get_user_data(resource);
+
+	weston_desktop_surface_set_app_id(surface, app_id);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_show_window_menu(struct wl_client *wl_client,
+						     struct wl_resource *resource,
+						     struct wl_resource *seat_resource,
+						     uint32_t serial,
+						     int32_t x, int32_t y)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_seat *seat =
+		wl_resource_get_user_data(seat_resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_api_show_window_menu(surface->desktop, dsurface, seat, x, y);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_move(struct wl_client *wl_client,
+					 struct wl_resource *resource,
+					 struct wl_resource *seat_resource,
+					 uint32_t serial)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_seat *seat =
+		wl_resource_get_user_data(seat_resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_api_move(surface->desktop, dsurface, seat, serial);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_resize(struct wl_client *wl_client,
+					   struct wl_resource *resource,
+					   struct wl_resource *seat_resource,
+					   uint32_t serial,
+					   enum xdg_surface_resize_edge edges)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_seat *seat =
+		wl_resource_get_user_data(seat_resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_api_resize(surface->desktop, dsurface, seat, serial, edges);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
+						  struct wl_resource *resource,
+						  uint32_t serial)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	if (surface->configure_serial != serial)
+		return;
+
+	surface->next_state = surface->requested_state;
+}
+
+static void
+weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
+							struct wl_resource *resource,
+							int32_t x, int32_t y,
+							int32_t width, int32_t height)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	surface->has_next_geometry = true;
+	surface->next_geometry.x = x;
+	surface->next_geometry.y = y;
+	surface->next_geometry.width = width;
+	surface->next_geometry.height = height;
+}
+
+static void
+weston_desktop_xdg_surface_protocol_set_maximized(struct wl_client *wl_client,
+						  struct wl_resource *resource)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_api_maximized_requested(surface->desktop, dsurface, true);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_unset_maximized(struct wl_client *wl_client,
+						    struct wl_resource *resource)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_api_maximized_requested(surface->desktop, dsurface, false);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_set_fullscreen(struct wl_client *wl_client,
+						   struct wl_resource *resource,
+						   struct wl_resource *output_resource)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+	struct weston_output *output = NULL;
+
+	if (output_resource != NULL)
+		output = wl_resource_get_user_data(output_resource);
+
+	weston_desktop_api_fullscreen_requested(surface->desktop, dsurface,
+						true, output);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_unset_fullscreen(struct wl_client *wl_client,
+					       struct wl_resource *resource)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_api_fullscreen_requested(surface->desktop, dsurface,
+						false, NULL);
+}
+
+static void
+weston_desktop_xdg_surface_protocol_set_minimized(struct wl_client *wl_client,
+						  struct wl_resource *resource)
+{
+	struct weston_desktop_surface *dsurface =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop_xdg_surface *surface =
+		weston_desktop_surface_get_implementation_data(dsurface);
+
+	weston_desktop_api_minimized_requested(surface->desktop, dsurface);
+}
+
+static const struct xdg_surface_interface weston_desktop_xdg_surface_implementation = {
+	.destroy             = weston_desktop_destroy_request,
+	.set_parent          = weston_desktop_xdg_surface_protocol_set_parent,
+	.set_title           = weston_desktop_xdg_surface_protocol_set_title,
+	.set_app_id          = weston_desktop_xdg_surface_protocol_set_app_id,
+	.show_window_menu    = weston_desktop_xdg_surface_protocol_show_window_menu,
+	.move                = weston_desktop_xdg_surface_protocol_move,
+	.resize              = weston_desktop_xdg_surface_protocol_resize,
+	.ack_configure       = weston_desktop_xdg_surface_protocol_ack_configure,
+	.set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
+	.set_maximized       = weston_desktop_xdg_surface_protocol_set_maximized,
+	.unset_maximized     = weston_desktop_xdg_surface_protocol_unset_maximized,
+	.set_fullscreen      = weston_desktop_xdg_surface_protocol_set_fullscreen,
+	.unset_fullscreen    = weston_desktop_xdg_surface_protocol_unset_fullscreen,
+	.set_minimized       = weston_desktop_xdg_surface_protocol_set_minimized,
+};
+
+static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
+	.set_maximized = weston_desktop_xdg_surface_set_maximized,
+	.set_fullscreen = weston_desktop_xdg_surface_set_fullscreen,
+	.set_resizing = weston_desktop_xdg_surface_set_resizing,
+	.set_activated = weston_desktop_xdg_surface_set_activated,
+	.set_size = weston_desktop_xdg_surface_set_size,
+	.committed = weston_desktop_xdg_surface_committed,
+	.ping = weston_desktop_xdg_surface_ping,
+	.close = weston_desktop_xdg_surface_close,
+
+	.get_maximized = weston_desktop_xdg_surface_get_maximized,
+	.get_fullscreen = weston_desktop_xdg_surface_get_fullscreen,
+	.get_resizing = weston_desktop_xdg_surface_get_resizing,
+	.get_activated = weston_desktop_xdg_surface_get_activated,
+
+	.destroy = weston_desktop_xdg_surface_destroy,
+};
+
+static void
+weston_desktop_xdg_popup_close(struct weston_desktop_surface *dsurface,
+			       void *user_data)
+{
+	struct weston_desktop_xdg_popup *popup = user_data;
+
+	xdg_popup_send_popup_done(popup->resource);
+}
+
+static void
+weston_desktop_xdg_popup_destroy(struct weston_desktop_surface *dsurface,
+				 void *user_data)
+{
+	struct weston_desktop_xdg_popup *popup = user_data;
+	struct weston_desktop_surface *topmost;
+	struct weston_desktop_client *client =
+		weston_desktop_surface_get_client(popup->popup);
+
+	if (!weston_desktop_surface_get_grab(popup->popup))
+		goto end;
+
+	topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
+	if (topmost != popup->popup) {
+		struct wl_resource *client_resource =
+			weston_desktop_client_get_resource(client);
+
+		wl_resource_post_error(client_resource,
+				       XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
+				       "xdg_popup was destroyed while it was not the topmost popup.");
+	}
+
+	weston_desktop_surface_popup_ungrab(popup->popup, popup->seat);
+
+end:
+	free(popup);
+}
+
+static const struct xdg_popup_interface weston_desktop_xdg_popup_implementation = {
+	.destroy             = weston_desktop_destroy_request,
+};
+
+static const struct weston_desktop_surface_implementation weston_desktop_xdg_popup_internal_implementation = {
+	.close = weston_desktop_xdg_popup_close,
+
+	.destroy = weston_desktop_xdg_popup_destroy,
+};
+
+static void
+weston_desktop_xdg_shell_protocol_use_unstable_version(struct wl_client *wl_client,
+						       struct wl_resource *resource,
+						       int32_t version)
+{
+	if (version > 1) {
+		wl_resource_post_error(resource,
+				       1, "xdg_shell version not supported");
+		return;
+	}
+}
+
+static void
+weston_desktop_xdg_surface_add(void *user_data)
+{
+	struct weston_desktop_xdg_surface *surface = user_data;
+	surface->add_idle = NULL;
+	weston_desktop_api_surface_added(surface->desktop, surface->surface);
+}
+
+static void
+weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
+						  struct wl_resource *resource,
+						  uint32_t id,
+						  struct wl_resource *surface_resource)
+{
+	struct weston_desktop_client *client =
+		wl_resource_get_user_data(resource);
+	struct weston_desktop *desktop =
+		weston_desktop_client_get_desktop(client);
+	struct weston_surface *wsurface =
+		wl_resource_get_user_data(surface_resource);
+	struct weston_desktop_xdg_surface *surface;
+	struct wl_display *display = weston_desktop_get_display(desktop);
+	struct wl_event_loop *loop = wl_display_get_event_loop(display);
+
+	if (weston_surface_set_role(wsurface, "xdg_surface", resource, XDG_SHELL_ERROR_ROLE) < 0)
+		return;
+
+	surface = zalloc(sizeof(struct weston_desktop_xdg_surface));
+	if (surface == NULL) {
+		wl_client_post_no_memory(wl_client);
+		return;
+	}
+
+	surface->desktop = desktop;
+
+	surface->surface =
+		weston_desktop_surface_create(surface->desktop, client,
+					      wsurface,
+					      &weston_desktop_xdg_surface_internal_implementation,
+					      surface);
+	if (surface->surface == NULL) {
+		free(surface);
+		return;
+	}
+
+	surface->resource =
+		weston_desktop_surface_add_resource(surface->surface,
+						    &xdg_surface_interface,
+						    &weston_desktop_xdg_surface_implementation,
+						    id, NULL);
+	if (surface->resource == NULL)
+		return;
+
+	surface->add_idle =
+		wl_event_loop_add_idle(loop,
+				       weston_desktop_xdg_surface_add,
+				       surface);
+}
+
+static void
+weston_desktop_xdg_shell_protocol_get_xdg_popup(struct wl_client *wl_client,
+						struct wl_resource *resource,
+						uint32_t id,
+						struct wl_resource *surface_resource,
+						struct wl_resource *parent_resource,
+						struct wl_resource *seat_resource,
+						uint32_t serial,
+						int32_t x, int32_t y)
+{
+	struct weston_desktop_client *client =
+		wl_resource_get_user_data(resource);
+	struct weston_surface *wsurface =
+		wl_resource_get_user_data(surface_resource);
+	struct weston_surface *wparent =
+		wl_resource_get_user_data(parent_resource);
+	struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
+	struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
+	struct weston_desktop_surface *parent, *topmost;
+	bool parent_is_popup, parent_is_xdg;
+	struct weston_desktop_xdg_popup *popup;
+
+	if (weston_surface_set_role(wsurface, "xdg_popup", resource, XDG_SHELL_ERROR_ROLE) < 0)
+		return;
+
+	if (!weston_surface_is_desktop_surface(wparent)) {
+		wl_resource_post_error(resource,
+				       XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
+				       "xdg_popup parent was invalid");
+		return;
+	}
+
+	parent = weston_surface_get_desktop_surface(wparent);
+	parent_is_xdg =
+		weston_desktop_surface_has_implementation(parent,
+							  &weston_desktop_xdg_surface_internal_implementation);
+	parent_is_popup =
+		weston_desktop_surface_has_implementation(parent,
+							  &weston_desktop_xdg_popup_internal_implementation);
+
+	if (!parent_is_xdg && !parent_is_popup) {
+		wl_resource_post_error(resource,
+				       XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
+				       "xdg_popup parent was invalid");
+		return;
+	}
+
+	topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
+	if ((topmost == NULL && parent_is_popup) ||
+	    (topmost != NULL && topmost != parent)) {
+		wl_resource_post_error(resource,
+				       XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
+				       "xdg_popup was not created on the topmost popup");
+		return;
+	}
+
+	popup = zalloc(sizeof(struct weston_desktop_xdg_popup));
+	if (popup == NULL) {
+		wl_client_post_no_memory(wl_client);
+		return;
+	}
+
+	popup->desktop = weston_desktop_client_get_desktop(client);
+	popup->display = weston_desktop_get_display(popup->desktop);
+	popup->seat = seat;
+
+	popup->popup =
+		weston_desktop_surface_create(popup->desktop, client, wsurface,
+					      &weston_desktop_xdg_popup_internal_implementation,
+					      popup);
+	if (popup->popup == NULL) {
+		free(popup);
+		return;
+	}
+
+	popup->resource =
+		weston_desktop_surface_add_resource(popup->popup,
+						    &xdg_popup_interface,
+						    &weston_desktop_xdg_popup_implementation,
+						    id, NULL);
+	if (popup->resource == NULL)
+		return;
+
+	weston_desktop_surface_set_relative_to(popup->popup, parent, x, y, false);
+	weston_desktop_surface_popup_grab(popup->popup, popup->seat, serial);
+}
+
+static void
+weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
+				       struct wl_resource *resource,
+				       uint32_t serial)
+{
+	struct weston_desktop_client *client =
+		wl_resource_get_user_data(resource);
+
+	weston_desktop_client_pong(client, serial);
+}
+
+static const struct xdg_shell_interface weston_desktop_xdg_shell_implementation = {
+	.destroy = weston_desktop_destroy_request,
+	.use_unstable_version = weston_desktop_xdg_shell_protocol_use_unstable_version,
+	.get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
+	.get_xdg_popup = weston_desktop_xdg_shell_protocol_get_xdg_popup,
+	.pong = weston_desktop_xdg_shell_protocol_pong,
+};
+
+static int
+xdg_shell_unversioned_dispatch(const void *implementation,
+			       void *_target, uint32_t opcode,
+			       const struct wl_message *message,
+			       union wl_argument *args)
+{
+	struct wl_resource *resource = _target;
+	struct weston_desktop_client *client =
+		wl_resource_get_user_data(resource);
+
+	if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
+		wl_resource_post_error(resource,
+				       WL_DISPLAY_ERROR_INVALID_OBJECT,
+				       "must call use_unstable_version first");
+		return 0;
+	}
+
+#define XDG_SERVER_VERSION 5
+
+	if (args[0].i != XDG_SERVER_VERSION) {
+		wl_resource_post_error(resource,
+				       WL_DISPLAY_ERROR_INVALID_OBJECT,
+				       "incompatible version, server is %d " "client wants %d",
+				       XDG_SERVER_VERSION, args[0].i);
+		return 0;
+	}
+
+	wl_resource_set_implementation(resource,
+				       &weston_desktop_xdg_shell_implementation,
+				       client, implementation);
+
+	return 1;
+}
+
+static void
+weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
+			      uint32_t version, uint32_t id)
+{
+	struct weston_desktop *desktop = data;
+
+	weston_desktop_client_create(desktop, client,
+				     xdg_shell_unversioned_dispatch,
+				     &xdg_shell_interface, NULL, version, id);
+}
+
+struct wl_global *
+weston_desktop_xdg_shell_v5_create(struct weston_desktop *desktop,
+				   struct wl_display *display)
+{
+	return wl_global_create(display,
+				&xdg_shell_interface,
+				WD_XDG_SHELL_PROTOCOL_VERSION,
+				desktop, weston_desktop_xdg_shell_bind);
+}
diff --git a/libweston-desktop/xwayland.c b/libweston-desktop/xwayland.c
new file mode 100644
index 0000000..9282bf2
--- /dev/null
+++ b/libweston-desktop/xwayland.c
@@ -0,0 +1,360 @@
+/*
+ * Copyright © 2010-2012 Intel Corporation
+ * Copyright © 2011-2012 Collabora, Ltd.
+ * Copyright © 2013 Raspberry Pi Foundation
+ * Copyright © 2016 Quentin "Sardem FF7" Glidic
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <assert.h>
+
+#include <wayland-server.h>
+
+#include "compositor.h"
+#include "zalloc.h"
+
+#include "libweston-desktop.h"
+#include "internal.h"
+
+enum weston_desktop_xwayland_surface_state {
+	NONE,
+	TOPLEVEL,
+	MAXIMIZED,
+	FULLSCREEN,
+	TRANSIENT,
+	XWAYLAND,
+};
+
+struct weston_desktop_xwayland {
+	struct weston_desktop *desktop;
+	struct weston_desktop_client *client;
+	struct weston_layer layer;
+};
+
+struct shell_surface {
+	struct weston_desktop_xwayland *xwayland;
+	struct weston_desktop *desktop;
+	struct weston_desktop_surface *surface;
+	struct wl_listener resource_destroy_listener;
+	struct weston_view *view;
+	const struct weston_shell_client *client;
+	struct weston_geometry next_geometry;
+	bool has_next_geometry;
+	bool added;
+	enum weston_desktop_xwayland_surface_state state;
+};
+
+static void
+weston_desktop_xwayland_surface_change_state(struct shell_surface *surface,
+					     enum weston_desktop_xwayland_surface_state state,
+					     struct weston_desktop_surface *parent,
+					     int32_t x, int32_t y)
+{
+	bool to_add = (parent == NULL && state != XWAYLAND);
+
+	assert(state != NONE);
+
+	if (to_add && surface->added)
+		return;
+
+	if (surface->state != state) {
+		if (surface->state == XWAYLAND) {
+			weston_desktop_surface_destroy_view(surface->view);
+			surface->view = NULL;
+		}
+
+		if (to_add) {
+			weston_desktop_surface_unset_relative_to(surface->surface);
+			weston_desktop_api_surface_added(surface->desktop,
+							 surface->surface);
+		} else if (surface->added) {
+			weston_desktop_api_surface_removed(surface->desktop,
+							   surface->surface);
+		}
+
+		if (state == XWAYLAND) {
+			surface->view =
+				weston_desktop_surface_create_view(surface->surface);
+			weston_layer_entry_insert(&surface->xwayland->layer.view_list,
+						  &surface->view->layer_link);
+			weston_view_set_position(surface->view, x, y);
+		}
+
+		surface->state = state;
+		surface->added = to_add;
+	}
+
+	if (parent != NULL)
+		weston_desktop_surface_set_relative_to(surface->surface, parent,
+						       x, y, false);
+}
+
+static void
+weston_desktop_xwayland_surface_committed(struct weston_desktop_surface *dsurface,
+					  void *user_data, bool new_buffer,
+					  int32_t sx, int32_t sy)
+{
+	struct shell_surface *surface = user_data;
+
+	if (surface->has_next_geometry) {
+		surface->has_next_geometry = false;
+		weston_desktop_surface_set_geometry(surface->surface,
+						    surface->next_geometry);
+	}
+
+	if (surface->added)
+		weston_desktop_api_committed(surface->desktop, surface->surface,
+					     sx, sy);
+}
+
+static void
+weston_desktop_xwayland_surface_set_size(struct weston_desktop_surface *dsurface,
+					  void *user_data,
+					  int32_t width, int32_t height)
+{
+	struct shell_surface *surface = user_data;
+
+	surface->client->send_configure(weston_desktop_surface_get_surface(surface->surface),
+				       width, height);
+}
+
+static void
+weston_desktop_xwayland_surface_destroy(struct weston_desktop_surface *dsurface,
+					void *user_data)
+{
+	struct shell_surface *surface = user_data;
+
+	wl_list_remove(&surface->resource_destroy_listener.link);
+
+	weston_desktop_surface_unset_relative_to(surface->surface);
+	if (surface->added)
+		weston_desktop_api_surface_removed(surface->desktop,
+						   surface->surface);
+	else if (surface->state == XWAYLAND)
+		weston_desktop_surface_destroy_view(surface->view);
+
+	free(surface);
+}
+
+static bool
+weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface *dsurface,
+					       void *user_data)
+{
+	struct shell_surface *surface = user_data;
+
+	return surface->state == MAXIMIZED;
+}
+
+static bool
+weston_desktop_xwayland_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
+					        void *user_data)
+{
+	struct shell_surface *surface = user_data;
+
+	return surface->state == FULLSCREEN;
+}
+
+static const struct weston_desktop_surface_implementation weston_desktop_xwayland_surface_internal_implementation = {
+	.committed = weston_desktop_xwayland_surface_committed,
+	.set_size = weston_desktop_xwayland_surface_set_size,
+
+	.get_maximized = weston_desktop_xwayland_surface_get_maximized,
+	.get_fullscreen = weston_desktop_xwayland_surface_get_fullscreen,
+
+	.destroy = weston_desktop_xwayland_surface_destroy,
+};
+
+static void
+weston_destop_xwayland_resource_destroyed(struct wl_listener *listener,
+					  void *data)
+{
+	struct shell_surface *surface =
+		wl_container_of(listener, surface, resource_destroy_listener);
+
+	weston_desktop_surface_destroy(surface->surface);
+}
+
+static struct shell_surface *
+create_shell_surface(void *shell,
+		     struct weston_surface *wsurface,
+		     const struct weston_shell_client *client)
+{
+	struct weston_desktop_xwayland *xwayland = shell;
+	struct shell_surface *surface;
+
+	surface = zalloc(sizeof(struct shell_surface));
+	if (surface == NULL)
+		return NULL;
+
+	surface->xwayland = xwayland;
+	surface->desktop = xwayland->desktop;
+	surface->client = client;
+
+	surface->surface =
+		weston_desktop_surface_create(surface->desktop,
+					      xwayland->client, wsurface,
+					      &weston_desktop_xwayland_surface_internal_implementation,
+					      surface);
+	if (surface->surface == NULL) {
+		free(surface);
+		return NULL;
+	}
+
+	surface->resource_destroy_listener.notify =
+		weston_destop_xwayland_resource_destroyed;
+	wl_resource_add_destroy_listener(wsurface->resource,
+					 &surface->resource_destroy_listener);
+
+	return surface;
+}
+
+static void
+set_toplevel(struct shell_surface *surface)
+{
+	weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
+						     0, 0);
+}
+
+static void
+set_transient(struct shell_surface *surface,
+	      struct weston_surface *wparent, int x, int y, uint32_t flags)
+{
+	struct weston_desktop_surface *parent;
+
+	if (!weston_surface_is_desktop_surface(wparent))
+		return;
+
+	parent = weston_surface_get_desktop_surface(wparent);
+	if (flags & WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
+		weston_desktop_xwayland_surface_change_state(surface, TRANSIENT,
+							     parent, x, y);
+	} else {
+		weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL,
+							     NULL, 0, 0);
+		weston_desktop_api_set_parent(surface->desktop,
+					      surface->surface, parent);
+	}
+}
+
+static void
+set_fullscreen(struct shell_surface *surface, uint32_t method,
+	       uint32_t framerate, struct weston_output *output)
+{
+	weston_desktop_xwayland_surface_change_state(surface, FULLSCREEN, NULL,
+						     0, 0);
+	weston_desktop_api_fullscreen_requested(surface->desktop,
+						surface->surface, true, output);
+}
+
+static void
+set_xwayland(struct shell_surface *surface, int x, int y,
+	     uint32_t flags)
+{
+	weston_desktop_xwayland_surface_change_state(surface, XWAYLAND, NULL,
+						     x, y);
+}
+
+static int
+move(struct shell_surface *surface,
+     struct weston_pointer *pointer)
+{
+	if (surface->state == TOPLEVEL ||
+	    surface->state == MAXIMIZED ||
+	    surface->state == FULLSCREEN)
+		weston_desktop_api_move(surface->desktop, surface->surface,
+					pointer->seat, pointer->grab_serial);
+	return 0;
+}
+
+static int
+resize(struct shell_surface *surface,
+       struct weston_pointer *pointer, uint32_t edges)
+{
+	if (surface->state == TOPLEVEL ||
+	    surface->state == MAXIMIZED ||
+	    surface->state == FULLSCREEN)
+		weston_desktop_api_resize(surface->desktop, surface->surface,
+					  pointer->seat, pointer->grab_serial,
+					  edges);
+	return 0;
+}
+
+static void
+set_title(struct shell_surface *surface, const char *title)
+{
+	weston_desktop_surface_set_title(surface->surface, title);
+}
+
+static void
+set_window_geometry(struct shell_surface *surface,
+		    int32_t x, int32_t y, int32_t width, int32_t height)
+{
+	surface->has_next_geometry = true;
+	surface->next_geometry.x = x;
+	surface->next_geometry.y = y;
+	surface->next_geometry.width = width;
+	surface->next_geometry.height = height;
+}
+
+static void
+set_maximized(struct shell_surface *surface)
+{
+	weston_desktop_xwayland_surface_change_state(surface, MAXIMIZED, NULL,
+						     0, 0);
+	weston_desktop_api_maximized_requested(surface->desktop,
+					       surface->surface, true);
+}
+
+static void
+set_pid(struct shell_surface *surface, pid_t pid)
+{
+}
+
+void
+weston_desktop_xwayland_init(struct weston_desktop *desktop)
+{
+	struct weston_compositor *compositor = weston_desktop_get_compositor(desktop);
+	struct weston_desktop_xwayland *xwayland;
+
+	xwayland = zalloc(sizeof(struct weston_desktop_xwayland));
+	if (xwayland == NULL)
+		return;
+
+	xwayland->desktop = desktop;
+	xwayland->client = weston_desktop_client_create(desktop, NULL, NULL, NULL, NULL, 0, 0);
+
+	weston_layer_init(&xwayland->layer, &compositor->cursor_layer.link);
+
+	compositor->shell_interface.shell = xwayland;
+	compositor->shell_interface.create_shell_surface = create_shell_surface;
+	compositor->shell_interface.set_toplevel = set_toplevel;
+	compositor->shell_interface.set_transient = set_transient;
+	compositor->shell_interface.set_fullscreen = set_fullscreen;
+	compositor->shell_interface.set_xwayland = set_xwayland;
+	compositor->shell_interface.move = move;
+	compositor->shell_interface.resize = resize;
+	compositor->shell_interface.set_title = set_title;
+	compositor->shell_interface.set_window_geometry = set_window_geometry;
+	compositor->shell_interface.set_maximized = set_maximized;
+	compositor->shell_interface.set_pid = set_pid;
+}
diff --git a/libweston/compositor.c b/libweston/compositor.c
index be3c9fa..98c5bd6 100644
--- a/libweston/compositor.c
+++ b/libweston/compositor.c
@@ -3382,6 +3382,43 @@ weston_surface_get_content_size(struct weston_surface *surface,
 	rer->surface_get_content_size(surface, width, height);
 }
 
+/** Get the bounding box of a surface and its subsurfaces
+ *
+ * \param surface The surface to query.
+ * \return The bounding box relative to the surface origin.
+ *
+ */
+WL_EXPORT struct weston_geometry
+weston_surface_get_bounding_box(struct weston_surface *surface)
+{
+	pixman_region32_t region;
+	pixman_box32_t *box;
+	struct weston_subsurface *subsurface;
+
+	pixman_region32_init_rect(&region,
+				  0, 0,
+				  surface->width, surface->height);
+
+	wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
+		pixman_region32_union_rect(&region, &region,
+					   subsurface->position.x,
+					   subsurface->position.y,
+					   subsurface->surface->width,
+					   subsurface->surface->height);
+
+	box = pixman_region32_extents(&region);
+	struct weston_geometry geometry = {
+		.x = box->x1,
+		.y = box->y1,
+		.width = box->x2 - box->x1,
+		.height = box->y2 - box->y1,
+	};
+
+	pixman_region32_fini(&region);
+
+	return geometry;
+}
+
 /** Copy surface contents to system memory.
  *
  * \param surface The surface to copy from.
diff --git a/libweston/compositor.h b/libweston/compositor.h
index 5d3a2bf..f992263 100644
--- a/libweston/compositor.h
+++ b/libweston/compositor.h
@@ -46,6 +46,19 @@ extern "C" {
 #include "zalloc.h"
 #include "timeline-object.h"
 
+struct weston_geometry {
+	int32_t x, y;
+	int32_t width, height;
+};
+
+struct weston_position {
+	int32_t x, y;
+};
+
+struct weston_size {
+	int32_t width, height;
+};
+
 struct weston_transform {
 	struct weston_matrix matrix;
 	struct wl_list link;
@@ -1547,6 +1560,9 @@ void
 weston_surface_get_content_size(struct weston_surface *surface,
 				int *width, int *height);
 
+struct weston_geometry
+weston_surface_get_bounding_box(struct weston_surface *surface);
+
 int
 weston_surface_copy_content(struct weston_surface *surface,
 			    void *target, size_t size,
-- 
2.9.2



More information about the wayland-devel mailing list