From: Toni Wilen Date: Sun, 4 Feb 2018 13:51:57 +0000 (+0200) Subject: Config file copy helper routines. X-Git-Tag: 4000~204 X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=4cd140e3e509f9ef9858cc47702a9dff4d8cfe46;p=francis%2Fwinuae.git Config file copy helper routines. --- diff --git a/cfgfile.cpp b/cfgfile.cpp index ab45a000..c4c5c8b2 100644 --- a/cfgfile.cpp +++ b/cfgfile.cpp @@ -505,7 +505,7 @@ static TCHAR *cfgfile_escape (const TCHAR *s, const TCHAR *escstr, bool quote) } if (escstr == NULL && quote) doquote = true; - TCHAR *s2 = xmalloc (TCHAR, _tcslen (s) + cnt * 4 + 1); + TCHAR *s2 = xmalloc (TCHAR, _tcslen (s) + cnt * 4 + 2 + 1); TCHAR *p = s2; if (doquote) *p++ = '\"'; @@ -7145,6 +7145,57 @@ static void default_prefs_mini (struct uae_prefs *p, int type) #include "sounddep/sound.h" +static void copy_inputdevice_settings(struct uae_input_device *src, struct uae_input_device *dst) +{ + for (int l = 0; l < MAX_INPUT_DEVICE_EVENTS; l++) { + for (int i = 0; i < MAX_INPUT_SUB_EVENT_ALL; i++) { + if (src->custom[l][i]) { + dst->custom[l][i] = my_strdup(src->custom[l][i]); + } else { + dst->custom[l][i] = NULL; + } + } + } +} + +static void copy_inputdevice_settings_free(struct uae_input_device *src, struct uae_input_device *dst) +{ + for (int l = 0; l < MAX_INPUT_DEVICE_EVENTS; l++) { + for (int i = 0; i < MAX_INPUT_SUB_EVENT_ALL; i++) { + if (dst->custom[l][i] == NULL && src->custom[l][i] == NULL) + continue; + // same string in both src and dest: separate them (fixme: this shouldn't happen!) + if (dst->custom[l][i] == src->custom[l][i]) { + dst->custom[l][i] = NULL; + } else { + if (dst->custom[l][i]) { + xfree(dst->custom[l][i]); + dst->custom[l][i] = NULL; + } + } + } + } +} + +void copy_prefs(struct uae_prefs *src, struct uae_prefs *dst) +{ + for (int slot = 0; slot < MAX_INPUT_SETTINGS; slot++) { + for (int m = 0; m < MAX_INPUT_DEVICES; m++) { + copy_inputdevice_settings_free(&src->joystick_settings[slot][m], &dst->joystick_settings[slot][m]); + copy_inputdevice_settings_free(&src->mouse_settings[slot][m], &dst->mouse_settings[slot][m]); + copy_inputdevice_settings_free(&src->keyboard_settings[slot][m], &dst->keyboard_settings[slot][m]); + } + } + memcpy(dst, src, sizeof(struct uae_prefs)); + for (int slot = 0; slot < MAX_INPUT_SETTINGS; slot++) { + for (int m = 0; m < MAX_INPUT_DEVICES; m++) { + copy_inputdevice_settings(&src->joystick_settings[slot][m], &dst->joystick_settings[slot][m]); + copy_inputdevice_settings(&src->mouse_settings[slot][m], &dst->mouse_settings[slot][m]); + copy_inputdevice_settings(&src->keyboard_settings[slot][m], &dst->keyboard_settings[slot][m]); + } + } +} + void default_prefs (struct uae_prefs *p, bool reset, int type) { int i; diff --git a/include/options.h b/include/options.h index 18c60b85..f8974723 100644 --- a/include/options.h +++ b/include/options.h @@ -860,6 +860,7 @@ extern bool is_error_log (void); extern void default_prefs (struct uae_prefs *, bool, int); extern void discard_prefs (struct uae_prefs *, int); +extern void copy_prefs(struct uae_prefs *src, struct uae_prefs *dst); int parse_cmdline_option (struct uae_prefs *, TCHAR, const TCHAR*); diff --git a/main.cpp b/main.cpp index c1c20e6a..1d6e8c0e 100644 --- a/main.cpp +++ b/main.cpp @@ -1050,7 +1050,7 @@ static int real_main2 (int argc, TCHAR **argv) if (restart_config[0]) parse_cmdline_and_init_file (argc, argv); else - currprefs = changed_prefs; + copy_prefs(&changed_prefs, &currprefs); if (!machdep_init ()) { restart_program = 0; @@ -1068,7 +1068,8 @@ static int real_main2 (int argc, TCHAR **argv) } inputdevice_init (); - changed_prefs = currprefs; + copy_prefs(&currprefs, &changed_prefs); + no_gui = ! currprefs.start_gui; if (restart_program == 2) no_gui = 1; @@ -1077,7 +1078,7 @@ static int real_main2 (int argc, TCHAR **argv) restart_program = 0; if (! no_gui) { int err = gui_init (); - currprefs = changed_prefs; + copy_prefs(&changed_prefs, &currprefs); set_config_changed (); if (err == -1) { write_log (_T("Failed to initialize the GUI\n")); @@ -1120,7 +1121,7 @@ static int real_main2 (int argc, TCHAR **argv) #ifdef RETROPLATFORM rp_fixup_options (&currprefs); #endif - changed_prefs = currprefs; + copy_prefs(&currprefs, &changed_prefs); target_run (); /* force sound settings change */ currprefs.produce_sound = 0; @@ -1174,7 +1175,7 @@ void real_main (int argc, TCHAR **argv) while (restart_program) { int ret; - changed_prefs = currprefs; + copy_prefs(&currprefs, &changed_prefs); ret = real_main2 (argc, argv); if (ret == 0 && quit_to_gui) restart_program = 1; diff --git a/od-win32/win32gui.cpp b/od-win32/win32gui.cpp index f4a649fc..80f68e65 100644 --- a/od-win32/win32gui.cpp +++ b/od-win32/win32gui.cpp @@ -2292,7 +2292,7 @@ static void prefs_to_gui (struct uae_prefs *p) { int st = savestate_state; default_prefs(&workprefs, false, 0); - workprefs = *p; + copy_prefs(p, &workprefs); /* filesys hack */ workprefs.mountitems = currprefs.mountitems; memcpy (&workprefs.mountconfig, &currprefs.mountconfig, MOUNT_CONFIG_SIZE * sizeof (struct uaedev_config_info)); @@ -2304,7 +2304,7 @@ static void prefs_to_gui (struct uae_prefs *p) static void gui_to_prefs (void) { /* Always copy our prefs to changed_prefs, ... */ - changed_prefs = workprefs; + copy_prefs(&workprefs, &changed_prefs); /* filesys hack */ currprefs.mountitems = changed_prefs.mountitems; memcpy (&currprefs.mountconfig, &changed_prefs.mountconfig, MOUNT_CONFIG_SIZE * sizeof (struct uaedev_config_info));