From dcd2d2d5f91d922ca8b4efeff9abfdafee89d2bd Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Tue, 30 Aug 2022 16:41:54 +0200 Subject: [PATCH 1/3] Add a follow mode Repeat the commands requested after sleeping the request amount of seconds, or 2 seconds by default. This can be useful to monitor a battery status. --- src/main.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index ab7841ed..372023e5 100644 --- a/src/main.c +++ b/src/main.c @@ -278,6 +278,8 @@ int main(int argc, char* argv[]) int print_capabilities = -1; int equalizer_preset = -1; int dev_mode = 0; + int follow = 0; + unsigned follow_sec = 2; struct option opts[] = { { "battery", no_argument, NULL, 'b' }, @@ -288,6 +290,7 @@ int main(int argc, char* argv[]) { "equalizer-preset", required_argument, NULL, 'p' }, { "inactive-time", required_argument, NULL, 'i' }, { "light", required_argument, NULL, 'l' }, + { "follow", optional_argument, NULL, 'f' }, { "notificate", required_argument, NULL, 'n' }, { "rotate-to-mute", required_argument, NULL, 'r' }, { "sidetone", required_argument, NULL, 's' }, @@ -302,7 +305,7 @@ int main(int argc, char* argv[]) // Init all information of supported devices init_devices(); - while ((c = getopt_long(argc, argv, "bchi:l:mn:r:s:uv:p:?", opts, &option_index)) != -1) { + while ((c = getopt_long(argc, argv, "bchi:l:f::n:r:s:uv:p:?", opts, &option_index)) != -1) { char* endptr = NULL; // for strtol switch (c) { @@ -312,6 +315,16 @@ int main(int argc, char* argv[]) case 'c': short_output = 1; break; + case 'f': + follow = 1; + if (optarg) { + follow_sec = strtol(optarg, &endptr, 10); + if (follow_sec == 0) { + printf("Usage: %s -f[secs timeout]\n", argv[0]); + return 1; + } + } + break; case 'i': inactive_time = strtol(optarg, &endptr, 10); @@ -388,6 +401,7 @@ int main(int argc, char* argv[]) printf(" -v, --voice-prompt 0|1\tTurn voice prompts on or off (0 = off, 1 = on)\n"); printf(" -r, --rotate-to-mute 0|1\tTurn rotate to mute feature on or off (0 = off, 1 = on)\n"); printf(" -p, --equalizer-preset number\tSets equalizer preset, number must be between 0 and 3, 0 sets the default\n"); + printf(" -f, --follow=[secs timeout]\tRe-run the commands after the specified seconds timeout or 2 by default\n"); printf("\n"); printf(" --timeout 0-100000\t\tSpecifies the timeout in ms for reading data from device (default 5000)\n"); printf(" -?, --capabilities\t\tPrint every feature headsetcontrol supports of the connected headset\n"); @@ -441,6 +455,7 @@ int main(int argc, char* argv[]) // Set all features the user wants us to set int error = 0; +loop_start: if (sidetone_loudness != -1) { if ((error = handle_feature(&device_found, &device_handle, &hid_path, CAP_SIDETONE, sidetone_loudness)) != 0) goto error; @@ -506,6 +521,11 @@ int main(int argc, char* argv[]) printf("You didn't set any arguments, so nothing happened.\nType %s -h for help.\n", argv[0]); } + if (follow) { + sleep(follow_sec); + goto loop_start; + } + terminate_hid(&device_handle, &hid_path); return 0; From 47092b954675eaafbb6a06b572429d85d21aa717 Mon Sep 17 00:00:00 2001 From: Denis Arnst Date: Sat, 5 Nov 2022 13:13:13 +0100 Subject: [PATCH 2/3] Make optional argument intuitive --- src/main.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 372023e5..5d7ccff4 100644 --- a/src/main.c +++ b/src/main.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -263,6 +264,13 @@ static int handle_feature(struct device* device_found, hid_device** device_handl return 0; } +// Makes parsing of optiona arguments easier +// Credits to https://cfengine.com/blog/2021/optional-arguments-with-getopt-long/ +#define OPTIONAL_ARGUMENT_IS_PRESENT \ + ((optarg == NULL && optind < argc && argv[optind][0] != '-') \ + ? (bool) (optarg = argv[optind++]) \ + : (optarg != NULL)) + int main(int argc, char* argv[]) { int c; @@ -317,10 +325,10 @@ int main(int argc, char* argv[]) break; case 'f': follow = 1; - if (optarg) { + if (OPTIONAL_ARGUMENT_IS_PRESENT) { follow_sec = strtol(optarg, &endptr, 10); if (follow_sec == 0) { - printf("Usage: %s -f[secs timeout]\n", argv[0]); + printf("Usage: %s -f [secs timeout]\n", argv[0]); return 1; } } @@ -401,7 +409,7 @@ int main(int argc, char* argv[]) printf(" -v, --voice-prompt 0|1\tTurn voice prompts on or off (0 = off, 1 = on)\n"); printf(" -r, --rotate-to-mute 0|1\tTurn rotate to mute feature on or off (0 = off, 1 = on)\n"); printf(" -p, --equalizer-preset number\tSets equalizer preset, number must be between 0 and 3, 0 sets the default\n"); - printf(" -f, --follow=[secs timeout]\tRe-run the commands after the specified seconds timeout or 2 by default\n"); + printf(" -f, --follow [secs timeout]\tRe-run the commands after the specified seconds timeout or 2 by default\n"); printf("\n"); printf(" --timeout 0-100000\t\tSpecifies the timeout in ms for reading data from device (default 5000)\n"); printf(" -?, --capabilities\t\tPrint every feature headsetcontrol supports of the connected headset\n"); From 62dd849ea0ce95d725517e65a0ee401e62a6558a Mon Sep 17 00:00:00 2001 From: Denis Arnst Date: Sat, 5 Nov 2022 13:16:44 +0100 Subject: [PATCH 3/3] Rerun format --- src/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 5d7ccff4..460f4520 100644 --- a/src/main.c +++ b/src/main.c @@ -266,10 +266,10 @@ static int handle_feature(struct device* device_found, hid_device** device_handl // Makes parsing of optiona arguments easier // Credits to https://cfengine.com/blog/2021/optional-arguments-with-getopt-long/ -#define OPTIONAL_ARGUMENT_IS_PRESENT \ +#define OPTIONAL_ARGUMENT_IS_PRESENT \ ((optarg == NULL && optind < argc && argv[optind][0] != '-') \ - ? (bool) (optarg = argv[optind++]) \ - : (optarg != NULL)) + ? (bool)(optarg = argv[optind++]) \ + : (optarg != NULL)) int main(int argc, char* argv[]) {