<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On 17 July 2014 19:54, Jasper St. Pierre <span dir="ltr"><<a href="mailto:jstpierre@mecheye.net" target="_blank">jstpierre@mecheye.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This allows compositors to easily select a good display to listen on.<br>
---<br>
src/wayland-server.c | 97 ++++++++++++++++++++++++++++++++++++++--------------<br>
src/wayland-server.h | 1 +<br>
2 files changed, 73 insertions(+), 25 deletions(-)<br>
<br>
diff --git a/src/wayland-server.c b/src/wayland-server.c<br>
index 55b3e25..eff1c9b 100644<br>
--- a/src/wayland-server.c<br>
+++ b/src/wayland-server.c<br>
@@ -1098,11 +1098,81 @@ wl_socket_init_for_display_name(struct wl_socket *s, const char *name)<br>
return 0;<br>
}<br>
<br>
+static int<br>
+_wl_display_add_socket(struct wl_display *display, struct wl_socket *s)<br>
+{<br>
+ socklen_t size;<br>
+<br>
+ s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);<br>
+ if (s->fd < 0) {<br>
+ return -1;<br>
+ }<br>
+<br>
+ size = offsetof (struct sockaddr_un, sun_path) + strlen(s->addr.sun_path);<br>
+ if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {<br>
+ wl_log("bind() failed with error: %m\n");<br>
+ return -1;<br>
+ }<br>
+<br>
+ if (listen(s->fd, 1) < 0) {<br>
+ wl_log("listen() failed with error: %m\n");<br>
+ return -1;<br>
+ }<br>
+<br>
+ s->source = wl_event_loop_add_fd(display->loop, s->fd,<br>
+ WL_EVENT_READABLE,<br>
+ socket_data, display);<br>
+ if (s->source == NULL) {<br>
+ return -1;<br>
+ }<br>
+<br>
+ wl_list_insert(display->socket_list.prev, &s->link);<br>
+ return 0;<br>
+}<br>
+<br>
+WL_EXPORT const char *<br>
+wl_display_add_socket_auto(struct wl_display *display)<br>
+{<br>
+ struct wl_socket *s;<br>
+ int displayno = 0;<br>
+ char display_name[16] = "";<br>
+<br>
+ /* A reasonable number of maximum default sockets. If<br>
+ * you need more than this, use the explicit add_socket API. */<br>
+ const int MAX_DISPLAYNO = 32;<br>
+<br></blockquote><div><br></div><div>I don't have anything against this patch, just an idea: You could get this number as an argument (i. e. 0 is default number,<br></div><div>non-zero is user's choise of trials). Of course you would need to use display_name that is large enough, or allocate it dynamically.<br>
</div><div>Dynamic allocation would have the advantage, that in the previous path, you can just do:<br></div><div> s->display_name = name;<br></div><div>but the drawback is, that you have to strdup the static strings when normal wl_display_add_socket is used.<br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ s = malloc(sizeof *s);<br>
+ if (s == NULL)<br>
+ return NULL;<br>
+<br></blockquote><div><br></div><div>(Still the memset/calloc is needed as in the first patch).<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ do {<br>
+ snprintf(display_name, sizeof display_name, "wayland-%d", displayno);<br>
+ if (wl_socket_init_for_display_name(s, display_name) < 0) {<br>
+ wl_socket_destroy(s);<br>
+ return NULL;<br>
+ }<br>
+<br>
+ if (wl_socket_lock(s) < 0)<br>
+ continue;<br>
+<br>
+ if (_wl_display_add_socket(display, s) < 0) { <br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ wl_socket_destroy(s);<br>
+ return NULL;<br>
+ }<br>
+<br>
+ return s->display_name;<br>
+ } while (displayno++ < MAX_DISPLAYNO);<br>
+<br>
+ /* Ran out of display names. */<br>
+ wl_socket_destroy(s);<br>
+ errno = EINVAL;<br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ return NULL;<br>
+}<br>
+<br>
WL_EXPORT int<br>
wl_display_add_socket(struct wl_display *display, const char *name)<br>
{<br>
struct wl_socket *s;<br>
- socklen_t size;<br>
<br>
s = malloc(sizeof *s);<br>
if (s == NULL)<br>
@@ -1123,34 +1193,11 @@ wl_display_add_socket(struct wl_display *display, const char *name)<br>
return -1;<br>
}<br>
<br>
- s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);<br>
- if (s->fd < 0) {<br>
- wl_socket_destroy(s);<br>
- return -1;<br>
- }<br>
-<br>
- size = offsetof (struct sockaddr_un, sun_path) + strlen(s->addr.sun_path);<br>
- if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {<br>
- wl_log("bind() failed with error: %m\n");<br>
- wl_socket_destroy(s);<br>
- return -1;<br>
- }<br>
-<br>
- if (listen(s->fd, 1) < 0) {<br>
- wl_log("listen() failed with error: %m\n");<br>
+ if (_wl_display_add_socket(display, s) < 0) {<br>
wl_socket_destroy(s);<br>
return -1;<br>
}<br>
<br>
- s->source = wl_event_loop_add_fd(display->loop, s->fd,<br>
- WL_EVENT_READABLE,<br>
- socket_data, display);<br>
- if (s->source == NULL) {<br>
- wl_socket_destroy(s);<br>
- return -1;<br>
- }<br>
- wl_list_insert(display->socket_list.prev, &s->link);<br>
-<br>
return 0;<br>
}<br>
<br>
diff --git a/src/wayland-server.h b/src/wayland-server.h<br>
index 7fc5b47..287d015 100644<br>
--- a/src/wayland-server.h<br>
+++ b/src/wayland-server.h<br>
@@ -92,6 +92,7 @@ struct wl_display *wl_display_create(void);<br>
void wl_display_destroy(struct wl_display *display);<br>
struct wl_event_loop *wl_display_get_event_loop(struct wl_display *display);<br>
int wl_display_add_socket(struct wl_display *display, const char *name);<br>
+const char *wl_display_add_socket_auto(struct wl_display *display);<br>
void wl_display_terminate(struct wl_display *display);<br>
void wl_display_run(struct wl_display *display);<br>
void wl_display_flush_clients(struct wl_display *display);<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.0.1<br>
<br>
_______________________________________________<br>
wayland-devel mailing list<br>
<a href="mailto:wayland-devel@lists.freedesktop.org">wayland-devel@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/wayland-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/wayland-devel</a><br>
</font></span></blockquote></div><br></div></div>