fix: refactor cli.c to use action dispatcher pattern

Co-authored-by: aider (deepseek/deepseek-coder) <aider@aider.chat>
This commit is contained in:
Loic Coenen
2026-05-02 14:37:02 +00:00
parent 4b2c315c50
commit 02152b4491

55
cli.c
View File

@@ -75,9 +75,11 @@ int cli_process_line(Engine *engine, const char *line) {
} }
int idx = atoi(idx_str); int idx = atoi(idx_str);
if (strcasecmp(sub, "clip") == 0) { if (strcasecmp(sub, "clip") == 0) {
engine_trigger_clip(engine, idx); Action action = { .type = ACTION_TRIGGER_CLIP, .data.trigger_clip = { .clip_index = idx } };
engine->dispatch(action);
} else if (strcasecmp(sub, "scene") == 0) { } else if (strcasecmp(sub, "scene") == 0) {
engine_trigger_scene(engine, idx); Action action = { .type = ACTION_TRIGGER_SCENE, .data.trigger_scene = { .scene_index = idx } };
engine->dispatch(action);
} else { } else {
printf("Unknown trigger type: %s\n", sub); printf("Unknown trigger type: %s\n", sub);
} }
@@ -95,10 +97,11 @@ int cli_process_line(Engine *engine, const char *line) {
return 1; return 1;
} }
int idx = atoi(idx_str); int idx = atoi(idx_str);
engine_reset_clip(engine, idx); Action action = { .type = ACTION_RESET_CLIP, .data.reset_clip = { .clip_index = idx } };
engine->dispatch(action);
} else if (strcasecmp(sub, "transport") == 0) { } else if (strcasecmp(sub, "transport") == 0) {
engine_transport_stop(engine); Action action = { .type = ACTION_RESET_TRANSPORT };
engine_process_commands(engine); engine->dispatch(action);
} else { } else {
printf("Unknown reset target: %s\n", sub); printf("Unknown reset target: %s\n", sub);
} }
@@ -110,7 +113,8 @@ int cli_process_line(Engine *engine, const char *line) {
return 1; return 1;
} }
QuantizeMode mode = parse_quantize_mode(mode_str); QuantizeMode mode = parse_quantize_mode(mode_str);
engine_set_quantize_mode(engine, mode); Action action = { .type = ACTION_SET_QUANTIZE_MODE, .data.set_quantize_mode = { .mode = mode } };
engine->dispatch(action);
} }
else if (strcasecmp(token, "threshold") == 0) { else if (strcasecmp(token, "threshold") == 0) {
char *val_str = strtok(NULL, " \t"); char *val_str = strtok(NULL, " \t");
@@ -119,26 +123,27 @@ int cli_process_line(Engine *engine, const char *line) {
return 1; return 1;
} }
jack_nframes_t samples = (jack_nframes_t)atol(val_str); jack_nframes_t samples = (jack_nframes_t)atol(val_str);
engine_set_quantize_threshold(engine, samples); Action action = { .type = ACTION_SET_QUANTIZE_THRESHOLD, .data.set_quantize_threshold = { .threshold = samples } };
engine->dispatch(action);
} }
else if (strcasecmp(token, "play") == 0) { else if (strcasecmp(token, "play") == 0) {
engine_transport_play(engine); Action action = { .type = ACTION_TRANSPORT_PLAY };
engine_process_commands(engine); engine->dispatch(action);
printf("Transport: Playing\n"); printf("Transport: Playing\n");
} }
else if (strcasecmp(token, "pause") == 0) { else if (strcasecmp(token, "pause") == 0) {
engine_transport_pause(engine); Action action = { .type = ACTION_TRANSPORT_PAUSE };
engine_process_commands(engine); engine->dispatch(action);
printf("Transport: Paused\n"); printf("Transport: Paused\n");
} }
else if (strcasecmp(token, "stop") == 0) { else if (strcasecmp(token, "stop") == 0) {
engine_transport_stop(engine); Action action = { .type = ACTION_TRANSPORT_STOP };
engine_process_commands(engine); engine->dispatch(action);
printf("Transport: Stopped\n"); printf("Transport: Stopped\n");
} }
else if (strcasecmp(token, "toggle") == 0) { else if (strcasecmp(token, "toggle") == 0) {
engine_transport_toggle_play(engine); Action action = { .type = ACTION_TRANSPORT_TOGGLE_PLAY };
engine_process_commands(engine); engine->dispatch(action);
printf("Transport: Toggled\n"); printf("Transport: Toggled\n");
} }
else if (strcasecmp(token, "clock") == 0) { else if (strcasecmp(token, "clock") == 0) {
@@ -148,12 +153,12 @@ int cli_process_line(Engine *engine, const char *line) {
return 1; return 1;
} }
if (strcasecmp(source_str, "internal") == 0) { if (strcasecmp(source_str, "internal") == 0) {
engine_set_clock_source(engine, CLOCK_SOURCE_INTERNAL); Action action = { .type = ACTION_SET_CLOCK_SOURCE, .data.set_clock_source = { .source = CLOCK_SOURCE_INTERNAL } };
engine_process_commands(engine); engine->dispatch(action);
printf("Clock source: Internal\n"); printf("Clock source: Internal\n");
} else if (strcasecmp(source_str, "midi") == 0) { } else if (strcasecmp(source_str, "midi") == 0) {
engine_set_clock_source(engine, CLOCK_SOURCE_MIDI); Action action = { .type = ACTION_SET_CLOCK_SOURCE, .data.set_clock_source = { .source = CLOCK_SOURCE_MIDI } };
engine_process_commands(engine); engine->dispatch(action);
printf("Clock source: MIDI\n"); printf("Clock source: MIDI\n");
} else { } else {
printf("Unknown clock source: %s\n", source_str); printf("Unknown clock source: %s\n", source_str);
@@ -167,7 +172,10 @@ int cli_process_line(Engine *engine, const char *line) {
return 1; return 1;
} }
int clip_idx = atoi(clip_str); int clip_idx = atoi(clip_str);
save_load_queue_push(&engine->save_load_queue, REQ_LOAD_CLIP, clip_idx, filename); Action action = { .type = ACTION_LOAD_CLIP, .data.load_clip = { .clip_index = clip_idx } };
strncpy(action.data.load_clip.filename, filename, 255);
action.data.load_clip.filename[255] = '\0';
engine->dispatch(action);
printf("Loading %s into clip %d...\n", filename, clip_idx); printf("Loading %s into clip %d...\n", filename, clip_idx);
} }
else if (strcasecmp(token, "save") == 0) { else if (strcasecmp(token, "save") == 0) {
@@ -177,7 +185,8 @@ int cli_process_line(Engine *engine, const char *line) {
return 1; return 1;
} }
int clip_idx = atoi(clip_str); int clip_idx = atoi(clip_str);
save_load_queue_push(&engine->save_load_queue, REQ_SAVE_CLIP, clip_idx, ""); Action action = { .type = ACTION_SAVE_CLIP, .data.save_clip = { .clip_index = clip_idx } };
engine->dispatch(action);
printf("Saving clip %d...\n", clip_idx); printf("Saving clip %d...\n", clip_idx);
} }
else if (strcasecmp(token, "bpm") == 0) { else if (strcasecmp(token, "bpm") == 0) {
@@ -188,8 +197,8 @@ int cli_process_line(Engine *engine, const char *line) {
} }
double bpm = atof(bpm_str); double bpm = atof(bpm_str);
if (bpm >= 1.0 && bpm <= 999.0) { if (bpm >= 1.0 && bpm <= 999.0) {
engine_set_bpm(engine, bpm); Action action = { .type = ACTION_SET_BPM, .data.set_bpm = { .bpm = bpm } };
engine_process_commands(engine); engine->dispatch(action);
printf("BPM set to: %.1f\n", bpm); printf("BPM set to: %.1f\n", bpm);
} else { } else {
printf("BPM must be between 1.0 and 999.0\n"); printf("BPM must be between 1.0 and 999.0\n");