diff --git a/lib/wp/settings.c b/lib/wp/settings.c index 33137dcd..6b23a491 100644 --- a/lib/wp/settings.c +++ b/lib/wp/settings.c @@ -151,6 +151,32 @@ wp_settings_get_int (WpSettings *self, const gchar *setting, return true; } +/*! + * \brief Gets the float value of a setting + * \ingroup wpsettings + * \param self the settings object + * \param setting name of the setting + * \param val (out): the float value of the setting + * \returns TRUE if the setting is defined, FALSE otherwise + */ +gboolean +wp_settings_get_float (WpSettings *self, const gchar *setting, + gfloat *val) +{ + g_return_val_if_fail (self, false); + g_return_val_if_fail (setting, false); + + if (!(wp_object_get_active_features (WP_OBJECT (self)) & + WP_OBJECT_FEATURES_ALL)) + return false; + + if (!wp_properties_get (self->settings, setting)) + return false; + + spa_atof (wp_properties_get (self->settings, setting), val); + return true; +} + /*! * \brief Applies the rules and returns the applied properties. * diff --git a/lib/wp/settings.h b/lib/wp/settings.h index 79af91a1..3dfd1b79 100644 --- a/lib/wp/settings.h +++ b/lib/wp/settings.h @@ -42,10 +42,15 @@ gboolean wp_settings_get_boolean (WpSettings *self, const gchar *setting, WP_API gboolean wp_settings_get_string (WpSettings *self, const gchar *setting, const char **value); + WP_API gboolean wp_settings_get_int (WpSettings *self, const gchar *setting, gint64 *val); +WP_API +gboolean wp_settings_get_float (WpSettings *self, const gchar *setting, + gfloat *val); + WP_API gboolean wp_settings_apply_rule (WpSettings *self, const gchar *rule, WpProperties *client_props, WpProperties *applied_props); diff --git a/modules/module-lua-scripting/api/api.c b/modules/module-lua-scripting/api/api.c index 2b566366..9e990ee1 100644 --- a/modules/module-lua-scripting/api/api.c +++ b/modules/module-lua-scripting/api/api.c @@ -1544,6 +1544,30 @@ get_int (lua_State *L) return 1; } +static gboolean +get_float (lua_State *L) +{ + const char *setting = luaL_checkstring (L, 1); + const char *m = NULL; + + if (lua_type (L, 2) == LUA_TSTRING) + m = luaL_checkstring (L, 2); + + g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), m); + + if (s) + { + gfloat value = 0; + if (wp_settings_get_float (s, setting, &value)) + lua_pushnumber (L, value); + else + lua_pushnil (L); + } + else + lua_pushnil (L); + return 1; +} + static gboolean apply_rule (lua_State *L) { @@ -1574,6 +1598,7 @@ static const luaL_Reg settings_methods[] = { { "apply_rule", apply_rule }, { "get_string", get_string }, { "get_int", get_int }, + { "get_float", get_float }, { NULL, NULL } }; diff --git a/tests/wp/settings.c b/tests/wp/settings.c index cf74ed8d..f9d64b09 100644 --- a/tests/wp/settings.c +++ b/tests/wp/settings.c @@ -112,7 +112,7 @@ test_parsing_setup (TestSettingsFixture *self, gconstpointer user_data) self->settings = g_steal_pointer (&settings); /* total no.of settings in the conf file */ - g_assert_cmpint (data.count, ==, 11); + g_assert_cmpint (data.count, ==, 13); } } @@ -129,7 +129,7 @@ static void test_parsing (TestSettingsFixture *self, gconstpointer data) { /* total no.of settings in the conf file */ - g_assert_cmpint (wp_properties_get_count(self->settings), ==, 11); + g_assert_cmpint (wp_properties_get_count(self->settings), ==, 13); } static void @@ -277,6 +277,7 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data) g_assert_true (wp_settings_get_int (s, "test-property4-min-int", &value)); g_assert_cmpint (value, ==, G_MININT64); + value = 0; g_assert_true (wp_settings_get_int (s, "test-property4-max-int-one-more", &value)); g_assert_cmpint (value, ==, 0); @@ -305,6 +306,21 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data) g_assert_cmpstr (value, ==, "-20"); } + { + gfloat value = 0.0; + /* _get_float () */ + g_assert_false (wp_settings_get_float (s, "test-property-undefined", + &value)); + + g_assert_true (wp_settings_get_float (s, "test-property-float1", + &value)); + + g_assert_cmpfloat_with_epsilon (value, 3.14, 0.001); + + g_assert_true (wp_settings_get_float (s, "test-property-float2", + &value)); + g_assert_cmpfloat_with_epsilon (value, 0.4, 0.001); + } /* test the wp_settings_get_instance () API */ { g_autoptr (WpSettings) s1 = diff --git a/tests/wp/settings.conf b/tests/wp/settings.conf index 4ad47035..27f5b014 100644 --- a/tests/wp/settings.conf +++ b/tests/wp/settings.conf @@ -129,6 +129,8 @@ wireplumber.settings = { } } ] + test-property-float1 = "3.14" + test-property-float2 = "0.4" rule_three = [ { matches = [ @@ -136,6 +138,9 @@ wireplumber.settings = { test.string6 = "~metal*" test.table.entry = true } + { + test-string6-wildcard = "~*" + } ] actions = { update-props = { @@ -161,7 +166,5 @@ wireplumber.settings = { } } } - ] - } diff --git a/tests/wplua/scripts/settings.lua b/tests/wplua/scripts/settings.lua index 902ddffc..205d6b4d 100644 --- a/tests/wplua/scripts/settings.lua +++ b/tests/wplua/scripts/settings.lua @@ -7,6 +7,7 @@ local value = Settings.get_boolean ("test-property1", "test-settings") assert (value == false) value = Settings.get_boolean ("test-property2", "test-settings") +assert ("boolean" == type (value)) assert (value == true) value = Settings.get_boolean ("test-property1") @@ -22,6 +23,7 @@ value = Settings.get_int ("test-property-undefined", "test-settings") assert (value == nil) value = Settings.get_int ("test-property3-int", "test-settings") +assert ("number" == type (value)) assert (value == -20) value = Settings.get_int ("test-property4-max-int", "test-settings") @@ -43,11 +45,24 @@ value = Settings.get_string ("test-property-undefined", "test-settings") assert (value == nil) value = Settings.get_string ("test-property4-string", "test-settings") +assert ("string" == type (value)) assert (value == "blahblah") value = Settings.get_string ("test-property3-int", "test-settings") assert (value == "-20") +-- test settings _get_float () +value = Settings.get_float ("test-property-undefined", "test-settings") +assert (value == nil) + +value = Settings.get_float ("test-property-float1", "test-settings") +assert ("number" == type (value)) +assert ((value - 3.14) < 0.00001) + +value = Settings.get_float ("test-property-float2", "test-settings") +assert ((value - 0.4) < 0.00001) + + -- test rules -- test #1 local cp = { @@ -147,3 +162,14 @@ assert (ap["prop.electrical.conductivity"] == "false") assert (ap["prop.state"] == "gas") assert (ap["prop.example"] == "neon") +-- test #9 +local cp = { + ["test-string6-wildcard"] = "wild_cat", +} + +local applied, ap = Settings.apply_rule ("rule_three", cp, "test-settings") + +assert (applied == true) +assert (ap["prop.electrical.conductivity"] == "true") +assert (ap["prop.state"] == "solid") +assert (ap["prop.example"] == "ferrous")