daemon: add support for properties in the config file

Make a set-prop command to set a property from the config file
into a pw_properties. Pass this to the pw_core_new() and the
main-loop to tweak some stuff.

Move some warns to errors
This commit is contained in:
Wim Taymans 2019-06-20 15:19:28 +02:00
parent 85caf0b485
commit 03eeb945f3
11 changed files with 122 additions and 65 deletions

View file

@ -37,16 +37,17 @@
/** \cond */
static struct pw_command *parse_command_help(const char *line, char **err);
static struct pw_command *parse_command_add_spa_lib(const char *line, char **err);
static struct pw_command *parse_command_module_load(const char *line, char **err);
static struct pw_command *parse_command_exec(const char *line, char **err);
static struct pw_command *parse_command_help(struct pw_properties *properties, const char *line, char **err);
static struct pw_command *parse_command_set_prop(struct pw_properties *properties, const char *line, char **err);
static struct pw_command *parse_command_add_spa_lib(struct pw_properties *properties, const char *line, char **err);
static struct pw_command *parse_command_module_load(struct pw_properties *properties, const char *line, char **err);
static struct pw_command *parse_command_exec(struct pw_properties *properties, const char *line, char **err);
struct impl {
struct pw_command this;
};
typedef struct pw_command *(*pw_command_parse_func_t) (const char *line, char **err);
typedef struct pw_command *(*pw_command_parse_func_t) (struct pw_properties *properties, const char *line, char **err);
struct command_parse {
const char *name;
@ -56,6 +57,7 @@ struct command_parse {
static const struct command_parse parsers[] = {
{"help", "Show this help", parse_command_help},
{"set-prop", "Set a property", parse_command_set_prop},
{"add-spa-lib", "Add a library that provides a spa factory name regex", parse_command_add_spa_lib},
{"load-module", "Load a module", parse_command_module_load},
{"exec", "Execute a program", parse_command_exec},
@ -77,7 +79,7 @@ execute_command_help(struct pw_command *command, struct pw_core *core, char **er
return 0;
}
static struct pw_command *parse_command_help(const char *line, char **err)
static struct pw_command *parse_command_help(struct pw_properties *properties, const char *line, char **err)
{
struct impl *impl;
struct pw_command *this;
@ -97,6 +99,42 @@ no_mem:
return NULL;
}
static int
execute_command_set_prop(struct pw_command *command, struct pw_core *core, char **err)
{
return 0;
}
static struct pw_command *parse_command_set_prop(struct pw_properties *properties, const char *line, char **err)
{
struct impl *impl;
struct pw_command *this;
impl = calloc(1, sizeof(struct impl));
if (impl == NULL)
goto error_alloc;
this = &impl->this;
this->func = execute_command_set_prop;
this->args = pw_split_strv(line, whitespace, 4, &this->n_args);
if (this->n_args < 3)
goto error_arguments;
pw_log_debug("set property: '%s' = '%s'", this->args[1], this->args[2]);
pw_properties_set(properties, this->args[1], this->args[2]);
return this;
error_arguments:
asprintf(err, "%s requires <property-name> <value>", this->args[0]);
pw_free_strv(this->args);
return NULL;
error_alloc:
asprintf(err, "alloc failed: %m");
return NULL;
}
static int
execute_command_add_spa_lib(struct pw_command *command, struct pw_core *core, char **err)
{
@ -110,7 +148,7 @@ execute_command_add_spa_lib(struct pw_command *command, struct pw_core *core, ch
return 0;
}
static struct pw_command *parse_command_add_spa_lib(const char *line, char **err)
static struct pw_command *parse_command_add_spa_lib(struct pw_properties *properties, const char *line, char **err)
{
struct impl *impl;
struct pw_command *this;
@ -150,7 +188,7 @@ execute_command_module_load(struct pw_command *command, struct pw_core *core, ch
return 0;
}
static struct pw_command *parse_command_module_load(const char *line, char **err)
static struct pw_command *parse_command_module_load(struct pw_properties *properties, const char *line, char **err)
{
struct impl *impl;
struct pw_command *this;
@ -194,7 +232,7 @@ execute_command_exec(struct pw_command *command, struct pw_core *core, char **er
return 0;
}
static struct pw_command *parse_command_exec(const char *line, char **err)
static struct pw_command *parse_command_exec(struct pw_properties *properties, const char *line, char **err)
{
struct impl *impl;
struct pw_command *this;
@ -250,7 +288,7 @@ void pw_command_free(struct pw_command *command)
* \memberof pw_command
*/
SPA_EXPORT
struct pw_command *pw_command_parse(const char *line, char **err)
struct pw_command *pw_command_parse(struct pw_properties *properties, const char *line, char **err)
{
struct pw_command *command = NULL;
const struct command_parse *parse;
@ -263,7 +301,7 @@ struct pw_command *pw_command_parse(const char *line, char **err)
for (parse = parsers; parse->name != NULL; parse++) {
if (strcmp(name, parse->name) == 0) {
command = parse->func(line, err);
command = parse->func(properties, line, err);
goto out;
}
}

View file

@ -30,17 +30,26 @@
extern "C" {
#endif
#include <pipewire/core.h>
struct pw_command;
typedef int (*pw_command_func_t) (struct pw_command *command, struct pw_core *core, char **err);
/** \class pw_command
*
* A configuration command
*/
struct pw_command;
#include <pipewire/core.h>
struct pw_command {
struct spa_list link; /**< link in list of commands */
pw_command_func_t func;
char **args;
uint32_t id; /**< id of command */
int n_args;
};
struct pw_command *
pw_command_parse(const char *line, char **err);
pw_command_parse(struct pw_properties *properties, const char *line, char **err);
void
pw_command_free(struct pw_command *command);

View file

@ -32,9 +32,9 @@
#include <errno.h>
#include <pipewire/pipewire.h>
#include <pipewire/command.h>
#include <pipewire/private.h>
#include "daemon/command.h"
#include "daemon/daemon-config.h"
#define DEFAULT_CONFIG_FILE PIPEWIRE_CONFIG_DIR "/pipewire.conf"
@ -45,7 +45,6 @@ parse_line(struct pw_daemon_config *config,
{
struct pw_command *command = NULL;
char *p;
int ret = 0;
char *local_err = NULL;
/* search for comments */
@ -55,17 +54,20 @@ parse_line(struct pw_daemon_config *config,
/* remove whitespaces */
line = pw_strip(line, "\n\r \t");
if (*line == '\0') /* empty line */
return 0;
goto out;
if ((command = pw_command_parse(line, &local_err)) == NULL) {
asprintf(err, "%s:%u: %s", filename, lineno, local_err);
free(local_err);
ret = -EINVAL;
} else {
spa_list_append(&config->commands, &command->link);
}
if ((command = pw_command_parse(config->properties, line, &local_err)) == NULL)
goto error_parse;
return ret;
spa_list_append(&config->commands, &command->link);
out:
return 0;
error_parse:
asprintf(err, "%s:%u: %s", filename, lineno, local_err);
free(local_err);
return -EINVAL;
}
/**
@ -73,14 +75,21 @@ parse_line(struct pw_daemon_config *config,
*
* Returns a new empty #struct pw_daemon_config.
*/
struct pw_daemon_config *pw_daemon_config_new(void)
struct pw_daemon_config *pw_daemon_config_new(struct pw_properties *properties)
{
struct pw_daemon_config *config;
config = calloc(1, sizeof(struct pw_daemon_config));
if (config == NULL)
goto error_exit;
config->properties = properties;
spa_list_init(&config->commands);
return config;
error_exit:
return NULL;
}
/**

View file

@ -34,9 +34,10 @@ extern "C" {
struct pw_daemon_config {
struct spa_list commands;
struct pw_properties *properties;
};
struct pw_daemon_config * pw_daemon_config_new(void);
struct pw_daemon_config * pw_daemon_config_new(struct pw_properties *properties);
void pw_daemon_config_free(struct pw_daemon_config *config);

View file

@ -55,6 +55,7 @@ int main(int argc, char *argv[])
struct pw_core *core;
struct pw_main_loop *loop;
struct pw_daemon_config *config;
struct pw_properties *properties;
char *err = NULL;
static const struct option long_options[] = {
{"help", 0, NULL, 'h'},
@ -88,8 +89,12 @@ int main(int argc, char *argv[])
}
}
properties = pw_properties_new(
PW_KEY_CORE_NAME, daemon_name,
PW_KEY_CORE_DAEMON, "1", NULL);
/* parse configuration */
config = pw_daemon_config_new();
config = pw_daemon_config_new(properties);
if (pw_daemon_config_load(config, &err) < 0) {
pw_log_error("failed to parse config: %s", err);
free(err);
@ -97,15 +102,17 @@ int main(int argc, char *argv[])
}
loop = pw_main_loop_new(NULL);
loop = pw_main_loop_new(pw_properties_copy(properties));
if (loop == NULL) {
pw_log_error("failed to create main-loop: %m");
return -1;
}
pw_loop_add_signal(pw_main_loop_get_loop(loop), SIGINT, do_quit, loop);
pw_loop_add_signal(pw_main_loop_get_loop(loop), SIGTERM, do_quit, loop);
core = pw_core_new(pw_main_loop_get_loop(loop),
pw_properties_new(
PW_KEY_CORE_NAME, daemon_name,
PW_KEY_CORE_DAEMON, "1", NULL),
0);
properties, 0);
if (core == NULL) {
pw_log_error("failed to create core: %m");

View file

@ -1,5 +1,7 @@
#daemon config file for PipeWire version @VERSION@
#set-prop library.name.system support/libspa-support
#set-prop core.data-loop.library.name.system support/libspa-support
add-spa-lib api.alsa.* alsa/libspa-alsa
add-spa-lib api.v4l2.* v4l2/libspa-v4l2