Rework the way config file works in gtk frontend. From a write once on exit

we are moving to write only what changed when is changed model which i think
is more correct.
This commit is contained in:
riccardom 2009-05-12 19:05:56 +00:00
parent d33cfa55a5
commit 2b574440ab
3 changed files with 161 additions and 89 deletions

View File

@ -16,7 +16,8 @@ desmume_SOURCES = \
../sndsdl.cpp \
../ctrlssdl.h ../ctrlssdl.cpp \
osmesa_3Demu.cpp osmesa_3Demu.h \
main.cpp
desmume_config.cpp \
main.cpp
desmume_LDADD = ../libdesmume.a \
$(SDL_LIBS) $(GTK_LIBS) $(GTHREAD_LIBS)
if HAVE_GDB_STUB

View File

@ -0,0 +1,151 @@
/* main.c - this file is part of DeSmuME
*
* Copyright (C) 2009 DeSmuME Team
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <glib.h>
#include <gdk/gdkkeysyms.h>
#include "ctrlssdl.h"
#include "desmume_config.h"
static const gchar *desmume_config_file = ".desmume.ini";
static const u16 gtk_kb_cfg[NB_KEYS] = {
GDK_x, // A
GDK_z, // B
GDK_Shift_R, // select
GDK_Return, // start
GDK_Right, // Right
GDK_Left, // Left
GDK_Up, // Up
GDK_Down, // Down
GDK_w, // R
GDK_q, // L
GDK_s, // X
GDK_a, // Y
GDK_p, // DEBUG
GDK_o // BOOST
};
GKeyFile *desmume_config_read_file()
{
gchar *config_file;
GKeyFile *keyfile;
GError *error = NULL;
gboolean ret;
config_file = g_build_filename(g_get_home_dir(), desmume_config_file, NULL);
keyfile = g_key_file_new();
ret = g_key_file_load_from_file(keyfile, config_file, G_KEY_FILE_NONE, &error);
if (!ret) {
g_error_free(error);
}
g_free(config_file);
desmume_config_read_keys(keyfile);
desmume_config_read_joykeys(keyfile);
return keyfile;
}
void desmume_config_dispose(GKeyFile *keyfile)
{
g_key_file_free(keyfile);
}
static gboolean desmume_config_write_file(GKeyFile *keyfile)
{
gchar *config_file;
gchar *data;
GError *error = NULL;
gsize length;
gboolean ret = TRUE;
config_file = g_build_filename(g_get_home_dir(), desmume_config_file, NULL);
data = g_key_file_to_data(keyfile, &length, NULL);
ret = g_file_set_contents(config_file, data, length, &error);
if (!ret) {
g_error_free(error);
}
g_free(config_file);
g_free(data);
return ret;
}
gboolean desmume_config_update_keys(GKeyFile *keyfile)
{
for(int i = 0; i < NB_KEYS; i++) {
g_key_file_set_integer(keyfile, "KEYS", key_names[i], keyboard_cfg[i]);
}
return desmume_config_write_file(keyfile);
}
gboolean desmume_config_update_joykeys(GKeyFile *keyfile)
{
for(int i = 0; i < NB_KEYS; i++) {
g_key_file_set_integer(keyfile, "JOYKEYS", key_names[i], joypad_cfg[i]);
}
return desmume_config_write_file(keyfile);
}
gboolean desmume_config_read_keys(GKeyFile *keyfile)
{
GError *error = NULL;
if (!g_key_file_has_group(keyfile, "KEYS")) {
for (int i = 0; i < NB_KEYS; i++) {
keyboard_cfg[i] = gtk_kb_cfg[i];
}
desmume_config_update_keys(keyfile);
return TRUE;
}
for (int i = 0; i < NB_KEYS; i++) {
keyboard_cfg[i] = g_key_file_get_integer(keyfile, "KEYS", key_names[i], &error);
if (error != NULL) {
g_error_free(error);
return FALSE;
}
}
return TRUE;
}
gboolean desmume_config_read_joykeys(GKeyFile *keyfile)
{
GError *error = NULL;
if (!g_key_file_has_group(keyfile, "KEYS"))
return FALSE;
for (int i = 0; i < NB_KEYS; i++) {
joypad_cfg[i] = g_key_file_get_integer(keyfile, "JOYKEYS", key_names[i], &error);
if (error != NULL) {
g_error_free(error);
return FALSE;
}
}
return TRUE;
}

View File

@ -1,6 +1,6 @@
/* main.c - this file is part of DeSmuME
*
* Copyright (C) 2006,2007 DeSmuME Team
* Copyright (C) 2006-2009 DeSmuME Team
* Copyright (C) 2007 Pascal Giard (evilynux)
*
* This file is free software; you can redistribute it and/or modify
@ -43,6 +43,7 @@
#include "saves.h"
#include "mic.h"
#include "dTool.h"
#include "desmume_config.h"
#ifdef GDB_STUB
#include "gdbstub.h"
@ -72,24 +73,6 @@ static const char *bad_glob_cflash_disk_image_file;
static SDL_sem *fps_limiter_semaphore;
static int gtk_fps_limiter_disabled;
const u16 gtk_kb_cfg[NB_KEYS] =
{
GDK_x, // A
GDK_z, // B
GDK_Shift_R, // select
GDK_Return, // start
GDK_Right, // Right
GDK_Left, // Left
GDK_Up, // Up
GDK_Down, // Down
GDK_w, // R
GDK_q, // L
GDK_s, // X
GDK_a, // Y
GDK_p, // DEBUG
GDK_o // BOOST
};
enum {
MAIN_BG_0 = 0,
MAIN_BG_1,
@ -326,6 +309,8 @@ GPU3DInterface *core3DList[] = {
#endif
};
GKeyFile *keyfile;
struct modify_key_ctx {
gint mk_key_chosen;
GtkWidget *label;
@ -509,70 +494,6 @@ joinThread_gdb( void *thread_handle) {
}
#endif
static int Write_ConfigFile(const gchar *config_file)
{
int i;
GKeyFile * keyfile;
gchar *contents;
gboolean ret;
keyfile = g_key_file_new();
for(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, "JOYKEYS", key_names[i], joypad_cfg[i]);
}
contents = g_key_file_to_data(keyfile, 0, 0);
ret = g_file_set_contents(config_file, contents, -1, NULL);
if (!ret)
g_printerr("Failed to write to %s\n", config_file);
g_free (contents);
g_key_file_free(keyfile);
return 0;
}
static int Read_ConfigFile(const gchar *config_file)
{
int i, tmp;
GKeyFile * keyfile = g_key_file_new();
GError * error = NULL;
load_default_config(gtk_kb_cfg);
g_key_file_load_from_file(keyfile, config_file, G_KEY_FILE_NONE, 0);
/* Load keyboard keys */
for(i = 0; i < NB_KEYS; i++) {
tmp = g_key_file_get_integer(keyfile, "KEYS", key_names[i], &error);
if (error != NULL) {
g_error_free(error);
error = NULL;
} else {
keyboard_cfg[i] = tmp;
}
}
/* Load joystick keys */
for(i = 0; i < NB_KEYS; i++) {
tmp = g_key_file_get_integer(keyfile, "JOYKEYS", key_names[i], &error);
if (error != NULL) {
g_error_free(error);
error = NULL;
} else {
joypad_cfg[i] = tmp;
}
}
g_key_file_free(keyfile);
return 0;
}
/************************ GTK *******************************/
uint Frameskip = 0;
@ -1209,6 +1130,7 @@ 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);
case GTK_RESPONSE_CANCEL:
case GTK_RESPONSE_NONE:
break;
@ -1600,11 +1522,11 @@ static gboolean timeout_exit_cb(gpointer data)
return FALSE;
}
static int
common_gtk_main( struct configured_features *my_config)
{
SDL_TimerID limiter_timer = NULL;
gchar *config_file;
GtkAccelGroup * accel_group;
GtkWidget *pVBox;
@ -1694,8 +1616,7 @@ common_gtk_main( struct configured_features *my_config)
if (dTools_running != NULL)
memset(dTools_running, FALSE, sizeof(BOOL) * dTools_list_size);
config_file = g_build_filename(g_get_home_dir(), ".desmume.ini", NULL);
Read_ConfigFile(config_file);
keyfile = desmume_config_read_file();
/* Create the window */
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@ -1861,8 +1782,7 @@ common_gtk_main( struct configured_features *my_config)
SDL_Quit();
Write_ConfigFile(config_file);
g_free(config_file);
desmume_config_dispose(keyfile);
#ifdef GDB_STUB
if ( my_config->arm9_gdb_port != 0) {