Files
looper/client/src/client_cmd.c

136 lines
4.1 KiB
C

#include "client_cmd.h"
#include "plugins.h"
#include "carla_host.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static char from_port[256] = "";
static char to_port[256] = "";
char g_connect_error[512] = "";
const char* get_stored_from(void) { return from_port; }
const char* get_stored_to(void) { return to_port; }
static int get_plugin_id_for_port(const char *port_spec) {
// port_spec format: "plugin_id:port_name"
const char *colon = strchr(port_spec, ':');
if (!colon) return -1;
int id = atoi(port_spec);
(void)colon; // atoi stops at colon
return id;
}
int handle_client_command(const char *input, int *out_id) {
if (!input || *input == '\0') return -1;
// Copy input so we can use strtok
char buf[256];
strncpy(buf, input, sizeof(buf)-1);
buf[sizeof(buf)-1] = '\0';
const char *token = strtok(buf, " ");
if (!token) return -1;
// --- from <port> ---
if (strcmp(token, "from") == 0) {
const char *port = strtok(NULL, " ");
if (!port) return -1;
int ret = carla_connect_direct(port, "looper:input");
if (ret == 0) {
strncpy(from_port, port, sizeof(from_port)-1);
from_port[sizeof(from_port)-1] = '\0';
g_connect_error[0] = '\0';
} else {
snprintf(g_connect_error, sizeof(g_connect_error),
"Failed: %s -> looper:input (ret=%d)", port, ret);
}
return ret;
}
// --- to <port> ---
if (strcmp(token, "to") == 0) {
const char *port = strtok(NULL, " ");
if (!port) return -1;
int ret = carla_connect_direct("looper:output", port);
if (ret == 0) {
strncpy(to_port, port, sizeof(to_port)-1);
to_port[sizeof(to_port)-1] = '\0';
g_connect_error[0] = '\0';
} else {
snprintf(g_connect_error, sizeof(g_connect_error),
"Failed: looper:output -> %s (ret=%d)", port, ret);
}
return ret;
}
// --- addplugin <path> ---
if (strcmp(token, "addplugin") == 0) {
const char *path = strtok(NULL, " ");
if (!path || *path == '\0') return -1;
int id;
int ret = plugin_load(path, path, &id);
if (ret == 0 && out_id) *out_id = id;
// auto-connect using stored :from/:to if set
if (ret == 0 && from_port[0] && to_port[0]) {
// parse plugin port name from stored from_port ("plugin_id:port_name")
const char *colon = strchr(from_port, ':');
if (colon) {
const char *pname = colon + 1;
plugin_connect(id, pname, to_port);
}
}
return ret;
}
// --- connect [<from_port>] [<to_port>] ---
if (strcmp(token, "connect") == 0) {
const char *from = strtok(NULL, " ");
const char *to = strtok(NULL, " ");
if (!from) {
if (from_port[0]) from = from_port;
else return -1;
}
if (!to) {
if (to_port[0]) to = to_port;
else return -1;
}
// Parse plugin id from "plugin_id:port"
int id_from = get_plugin_id_for_port(from);
if (id_from < 0) return -1;
const char *port_name = strchr(from, ':');
if (!port_name) return -1;
port_name++;
return plugin_connect(id_from, port_name, to);
}
// --- disconnect [<from_port>] [<to_port>] ---
if (strcmp(token, "disconnect") == 0) {
const char *from = strtok(NULL, " ");
const char *to = strtok(NULL, " ");
if (!from) {
if (from_port[0]) from = from_port;
else return -1;
}
if (!to) {
if (to_port[0]) to = to_port;
else return -1;
}
return plugin_disconnect(from, to);
}
// --- rack / grid commands toggle via colon mode (just acknowledge) ---
if (strcmp(token, "rack") == 0 || strcmp(token, "grid") == 0) {
// rack mode toggled by 'R' key in tui; colon commands do nothing except return success
return 0;
}
return -1; // unknown command
}