[GTK3] Make keyboard inputs less hardcoded

The mapping is now saved in a modifiable array
instead of being hardcoded into a switch-case
statement.
This commit is contained in:
Maximilian Mader 2020-05-02 18:44:24 +02:00
parent 4075a13b61
commit 4042b7f38c
Signed by: Max
GPG Key ID: F71D56A3151C4FB3

View File

@ -157,6 +157,42 @@ static GuiData gui_data = {
};
GB_gameboy_t gb;
typedef enum {
INPUT_UP,
INPUT_DOWN,
INPUT_LEFT,
INPUT_RIGHT,
INPUT_A,
INPUT_B,
INPUT_START,
INPUT_SELECT,
INPUT_TURBO,
INPUT_REWIND,
INPUT_SLOWDOWN,
INPUT_FULLSCREEN,
} input_names_t;
static unsigned key_map[] = {
[INPUT_UP] = GDK_KEY_w,
[INPUT_DOWN] = GDK_KEY_a,
[INPUT_LEFT] = GDK_KEY_s,
[INPUT_RIGHT] = GDK_KEY_d,
[INPUT_A] = GDK_KEY_l,
[INPUT_B] = GDK_KEY_k,
[INPUT_START] = GDK_KEY_h,
[INPUT_SELECT] = GDK_KEY_g,
[INPUT_TURBO] = GDK_KEY_space,
[INPUT_REWIND] = GDK_KEY_Tab,
[INPUT_SLOWDOWN] = GDK_KEY_Shift_L,
[INPUT_FULLSCREEN] = GDK_KEY_F11,
};
// Forward declarations of the actions
static void activate_open(GSimpleAction *action, GVariant *parameter, gpointer app);
static void activate_close(GSimpleAction *action, GVariant *parameter, gpointer app);
@ -1458,50 +1494,43 @@ static void create_action_groups(GApplication *app) {
static gboolean on_key_press(GtkWidget *w, GdkEventKey *event, gpointer data) {
uint8_t mask;
// TODO: Allow control remapping in the GUI
switch (event->keyval) {
case GDK_KEY_w: mask = BUTTON_MASK_UP; break;
case GDK_KEY_a: mask = BUTTON_MASK_LEFT; break;
case GDK_KEY_s: mask = BUTTON_MASK_DOWN; break;
case GDK_KEY_d: mask = BUTTON_MASK_RIGHT; break;
if (event->keyval == key_map[INPUT_UP]) mask = BUTTON_MASK_UP;
if (event->keyval == key_map[INPUT_DOWN]) mask = BUTTON_MASK_DOWN;
if (event->keyval == key_map[INPUT_LEFT]) mask = BUTTON_MASK_LEFT;
if (event->keyval == key_map[INPUT_RIGHT]) mask = BUTTON_MASK_RIGHT;
if (event->keyval == key_map[INPUT_START]) mask = BUTTON_MASK_START;
if (event->keyval == key_map[INPUT_SELECT]) mask = BUTTON_MASK_SELECT;
if (event->keyval == key_map[INPUT_A]) mask = BUTTON_MASK_A;
if (event->keyval == key_map[INPUT_B]) mask = BUTTON_MASK_B;
case GDK_KEY_g: mask = BUTTON_MASK_SELECT; break;
case GDK_KEY_h: mask = BUTTON_MASK_START; break;
if (event->keyval == key_map[INPUT_REWIND]) {
gui_data.rewind_down = event->type == GDK_KEY_PRESS;
GB_set_turbo_mode(&gb, gui_data.turbo_down, gui_data.turbo_down && gui_data.rewind_down);
case GDK_KEY_k: mask = BUTTON_MASK_B; break;
case GDK_KEY_l: mask = BUTTON_MASK_A; break;
if (event->type == GDK_KEY_RELEASE) {
gui_data.rewind_paused = false;
}
}
case GDK_KEY_Tab: {
gui_data.rewind_down = event->type == GDK_KEY_PRESS;
GB_set_turbo_mode(&gb, gui_data.turbo_down, gui_data.turbo_down && gui_data.rewind_down);
if (event->keyval == key_map[INPUT_TURBO]) {
gui_data.turbo_down = event->type == GDK_KEY_PRESS;
GB_audio_clear_queue();
GB_set_turbo_mode(&gb, gui_data.turbo_down, gui_data.turbo_down && gui_data.rewind_down);
}
if (event->type == GDK_KEY_RELEASE) {
gui_data.rewind_paused = false;
if (event->keyval == key_map[INPUT_SLOWDOWN]) {
gui_data.underclock_down = event->type == GDK_KEY_PRESS;
}
if (event->keyval == key_map[INPUT_FULLSCREEN]) {
if (event->type == GDK_KEY_RELEASE) {
if (gui_data.is_fullscreen) {
gtk_window_unfullscreen(GTK_WINDOW(gui_data.main_window));
}
break; }
case GDK_KEY_space: {
gui_data.turbo_down = event->type == GDK_KEY_PRESS;
GB_audio_clear_queue();
GB_set_turbo_mode(&gb, gui_data.turbo_down, gui_data.turbo_down && gui_data.rewind_down);
break; }
case GDK_KEY_dead_acute: // fall through
case GDK_KEY_acute: // fall through
case GDK_KEY_apostrophe:
gui_data.underclock_down = event->type == GDK_KEY_PRESS;
break;
case GDK_KEY_F11: {
if (event->type == GDK_KEY_RELEASE) {
if (gui_data.is_fullscreen) {
gtk_window_unfullscreen(GTK_WINDOW(gui_data.main_window));
}
else {
gtk_window_fullscreen(GTK_WINDOW(gui_data.main_window));
}
else {
gtk_window_fullscreen(GTK_WINDOW(gui_data.main_window));
}
break; }
}
}
if (event->type == GDK_KEY_PRESS) {