feat: add command queue and FIFO pipe for unified input handling

Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
This commit is contained in:
Loic Coenen
2026-05-09 21:31:54 +00:00
parent f7f18f9fa7
commit 392dabbc0f
8 changed files with 234 additions and 38 deletions

View File

@@ -9,6 +9,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "command.h"
#include "queue.h"
/* Global state (shared across files) */
struct channel_t channels[MAX_CHANNELS];
@@ -20,10 +22,46 @@ jack_port_t *midi_control_port = NULL;
jack_port_t *midi_clock_port = NULL;
atomic_int control_key_active = 0;
atomic_int bind_channel = 0;
spsc_queue_t cmd_queue;
/* Deferred removal index (1 second grace) */
static int pending_unregister_idx = -1;
static void apply_command(command_t cmd) {
switch (cmd.type) {
case CMD_CYCLE:
if (cmd.channel >= 0 && cmd.channel < MAX_CHANNELS) {
int cur = atomic_load(&channels[cmd.channel].state);
int next;
switch (cur) {
case STATE_IDLE: next = STATE_RECORD; break;
case STATE_RECORD: next = STATE_LOOPING; break;
case STATE_LOOPING: next = STATE_PAUSED; break;
case STATE_PAUSED: next = STATE_LOOPING; break;
default: next = STATE_IDLE; break;
}
atomic_store(&channels[cmd.channel].state, next);
}
break;
case CMD_STOP:
if (cmd.channel >= 0 && cmd.channel < MAX_CHANNELS)
atomic_store(&channels[cmd.channel].state, STATE_IDLE);
else {
for (int i = 0; i < MAX_CHANNELS; i++)
atomic_store(&channels[i].state, STATE_IDLE);
}
break;
case CMD_BIND_CHANNEL:
atomic_store(&bind_channel, cmd.data);
break;
case CMD_UNBIND:
atomic_store(&bind_channel, 0);
break;
default:
break;
}
}
/* ----------------------------------------------------------------
* process callback
* ---------------------------------------------------------------- */
@@ -37,6 +75,12 @@ int process_callback(jack_nframes_t nframes, void *arg) {
}
}
/* drain RTsafe commands */
command_t cmd;
while (queue_pop(&cmd_queue, &cmd)) {
apply_command(cmd);
}
/* process each active channel */
for (int c = 0; c < MAX_CHANNELS; c++) {
if (!atomic_load(&channels[c].active))
@@ -171,6 +215,7 @@ void jack_shutdown_cb(void *arg) {
* looper initialisation
* ---------------------------------------------------------------- */
int looper_init(jack_client_t *client) {
queue_init(&cmd_queue);
/* channel 0 */
channels[0].active = 1;
atomic_store(&channels[0].state, STATE_IDLE);