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] 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
+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,
__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) {
+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,
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) {
+6 -3
View File
@@ -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,
+19 -2
View File
@@ -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;
}
+1 -1
View File
@@ -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 */