[PATCH wayland 3/3] server: Find an open wayland-* socket by default

Jasper St. Pierre jstpierre at mecheye.net
Wed May 7 06:05:56 PDT 2014


Rather than just trying wayland-0 and bailing out if it's in use.
---
 src/wayland-server.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/wayland-server.c b/src/wayland-server.c
index 3390171..ba13168 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -1078,11 +1078,31 @@ open_socket_for_name(struct wl_socket *s, const char *name)
 	return 0;
 }
 
+static int
+open_default_socket(struct wl_socket *s)
+{
+	char socket_name[16] = "";
+	int sockno = 0;
+
+	/* A reasonable number of maximum default sockets. If
+	 * you need more than this, set WAYLAND_DISPLAY explicitly. */
+	const int MAX_SOCKNO = 32;
+
+	do {
+		snprintf(socket_name, sizeof socket_name, "wayland-%d", sockno);
+		if (open_socket_for_name(s, socket_name) >= 0)
+			return 0;
+	} while (sockno++ < MAX_SOCKNO);
+
+	return -1;
+}
+
 WL_EXPORT int
 wl_display_add_socket(struct wl_display *display, const char *name)
 {
 	struct wl_socket *s;
 	socklen_t size;
+	int ret;
 
 	s = malloc(sizeof *s);
 	if (s == NULL)
@@ -1090,10 +1110,13 @@ wl_display_add_socket(struct wl_display *display, const char *name)
 
 	if (name == NULL)
 		name = getenv("WAYLAND_DISPLAY");
-	if (name == NULL)
-		name = "wayland-0";
 
-	if (open_socket_for_name(s, name) < 0) {
+	if (name)
+		ret = open_socket_for_name(s, name);
+	else
+		ret = open_default_socket(s);
+
+	if (ret < 0) {
 		free(s);
 		return -1;
 	}
-- 
1.9.0



More information about the wayland-devel mailing list