feat: add status FIFO and parse status line in client

This commit is contained in:
Loic Coenen
2026-05-14 14:56:11 +00:00
committed by Loic Coenen (aider)
parent 791744beeb
commit 5341cb676a
3 changed files with 65 additions and 1 deletions

View File

@@ -4,7 +4,7 @@ CFLAGS = -Wall -Wextra -Wpedantic -std=c11
all: test_status_parse all: test_status_parse
test_status_parse: tests/test_status_parse.c test_status_parse: tests/test_status_parse.c
$(CC) $(CFLAGS) -I../src -o test_status_parse tests/test_status_parse.c ../src/tui.c -lncurses $(CC) $(CFLAGS) -Isrc -o test_status_parse tests/test_status_parse.c src/tui.c -lncurses
test: test_status_parse test: test_status_parse
./test_status_parse ./test_status_parse

View File

@@ -63,6 +63,28 @@ typedef struct {
} FuzzySearch; } FuzzySearch;
static FuzzySearch fuzzy_search = {0}; static FuzzySearch fuzzy_search = {0};
/* ---------- Parse status line from engine status FIFO ---------- */
typedef enum { STATE_IDLE, STATE_RECORD, STATE_LOOPING, STATE_PAUSED } ChannelState;
bool parse_status_line(const char *line, int *ch, int *scene, ChannelState *state) {
int sta;
if (sscanf(line, "CH=%d SC=%d STATE=%d", ch, scene, &sta) == 3) {
if (sta >= 0 && sta <= 3) {
*state = (ChannelState)sta;
return true;
}
}
/* try text-based format */
char state_str[32];
if (sscanf(line, "CH=%d SC=%d STATE=%31s", ch, scene, state_str) != 3)
return false;
if (strcmp(state_str, "IDLE") == 0) { *state = STATE_IDLE; return true; }
if (strcmp(state_str, "RECORD") == 0) { *state = STATE_RECORD; return true; }
if (strcmp(state_str, "LOOPING") == 0) { *state = STATE_LOOPING; return true; }
if (strcmp(state_str, "PAUSED") == 0) { *state = STATE_PAUSED; return true; }
return false;
}
/* ---------- State to color (dummy: all white) ---------- */ /* ---------- State to color (dummy: all white) ---------- */
static int state_to_color(ClipState s) { (void)s; return COLOR_EMPTY; } static int state_to_color(ClipState s) { (void)s; return COLOR_EMPTY; }

View File

@@ -5,6 +5,9 @@
#include "midi.h" #include "midi.h"
#include "queue.h" #include "queue.h"
#include <jack/jack.h> #include <jack/jack.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <jack/midiport.h> #include <jack/midiport.h>
#include <math.h> #include <math.h>
#include <stdatomic.h> #include <stdatomic.h>
@@ -12,6 +15,39 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define STATUS_FIFO "/tmp/looper_status"
static void looper_write_status(void) {
int fd = open(STATUS_FIFO, O_WRONLY | O_NONBLOCK);
if (fd < 0)
return;
struct channel_t *cur = get_channels_array();
int cap = atomic_load(&channel_capacity);
char buf[256];
for (int ch = 0; ch < cap; ch++) {
if (!atomic_load(&cur[ch].active))
continue;
int sc_idx = atomic_load(&cur[ch].current_scene);
int state = atomic_load(&cur[ch].scenes[sc_idx].state);
const char *state_str;
switch (state) {
case STATE_IDLE: state_str = "IDLE"; break;
case STATE_RECORD: state_str = "RECORD"; break;
case STATE_LOOPING: state_str = "LOOPING"; break;
case STATE_PAUSED: state_str = "PAUSED"; break;
default: state_str = "UNKNOWN";
}
int n = snprintf(buf, sizeof(buf),
"CH=%d SC=%d STATE=%s\n",
ch, sc_idx, state_str);
if (n > 0) {
int ret = write(fd, buf, n);
(void)ret;
}
}
close(fd);
}
/* Global state (shared across files) */ /* Global state (shared across files) */
struct channel_t *_Atomic channels = NULL; struct channel_t *_Atomic channels = NULL;
atomic_int channel_capacity = 0; atomic_int channel_capacity = 0;
@@ -393,6 +429,9 @@ void jack_shutdown_cb(void *arg) {
* looper initialisation * looper initialisation
* ---------------------------------------------------------------- */ * ---------------------------------------------------------------- */
int looper_init(jack_client_t *client) { int looper_init(jack_client_t *client) {
/* create status FIFO (ignore if already exists) */
mkfifo(STATUS_FIFO, 0666);
queue_init(&cmd_queue); queue_init(&cmd_queue);
queue_init(&cmd_queue_main_midi); queue_init(&cmd_queue_main_midi);
queue_init(&cmd_queue_main_fifo); queue_init(&cmd_queue_main_fifo);
@@ -631,4 +670,7 @@ void looper_process_commands(jack_client_t *client) {
pending_old = NULL; pending_old = NULL;
} }
} }
/* write current state to status FIFO */
looper_write_status();
} }