[PATCH weston v4] refactor x11-backend configuration API

Giulio Camuffo giuliocamuffo at gmail.com
Mon Mar 28 08:56:38 UTC 2016


Sorry, i hit send by mistake before i was finished...

2016-03-28 11:45 GMT+03:00 Giulio Camuffo <giuliocamuffo at gmail.com>:
> 2016-03-21 23:58 GMT+02:00 Benoit Gschwind <gschwind at gnu-log.net>:
>> Use a "well" defined structure to configure x11-backend and move configuration
>> file parsing inside the weston compositor code.
>>
>> v4: add struct_version, and check for null pointer of the configuration
>> structure.
>> v3: properly check memory error for the memory allocation of the outputs
>> array.
>> v2: add struct_size and related change, change output list from wl_list
>> to regular C array.
>> v1: initial implementation.
>> ---
>>  Makefile.am          |   1 +
>>  src/compositor-x11.c | 154 +++++++++++++++---------------------------
>>  src/compositor-x11.h |  82 +++++++++++++++++++++++
>>  src/compositor.h     |   1 +
>>  src/main.c           | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++-
>>  5 files changed, 319 insertions(+), 103 deletions(-)
>>  create mode 100644 src/compositor-x11.h
>>
>> diff --git a/Makefile.am b/Makefile.am
>> index 623621d..7acef6a 100644
>> --- a/Makefile.am
>> +++ b/Makefile.am
>> @@ -72,6 +72,7 @@ weston_SOURCES =                                      \
>>         src/log.c                                       \
>>         src/compositor.c                                \
>>         src/compositor.h                                \
>> +       src/compositor-x11.h                            \
>>         src/input.c                                     \
>>         src/data-device.c                               \
>>         src/screenshooter.c                             \
>> diff --git a/src/compositor-x11.c b/src/compositor-x11.c
>> index 13a5d73..b7eecad 100644
>> --- a/src/compositor-x11.c
>> +++ b/src/compositor-x11.c
>> @@ -50,6 +50,7 @@
>>  #include <xkbcommon/xkbcommon.h>
>>
>>  #include "compositor.h"
>> +#include "compositor-x11.h"
>>  #include "gl-renderer.h"
>>  #include "pixman-renderer.h"
>>  #include "shared/config-parser.h"
>> @@ -60,11 +61,6 @@
>>
>>  #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
>>
>> -static int option_width;
>> -static int option_height;
>> -static int option_scale;
>> -static int option_count;
>> -
>>  struct x11_backend {
>>         struct weston_backend    base;
>>         struct weston_compositor *compositor;
>> @@ -105,6 +101,7 @@ struct x11_backend {
>>                 xcb_atom_t               cardinal;
>>                 xcb_atom_t               xkb_names;
>>         } atom;
>> +
>
> Try to avoid this newline changes, there are a few of them in the patch.
>
>>  };
>>
>>  struct x11_output {
>> @@ -130,6 +127,11 @@ struct window_delete_data {
>>
>>  struct gl_renderer_interface *gl_renderer;
>>
>> +static void
>> +weston_x11_backend_config_init_default(struct weston_x11_backend_config * ths) {
>
> Brackets for functions go on a newline.
>
>> +       bzero(ths, sizeof(*ths));
>
> bzero is deprecated.
>
>> +}
>> +
>>  static xcb_screen_t *
>>  x11_compositor_get_default_screen(struct x11_backend *b)
>>  {
>> @@ -1548,6 +1550,7 @@ x11_destroy(struct weston_compositor *ec)
>>         weston_compositor_shutdown(ec); /* destroys outputs, too */
>>
>>         XCloseDisplay(backend->dpy);
>> +
>>         free(backend);
>>  }
>>
>> @@ -1568,23 +1571,24 @@ init_gl_renderer(struct x11_backend *b)
>>  }
>>  static struct x11_backend *
>>  x11_backend_create(struct weston_compositor *compositor,
>> -                  int fullscreen,
>> -                  int no_input,
>> -                  int use_pixman,
>> -                  int *argc, char *argv[],
>> -                  struct weston_config *config)
>> +                  struct weston_x11_backend_config *foreign_config)
>>  {
>>         struct x11_backend *b;
>>         struct x11_output *output;
>> -       struct weston_config_section *section;
>> -       int i, x = 0, output_count = 0;
>> -       int width, height, scale, count;
>> -       const char *section_name;
>> -       char *name, *t, *mode;
>> -       uint32_t transform;
>> +       struct weston_x11_backend_config config = { 0, };
>> +       int x = 0;
>> +       unsigned i;
>>
>>         weston_log("initializing x11 backend\n");
>>
>> +       if(foreign_config == NULL ||
>> +          foreign_config->struct_version != WESTON_X11_BACKEND_CONFIG_VERSION ||
>> +          foreign_config->struct_size > sizeof(struct weston_x11_backend_config))
>> +               return NULL;
>> +
>> +       weston_x11_backend_config_init_default(&config);
>
> You already initialized the struct to 0 above, so what's the point of this call?
>
>> +       memcpy(&config, foreign_config, foreign_config->struct_size);
>> +
>>         b = zalloc(sizeof *b);
>>         if (b == NULL)
>>                 return NULL;
>> @@ -1609,13 +1613,13 @@ x11_backend_create(struct weston_compositor *compositor,
>>         x11_backend_get_resources(b);
>>         x11_backend_get_wm_info(b);
>>
>> -       if (!b->has_net_wm_state_fullscreen && fullscreen) {
>> +       if (!b->has_net_wm_state_fullscreen && config.fullscreen) {
>>                 weston_log("Can not fullscreen without window manager support"
>>                            "(need _NET_WM_STATE_FULLSCREEN)\n");
>> -               fullscreen = 0;
>> +               config.fullscreen = 0;
>>         }
>>
>> -       b->use_pixman = use_pixman;
>> +       b->use_pixman = config.use_pixman;
>>         if (b->use_pixman) {
>>                 if (pixman_renderer_init(compositor) < 0) {
>>                         weston_log("Failed to initialize pixman renderer for X11 backend\n");
>> @@ -1625,84 +1629,54 @@ x11_backend_create(struct weston_compositor *compositor,
>>         else if (init_gl_renderer(b) < 0) {
>>                 goto err_xdisplay;
>>         }
>> -       weston_log("Using %s renderer\n", use_pixman ? "pixman" : "gl");
>> +       weston_log("Using %s renderer\n", config.use_pixman ? "pixman" : "gl");
>>
>>         b->base.destroy = x11_destroy;
>>         b->base.restore = x11_restore;
>>
>> -       if (x11_input_create(b, no_input) < 0) {
>> +       if (x11_input_create(b, config.no_input) < 0) {
>>                 weston_log("Failed to create X11 input\n");
>>                 goto err_renderer;
>>         }
>>
>> -       width = option_width ? option_width : 1024;
>> -       height = option_height ? option_height : 640;
>> -       scale = option_scale ? option_scale : 1;
>> -       count = option_count ? option_count : 1;
>> +       for(i = 0; i < config.noutputs; ++i) {
>> +               struct weston_x11_backend_output_config * output_iterator =
>> +                               &config.outputs[i];
>>
>> -       section = NULL;
>> -       while (weston_config_next_section(config,
>> -                                         &section, &section_name)) {
>> -               if (strcmp(section_name, "output") != 0)
>> -                       continue;
>> -               weston_config_section_get_string(section, "name", &name, NULL);
>> -               if (name == NULL || name[0] != 'X') {
>> -                       free(name);
>> +               if (output_iterator->name == NULL) {
>>                         continue;
>>                 }
>>
>> -               weston_config_section_get_string(section,
>> -                                                "mode", &mode, "1024x600");
>> -               if (sscanf(mode, "%dx%d", &width, &height) != 2) {
>> -                       weston_log("Invalid mode \"%s\" for output %s\n",
>> -                                  mode, name);
>> -                       width = 1024;
>> -                       height = 600;
>> -               }
>> -               free(mode);
>> -
>> -               if (option_width)
>> -                       width = option_width;
>> -               if (option_height)
>> -                       height = option_height;
>> -
>> -               weston_config_section_get_int(section, "scale", &scale, 1);
>> -               if (option_scale)
>> -                       scale = option_scale;
>> -
>> -               weston_config_section_get_string(section,
>> -                                                "transform", &t, "normal");
>> -               if (weston_parse_transform(t, &transform) < 0)
>> -                       weston_log("Invalid transform \"%s\" for output %s\n",
>> -                                  t, name);
>> -               free(t);
>> -
>> -               output = x11_backend_create_output(b, x, 0,
>> -                                                  width, height,
>> -                                                  fullscreen, no_input,
>> -                                                  name, transform, scale);
>> -               free(name);
>> -               if (output == NULL) {
>> -                       weston_log("Failed to create configured x11 output\n");
>> -                       goto err_x11_input;
>> +               if(output_iterator->width < 1) {
>> +                       weston_log("Invalid width \"%d\" for output %s\n",
>> +                                  output_iterator->width, output_iterator->name);
>> +                       output_iterator->width = 1024;
>>                 }
>>
>> -               x = pixman_region32_extents(&output->base.region)->x2;
>> +               if(output_iterator->height < 1) {
>> +                       weston_log("Invalid height \"%d\" for output %s\n",
>> +                                  output_iterator->height, output_iterator->name);
>> +                       output_iterator->height = 600;
>> +               }
>>
>> -               output_count++;
>> -               if (option_count && output_count >= option_count)
>> -                       break;
>> -       }
>> +               output = x11_backend_create_output(b,
>> +                               x,
>> +                               0,
>> +                               output_iterator->width,
>> +                               output_iterator->height,
>> +                               config.fullscreen,
>> +                               config.no_input,
>> +                               output_iterator->name,
>> +                               output_iterator->transform,
>> +                               output_iterator->scale);
>>
>> -       for (i = output_count; i < count; i++) {
>> -               output = x11_backend_create_output(b, x, 0, width, height,
>> -                                                  fullscreen, no_input, NULL,
>> -                                                  WL_OUTPUT_TRANSFORM_NORMAL, scale);
>>                 if (output == NULL) {
>> -                       weston_log("Failed to create x11 output #%d\n", i);
>> +                       weston_log("Failed to create configured x11 output\n");
>>                         goto err_x11_input;
>>                 }
>> +
>>                 x = pixman_region32_extents(&output->base.region)->x2;
>> +
>>         }
>>
>>         b->xcb_source =
>> @@ -1734,32 +1708,12 @@ err_free:
>>  }
>>
>>  WL_EXPORT int
>> -backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
>> -            struct weston_config *config,
>> -            struct weston_backend_config *config_base)
>> +x11_backend_init(struct weston_compositor *compositor,
>> +            struct weston_x11_backend_config *config)

You should not change the name of the entry point function, it's meant
to be shared between all backends.

>>  {
>>         struct x11_backend *b;
>> -       int fullscreen = 0;
>> -       int no_input = 0;
>> -       int use_pixman = 0;
>> -
>> -       const struct weston_option x11_options[] = {
>> -               { WESTON_OPTION_INTEGER, "width", 0, &option_width },
>> -               { WESTON_OPTION_INTEGER, "height", 0, &option_height },
>> -               { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
>> -               { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &fullscreen },
>> -               { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
>> -               { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
>> -               { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
>> -       };
>> -
>> -       parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
>>
>> -       b = x11_backend_create(compositor,
>> -                              fullscreen,
>> -                              no_input,
>> -                              use_pixman,
>> -                              argc, argv, config);
>> +       b = x11_backend_create(compositor, config);
>>         if (b == NULL)
>>                 return -1;
>>         return 0;
>> diff --git a/src/compositor-x11.h b/src/compositor-x11.h
>> new file mode 100644
>> index 0000000..903d515
>> --- /dev/null
>> +++ b/src/compositor-x11.h
>> @@ -0,0 +1,82 @@
>> +/*
>> + * Copyright © 2016 Benoit Gschwind
>> + *
>> + * Permission is hereby granted, free of charge, to any person obtaining
>> + * a copy of this software and associated documentation files (the
>> + * "Software"), to deal in the Software without restriction, including
>> + * without limitation the rights to use, copy, modify, merge, publish,
>> + * distribute, sublicense, and/or sell copies of the Software, and to
>> + * permit persons to whom the Software is furnished to do so, subject to
>> + * the following conditions:
>> + *
>> + * The above copyright notice and this permission notice (including the
>> + * next paragraph) shall be included in all copies or substantial
>> + * portions of the Software.
>> + *
>> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
>> + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
>> + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
>> + * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
>> + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
>> + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
>> + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
>> + * SOFTWARE.
>> + */
>> +#ifndef SRC_COMPOSITOR_X11_H_
>> +#define SRC_COMPOSITOR_X11_H_
>> +
>> +#ifdef  __cplusplus
>> +extern "C" {
>> +#endif
>> +
>> +#include "compositor.h"
>> +
>> +#define WESTON_X11_BACKEND_CONFIG_VERSION 1
>> +
>> +struct weston_x11_backend_output_config {
>> +       int width, height;
>> +       char *name;
>> +       uint32_t transform;
>> +       int32_t scale;
>> +};
>> +
>> +struct weston_x11_backend_config {

The first member should be a weston_backend_config field, so that it
can be passed to the backend entry point.

>> +       /** Major version for the backend-specific config struct
>> +        *
>> +        * This version must match exactly what the backend expects, otherwise
>> +        * the struct is incompatible.
>> +        *
>> +        * use WESTON_X11_BACKEND_CONFIG_VERSION
>> +        */
>> +       uint32_t struct_version;
>> +
>> +       /** Minor version of the backend-specific config struct
>> +        *
>> +        * This must be set to sizeof(struct backend-specific config).
>> +        * If the value here is smaller than what the backend expects, the
>> +        * extra config members will assume their default values.
>> +        *
>> +        * A value greater than what the backend expects is incompatible.
>> +        */
>> +       size_t struct_size;

Maybe it makes sense to put these two in weston_backend_config, since
all backends will want them.

>> +
>> +       bool fullscreen;
>> +       bool no_input;
>> +       bool use_pixman;
>> +       unsigned noutputs;
>
> I think "noutputs" is a bit cryptic, i'd call it number_outpus, or
> num_outputs. Also i think it's a good idea to use uint32_t instead of
> unsigned.
>
>> +       struct weston_x11_backend_output_config * outputs;
>> +};
>> +
>> +typedef int (*x11_backend_init_func)(
>> +               struct weston_compositor *c,
>> +           struct weston_x11_backend_config *config);
>> +
>> +WL_EXPORT int
>> +x11_backend_init(struct weston_compositor *compositor,
>> +            struct weston_x11_backend_config *config);
>> +
>> +#ifdef  __cplusplus
>> +}
>> +#endif
>> +
>> +#endif /* SRC_COMPOSITOR_X11_H_ */
>> diff --git a/src/compositor.h b/src/compositor.h
>> index 58ae94b..8abec40 100644
>> --- a/src/compositor.h
>> +++ b/src/compositor.h
>> @@ -1666,6 +1666,7 @@ backend_init(struct weston_compositor *c,
>>              int *argc, char *argv[],
>>              struct weston_config *config,
>>              struct weston_backend_config *config_base);
>> +
>>  int
>>  module_init(struct weston_compositor *compositor,
>>             int *argc, char *argv[]);
>> diff --git a/src/main.c b/src/main.c
>> index 1850fa6..32eda33 100644
>> --- a/src/main.c
>> +++ b/src/main.c
>> @@ -42,6 +42,7 @@
>>  #endif
>>
>>  #include "compositor.h"
>> +#include "compositor-x11.h"
>>  #include "../shared/os-compatibility.h"
>>  #include "../shared/helpers.h"
>>  #include "git-version.h"
>> @@ -653,17 +654,194 @@ load_backend_old(struct weston_compositor *compositor, const char *backend,
>>         return backend_init(compositor, argc, argv, wc, NULL);
>>  }
>>
>> +/** Create a new default drm backend configuration */
>> +static void
>> +weston_x11_backend_config_init(struct weston_x11_backend_config * ths) {
>> +       bzero(ths, sizeof(*ths));
>> +       ths->struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
>> +       ths->struct_size = sizeof(struct weston_x11_backend_config);
>> +}
>> +
>> +static void
>> +weston_x11_backend_config_outputs_clear(
>> +               struct weston_x11_backend_config *ths) {
>> +       unsigned i;
>> +       for(i = 0; i < ths->noutputs; ++i) {
>> +               free(ths->outputs[i].name);
>> +       }
>> +       free(ths->outputs);
>> +       ths->outputs = NULL;
>> +}
>> +
>> +static int
>> +weston_x11_backend_load(
>> +               struct weston_compositor *compositor,
>> +               struct weston_x11_backend_config *config) {
>> +
>> +       x11_backend_init_func backend_init;
>> +
>> +       backend_init = weston_load_module("x11-backend.so", "x11_backend_init");
>> +       if (!backend_init)
>> +               return -1;
>> +
>> +       return backend_init(compositor, config);
>> +}
>> +
>> +static int
>> +weston_x11_backend_config_append_output_config(
>> +               struct weston_x11_backend_config * ths,
>> +               struct weston_x11_backend_output_config * output_config
>> +               ) {
>> +
>> +       struct weston_x11_backend_output_config * new_outputs =
>> +                       realloc(ths->outputs, (ths->noutputs+1) *
>> +                                       sizeof(struct weston_x11_backend_output_config));
>> +       if(!new_outputs)
>> +               return -1;
>> +       ths->outputs = new_outputs;
>> +       ths->outputs[(ths->noutputs)++] = *output_config;
>> +       return 0;
>> +}
>> +
>> +
>> +static int
>> +load_x11_backend(struct weston_compositor *c, char const * backend,
>> +                int *argc, char **argv, struct weston_config *wc)
>> +{
>> +       struct weston_x11_backend_config config;
>> +       struct weston_config_section *section;
>> +       int ret = 0;
>> +
>> +       weston_x11_backend_config_init(&config);
>> +
>> +       int option_width = 0;
>> +       int option_height = 0;
>> +       int option_scale = 0;
>> +       int option_count = 0;
>> +
>> +       config.fullscreen = 0;
>> +       config.no_input = 0;
>> +       config.use_pixman = 0;
>> +
>> +       const struct weston_option x11_options[] = {
>> +               { WESTON_OPTION_INTEGER, "width", 0, &option_width },
>> +               { WESTON_OPTION_INTEGER, "height", 0, &option_height },
>> +               { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
>> +               { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
>> +               { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
>> +               { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
>> +               { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
>> +       };
>> +
>> +       parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
>> +
>> +       int output_count = 0;
>> +       char const *section_name;
>> +       char *name;
>> +       section = NULL;
>> +       config.noutputs = 0;
>> +       config.outputs = NULL;
>> +
>> +       while (weston_config_next_section(wc, &section, &section_name)) {
>> +               struct weston_x11_backend_output_config current_output = { 0, };
>> +
>> +               if (strcmp(section_name, "output") != 0) {
>> +                          continue;
>> +               }
>> +
>> +               weston_config_section_get_string(section, "name", &name, NULL);
>> +               if (name == NULL || name[0] != 'X') {
>> +                       free(name);
>> +                       continue;
>> +               }
>> +
>> +               current_output.name = name;
>> +
>> +               char *t;
>> +               char *mode;
>> +               weston_config_section_get_string(section, "mode", &mode, "1024x600");
>> +               if (sscanf(mode, "%dx%d", &current_output.width,
>> +                               &current_output.height) != 2) {
>> +                         weston_log("Invalid mode \"%s\" for output %s\n",
>> +                                                mode, name);
>> +                         current_output.width = 1024;
>> +                         current_output.height = 600;
>> +               }
>> +               free(mode);
>> +
>> +               if (current_output.width < 1)
>> +                       current_output.width = 1024;
>> +               if (current_output.height < 1)
>> +                       current_output.height = 600;
>> +
>> +               weston_config_section_get_int(section, "scale", &current_output.scale, 1);
>> +               if (option_scale)
>> +                       current_output.scale = option_scale;
>> +
>> +               weston_config_section_get_string(section,
>> +                                                                               "transform", &t, "normal");
>> +               if (weston_parse_transform(t, &current_output.transform) < 0)
>> +                          weston_log("Invalid transform \"%s\" for output %s\n",
>> +                                                 t, name);
>> +               free(t);
>> +
>> +               if(weston_x11_backend_config_append_output_config(&config,
>> +                               &current_output) < 0) {
>> +                       ret = -1;
>> +                       goto error;
>> +               }
>> +
>> +               output_count++;
>> +               if (option_count && output_count >= option_count)
>> +                       break;
>> +
>> +       }
>> +
>> +       struct weston_x11_backend_output_config default_output;
>> +       default_output.name = NULL;
>> +       default_output.width = option_width ? option_width : 1024;
>> +       default_output.height = option_height ? option_height : 600;
>> +       default_output.scale = option_scale ? option_scale : 1;
>> +       default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;
>> +
>> +       int i;
>> +       for (i = output_count; i < option_count; i++) {
>> +               char name[16];
>> +               snprintf(name, 16, "screen%d", i);
>> +               default_output.name = strdup(name);
>> +
>> +               if(weston_x11_backend_config_append_output_config(&config,
>> +                               &default_output) < 0) {
>> +                       ret = -1;
>> +                       goto error;
>> +               }
>> +
>> +       }
>> +
>> +       /* load the actual drm backend and configure it */
>> +       if (weston_x11_backend_load(c, &config) < 0) {
>> +               ret = -1;
>> +               goto error;
>> +       }

We have weston_load_backend_new() for the backend loading, see
https://patchwork.freedesktop.org/patch/76423/

>> +
>> +       return ret;
>> +
>> +error:
>> +       weston_x11_backend_config_outputs_clear(&config);
>> +       return ret;
>> +}
>> +
>>  static int
>>  load_backend(struct weston_compositor *compositor, const char *backend,
>>              int *argc, char **argv, struct weston_config *config)
>>  {
>> +       if (strstr(backend, "x11-backend.so"))
>> +               return load_x11_backend(compositor, backend, argc, argv, config);
>>  #if 0
>> -       if (strstr(backend, "drm-backend.so"))
>> +       else if (strstr(backend, "drm-backend.so"))
>>                 return load_drm_backend(compositor, backend, argc, argv, config);
>>         else if (strstr(backend, "wayland-backend.so"))
>>                 return load_wayland_backend(compositor, backend, argc, argv, config);
>> -       else if (strstr(backend, "x11-backend.so"))
>> -               return load_x11_backend(compositor, backend, argc, argv, config);
>>         else if (strstr(backend, "fbdev-backend.so"))
>>                 return load_fbdev_backend(compositor, backend, argc, argv, config);
>>         else if (strstr(backend, "headless-backend.so"))
>> --
>> 2.7.3
>>
>> _______________________________________________
>> wayland-devel mailing list
>> wayland-devel at lists.freedesktop.org
>> https://lists.freedesktop.org/mailman/listinfo/wayland-devel


More information about the wayland-devel mailing list