improve config parsing

Use get_string to get object keys in a more generic way.
This commit is contained in:
Wim Taymans 2021-01-27 15:35:01 +01:00
parent fc68e901e5
commit 0be851739f
5 changed files with 35 additions and 40 deletions

View file

@ -194,28 +194,27 @@ static const struct pw_metadata_events metadata_events = {
static void session_create(void *data, struct sm_object *object) static void session_create(void *data, struct sm_object *object)
{ {
struct impl *impl = data; struct impl *impl = data;
const struct spa_dict_item *it; const struct spa_dict_item *item;
if (strcmp(object->type, PW_TYPE_INTERFACE_Node) != 0) if (strcmp(object->type, PW_TYPE_INTERFACE_Node) != 0)
return; return;
spa_dict_for_each(it, &impl->properties->dict) { spa_dict_for_each(item, &impl->properties->dict) {
struct spa_json json[2]; struct spa_json it[2];
int len;
const char *value; const char *value;
char name [1024] = "\0"; char name [1024] = "\0", key[128];
struct find_data d; struct find_data d;
spa_json_init(&json[0], it->value, strlen(it->value)); spa_json_init(&it[0], item->value, strlen(item->value));
if (spa_json_enter_object(&json[0], &json[1]) <= 0) if (spa_json_enter_object(&it[0], &it[1]) <= 0)
continue; continue;
while ((len = spa_json_next(&json[1], &value)) > 0) { while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
if (strncmp(value, "\"name\"", len) == 0) { if (strcmp(key, "name") == 0) {
if (spa_json_get_string(&json[1], name, sizeof(name)) <= 0) if (spa_json_get_string(&it[1], name, sizeof(name)) <= 0)
continue; continue;
} else { } else {
if (spa_json_next(&json[1], &value) <= 0) if (spa_json_next(&it[1], &value) <= 0)
break; break;
} }
} }
@ -224,9 +223,9 @@ static void session_create(void *data, struct sm_object *object)
char val[16]; char val[16];
snprintf(val, sizeof(val)-1, "%u", d.id); snprintf(val, sizeof(val)-1, "%u", d.id);
pw_log_info("found %s with id:%s restore as %s", pw_log_info("found %s with id:%s restore as %s",
name, val, it->key); name, val, item->key);
pw_metadata_set_property(impl->session->metadata, pw_metadata_set_property(impl->session->metadata,
PW_ID_CORE, it->key, SPA_TYPE_INFO_BASE"Id", val); PW_ID_CORE, item->key, SPA_TYPE_INFO_BASE"Id", val);
} }
} }
} }

View file

