1 /*
2  * Copyright 2009-2017 Peter Kosyh <p.kosyh at gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include "externals.h"
26 #include "internals.h"
27 
28 #if defined(S60) || defined(_WIN32_WCE) || defined(MAEMO)
29 int opt_fsize = 12;
30 #elif defined(IOS) || defined(SAILFISHOS) || defined(ANDROID) || defined(WINRT)
31 int opt_fsize = 5;
32 #else
33 int opt_fsize = 0;
34 #endif
35 
36 #if defined(MAEMO)
37 int opt_fs = 1;
38 int opt_owntheme = 0;
39 int opt_hl = 0;
40 #elif defined(IOS) || defined(ANDROID) || defined(SAILFISHOS)
41 int opt_fs = 1;
42 int opt_owntheme = 1;
43 int opt_hl = 0;
44 #elif defined(_WIN32_WCE) || defined(WINRT)
45 int opt_fs = 1;
46 int opt_owntheme = 1;
47 int opt_hl = 0;
48 #else
49 int opt_fs = 0;
50 int opt_owntheme = 1;
51 int opt_hl = 1;
52 #endif
53 int opt_fading = 1;
54 #if defined(S60) || defined(MAEMO) || defined(_WIN32_WCE)
55 int opt_hz = 22050;
56 #else
57 int opt_hz = 44100;
58 #endif
59 int opt_vol = 127;
60 int opt_motion = 1;
61 int opt_click = 1;
62 int opt_music = 1;
63 int opt_autosave = 1;
64 int opt_filter = 1;
65 int opt_kbd = KBD_SMART;
66 int opt_justify = 0;
67 int opt_vsync = -1;
68 int opt_debug = -1;
69 int opt_resizable = -1;
70 int opt_hires = 1;
71 
72 char *opt_game = NULL;
73 char *opt_theme = NULL;
74 char *opt_lang = NULL;
75 int opt_mode[2] = {-1, -1};
76 
parse_mode(const char * v,void * data)77 int parse_mode(const char *v, void *data)
78 {
79 	int w, h;
80 	char *eptr;
81 	int *p = ((int *)data);
82 	w = strtol(v, &eptr, 10);
83 	if (!w || (eptr == v) || (*eptr != 'x'))
84 		return -1;
85 	eptr ++;
86 	h = strtol(eptr, &eptr, 10);
87 	if (!h || (*eptr))
88 		return -1;
89 	p[0] = w;
90 	p[1] = h;
91 	return 0;
92 }
93 
94 static struct parser cfg_parser[] = {
95 	{ "hz", parse_int, &opt_hz, 0 },
96 	{ "fs", parse_int, &opt_fs, 0 },
97 	{ "vol", parse_int, &opt_vol, 0 },
98 	{ "hl", parse_int, &opt_hl, 0 },
99 	{ "game", parse_string, &opt_game, 0 },
100 	{ "theme", parse_string, &opt_theme, 0 },
101 	{ "autosave", parse_int, &opt_autosave, 0 },
102 	{ "motion", parse_int, &opt_motion, 0 },
103 	{ "click", parse_int, &opt_click, 0 },
104 	{ "music", parse_int, &opt_music, 0 },
105 	{ "fscale", parse_int, &opt_fsize, 0 },
106 	{ "filter", parse_int, &opt_filter, 0 },
107 	{ "owntheme", parse_int, &opt_owntheme, 0 },
108 	{ "lang", parse_string, &opt_lang, 0 },
109 	{ "kbd", parse_int, &opt_kbd, 0 },
110 	{ "mode", parse_mode, opt_mode, 0 },
111 	{ "justify", parse_int, &opt_justify, 0 },
112 	{ "fading", parse_int, &opt_fading, 0 },
113 	{ "vsync", parse_int, &opt_vsync, 0 },
114 	{ "debug", parse_int, &opt_debug, 0 },
115 	{ "resizable", parse_int, &opt_resizable, 0 },
116 	{ "hires", parse_int, &opt_hires, 0 },
117 	{ NULL, NULL, NULL, 0 },
118 };
119 
cfg_init(void)120 void cfg_init(void)
121 {
122 #if defined(IOS)
123 	ios_cfg_init();
124 #endif
125 }
126 
cfg_parse(const char * path)127 static int cfg_parse(const char *path)
128 {
129 	return parse_ini(path, cfg_parser);
130 }
131 
cfg_load(void)132 int cfg_load(void)
133 {
134 	char *p = game_cfg_path();
135 	if (!p)
136 		return -1;
137 	if (access(p, R_OK))
138 		return 0;
139 	return cfg_parse(p);
140 }
141 
142 extern int noowntheme_sw;
143 
cfg_save(void)144 int cfg_save(void)
145 {
146 	int save_owntheme = (opt_owntheme == 2)?0:((noowntheme_sw)?1:opt_owntheme);
147 	int save_autosave = (opt_autosave == 2)?1:opt_autosave;
148 	FILE *fp;
149 	char *p = game_cfg_path();
150 	if (!p)
151 		return -1;
152 	fp = fopen(p, "w");
153 	if (!fp)
154 		return -1;
155 	fprintf(fp, "fs = %d\nhl = %d\nhz = %d\nvol = %d\nautosave = %d\n\
156 game = %s\nfscale = %d\nmotion = %d\n\
157 click = %d\nmusic = %d\ntheme = %s\n\
158 filter = %d\nowntheme = %d\nlang = %s\nkbd = %d\nmode = %dx%d\njustify = %d\nfading = %d\nhires = %d\n",
159 		opt_fs, opt_hl, opt_hz, opt_vol, save_autosave,
160 		curgame_dir?curgame_dir:"", opt_fsize, opt_motion,
161 		opt_click, opt_music, curtheme_dir[THEME_GLOBAL]?curtheme_dir[THEME_GLOBAL]:DEFAULT_THEME,
162 		opt_filter, save_owntheme, opt_lang, opt_kbd, opt_mode[0], opt_mode[1],
163 		opt_justify, opt_fading, opt_hires);
164 	if (opt_vsync != -1)
165 		fprintf(fp, "vsync = %d\n", opt_vsync);
166 	if (opt_debug != -1)
167 		fprintf(fp, "debug = %d\n", opt_debug);
168 	if (opt_resizable != -1)
169 		fprintf(fp, "resizable = %d\n", opt_resizable);
170 	fclose(fp);
171 	data_sync();
172 	return 0;
173 }
174