From 473802cab83c79140d6de334c3a0942340bd8fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 11 May 2019 10:43:14 +0200 Subject: [PATCH] bar: allow explicit backend selection --- bar/bar.c | 66 ++++++++++++++++++++++++++++++++++++++++--------------- bar/bar.h | 3 +++ config.c | 2 ++ 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/bar/bar.c b/bar/bar.c index 44d70fa..e417710 100644 --- a/bar/bar.c +++ b/bar/bar.c @@ -368,6 +368,52 @@ destroy(struct bar *bar) struct bar * bar_new(const struct bar_config *config) { + void *backend_data = NULL; + const struct backend *backend_iface = NULL; + + switch (config->backend) { + case BAR_BACKEND_AUTO: +#if defined(ENABLE_X11) && !defined(ENABLE_WAYLAND) + backend_data = bar_backend_xcb_new(); + backend_iface = &xcb_backend_iface; +#elif !defined(ENABLE_X11) && defined(ENABLE_WAYLAND) + backend_data = bar_backend_wayland_new(); + backend_iface = &wayland_backend_iface; +#else + if (getenv("WAYLAND_DISPLAY") != NULL) { + backend_data = bar_backend_wayland_new(); + backend_iface = &wayland_backend_iface; + } else { + backend_data = bar_backend_xcb_new(); + backend_iface = &xcb_backend_iface; + } +#endif + break; + + case BAR_BACKEND_XCB: +#if defined(ENABLE_X11) + backend_data = bar_backend_xcb_new(); + backend_iface = &xcb_backend_iface; +#else + LOG_ERR("f00bar was compiled without the XCB backend"); + return NULL; +#endif + break; + + case BAR_BACKEND_WAYLAND: +#if defined(BAR_WAYLAND) + backend_data = bar_backend_wayland_new(); + backend_iface = &wayland_backend_iface; +#else + LOG_ERR("f00bar was compiled without the Wayland backend"); + return NULL; +#endif + break; + } + + if (backend_data == NULL) + return NULL; + struct private *priv = calloc(1, sizeof(*priv)); priv->monitor = config->monitor != NULL ? strdup(config->monitor) : NULL; priv->location = config->location; @@ -392,24 +438,8 @@ bar_new(const struct bar_config *config) priv->left.count = config->left.count; priv->center.count = config->center.count; priv->right.count = config->right.count; - -#if defined(ENABLE_X11) && !defined(ENABLE_WAYLAND) - priv->backend.data = bar_backend_xcb_new(); - priv->backend.iface = &xcb_backend_iface; -#else -#if !defined(ENABLE_X11) && defined(ENABLE_WAYLAND) - priv->backend.data = bar_backend_wayland_new(); - priv->backend.iface = &wayland_backend_iface; -#else - if (getenv("WAYLAND_DISPLAY") != NULL) { - priv->backend.data = bar_backend_wayland_new(); - priv->backend.iface = &wayland_backend_iface; - } else { - priv->backend.data = bar_backend_xcb_new(); - priv->backend.iface = &xcb_backend_iface; - } -#endif -#endif + priv->backend.data = backend_data; + priv->backend.iface = backend_iface; for (size_t i = 0; i < priv->left.count; i++) priv->left.mods[i] = config->left.mods[i]; diff --git a/bar/bar.h b/bar/bar.h index 90bbb96..108a972 100644 --- a/bar/bar.h +++ b/bar/bar.h @@ -15,8 +15,11 @@ struct bar { }; enum bar_location { BAR_TOP, BAR_BOTTOM }; +enum bar_backend { BAR_BACKEND_AUTO, BAR_BACKEND_XCB, BAR_BACKEND_WAYLAND }; struct bar_config { + enum bar_backend backend; + const char *monitor; enum bar_location location; int height; diff --git a/config.c b/config.c index d576460..e45d5cd 100644 --- a/config.c +++ b/config.c @@ -176,6 +176,8 @@ conf_to_bar(const struct yml_node *bar) struct bar_config conf = {0}; + conf.backend = BAR_BACKEND_AUTO; + /* * Required attributes */