1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/* PipeWire */
/* SPDX-FileCopyrightText: Copyright © 2021 Wim Taymans */
/* SPDX-License-Identifier: MIT */
/*
This is a partial copy of functions from libpipewire's conf.c that is meant to
live here temporarily until pw_context_parse_conf_section() is fixed upstream.
See https://gitlab.freedesktop.org/pipewire/pipewire/-/merge_requests/1925
*/
#include <string.h>
#include <spa/utils/result.h>
#include <spa/utils/string.h>
#include <spa/utils/json.h>
#include <spa/utils/cleanup.h>
#include <pipewire/impl.h>
struct data {
struct pw_context *context;
struct pw_properties *props;
int count;
};
/* context.spa-libs = {
* <factory-name regex> = <library-name>
* }
*/
static int parse_spa_libs(void *user_data, const char *location,
const char *section, const char *str, size_t len)
{
struct data *d = user_data;
struct pw_context *context = d->context;
struct spa_json it[2];
char key[512], value[512];
spa_json_init(&it[0], str, len);
if (spa_json_enter_object(&it[0], &it[1]) < 0) {
pw_log_error("config file error: context.spa-libs is not an object");
return -EINVAL;
}
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
if (spa_json_get_string(&it[1], value, sizeof(value)) > 0) {
pw_context_add_spa_lib(context, key, value);
d->count++;
}
}
return 0;
}
static int load_module(struct pw_context *context, const char *key, const char *args, const char *flags)
{
if (pw_context_load_module(context, key, args, NULL) == NULL) {
if (errno == ENOENT && flags && strstr(flags, "ifexists") != NULL) {
pw_log_info("%p: skipping unavailable module %s",
context, key);
} else if (flags == NULL || strstr(flags, "nofail") == NULL) {
pw_log_error("%p: could not load mandatory module \"%s\": %m",
context, key);
return -errno;
} else {
pw_log_info("%p: could not load optional module \"%s\": %m",
context, key);
}
} else {
pw_log_info("%p: loaded module %s", context, key);
}
return 0;
}
/*
* context.modules = [
* { name = <module-name>
* ( args = { <key> = <value> ... } )
* ( flags = [ ( ifexists ) ( nofail ) ]
* ( condition = [ { key = value, .. } .. ] )
* }
* ]
*/
static int parse_modules(void *user_data, const char *location,
const char *section, const char *str, size_t len)
{
struct data *d = user_data;
struct pw_context *context = d->context;
struct spa_json it[4];
char key[512];
int res = 0;
spa_autofree char *s = strndup(str, len);
spa_json_init(&it[0], s, len);
if (spa_json_enter_array(&it[0], &it[1]) < 0) {
pw_log_error("config file error: context.modules is not an array");
return -EINVAL;
}
while (spa_json_enter_object(&it[1], &it[2]) > 0) {
char *name = NULL, *args = NULL, *flags = NULL;
bool have_match = true;
while (spa_json_get_string(&it[2], key, sizeof(key)) > 0) {
const char *val;
int len;
if ((len = spa_json_next(&it[2], &val)) <= 0)
break;
if (spa_streq(key, "name")) {
name = (char*)val;
spa_json_parse_stringn(val, len, name, len+1);
} else if (spa_streq(key, "args")) {
if (spa_json_is_container(val, len))
len = spa_json_container_len(&it[2], val, len);
args = (char*)val;
spa_json_parse_stringn(val, len, args, len+1);
} else if (spa_streq(key, "flags")) {
if (spa_json_is_container(val, len))
len = spa_json_container_len(&it[2], val, len);
flags = (char*)val;
spa_json_parse_stringn(val, len, flags, len+1);
}
}
if (!have_match)
continue;
if (name != NULL)
res = load_module(context, name, args, flags);
if (res < 0)
break;
d->count++;
}
return res;
}
static int _pw_context_parse_conf_section(struct pw_context *context,
struct pw_properties *conf, const char *section)
{
struct data data = { .context = context };
int res;
if (spa_streq(section, "context.spa-libs"))
res = pw_conf_section_for_each(&conf->dict, section,
parse_spa_libs, &data);
else if (spa_streq(section, "context.modules"))
res = pw_conf_section_for_each(&conf->dict, section,
parse_modules, &data);
else
res = -EINVAL;
return res == 0 ? data.count : res;
}
|