Skip to content

Commit

Permalink
cli: fix joypad and keyboard config retrieval
Browse files Browse the repository at this point in the history
because the GTK frontends use GDK keysyms, not SDL ones, - the former
are being stored in the config file and used by the GTK ui) -  a temporary
workaround was put into place 14 years ago: the loaded config values
were simply being overwritten with the hardcoded defaults.

this commit removes the overriding of the config, and introduces
a cli frontend specific section "SDLKEYS", which is written by the
GTK2 frontend upon a configuration change.
it tries to convert the GDK keycodes into SDL2 ones while doing so.

an alternative solution (involving less code changes) would have been
to do the conversion in the cli frontend, but that would require having
the gdk header available for compilation, which may not be the case
if the user only wants the cli frontend. such a user could now create
the config file on another machine with the GTK frontend, or simply
manually take the desired values from the SDL_keycode.h header.

this change is instigated by one of the changes in PR #822, which simply
removed the workaround and kept parsing on error, which mitigated the
problem for some keys, but not all.
  • Loading branch information
rofl0r committed Oct 27, 2024
1 parent 0a36e96 commit b45d249
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 15 deletions.
4 changes: 1 addition & 3 deletions desmume/src/frontend/posix/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,8 @@ int main(int argc, char ** argv) {
/* Initialize joysticks */
if(!init_joy()) return 1;
/* Load keyboard and joystick configuration */
keyfile = desmume_config_read_file(cli_kb_cfg);
keyfile = desmume_config_read_file(cli_kb_cfg, "SDLKEYS");
desmume_config_dispose(keyfile);
/* Since gtk has a different mapping the keys stop to work with the saved configuration :| */
load_default_config(cli_kb_cfg);

if(my_config.load_slot != -1){
loadstate_slot(my_config.load_slot);
Expand Down
19 changes: 17 additions & 2 deletions desmume/src/frontend/posix/gtk2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2194,13 +2194,19 @@ static void Modify_Key(GtkWidget* widget, gpointer data)

}

#include "../shared/gdksdl.cpp"

static void Edit_Controls()
{
GtkWidget *ecDialog;
GtkWidget *ecKey;
gchar *Key_Label;
u32 keyboard_cfg_sdl[NB_KEYS];
int i;

g_assert(sizeof(Keypad_Temp) == sizeof(keyboard_cfg) &&
sizeof(keyboard_cfg) == sizeof(keyboard_cfg_sdl));

memcpy(&Keypad_Temp, &keyboard_cfg, sizeof(keyboard_cfg));

ecDialog = gtk_dialog_new_with_buttons("Edit controls",
Expand All @@ -2222,8 +2228,17 @@ static void Edit_Controls()

switch (gtk_dialog_run(GTK_DIALOG(ecDialog))) {
case GTK_RESPONSE_OK:
memcpy(&keyboard_cfg, &Keypad_Temp, sizeof(keyboard_cfg));
desmume_config_update_keys(keyfile);
/* convert keycodes to SDL for the cli frontend, since it has no config menu */
for (i = 0; i < NB_KEYS; ++i) {
int sk = gdk_to_sdl_keycode(Keypad_Temp[i]);
/* if we don't know the keycode, chances are that SDL knows it anyways */
if (sk == -1) sk = (u32) Keypad_Temp[i];
keyboard_cfg_sdl[i] = sk;
}
memcpy(keyboard_cfg, keyboard_cfg_sdl, sizeof(keyboard_cfg));
desmume_config_update_keys(keyfile, "SDLKEYS");
memcpy(keyboard_cfg, Keypad_Temp, sizeof(keyboard_cfg));
desmume_config_update_keys(keyfile, "KEYS");
break;
case GTK_RESPONSE_CANCEL:
case GTK_RESPONSE_NONE:
Expand Down
14 changes: 7 additions & 7 deletions desmume/src/frontend/posix/shared/desmume_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static const gchar *desmume_old_config_file = ".desmume.ini";
static const gchar *desmume_config_dir = "desmume";
static const gchar *desmume_config_file = "config";

GKeyFile *desmume_config_read_file(const u32 *kb_cfg)
GKeyFile *desmume_config_read_file(const u32 *kb_cfg, const char* keysection)
{
gchar *config_file, *config_dir, *old_config_file;
GKeyFile *keyfile;
Expand Down Expand Up @@ -56,7 +56,7 @@ GKeyFile *desmume_config_read_file(const u32 *kb_cfg)
g_free(old_config_file);

load_default_config(kb_cfg);
desmume_config_read_keys(keyfile);
desmume_config_read_keys(keyfile, keysection);
desmume_config_read_joykeys(keyfile);

return keyfile;
Expand Down Expand Up @@ -92,10 +92,10 @@ static gboolean desmume_config_write_file(GKeyFile *keyfile)
return ret;
}

gboolean desmume_config_update_keys(GKeyFile *keyfile)
gboolean desmume_config_update_keys(GKeyFile *keyfile, const char *section)
{
for(int i = 0; i < NB_KEYS; i++) {
g_key_file_set_integer(keyfile, "KEYS", key_names[i], keyboard_cfg[i]);
g_key_file_set_integer(keyfile, section, key_names[i], keyboard_cfg[i]);
}

return desmume_config_write_file(keyfile);
Expand All @@ -110,15 +110,15 @@ gboolean desmume_config_update_joykeys(GKeyFile *keyfile)
return desmume_config_write_file(keyfile);
}

gboolean desmume_config_read_keys(GKeyFile *keyfile)
gboolean desmume_config_read_keys(GKeyFile *keyfile, const char *section)
{
GError *error = NULL;

if (!g_key_file_has_group(keyfile, "KEYS"))
if (!g_key_file_has_group(keyfile, section))
return TRUE;

for (int i = 0; i < NB_KEYS; i++) {
keyboard_cfg[i] = g_key_file_get_integer(keyfile, "KEYS", key_names[i], &error);
keyboard_cfg[i] = g_key_file_get_integer(keyfile, section, key_names[i], &error);
if (error != NULL) {
g_error_free(error);
return FALSE;
Expand Down
12 changes: 9 additions & 3 deletions desmume/src/frontend/posix/shared/desmume_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
#ifndef _DESMUME_GTK_CONFIG
#define _DESMUME_GTK_CONFIG

GKeyFile *desmume_config_read_file(const u32 *);
GKeyFile *desmume_config_read_file(const u32 *, const char *keysection = "KEYS");
void desmume_config_dispose(GKeyFile *);

gboolean desmume_config_update_keys(GKeyFile*);
/* since GTK uses GDK keysymbols, not SDL2 ones, we need
different sections for cli/gtk frontends.
KEYS = gtk keys,
SDLKEYS = sdl2 keys
*/
gboolean desmume_config_update_keys(GKeyFile*, const char *section = "KEYS");
gboolean desmume_config_read_keys(GKeyFile*, const char *section = "KEYS");

gboolean desmume_config_update_joykeys(GKeyFile*);
gboolean desmume_config_read_keys(GKeyFile*);
gboolean desmume_config_read_joykeys(GKeyFile*);

#endif
Loading

0 comments on commit b45d249

Please sign in to comment.