@ -208,8 +208,7 @@ static int find_saved_profile(struct device *dev, struct profile *pr)
struct spa_json it[2]; struct spa_json it[2];
struct impl *impl = dev->impl; struct impl *impl = dev->impl;
const char *json, *value; const char *json, *value;
int len; char name[1024] = "\0", key[128];
char name[1024] = "\0";
struct sm_param *p; struct sm_param *p;
json = pw_properties_get(impl->properties, dev->key); json = pw_properties_get(impl->properties, dev->key);
@ -220,8 +219,8 @@ static int find_saved_profile(struct device *dev, struct profile *pr)
if (spa_json_enter_object(&it[0], &it[1]) <= 0) if (spa_json_enter_object(&it[0], &it[1]) <= 0)
return -EINVAL; return -EINVAL;
while ((len = spa_json_next(&it[1], &value)) > 0) { while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
if (strncmp(value, "\"name\"", len) == 0) { if (strcmp(key, "name") == 0) {
if (spa_json_get_string(&it[1], name, sizeof(name)) <= 0) if (spa_json_get_string(&it[1], name, sizeof(name)) <= 0)
continue; continue;
} else { } else {

View file

@ -204,9 +204,8 @@ static char *serialize_props(struct device *dev, const struct spa_pod *param)
static int restore_route(struct device *dev, const char *val, uint32_t index, uint32_t device_id) static int restore_route(struct device *dev, const char *val, uint32_t index, uint32_t device_id)
{ {
struct spa_json it[3]; struct spa_json it[3];
char buf[1024]; char buf[1024], key[128];
const char *value; const char *value;
int len;
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf)); struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf));
struct spa_pod_frame f[2]; struct spa_pod_frame f[2];
struct spa_pod *param; struct spa_pod *param;
@ -226,22 +225,22 @@ static int restore_route(struct device *dev, const char *val, uint32_t index, ui
spa_pod_builder_push_object(&b, &f[1], spa_pod_builder_push_object(&b, &f[1],
SPA_TYPE_OBJECT_Props, SPA_PARAM_Route); SPA_TYPE_OBJECT_Props, SPA_PARAM_Route);
while ((len = spa_json_next(&it[1], &value)) > 0) { while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
if (strncmp(value, "\"volume\"", len) == 0) { if (strcmp(key, "volume") == 0) {
float vol; float vol;
if (spa_json_get_float(&it[1], &vol) <= 0) if (spa_json_get_float(&it[1], &vol) <= 0)
continue; continue;
spa_pod_builder_prop(&b, SPA_PROP_volume, 0); spa_pod_builder_prop(&b, SPA_PROP_volume, 0);
spa_pod_builder_float(&b, vol); spa_pod_builder_float(&b, vol);
} }
else if (strncmp(value, "\"mute\"", len) == 0) { else if (strcmp(key, "mute") == 0) {
bool mute; bool mute;
if (spa_json_get_bool(&it[1], &mute) <= 0) if (spa_json_get_bool(&it[1], &mute) <= 0)
continue; continue;
spa_pod_builder_prop(&b, SPA_PROP_mute, 0); spa_pod_builder_prop(&b, SPA_PROP_mute, 0);
spa_pod_builder_bool(&b, mute); spa_pod_builder_bool(&b, mute);
} }
else if (strncmp(value, "\"volumes\"", len) == 0) { else if (strcmp(key, "volumes") == 0) {
uint32_t n_vols; uint32_t n_vols;
float vols[SPA_AUDIO_MAX_CHANNELS]; float vols[SPA_AUDIO_MAX_CHANNELS];
@ -259,7 +258,7 @@ static int restore_route(struct device *dev, const char *val, uint32_t index, ui
spa_pod_builder_array(&b, sizeof(float), SPA_TYPE_Float, spa_pod_builder_array(&b, sizeof(float), SPA_TYPE_Float,
n_vols, vols); n_vols, vols);
} }
else if (strncmp(value, "\"channels\"", len) == 0) { else if (strcmp(key, "channels") == 0) {
uint32_t n_ch; uint32_t n_ch;
uint32_t map[SPA_AUDIO_MAX_CHANNELS]; uint32_t map[SPA_AUDIO_MAX_CHANNELS];

View file

@ -276,8 +276,7 @@ static int restore_stream(struct stream *str, const char *val)
{ {
struct spa_json it[3]; struct spa_json it[3];
const char *value; const char *value;
int len; char buf[1024], key[128];
char buf[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf)); struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf));
struct spa_pod_frame f[2]; struct spa_pod_frame f[2];
struct spa_pod *param; struct spa_pod *param;
@ -290,22 +289,22 @@ static int restore_stream(struct stream *str, const char *val)
spa_pod_builder_push_object(&b, &f[0], spa_pod_builder_push_object(&b, &f[0],
SPA_TYPE_OBJECT_Props, SPA_PARAM_Props); SPA_TYPE_OBJECT_Props, SPA_PARAM_Props);
while ((len = spa_json_next(&it[1], &value)) > 0) { while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
if (strncmp(value, "\"volume\"", len) == 0) { if (strcmp(key, "volume") == 0) {
float vol; float vol;
if (spa_json_get_float(&it[1], &vol) <= 0) if (spa_json_get_float(&it[1], &vol) <= 0)
continue; continue;
spa_pod_builder_prop(&b, SPA_PROP_volume, 0); spa_pod_builder_prop(&b, SPA_PROP_volume, 0);
spa_pod_builder_float(&b, vol); spa_pod_builder_float(&b, vol);
} }
else if (strncmp(value, "\"mute\"", len) == 0) { else if (strcmp(key, "mute") == 0) {
bool mute; bool mute;
if (spa_json_get_bool(&it[1], &mute) <= 0) if (spa_json_get_bool(&it[1], &mute) <= 0)
continue; continue;
spa_pod_builder_prop(&b, SPA_PROP_mute, 0); spa_pod_builder_prop(&b, SPA_PROP_mute, 0);
spa_pod_builder_bool(&b, mute); spa_pod_builder_bool(&b, mute);
} }
else if (strncmp(value, "\"volumes\"", len) == 0) { else if (strcmp(key, "volumes") == 0) {
uint32_t n_vols; uint32_t n_vols;
float vols[SPA_AUDIO_MAX_CHANNELS]; float vols[SPA_AUDIO_MAX_CHANNELS];
@ -323,7 +322,7 @@ static int restore_stream(struct stream *str, const char *val)
spa_pod_builder_array(&b, sizeof(float), SPA_TYPE_Float, spa_pod_builder_array(&b, sizeof(float), SPA_TYPE_Float,
n_vols, vols); n_vols, vols);
} }
else if (strncmp(value, "\"channels\"", len) == 0) { else if (strcmp(key, "channels") == 0) {
uint32_t n_ch; uint32_t n_ch;
uint32_t map[SPA_AUDIO_MAX_CHANNELS]; uint32_t map[SPA_AUDIO_MAX_CHANNELS];
@ -343,7 +342,7 @@ static int restore_stream(struct stream *str, const char *val)
spa_pod_builder_array(&b, sizeof(uint32_t), SPA_TYPE_Id, spa_pod_builder_array(&b, sizeof(uint32_t), SPA_TYPE_Id,
n_ch, map); n_ch, map);
} }
else if (strncmp(value, "\"target-node\"", len) == 0) { else if (strcmp(key, "target-node") == 0) {
char name[1024]; char name[1024];
if (spa_json_get_string(&it[1], name, sizeof(name)) <= 0) if (spa_json_get_string(&it[1], name, sizeof(name)) <= 0)

View file

@ -122,13 +122,12 @@ static int do_extension_stream_restore_read(struct client *client, uint32_t comm
spa_dict_for_each(item, &client->routes->dict) { spa_dict_for_each(item, &client->routes->dict) {
struct spa_json it[3]; struct spa_json it[3];
const char *value; const char *value;
char name[1024]; char name[1024], key[128];
char device_name[1024] = "\0"; char device_name[1024] = "\0";
bool mute = false; bool mute = false;
struct volume vol = VOLUME_INIT; struct volume vol = VOLUME_INIT;
struct channel_map map = CHANNEL_MAP_INIT; struct channel_map map = CHANNEL_MAP_INIT;
float volume = 0.0f; float volume = 0.0f;
int len;
if (key_to_name(item->key, name, sizeof(name)) < 0) if (key_to_name(item->key, name, sizeof(name)) < 0)
continue; continue;
@ -139,16 +138,16 @@ static int do_extension_stream_restore_read(struct client *client, uint32_t comm
if (spa_json_enter_object(&it[0], &it[1]) <= 0) if (spa_json_enter_object(&it[0], &it[1]) <= 0)
continue; continue;
while ((len = spa_json_next(&it[1], &value)) > 0) { while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
if (strncmp(value, "\"volume\"", len) == 0) { if (strcmp(key, "volume") == 0) {
if (spa_json_get_float(&it[1], &volume) <= 0) if (spa_json_get_float(&it[1], &volume) <= 0)
continue; continue;
} }
else if (strncmp(value, "\"mute\"", len) == 0) { else if (strcmp(key, "mute") == 0) {
if (spa_json_get_bool(&it[1], &mute) <= 0) if (spa_json_get_bool(&it[1], &mute) <= 0)
continue; continue;
} }
else if (strncmp(value, "\"volumes\"", len) == 0) { else if (strcmp(key, "volumes") == 0) {
vol = VOLUME_INIT; vol = VOLUME_INIT;
if (spa_json_enter_array(&it[1], &it[2]) <= 0) if (spa_json_enter_array(&it[1], &it[2]) <= 0)
continue; continue;
@ -158,7 +157,7 @@ static int do_extension_stream_restore_read(struct client *client, uint32_t comm
break; break;
} }
} }
else if (strncmp(value, "\"channels\"", len) == 0) { else if (strcmp(key, "channels") == 0) {
if (spa_json_enter_array(&it[1], &it[2]) <= 0) if (spa_json_enter_array(&it[1], &it[2]) <= 0)
continue; continue;
@ -169,7 +168,7 @@ static int do_extension_stream_restore_read(struct client *client, uint32_t comm
map.map[map.channels] = channel_name2id(chname); map.map[map.channels] = channel_name2id(chname);
} }
} }
else if (strncmp(value, "\"target-node\"", len) == 0) { else if (strcmp(key, "target-node") == 0) {
if (spa_json_get_string(&it[1], device_name, sizeof(device_name)) <= 0) if (spa_json_get_string(&it[1], device_name, sizeof(device_name)) <= 0)
continue; continue;
} }