aa8cb401c8330b794e25173c632676a2c3f7e96f
[supertux.git] / lib / utils / configfile.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Michael George <mike@georgetech.com>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <cstdlib>
23 #include <string>
24
25 #include "configfile.h"
26 #include "app/setup.h"
27 #include "app/globals.h"
28 #include "audio/sound_manager.h"
29
30 using namespace SuperTux;
31
32 #ifdef WIN32
33 const char * config_filename = ("/"+ package_symbol_name + "_config.dat").c_str();
34 #else
35 const char * config_filename = "/config";
36 #endif
37
38 Config* SuperTux::config = 0;
39
40 static void defaults ()
41 {
42   /* Set defaults: */
43   debug_mode = false;
44   SoundManager::get()->set_audio_device_available(true);
45
46   use_fullscreen = false;
47   show_fps = false;
48   use_gl = false;
49
50   SoundManager::get()->enable_sound(true);
51   SoundManager::get()->enable_music(true);
52 }
53
54 FILE * SuperTux::opendata(const std::string& rel_filename, const char *mode)
55 {
56   std::string filename;
57   FILE * fi;
58
59   filename = st_dir + rel_filename;
60
61   /* Try opening the file: */
62   fi = fopen(filename.c_str(), mode);
63
64   if (fi == NULL)
65     {
66       fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename.c_str());
67
68       if (strcmp(mode, "r") == 0)
69         fprintf(stderr, "for read!!!\n");
70       else if (strcmp(mode, "w") == 0)
71         fprintf(stderr, "for write!!!\n");
72     }
73
74   return(fi);
75 }
76
77 void Config::load()
78 {
79   FILE * file = NULL;
80
81   defaults();
82
83   /* override defaults from config file */
84
85   file = opendata(config_filename, "r");
86
87   if (file == NULL)
88     return;
89
90   /* read config file */
91
92   lisp_stream_t   stream;
93   lisp_object_t * root_obj = NULL;
94
95   lisp_stream_init_file (&stream, file);
96   root_obj = lisp_read (&stream);
97
98   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
99     return;
100
101   if (strcmp(lisp_symbol(lisp_car(root_obj)), (package_symbol_name+"-config").c_str()) != 0)
102     return;
103
104   LispReader reader(lisp_cdr(root_obj));
105
106   reader.read_bool("fullscreen", use_fullscreen);
107   bool temp;
108   reader.read_bool("sound",     temp);
109   SoundManager::get()->enable_sound(temp);
110   reader.read_bool("music",      temp);
111   SoundManager::get()->enable_music(temp);
112   reader.read_bool("show_fps",   show_fps);
113
114   std::string video;
115   reader.read_string ("video", video);
116   if (video == "opengl")
117     use_gl = true;
118   else
119     use_gl = false;
120
121   reader.read_int ("joystick", joystick_num);
122
123   if (joystick_num >= 0)
124     {
125     reader.read_int ("joystick-x", joystick_keymap.x_axis);
126     reader.read_int ("joystick-y", joystick_keymap.y_axis);
127     reader.read_int ("joystick-a", joystick_keymap.a_button);
128     reader.read_int ("joystick-b", joystick_keymap.b_button);
129     reader.read_int ("joystick-start", joystick_keymap.start_button);
130     reader.read_int ("joystick-deadzone", joystick_keymap.dead_zone);
131     }
132
133   customload(reader);
134
135   lisp_free(root_obj);
136   fclose(file);
137 }
138
139 void Config::save ()
140 {
141   /* write settings to config file */
142   FILE * config = opendata(config_filename, "w");
143
144   if(config)
145     {
146       fprintf(config, ("("+package_symbol_name+"-config\n").c_str());
147       fprintf(config, "\t;; the following options can be set to #t or #f:\n");
148       fprintf(config, "\t(fullscreen %s)\n", use_fullscreen ? "#t" : "#f");
149       fprintf(config, "\t(sound      %s)\n", SoundManager::get()->sound_enabled()      ? "#t" : "#f");
150       fprintf(config, "\t(music      %s)\n", SoundManager::get()->music_enabled()      ? "#t" : "#f");
151       fprintf(config, "\t(show_fps   %s)\n", show_fps       ? "#t" : "#f");
152
153       fprintf(config, "\n\t;; either \"opengl\" or \"sdl\"\n");
154       fprintf(config, "\t(video      \"%s\")\n", use_gl ? "opengl" : "sdl");
155
156       if(use_joystick)
157         {
158         fprintf(config, "\n\t;; joystick number:\n");
159         fprintf(config, "\t(joystick   %d)\n", joystick_num);
160
161         fprintf(config, "\t(joystick-x   %d)\n", joystick_keymap.x_axis);
162         fprintf(config, "\t(joystick-y   %d)\n", joystick_keymap.y_axis);
163         fprintf(config, "\t(joystick-a   %d)\n", joystick_keymap.a_button);
164         fprintf(config, "\t(joystick-b   %d)\n", joystick_keymap.b_button);
165         fprintf(config, "\t(joystick-start  %d)\n", joystick_keymap.start_button);
166         fprintf(config, "\t(joystick-deadzone  %d)\n", joystick_keymap.dead_zone);
167         }
168         
169         customsave(config);
170
171       fprintf(config, ")\n");
172       fclose(config);
173     }
174 }
175
176 /* EOF */