From 02152b449106165fe0013c8176c8d6f8bd068c2e Mon Sep 17 00:00:00 2001 From: Loic Coenen Date: Sat, 2 May 2026 14:37:02 +0000 Subject: [PATCH] fix: refactor cli.c to use action dispatcher pattern Co-authored-by: aider (deepseek/deepseek-coder) --- cli.c | 55 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/cli.c b/cli.c index 66a6dc8..11d4bae 100644 --- a/cli.c +++ b/cli.c @@ -75,9 +75,11 @@ int cli_process_line(Engine *engine, const char *line) { } int idx = atoi(idx_str); 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) { - engine_trigger_scene(engine, idx); + Action action = { .type = ACTION_TRIGGER_SCENE, .data.trigger_scene = { .scene_index = idx } }; + engine->dispatch(action); } else { printf("Unknown trigger type: %s\n", sub); } @@ -95,10 +97,11 @@ int cli_process_line(Engine *engine, const char *line) { return 1; } 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) { - engine_transport_stop(engine); - engine_process_commands(engine); + Action action = { .type = ACTION_RESET_TRANSPORT }; + engine->dispatch(action); } else { printf("Unknown reset target: %s\n", sub); } @@ -110,7 +113,8 @@ int cli_process_line(Engine *engine, const char *line) { return 1; } 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) { char *val_str = strtok(NULL, " \t"); @@ -119,26 +123,27 @@ int cli_process_line(Engine *engine, const char *line) { return 1; } 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) { - engine_transport_play(engine); - engine_process_commands(engine); + Action action = { .type = ACTION_TRANSPORT_PLAY }; + engine->dispatch(action); printf("Transport: Playing\n"); } else if (strcasecmp(token, "pause") == 0) { - engine_transport_pause(engine); - engine_process_commands(engine); + Action action = { .type = ACTION_TRANSPORT_PAUSE }; + engine->dispatch(action); printf("Transport: Paused\n"); } else if (strcasecmp(token, "stop") == 0) { - engine_transport_stop(engine); - engine_process_commands(engine); + Action action = { .type = ACTION_TRANSPORT_STOP }; + engine->dispatch(action); printf("Transport: Stopped\n"); } else if (strcasecmp(token, "toggle") == 0) { - engine_transport_toggle_play(engine); - engine_process_commands(engine); + Action action = { .type = ACTION_TRANSPORT_TOGGLE_PLAY }; + engine->dispatch(action); printf("Transport: Toggled\n"); } else if (strcasecmp(token, "clock") == 0) { @@ -148,12 +153,12 @@ int cli_process_line(Engine *engine, const char *line) { return 1; } if (strcasecmp(source_str, "internal") == 0) { - engine_set_clock_source(engine, CLOCK_SOURCE_INTERNAL); - engine_process_commands(engine); + Action action = { .type = ACTION_SET_CLOCK_SOURCE, .data.set_clock_source = { .source = CLOCK_SOURCE_INTERNAL } }; + engine->dispatch(action); printf("Clock source: Internal\n"); } else if (strcasecmp(source_str, "midi") == 0) { - engine_set_clock_source(engine, CLOCK_SOURCE_MIDI); - engine_process_commands(engine); + Action action = { .type = ACTION_SET_CLOCK_SOURCE, .data.set_clock_source = { .source = CLOCK_SOURCE_MIDI } }; + engine->dispatch(action); printf("Clock source: MIDI\n"); } else { printf("Unknown clock source: %s\n", source_str); @@ -167,7 +172,10 @@ int cli_process_line(Engine *engine, const char *line) { return 1; } 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); } else if (strcasecmp(token, "save") == 0) { @@ -177,7 +185,8 @@ int cli_process_line(Engine *engine, const char *line) { return 1; } 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); } else if (strcasecmp(token, "bpm") == 0) { @@ -188,8 +197,8 @@ int cli_process_line(Engine *engine, const char *line) { } double bpm = atof(bpm_str); if (bpm >= 1.0 && bpm <= 999.0) { - engine_set_bpm(engine, bpm); - engine_process_commands(engine); + Action action = { .type = ACTION_SET_BPM, .data.set_bpm = { .bpm = bpm } }; + engine->dispatch(action); printf("BPM set to: %.1f\n", bpm); } else { printf("BPM must be between 1.0 and 999.0\n");