settings: remove _get_{boolean|int|float|string}() APIs

They is really no needed with the new _get() API and the WpSpaJson API. In C,
users can use 'wp_spa_json_parse_{boolean|int|float|string}()' APIs to parse the
WpSpaJson. In Lua, users can just do 'Settings.get(setting, m):parse()'.
This commit is contained in:
Julian Bouzas
2022-08-18 13:11:34 -04:00
parent 535af7a33e
commit 2f1aece8b6
16 changed files with 163 additions and 301 deletions

View File

@@ -161,70 +161,6 @@ wp_settings_unsubscribe (WpSettings *self, guintptr subscription_id)
return ret;
}
/*!
* \brief Gets the boolean value of a setting
*
* \ingroup wpsettings
* \param self the settings object
* \param setting name of the setting
* \param value (out): the boolean value of the setting
* \returns TRUE if the setting is defined, FALSE otherwise
*/
gboolean
wp_settings_get_boolean (WpSettings *self, const gchar *setting,
gboolean *value)
{
g_autoptr (WpSpaJson) json = wp_settings_get (self, setting);
return json && wp_spa_json_parse_boolean (json, value);
}
/*!
* \brief Gets the string value of a setting
* \ingroup wpsettings
* \param self the settings object
* \param setting name of the setting
* \returns (transfer full) (nullable): the string value of the setting, or NULL
* if the string could not be parsed
*/
gchar *
wp_settings_get_string (WpSettings *self, const gchar *setting)
{
g_autoptr (WpSpaJson) json = wp_settings_get (self, setting);
return json ? wp_spa_json_parse_string (json) : NULL;
}
/*!
* \brief Gets the integer (signed) value of a setting
* \ingroup wpsettings
* \param self the settings object
* \param setting name of the setting
* \param value (out): the integer value of the setting
* \returns TRUE if the setting is defined, FALSE otherwise
*/
gboolean
wp_settings_get_int (WpSettings *self, const gchar *setting,
gint *value)
{
g_autoptr (WpSpaJson) json = wp_settings_get (self, setting);
return json && wp_spa_json_parse_int (json, value);
}
/*!
* \brief Gets the float value of a setting
* \ingroup wpsettings
* \param self the settings object
* \param setting name of the setting
* \param value (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 *value)
{
g_autoptr (WpSpaJson) json = wp_settings_get (self, setting);
return json && wp_spa_json_parse_float (json, value);
}
/*!
* \brief Gets the WpSpaJson of a setting
* \ingroup wpsettings

View File

@@ -61,21 +61,6 @@ WP_API
gboolean wp_settings_unsubscribe (WpSettings *self,
guintptr subscription_id);
WP_API
gboolean wp_settings_get_boolean (WpSettings *self, const gchar *setting,
gboolean *value);
WP_API
gchar * wp_settings_get_string (WpSettings *self, const gchar *setting);
WP_API
gboolean wp_settings_get_int (WpSettings *self, const gchar *setting,
gint *value);
WP_API
gboolean wp_settings_get_float (WpSettings *self, const gchar *setting,
gfloat *value);
WP_API
WpSpaJson * wp_settings_get (WpSettings *self, const gchar *setting);

View File

@@ -746,16 +746,47 @@ wireplumber__module_init (WpCore * core, GVariant * args, GError ** error)
g_autofree gchar *echo_cancel_source_name = NULL;
g_autoptr (WpSettings) settings = wp_settings_get_instance(core, NULL);
wp_settings_get_int (settings, "device.save-interval-ms",
&save_interval_ms);
wp_settings_get_boolean (settings, "device.use-persistent-storage",
&use_persistent_storage);
wp_settings_get_boolean (settings, "device.auto-echo-cancel",
&auto_echo_cancel);
echo_cancel_sink_name = wp_settings_get_string (settings,
"device.echo-cancel-sink-name");
echo_cancel_source_name = wp_settings_get_string (settings,
"device.echo-cancel-source-name");
{
g_autoptr (WpSpaJson) j = wp_settings_get (settings,
"device.save-interval-ms");
if (j && !wp_spa_json_parse_int (j, &save_interval_ms))
wp_warning ("Failed to parse integer in device.save-interval-ms");
}
{
g_autoptr (WpSpaJson) j = wp_settings_get (settings,
"device.use-persistent-storage");
if (j && !wp_spa_json_parse_boolean (j, &save_interval_ms))
wp_warning ("Failed to parse boolean in device.use-persistent-storage");
}
{
g_autoptr (WpSpaJson) j = wp_settings_get (settings,
"device.auto-echo-cancel");
if (j && !wp_spa_json_parse_boolean (j, &auto_echo_cancel))
wp_warning ("Failed to parse boolean in device.auto-echo-cancel");
}
{
g_autoptr (WpSpaJson) j = wp_settings_get (settings,
"device.echo-cancel-sink-name");
if (j) {
echo_cancel_sink_name = wp_spa_json_parse_string (j);
if (!echo_cancel_sink_name)
wp_warning ("Failed to parse string in device.echo-cancel-sink-name");
}
}
{
g_autoptr (WpSpaJson) j = wp_settings_get (settings,
"device.echo-cancel-source-name");
if (j) {
echo_cancel_sink_name = wp_spa_json_parse_string (j);
if (!echo_cancel_sink_name)
wp_warning ("Failed to parse string in device.echo-cancel-source-name");
}
}
wp_plugin_register (g_object_new (wp_default_nodes_get_type (),
"name", NAME,

View File

@@ -1472,94 +1472,6 @@ impl_module_new (lua_State *L)
}
}
static int
settings_get_boolean (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) {
gboolean value = 0;
if (wp_settings_get_boolean (s, setting, &value))
lua_pushboolean (L, value);
else
lua_pushnil (L);
} else
lua_pushnil (L);
return 1;
}
static int
settings_get_string (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) {
g_autofree gchar *value = wp_settings_get_string (s, setting);
if (value)
lua_pushstring (L, value);
else
lua_pushnil (L);
} else
lua_pushnil (L);
return 1;
}
static int
settings_get_int (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) {
gint value = 0;
if (wp_settings_get_int (s, setting, &value))
lua_pushinteger (L, value);
else
lua_pushnil (L);
} else
lua_pushnil (L);
return 1;
}
static int
settings_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 int
settings_get (lua_State *L)
{
@@ -1667,10 +1579,6 @@ settings_unsubscribe (lua_State *L)
}
static const luaL_Reg settings_methods[] = {
{ "get_boolean", settings_get_boolean },
{ "get_string", settings_get_string },
{ "get_int", settings_get_int },
{ "get_float", settings_get_float },
{ "get", settings_get },
{ "get_all", settings_get_all },
{ "apply_rule", settings_apply_rule },

View File

@@ -182,8 +182,8 @@ do_load_components(void *data, const char *location, const char *section,
if (wp_spa_json_object_get (o, "deps", "s", &deps, NULL) && deps) {
gboolean value = 0;
gboolean defined = wp_settings_get_boolean (settings, deps, &value);
if ((!defined) || (defined && !value)) {
g_autoptr (WpSpaJson) j = wp_settings_get (settings, deps);
if (!j || !wp_spa_json_parse_boolean (j, &value)) {
wp_info ("deps(%s) not met for component(%s), skip loading it",
deps, name);
continue;

View File

@@ -16,7 +16,7 @@ function configProperties (node)
["item.node"] = node,
["item.plugged.usec"] = GLib.get_monotonic_time (),
["item.features.no-dsp"] =
Settings.get_boolean ("default-policy-audio.no-dsp"),
Settings.get ("default-policy-audio.no-dsp"):parse(),
["item.features.monitor"] = true,
["item.features.control-port"] = false,
["node.id"] = node ["bound-id"],

View File

@@ -27,9 +27,9 @@
-- settings file: policy.conf
local use_persistent_storage =
Settings.get_boolean ("bt-policy-use-persistent-storage") or false
Settings.get ("bt-policy-use-persistent-storage"):parse() or false
local use_headset_profile =
Settings.get_boolean ("bt-policy-media-role.use-headset-profile") or false
Settings.get ("bt-policy-media-role.use-headset-profile"):parse() or false
local applications = {}
local profile_restore_timeout_msec = 2000
@@ -45,10 +45,7 @@ local last_profiles = {}
local active_streams = {}
local previous_streams = {}
local apps_setting_str =
Settings.get_string ("bt-policy-media-role.applications")
json = Json.Raw (apps_setting_str)
apps_setting = json:parse ()
local apps_setting = Settings.get ("bt-policy-media-role.applications"):parse()
for i = 1, #apps_setting do
applications [apps_setting [i]] = true

View File

@@ -18,12 +18,28 @@
-- settings file: device.conf
use_persistent_storage =
Settings.get_boolean ("device.use-persistent-storage") or false
local use_persistent_storage =
Settings.get ("device.use-persistent-storage"):parse() or false
-- the default volume to apply
default_volume = tonumber (Settings.get_float ("device.default-volume") or 0.4^3)
default_input_volume = tonumber (Settings.get_float ("default-input-volume") or 1.0)
-- the default volume to apply (optional property)
local default_volume = 0.4^3
local default_volume_json = Settings.get ("device.default-volume")
if default_input_volume_json ~= nil then
local val = default_volume_json:parse()
if val ~= nil then
default_volume = val
end
end
-- the default input volume to apply (optional property)
local default_input_volume = 1.0
local default_input_volume_json = Settings.get ("default-input-volume")
if default_input_volume_json ~= nil then
local val = default_input_volume_json:parse()
if val ~= nil then
default_input_volume = val
end
end
-- table of device info
dev_infos = {}

View File

@@ -5,13 +5,15 @@
--
-- SPDX-License-Identifier: MIT
local roles = {}
local duck_level = Settings.get_float ("default-policy-duck.level") or 0.3
local duck_level = Settings.get ("default-policy-duck.level"):parse() or 0.3
local endpoint_roles_setting = Settings.get_string ("endpoints-roles")
if endpoint_roles_setting then
json = Json.Raw (endpoint_roles_setting)
roles = json:parse ()
local roles = {}
local roles_json = Settings.get ("endpoints-roles")
if roles_json ~= nil then
local val = roles_json:parse()
if val ~= nil then
roles = val
end
end
function findRole(role)

View File

@@ -7,10 +7,12 @@
-- Receive script arguments from config.lua
local roles = {}
local endpoint_roles_setting = Settings.get_string ("endpoints-roles")
if endpoint_roles_setting then
json = Json.Raw (endpoint_roles_setting)
roles = json:parse ()
local roles_json = Settings.get ("endpoints-roles")
if roles_json ~= nil then
local val = roles_json:parse()
if val ~= nil then
roles = val
end
end
local self = {}

View File

@@ -5,8 +5,8 @@
--
-- SPDX-License-Identifier: MIT
local move = Settings.get_boolean ("default-policy-move") or false
local follow = Settings.get_boolean ("default-policy-follow") or false
local move = Settings.get ("default-policy-move"):parse() or false
local follow = Settings.get ("default-policy-follow"):parse() or false
local self = {}
self.scanning = false

View File

@@ -16,9 +16,9 @@
-- settings file: policy.conf
local move = Settings.get_boolean ("default-policy-move") or false
local follow = Settings.get_boolean ("default-policy-follow") or false
local filter_forward_format = Settings.get_boolean ("filter.forward-format") or false
local move = Settings.get ("default-policy-move"):parse() or false
local follow = Settings.get ("default-policy-follow"):parse() or false
local filter_forward_format = Settings.get ("filter.forward-format"):parse() or false
local self = {}
self.scanning = false

View File

@@ -18,9 +18,9 @@
-- settings file: stream.conf
config_restore_props = Settings.get_boolean ("stream_default.restore-props")
config_restore_props = Settings.get ("stream_default.restore-props"):parse()
or false
config_restore_target = Settings.get_boolean ("stream_default.restore-target")
config_restore_target = Settings.get ("stream_default.restore-target"):parse()
or false
config_default_channel_volume = Settings.get_float ("stream.default-channel-volume")
or 1.0

View File

@@ -7,10 +7,12 @@
-- Receive script arguments from config.lua
local endpoints = {}
local endpoint_setting = Settings.get_string ("endpoints")
if endpoint_setting then
json = Json.Raw (endpoint_setting)
endpoints = json:parse ()
local endpoints_json = Settings.get ("endpoints")
if endpoints_json ~= nil then
local val = endpoints_json:parse()
if val ~= nil then
endpoints = val
end
end
function createEndpoint (factory_name, properties)

View File

@@ -262,14 +262,23 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
WpSettings *s = self->s;
{
gboolean value = 0;
/* undefined */
g_autoptr (WpSpaJson) j = wp_settings_get (s, "test-setting-undefined");
g_assert_null (j);
}
/* test settings _get_boolean */
g_assert_false (wp_settings_get_boolean (s, "test-setting-undefined",
&value));
g_assert_true (wp_settings_get_boolean (s, "test-setting1", &value));
{
gboolean value = FALSE;
/* _get_boolean */
g_autoptr (WpSpaJson) j = wp_settings_get (s, "test-setting1");
g_assert_nonnull (j);
g_assert_true (wp_spa_json_parse_boolean (j, &value));
g_assert_false (value);
g_assert_true (wp_settings_get_boolean (s, "test-setting2", &value));
g_autoptr (WpSpaJson) j2 = wp_settings_get (s, "test-setting2");
g_assert_nonnull (j2);
g_assert_true (wp_spa_json_parse_boolean (j2, &value));
g_assert_true (value);
}
@@ -277,10 +286,9 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
gint value = 0;
/* _get_int () */
g_assert_false (wp_settings_get_int (s, "test-setting-undefined",
&value));
g_assert_true (wp_settings_get_int (s, "test-setting3-int", &value));
g_autoptr (WpSpaJson) j = wp_settings_get (s, "test-setting3-int");
g_assert_nonnull (j);
g_assert_true (wp_spa_json_parse_int (j, &value));
g_assert_cmpint (value, ==, -20);
}
@@ -288,34 +296,19 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
/* _get_string () */
{
g_autofree gchar *value = NULL;
value = wp_settings_get_string (s, "test-setting-undefined");
g_assert_null (value);
}
{
g_autofree gchar *value = NULL;
value = wp_settings_get_string (s, "test-setting4-string");
g_autoptr (WpSpaJson) j = wp_settings_get (s, "test-setting4-string");
g_assert_nonnull (j);
value = wp_spa_json_parse_string (j);
g_assert_nonnull (value);
g_assert_cmpstr (value, ==, "blahblah");
}
{
g_autofree gchar *value = NULL;
value = wp_settings_get_string (s, "test-setting2");
g_assert_nonnull (value);
g_assert_cmpstr (value, ==, "true");
}
{
g_autofree gchar *value = NULL;
value = wp_settings_get_string (s, "test-setting3-int");
g_assert_nonnull (value);
g_assert_cmpstr (value, ==, "-20");
}
{
g_autofree gchar *value = NULL;
value = wp_settings_get_string (s, "test-setting5-string-with-quotes");
g_autoptr (WpSpaJson) j = NULL;
j = wp_settings_get (s, "test-setting5-string-with-quotes");
g_assert_nonnull (j);
value = wp_spa_json_parse_string (j);
g_assert_nonnull (value);
g_assert_cmpstr (value, ==, "a string with \"quotes\"");
}
@@ -323,19 +316,18 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
{
gfloat value = 0.0;
/* _get_float () */
g_assert_false (wp_settings_get_float (s, "test-setting-undefined",
&value));
g_assert_true (wp_settings_get_float (s, "test-setting-float1",
&value));
g_autoptr (WpSpaJson) j = wp_settings_get (s, "test-setting-float1");
g_assert_nonnull (j);
g_assert_true (wp_spa_json_parse_float (j, &value));
g_assert_cmpfloat_with_epsilon (value, 3.14, 0.001);
g_assert_true (wp_settings_get_float (s, "test-setting-float2",
&value));
g_autoptr (WpSpaJson) j2 = wp_settings_get (s, "test-setting-float2");
g_assert_nonnull (j2);
g_assert_true (wp_spa_json_parse_float (j2, &value));
g_assert_cmpfloat_with_epsilon (value, 0.4, 0.001);
}
/* test the wp_settings_get_instance () API */
{
g_autoptr (WpSettings) s1 =
@@ -593,19 +585,21 @@ void wp_settings_changed_callback (WpSettings *obj, const gchar *setting,
TestSettingsFixture *self = user_data;
g_assert_cmpstr (setting, ==, self->triggered_setting);
self->triggered_callback = true;
g_autoptr (WpSpaJson) j = wp_settings_get (self->s, setting);
g_assert_nonnull (j);
if (self->setting_type == BOOLEAN) {
gboolean value = false;
wp_settings_get_boolean (self->s, setting, &value);
gboolean value = FALSE;
g_assert_true (wp_spa_json_parse_boolean (j, &value));
g_assert_cmpint (value, ==, spa_atob (self->triggered_setting_value));
g_assert_cmpstr (raw_value, ==, self->triggered_setting_value);
} else if (self->setting_type == INTEGER) {
gint value = 0;
wp_settings_get_int (self->s, setting, &value);
g_assert_true (wp_spa_json_parse_int (j, &value));
g_assert_cmpint (value, ==, atoi (self->triggered_setting_value));
g_assert_cmpstr (raw_value, ==, self->triggered_setting_value);
} else if (self->setting_type == STRING) {
g_autofree gchar *value = wp_settings_get_string (self->s, setting);
g_autofree gchar *value = wp_spa_json_parse_string (j);
g_assert_nonnull (value);
g_assert_cmpstr (value, ==, self->triggered_setting_value);
g_assert_cmpstr (raw_value, ==, self->triggered_setting_value);

View File

@@ -4,51 +4,45 @@
Script.async_activation = true
-- test settings undefined
value = Settings.get ("test-setting-undefined", "test-settings")
assert (value == nil)
value = Settings.get ("test-setting1")
assert (value == nil)
-- test settings _get_boolean ()
local value = Settings.get_boolean ("test-setting1", "test-settings")
local value = Settings.get ("test-setting1", "test-settings"):parse()
assert (value == false)
value = Settings.get_boolean ("test-setting2", "test-settings")
value = Settings.get ("test-setting2", "test-settings"):parse()
assert ("boolean" == type (value))
assert (value == true)
value = Settings.get_boolean ("test-setting1")
assert (value == nil)
value = Settings.get_boolean ("test-setting-undefined",
"test-settings")
assert (value == nil)
-- test settings _get_int ()
value = Settings.get_int ("test-setting-undefined", "test-settings")
assert (value == nil)
value = Settings.get_int ("test-setting3-int", "test-settings")
value = Settings.get ("test-setting3-int", "test-settings"):parse()
assert ("number" == type (value))
assert (value == -20)
-- test settings _get_string ()
value = Settings.get_string ("test-setting-undefined", "test-settings")
assert (value == nil)
value = Settings.get_string ("test-setting4-string", "test-settings")
value = Settings.get ("test-setting4-string", "test-settings"):parse()
assert ("string" == type (value))
assert (value == "blahblah")
value = Settings.get_string ("test-setting3-int", "test-settings")
assert (value == "-20")
value = Settings.get ("test-setting5-string-with-quotes", "test-settings"):parse()
assert ("string" == type (value))
assert (value == "a string with \"quotes\"")
-- test settings _get_float ()
value = Settings.get_float ("test-setting-undefined", "test-settings")
assert (value == nil)
value = Settings.get_float ("test-setting-float1", "test-settings")
value = Settings.get ("test-setting-float1", "test-settings"):parse()
assert ("number" == type (value))
assert ((value - 3.14) < 0.00001)
value = Settings.get_float ("test-setting-float2", "test-settings")
value = Settings.get ("test-setting-float2", "test-settings"):parse()
assert ((value - 0.4) < 0.00001)
-- test settings _get ()
@@ -205,30 +199,29 @@ metadata_om:activate()
local setting
local setting_value
local callback
local setting_type
local finish_activation
function callback (obj, s, rawvalue)
if (setting_type == "boolean") then
assert (s == setting)
callback = true
assert (rawvalue == tostring(setting_value))
assert ((setting_value and true or false) ==
Settings.get_boolean (s, "test-settings"))
local json = Json.Raw (rawvalue)
assert (json ~= nil)
elseif (setting_type == "integer") then
if (json:is_boolean()) then
assert (s == setting)
callback = true
assert (rawvalue == tostring(setting_value))
assert (setting_value ==
Settings.get_int (s, "test-settings"))
assert (json:parse() == setting_value)
assert (setting_value == Settings.get (s, "test-settings"):parse())
elseif (setting_type == "string") then
elseif (json:is_int()) then
assert (s == setting)
callback = true
assert (rawvalue == setting_value)
assert (setting_value ==
Settings.get_string (s, "test-settings"))
assert (json:parse() == setting_value)
assert (setting_value == Settings.get (s, "test-settings"):parse())
elseif (json:is_string()) then
assert (s == setting)
callback = true
assert (json:parse() == setting_value)
assert (setting_value == Settings.get (s, "test-settings")):parse()
end
if (finish_activation) then
@@ -251,7 +244,6 @@ metadata_om:connect("objects-changed", function (om)
setting = "test-setting1"
setting_value = true
callback = false
setting_type = "boolean"
metadata:set(0, setting, "Spa:String:JSON", tostring(setting_value))
assert (callback)
@@ -260,7 +252,6 @@ metadata_om:connect("objects-changed", function (om)
setting = "test-setting1"
setting_value = true
callback = false
setting_type = "boolean"
metadata:set(0, setting, "Spa:String:JSON", tostring(setting_value))
assert (not callback)
@@ -269,7 +260,6 @@ metadata_om:connect("objects-changed", function (om)
setting = "test-setting3-int"
setting_value = 99
callback = false
setting_type = "integer"
metadata:set(0, setting, "Spa:String:JSON", setting_value)
assert (callback)
@@ -278,7 +268,6 @@ metadata_om:connect("objects-changed", function (om)
setting = "test-setting4-string"
setting_value = "lets not blabber"
callback = false
setting_type = "string"
finish_activation = true
metadata:set(0, setting, "Spa:String:JSON", setting_value)