refactor: state read event struct

This commit is contained in:
2025-11-14 09:23:25 +01:00
parent c229b9bc68
commit 7739ac8254
6 changed files with 65 additions and 32 deletions
+2 -1
View File
@@ -118,12 +118,13 @@ make -f Makefile.dev release-arch
- [x] Add NanoKontrol setup file - [x] Add NanoKontrol setup file
- [x] Find and fix opengl errors 0500 ? - [x] Find and fix opengl errors 0500 ?
- [ ] Extra features - [ ] Extra features
- [ ] `--auto-random-cycle=4` - [x] `--auto-random-cycle=4`
- [ ] Arrows (up-down: bpm / left-right: cycle) - [ ] Arrows (up-down: bpm / left-right: cycle)
- [ ] Save states (numkey: load / shift + numkey: save) - [ ] Save states (numkey: load / shift + numkey: save)
- [ ] Configurable key codes - [ ] Configurable key codes
- [ ] Key codes as inputs - [ ] Key codes as inputs
- [ ] Mouse position and scroll as inputs - [ ] Mouse position and scroll as inputs
- [ ] Joystick as input
- [ ] Record show as text files - [ ] Record show as text files
- [ ] Play from record text file - [ ] Play from record text file
- [ ] Fixes - [ ] Fixes
+9 -22
View File
@@ -119,36 +119,23 @@ static void error_callback(int error, const char *description) {
static void key_callback(Window *window, int key, static void key_callback(Window *window, int key,
__attribute__((unused)) int scancode, int action, __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)) { if (window_escape_key(key, action)) {
// close window on escape key // close window on escape key
log_info("[ESC] Closing..."); log_info("[ESC] Closing...");
window_close(window); window_close(window);
} else if (window_char_key(key, action, 82)) { } else if (event > 0) {
// R: randomize state_key_event(context, &project.state_config, event, &midi);
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;
} }
} }
static void midi_callback(unsigned char code, unsigned char value) { static void midi_callback(unsigned char code, unsigned char value) {
state_apply_event(context, &project.state_config, &midi, code, value, state_midi_event(context, &project.state_config, &midi, code, value,
trace_midi); trace_midi);
} }
static void loop(bool hr, bool trace_fps) { static void loop(bool hr, bool trace_fps) {
+28 -3
View File
@@ -190,9 +190,9 @@ static void update_values(const SharedContext *context,
} }
} }
void state_apply_event(SharedContext *context, const StateConfig *state_config, void state_midi_event(SharedContext *context, const StateConfig *state_config,
const MidiDevice *midi, unsigned char code, const MidiDevice *midi, unsigned char code,
unsigned char value, bool trace_midi) { unsigned char value, bool trace_midi) {
unsigned int i, j, k, part; unsigned int i, j, k, part;
bool found; 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, void state_apply(const SharedContext *context, const StateConfig *state_config,
const MidiDevice *midi) { const MidiDevice *midi) {
if (!midi->error) { if (!midi->error) {
+6 -3
View File
@@ -5,9 +5,12 @@
void state_parse_config(StateConfig *state_config, const ConfigFile *config); void state_parse_config(StateConfig *state_config, const ConfigFile *config);
void state_apply_event(SharedContext *context, const StateConfig *state_config, void state_midi_event(SharedContext *context, const StateConfig *state_config,
const MidiDevice *midi, unsigned char code, const MidiDevice *midi, unsigned char code,
unsigned char value, bool trace_midi); 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, bool state_background_write(SharedContext *context,
const StateConfig *state_config, const StateConfig *state_config,
+19 -2
View File
@@ -145,6 +145,23 @@ bool window_escape_key(int key, int action) {
return key == GLFW_KEY_ESCAPE && action == GLFW_PRESS; return key == GLFW_KEY_ESCAPE && action == GLFW_PRESS;
} }
bool window_char_key(int key, int action, const int char_code) { unsigned int window_read_key(int key, int action, int mods) {
return key == char_code && action == GLFW_PRESS; 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;
} }
+1 -1
View File
@@ -27,6 +27,6 @@ bool window_should_close(Window *window);
bool window_escape_key(int key, int action); 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 */ #endif /* WINDOW_H */