1 /*
2  * XPilot NG, a multiplayer space war game.
3  *
4  * Copyright (C) 2003 Kristian S�derblom <kps@users.sourceforge.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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 
21 #ifndef OPTION_H
22 #define OPTION_H
23 
24 typedef enum {
25     xp_noarg_option,
26     xp_bool_option,
27     xp_int_option,
28     xp_double_option,
29     xp_string_option,
30     xp_key_option
31 } xp_option_type_t;
32 
33 typedef struct xp_option xp_option_t;
34 
35 typedef enum {
36     xp_option_origin_default,	/* originates in code */
37     xp_option_origin_cmdline,	/* command line */
38     xp_option_origin_env,	/* environment variable */
39     xp_option_origin_xpilotrc,	/* xpilotrc file */
40     xp_option_origin_config,	/* config menu or such */
41     xp_option_origin_setcmd	/* set client command */
42 } xp_option_origin_t;
43 
44 typedef bool (*xp_bool_option_setfunc_t)   (xp_option_t *opt, bool val);
45 typedef bool (*xp_int_option_setfunc_t)    (xp_option_t *opt, int val);
46 typedef bool (*xp_double_option_setfunc_t) (xp_option_t *opt, double val);
47 typedef bool (*xp_string_option_setfunc_t) (xp_option_t *opt, const char *val);
48 typedef const char *(*xp_string_option_getfunc_t)(xp_option_t *opt);
49 
50 typedef int xp_option_flags_t;
51 
52 /* flag bits */
53 /* option shows up in default menu in X client */
54 #define XP_OPTFLAG_CONFIG_DEFAULT		(1 << 1)
55 /* option shows up in colors menu in X client */
56 #define XP_OPTFLAG_CONFIG_COLORS		(1 << 2)
57 /* option is not saved in xpilotrc if it isn't there already */
58 #define XP_OPTFLAG_KEEP				(1 << 3)
59 /* option value from xpilotrc won't be accepted */
60 #define XP_OPTFLAG_NEVER_SAVE			(1 << 4)
61 /* default flags, nothing here yet */
62 #define XP_OPTFLAG_DEFAULT         		(0)
63 
64 
65 /*
66  * NOTE: DON'T ACCESS THIS STRUCTURE DIRECTLY, USE THE INITIALIZER MACROS,
67  * AND OTHER ACCESS FUNCTIONS.
68  */
69 struct xp_option {
70     xp_option_type_t type;
71 
72     const char *name;
73 
74     xp_option_flags_t flags;
75     xp_option_origin_t origin;
76 
77     const char *help;
78     void *private_data;		/* currently only used for string options */
79 
80     /* noarg option stuff */
81 
82 #define XP_NOARG_OPTION_DUMMY \
83 	NULL
84 
85     bool *noarg_ptr;
86 
87     /* bool option stuff */
88 
89 #define XP_BOOL_OPTION_DUMMY \
90 	false, NULL, NULL
91 
92     bool bool_defval;
93     bool *bool_ptr;
94     xp_bool_option_setfunc_t bool_setfunc;
95 
96     /* integer option stuff */
97 
98 #define XP_INT_OPTION_DUMMY \
99 	0, 0, 0, NULL, NULL
100 
101     int int_defval;
102     int int_minval;
103     int int_maxval;
104     int *int_ptr;
105     xp_int_option_setfunc_t int_setfunc;
106 
107     /* double option stuff */
108 
109 #define XP_DOUBLE_OPTION_DUMMY \
110 	0, 0, 0, NULL, NULL
111 
112     double dbl_defval;
113     double dbl_minval;
114     double dbl_maxval;
115     double *dbl_ptr;
116     xp_double_option_setfunc_t dbl_setfunc;
117 
118     /* string option stuff */
119 
120 #define XP_STRING_OPTION_DUMMY \
121 	NULL, NULL, 0, NULL, NULL
122 
123     const char *str_defval;
124     char *str_ptr;
125     size_t str_size;
126     xp_string_option_setfunc_t str_setfunc;
127     xp_string_option_getfunc_t str_getfunc;
128 
129     /* key option stuff */
130 
131 #define XP_KEY_OPTION_DUMMY \
132 	NULL, NULL, KEY_DUMMY
133 
134     const char *key_defval;
135     char *key_string;
136     keys_t key;
137 
138     /* ... */
139 
140 };
141 
142 
143 /* number of options in global option array */
144 extern int num_options;
145 extern xp_option_t *options;
146 
147 extern void Parse_options(int *argcp, char **argvp);
148 
149 extern void Xpilotrc_get_filename(char *path, size_t size);
150 extern int Xpilotrc_read(const char *path);
151 extern int Xpilotrc_write(const char *path);
152 
153 extern bool Set_option(const char *name, const char *value,
154 		       xp_option_origin_t origin);
155 extern bool Set_noarg_option(xp_option_t *opt, bool value,
156 			     xp_option_origin_t origin);
157 extern bool Set_bool_option(xp_option_t *opt, bool value,
158 			    xp_option_origin_t origin);
159 extern bool Set_int_option(xp_option_t *opt, int value,
160 			   xp_option_origin_t origin);
161 extern bool Set_double_option(xp_option_t *opt, double value,
162 			      xp_option_origin_t origin);
163 extern bool Set_string_option(xp_option_t *opt, const char *value,
164 			      xp_option_origin_t origin);
165 
166 extern xp_option_t *Find_option(const char *name);
167 extern void Set_command(const char *command);
168 extern void Get_command(const char *command);
169 
170 extern void Usage(void);
171 extern const char *Get_keyHelpString(keys_t key);
172 extern const char *Get_keyResourceString(keys_t key);
173 extern const char *Option_value_to_string(xp_option_t *opt);
174 
175 void Store_option(xp_option_t *);
176 
177 #define STORE_OPTIONS(option_array) \
178 { \
179     int ii; \
180     for (ii = 0; ii < NELEM(option_array); ii++) \
181 	Store_option(& (option_array) [ii]); \
182 } \
183 
Option_get_name(xp_option_t * opt)184 static inline const char *Option_get_name(xp_option_t *opt)
185 {
186     assert(opt);
187     return opt->name;
188 }
189 
Option_get_type(xp_option_t * opt)190 static inline xp_option_type_t Option_get_type(xp_option_t *opt)
191 {
192     assert(opt);
193     return opt->type;
194 }
195 
Option_get_flags(xp_option_t * opt)196 static inline xp_option_flags_t Option_get_flags(xp_option_t *opt)
197 {
198     assert(opt);
199     return opt->flags;
200 }
201 
Option_get_help(xp_option_t * opt)202 static inline const char *Option_get_help(xp_option_t *opt)
203 {
204     assert(opt);
205     return opt->help;
206 }
207 
Option_get_origin(xp_option_t * opt)208 static inline xp_option_origin_t Option_get_origin(xp_option_t *opt)
209 {
210     assert(opt);
211     return opt->origin;
212 }
213 
Option_get_key(xp_option_t * opt)214 static inline keys_t Option_get_key(xp_option_t *opt)
215 {
216     assert(opt);
217     return opt->key;
218 }
219 
Option_get_private_data(xp_option_t * opt)220 static inline void *Option_get_private_data(xp_option_t *opt)
221 {
222     assert(opt);
223     return opt->private_data;
224 }
225 
Option_by_index(int ind)226 static inline xp_option_t *Option_by_index(int ind)
227 {
228     if (ind < 0 || ind >= num_options)
229 	return NULL;
230     return &options[ind];
231 }
232 
233 /*
234  * Macros for initalizing options.
235  */
236 
237 #define XP_NOARG_OPTION(name, valptr, flags, help) \
238 { \
239     xp_noarg_option,\
240 	name,\
241 	flags, xp_option_origin_default,\
242 	help,\
243 	NULL,\
244 	valptr,\
245 	XP_BOOL_OPTION_DUMMY,\
246 	XP_INT_OPTION_DUMMY,\
247 	XP_DOUBLE_OPTION_DUMMY,\
248 	XP_STRING_OPTION_DUMMY,\
249 	XP_KEY_OPTION_DUMMY,\
250 }
251 
252 #define XP_BOOL_OPTION(name, defval, valptr, setfunc, flags, help) \
253 { \
254     xp_bool_option,\
255 	name,\
256 	flags, xp_option_origin_default,\
257 	help,\
258 	NULL,\
259 	XP_NOARG_OPTION_DUMMY,\
260 	defval,\
261 	valptr,\
262 	setfunc,\
263 	XP_INT_OPTION_DUMMY,\
264 	XP_DOUBLE_OPTION_DUMMY,\
265 	XP_STRING_OPTION_DUMMY,\
266 	XP_KEY_OPTION_DUMMY,\
267 }
268 
269 #define XP_INT_OPTION(name, defval, minval, maxval, valptr, setfunc, flags, help) \
270 { \
271     xp_int_option,\
272 	name,\
273 	flags, xp_option_origin_default,\
274 	help,\
275 	NULL,\
276 	XP_NOARG_OPTION_DUMMY,\
277 	XP_BOOL_OPTION_DUMMY,\
278 	defval,\
279 	minval,\
280 	maxval,\
281 	valptr,\
282 	setfunc,\
283 	XP_DOUBLE_OPTION_DUMMY,\
284 	XP_STRING_OPTION_DUMMY,\
285 	XP_KEY_OPTION_DUMMY,\
286 }
287 
288 #define COLOR_INDEX_OPTION(name, defval, valptr, help) \
289 XP_INT_OPTION(name, defval, 0, MAX_COLORS-1, valptr, NULL, XP_OPTFLAG_CONFIG_COLORS, help)
290 
291 #define COLOR_INDEX_OPTION_WITH_SETFUNC(name, defval, valptr, setfunc, help) \
292 XP_INT_OPTION(name, defval, 0, MAX_COLORS-1, valptr, setfunc, XP_OPTFLAG_CONFIG_COLORS, help)
293 
294 
295 #define XP_DOUBLE_OPTION(name, defval, minval, maxval, valptr, setfunc, flags, help) \
296 { \
297     xp_double_option,\
298 	name,\
299 	flags, xp_option_origin_default,\
300 	help,\
301 	NULL,\
302 	XP_NOARG_OPTION_DUMMY,\
303 	XP_BOOL_OPTION_DUMMY,\
304 	XP_INT_OPTION_DUMMY,\
305 	defval,\
306 	minval,\
307 	maxval,\
308 	valptr,\
309 	setfunc,\
310 	XP_STRING_OPTION_DUMMY,\
311 	XP_KEY_OPTION_DUMMY,\
312 }
313 
314 #define XP_STRING_OPTION(name, defval, valptr, size, setfunc, private_data, getfunc, flags, help) \
315 { \
316     xp_string_option,\
317 	name,\
318 	flags, xp_option_origin_default,\
319 	help,\
320 	private_data,\
321 	XP_NOARG_OPTION_DUMMY,\
322 	XP_BOOL_OPTION_DUMMY,\
323 	XP_INT_OPTION_DUMMY,\
324 	XP_DOUBLE_OPTION_DUMMY,\
325 	defval,\
326 	valptr,\
327 	size,\
328 	setfunc,\
329 	getfunc,\
330 	XP_KEY_OPTION_DUMMY,\
331 }
332 
333 #define XP_KEY_OPTION(name, defval, key, help) \
334 { \
335     xp_key_option,\
336 	name,\
337 	XP_OPTFLAG_DEFAULT, xp_option_origin_default,\
338 	help,\
339 	NULL,\
340 	XP_NOARG_OPTION_DUMMY,\
341 	XP_BOOL_OPTION_DUMMY,\
342 	XP_INT_OPTION_DUMMY,\
343 	XP_DOUBLE_OPTION_DUMMY,\
344 	XP_STRING_OPTION_DUMMY,\
345 	defval,\
346 	NULL,\
347 	key,\
348 }
349 
350 #define XP_KS_UNKNOWN (-1)
351 typedef int xp_keysym_t;
352 /* no const because of mfc client */
353 extern xp_keysym_t String_to_xp_keysym(/*const*/ char *str);
354 extern keys_t Generic_lookup_key(xp_keysym_t ks, bool reset);
355 
356 typedef struct {
357     xp_keysym_t	keysym;
358     keys_t	key;
359 } xp_keydefs_t;
360 
361 extern xp_keydefs_t	*keydefs;
362 extern int		num_keydefs;
363 extern int		max_keydefs;
364 
365 
366 
367 #endif /* OPTION_H */
368