vmrcli/src/vmrcli.c

353 lines
8.6 KiB
C
Raw Normal View History

/**
* @file vmrcli.c
* @author Onyx and Iris (code@onyxandiris.online)
* @brief A Voicemeeter Remote Command Line Interface
2024-07-09 13:14:52 +01:00
* @version 0.7.0
* @date 2024-07-06
*
* @copyright Copyright (c) 2024
* https://github.com/onyx-and-iris/vmrcli/blob/main/LICENSE
*/
2024-06-25 04:34:28 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
2024-07-03 13:57:58 +01:00
#include <string.h>
#include <ctype.h>
#include <windows.h>
#include "ivmr.h"
#include "wrapper.h"
2024-06-26 16:44:28 +01:00
#include "log.h"
#include "util.h"
2024-06-25 04:34:28 +01:00
#define USAGE "Usage: .\\vmrcli.exe [-h] [-i] [-k] [-D] [-v] [-c] [-m] [-s] <api commands>\n" \
"Where: \n" \
"\th: Prints the help message\n" \
"\ti: Enable interactive mode\n" \
"\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\n" \
"\tv: Enable extra console output (toggle, set messages)\n" \
"\tc: Load a user configuration (give the full file path)\n" \
"\tm: Launch the MacroButtons application\n" \
"\ts: Launch the StreamerView application"
#define OPTSTR ":hk:msc:iD:v"
#define MAX_LINE 512
2024-06-25 04:34:28 +01:00
2024-07-02 11:15:17 +01:00
/**
* @enum The kind of values a get call may return.
2024-07-02 11:15:17 +01:00
*/
2024-06-25 23:32:32 +01:00
enum
{
FLOAT_T,
STRING_T,
};
2024-07-02 11:15:17 +01:00
/**
* @struct A struct holding the result of a get call.
2024-07-02 11:15:17 +01:00
*/
2024-06-25 23:32:32 +01:00
struct result
{
int type;
union val
{
float f;
wchar_t s[MAX_LINE];
2024-06-25 23:32:32 +01:00
} val;
};
static bool vflag = false;
static void usage(void);
enum kind set_kind(char *kval);
void interactive(PT_VMR vmr);
void parse_input(PT_VMR vmr, char *input);
void parse_command(PT_VMR vmr, char *command);
void get(PT_VMR vmr, char *command, struct result *res);
2024-06-25 04:34:28 +01:00
int main(int argc, char *argv[])
{
bool iflag = false,
mflag = false,
sflag = false,
2024-07-05 11:28:29 +01:00
cflag = false;
2024-06-25 20:01:38 +01:00
int opt;
2024-06-26 16:44:28 +01:00
int dvalue;
2024-07-05 11:28:29 +01:00
char *cvalue;
enum kind kind = BANANAX64;
2024-06-25 04:34:28 +01:00
if (argc == 1)
{
usage();
}
log_set_level(LOG_WARN);
2024-06-26 16:44:28 +01:00
opterr = 0;
while ((opt = getopt(argc, argv, OPTSTR)) != -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 'k':
kind = set_kind(optarg);
if (kind == UNKNOWN)
{
log_fatal("Unknown Voicemeeter kind '%s'", optarg);
exit(EXIT_FAILURE);
}
break;
case 'm':
mflag = true;
break;
case 's':
sflag = true;
break;
2024-07-05 11:28:29 +01:00
case 'c':
cflag = true;
cvalue = optarg;
break;
case 'i':
iflag = true;
break;
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
{
log_warn(
"-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;
case 'v':
vflag = true;
break;
case '?':
2024-07-09 13:18:57 +01:00
log_fatal("unknown option -- '%c'\n"
"Try .\\vmrcli.exe -h for more information.",
optopt);
exit(EXIT_FAILURE);
case ':':
2024-07-09 13:18:57 +01:00
log_fatal("missing argument for option -- '%c'\n"
"Try .\\vmrcli.exe -h for more information.",
optopt);
exit(EXIT_FAILURE);
case 'h':
[[fallthrough]];
2024-06-25 04:34:28 +01:00
default:
usage();
2024-06-25 04:34:28 +01:00
}
}
PT_VMR vmr = create_interface();
2024-06-25 04:34:28 +01:00
int rep = login(vmr, kind);
2024-06-25 17:22:46 +01:00
if (rep != 0)
2024-06-25 04:34:28 +01:00
{
log_fatal("Error logging into the Voicemeeter API");
2024-06-25 04:34:28 +01:00
exit(EXIT_FAILURE);
}
if (mflag)
{
log_info("MacroButtons app launched");
run_voicemeeter(vmr, MACROBUTTONS);
}
if (sflag)
{
log_info("StreamerView app launched");
run_voicemeeter(vmr, STREAMERVIEW);
}
2024-07-05 11:28:29 +01:00
if (cflag)
{
2024-07-05 11:28:29 +01:00
log_info("Profile %s loaded", cvalue);
set_parameter_string(vmr, "command.load", cvalue);
2024-07-07 07:38:39 +01:00
Sleep(300);
clear_dirty(vmr);
}
2024-06-25 04:34:28 +01:00
if (iflag)
{
puts("Interactive mode enabled. Enter 'Q' to exit.");
interactive(vmr);
}
else
{
for (int i = optind; i < argc; i++)
{
parse_input(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;
}
2024-06-25 04:34:28 +01:00
else
{
log_fatal("Error logging out of the Voicemeeter API");
2024-06-25 04:34:28 +01:00
return EXIT_FAILURE;
}
}
/**
2024-07-02 11:15:17 +01:00
* @brief prints the help message
*/
static void usage()
2024-06-25 20:01:38 +01:00
{
puts(USAGE);
exit(EXIT_SUCCESS);
2024-06-25 20:01:38 +01:00
}
/**
* @brief Set the kind of Voicemeeter based on the value of -k flag.
* For 64 bit systems the value is promoted to X64.
*
* @param kval Value of the -k flag
* @return enum kind
*/
enum kind set_kind(char *kval)
{
if (strcmp(kval, "basic") == 0)
return sizeof(void *) == 8 ? BASICX64 : BASIC;
else if (strcmp(kval, "banana") == 0)
return sizeof(void *) == 8 ? BANANAX64 : BANANA;
else if (strcmp(kval, "potato") == 0)
return sizeof(void *) == 8 ? POTATOX64 : POTATO;
else
return UNKNOWN;
2024-06-25 04:34:28 +01:00
}
2024-07-02 11:15:17 +01:00
/**
* @brief Continuously read lines from stdin.
* Break if 'Q' is entered on the interactive prompt.
* Each line is passed to parse_input()
*
* @param vmr Pointer to the iVMR interface
2024-07-02 11:15:17 +01:00
*/
void interactive(PT_VMR vmr)
2024-06-25 04:34:28 +01:00
{
char input[MAX_LINE];
2024-06-25 04:34:28 +01:00
printf(">> ");
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 && toupper(input[0]) == 'Q')
2024-06-25 04:34:28 +01:00
break;
parse_input(vmr, input);
memset(input, 0, MAX_LINE); /* reset input buffer */
printf(">> ");
2024-06-25 23:32:32 +01:00
}
}
2024-07-02 11:15:17 +01:00
/**
2024-07-08 17:31:24 +01:00
* @brief Walks through each line split by " \t;," delimiters.
2024-07-02 11:15:17 +01:00
* Each token is passed to parse_command()
*
* @param vmr Pointer to the iVMR interface
2024-07-02 11:15:17 +01:00
* @param input Each input line, from stdin or CLI args
*/
void parse_input(PT_VMR vmr, char *input)
{
char *token, *p;
token = strtok_r(input, " \t;,", &p);
while (token != NULL)
{
parse_command(vmr, token);
token = strtok_r(NULL, " \t;,", &p);
}
}
2024-07-02 11:15:17 +01:00
/**
* @brief Execute each command according to type.
* See command type definitions in:
* https://github.com/onyx-and-iris/vmrcli?tab=readme-ov-file#api-commands
*
* @param vmr Pointer to the iVMR interface
2024-07-02 11:15:17 +01:00
* @param command Each token from the input line as its own command string
*/
void parse_command(PT_VMR vmr, char *command)
2024-06-25 23:32:32 +01:00
{
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)
{
2024-07-01 03:40:33 +01:00
if (res.val.f == 1 || res.val.f == 0)
{
2024-07-01 03:40:33 +01:00
set_parameter_float(vmr, command, 1 - res.val.f);
if (vflag)
{
printf("Toggling %s\n", command);
}
}
2024-07-01 03:40:33 +01:00
else
log_warn("%s does not appear to be a boolean parameter", command);
2024-06-26 00:03:50 +01:00
}
2024-06-25 23:32:32 +01:00
return;
}
if (strchr(command, '=') != NULL) /* set */
{
set_parameters(vmr, command);
if (vflag)
{
printf("Setting %s\n", command);
}
2024-06-25 23:32:32 +01:00
}
else /* get */
{
struct result res = {.type = FLOAT_T};
get(vmr, command, &res);
switch (res.type)
{
case FLOAT_T:
printf("%s: %.1f\n", command, res.val.f);
2024-06-25 23:32:32 +01:00
break;
case STRING_T:
if (res.val.s[0] != '\0')
printf("%s: %ls\n", command, res.val.s);
2024-06-25 23:32:32 +01:00
break;
default:
break;
}
}
}
2024-07-02 11:15:17 +01:00
/**
2024-07-08 08:45:14 +01:00
* @brief Get the value of a float or string parameter.
* Stores its type and value into a result struct
2024-07-02 11:15:17 +01:00
*
* @param vmr Pointer to the iVMR interface
2024-07-02 11:15:17 +01:00
* @param command A parsed 'get' command as a string
* @param res A struct holding the result of the API call.
*/
void get(PT_VMR 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;
log_error("Unknown parameter '%s'", command);
}
2024-06-25 04:34:28 +01:00
}
}