From 7739ac8254a97f9c7685dfc42b77a5c291e023f7 Mon Sep 17 00:00:00 2001 From: klemek Date: Fri, 14 Nov 2025 09:23:25 +0100 Subject: [PATCH] refactor: state read event struct --- DEVELOPMENT.md | 3 ++- src/forge.c | 31 +++++++++---------------------- src/state.c | 31 ++++++++++++++++++++++++++++--- src/state.h | 9 ++++++--- src/window.c | 21 +++++++++++++++++++-- src/window.h | 2 +- 6 files changed, 65 insertions(+), 32 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 83c740c..f035203 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -118,12 +118,13 @@ make -f Makefile.dev release-arch - [x] Add NanoKontrol setup file - [x] Find and fix opengl errors 0500 ? - [ ] Extra features - - [ ] `--auto-random-cycle=4` + - [x] `--auto-random-cycle=4` - [ ] Arrows (up-down: bpm / left-right: cycle) - [ ] Save states (numkey: load / shift + numkey: save) - [ ] Configurable key codes - [ ] Key codes as inputs - [ ] Mouse position and scroll as inputs + - [ ] Joystick as input - [ ] Record show as text files - [ ] Play from record text file - [ ] Fixes diff --git a/src/forge.c b/src/forge.c index a00cda3..6807d91 100644 --- a/src/forge.c +++ b/src/forge.c @@ -119,36 +119,23 @@ static void error_callback(int error, const char *description) { static void key_callback(Window *window, int key, __attribute__((unused)) int scancode, int action, - __attribute__((unused)) int mods) { + int mods) { + unsigned int event; + + event = window_read_key(key, action, mods); + if (window_escape_key(key, action)) { // close window on escape key log_info("[ESC] Closing..."); window_close(window); - } else if (window_char_key(key, action, 82)) { - // R: randomize - log_info("[R] Randomized"); - state_randomize(context, &project.state_config); - state_apply(context, &project.state_config, &midi); - } else if (window_char_key(key, action, 48)) { - // 0: reset - log_info("[0] Reset"); - state_reset(context); - state_apply(context, &project.state_config, &midi); - } else if (window_char_key(key, action, 68)) { - // D: demo on/off - log_info((context->demo ? "[D] Demo OFF" : "[D] Demo ON")); - context->demo = !context->demo; - } else if (window_char_key(key, action, 65)) { - // A: auto random on/off - log_info( - (context->auto_random ? "[A] Auto Random OFF" : "[A] Auto Random ON")); - context->auto_random = !context->auto_random; + } else if (event > 0) { + state_key_event(context, &project.state_config, event, &midi); } } static void midi_callback(unsigned char code, unsigned char value) { - state_apply_event(context, &project.state_config, &midi, code, value, - trace_midi); + state_midi_event(context, &project.state_config, &midi, code, value, + trace_midi); } static void loop(bool hr, bool trace_fps) { diff --git a/src/state.c b/src/state.c index 8ac4e14..42d02ba 100644 --- a/src/state.c +++ b/src/state.c @@ -190,9 +190,9 @@ static void update_values(const SharedContext *context, } } -void state_apply_event(SharedContext *context, const StateConfig *state_config, - const MidiDevice *midi, unsigned char code, - unsigned char value, bool trace_midi) { +void state_midi_event(SharedContext *context, const StateConfig *state_config, + const MidiDevice *midi, unsigned char code, + unsigned char value, bool trace_midi) { unsigned int i, j, k, part; bool found; @@ -281,6 +281,31 @@ void state_apply_event(SharedContext *context, const StateConfig *state_config, } } +void state_key_event(SharedContext *context, const StateConfig *state_config, + unsigned int code, const MidiDevice *midi) { + if (code == 82) { + // R: randomize + log_info("[R] Randomized"); + state_randomize(context, state_config); + state_apply(context, state_config, midi); + } else if (code == 48) { + log_info("[0] Reset"); + state_reset(context); + state_apply(context, state_config, midi); + } else if (code == 68) { + // D: demo on/off + log_info((context->demo ? "[D] Demo OFF" : "[D] Demo ON")); + context->demo = !context->demo; + } else if (code == 65) { + // A: auto random on/off + log_info( + (context->auto_random ? "[A] Auto Random OFF" : "[A] Auto Random ON")); + context->auto_random = !context->auto_random; + } else { + log_info("[%d] No hotkey defined", code); + } +} + void state_apply(const SharedContext *context, const StateConfig *state_config, const MidiDevice *midi) { if (!midi->error) { diff --git a/src/state.h b/src/state.h index b726332..2694705 100644 --- a/src/state.h +++ b/src/state.h @@ -5,9 +5,12 @@ void state_parse_config(StateConfig *state_config, const ConfigFile *config); -void state_apply_event(SharedContext *context, const StateConfig *state_config, - const MidiDevice *midi, unsigned char code, - unsigned char value, bool trace_midi); +void state_midi_event(SharedContext *context, const StateConfig *state_config, + const MidiDevice *midi, unsigned char code, + unsigned char value, bool trace_midi); + +void state_key_event(SharedContext *context, const StateConfig *state_config, + unsigned int code, const MidiDevice *midi); bool state_background_write(SharedContext *context, const StateConfig *state_config, diff --git a/src/window.c b/src/window.c index 8598033..68d75da 100644 --- a/src/window.c +++ b/src/window.c @@ -145,6 +145,23 @@ bool window_escape_key(int key, int action) { return key == GLFW_KEY_ESCAPE && action == GLFW_PRESS; } -bool window_char_key(int key, int action, const int char_code) { - return key == char_code && action == GLFW_PRESS; +unsigned int window_read_key(int key, int action, int mods) { + unsigned int result; + if (action == GLFW_RELEASE || key == GLFW_KEY_LEFT_SHIFT || + key == GLFW_KEY_RIGHT_SHIFT || key == GLFW_KEY_LEFT_CONTROL || + key == GLFW_KEY_RIGHT_CONTROL || key == GLFW_KEY_LEFT_ALT || + key == GLFW_KEY_RIGHT_ALT) { + return 0; + } + result = key; + if ((mods & GLFW_MOD_SHIFT) > 0) { + result += 1000; + } + if ((mods & GLFW_MOD_CONTROL) > 0) { + result += 10000; + } + if ((mods & GLFW_MOD_ALT) > 0) { + result += 100000; + } + return result; } \ No newline at end of file diff --git a/src/window.h b/src/window.h index 8ac12ed..6c8bb14 100644 --- a/src/window.h +++ b/src/window.h @@ -27,6 +27,6 @@ bool window_should_close(Window *window); bool window_escape_key(int key, int action); -bool window_char_key(int key, int action, const int char_code); +unsigned int window_read_key(int key, int action, int mods); #endif /* WINDOW_H */ \ No newline at end of file