SDL: Save settings on exit

This commit is contained in:
Ilari Liusvaara 2012-05-14 17:09:32 +03:00
parent 0d8d214e98
commit 1db083990b
5 changed files with 100 additions and 4 deletions

View file

@ -386,7 +386,6 @@ private:
std::map<size_t, page> memory;
};
/**
* Clip range inside another.
*

View file

@ -421,6 +421,8 @@ void notify_emulator_exit();
* The user interface loop. Call in UI thread. notify_emulator_exit() causes this to return.
*/
void ui_loop();
/**
* Save the config.
*/
void lsnes_sdl_save_config();
#endif

View file

@ -1,5 +1,5 @@
ifeq ($(GRAPHICS), SDL)
OBJECTS = commandline.$(OBJECT_SUFFIX) drawprim.$(OBJECT_SUFFIX) graphicsfn.$(OBJECT_SUFFIX) keyboard.$(OBJECT_SUFFIX) main.$(OBJECT_SUFFIX) thread.$(OBJECT_SUFFIX) status.$(OBJECT_SUFFIX)
OBJECTS = commandline.$(OBJECT_SUFFIX) drawprim.$(OBJECT_SUFFIX) graphicsfn.$(OBJECT_SUFFIX) keyboard.$(OBJECT_SUFFIX) main.$(OBJECT_SUFFIX) thread.$(OBJECT_SUFFIX) status.$(OBJECT_SUFFIX) savesettings.$(OBJECT_SUFFIX)
SDL_CFLAGS += $(shell $(CROSS_PREFIX)sdl-config --cflags)
SDL_LDFLAGS += $(shell $(CROSS_PREFIX)sdl-config --libs)
else

View file

@ -232,6 +232,7 @@ int main(int argc, char** argv)
fatal_error();
return 1;
}
lsnes_sdl_save_config();
rrdata::close();
platform::quit();
quit_lua();

View file

@ -0,0 +1,94 @@
#include "core/command.hpp"
#include "core/controller.hpp"
#include "core/dispatch.hpp"
#include "core/framerate.hpp"
#include "lua/lua.hpp"
#include "core/mainloop.hpp"
#include "core/misc.hpp"
#include "core/moviedata.hpp"
#include "core/rom.hpp"
#include "core/rrdata.hpp"
#include "core/settings.hpp"
#include "core/window.hpp"
#include "library/zip.hpp"
namespace
{
void write_configuration(const std::string& cfg)
{
std::ofstream cfgfile(cfg.c_str());
//Joystick axis.
for(auto i : keygroup::get_axis_set()) {
keygroup* k = keygroup::lookup_by_name(i);
auto p = k->get_parameters();
cfgfile << "set-axis " << i << " ";
switch(p.ktype) {
case keygroup::KT_DISABLED: cfgfile << "disabled"; break;
case keygroup::KT_AXIS_PAIR: cfgfile << "axis"; break;
case keygroup::KT_AXIS_PAIR_INVERSE: cfgfile << "axis-inverse"; break;
case keygroup::KT_PRESSURE_M0: cfgfile << "pressure-0"; break;
case keygroup::KT_PRESSURE_MP: cfgfile << "pressure-+"; break;
case keygroup::KT_PRESSURE_0M: cfgfile << "pressure0-"; break;
case keygroup::KT_PRESSURE_0P: cfgfile << "pressure0+"; break;
case keygroup::KT_PRESSURE_PM: cfgfile << "pressure+-"; break;
case keygroup::KT_PRESSURE_P0: cfgfile << "pressure+0"; break;
};
cfgfile << " minus=" << p.cal_left << " zero=" << p.cal_center << " plus=" << p.cal_right
<< " tolerance=" << p.cal_tolerance << std::endl;
}
//Settings.
for(auto i : setting::get_settings_set()) {
if(!setting::is_set(i))
cfgfile << "unset-setting " << i << std::endl;
else
cfgfile << "set-setting " << i << " " << setting::get(i) << std::endl;
}
//Aliases.
for(auto i : command::get_aliases()) {
std::string old_alias_value = command::get_alias_for(i);
while(old_alias_value != "") {
std::string aliasline;
size_t s = old_alias_value.find_first_of("\n");
if(s < old_alias_value.length()) {
aliasline = old_alias_value.substr(0, s);
old_alias_value = old_alias_value.substr(s + 1);
} else {
aliasline = old_alias_value;
old_alias_value = "";
}
cfgfile << "alias-command " << i << " " << aliasline << std::endl;
}
}
//Keybindings.
for(auto i : keymapper::get_bindings()) {
std::string i2 = i;
size_t s = i2.find_first_of("|");
size_t s2 = i2.find_first_of("/");
if(s > i2.length() || s2 > s)
continue;
std::string key = i2.substr(s + 1);
std::string mod = i2.substr(0, s2);
std::string modspec = i2.substr(s2 + 1, s - s2 - 1);
std::string old_command_value = keymapper::get_command_for(i);
if(mod != "" || modspec != "")
cfgfile << "bind-key " << mod << "/" << modspec << " " << key << " "
<< old_command_value << std::endl;
else
cfgfile << "bind-key " << key << " " << old_command_value << std::endl;
}
}
}
void lsnes_sdl_save_config()
{
std::string cfg = get_config_path() + "/lsnes.rc";
std::string cfgn = cfg + ".new";
write_configuration(cfg + ".new");
#if defined(_WIN32) || defined(_WIN64) || defined(TEST_WIN32_CODE)
//Grumble, Windows seemingly can't do this atomically.
remove(cfg.c_str());
#endif
rename(cfgn.c_str(), cfg.c_str());
}