vmrcli/src/vmrcli.c

210 lines
4.0 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"
#define MAX_LINE 1024
2024-06-25 23:32:32 +01:00
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);
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);
struct result *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 = "";
int kind = BANANA;
2024-06-25 04:34:28 +01:00
2024-06-25 20:01:38 +01:00
while ((opt = getopt(argc, argv, "k:ih")) != -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;
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-25 04:34:28 +01:00
default:
abort();
}
}
static T_VBVMR_INTERFACE iVMR;
T_VBVMR_INTERFACE *vmr = &iVMR;
2024-06-25 20:01:38 +01:00
if (kvalue && kvalue[0] != '\0')
{
kind = set_kind(kvalue);
}
2024-06-25 04:34:28 +01:00
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 20:41:17 +01:00
puts("Successfully logged out of the Voicemeeter API");
2024-06-25 04:34:28 +01:00
return EXIT_SUCCESS;
}
2024-06-25 04:34:28 +01:00
else
{
2024-06-25 04:34:28 +01:00
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(
"Usage: ./vmrcli.exe [-i] [-k] <api commands>\n"
2024-06-25 20:01:38 +01:00
"Where: \n"
"\ti: Enable interactive mode\n"
2024-06-25 23:32:32 +01:00
"\tk: The kind of Voicemeeter (basic, banana, potato)");
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
{
fprintf(stderr, "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)
{
fputs("Voicemeeter is not installed", stderr);
}
else
{
fprintf(stderr, "Error loading Voicemeeter dll with code %d\n", rep);
}
return rep;
}
rep = login(vmr, kind);
2024-06-25 04:34:28 +01:00
if (rep != 0)
{
fputs("Error logging into Voicemeeter", stderr);
return rep;
}
return 0;
}
void interactive(T_VBVMR_INTERFACE *vmr)
{
char input[MAX_LINE];
while (fgets(input, MAX_LINE, stdin) != NULL)
{
2024-06-25 17:08:31 +01:00
if (strlen(input) == 2 && (strncmp(input, "Q", 1) == 0 || strncmp(input, "q", 1) == 0))
2024-06-25 04:34:28 +01:00
break;
2024-06-25 23:32:32 +01:00
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);
2024-06-25 04:34:28 +01:00
}
2024-06-25 23:32:32 +01:00
return res;
2024-06-25 04:34:28 +01:00
}