[RFC] add a shutdown event

Hardening rdp.effort at gmail.com
Sun Mar 17 15:32:21 PDT 2013


This patch adds a shutdow event so that clients can be notified
when the compositor is about to exit (and potentially kill its
child processes). 
This patch is based on gh next.
---
 protocol/wayland.xml |    7 +++++++
 src/wayland-client.c |   26 +++++++++++++++++++++++++-
 src/wayland-client.h |    4 ++++
 src/wayland-server.c |    9 +++++++++
 4 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/protocol/wayland.xml b/protocol/wayland.xml
index 8fb5841..563c22a 100644
--- a/protocol/wayland.xml
+++ b/protocol/wayland.xml
@@ -90,6 +90,13 @@
       </description>
       <arg name="id" type="uint" />
     </event>
+    
+    <event name="shutdown">
+      <description summary="notifies compositor shutdown">
+      This event is used to notify clients that the compositor is 
+      about to shutdown.
+      </description>
+    </event>
   </interface>
 
   <interface name="wl_registry" version="1">
diff --git a/src/wayland-client.c b/src/wayland-client.c
index 74e4657..fd5919d 100644
--- a/src/wayland-client.c
+++ b/src/wayland-client.c
@@ -82,6 +82,7 @@ struct wl_display {
 	struct wl_map objects;
 	struct wl_event_queue queue;
 	struct wl_list event_queue_list;
+	wl_display_shutdown_func_t shutdown_callback;
 	pthread_mutex_t mutex;
 };
 
@@ -423,9 +424,17 @@ display_handle_delete_id(void *data, struct wl_display *display, uint32_t id)
 	pthread_mutex_unlock(&display->mutex);
 }
 
+static void
+display_handle_shutdown(void *data, struct wl_display *display)
+{
+	if(display->shutdown_callback)
+		(*display->shutdown_callback)(display);
+}
+
 static const struct wl_display_listener display_listener = {
 	display_handle_error,
-	display_handle_delete_id
+	display_handle_delete_id,
+	display_handle_shutdown
 };
 
 static int
@@ -1025,6 +1034,21 @@ wl_display_dispatch_pending(struct wl_display *display)
 	return dispatch_queue(display, &display->queue, 0);
 }
 
+/**  Install a listener for shutdown event
+ *
+ *	\param notify the callback to install
+ *
+ *	The shutdown event is triggered when the compositor is about to exit,
+ *	so the callback should perform last chance cleanups as fast as possible.
+ *
+ *	\memberof wl_display
+ */
+void wl_display_set_shutdown_notify(struct wl_display *display,
+		wl_display_shutdown_func_t notify)
+{
+	display->shutdown_callback = notify;
+}
+
 /** Retrieve the last error occurred on a display
  *
  * \param display The display context object
diff --git a/src/wayland-client.h b/src/wayland-client.h
index 578fa7e..0fc08e3 100644
--- a/src/wayland-client.h
+++ b/src/wayland-client.h
@@ -136,6 +136,8 @@ void wl_proxy_set_queue(struct wl_proxy *proxy, struct wl_event_queue *queue);
 
 #include "wayland-client-protocol.h"
 
+typedef void (*wl_display_shutdown_func_t)(struct wl_display *display);
+
 struct wl_display *wl_display_connect(const char *name);
 struct wl_display *wl_display_connect_to_fd(int fd);
 void wl_display_disconnect(struct wl_display *display);
@@ -146,6 +148,8 @@ int wl_display_dispatch_queue(struct wl_display *display,
 int wl_display_dispatch_queue_pending(struct wl_display *display,
 				      struct wl_event_queue *queue);
 int wl_display_dispatch_pending(struct wl_display *display);
+void wl_display_set_shutdown_notify(struct wl_display *display,
+						wl_display_shutdown_func_t notify);
 int wl_display_get_error(struct wl_display *display);
 
 int wl_display_flush(struct wl_display *display);
diff --git a/src/wayland-server.c b/src/wayland-server.c
index 2f3ddc9..e454f29 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -1099,6 +1099,15 @@ wl_display_destroy(struct wl_display *display)
 {
 	struct wl_socket *s, *next;
 	struct wl_global *global, *gnext;
+	struct wl_client *client, *cnext;
+
+	wl_list_for_each_safe(client, cnext, &display->client_list, link) {
+		wl_resource_queue_event(client->display_resource, WL_DISPLAY_SHUTDOWN);
+	}
+
+	wl_display_flush_clients(display);
+
+	/* XXX: wait a little for all sockets to close */
 
 	wl_signal_emit(&display->destroy_signal, display);
 
-- 
1.7.10.4



More information about the wayland-devel mailing list