tools: add getopt argument parsing

Add some help, version, remote options for tools
Add option for output filename in pw-profiler
Add option to start pw-cli as daemon or not, make it connect to the
default PipeWire instance by default (instead of local instance)
This commit is contained in:
Wim Taymans 2020-03-24 16:36:48 +01:00
parent a1846c9780
commit 646088b90c
4 changed files with 158 additions and 25 deletions

View file

@ -29,6 +29,7 @@
#include <string.h>
#include <ctype.h>
#include <alloca.h>
#include <getopt.h>
#include <spa/utils/result.h>
#include <spa/debug/pod.h>
@ -434,7 +435,7 @@ static bool do_connect(struct data *data, const char *cmd, char *args, char **er
struct pw_core *core;
struct remote_data *rd;
n = pw_split_ip(args, WHITESPACE, 1, a);
n = args ? pw_split_ip(args, WHITESPACE, 1, a) : 0;
if (n == 1) {
props = pw_properties_new(PW_KEY_REMOTE_NAME, a[0], NULL);
}
@ -2722,14 +2723,58 @@ static void do_quit(void *data, int signal_number)
pw_main_loop_quit(d->loop);
}
static void show_help(const char *name)
{
fprintf(stdout, "%s [options]\n"
" -h, --help Show this help\n"
" -v, --version Show version\n"
" -d, --daemon Start as daemon (Default false)\n"
" -r, --remote Remote daemon name\n",
name);
}
int main(int argc, char *argv[])
{
struct data data = { 0 };
struct pw_loop *l;
char *opt_remote = NULL;
char *error;
bool daemon = false;
static const struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{"daemon", 0, NULL, 'd'},
{"remote", 1, NULL, 'r'},
{NULL, 0, NULL, 0}
};
int c;
pw_init(&argc, &argv);
while ((c = getopt_long(argc, argv, "hvdr:", long_options, NULL)) != -1) {
switch (c) {
case 'h':
show_help(argv[0]);
return 0;
case 'v':
fprintf(stdout, "%s\n"
"Compiled with libpipewire %s\n"
"Linked with libpipewire %s\n",
argv[0],
pw_get_headers_version(),
pw_get_library_version());
return 0;
case 'd':
daemon = true;
break;
case 'r':
opt_remote = optarg;
break;
default:
return -1;
}
}
data.loop = pw_main_loop_new(NULL);
l = pw_main_loop_get_loop(data.loop);
pw_loop_add_signal(l, SIGINT, do_quit, &data);
@ -2738,7 +2783,11 @@ int main(int argc, char *argv[])
spa_list_init(&data.remotes);
pw_map_init(&data.vars, 64, 16);
data.context = pw_context_new(l, pw_properties_new(PW_KEY_CORE_DAEMON, "true", NULL), 0);
data.context = pw_context_new(l,
pw_properties_new(
PW_KEY_CORE_DAEMON, daemon ? "true" : NULL,
NULL),
0);
pw_context_load_module(data.context, "libpipewire-module-link-factory", NULL, NULL);
@ -2747,7 +2796,7 @@ int main(int argc, char *argv[])
fprintf(stdout, "Welcome to PipeWire version %s. Type 'help' for usage.\n",
pw_get_library_version());
do_connect(&data, "connect", "internal", &error);
do_connect(&data, "connect", opt_remote, &error);
pw_main_loop_run(data.loop);

View file

