|
|
@@ -40,6 +40,7 @@ struct app {
|
|
|
uint64_t target_fps;
|
|
|
double dt;
|
|
|
uint32_t last_press;
|
|
|
+ SDL_KeyCode last_pressed_key;
|
|
|
struct {
|
|
|
uint8_t tab_size;
|
|
|
} cfg;
|
|
|
@@ -285,42 +286,44 @@ char_to_keymod(char c)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+#define IF_GOTO(cond, to) \
|
|
|
+ if ( (cond) ) { \
|
|
|
+ fprintf(stderr, #cond"\n"); \
|
|
|
+ goto to; \
|
|
|
+ }
|
|
|
|
|
|
void
|
|
|
app_add_global_keybind(struct app *app, const char *keybind,
|
|
|
keybind_func func,
|
|
|
enum keybind_dir dir)
|
|
|
{
|
|
|
- (void) app;
|
|
|
- (void) func;
|
|
|
- (void) dir;
|
|
|
struct str kbd_str = str_from_cstr_ns(keybind);
|
|
|
- struct str_da words = str_split(kbd_str, ' ');
|
|
|
- if ( words.size != 1 ) {
|
|
|
- goto invalid_key;
|
|
|
+ if ( kbd_str.size == 1 ) {
|
|
|
+ SDL_KeyCode key = char_to_keycode(kbd_str.data[0]);
|
|
|
+ IF_GOTO(key == SDLK_UNKNOWN, invalid_key);
|
|
|
+ struct app_kbd kbd = {
|
|
|
+ .mod = KMOD_NONE,
|
|
|
+ .func = func,
|
|
|
+ .dir = dir
|
|
|
+ };
|
|
|
+ HT_ISET(app->kbds, (int64_t)(key), kbd);
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ struct str_da words = str_split(kbd_str, ' ');
|
|
|
+ IF_GOTO(words.size != 1, invalid_key);
|
|
|
/* for */ {
|
|
|
struct str_da chars = str_split(words.items[0], '-');
|
|
|
- if ( chars.size != 2 ) {
|
|
|
- goto invalid_key;
|
|
|
- }
|
|
|
- if ( chars.items[0].size != 1 ) {
|
|
|
- goto invalid_key;
|
|
|
- }
|
|
|
- if ( chars.items[1].size != 1 ) {
|
|
|
- goto invalid_key;
|
|
|
- }
|
|
|
+ IF_GOTO(chars.size != 2, invalid_key)
|
|
|
+ IF_GOTO(chars.items[0].size != 1, invalid_key)
|
|
|
+ IF_GOTO(chars.items[1].size != 1, invalid_key)
|
|
|
|
|
|
SDL_Keymod mod = char_to_keymod(chars.items[0].data[0]);
|
|
|
- if ( (int32_t)(mod) == -1 ) {
|
|
|
- fprintf(stderr, "[ERROR] Invalid mod in `%s`\n",
|
|
|
- keybind);
|
|
|
- exit(EXIT_FAILURE);
|
|
|
- }
|
|
|
+ IF_GOTO((int32_t)(mod) == -1, invalid_key);
|
|
|
+
|
|
|
SDL_KeyCode key = char_to_keycode(chars.items[1].data[0]);
|
|
|
- if ( key == SDLK_UNKNOWN ) {
|
|
|
- goto invalid_key;
|
|
|
- }
|
|
|
+ IF_GOTO(key == SDLK_UNKNOWN, invalid_key);
|
|
|
DA_DESTROY(chars);
|
|
|
|
|
|
struct app_kbd kbd = {
|
|
|
@@ -328,7 +331,7 @@ app_add_global_keybind(struct app *app, const char *keybind,
|
|
|
.func = func,
|
|
|
.dir = dir
|
|
|
};
|
|
|
- HT_ISET(app->kbds, (int64_t)key, kbd);
|
|
|
+ HT_ISET(app->kbds, (int64_t)(key + mod), kbd);
|
|
|
}
|
|
|
|
|
|
DA_DESTROY(words);
|