00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "global.hpp"
00022
00023 #define GETTEXT_DOMAIN "wesnoth-lib"
00024
00025 #include "config.hpp"
00026 #include "filesystem.hpp"
00027 #include "hotkeys.hpp"
00028 #include "log.hpp"
00029 #include "preferences.hpp"
00030 #include "sound.hpp"
00031 #include "video.hpp"
00032 #include "serialization/parser.hpp"
00033 #include "util.hpp"
00034
00035 #include <sys/stat.h>
00036 #include <boost/concept_check.hpp>
00037
00038 static lg::log_domain log_filesystem("filesystem");
00039 #define ERR_FS LOG_STREAM(err, log_filesystem)
00040
00041 namespace {
00042
00043 bool color_cursors = false;
00044
00045 bool no_preferences_save = false;
00046
00047 bool fps = false;
00048
00049 int draw_delay_ = 20;
00050
00051 config prefs;
00052 }
00053
00054 namespace preferences {
00055
00056 base_manager::base_manager()
00057 {
00058 #ifdef DEFAULT_PREFS_PATH
00059 scoped_istream stream = istream_file(get_default_prefs_file());
00060 read(prefs, *stream);
00061
00062 config user_prefs;
00063 stream = istream_file(get_prefs_file());
00064 read(user_prefs, *stream);
00065
00066 prefs.merge_with(user_prefs);
00067 #else
00068 scoped_istream stream = istream_file(get_prefs_file());
00069 read(prefs, *stream);
00070 #endif
00071 }
00072
00073 base_manager::~base_manager()
00074 {
00075 if (no_preferences_save) return;
00076
00077
00078 prefs["scroll_threshold"] = mouse_scroll_threshold();
00079
00080 write_preferences();
00081 }
00082
00083 void write_preferences()
00084 {
00085 #ifndef _WIN32
00086
00087 bool prefs_file_existed = access(get_prefs_file().c_str(), F_OK) == 0;
00088
00089 #endif
00090
00091 try {
00092 scoped_ostream prefs_file = ostream_file(get_prefs_file());
00093 write(*prefs_file, prefs);
00094 } catch(io_exception&) {
00095 ERR_FS << "error writing to preferences file '" << get_prefs_file() << "'\n";
00096 }
00097
00098
00099 #ifndef _WIN32
00100
00101 if(!prefs_file_existed) {
00102
00103 if(chmod(get_prefs_file().c_str(), 0600) == -1) {
00104 ERR_FS << "error setting permissions of preferences file '" << get_prefs_file() << "'\n";
00105 }
00106
00107 }
00108
00109 #endif
00110
00111
00112 }
00113
00114 void set(const std::string &key, bool value)
00115 {
00116 prefs[key] = value;
00117 }
00118
00119 void set(const std::string &key, int value)
00120 {
00121 prefs[key] = value;
00122 }
00123
00124 void set(const std::string &key, char const *value)
00125 {
00126 prefs[key] = value;
00127 }
00128
00129 void set(const std::string &key, const std::string &value)
00130 {
00131 prefs[key] = value;
00132 }
00133
00134 void clear(const std::string& key)
00135 {
00136 prefs.recursive_clear_value(key);
00137 }
00138
00139 void set_child(const std::string& key, const config& val) {
00140 prefs.clear_children(key);
00141 prefs.add_child(key, val);
00142 }
00143
00144 const config &get_child(const std::string& key)
00145 {
00146 return prefs.child(key);
00147 }
00148
00149 void erase(const std::string& key) {
00150 prefs.remove_attribute(key);
00151 }
00152
00153 bool have_setting(const std::string& key) {
00154 return prefs.has_attribute(key);
00155 }
00156
00157 std::string get(const std::string& key) {
00158 return prefs[key];
00159 }
00160
00161 bool get(const std::string &key, bool def)
00162 {
00163 return prefs[key].to_bool(def);
00164 }
00165
00166 void disable_preferences_save() {
00167 no_preferences_save = true;
00168 }
00169
00170 config* get_prefs(){
00171 config* pointer = &prefs;
00172 return pointer;
00173 }
00174
00175 bool fullscreen()
00176 {
00177 return get("fullscreen", false);
00178 }
00179
00180 void _set_fullscreen(bool ison)
00181 {
00182 prefs["fullscreen"] = ison;
00183 }
00184
00185 bool scroll_to_action()
00186 {
00187 return get("scroll_to_action", true);
00188 }
00189
00190 void _set_scroll_to_action(bool ison)
00191 {
00192 prefs["scroll_to_action"] = ison;
00193 }
00194
00195 int min_allowed_width()
00196 {
00197 return 800;
00198 }
00199
00200 int min_allowed_height()
00201 {
00202 return 480;
00203 }
00204
00205 std::pair<int,int> resolution()
00206 {
00207 const std::string postfix = fullscreen() ? "resolution" : "windowsize";
00208 std::string x = prefs['x' + postfix], y = prefs['y' + postfix];
00209 if (!x.empty() && !y.empty()) {
00210 std::pair<int,int> res(std::max(atoi(x.c_str()), min_allowed_width()),
00211 std::max(atoi(y.c_str()), min_allowed_height()));
00212
00213
00214
00215
00216 return res;
00217 } else {
00218 return std::pair<int,int>(1024,768);
00219 }
00220 }
00221
00222 bool turbo()
00223 {
00224 if(non_interactive()) {
00225 return true;
00226 }
00227
00228 return get("turbo", false);
00229 }
00230
00231 void _set_turbo(bool ison)
00232 {
00233 prefs["turbo"] = ison;
00234 }
00235
00236 double turbo_speed()
00237 {
00238 return prefs["turbo_speed"].to_double(2.0);
00239 }
00240
00241 void save_turbo_speed(const double speed)
00242 {
00243 prefs["turbo_speed"] = speed;
00244 }
00245
00246 bool idle_anim()
00247 {
00248 return get("idle_anim", true);
00249 }
00250
00251 void _set_idle_anim(const bool ison)
00252 {
00253 prefs["idle_anim"] = ison;
00254 }
00255
00256 int idle_anim_rate()
00257 {
00258 return prefs["idle_anim_rate"];
00259 }
00260
00261 void _set_idle_anim_rate(const int rate)
00262 {
00263 prefs["idle_anim_rate"] = rate;
00264 }
00265
00266 std::string language()
00267 {
00268 return prefs["locale"];
00269 }
00270
00271 void set_language(const std::string& s)
00272 {
00273 preferences::set("locale", s);
00274 }
00275
00276 bool ellipses()
00277 {
00278 return get("show_side_colors", false);
00279 }
00280
00281 void _set_ellipses(bool ison)
00282 {
00283 preferences::set("show_side_colors", ison);
00284 }
00285
00286 bool grid()
00287 {
00288 return get("grid", false);
00289 }
00290
00291 void _set_grid(bool ison)
00292 {
00293 preferences::set("grid", ison);
00294 }
00295
00296 size_t sound_buffer_size()
00297 {
00298
00299
00300 #ifdef _WIN32
00301 const size_t buf_size = 4096;
00302 #else
00303 const size_t buf_size = 1024;
00304 #endif
00305
00306 return prefs["sound_buffer_size"].to_int(buf_size);
00307 }
00308
00309 void save_sound_buffer_size(const size_t size)
00310 {
00311 #ifdef _WIN32
00312 const char* buf_size = "4096";
00313 #else
00314 const char* buf_size = "1024";
00315 #endif
00316
00317 const std::string new_size = lexical_cast_default<std::string>(size, buf_size);
00318 if (get("sound_buffer_size") == new_size)
00319 return;
00320
00321 preferences::set("sound_buffer_size", new_size);
00322
00323 sound::reset_sound();
00324 }
00325
00326 int music_volume()
00327 {
00328 return prefs["music_volume"].to_int(100);
00329 }
00330
00331 void set_music_volume(int vol)
00332 {
00333 if(music_volume() == vol) {
00334 return;
00335 }
00336
00337 prefs["music_volume"] = vol;
00338 sound::set_music_volume(music_volume());
00339 }
00340
00341 int sound_volume()
00342 {
00343 return prefs["sound_volume"].to_int(100);
00344 }
00345
00346 void set_sound_volume(int vol)
00347 {
00348 if(sound_volume() == vol) {
00349 return;
00350 }
00351
00352 prefs["sound_volume"] = vol;
00353 sound::set_sound_volume(sound_volume());
00354 }
00355
00356 int bell_volume()
00357 {
00358 return prefs["bell_volume"].to_int(100);
00359 }
00360
00361 void set_bell_volume(int vol)
00362 {
00363 if(bell_volume() == vol) {
00364 return;
00365 }
00366
00367 prefs["bell_volume"] = vol;
00368 sound::set_bell_volume(bell_volume());
00369 }
00370
00371 int UI_volume()
00372 {
00373 return prefs["UI_volume"].to_int(100);
00374 }
00375
00376 void set_UI_volume(int vol)
00377 {
00378 if(UI_volume() == vol) {
00379 return;
00380 }
00381
00382 prefs["UI_volume"] = vol;
00383 sound::set_UI_volume(UI_volume());
00384 }
00385
00386 bool turn_bell()
00387 {
00388 return get("turn_bell", true);
00389 }
00390
00391 bool set_turn_bell(bool ison)
00392 {
00393 if(!turn_bell() && ison) {
00394 preferences::set("turn_bell", true);
00395 if(!music_on() && !sound_on() && !UI_sound_on()) {
00396 if(!sound::init_sound()) {
00397 preferences::set("turn_bell", false);
00398 return false;
00399 }
00400 }
00401 } else if(turn_bell() && !ison) {
00402 preferences::set("turn_bell", false);
00403 sound::stop_bell();
00404 if(!music_on() && !sound_on() && !UI_sound_on())
00405 sound::close_sound();
00406 }
00407 return true;
00408 }
00409
00410 bool UI_sound_on()
00411 {
00412 return get("UI_sound", true);
00413 }
00414
00415 bool set_UI_sound(bool ison)
00416 {
00417 if(!UI_sound_on() && ison) {
00418 preferences::set("UI_sound", true);
00419 if(!music_on() && !sound_on() && !turn_bell()) {
00420 if(!sound::init_sound()) {
00421 preferences::set("UI_sound", false);
00422 return false;
00423 }
00424 }
00425 } else if(UI_sound_on() && !ison) {
00426 preferences::set("UI_sound", false);
00427 sound::stop_UI_sound();
00428 if(!music_on() && !sound_on() && !turn_bell())
00429 sound::close_sound();
00430 }
00431 return true;
00432 }
00433
00434 bool message_bell()
00435 {
00436 return get("message_bell", true);
00437 }
00438
00439 bool sound_on()
00440 {
00441 return get("sound", true);
00442 }
00443
00444 bool set_sound(bool ison) {
00445 if(!sound_on() && ison) {
00446 preferences::set("sound", true);
00447 if(!music_on() && !turn_bell() && !UI_sound_on()) {
00448 if(!sound::init_sound()) {
00449 preferences::set("sound", false);
00450 return false;
00451 }
00452 }
00453 } else if(sound_on() && !ison) {
00454 preferences::set("sound", false);
00455 sound::stop_sound();
00456 if(!music_on() && !turn_bell() && !UI_sound_on())
00457 sound::close_sound();
00458 }
00459 return true;
00460 }
00461
00462 bool music_on()
00463 {
00464 return get("music", true);
00465 }
00466
00467 bool set_music(bool ison) {
00468 if(!music_on() && ison) {
00469 preferences::set("music", true);
00470 if(!sound_on() && !turn_bell() && !UI_sound_on()) {
00471 if(!sound::init_sound()) {
00472 preferences::set("music", false);
00473 return false;
00474 }
00475 }
00476 else
00477 sound::play_music();
00478 } else if(music_on() && !ison) {
00479 preferences::set("music", false);
00480 if(!sound_on() && !turn_bell() && !UI_sound_on())
00481 sound::close_sound();
00482 else
00483 sound::stop_music();
00484 }
00485 return true;
00486 }
00487
00488 namespace {
00489 double scroll = 0.2;
00490 }
00491
00492 bool joystick_support_enabled()
00493 {
00494 return get("joystick_support_enabled", false);
00495 }
00496
00497 int joystick_mouse_deadzone()
00498 {
00499 const int value = lexical_cast_in_range<int>(get("joystick_scroll_deadzone"), 1500, 0, 16000);
00500 return value;
00501 }
00502
00503 int joystick_num_mouse_xaxis()
00504 {
00505 const int value = lexical_cast_in_range<int>(get("joystick_num_scroll_xaxis"), 0, -1, 3);
00506 return value;
00507 }
00508
00509 int joystick_mouse_xaxis_num()
00510 {
00511 const int value = lexical_cast_in_range<int>(get("joystick_scroll_xaxis_num"), 0, 0, 7);
00512 return value;
00513 }
00514
00515 int joystick_num_mouse_yaxis()
00516 {
00517 const int value = lexical_cast_in_range<int>(get("joystick_num_scroll_yaxis"), 0, -1, 3);
00518 return value;
00519 }
00520
00521 int joystick_mouse_yaxis_num()
00522 {
00523 const int value = lexical_cast_in_range<int>(get("joystick_scroll_yaxis_num"), 1, 0, 7);
00524 return value;
00525 }
00526
00527 int joystick_scroll_deadzone()
00528 {
00529 const int value = lexical_cast_in_range<int>(get("joystick_scroll_deadzone"), 1500, 0, 16000);
00530 return value;
00531 }
00532
00533 int joystick_cursor_deadzone()
00534 {
00535 const int value = lexical_cast_in_range<int>(get("joystick_cursor_deadzone"), 1500, 0, 16000);
00536 return value;
00537 }
00538
00539 int joystick_thrusta_deadzone()
00540 {
00541 const int value = lexical_cast_in_range<int>(get("joystick_thrusta_deadzone"), 1500, 0, 16000);
00542 return value;
00543 }
00544
00545 int joystick_thrustb_deadzone()
00546 {
00547 const int value = lexical_cast_in_range<int>(get("joystick_thrustb_deadzone"), 1500, 0, 16000);
00548 return value;
00549 }
00550
00551 int joystick_cursor_threshold()
00552 {
00553 const int value = lexical_cast_in_range<int>(get("joystick_cursor_threshold"), 10000, 0, 16000);
00554 return value;
00555 }
00556
00557 int joystick_num_scroll_xaxis()
00558 {
00559 const int value = lexical_cast_in_range<int>(get("joystick_num_scroll_xaxis"), 0, -1, 3);
00560 return value;
00561 }
00562
00563 int joystick_scroll_xaxis_num()
00564 {
00565 const int value = lexical_cast_in_range<int>(get("joystick_scroll_xaxis_num"), 0, 0, 7);
00566 return value;
00567 }
00568
00569 int joystick_num_scroll_yaxis()
00570 {
00571 const int value = lexical_cast_in_range<int>(get("joystick_num_scroll_yaxis"), 0, -1, 3);
00572 return value;
00573 }
00574
00575 int joystick_scroll_yaxis_num()
00576 {
00577 const int value = lexical_cast_in_range<int>(get("joystick_scroll_yaxis_num"), 1, 0, 7);
00578 return value;
00579 }
00580
00581 int joystick_num_cursor_xaxis()
00582 {
00583 const int value = lexical_cast_in_range<int>(get("joystick_num_cursor_xaxis"), 0, -1, 3);
00584 return value;
00585 }
00586
00587 int joystick_cursor_xaxis_num()
00588 {
00589 const int value = lexical_cast_in_range<int>(get("joystick_cursor_xaxis_num"), 3, 0, 7);
00590 return value;
00591 }
00592
00593 int joystick_num_cursor_yaxis()
00594 {
00595 const int value = lexical_cast_in_range<int>(get("joystick_num_cursor_yaxis"), 0, -1, 3);
00596 return value;
00597 }
00598
00599 int joystick_cursor_yaxis_num()
00600 {
00601 const int value = lexical_cast_in_range<int>(get("joystick_cursor_yaxis_num"), 4, 0, 7);
00602 return value;
00603 }
00604
00605 int joystick_num_thrusta_axis()
00606 {
00607 const int value = lexical_cast_in_range<int>(get("joystick_num_thrusta_axis"), 0, -1, 3);
00608 return value;
00609 }
00610
00611 int joystick_thrusta_axis_num()
00612 {
00613 const int value = lexical_cast_in_range<int>(get("joystick_thrusta_axis_num"), 2, 0, 7);
00614 return value;
00615 }
00616
00617 int joystick_num_thrustb_axis()
00618 {
00619 const int value = lexical_cast_in_range<int>(get("joystick_num_thrustb_axis"), 0, -1, 3);
00620 return value;
00621 }
00622
00623 int joystick_thrustb_axis_num()
00624 {
00625 const int value = lexical_cast_in_range<int>(get("joystick_thrustb_axis_num"), 2, 0, 7);
00626 return value;
00627 }
00628
00629
00630 int scroll_speed()
00631 {
00632 const int value = lexical_cast_in_range<int>(get("scroll"), 50, 1, 100);
00633 scroll = value/100.0;
00634
00635 return value;
00636 }
00637
00638 void set_scroll_speed(const int new_speed)
00639 {
00640 prefs["scroll"] = new_speed;
00641 scroll = new_speed / 100.0;
00642 }
00643
00644 bool middle_click_scrolls()
00645 {
00646 return get("middle_click_scrolls", true);
00647 }
00648
00649 bool mouse_scroll_enabled()
00650 {
00651 return get("mouse_scrolling", true);
00652 }
00653
00654 void enable_mouse_scroll(bool value)
00655 {
00656 set("mouse_scrolling", value);
00657 }
00658
00659 int mouse_scroll_threshold()
00660 {
00661 return prefs["scroll_threshold"].to_int(10);
00662 }
00663
00664 bool animate_map()
00665 {
00666 return preferences::get("animate_map", true);
00667 }
00668
00669 void set_animate_map(bool value)
00670 {
00671 set("animate_map", value);
00672 }
00673
00674 bool show_standing_animations()
00675 {
00676 return preferences::get("unit_standing_animations", true);
00677 }
00678
00679 void set_show_standing_animations(bool value)
00680 {
00681 set("unit_standing_animations", value);
00682 }
00683
00684 bool show_fps()
00685 {
00686 return fps;
00687 }
00688
00689 void set_show_fps(bool value)
00690 {
00691 fps = value;
00692 }
00693
00694 int draw_delay()
00695 {
00696 return draw_delay_;
00697 }
00698
00699 void set_draw_delay(int value)
00700 {
00701 draw_delay_ = value;
00702 }
00703
00704 bool use_color_cursors()
00705 {
00706 return color_cursors;
00707 }
00708
00709 void _set_color_cursors(bool value)
00710 {
00711 preferences::set("color_cursors", value);
00712 color_cursors = value;
00713 }
00714
00715 void load_hotkeys()
00716 {
00717 hotkey::load_hotkeys(prefs);
00718 }
00719
00720 void save_hotkeys()
00721 {
00722 hotkey::save_hotkeys(prefs);
00723 }
00724
00725 void clear_hotkeys()
00726 {
00727 hotkey::reset_default_hotkeys();
00728 prefs.clear_children("hotkey");
00729 }
00730
00731 void add_alias(const std::string &alias, const std::string &command)
00732 {
00733 config &alias_list = prefs.child_or_add("alias");
00734 alias_list[alias] = command;
00735 }
00736
00737
00738 const config &get_alias()
00739 {
00740 return get_child("alias");
00741 }
00742
00743 unsigned int sample_rate()
00744 {
00745 return prefs["sample_rate"].to_int(44100);
00746 }
00747
00748 void save_sample_rate(const unsigned int rate)
00749 {
00750 if (sample_rate() == rate)
00751 return;
00752
00753 prefs["sample_rate"] = int(rate);
00754
00755
00756 sound::reset_sound();
00757 }
00758
00759 bool confirm_load_save_from_different_version()
00760 {
00761 return get("confirm_load_save_from_different_version", true);
00762 }
00763
00764 bool use_twelve_hour_clock_format()
00765 {
00766 return get("use_twelve_hour_clock_format", false);
00767 }
00768
00769 }
00770