[RFC weston 3/5] libweston: Add config getters API
Quentin Glidic
sardemff7+wayland at sardemff7.net
Tue Feb 9 15:14:42 UTC 2016
From: Quentin Glidic <sardemff7+git at sardemff7.net>
Signed-off-by: Quentin Glidic <sardemff7+git at sardemff7.net>
---
Makefile.am | 1 +
lib/backend-config.c | 33 ++++++++++++
lib/libweston-internal.h | 10 ++++
lib/libweston.h | 11 ++++
src/compositor.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++
src/compositor.h | 2 +
src/main.c | 12 +++--
7 files changed, 201 insertions(+), 5 deletions(-)
create mode 100644 lib/backend-config.c
diff --git a/Makefile.am b/Makefile.am
index 1cfe154..36078ba 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -103,6 +103,7 @@ noinst_LTLIBRARIES += libweston.la
libweston_la_SOURCES = \
lib/libweston.h \
lib/libweston.c \
+ lib/backend-config.c \
$(NULL)
libweston_la_CFLAGS = $(AM_CFLAGS) $(COMPOSITOR_CFLAGS) $(LIBUNWIND_CFLAGS)
libweston_la_LDFLAGS = \
diff --git a/lib/backend-config.c b/lib/backend-config.c
new file mode 100644
index 0000000..56e0936
--- /dev/null
+++ b/lib/backend-config.c
@@ -0,0 +1,33 @@
+
+#include "libweston-internal.h"
+#include "backend-config.h"
+
+WL_EXPORT void
+libweston_backend_config_set_user_data(struct libweston_context *context, void *user_data)
+{
+ context->backend_config.user_data = user_data;
+}
+
+WL_EXPORT void
+libweston_backend_config_set_bool_getter(struct libweston_context *context, libweston_backend_bool_getter getter)
+{
+ context->backend_config.bool_getter = getter;
+}
+
+WL_EXPORT void
+libweston_backend_config_set_string_getter(struct libweston_context *context, libweston_backend_string_getter getter)
+{
+ context->backend_config.string_getter = getter;
+}
+
+WL_EXPORT void
+libweston_backend_config_set_string_list_getter(struct libweston_context *context, libweston_backend_string_list_getter getter)
+{
+ context->backend_config.string_list_getter = getter;
+}
+
+WL_EXPORT void
+libweston_backend_config_set_int_getter(struct libweston_context *context, libweston_backend_int_getter getter)
+{
+ context->backend_config.int_getter = getter;
+}
diff --git a/lib/libweston-internal.h b/lib/libweston-internal.h
index 2e1b7c3..1e5267e 100644
--- a/lib/libweston-internal.h
+++ b/lib/libweston-internal.h
@@ -7,8 +7,18 @@
int libweston_backend_init(struct libweston_context *context);
+struct backend_config_interface {
+ void *user_data;
+
+ libweston_backend_bool_getter bool_getter;
+ libweston_backend_string_getter string_getter;
+ libweston_backend_string_list_getter string_list_getter;
+ libweston_backend_int_getter int_getter;
+};
+
struct libweston_context {
struct weston_compositor *compositor;
+ struct backend_config_interface backend_config;
};
#endif /* _LIBWESTON_INTERNAL_H_ */
diff --git a/lib/libweston.h b/lib/libweston.h
index 9d1bfc1..7a995b8 100644
--- a/lib/libweston.h
+++ b/lib/libweston.h
@@ -2,6 +2,7 @@
#ifndef _LIBWESTON_H_
#define _LIBWESTON_H_
+#include <stdbool.h>
#include "compositor.h"
struct libweston_context;
@@ -16,4 +17,14 @@ enum libweston_backend {
};
int libweston_load_backend(struct libweston_context *context, enum libweston_backend preffered);
+typedef bool (*libweston_backend_bool_getter)(const char *section, const char *key, bool default_value, void *user_data);
+typedef char *(*libweston_backend_string_getter)(const char *section, const char *key, const char *default_value, void *user_data);
+typedef char **(*libweston_backend_string_list_getter)(const char *section, const char *key, const char * const *default_value, size_t *size, void *user_data);
+typedef int (*libweston_backend_int_getter)(const char *section, const char *key, int default_value, void *user_data);
+void libweston_backend_config_set_user_data(struct libweston_context *context, void *user_data);
+void libweston_backend_config_set_bool_getter(struct libweston_context *context, libweston_backend_bool_getter getter);
+void libweston_backend_config_set_string_getter(struct libweston_context *context, libweston_backend_string_getter getter);
+void libweston_backend_config_set_string_list_getter(struct libweston_context *context, libweston_backend_string_list_getter getter);
+void libweston_backend_config_set_int_getter(struct libweston_context *context, libweston_backend_int_getter getter);
+
#endif /* _LIBWESTON_H_ */
diff --git a/src/compositor.c b/src/compositor.c
index 5b698d1..57c3612 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -4701,6 +4701,137 @@ timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
weston_timeline_open(compositor);
}
+static bool
+backend_config_bool_getter(const char *section, const char *key, bool default_value, void *user_data)
+{
+ struct weston_compositor *compositor = user_data;
+ int r = default_value;
+
+ if (!section) {
+ const struct weston_option options[] = {
+ { WESTON_OPTION_BOOLEAN, key, 0, &r },
+ };
+
+ parse_options(options, ARRAY_LENGTH(options), &compositor->argc, compositor->argv);
+ }
+ else
+ {
+ struct weston_config_section *config_section;
+
+ config_section = weston_config_get_section(compositor->config, section, NULL, NULL);
+ if (!config_section)
+ return default_value;
+
+ weston_config_section_get_bool(config_section, key, &r, default_value);
+ }
+
+ return r;
+}
+
+static char *
+backend_config_string_getter(const char *section, const char *key, const char *default_value, void *user_data)
+{
+ struct weston_compositor *compositor = user_data;
+ char *r = NULL;
+
+ if (!section) {
+ const struct weston_option options[] = {
+ { WESTON_OPTION_STRING, key, 0, &r },
+ };
+
+ parse_options(options, ARRAY_LENGTH(options), &compositor->argc, compositor->argv);
+ }
+ else
+ {
+ struct weston_config_section *config_section;
+
+ config_section = weston_config_get_section(compositor->config, section, NULL, NULL);
+ if (config_section)
+ weston_config_section_get_string(config_section, key, &r, default_value);
+ }
+ if (!r && default_value)
+ return strdup(default_value);
+ return r;
+}
+
+static char **
+backend_config_string_list_getter(const char *section, const char *key, const char * const *default_value, size_t *size, void *user_data)
+{
+ struct weston_compositor *compositor = user_data;
+ char *s = NULL;
+ char **r = NULL;
+
+ if (!section) {
+ const struct weston_option options[] = {
+ { WESTON_OPTION_STRING, key, 0, &s },
+ };
+
+ parse_options(options, ARRAY_LENGTH(options), &compositor->argc, compositor->argv);
+ }
+ else
+ {
+ struct weston_config_section *config_section;
+
+ config_section = weston_config_get_section(compositor->config, section, NULL, NULL);
+ if (config_section)
+ weston_config_section_get_string(config_section, key, &s, NULL);
+ }
+ if (!s && default_value) {
+ size_t i;
+
+ r = malloc(sizeof(char*) * *size);
+ for (i = 0; i < *size; ++i)
+ r[i] = strdup(default_value[i]);
+ } else if (s) {
+ char *w = s, *sep;
+ size_t i = 0;
+ while ((sep = strchr(w, ','))) {
+ ++i;
+ w = ++sep;
+ }
+
+ r = malloc(sizeof(char*) * i);
+ w = s;
+ i = 0;
+ while ((sep = strchr(w, ','))) {
+ r[i] = strndup(w, sep - w);
+ ++i;
+ w = ++sep;
+ }
+ *size = i;
+ } else
+ *size = 0;
+
+ return r;
+}
+
+static int
+backend_config_int_getter(const char *section, const char *key, int default_value, void *user_data)
+{
+ struct weston_compositor *compositor = user_data;
+ int r = default_value;
+
+ if (!section) {
+ const struct weston_option options[] = {
+ { WESTON_OPTION_INTEGER, key, 0, &r },
+ };
+
+ parse_options(options, ARRAY_LENGTH(options), &compositor->argc, compositor->argv);
+ }
+ else
+ {
+ struct weston_config_section *config_section;
+
+ config_section = weston_config_get_section(compositor->config, section, NULL, NULL);
+ if (!config_section)
+ return default_value;
+
+ weston_config_section_get_int(config_section, key, &r, default_value);
+ }
+
+ return r;
+}
+
/** Create the compositor.
*
* This functions creates and initializes a compositor instance.
@@ -4724,6 +4855,12 @@ weston_compositor_create(struct wl_display *display, void *user_data)
if (!ec->libweston)
goto fail;
+ libweston_backend_config_set_user_data(ec->libweston, ec);
+ libweston_backend_config_set_bool_getter(ec->libweston, backend_config_bool_getter);
+ libweston_backend_config_set_string_getter(ec->libweston, backend_config_string_getter);
+ libweston_backend_config_set_string_list_getter(ec->libweston, backend_config_string_list_getter);
+ libweston_backend_config_set_int_getter(ec->libweston, backend_config_int_getter);
+
ec->wl_display = display;
ec->user_data = user_data;
wl_signal_init(&ec->destroy_signal);
diff --git a/src/compositor.h b/src/compositor.h
index 7b6b013..dfe47ae 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -708,6 +708,8 @@ struct weston_compositor {
struct wl_display *wl_display;
struct weston_shell_interface shell_interface;
struct weston_config *config;
+ int argc;
+ char **argv;
/* surface signals */
struct wl_signal create_surface_signal;
diff --git a/src/main.c b/src/main.c
index 2425fda..3e52168 100644
--- a/src/main.c
+++ b/src/main.c
@@ -783,8 +783,10 @@ int main(int argc, char *argv[])
ec->config = config;
if (weston_compositor_init_config(ec, config) < 0)
goto out;
+ ec->argc = argc;
+ ec->argv = argv;
- if (load_backend(ec, backend, &argc, argv, config) < 0) {
+ if (load_backend(ec, backend, &ec->argc, ec->argv, config) < 0) {
weston_log("fatal: failed to create compositor backend\n");
goto out;
}
@@ -831,14 +833,14 @@ int main(int argc, char *argv[])
weston_config_section_get_string(section, "shell", &shell,
"desktop-shell.so");
- if (load_modules(ec, shell, &argc, argv) < 0)
+ if (load_modules(ec, shell, &ec->argc, ec->argv) < 0)
goto out;
weston_config_section_get_string(section, "modules", &modules, "");
- if (load_modules(ec, modules, &argc, argv) < 0)
+ if (load_modules(ec, modules, &ec->argc, ec->argv) < 0)
goto out;
- if (load_modules(ec, option_modules, &argc, argv) < 0)
+ if (load_modules(ec, option_modules, &ec->argc, ec->argv) < 0)
goto out;
section = weston_config_get_section(config, "keyboard", NULL, NULL);
@@ -857,7 +859,7 @@ int main(int argc, char *argv[])
for (i = 1; i < argc; i++)
weston_log("fatal: unhandled option: %s\n", argv[i]);
- if (argc > 1)
+ if (ec->argc > 1)
goto out;
weston_compositor_wake(ec);
--
2.6.4
More information about the wayland-devel
mailing list