From a75cdbbc5930bdc169195cc5233d871f4a3b229e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guido=20G=C3=BCnther?= Date: Mon, 5 Dec 2022 18:44:18 +0100 Subject: [PATCH] theme-expander: Allow to set the theme to expand --- src/fbd-feedback-manager.c | 1 + src/fbd-theme-expander.c | 59 +++++++++++++++++++++++++++++---- src/fbd-theme-expander.h | 3 ++ tests/meson.build | 2 +- tests/test-fbd-theme-expander.c | 37 +++++++++++++++++++-- 5 files changed, 91 insertions(+), 11 deletions(-) diff --git a/src/fbd-feedback-manager.c b/src/fbd-feedback-manager.c index 7872132..a8f45e4 100644 --- a/src/fbd-feedback-manager.c +++ b/src/fbd-feedback-manager.c @@ -556,6 +556,7 @@ fbd_feedback_manager_load_theme (FbdFeedbackManager *self) } expander = fbd_theme_expander_new ((const char *const *)compatibles, + NULL, g_getenv (FEEDBACKD_THEME_VAR)); theme = fbd_theme_expander_load_theme_files (expander, &err); diff --git a/src/fbd-theme-expander.c b/src/fbd-theme-expander.c index 69a9cea..2979e3b 100644 --- a/src/fbd-theme-expander.c +++ b/src/fbd-theme-expander.c @@ -12,9 +12,6 @@ #include "fbd-feedback-theme.h" #include "fbd-theme-expander.h" -#define DEVICE_TREE_PATH "/sys/firmware/devicetree/base/compatible" -#define DEVICE_NAME_MAX 1024 - #define DEFAULT_THEME_NAME "default" #define DEVICE_THEME_NAME "$device" @@ -31,6 +28,7 @@ enum { PROP_0, + PROP_THEME_NAME, PROP_THEME_FILE, PROP_COMPATIBLES, PROP_LAST_PROP @@ -40,6 +38,7 @@ static GParamSpec *props[PROP_LAST_PROP]; struct _FbdThemeExpander { GObject parent; + char *theme_name; char *theme_file; gboolean device_theme_loaded; GStrv compatibles; @@ -59,6 +58,20 @@ fbd_theme_expander_set_compatibles (FbdThemeExpander *self, const char * const * self->device_theme_loaded = FALSE; } +static void +fbd_theme_expander_set_theme_name (FbdThemeExpander *self, const char *theme_name) +{ + g_return_if_fail (FBD_IS_THEME_EXPANDER (self)); + + g_free (self->theme_name); + if (theme_name) + self->theme_name = g_strdup (theme_name); + else + self->theme_name = g_strdup (DEFAULT_THEME_NAME); + + /* Make sure we reload the device theme */ + self->device_theme_loaded = FALSE; +} static void fbd_theme_expander_set_theme_file (FbdThemeExpander *self, const char *theme_file) @@ -187,6 +200,9 @@ fbd_theme_expander_set_property (GObject *object, FbdThemeExpander *self = FBD_THEME_EXPANDER (object); switch (property_id) { + case PROP_THEME_NAME: + fbd_theme_expander_set_theme_name (self, g_value_get_string (value)); + break; case PROP_THEME_FILE: fbd_theme_expander_set_theme_file (self, g_value_get_string (value)); break; @@ -209,6 +225,9 @@ fbd_theme_expander_get_property (GObject *object, FbdThemeExpander *self = FBD_THEME_EXPANDER (object); switch (property_id) { + case PROP_THEME_NAME: + g_value_set_string (value, fbd_theme_expander_get_theme_name (self)); + break; case PROP_THEME_FILE: g_value_set_string (value, fbd_theme_expander_get_theme_file (self)); break; @@ -227,6 +246,7 @@ fbd_theme_expander_finalize (GObject *object) { FbdThemeExpander *self = FBD_THEME_EXPANDER(object); + g_clear_pointer (&self->theme_name, g_free); g_clear_pointer (&self->theme_file, g_free); g_clear_pointer (&self->compatibles, g_strfreev); @@ -243,6 +263,18 @@ fbd_theme_expander_class_init (FbdThemeExpanderClass *klass) object_class->set_property = fbd_theme_expander_set_property; object_class->finalize = fbd_theme_expander_finalize; + /** + * FbdThemeExpander:theme-name: + * + * Specifies the theme to expand. Assumes `default` if unset. + * + * The `compatibles` defines which device specific theme bits are loaded. + */ + props[PROP_THEME_NAME] = + g_param_spec_string ("theme-name", "", "", + DEFAULT_THEME_NAME, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | + G_PARAM_STATIC_STRINGS); /** * FbdThemeExpander:theme-file: * @@ -278,9 +310,12 @@ fbd_theme_expander_init (FbdThemeExpander *self) FbdThemeExpander * -fbd_theme_expander_new (const char * const *compatibles, const char *theme_file) +fbd_theme_expander_new (const char * const *compatibles, + const char *theme_name, + const char *theme_file) { return FBD_THEME_EXPANDER (g_object_new (FBD_TYPE_THEME_EXPANDER, + "theme-name", theme_name, "theme-file", theme_file, "compatibles", compatibles, NULL)); @@ -322,10 +357,10 @@ fbd_theme_expander_load_theme_files (FbdThemeExpander *self, GError **err) g_return_val_if_fail (FBD_IS_THEME_EXPANDER (self), NULL); g_return_val_if_fail (err == NULL || *err == NULL, NULL); - if (self->theme_file) + if (self->theme_file) { theme_file = g_strdup (self->theme_file); - else { - theme_file = fbd_theme_expander_find_theme_path (self, DEFAULT_THEME_NAME); + } else { + theme_file = fbd_theme_expander_find_theme_path (self, self->theme_name); if (g_strcmp0 (self->theme_file, theme_file)) { self->theme_file = g_steal_pointer (&theme_file); g_object_notify_by_pspec (G_OBJECT (self), props[PROP_THEME_FILE]); @@ -380,9 +415,19 @@ fbd_theme_expander_load_theme_files (FbdThemeExpander *self, GError **err) /* Merge themes bottom to top */ g_queue_foreach (queue, update_theme, merged); + fbd_feedback_theme_set_name (merged, self->theme_name); return g_steal_pointer (&merged); } +const char * +fbd_theme_expander_get_theme_name (FbdThemeExpander *self) +{ + g_return_val_if_fail (FBD_IS_THEME_EXPANDER (self), NULL); + + return self->theme_name; + +} + const char * fbd_theme_expander_get_theme_file (FbdThemeExpander *self) { diff --git a/src/fbd-theme-expander.h b/src/fbd-theme-expander.h index 8d2db79..834907e 100644 --- a/src/fbd-theme-expander.h +++ b/src/fbd-theme-expander.h @@ -18,10 +18,13 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (FbdThemeExpander, fbd_theme_expander, FBD, THEME_EXPANDER, GObject) + FbdThemeExpander *fbd_theme_expander_new (const char * const *compatibles, + const char *theme_name, const char *theme_file); FbdFeedbackTheme *fbd_theme_expander_load_theme_files (FbdThemeExpander *self, GError **err); +const char *fbd_theme_expander_get_theme_name (FbdThemeExpander *self); const char *fbd_theme_expander_get_theme_file (FbdThemeExpander *self); const char * const *fbd_theme_expander_get_compatibles (FbdThemeExpander *self); diff --git a/tests/meson.build b/tests/meson.build index c776cf0..254f684 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -8,7 +8,7 @@ test_env.set('GSETTINGS_BACKEND', 'memory') test_env.set('GSETTINGS_SCHEMA_DIR', '@0@/data'.format(meson.project_build_root())) test_env.set('PYTHONDONTWRITEBYTECODE', 'yes') test_env.set('MALLOC_CHECK_', '2') -test_env.set('XDG_CONFIG_HOME', meson.current_source_dir()) +test_env.set('XDG_CONFIG_HOME', meson.current_source_dir() / 'data' / 'user-config') test_env.set('XDG_CONFIG_DIRS', meson.current_source_dir()) # Shared library Unit tests diff --git a/tests/test-fbd-theme-expander.c b/tests/test-fbd-theme-expander.c index 1bde3cd..1ffb2af 100644 --- a/tests/test-fbd-theme-expander.c +++ b/tests/test-fbd-theme-expander.c @@ -15,6 +15,7 @@ test_fbd_theme_expander_object (void) { g_autoptr (FbdThemeExpander) expander = g_object_new (FBD_TYPE_THEME_EXPANDER, NULL); + g_assert_cmpstr ("default", ==, fbd_theme_expander_get_theme_name (expander)); g_assert_null (fbd_theme_expander_get_theme_file (expander)); g_assert_null (fbd_theme_expander_get_compatibles (expander)); } @@ -32,7 +33,9 @@ test_fbd_theme_expander_device (void) /* just the default profile */ compatibles[0] = "doesnotexist"; - expander = fbd_theme_expander_new (compatibles, NULL); + expander = fbd_theme_expander_new (compatibles, NULL, NULL); + g_assert_cmpstr ("default", ==, fbd_theme_expander_get_theme_name (expander)); + theme = fbd_theme_expander_load_theme_files (expander, &err); compatibles2 = fbd_theme_expander_get_compatibles (expander); g_assert_cmpstrv (compatibles2, compatibles); @@ -48,7 +51,7 @@ test_fbd_theme_expander_device (void) /* Empty device theme that just chains up */ compatibles[0] = "chainup"; - expander = fbd_theme_expander_new (compatibles, NULL); + expander = fbd_theme_expander_new (compatibles, NULL, NULL); compatibles2 = fbd_theme_expander_get_compatibles (expander); g_assert_cmpstrv (compatibles2, compatibles); theme = fbd_theme_expander_load_theme_files (expander, &err); @@ -64,7 +67,7 @@ test_fbd_theme_expander_device (void) /* Replace an existing event */ compatibles[0] = "replace"; - expander = fbd_theme_expander_new (compatibles, NULL); + expander = fbd_theme_expander_new (compatibles, NULL, NULL); compatibles2 = fbd_theme_expander_get_compatibles (expander); g_assert_cmpstrv (compatibles2, compatibles); theme = fbd_theme_expander_load_theme_files (expander, &err); @@ -82,6 +85,33 @@ test_fbd_theme_expander_device (void) g_assert_finalize_object (expander); } +static void +test_fbd_theme_expander_custom (void) +{ + g_autoptr (GError) err = NULL; + const char *compatibles[] = { NULL, NULL }; + FbdFeedbackProfile *profile; + FbdThemeExpander *expander; + FbdFeedbackTheme *theme; + FbdFeedbackBase *fb; + + /* Load a custom theme that overrides a single event on top of the `replace` from above */ + compatibles[0] = "replace"; + expander = fbd_theme_expander_new (compatibles, "custom", NULL); + theme = fbd_theme_expander_load_theme_files (expander, &err); + g_assert_no_error (err); + profile = fbd_feedback_theme_get_profile (theme, "full"); + g_assert_true (FBD_IS_FEEDBACK_PROFILE(profile)); + fb = fbd_feedback_profile_get_feedback (profile, "test-dummy-0"); + g_assert_cmpstr ("test-dummy-0", ==, fbd_feedback_get_event_name (fb)); + g_assert_cmpint (fbd_feedback_dummy_get_duration (FBD_FEEDBACK_DUMMY (fb)), ==, 0x10); + g_assert_true (FBD_IS_FEEDBACK_PROFILE(profile)); + fb = fbd_feedback_profile_get_feedback (profile, "test-dummy-2"); + g_assert_cmpstr ("test-dummy-2", ==, fbd_feedback_get_event_name (fb)); + g_assert_cmpint (fbd_feedback_dummy_get_duration (FBD_FEEDBACK_DUMMY (fb)), ==, 0x30); + g_assert_finalize_object (theme); + g_assert_finalize_object (expander); +} gint main (int argc, char *argv[]) @@ -90,6 +120,7 @@ main (int argc, char *argv[]) g_test_add_func("/feedbackd/fbd/theme-expander/object", test_fbd_theme_expander_object); g_test_add_func("/feedbackd/fbd/theme-expander/device", test_fbd_theme_expander_device); + g_test_add_func("/feedbackd/fbd/theme-expander/custom", test_fbd_theme_expander_custom); return g_test_run(); }