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

Marek Chalupa mchqwerty at gmail.com
Fri Jul 18 02:05:03 PDT 2014


On 17 July 2014 19:54, Jasper St. Pierre <jstpierre at mecheye.net> wrote:

> This allows compositors to easily select a good display to listen on.
> ---
>  src/wayland-server.c | 97
> ++++++++++++++++++++++++++++++++++++++--------------
>  src/wayland-server.h |  1 +
>  2 files changed, 73 insertions(+), 25 deletions(-)
>
> diff --git a/src/wayland-server.c b/src/wayland-server.c
> index 55b3e25..eff1c9b 100644
> --- a/src/wayland-server.c
> +++ b/src/wayland-server.c
> @@ -1098,11 +1098,81 @@ wl_socket_init_for_display_name(struct wl_socket
> *s, const char *name)
>         return 0;
>  }
>
> +static int
> +_wl_display_add_socket(struct wl_display *display, struct wl_socket *s)
> +{
> +       socklen_t size;
> +
> +       s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
> +       if (s->fd < 0) {
> +               return -1;
> +       }
> +
> +       size = offsetof (struct sockaddr_un, sun_path) +
> strlen(s->addr.sun_path);
> +       if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
> +               wl_log("bind() failed with error: %m\n");
> +               return -1;
> +       }
> +
> +       if (listen(s->fd, 1) < 0) {
> +               wl_log("listen() failed with error: %m\n");
> +               return -1;
> +       }
> +
> +       s->source = wl_event_loop_add_fd(display->loop, s->fd,
> +                                        WL_EVENT_READABLE,
> +                                        socket_data, display);
> +       if (s->source == NULL) {
> +               return -1;
> +       }
> +
> +       wl_list_insert(display->socket_list.prev, &s->link);
> +       return 0;
> +}
> +
> +WL_EXPORT const char *
> +wl_display_add_socket_auto(struct wl_display *display)
> +{
> +       struct wl_socket *s;
> +       int displayno = 0;
> +       char display_name[16] = "";
> +
> +       /* A reasonable number of maximum default sockets. If
> +        * you need more than this, use the explicit add_socket API. */
> +       const int MAX_DISPLAYNO = 32;
> +
>

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,
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.
Dynamic allocation would have the advantage, that in the previous path, you
can just do:
    s->display_name = name;
but the drawback is, that you have to strdup the static strings when normal
wl_display_add_socket is used.


> +       s = malloc(sizeof *s);
> +       if (s == NULL)
> +               return NULL;
> +
>

(Still the memset/calloc is needed as in the first patch).


> +       do {
> +               snprintf(display_name, sizeof display_name, "wayland-%d",
> displayno);
> +               if (wl_socket_init_for_display_name(s, display_name) < 0) {
> +                       wl_socket_destroy(s);
> +                       return NULL;
> +               }
> +
> +               if (wl_socket_lock(s) < 0)
> +                       continue;
> +
> +               if (_wl_display_add_socket(display, s) < 0) {
>
+                       wl_socket_destroy(s);
> +                       return NULL;
> +               }
> +
> +               return s->display_name;
> +       } while (displayno++ < MAX_DISPLAYNO);
> +
> +       /* Ran out of display names. */
> +       wl_socket_destroy(s);
> +       errno = EINVAL;
>
+       return NULL;
> +}
> +
>  WL_EXPORT int
>  wl_display_add_socket(struct wl_display *display, const char *name)
>  {
>         struct wl_socket *s;
> -       socklen_t size;
>
>         s = malloc(sizeof *s);
>         if (s == NULL)
> @@ -1123,34 +1193,11 @@ wl_display_add_socket(struct wl_display *display,
> const char *name)
>                 return -1;
>         }
>
> -       s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
> -       if (s->fd < 0) {
> -               wl_socket_destroy(s);
> -               return -1;
> -       }
> -
> -       size = offsetof (struct sockaddr_un, sun_path) +
> strlen(s->addr.sun_path);
> -       if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
> -               wl_log("bind() failed with error: %m\n");
> -               wl_socket_destroy(s);
> -               return -1;
> -       }
> -
> -       if (listen(s->fd, 1) < 0) {
> -               wl_log("listen() failed with error: %m\n");
> +       if (_wl_display_add_socket(display, s) < 0) {
>                 wl_socket_destroy(s);
>                 return -1;
>         }
>
> -       s->source = wl_event_loop_add_fd(display->loop, s->fd,
> -                                        WL_EVENT_READABLE,
> -                                        socket_data, display);
> -       if (s->source == NULL) {
> -               wl_socket_destroy(s);
> -               return -1;
> -       }
> -       wl_list_insert(display->socket_list.prev, &s->link);
> -
>         return 0;
>  }
>
> diff --git a/src/wayland-server.h b/src/wayland-server.h
> index 7fc5b47..287d015 100644
> --- a/src/wayland-server.h
> +++ b/src/wayland-server.h
> @@ -92,6 +92,7 @@ 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);
>  int wl_display_add_socket(struct wl_display *display, const char *name);
> +const char *wl_display_add_socket_auto(struct wl_display *display);
>  void wl_display_terminate(struct wl_display *display);
>  void wl_display_run(struct wl_display *display);
>  void wl_display_flush_clients(struct wl_display *display);
> --
> 2.0.1
>
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/wayland-devel/attachments/20140718/155f2fd3/attachment-0001.html>


More information about the wayland-devel mailing list