Skip to content

Commit

Permalink
Make gamepad key configuration non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
thesourcehim committed Oct 10, 2024
1 parent 1cf7c37 commit 72315fa
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 deletions desmume/src/frontend/posix/gtk/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ int real_framebuffer_height = GPU_FRAMEBUFFER_NATIVE_HEIGHT;


desmume::config::Config config;
bool in_joy_config_mode = false;

#ifdef GDB_STUB
gdbstub_handle_t arm9_gdb_stub = NULL;
Expand Down Expand Up @@ -2075,6 +2076,63 @@ static void Edit_Controls(GSimpleAction *action, GVariant *parameter, gpointer u

}

static gboolean JoyKeyAcceptTimerFunc(gpointer data)
{
if(!in_joy_config_mode)
return FALSE;
SDL_Event event;
u16 key;
bool done = FALSE;
while(SDL_PollEvent(&event) && !done)
{
switch(event.type)
{
case SDL_JOYBUTTONDOWN:
key = ((event.jbutton.which & 15) << 12) | JOY_BUTTON << 8 | (event.jbutton.button & 255);
done = TRUE;
break;
case SDL_JOYAXISMOTION:
if( ((u32)abs(event.jaxis.value) >> 14) != 0 )
{
key = ((event.jaxis.which & 15) << 12) | JOY_AXIS << 8 | ((event.jaxis.axis & 127) << 1);
if (event.jaxis.value > 0)
key |= 1;
done = TRUE;
}
break;
case SDL_JOYHATMOTION:
if (event.jhat.value != SDL_HAT_CENTERED) {
key = ((event.jhat.which & 15) << 12) | JOY_HAT << 8 | ((event.jhat.hat & 63) << 2);
if ((event.jhat.value & SDL_HAT_UP) != 0)
key |= JOY_HAT_UP;
else if ((event.jhat.value & SDL_HAT_RIGHT) != 0)
key |= JOY_HAT_RIGHT;
else if ((event.jhat.value & SDL_HAT_DOWN) != 0)
key |= JOY_HAT_DOWN;
else if ((event.jhat.value & SDL_HAT_LEFT) != 0)
key |= JOY_HAT_LEFT;
done = TRUE;
}
break;
}
}

if(done) {
struct modify_key_ctx *ctx = (struct modify_key_ctx*)data;
ctx->mk_key_chosen = key;
gchar* YouPressed = g_strdup_printf("You pressed : %d\nClick OK to keep this key.", ctx->mk_key_chosen);
gtk_label_set_text(GTK_LABEL(ctx->label), YouPressed);
g_free(YouPressed);
return FALSE;
}
return in_joy_config_mode;
}

static void JoyKeyAcceptTimerStart(GtkWidget *w, GdkEventFocus *e, struct modify_key_ctx *ctx)
{
g_timeout_add(200, JoyKeyAcceptTimerFunc, ctx);
}

static void AcceptNewJoyKey(GtkWidget *w, GdkEventFocus *e, struct modify_key_ctx *ctx)
{
gchar *YouPressed;
Expand All @@ -2097,7 +2155,7 @@ static void Modify_JoyKey(GtkWidget* widget, gpointer data)
Key = GPOINTER_TO_INT(data);
/* Joypad keys start at 1 */
ctx.key_id = Key+1;
ctx.mk_key_chosen = 0;
ctx.mk_key_chosen = Keypad_Temp[Key];
Title = g_strdup_printf("Press \"%s\" key ...\n", key_names[Key]);
mkDialog = gtk_dialog_new_with_buttons(Title,
GTK_WINDOW(pWindow),
Expand All @@ -2111,7 +2169,9 @@ static void Modify_JoyKey(GtkWidget* widget, gpointer data)
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(mkDialog))), ctx.label, TRUE, FALSE, 0);
gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(mkDialog)));

g_signal_connect(G_OBJECT(mkDialog), "focus_in_event", G_CALLBACK(AcceptNewJoyKey), &ctx);
in_joy_config_mode = true;
g_signal_connect(G_OBJECT(mkDialog), "focus_in_event", G_CALLBACK(JoyKeyAcceptTimerStart), &ctx);
JoyKeyAcceptTimerFunc(&ctx);

switch(gtk_dialog_run(GTK_DIALOG(mkDialog))) {
case GTK_RESPONSE_OK:
Expand All @@ -2125,6 +2185,7 @@ static void Modify_JoyKey(GtkWidget* widget, gpointer data)
ctx.mk_key_chosen = 0;
break;
}
in_joy_config_mode = false;

gtk_widget_destroy(mkDialog);

Expand Down Expand Up @@ -2852,7 +2913,8 @@ gboolean EmuLoop(gpointer data)
#endif

/* Merge the joystick keys with the keyboard ones */
process_joystick_events(&keys_latch);
if(!in_joy_config_mode)
process_joystick_events(&keys_latch);
/* Update! */
update_keypad(keys_latch);

Expand Down

0 comments on commit 72315fa

Please sign in to comment.