Debian Codegraph @

src/prefs.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* OES Preferences, S-Expressions */
2
3 #include <string.h>
4 #include <pwd.h>
5
6 #include "config.h"
7 #include "snipe2d.h"
8 #include "sexpr/sexp.h"
9 #include "binds.h"
10
11 /* default web browser. */
•12 #define WWWBROWSER "/usr/bin/sensible-browser"
13
14 extern PREFS gPrefs;
15 struct passwd *pw_ent = 0;
16
17 const char *PATHSEP = "/";
18 const char *dotdir = ".oes";
19 const char *confname = "prefs.cfg";
20 const char *scorename = "scores.bin";
21 const char *bindname = "binds.cfg";
22 const char *keysname = "keys.cfg";
23
24
25 #define PAIRP(se) ((se)->ty == SEXP_LIST)
26 #define ATOMP(se) ((se)->ty == SEXP_VALUE)
27 #define CAR(se) (ATOMP(se) ? se : hd_sexp(se))
28 #define CDR(se) (ATOMP(se) ? next_sexp(se) : tl_sexp(se))
29
30
31 sexp_t *
32 assoc_sexp (sexp_t *alist, const char *key)
33 {
34 //char buf[1024];
35 sexp_t *pair;
36
37 if ((alist->ty == SEXP_LIST) && (alist->next == NULL))
38 alist = alist->list;
39 while (alist)
40 {
41 //print_sexp(buf, sizeof(buf), alist);
42 //printf("alist = %s\n", buf);
43 if (alist->ty == SEXP_LIST)
44 {
45 /* pair */
46 pair = hd_sexp(alist);
47 //print_sexp(buf, sizeof(buf), CAR(pair));
48 //printf("(car pair) = %s\n", buf);
49 //print_sexp(buf, sizeof(buf), CDR(pair));
50 //printf("(cdr pair) = %s\n", buf);
51 if (0 == strcasecmp(key, CAR(pair)->val))
52 return pair;
53 }
54 /* else ignore */
55 alist = next_sexp(alist);
56 }
57 return NULL;
58 }
59
60
61 int alist_get_int (sexp_t *se, const char *key)
62 {
63 sexp_t *pair;
64 int n;
65
66 pair = assoc_sexp(se, key);
67 if (!pair) return 0;
68 n = strtol(CAR(CDR(pair))->val, NULL, 0);
69 return n;
70 }
71
72 float alist_get_float (sexp_t *se, const char *key)
73 {
74 sexp_t *pair;
75 float f;
76
77 pair = assoc_sexp(se, key);
78 if (!pair) return 0;
79 f = atof(CAR(CDR(pair))->val);
80 return f;
81 }
82
83 const char * alist_get_string (sexp_t *se, const char *key)
84 {
85 sexp_t *pair;
86 const char *s;
87
88 pair = assoc_sexp(se, key);
89 if (!pair) return 0;
90 s = pair->next->val;
91 return s;
92 }
93
94 void prefs_save_stub()
95 {
96 prefs_save(&gPrefs);
97 }
98
99
100 /* initialise paths and defaults. */
101 PREFS *
102 prefs_init (PREFS *self)
103 {
104 int homedirlen, datadirlen, cfgpathlen, scorelen, keyslen, bindlen;
105
106 if (!self)
107 self = (PREFS*)malloc(sizeof(PREFS));
108 memset(self, 0, sizeof(*self));
109 if (!pw_ent)
110 pw_ent = getpwuid(geteuid());
111 self->fullscreen = 0;
112 self->audio = 1;
113 self->verbose = 2;
114 self->difficulty = 2;
115 self->wwwbrowser = WWWBROWSER;
116 homedirlen = strlen(pw_ent->pw_dir) + 1;
117 datadirlen = homedirlen + strlen(dotdir) + strlen(PATHSEP) + 1;
118 bindlen = datadirlen + strlen(bindname) + strlen(PATHSEP) + 1;
119 cfgpathlen = datadirlen + strlen(confname) + strlen(PATHSEP) + 1;
120 scorelen = datadirlen + strlen(scorename) + strlen(PATHSEP) + 1;
121 keyslen = datadirlen + strlen(keysname) + strlen(PATHSEP) + 1;
122 self->homedir = (char*)malloc(homedirlen);
123 self->datadir = (char*)malloc(datadirlen);
124 self->bindpath = (char*)malloc(bindlen);
125 self->scorepath = (char*)malloc(scorelen);
126 self->cfgpath = (char*)malloc(cfgpathlen);
127 self->keyspath = (char*)malloc(keyslen);
128 snprintf(self->homedir, homedirlen, "%s", pw_ent->pw_dir);
129 snprintf(self->datadir, datadirlen, "%s%s%s", self->homedir, PATHSEP, dotdir);
130 snprintf(self->scorepath, scorelen, "%s%s%s", self->datadir, PATHSEP, scorename);
131 snprintf(self->cfgpath, cfgpathlen, "%s%s%s", self->datadir, PATHSEP, confname);
132 snprintf(self->bindpath, bindlen, "%s%s%s", self->datadir, PATHSEP, bindname);
133 // snprintf(self->keyspath, keyslen, "%s%s%s", self->datadir, PATHSEP, keysname);
134 snprintf(self->keyspath, keyslen, "%s", keysname);
135 //printf("Test on datadir '%s' => %d\n", self->datadir, access(self->datadir, W_OK|R_OK|X_OK));
136 return self;
137 }
138
139
140 /* Populate PREFS with settings from game. */
141 int
142 prefs_update (PREFS *self)
143 {
144 self->verbose = Game.verbosity;
145 return 0;
146 }
147
148
149 /* Load preferences from saved file. */
150 PREFS *
151 prefs_load (PREFS *self)
152 {
153 //char buf[1024];
154 FILE *preffile;
155 sexp_t *se;
156 sexp_iowrap_t *io;
157 char *s;
158 const char *sym;
159 int i;
160
161 if (!self)
162 self = prefs_init(self);
163 // if (!prefs_init()) return 0;
164 //printf("Prefs Style 2\n");
165 if (!(preffile = fopen(self->cfgpath, "r")))
166 {
167 /* Maybe first time ever running. Create prefs. */
168 prefs_create(self);
169 if (!(preffile = fopen(self->cfgpath, "r")))
170 {
171 /* still can't open it. */
172 printf("Could not load from '%s'\n", self->cfgpath);
173 return 0;
174 }
175 }
176
177 io = init_iowrap(fileno(preffile));
178 se = read_one_sexp(io);
179
180 s = CAR(CDR(assoc_sexp(se, "package")))->val;
181 //printf("Detected pref package: %s\n", s);
182 i = alist_get_int(se, "version");
183 //printf("Detected pref version: %d\n", i);
184 i = alist_get_int(se, "fullscreen");
185 //printf("Fullscreen=%d\n", i);
186 self->fullscreen = i;
187 i = alist_get_int(se, "audio");
188 //printf("Audio=%d\n", i);
189 self->audio = i;
190 i = alist_get_int(se, "verbose");
191 //printf("Verbosity=%d\n", i);
192 self->verbose = i;
193 i = alist_get_int(se, "difficulty");
194 //printf("Difficulty=%d\n", i);
195 self->difficulty = i;
196 i = alist_get_int(se, "joystick");
197 //printf("Joystick=%d\n", i);
198 self->joystick = i;
199 sym = alist_get_string(se, "wwwbrowser");
200 if (sym)
201 {
202 self->wwwbrowser = strdup(sym);
203 }
204
205 destroy_iowrap(io);
206 fclose(preffile);
207
208 atexit (prefs_save_stub); /* XXX: hrm. */
209
210 return self;
211 }
212
213
214 /* Save preferences to file. */
215 PREFS *
216 prefs_save (PREFS *self)
217 {
218 FILE *preffile;
219
220 if (!self) return 0;
221 if (!self->cfgpath) return 0;
222
223 prefs_update(self);
224
225 if (!(preffile = fopen(self->cfgpath, "w")))
226 {
227 printf("Could not save to '%s'\n", self->cfgpath);
228 return 0;
229 }
230
231 fprintf(preffile, "; OES Preferences\n");
232 fprintf(preffile, "\n");
233 fprintf(preffile, "(prefs\n");
234 fprintf(preffile, " (package \"%s\")\n", PACKAGE);
235 fprintf(preffile, " (version %d)\n", 1);
236 fprintf(preffile, " (fullscreen %d)\n", self->fullscreen);
237 fprintf(preffile, " (audio %d)\n", self->audio);
238 fprintf(preffile, " (verbose %d)\n", self->verbose);
239 fprintf(preffile, " (difficulty %d)\n", self->difficulty);
240 fprintf(preffile, " (joystick %d)\n", self->joystick);
241 fprintf(preffile, " (wwwbrowser \"%s\")\n", self->wwwbrowser);
242 fprintf(preffile, ")\n");
243
244 fclose(preffile);
245
246 return self;
247 }
248
249
250 /* Ensure necessary files exist. */
251 PREFS *
252 prefs_create (PREFS *self)
253 {
254 if (!self)
255 self = prefs_init(self);
256 if (access(self->datadir, W_OK | R_OK | X_OK))
257 {
258 /* problem accessing directory. Assume missing. */
259 mkdir(self->datadir, 0755);
260 if (access(self->datadir, W_OK | R_OK | X_OK))
261 {
262 /* still problem. */
263 fprintf(stderr, "Unable to create pref directory '%s'\n", self->datadir);
264 exit(1);
265 }
266 }
267 /* 0 == read-write-search OK */
268
269 /* Check for the conf file itself. */
270 //printf("Test on cfgpath '%s' => %d\n", self->cfgpath, access(self->cfgpath, W_OK|R_OK));
271 if (access(self->cfgpath, W_OK | R_OK))
272 {
273 /* error. assume missing. */
274 prefs_save(self);
275 if (access(self->cfgpath, R_OK))
276 {
277 /* still error. bad. */
278 fprintf(stderr, "Unable to create prefs file '%s'\n", self->cfgpath);
279 exit(1);
280 }
281 }
282 /* 0 == read-write OK */
283
284 return self;
285 }
286
287
288 PREFS *
289 prefs_destroy (PREFS *self)
290 {
291 if (!self) return 0;
292 prefs_save(self);
293 free(self->cfgpath); self->cfgpath = NULL;
294 free(self->scorepath); self->scorepath = NULL;
295 free(self->datadir); self->datadir = NULL;
296 free(self->homedir); self->homedir = NULL;
297 return self;
298 }
299
300 void
301 prefs_delete (PREFS *self)
302 {
303 if (!self) return;
304 prefs_destroy (self);
305 free(self);
306 }
307
308
309 #if 0
310 /* dummy function. */
311 void prefs()
312 {
313 }
314 #endif /* 0 */