preferences.cpp

Go to the documentation of this file.
00001 /* $Id: preferences.cpp 25891 2008-04-17 16:39:47Z mordante $ */
00002 /*
00003    Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 //! @file preferences.cpp 
00016 //! Get and set user-preferences.
00017 
00018 #include "global.hpp"
00019 
00020 #define GETTEXT_DOMAIN "wesnoth-lib"
00021 
00022 #include "config.hpp"
00023 #include "filesystem.hpp"
00024 #include "gettext.hpp"
00025 #include "hotkeys.hpp"
00026 #include "log.hpp"
00027 #include "preferences.hpp"
00028 #include "sound.hpp"
00029 #include "util.hpp"
00030 #include "video.hpp" // non_interactive()
00031 #include "serialization/parser.hpp"
00032 #include "serialization/string_utils.hpp"
00033 
00034 #include <cstdlib>
00035 #include <iostream>
00036 #include <iterator>
00037 #include <sstream>
00038 
00039 namespace {
00040 
00041 bool colour_cursors = false;
00042 
00043 bool no_preferences_save = false;
00044 
00045 bool fps = false;
00046 
00047 int draw_delay_ = 20;
00048 
00049 config prefs;
00050 }
00051 
00052 namespace preferences {
00053 
00054 base_manager::base_manager()
00055 {
00056     scoped_istream stream = istream_file(get_prefs_file());
00057     read(prefs, *stream);
00058 }
00059 
00060 base_manager::~base_manager()
00061 {
00062     if (no_preferences_save) return;
00063     write_preferences();
00064 }
00065 
00066 void write_preferences()
00067 {
00068     try {
00069         scoped_ostream prefs_file = ostream_file(get_prefs_file());
00070         write(*prefs_file, prefs);
00071     } catch(io_exception&) {
00072         std::cerr << "error writing to preferences file '" << get_prefs_file() << "'\n";
00073     }
00074 
00075 }
00076 
00077 void set(const std::string key, std::string value) {
00078     prefs[key] = value;
00079 }
00080 
00081 void set_child(const std::string& key, const config& val) {
00082     prefs.clear_children(key);
00083     prefs.add_child(key, val);
00084 }
00085 
00086 config* get_child(const std::string& key)
00087 {
00088     return prefs.child(key);
00089 }
00090 
00091 void erase(const std::string key) {
00092     prefs.values.erase(key);
00093 }
00094 
00095 const std::string get(const std::string key) {
00096     return prefs[key];
00097 }
00098 
00099 void disable_preferences_save() {
00100     no_preferences_save = true;
00101 }
00102 
00103 config* get_prefs(){
00104     config* pointer = &prefs;
00105     return pointer;
00106 }
00107 
00108 bool fullscreen()
00109 {
00110     return utils::string_bool(get("fullscreen"), false);
00111 }
00112 
00113 void _set_fullscreen(bool ison)
00114 {
00115     prefs["fullscreen"] = (ison ? "true" : "false");
00116 }
00117 
00118 std::pair<int,int> resolution()
00119 {
00120     const std::string postfix = fullscreen() ? "resolution" : "windowsize";
00121     const string_map::const_iterator x = prefs.values.find('x' + postfix);
00122     const string_map::const_iterator y = prefs.values.find('y' + postfix);
00123     if(x != prefs.values.end() && y != prefs.values.end() &&
00124        x->second.empty() == false && y->second.empty() == false) {
00125         std::pair<int,int> res (maximum(atoi(x->second.c_str()),min_allowed_width),
00126                                 maximum(atoi(y->second.c_str()),min_allowed_height));
00127 
00128         // Make sure resolutions are always divisible by 4
00129         //res.first &= ~3;
00130         //res.second &= ~3;
00131         return res;
00132     } else {
00133         return std::pair<int,int>(1024,768);
00134     }
00135 }
00136 
00137 bool turbo()
00138 {
00139     if(non_interactive()) {
00140         return true;
00141     }
00142 
00143     return  utils::string_bool(get("turbo"), false);
00144 }
00145 
00146 void _set_turbo(bool ison)
00147 {
00148     prefs["turbo"] = (ison ? "true" : "false");
00149 }
00150 
00151 double turbo_speed()
00152 {
00153     return lexical_cast_default<double>(get("turbo_speed"), 2.0);
00154 }
00155 
00156 void save_turbo_speed(const double speed)
00157 {
00158     preferences::set("turbo_speed", lexical_cast<std::string>(speed));
00159 }
00160 
00161 bool idle_anim()
00162 {
00163     return  utils::string_bool(get("idle_anim"), true);
00164 }
00165 
00166 void _set_idle_anim(const bool ison)
00167 {
00168     prefs["idle_anim"] = (ison ? "yes" : "no");
00169 }
00170 
00171 int idle_anim_rate()
00172 {
00173     return lexical_cast_default<int>(get("idle_anim_rate"), 0);
00174 }
00175 
00176 void _set_idle_anim_rate(const int rate)
00177 {
00178     preferences::set("idle_anim_rate", lexical_cast<std::string>(rate));
00179 }
00180 
00181 const std::string& language()
00182 {
00183     return prefs["locale"];
00184 }
00185 
00186 void set_language(const std::string& s)
00187 {
00188     preferences::set("locale", s);
00189 }
00190 
00191 bool adjust_gamma()
00192 {
00193     return utils::string_bool(get("adjust_gamma"), false);
00194 }
00195 
00196 void _set_adjust_gamma(bool val)
00197 {
00198     preferences::set("adjust_gamma", val ? "yes" : "no");
00199 }
00200 
00201 int gamma()
00202 {
00203     static const int default_value = 100;
00204 
00205     if(!adjust_gamma()) {
00206         return default_value;
00207     }
00208 
00209     return lexical_cast_default<int>(get("gamma"), default_value);
00210 }
00211 
00212 void _set_gamma(int gamma)
00213 {
00214     preferences::set("gamma", lexical_cast<std::string>(gamma));
00215 }
00216 
00217 bool grid()
00218 {
00219     return utils::string_bool(get("grid"), false);
00220 }
00221 
00222 void _set_grid(bool ison)
00223 {
00224     preferences::set("grid",  (ison ? "true" : "false"));
00225 }
00226 
00227 size_t sound_buffer_size()
00228 {
00229     // Sounds don't sound good on Windows unless the buffer size is 4k,
00230     // but this seems to cause crashes on other systems...
00231     #ifdef WIN32
00232         const size_t buf_size = 4096;
00233     #else
00234         const size_t buf_size = 1024;
00235     #endif
00236 
00237     return lexical_cast_default<size_t>(get("sound_buffer_size"), buf_size);
00238 }
00239 
00240 void save_sound_buffer_size(const size_t size)
00241 {
00242     #ifdef WIN32
00243         const char* buf_size = "4096";
00244     #else
00245         const char* buf_size = "1024";
00246     #endif
00247 
00248     const std::string new_size = lexical_cast_default<std::string>(size, buf_size);
00249     if (get("sound_buffer_size") == new_size)
00250         return;
00251 
00252     preferences::set("sound_buffer_size", new_size);
00253 
00254     sound::reset_sound();
00255 }
00256 
00257 int music_volume()
00258 {
00259     return lexical_cast_default<int>(get("music_volume"), 100);
00260 }
00261 
00262 void set_music_volume(int vol)
00263 {
00264     if(music_volume() == vol) {
00265         return;
00266     }
00267 
00268     preferences::set("music_volume", lexical_cast_default<std::string>(vol, "100"));
00269     sound::set_music_volume(music_volume());
00270 }
00271 
00272 int sound_volume()
00273 {
00274     return lexical_cast_default<int>(get("sound_volume"), 100);
00275 }
00276 
00277 void set_sound_volume(int vol)
00278 {
00279     if(sound_volume() == vol) {
00280         return;
00281     }
00282 
00283     preferences::set("sound_volume", lexical_cast_default<std::string>(vol, "100"));
00284     sound::set_sound_volume(sound_volume());
00285 }
00286 
00287 int bell_volume()
00288 {
00289     return lexical_cast_default<int>(get("bell_volume"), 100);
00290 }
00291 
00292 void set_bell_volume(int vol)
00293 {
00294     if(bell_volume() == vol) {
00295         return;
00296     }
00297 
00298     preferences::set("bell_volume", lexical_cast_default<std::string>(vol, "100"));
00299     sound::set_bell_volume(bell_volume());
00300 }
00301 
00302 int UI_volume()
00303 {
00304     return lexical_cast_default<int>(get("UI_volume"), 100);
00305 }
00306 
00307 void set_UI_volume(int vol)
00308 {
00309     if(UI_volume() == vol) {
00310         return;
00311     }
00312 
00313     preferences::set("UI_volume", lexical_cast_default<std::string>(vol, "100"));
00314     sound::set_UI_volume(UI_volume());
00315 }
00316 
00317 bool turn_bell()
00318 {
00319     return utils::string_bool(get("turn_bell"), true);
00320 }
00321 
00322 bool set_turn_bell(bool ison)
00323 {
00324     if(!turn_bell() && ison) {
00325         preferences::set("turn_bell", "yes");
00326         if(!music_on() && !sound_on() && !UI_sound_on()) {
00327             if(!sound::init_sound()) {
00328                 preferences::set("turn_bell", "no");
00329                 return false;
00330             }
00331         }
00332     } else if(turn_bell() && !ison) {
00333         preferences::set("turn_bell", "no");
00334         sound::stop_bell();
00335         if(!music_on() && !sound_on() && !UI_sound_on())
00336             sound::close_sound();
00337     }
00338     return true;
00339 }
00340 
00341 bool UI_sound_on()
00342 {
00343     return utils::string_bool(get("UI_sound"), true);
00344 }
00345 
00346 bool set_UI_sound(bool ison)
00347 {
00348     if(!UI_sound_on() && ison) {
00349         preferences::set("UI_sound", "yes");
00350         if(!music_on() && !sound_on() && !turn_bell()) {
00351             if(!sound::init_sound()) {
00352                 preferences::set("UI_sound", "no");
00353                 return false;
00354             }
00355         }
00356     } else if(UI_sound_on() && !ison) {
00357         preferences::set("UI_sound", "no");
00358         sound::stop_UI_sound();
00359         if(!music_on() && !sound_on() && !turn_bell())
00360             sound::close_sound();
00361     }
00362     return true;
00363 }
00364 
00365 bool message_bell()
00366 {
00367     return utils::string_bool(get("message_bell"), true);
00368 }
00369 
00370 bool sound_on()
00371 {
00372     return utils::string_bool(get("sound"), true);
00373 }
00374 
00375 bool set_sound(bool ison) {
00376     if(!sound_on() && ison) {
00377         preferences::set("sound", "yes");
00378         if(!music_on() && !turn_bell() && !UI_sound_on()) {
00379             if(!sound::init_sound()) {
00380                 preferences::set("sound", "no");
00381                 return false;
00382             }
00383         }
00384     } else if(sound_on() && !ison) {
00385         preferences::set("sound", "no");
00386         sound::stop_sound();
00387         if(!music_on() && !turn_bell() && !UI_sound_on())
00388             sound::close_sound();
00389     }
00390     return true;
00391 }
00392 
00393 bool music_on()
00394 {
00395     return utils::string_bool(get("music"), true);
00396 }
00397 
00398 bool set_music(bool ison) {
00399     if(!music_on() && ison) {
00400         preferences::set("music", "yes");
00401         if(!sound_on() && !turn_bell() && !UI_sound_on()) {
00402             if(!sound::init_sound()) {
00403                 preferences::set("music", "no");
00404                 return false;
00405             }
00406         }
00407         else
00408             sound::play_music();
00409     } else if(music_on() && !ison) {
00410         preferences::set("music", "no");
00411         if(!sound_on() && !turn_bell() && !UI_sound_on())
00412             sound::close_sound();
00413         else
00414             sound::stop_music();
00415     }
00416     return true;
00417 }
00418 
00419 namespace {
00420     double scroll = 0.2;
00421     
00422     void normalize_editor_rgb(int rval)
00423     {
00424         if (rval < -255) {
00425             rval = -255;
00426         }
00427         else if (rval > 255) {
00428             rval = 255;
00429         }
00430     }
00431 }
00432 
00433 void set_editor_r(int value)
00434 {
00435     normalize_editor_rgb(value);
00436     prefs["editor_r"] = lexical_cast<std::string>(value);
00437 }
00438 
00439 void set_editor_g(int value)
00440 {
00441     normalize_editor_rgb(value);
00442     prefs["editor_g"] = lexical_cast<std::string>(value);
00443 }
00444 
00445 void set_editor_b(int value)
00446 {
00447     normalize_editor_rgb(value);
00448     prefs["editor_b"] = lexical_cast<std::string>(value);
00449 }
00450 
00451 int editor_r(void)
00452 {
00453     return lexical_cast_in_range<int>(get("editor_r"), 0, -255, 255);
00454 }
00455 
00456 int editor_g(void)
00457 {
00458     return lexical_cast_in_range<int>(get("editor_g"), 0, -255, 255);
00459 }
00460 
00461 int editor_b(void)
00462 {
00463     return lexical_cast_in_range<int>(get("editor_b"), 0, -255, 255);
00464 }
00465 
00466 int scroll_speed()
00467 {
00468     const int value = lexical_cast_in_range<int>(get("scroll"), 50, 1, 100);
00469     scroll = value/100.0;
00470 
00471     return value;
00472 }
00473 
00474 void set_scroll_speed(const int new_speed)
00475 {
00476     prefs["scroll"] = lexical_cast<std::string>(new_speed);
00477     scroll = new_speed / 100.0;
00478 }
00479 
00480 bool mouse_scroll_enabled()
00481 {
00482     return utils::string_bool(get("mouse_scrolling"), true);
00483 }
00484 
00485 void enable_mouse_scroll(bool value)
00486 {
00487     set("mouse_scrolling", value ? "yes" : "no");
00488 }
00489 
00490 bool animate_map()
00491 {
00492     return utils::string_bool(preferences::get("animate_map"), true);
00493 }
00494 
00495 bool show_standing_animations()
00496 {
00497     return utils::string_bool(preferences::get("unit_standing_animations"), true);
00498 }
00499 
00500 bool show_fps()
00501 {
00502     return fps;
00503 }
00504 
00505 void set_show_fps(bool value)
00506 {
00507     fps = value;
00508 }
00509 
00510 int draw_delay()
00511 {
00512     return draw_delay_;
00513 }
00514 
00515 void set_draw_delay(int value)
00516 {
00517     draw_delay_ = value;
00518 }
00519 
00520 bool use_colour_cursors()
00521 {
00522     return colour_cursors;
00523 }
00524 
00525 void _set_colour_cursors(bool value)
00526 {
00527     preferences::set("colour_cursors", value ? "yes" : "no");
00528     colour_cursors = value;
00529 }
00530 
00531 void load_hotkeys() {
00532     hotkey::load_hotkeys(prefs);
00533 }
00534 void save_hotkeys() {
00535     hotkey::save_hotkeys(prefs);
00536 }
00537 
00538 unsigned int sample_rate()
00539 {
00540     return lexical_cast_default<unsigned int>(preferences::get("sample_rate"), 44100);
00541 }
00542 
00543 void save_sample_rate(const unsigned int rate)
00544 {
00545     if (sample_rate() == rate)
00546         return;
00547 
00548     preferences::set("sample_rate", lexical_cast<std::string>(rate));
00549 
00550     // If audio is open, we have to re set sample rate
00551     sound::reset_sound();
00552 }
00553 
00554 } // end namespace preferences
00555 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs