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

@ -24,6 +24,7 @@
#include <stdio.h>
#include <signal.h>
#include <getopt.h>
#include <spa/utils/result.h>
#include <spa/pod/parser.h>
@ -507,9 +508,9 @@ 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);