diff options
author | David Robillard <d@drobilla.net> | 2024-09-29 19:35:32 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2024-10-12 14:07:10 -0400 |
commit | dc6747841ece7250ccdd964b19ac30f8c66889be (patch) | |
tree | 4306c61829dab11a40ed120feeef89da7fdd6a9e /src | |
parent | 6451316012d42ad5efad6fe41ec555d772f5c25d (diff) | |
download | jalv-dc6747841ece7250ccdd964b19ac30f8c66889be.tar.gz jalv-dc6747841ece7250ccdd964b19ac30f8c66889be.tar.bz2 jalv-dc6747841ece7250ccdd964b19ac30f8c66889be.zip |
Factor frontend command-line arguments into a struct
Diffstat (limited to 'src')
-rw-r--r-- | src/frontend.h | 10 | ||||
-rw-r--r-- | src/jalv.c | 17 | ||||
-rw-r--r-- | src/jalv_console.c | 61 | ||||
-rw-r--r-- | src/jalv_gtk.c | 8 | ||||
-rw-r--r-- | src/jalv_qt.cpp | 7 |
5 files changed, 60 insertions, 43 deletions
diff --git a/src/frontend.h b/src/frontend.h index 774796d..d0c4a76 100644 --- a/src/frontend.h +++ b/src/frontend.h @@ -16,9 +16,15 @@ JALV_BEGIN_DECLS // Interface that must be implemented by UIs -/// Read command-line arguments and set `opts` accordingly +/// Command-line arguments passed to an executable +typedef struct { + int* argc; ///< Pointer to `argc` like in `main` + char*** argv; ///< Pointer to `argv` like in `main` +} JalvFrontendArgs; + +/// Consume command-line arguments and set `opts` accordingly int -jalv_frontend_init(int* argc, char*** argv, JalvOptions* opts); +jalv_frontend_init(JalvFrontendArgs* args, JalvOptions* opts); /// Return the URI of the "native" LV2 UI type const char* @@ -1121,8 +1121,9 @@ jalv_open(Jalv* const jalv, int* argc, char*** argv) #endif // Parse command-line arguments - int ret = 0; - if ((ret = jalv_frontend_init(argc, argv, &jalv->opts))) { + JalvFrontendArgs args = {argc, argv}; + const int ret = jalv_frontend_init(&args, &jalv->opts); + if (ret) { jalv_close(jalv); return ret; } @@ -1187,12 +1188,14 @@ jalv_open(Jalv* const jalv, int* argc, char*** argv) return -2; } plugin_uri = lilv_node_duplicate(lilv_state_get_plugin_uri(state)); - } else if (*argc > 1) { - plugin_uri = lilv_new_uri(world, (*argv)[*argc - 1]); - } - - if (!plugin_uri) { + } else if (*args.argc == 0) { plugin_uri = jalv_frontend_select_plugin(jalv); + } else if (*args.argc == 1) { + plugin_uri = lilv_new_uri(world, (*args.argv)[0]); + } else { + jalv_log(JALV_LOG_ERR, "Unexpected trailing arguments\n"); + jalv_close(jalv); + return -1; } if (!plugin_uri) { diff --git a/src/jalv_console.c b/src/jalv_console.c index 5abad56..750549a 100644 --- a/src/jalv_console.c +++ b/src/jalv_console.c @@ -95,42 +95,45 @@ jalv_ui_port_event(Jalv* jalv, } int -jalv_frontend_init(int* argc, char*** argv, JalvOptions* opts) +jalv_frontend_init(JalvFrontendArgs* const args, JalvOptions* const opts) { - const char* const cmd = (*argv)[0]; + const int argc = *args->argc; + char** argv = *args->argv; + + const char* const cmd = argv[0]; int n_controls = 0; int a = 1; - for (; a < *argc && (*argv)[a][0] == '-'; ++a) { - if ((*argv)[a][1] == 'h' || !strcmp((*argv)[a], "--help")) { + for (; a < argc && argv[a][0] == '-'; ++a) { + if (argv[a][1] == 'h' || !strcmp(argv[a], "--help")) { return print_usage(cmd, true); } - if ((*argv)[a][1] == 'V' || !strcmp((*argv)[a], "--version")) { + if (argv[a][1] == 'V' || !strcmp(argv[a], "--version")) { return print_version(); } - if ((*argv)[a][1] == 's') { + if (argv[a][1] == 's') { opts->show_ui = true; - } else if ((*argv)[a][1] == 'p') { + } else if (argv[a][1] == 'p') { opts->print_controls = true; - } else if ((*argv)[a][1] == 'U') { - if (++a == *argc) { + } else if (argv[a][1] == 'U') { + if (++a == argc) { return print_arg_error(cmd, "option requires an argument -- 'U'"); } - opts->ui_uri = jalv_strdup((*argv)[a]); - } else if ((*argv)[a][1] == 'l') { - if (++a == *argc) { + opts->ui_uri = jalv_strdup(argv[a]); + } else if (argv[a][1] == 'l') { + if (++a == argc) { return print_arg_error(cmd, "option requires an argument -- 'l'"); } - opts->load = jalv_strdup((*argv)[a]); - } else if ((*argv)[a][1] == 'b') { - if (++a == *argc) { + opts->load = jalv_strdup(argv[a]); + } else if (argv[a][1] == 'b') { + if (++a == argc) { return print_arg_error(cmd, "option requires an argument -- 'b'"); } - opts->buffer_size = atoi((*argv)[a]); - } else if ((*argv)[a][1] == 'c') { - if (++a == *argc) { + opts->buffer_size = atoi(argv[a]); + } else if (argv[a][1] == 'c') { + if (++a == argc) { return print_arg_error(cmd, "option requires an argument -- 'c'"); } @@ -142,28 +145,30 @@ jalv_frontend_init(int* argc, char*** argv, JalvOptions* opts) } opts->controls = new_controls; - opts->controls[n_controls++] = (*argv)[a]; + opts->controls[n_controls++] = argv[a]; opts->controls[n_controls] = NULL; - } else if ((*argv)[a][1] == 'i') { + } else if (argv[a][1] == 'i') { opts->non_interactive = true; - } else if ((*argv)[a][1] == 'd') { + } else if (argv[a][1] == 'd') { opts->dump = true; - } else if ((*argv)[a][1] == 't') { + } else if (argv[a][1] == 't') { opts->trace = true; - } else if ((*argv)[a][1] == 'n') { - if (++a == *argc) { + } else if (argv[a][1] == 'n') { + if (++a == argc) { return print_arg_error(cmd, "option requires an argument -- 'n'"); } free(opts->name); - opts->name = jalv_strdup((*argv)[a]); - } else if ((*argv)[a][1] == 'x') { + opts->name = jalv_strdup(argv[a]); + } else if (argv[a][1] == 'x') { opts->name_exact = 1; } else { - fprintf(stderr, "%s: unknown option -- '%c'\n", cmd, (*argv)[a][1]); - return print_usage((*argv)[0], true); + fprintf(stderr, "%s: unknown option -- '%c'\n", cmd, argv[a][1]); + return print_usage(argv[0], true); } } + *args->argc = *args->argc - a; + *args->argv = *args->argv + a; return 0; } diff --git a/src/jalv_gtk.c b/src/jalv_gtk.c index feab6c5..80067bd 100644 --- a/src/jalv_gtk.c +++ b/src/jalv_gtk.c @@ -69,7 +69,7 @@ on_window_destroy(GtkWidget* ZIX_UNUSED(widget), gpointer ZIX_UNUSED(data)) } int -jalv_frontend_init(int* argc, char*** argv, JalvOptions* opts) +jalv_frontend_init(JalvFrontendArgs* const args, JalvOptions* const opts) { const GOptionEntry entries[] = { {"preset", @@ -181,8 +181,8 @@ jalv_frontend_init(int* argc, char*** argv, JalvOptions* opts) GError* error = NULL; const int err = - gtk_init_with_args(argc, - argv, + gtk_init_with_args(args->argc, + args->argv, "PLUGIN_URI - Run an LV2 plugin as a Jack application", entries, NULL, @@ -192,6 +192,8 @@ jalv_frontend_init(int* argc, char*** argv, JalvOptions* opts) fprintf(stderr, "%s\n", error->message); } + --*args->argc; + ++*args->argv; return !err; } diff --git a/src/jalv_qt.cpp b/src/jalv_qt.cpp index acfbcdb..e849636 100644 --- a/src/jalv_qt.cpp +++ b/src/jalv_qt.cpp @@ -277,11 +277,12 @@ FlowLayout::smartSpacing(QStyle::PixelMetric pm) const extern "C" { int -jalv_frontend_init(int* argc, char*** argv, JalvOptions*) +jalv_frontend_init(JalvFrontendArgs* const args, JalvOptions*) { - app = new QApplication(*argc, *argv, true); + app = new QApplication(*args->argc, *args->argv, true); app->setStyleSheet("QGroupBox::title { subcontrol-position: top center }"); - + --*args->argc; + ++*args->argv; return 0; } |