[PATCH wayland 3/3] server: Add a simple API to find a good default display

Jasper St. Pierre jstpierre at mecheye.net
Wed May 7 07:25:30 PDT 2014


This allows compositors to easily select a good display to listen on.
---
 src/wayland-server.c | 23 +++++++++++++++++++++++
 src/wayland-server.h |  2 ++
 2 files changed, 25 insertions(+)

diff --git a/src/wayland-server.c b/src/wayland-server.c
index 6bc8dc3..5624199 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -1078,6 +1078,29 @@ open_socket_for_display_name(struct wl_socket *s, const char *name)
 	return 0;
 }
 
+WL_EXPORT char *
+wl_choose_default_display(void)
+{
+	struct wl_socket s = { 0 };
+	int displayno = 0;
+	char display_name[16] = "";
+
+	/* A reasonable number of maximum default sockets. If
+	 * you need more than this, set WAYLAND_DISPLAY explicitly. */
+	const int MAX_DISPLAYNO = 32;
+
+	do {
+		snprintf(display_name, sizeof display_name, "wayland-%d", displayno);
+		if (open_socket_for_display_name(&s, display_name) >= 0) {
+			close(s->fd_lock);
+			return strdup(display_name);
+		}
+	} while (displayno++ < MAX_DISPLAYNO);
+
+	errno = EINVAL;
+	return NULL;
+}
+
 WL_EXPORT int
 wl_display_add_socket(struct wl_display *display, const char *name)
 {
diff --git a/src/wayland-server.h b/src/wayland-server.h
index 7fc5b47..c9834f1 100644
--- a/src/wayland-server.h
+++ b/src/wayland-server.h
@@ -88,6 +88,8 @@ struct wl_listener *wl_event_loop_get_destroy_listener(
 					struct wl_event_loop *loop,
 					wl_notify_func_t notify);
 
+char *wl_choose_default_socket(void);
+
 struct wl_display *wl_display_create(void);
 void wl_display_destroy(struct wl_display *display);
 struct wl_event_loop *wl_display_get_event_loop(struct wl_display *display);
-- 
1.9.0



More information about the wayland-devel mailing list