Make wxwidgets gui load and save various settings

This commit is contained in:
Ilari Liusvaara 2011-11-08 23:59:10 +02:00
parent 348d597543
commit 44d8822af2
3 changed files with 175 additions and 0 deletions

View file

@ -55,4 +55,7 @@ private:
wxButton* close;
};
void load_settings();
void save_settings();
#endif

View file

@ -567,6 +567,7 @@ namespace
{
try {
struct emulator_boot_state* boot_state = reinterpret_cast<struct emulator_boot_state*>(state);
load_settings();
main_loop(*boot_state->rom, *boot_state->movie);
} catch(std::bad_alloc& e) {
OOM_panic();
@ -642,6 +643,7 @@ loop:
emu_cr->resume();
if(emu_cr->is_dead()) {
//Bye!
save_settings();
if(window1)
window1->Destroy();
if(window2)

View file

@ -1,9 +1,13 @@
#include "core/command.hpp"
#include "core/misc.hpp"
#include "core/moviefile.hpp"
#include "core/settings.hpp"
#include "core/window.hpp"
#include "plat-wxwidgets/settingseditor.hpp"
#include "plat-wxwidgets/common.hpp"
#include <fstream>
#include <stdexcept>
wx_settings_editor_setting::wx_settings_editor_setting(wxSizer* sizer, wxWindow* window, const std::string& name)
@ -148,3 +152,169 @@ void wx_settings_editor_listener::on_setting_clear(const std::string& _setting)
{
editor->clear_setting(_setting);
}
namespace
{
void load_settings(std::istream& in)
{
try {
std::string l;
while(std::getline(in, l)) {
if(l.length() > 0 && l[l.length() - 1] == '\r')
l = l.substr(0, l.length() - 1);
command::invokeC(l);
}
} catch(std::exception& e) {
messages << "Error loading config file: " << e.what();
}
}
void save_settings(std::ostream& out)
{
//Axes
try {
std::set<std::string> axisnames = keygroup::get_axis_set();
for(auto i : axisnames) {
try {
keygroup* k = keygroup::lookup_by_name(i);
std::ostringstream str;
str << "axismode " << i << " ";
enum keygroup::type ctype = k->get_parameters().ktype;
int16_t low = k->get_parameters().cal_left;
int16_t mid = k->get_parameters().cal_center;
int16_t high = k->get_parameters().cal_right;
double tol = k->get_parameters().cal_tolerance;
switch(ctype) {
case keygroup::KT_DISABLED:
str << "disabled ";
break;
case keygroup::KT_AXIS_PAIR:
str << "axis ";
break;
case keygroup::KT_AXIS_PAIR_INVERSE:
str << "axis-inverse ";
break;
case keygroup::KT_PRESSURE_0M:
str << "pressure0- ";
break;
case keygroup::KT_PRESSURE_0P:
str << "pressure0+ ";
break;
case keygroup::KT_PRESSURE_M0:
str << "pressure-0 ";
break;
case keygroup::KT_PRESSURE_MP:
str << "pressure-+ ";
break;
case keygroup::KT_PRESSURE_P0:
str << "pressure+0 ";
break;
case keygroup::KT_PRESSURE_PM:
str << "pressure+- ";
break;
};
str << "minus=" << low << " zero=" << mid << " plus=" << high;
out << str.str() << std::endl;
} catch(std::exception& e) {
messages << "Error saving axis " << i << ": " << e.what();
}
}
} catch(std::exception& e) {
messages << "Error saving axes: " << e.what();
}
//Aliases.
try {
std::set<std::string> bind = command::get_aliases();
for(auto i : bind) {
try {
std::string old_alias_value = command::get_alias_for(i);
size_t itr = 0;
while(itr < old_alias_value.length()) {
size_t nsplit = old_alias_value.find_first_of("\n", itr);
if(nsplit >= old_alias_value.length()) {
out << "alias-command " << i << " " << old_alias_value.substr(
itr) << std::endl;
itr = old_alias_value.length();
} else {
out << "alias-command " << i << " " << old_alias_value.substr(
itr, nsplit - itr) << std::endl;
itr = nsplit + 1;
}
}
} catch(std::exception& e) {
messages << "Error saving alias " << i << ": " << e.what();
}
}
} catch(std::exception& e) {
messages << "Error saving aliases: " << e.what();
}
//Keybindings
try {
std::set<std::string> bind = keymapper::get_bindings();
for(auto i : bind) {
try {
std::ostringstream str;
str << "bind-key ";
std::string command = keymapper::get_command_for(i);
std::string keyspec = i;
size_t split1 = keyspec.find_first_of("/");
size_t split2 = keyspec.find_first_of("|");
if(split1 >= keyspec.length() || split2 >= keyspec.length() || split1 > split2)
throw std::runtime_error("Bad keyspec " + keyspec);
std::string a = keyspec.substr(0, split1);
std::string b = keyspec.substr(split1 + 1, split2 - split1 - 1);
std::string c = keyspec.substr(split2 + 1);
if(a != "" || b != "")
str << a << "/" << b << " ";
str << c << " " << command;
out << str.str() << std::endl;
} catch(std::exception& e) {
messages << "Error saving keybinding " << i << ": " << e.what();
}
}
} catch(std::exception& e) {
messages << "Error saving keybindings: " << e.what();
}
//Settings
try {
std::set<std::string> bind = setting::get_settings_set();
for(auto i : bind) {
try {
if(setting::is_set(i))
out << "set-setting " << i << " " << setting::get(i) << std::endl;
else
out << "unset-setting " << i << std::endl;
} catch(std::exception& e) {
messages << "Error saving setting " << i << ": " << e.what();
}
}
} catch(std::exception& e) {
messages << "Error saving settings: " << e.what();
}
}
}
void load_settings()
{
std::string cfgpath = get_config_path();
std::ifstream cfg(cfgpath + "/lsneswx.rc");
if(!cfg) {
messages << "Can't load settings file!" << std::endl;
return;
}
load_settings(cfg);
cfg.close();
}
void save_settings()
{
std::string cfgpath = get_config_path();
std::ofstream cfg(cfgpath + "/lsneswx.rc");
if(!cfg) {
messages << "Can't save settings!" << std::endl;
return;
}
save_settings(cfg);
cfg.close();
}