Debian Codegraph @

src/prefs.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* OES Preferences, S-Expressions */cxx . . $ `<file>/src/prefs.cpp`/
2
3 #include <string.h>
4 #include <pwd.h>
5
6 #include "config.h"cxx . . $ `<file>/config.h`/
7 #include "snipe2d.h"cxx . . $ `<file>/src/snipe2d.h`/
8 #include "sexpr/sexp.h"cxx . . $ `<file>/src/sexpr/sexp.h`/
9 #include "binds.h"cxx . . $ `<file>/src/binds.h`/
10
11 /* default web browser. */
•12 #define WWWBROWSERcxx . . $ `src/prefs.cpp:12:9`! "/usr/bin/sensible-browser"
13
14 extern PREFScxx . . $ PREFS# gPrefscxx . . $ gPrefs.;
15 struct passwd *pw_entcxx . . $ pw_ent. = 0;
16
17 const char *PATHSEPcxx . . $ PATHSEP. = "/";
18 const char *dotdircxx . . $ dotdir. = ".oes";
19 const char *confnamecxx . . $ confname. = "prefs.cfg";
20 const char *scorenamecxx . . $ scorename. = "scores.bin";
21 const char *bindnamecxx . . $ bindname. = "binds.cfg";
22 const char *keysnamecxx . . $ keysname. = "keys.cfg";
23
24
25 #define PAIRPcxx . . $ `src/prefs.cpp:25:9`!(se) ((se)->ty == SEXP_LIST)
26 #define ATOMPcxx . . $ `src/prefs.cpp:26:9`!(se) ((se)->ty == SEXP_VALUE)
27 #define CARcxx . . $ `src/prefs.cpp:27:9`!(se) (ATOMPcxx . . $ `src/prefs.cpp:26:9`!(se) ? se : hd_sexpcxx . . $ `src/sexpr/sexp_ops.h:54:9`!(se))
28 #define CDRcxx . . $ `src/prefs.cpp:28:9`!(se) (ATOMPcxx . . $ `src/prefs.cpp:26:9`!(se) ? next_sexpcxx . . $ `src/sexpr/sexp_ops.h:64:9`!(se) : tl_sexpcxx . . $ `src/sexpr/sexp_ops.h:59:9`!(se))
29
30
31 sexp_tcxx . . $ sexp_t# *
32 assoc_sexpcxx . . $ assoc_sexp(23464af592d36b1b). (sexp_tcxx . . $ sexp_t# *alist, const char *key)
33 {
34 //char buf[1024];
35 sexp_tcxx . . $ sexp_t# *pair;
36
37 if ((alist->tycxx . . $ elt#ty. == SEXP_LISTcxx . . $ SEXP_LIST.) && (alist->nextcxx . . $ elt#next. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!))
38 alist = alist->listcxx . . $ elt#list.;
39 while (alist)
40 {
41 //print_sexp(buf, sizeof(buf), alist);
42 //printf("alist = %s\n", buf);
43 if (alist->tycxx . . $ elt#ty. == SEXP_LISTcxx . . $ SEXP_LIST.)
44 {
45 /* pair */
46 pair = hd_sexpcxx . . $ `src/sexpr/sexp_ops.h:54:9`!(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, CARcxx . . $ `src/prefs.cpp:27:9`!(pair)->valcxx . . $ elt#val.))
52 return pair;
53 }
54 /* else ignore */
55 alist = next_sexpcxx . . $ `src/sexpr/sexp_ops.h:64:9`!(alist);
56 }
57 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
58 }
59
60
61 int alist_get_intcxx . . $ alist_get_int(b13cc0d2cdce0e83). (sexp_tcxx . . $ sexp_t# *se, const char *key)
62 {
63 sexp_tcxx . . $ sexp_t# *pair;
64 int n;
65
66 pair = assoc_sexpcxx . . $ assoc_sexp(23464af592d36b1b).(se, key);
67 if (!pair) return 0;
68 n = strtolcxx . . $ strtol(8cc4c7acb13e570c).(CARcxx . . $ `src/prefs.cpp:27:9`!(CDRcxx . . $ `src/prefs.cpp:28:9`!(pair))->valcxx . . $ elt#val., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, 0);
69 return n;
70 }
71
72 float alist_get_floatcxx . . $ alist_get_float(195c743972ee2786). (sexp_tcxx . . $ sexp_t# *se, const char *key)
73 {
74 sexp_tcxx . . $ sexp_t# *pair;
75 float f;
76
77 pair = assoc_sexpcxx . . $ assoc_sexp(23464af592d36b1b).(se, key);
78 if (!pair) return 0;
79 f = atofcxx . . $ atof(9b0361fcbc284e4f).(CARcxx . . $ `src/prefs.cpp:27:9`!(CDRcxx . . $ `src/prefs.cpp:28:9`!(pair))->valcxx . . $ elt#val.);
80 return f;
81 }
82
83 const char * alist_get_stringcxx . . $ alist_get_string(bcfd75d68270e189). (sexp_tcxx . . $ sexp_t# *se, const char *key)
84 {
85 sexp_tcxx . . $ sexp_t# *pair;
86 const char *s;
87
88 pair = assoc_sexpcxx . . $ assoc_sexp(23464af592d36b1b).(se, key);
89 if (!pair) return 0;
90 s = pair->nextcxx . . $ elt#next.->valcxx . . $ elt#val.;
91 return s;
92 }
93
94 void prefs_save_stubcxx . . $ prefs_save_stub(49f6e7a06ebc5aa8).()
95 {
96 prefs_savecxx . . $ prefs_save(fac825aec9ca7bcf).(&gPrefscxx . . $ gPrefs.);
97 }
98
99
100 /* initialise paths and defaults. */
101 PREFScxx . . $ PREFS# *
102 prefs_initcxx . . $ prefs_init(fac825aec9ca7bcf). (PREFScxx . . $ PREFS# *self)
103 {
104 int homedirlen, datadirlen, cfgpathlen, scorelen, keyslen, bindlen;
105
106 if (!self)
107 self = (PREFScxx . . $ PREFS#*)malloccxx . . $ malloc(9f008b2a9698428b).(sizeof(PREFScxx . . $ PREFS#));
108 memset(self, 0, sizeof(*self));
109 if (!pw_entcxx . . $ pw_ent.)
110 pw_entcxx . . $ pw_ent. = getpwuid(geteuid());
111 self->fullscreencxx . . $ prefs_#fullscreen. = 0;
112 self->audiocxx . . $ prefs_#audio. = 1;
113 self->verbosecxx . . $ prefs_#verbose. = 2;
114 self->difficultycxx . . $ prefs_#difficulty. = 2;
115 self->wwwbrowsercxx . . $ prefs_#wwwbrowser. = WWWBROWSERcxx . . $ `src/prefs.cpp:12:9`!;
116 homedirlen = strlen(pw_entcxx . . $ pw_ent.->pw_dir) + 1;
117 datadirlen = homedirlen + strlen(dotdircxx . . $ dotdir.) + strlen(PATHSEPcxx . . $ PATHSEP.) + 1;
118 bindlen = datadirlen + strlen(bindnamecxx . . $ bindname.) + strlen(PATHSEPcxx . . $ PATHSEP.) + 1;
119 cfgpathlen = datadirlen + strlen(confnamecxx . . $ confname.) + strlen(PATHSEPcxx . . $ PATHSEP.) + 1;
120 scorelen = datadirlen + strlen(scorenamecxx . . $ scorename.) + strlen(PATHSEPcxx . . $ PATHSEP.) + 1;
121 keyslen = datadirlen + strlen(keysnamecxx . . $ keysname.) + strlen(PATHSEPcxx . . $ PATHSEP.) + 1;
122 self->homedircxx . . $ prefs_#homedir. = (char*)malloccxx . . $ malloc(9f008b2a9698428b).(homedirlen);
123 self->datadircxx . . $ prefs_#datadir. = (char*)malloccxx . . $ malloc(9f008b2a9698428b).(datadirlen);
124 self->bindpathcxx . . $ prefs_#bindpath. = (char*)malloccxx . . $ malloc(9f008b2a9698428b).(bindlen);
125 self->scorepathcxx . . $ prefs_#scorepath. = (char*)malloccxx . . $ malloc(9f008b2a9698428b).(scorelen);
126 self->cfgpathcxx . . $ prefs_#cfgpath. = (char*)malloccxx . . $ malloc(9f008b2a9698428b).(cfgpathlen);
127 self->keyspathcxx . . $ prefs_#keyspath. = (char*)malloccxx . . $ malloc(9f008b2a9698428b).(keyslen);
128 snprintf(self->homedircxx . . $ prefs_#homedir., homedirlen, "%s", pw_entcxx . . $ pw_ent.->pw_dir);
129 snprintf(self->datadircxx . . $ prefs_#datadir., datadirlen, "%s%s%s", self->homedircxx . . $ prefs_#homedir., PATHSEPcxx . . $ PATHSEP., dotdircxx . . $ dotdir.);
130 snprintf(self->scorepathcxx . . $ prefs_#scorepath., scorelen, "%s%s%s", self->datadircxx . . $ prefs_#datadir., PATHSEPcxx . . $ PATHSEP., scorenamecxx . . $ scorename.);
131 snprintf(self->cfgpathcxx . . $ prefs_#cfgpath., cfgpathlen, "%s%s%s", self->datadircxx . . $ prefs_#datadir., PATHSEPcxx . . $ PATHSEP., confnamecxx . . $ confname.);
132 snprintf(self->bindpathcxx . . $ prefs_#bindpath., bindlen, "%s%s%s", self->datadircxx . . $ prefs_#datadir., PATHSEPcxx . . $ PATHSEP., bindnamecxx . . $ bindname.);
133 // snprintf(self->keyspath, keyslen, "%s%s%s", self->datadir, PATHSEP, keysname);
134 snprintf(self->keyspathcxx . . $ prefs_#keyspath., keyslen, "%s", keysnamecxx . . $ 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_updatecxx . . $ prefs_update(ac0ad757caa46810). (PREFScxx . . $ PREFS# *self)
143 {
144 self->verbosecxx . . $ prefs_#verbose. = Gamecxx . . $ Game..verbositycxx . . $ orbitalsniper_t#verbosity.;
145 return 0;
146 }
147
148
149 /* Load preferences from saved file. */
150 PREFScxx . . $ PREFS# *
151 prefs_loadcxx . . $ prefs_load(fac825aec9ca7bcf). (PREFScxx . . $ PREFS# *self)
152 {
153 //char buf[1024];
154 FILE *preffile;
155 sexp_tcxx . . $ sexp_t# *se;
156 sexp_iowrap_tcxx . . $ sexp_iowrap_t# *io;
157 char *s;
158 const char *sym;
159 int i;
160
161 if (!self)
162 self = prefs_initcxx . . $ prefs_init(fac825aec9ca7bcf).(self);
163 // if (!prefs_init()) return 0;
164 //printf("Prefs Style 2\n");
165 if (!(preffile = fopen(self->cfgpathcxx . . $ prefs_#cfgpath., "r")))
166 {
167 /* Maybe first time ever running. Create prefs. */
168 prefs_createcxx . . $ prefs_create(fac825aec9ca7bcf).(self);
169 if (!(preffile = fopen(self->cfgpathcxx . . $ prefs_#cfgpath., "r")))
170 {
171 /* still can't open it. */
172 printf("Could not load from '%s'\n", self->cfgpathcxx . . $ prefs_#cfgpath.);
173 return 0;
174 }
175 }
176
177 io = init_iowrap(fileno(preffile));
178 se = read_one_sexp(io);
179
180 s = CARcxx . . $ `src/prefs.cpp:27:9`!(CDRcxx . . $ `src/prefs.cpp:28:9`!(assoc_sexp(se, "package")))->valcxx . . $ elt#val.;
181 //printf("Detected pref package: %s\n", s);
182 i = alist_get_intcxx . . $ alist_get_int(b13cc0d2cdce0e83).(se, "version");
183 //printf("Detected pref version: %d\n", i);
184 i = alist_get_intcxx . . $ alist_get_int(b13cc0d2cdce0e83).(se, "fullscreen");
185 //printf("Fullscreen=%d\n", i);
186 self->fullscreencxx . . $ prefs_#fullscreen. = i;
187 i = alist_get_intcxx . . $ alist_get_int(b13cc0d2cdce0e83).(se, "audio");
188 //printf("Audio=%d\n", i);
189 self->audiocxx . . $ prefs_#audio. = i;
190 i = alist_get_intcxx . . $ alist_get_int(b13cc0d2cdce0e83).(se, "verbose");
191 //printf("Verbosity=%d\n", i);
192 self->verbosecxx . . $ prefs_#verbose. = i;
193 i = alist_get_intcxx . . $ alist_get_int(b13cc0d2cdce0e83).(se, "difficulty");
194 //printf("Difficulty=%d\n", i);
195 self->difficultycxx . . $ prefs_#difficulty. = i;
196 i = alist_get_intcxx . . $ alist_get_int(b13cc0d2cdce0e83).(se, "joystick");
197 //printf("Joystick=%d\n", i);
198 self->joystickcxx . . $ prefs_#joystick. = i;
199 sym = alist_get_stringcxx . . $ alist_get_string(bcfd75d68270e189).(se, "wwwbrowser");
200 if (sym)
201 {
202 self->wwwbrowsercxx . . $ prefs_#wwwbrowser. = strdup(sym);
203 }
204
205 destroy_iowrap(io);
206 fclose(preffile);
207
208 atexitcxx . . $ atexit(cc8c495369588262). (prefs_save_stubcxx . . $ prefs_save_stub(49f6e7a06ebc5aa8).); /* XXX: hrm. */
209
210 return self;
211 }
212
213
214 /* Save preferences to file. */
215 PREFScxx . . $ PREFS# *
216 prefs_savecxx . . $ prefs_save(fac825aec9ca7bcf). (PREFScxx . . $ PREFS# *self)
217 {
218 FILE *preffile;
219
220 if (!self) return 0;
221 if (!self->cfgpathcxx . . $ prefs_#cfgpath.) return 0;
222
223 prefs_updatecxx . . $ prefs_update(ac0ad757caa46810).(self);
224
225 if (!(preffile = fopen(self->cfgpathcxx . . $ prefs_#cfgpath., "w")))
226 {
227 printf("Could not save to '%s'\n", self->cfgpathcxx . . $ prefs_#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", PACKAGEcxx . . $ `../config.h:83:9`!);
235 fprintf(preffile, " (version %d)\n", 1);
236 fprintf(preffile, " (fullscreen %d)\n", self->fullscreencxx . . $ prefs_#fullscreen.);
237 fprintf(preffile, " (audio %d)\n", self->audiocxx . . $ prefs_#audio.);
238 fprintf(preffile, " (verbose %d)\n", self->verbosecxx . . $ prefs_#verbose.);
239 fprintf(preffile, " (difficulty %d)\n", self->difficultycxx . . $ prefs_#difficulty.);
240 fprintf(preffile, " (joystick %d)\n", self->joystickcxx . . $ prefs_#joystick.);
241 fprintf(preffile, " (wwwbrowser \"%s\")\n", self->wwwbrowsercxx . . $ prefs_#wwwbrowser.);
242 fprintf(preffile, ")\n");
243
244 fclose(preffile);
245
246 return self;
247 }
248
249
250 /* Ensure necessary files exist. */
251 PREFScxx . . $ PREFS# *
252 prefs_createcxx . . $ prefs_create(fac825aec9ca7bcf). (PREFScxx . . $ PREFS# *self)
253 {
254 if (!self)
255 self = prefs_initcxx . . $ prefs_init(fac825aec9ca7bcf).(self);
256 if (access(self->datadircxx . . $ prefs_#datadir., W_OKcxx . . $ `<external>/12628407106812493757/unistd.h:282:9`! | R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`! | X_OKcxx . . $ `<external>/12628407106812493757/unistd.h:283:9`!))
257 {
258 /* problem accessing directory. Assume missing. */
259 mkdir(self->datadircxx . . $ prefs_#datadir., 0755);
260 if (access(self->datadircxx . . $ prefs_#datadir., W_OKcxx . . $ `<external>/12628407106812493757/unistd.h:282:9`! | R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`! | X_OKcxx . . $ `<external>/12628407106812493757/unistd.h:283:9`!))
261 {
262 /* still problem. */
263 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Unable to create pref directory '%s'\n", self->datadircxx . . $ prefs_#datadir.);
264 exitcxx . . $ exit(44ed87f96534fad7).(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->cfgpathcxx . . $ prefs_#cfgpath., W_OKcxx . . $ `<external>/12628407106812493757/unistd.h:282:9`! | R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`!))
272 {
273 /* error. assume missing. */
274 prefs_savecxx . . $ prefs_save(fac825aec9ca7bcf).(self);
275 if (access(self->cfgpathcxx . . $ prefs_#cfgpath., R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`!))
276 {
277 /* still error. bad. */
278 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Unable to create prefs file '%s'\n", self->cfgpathcxx . . $ prefs_#cfgpath.);
279 exitcxx . . $ exit(44ed87f96534fad7).(1);
280 }
281 }
282 /* 0 == read-write OK */
283
284 return self;
285 }
286
287
288 PREFScxx . . $ PREFS# *
289 prefs_destroycxx . . $ prefs_destroy(fac825aec9ca7bcf). (PREFScxx . . $ PREFS# *self)
290 {
291 if (!self) return 0;
292 prefs_savecxx . . $ prefs_save(fac825aec9ca7bcf).(self);
293 freecxx . . $ free(bd21765a0afc8e3c).(self->cfgpathcxx . . $ prefs_#cfgpath.); self->cfgpathcxx . . $ prefs_#cfgpath. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
294 freecxx . . $ free(bd21765a0afc8e3c).(self->scorepathcxx . . $ prefs_#scorepath.); self->scorepathcxx . . $ prefs_#scorepath. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
295 freecxx . . $ free(bd21765a0afc8e3c).(self->datadircxx . . $ prefs_#datadir.); self->datadircxx . . $ prefs_#datadir. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
296 freecxx . . $ free(bd21765a0afc8e3c).(self->homedircxx . . $ prefs_#homedir.); self->homedircxx . . $ prefs_#homedir. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
297 return self;
298 }
299
300 void
301 prefs_deletecxx . . $ prefs_delete(524415c8f49fd7f5). (PREFScxx . . $ PREFS# *self)
302 {
303 if (!self) return;
304 prefs_destroycxx . . $ prefs_destroy(fac825aec9ca7bcf). (self);
305 freecxx . . $ free(bd21765a0afc8e3c).(self);
306 }
307
308
309 #if 0
310 /* dummy function. */
311 void prefs()
312 {
313 }
314 #endif /* 0 */