vmrcli/src/vmrcli.c

258 lines
5.3 KiB
C
Raw Normal View History

2024-06-25 04:34:28 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
#include "cdll.h"
#include "vmr.h"
2024-06-26 16:44:28 +01:00
#include "log.h"
#include "util.h"
2024-06-25 04:34:28 +01:00
#define MAX_LINE 512
2024-06-25 04:34:28 +01:00
2024-06-25 23:32:32 +01:00
enum
{
FLOAT_T,
STRING_T,
};
struct result
{
int type;
union val
{
float f;
char s[MAX_LINE];
2024-06-25 23:32:32 +01:00
} val;
};
void help(void);
int set_kind(char *kval);
int init_voicemeeter(T_VBVMR_INTERFACE *vmr, int kind);
2024-06-25 04:34:28 +01:00
void interactive(T_VBVMR_INTERFACE *vmr);
2024-06-25 23:32:32 +01:00
void parse_command(T_VBVMR_INTERFACE *vmr, char *command);
void get(T_VBVMR_INTERFACE *vmr, char *command, struct result *res);
2024-06-25 04:34:28 +01:00
int main(int argc, char *argv[])
{
bool iflag = false;
2024-06-25 20:01:38 +01:00
int opt;
char *kvalue = "";
2024-06-26 16:44:28 +01:00
int dvalue;
int kind = BANANA;
2024-06-25 04:34:28 +01:00
if (argc == 1)
{
help();
exit(EXIT_SUCCESS);
}
log_set_level(LOG_WARN);
2024-06-26 16:44:28 +01:00
while ((opt = getopt(argc, argv, "k:ihD:")) != -1)
2024-06-25 04:34:28 +01:00
{
2024-06-25 20:01:38 +01:00
switch (opt)
2024-06-25 04:34:28 +01:00
{
case 'i':
iflag = true;
break;
case 'k':
kvalue = optarg;
2024-06-26 16:44:28 +01:00
kind = set_kind(kvalue);
break;
2024-06-25 20:01:38 +01:00
case 'h':
2024-06-25 23:32:32 +01:00
help();
2024-06-25 20:01:38 +01:00
exit(EXIT_SUCCESS);
2024-06-26 16:44:28 +01:00
case 'D':
dvalue = atoi(optarg);
if (dvalue >= LOG_TRACE && dvalue <= LOG_FATAL)
2024-06-26 16:44:28 +01:00
{
log_set_level(dvalue);
}
else
{
2024-06-27 08:27:49 +01:00
log_error(
"-D arg out of range, expected value from 0 up to 5\n"
2024-06-27 08:27:49 +01:00
"Log level will default to LOG_WARN (3).\n");
}
2024-06-26 16:44:28 +01:00
break;
2024-06-25 04:34:28 +01:00
default:
abort();
}
}
2024-06-26 06:01:01 +01:00
T_VBVMR_INTERFACE iVMR;
2024-06-25 04:34:28 +01:00
T_VBVMR_INTERFACE *vmr = &iVMR;
int rep = init_voicemeeter(vmr, kind);
2024-06-25 17:22:46 +01:00
if (rep != 0)
2024-06-25 04:34:28 +01:00
{
exit(EXIT_FAILURE);
}
if (iflag)
{
puts("Interactive mode enabled. Enter 'Q' to exit.");
interactive(vmr);
}
else
{
for (int i = optind; i < argc; i++)
{
2024-06-25 23:32:32 +01:00
parse_command(vmr, argv[i]);
2024-06-25 04:34:28 +01:00
}
}
rep = logout(vmr);
2024-06-25 17:22:46 +01:00
if (rep == 0)
2024-06-25 04:34:28 +01:00
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
2024-06-25 23:32:32 +01:00
void help()
2024-06-25 20:01:38 +01:00
{
2024-06-25 23:32:32 +01:00
puts(
2024-06-26 19:31:08 +01:00
"Usage: ./vmrcli.exe [-i] [-k] [-D] <api commands>\n"
2024-06-25 20:01:38 +01:00
"Where: \n"
"\ti: Enable interactive mode\n"
2024-06-26 16:44:28 +01:00
"\tk: The kind of Voicemeeter (basic, banana, potato)\n"
"\tD: Set log level 0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=FATAL");
2024-06-25 20:01:38 +01:00
}
int set_kind(char *kval)
{
if (strcmp(kval, "basic") == 0)
{
return BASIC;
}
else if (strcmp(kval, "banana") == 0)
{
return BANANA;
}
else if (strcmp(kval, "potato") == 0)
{
if (sizeof(void *) == 8)
return POTATOX64;
else
return POTATO;
}
else
{
2024-06-27 08:27:49 +01:00
log_error("Unknown Voicemeeter kind '%s'\n", kval);
exit(EXIT_FAILURE);
}
2024-06-25 04:34:28 +01:00
}
int init_voicemeeter(T_VBVMR_INTERFACE *vmr, int kind)
2024-06-25 04:34:28 +01:00
{
int rep = initialize_dll_interfaces(vmr);
if (rep < 0)
{
if (rep == -100)
{
2024-06-27 08:27:49 +01:00
log_error("Voicemeeter is not installed");
2024-06-25 04:34:28 +01:00
}
else
{
2024-06-27 08:27:49 +01:00
log_error("Error loading Voicemeeter dll with code %d\n", rep);
2024-06-25 04:34:28 +01:00
}
return rep;
}
rep = login(vmr, kind);
2024-06-25 04:34:28 +01:00
if (rep != 0)
{
2024-06-27 08:27:49 +01:00
log_error("Error logging into Voicemeeter");
2024-06-25 04:34:28 +01:00
return rep;
}
return 0;
}
void interactive(T_VBVMR_INTERFACE *vmr)
{
char input[MAX_LINE];
char *p = input;
char command[MAX_LINE];
int i;
2024-06-25 04:34:28 +01:00
while (fgets(input, MAX_LINE, stdin) != NULL)
{
2024-06-26 16:44:28 +01:00
input[strcspn(input, "\n")] = 0;
if (strlen(input) == 1 && (strncmp(input, "Q", 1) == 0 || strncmp(input, "q", 1) == 0))
2024-06-25 04:34:28 +01:00
break;
replace_multiple_space_with_one(input);
while (*p)
{
memset(command, '\0', sizeof(command));
i = 0;
2024-06-26 20:17:33 +01:00
while (!isspace(*p))
command[i++] = *p++;
command[i] = '\0';
if (command[0] != '\0')
parse_command(vmr, command);
2024-06-27 19:20:21 +01:00
p++; /* shift to next input char */
}
p = input; /* reset pointer */
2024-06-25 23:32:32 +01:00
}
}
void parse_command(T_VBVMR_INTERFACE *vmr, char *command)
{
2024-06-26 16:44:28 +01:00
log_debug("Parsing %s", command);
2024-06-25 23:32:32 +01:00
if (command[0] == '!') /* toggle */
{
2024-06-26 00:03:50 +01:00
command++;
struct result res = {.type = FLOAT_T};
get(vmr, command, &res);
if (res.type == FLOAT_T)
{
set_parameter_float(vmr, command, 1 - res.val.f);
}
2024-06-25 23:32:32 +01:00
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:
2024-06-27 02:00:17 +01:00
printf("%.1f\n", res.val.f);
2024-06-25 23:32:32 +01:00
break;
case STRING_T:
2024-06-26 00:03:50 +01:00
puts(res.val.s);
2024-06-25 23:32:32 +01:00
break;
default:
2024-06-27 08:27:49 +01:00
log_error("Unexpected result type");
2024-06-25 23:32:32 +01:00
break;
}
}
}
void get(T_VBVMR_INTERFACE *vmr, char *command, struct result *res)
2024-06-25 23:32:32 +01:00
{
2024-06-26 00:03:50 +01:00
clear_dirty(vmr);
2024-06-25 23:32:32 +01:00
if (get_parameter_float(vmr, command, &res->val.f) != 0)
{
res->type = STRING_T;
if (get_parameter_string(vmr, command, res->val.s) != 0)
{
res->val.s[0] = 0;
2024-06-27 08:27:49 +01:00
log_error("Unknown parameter");
}
2024-06-25 04:34:28 +01:00
}
}