@ -761,8 +761,7 @@ int main(int argc, char *argv[])
{
struct data data = { 0 };
struct pw_loop *l;
struct pw_properties *props = NULL;
const char *remote_name = NULL;
const char *opt_remote = NULL;
const char *dot_path = DEFAULT_DOT_PATH;
static const struct option long_options[] = {
{"help", 0, NULL, 'h'},
@ -804,8 +803,8 @@ int main(int argc, char *argv[])
fprintf(stdout, "detail option enabled\n");
break;
case 'r' :
remote_name = optarg;
fprintf(stdout, "set remote to %s\n", remote_name);
opt_remote = optarg;
fprintf(stdout, "set remote to %s\n", opt_remote);
break;
case 'o' :
dot_path = optarg;
@ -828,10 +827,11 @@ int main(int argc, char *argv[])
if (data.context == NULL)
return -1;
if (remote_name)
props = pw_properties_new(PW_KEY_REMOTE_NAME, remote_name, NULL);
data.core = pw_context_connect(data.context, props, 0);
data.core = pw_context_connect(data.context,
pw_properties_new(
PW_KEY_REMOTE_NAME, opt_remote,
NULL),
0);
if (data.core == NULL)
return -1;

View file

@ -24,6 +24,7 @@
#include <stdio.h>
#include <signal.h>
#include <getopt.h>
#include <spa/utils/result.h>
#include <spa/debug/pod.h>
@ -678,14 +679,51 @@ static void do_quit(void *data, int signal_number)
pw_main_loop_quit(d->loop);
}
static void show_help(const char *name)
{
fprintf(stdout, "%s [options]\n"
" -h, --help Show this help\n"
" -v, --version Show version\n"
" -r, --remote Remote daemon name\n",
name);
}
int main(int argc, char *argv[])
{
struct data data = { 0 };
struct pw_loop *l;
struct pw_properties *props = NULL;
const char *opt_remote = NULL;
static const struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{"remote", 1, NULL, 'r'},
{NULL, 0, NULL, 0}
};
int c;
pw_init(&argc, &argv);
while ((c = getopt_long(argc, argv, "hvr:", long_options, NULL)) != -1) {
switch (c) {
case 'h':
show_help(argv[0]);
return 0;
case 'v':
fprintf(stdout, "%s\n"
"Compiled with libpipewire %s\n"
"Linked with libpipewire %s\n",
argv[0],
pw_get_headers_version(),
pw_get_library_version());
return 0;
case 'r':
opt_remote = optarg;
break;
default:
return -1;
}
}
data.loop = pw_main_loop_new(NULL);
if (data.loop == NULL)
return -1;
@ -698,12 +736,13 @@ int main(int argc, char *argv[])
if (data.context == NULL)
return -1;
if (argc > 1)
props = pw_properties_new(PW_KEY_REMOTE_NAME, argv[1], NULL);
spa_list_init(&data.pending_list);
data.core = pw_context_connect(data.context, props, 0);
data.core = pw_context_connect(data.context,
pw_properties_new(
PW_KEY_REMOTE_NAME, opt_remote,
NULL),
0);
if (data.core == NULL)
return -1;

View file

@ -24,6 +24,7 @@
#include <stdio.h>
#include <signal.h>
#include <getopt.h>
#include <spa/utils/result.h>
#include <spa/pod/parser.h>
@ -507,10 +508,10 @@ static void on_core_error(void *_data, uint32_t id, int seq, int res, const char
pw_log_error("error id:%u seq:%d res:%d (%s): %s",
id, seq, res, spa_strerror(res), message);
if (id == 0) {
if (id == PW_ID_CORE)
pw_main_loop_quit(data->loop);
}
}
static void on_core_done(void *_data, uint32_t id, int seq)
{
@ -537,14 +538,58 @@ static void do_quit(void *data, int signal_number)
pw_main_loop_quit(d->loop);
}
static void show_help(const char *name)
{
fprintf(stdout, "%s [options]\n"
" -h, --help Show this help\n"
" -v, --version Show version\n"
" -r, --remote Remote daemon name\n"
" -o, --output Profiler output name (default \"%s\")\n",
name,
DEFAULT_FILENAME);
}
int main(int argc, char *argv[])
{
struct data data = { 0 };
struct pw_loop *l;
struct pw_properties *props = NULL;
const char *opt_remote = NULL;
const char *opt_output = DEFAULT_FILENAME;
static const struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{"remote", 1, NULL, 'r'},
{"output", 1, NULL, 'o'},
{NULL, 0, NULL, 0}
};
int c;
pw_init(&argc, &argv);
while ((c = getopt_long(argc, argv, "hvr:o:", long_options, NULL)) != -1) {
switch (c) {
case 'h':
show_help(argv[0]);
return 0;
case 'v':
fprintf(stdout, "%s\n"
"Compiled with libpipewire %s\n"
"Linked with libpipewire %s\n",
argv[0],
pw_get_headers_version(),
pw_get_library_version());
return 0;
case 'o':
opt_output = optarg;
break;
case 'r':
opt_remote = optarg;
break;
default:
return -1;
}
}
data.loop = pw_main_loop_new(NULL);
if (data.loop == NULL) {
fprintf(stderr, "Can't create data loop: %m");
@ -563,16 +608,17 @@ int main(int argc, char *argv[])
pw_context_load_module(data.context, PW_EXTENSION_MODULE_PROFILER, NULL, NULL);
if (argc > 1)
props = pw_properties_new(PW_KEY_REMOTE_NAME, argv[1], NULL);
data.core = pw_context_connect(data.context, props, 0);
data.core = pw_context_connect(data.context,
pw_properties_new(
PW_KEY_REMOTE_NAME, opt_remote,
NULL),
0);
if (data.core == NULL) {
fprintf(stderr, "Can't connect: %m");
return -1;
}
data.filename = DEFAULT_FILENAME;
data.filename = opt_output;
data.output = fopen(data.filename, "w");
if (data.output == NULL) {
@ -591,7 +637,6 @@ int main(int argc, char *argv[])
&data.registry_listener,
&registry_events, &data);
data.check_profiler = pw_core_sync(data.core, 0, 0);
pw_main_loop_run(data.loop);