From 304f26d64cabc7d05610a240ec6147969a4d753a Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Tue, 25 Jun 2024 23:32:32 +0100 Subject: [PATCH] add parse_command implement get logic --- include/vmr.h | 2 ++ src/vmr.c | 20 +++++++++++++ src/vmrcli.c | 79 +++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 92 insertions(+), 9 deletions(-) diff --git a/include/vmr.h b/include/vmr.h index 1e9000f..9b87aa1 100644 --- a/include/vmr.h +++ b/include/vmr.h @@ -19,6 +19,8 @@ long type(T_VBVMR_INTERFACE *iVMR, long *type); long version(T_VBVMR_INTERFACE *iVMR, long *version); bool pdirty(T_VBVMR_INTERFACE *iVMR); +long get_parameter_float(T_VBVMR_INTERFACE *iVMR, char *param, float *f); +long get_parameter_string(T_VBVMR_INTERFACE *iVMR, char *param, char *s); long set_parameters(T_VBVMR_INTERFACE *iVMR, char *command); bool mdirty(T_VBVMR_INTERFACE *iVMR); diff --git a/src/vmr.c b/src/vmr.c index 2296d08..e51fba4 100644 --- a/src/vmr.c +++ b/src/vmr.c @@ -63,6 +63,26 @@ bool pdirty(T_VBVMR_INTERFACE *iVMR) return iVMR->VBVMR_IsParametersDirty() == 1; } +long get_parameter_float(T_VBVMR_INTERFACE *iVMR, char *param, float *f) +{ + return iVMR->VBVMR_GetParameterFloat(param, f); +} + +long get_parameter_string(T_VBVMR_INTERFACE *iVMR, char *param, char *s) +{ + return iVMR->VBVMR_GetParameterStringA(param, s); +} + +long set_parameter_float(T_VBVMR_INTERFACE *iVMR, char *param, float val) +{ + return iVMR->VBVMR_SetParameterFloat(param, val); +} + +long set_parameter_string(T_VBVMR_INTERFACE *iVMR, char *param, char *s) +{ + return iVMR->VBVMR_SetParameterStringA(param, s); +} + long set_parameters(T_VBVMR_INTERFACE *iVMR, char *command) { return iVMR->VBVMR_SetParameters(command); diff --git a/src/vmrcli.c b/src/vmrcli.c index 455fc02..ad43e29 100644 --- a/src/vmrcli.c +++ b/src/vmrcli.c @@ -7,10 +7,28 @@ #define MAX_LINE 1024 -void help(char *progname); +enum +{ + FLOAT_T, + STRING_T, +}; + +struct result +{ + int type; + union val + { + float f; + char s[512]; + } val; +}; + +void help(void); int set_kind(char *kval); int init_voicemeeter(T_VBVMR_INTERFACE *vmr, int kind); void interactive(T_VBVMR_INTERFACE *vmr); +void parse_command(T_VBVMR_INTERFACE *vmr, char *command); +struct result *get(T_VBVMR_INTERFACE *vmr, char *command, struct result *res); int main(int argc, char *argv[]) { @@ -30,7 +48,7 @@ int main(int argc, char *argv[]) kvalue = optarg; break; case 'h': - help(argv[0]); + help(); exit(EXIT_SUCCESS); default: abort(); @@ -59,7 +77,7 @@ int main(int argc, char *argv[]) { for (int i = optind; i < argc; i++) { - set_parameters(vmr, argv[i]); + parse_command(vmr, argv[i]); } } @@ -75,14 +93,13 @@ int main(int argc, char *argv[]) } } -void help(char *progname) +void help() { - printf( - "Usage: ./%s [-i] [-k] \n" + puts( + "Usage: ./vmrcli.exe [-i] [-k] \n" "Where: \n" "\ti: Enable interactive mode\n" - "\tk: The kind of Voicemeeter (basic, banana, potato)\n", - progname); + "\tk: The kind of Voicemeeter (basic, banana, potato)"); } int set_kind(char *kval) @@ -144,6 +161,50 @@ void interactive(T_VBVMR_INTERFACE *vmr) if (strlen(input) == 2 && (strncmp(input, "Q", 1) == 0 || strncmp(input, "q", 1) == 0)) break; - set_parameters(vmr, input); + parse_command(vmr, input); } +} + +void parse_command(T_VBVMR_INTERFACE *vmr, char *command) +{ + printf("Parsing %s\n", command); + if (command[0] == '!') /* toggle */ + { + puts("Toggle"); + return; + } + + if (strchr(command, '=') != NULL) /* set */ + { + set_parameters(vmr, command); + } + else /* get */ + { + struct result res = {.type = FLOAT_T}; + + get(vmr, command, &res); + switch (res.type) + { + case FLOAT_T: + printf("%.2f\n", res.val.f); + break; + case STRING_T: + printf("%s\n", res.val.s); + break; + default: + printf("Unknown result..."); + break; + } + } +} + +struct result *get(T_VBVMR_INTERFACE *vmr, char *command, struct result *res) +{ + if (get_parameter_float(vmr, command, &res->val.f) != 0) + { + res->type = STRING_T; + get_parameter_string(vmr, command, res->val.s); + } + + return res; } \ No newline at end of file