spiv: Rewrite configuration, add config file.
[gfxprim/pasky.git] / demos / spiv / spiv_config.c
blob08b2f63b0a91ade5c9de59757fff845328109b66
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "cfg.h"
28 #include "image_actions.h"
29 #include "spiv_config.h"
32 * These are default config values, you can hardcompile yours here.
34 struct spiv_config config = {
35 .slideshow_delay = 0,
36 .show_info = 0,
37 .backend_init = "X11",
38 .emul_type = GP_PIXEL_UNKNOWN,
41 static int set_zoom_strategy(struct cfg_opt *self, unsigned int lineno)
43 (void) lineno;
44 //TODO!!!
45 printf("ZoomStrategy = %s\n", self->val);
47 return 0;
50 static int set_action(struct cfg_opt *self, unsigned int lineno)
52 (void) lineno;
53 image_action_set(atoi(self->key), self->val);
54 return 0;
57 static int set_opt(struct cfg_opt *self, unsigned int lineno)
59 (void) self;
60 (void) lineno;
62 switch (self->opt) {
63 case 'f':
64 config.floyd_steinberg = 1;
65 break;
66 case 'i':
67 config.show_info = 1;
68 break;
69 case 'p':
70 config.show_progress = 1;
71 break;
72 case 't':
73 config.timers = 1;
74 break;
77 return 0;
80 static int set_orientation(struct cfg_opt *self, unsigned int lineno)
82 if (!strcmp("0", self->val)) {
83 config.orientation = ROTATE_0;
84 return 0;
87 if (!strcmp("90", self->val)) {
88 config.orientation = ROTATE_90;
89 return 0;
92 if (!strcmp("180", self->val)) {
93 config.orientation = ROTATE_180;
94 return 0;
97 if (!strcmp("270", self->val)) {
98 config.orientation = ROTATE_270;
99 return 0;
102 fprintf(stderr, "ERROR: %u: Invalid orientation '%s'\n",
103 lineno, self->val);
104 return 1;
107 static int set_backend_init(struct cfg_opt *self, unsigned int lineno)
109 if (strlen(self->val) + 1 >= sizeof(config.backend_init)) {
110 fprintf(stderr, "ERROR: %u: Backend init string too long\n",
111 lineno);
112 return 1;
115 strcpy(config.backend_init, self->val);
117 return 0;
120 static int set_slideshow(struct cfg_opt *self, unsigned int lineno)
122 config.slideshow_delay = atof(self->val);
124 if (config.slideshow_delay == 0) {
125 fprintf(stderr, "ERROR: %u: Invalid slideshow delay '%s'\n",
126 lineno, self->val);
127 return 1;
130 return 0;
133 static int set_emulation(struct cfg_opt *self, unsigned int lineno)
135 config.emul_type = GP_PixelTypeByName(optarg);
137 if (config.emul_type == GP_PIXEL_UNKNOWN) {
138 fprintf(stderr, "ERROR: %u: Invalid pixel type '%s'\n",
139 lineno, self->val);
140 return 1;
143 return 0;
146 static int help(struct cfg_opt *self, unsigned int lineno)
148 (void) self;
149 (void) lineno;
151 print_help();
152 exit(0);
155 static int man(struct cfg_opt *self, unsigned int lineno)
157 (void) self;
158 (void) lineno;
160 print_man();
161 exit(0);
164 struct cfg_opt spiv_opts[] = {
165 {.name_space = NULL,
166 .key = NULL,
167 .opt = 'h',
168 .opt_long = "help",
169 .opt_has_value = 0,
170 .set = help,
171 .help = "Shows this help",
174 {.name_space = "Gui",
175 .key = "ShowInfo",
176 .opt = 'i',
177 .opt_long = "show-info",
178 .opt_has_value = 0,
179 .set = set_opt,
180 .help = "Show image info such as filename, size, etc...",
182 {.name_space = "Gui",
183 .key = "ShowProgress",
184 .opt = 'p',
185 .opt_long = "show-progress",
186 .set = set_opt,
187 .help = "Show progress bar when loading/resampling/... images",
189 {.name_space = "Gui",
190 .key = "SlideshowDelay",
191 .opt = 's',
192 .opt_long = "slideshow-delay",
193 .opt_has_value = 1,
194 .set = set_slideshow,
195 .help = "Delay between images in seconds (float) for slideshow",
197 {.name_space = "Gui",
198 .key = "UseFloydSteinberg",
199 .opt = 'f',
200 .opt_long = "floyd-steinberg",
201 .opt_has_value = 0,
202 .set = set_opt,
203 .help = "Turn on Floyd-Steinberg dithering",
205 {.name_space = "Gui",
206 .key = "Orientation",
207 .opt = 'o',
208 .opt_long = "orientation",
209 .opt_has_value = 1,
210 .set = set_orientation,
211 .help = "Orientation, one of 0, 90, 180, 270",
213 {.name_space = "Gui",
214 .key = "BackendInit",
215 .opt = 'b',
216 .opt_long = "backend-init",
217 .opt_has_value = 1,
218 .set = set_backend_init,
219 .help = "Backend init string, set it to 'help' for more info",
222 {.name_space = "Zoom",
223 .key = "ZoomStrategy",
224 .opt = 'z',
225 .opt_long = "zoom-strategy",
226 .opt_has_value = 1,
227 .set = set_zoom_strategy,
228 .help = "Zoom strategy",
232 {.name_space = "Actions",
233 .key = "1",
234 .opt = '1',
235 .opt_long = "action-1",
236 .opt_has_value = 1,
237 .set = set_action,
239 {.name_space = "Actions",
240 .key = "2",
241 .opt = '2',
242 .opt_long = "action-2",
243 .opt_has_value = 1,
244 .set = set_action,
246 {.name_space = "Actions",
247 .key = "3",
248 .opt = '3',
249 .opt_long = "action-3",
250 .opt_has_value = 1,
251 .set = set_action,
253 {.name_space = "Actions",
254 .key = "4",
255 .opt = '4',
256 .opt_long = "action-4",
257 .opt_has_value = 1,
258 .set = set_action,
260 {.name_space = "Actions",
261 .key = "5",
262 .opt = '5',
263 .opt_long = "action-5",
264 .opt_has_value = 1,
265 .set = set_action,
267 {.name_space = "Actions",
268 .key = "6",
269 .opt = '6',
270 .opt_long = "action-6",
271 .opt_has_value = 1,
272 .set = set_action,
274 {.name_space = "Actions",
275 .key = "7",
276 .opt = '7',
277 .opt_long = "action-7",
278 .opt_has_value = 1,
279 .set = set_action,
281 {.name_space = "Actions",
282 .key = "8",
283 .opt = '8',
284 .opt_long = "action-8",
285 .opt_has_value = 1,
286 .set = set_action,
288 {.name_space = "Actions",
289 .key = "9",
290 .opt = '9',
291 .opt_long = "action-9",
292 .opt_has_value = 1,
293 .set = set_action,
295 {.name_space = "Actions",
296 .key = "10",
297 .opt = '0',
298 .opt_long = "action-10",
299 .opt_has_value = 1,
300 .set = set_action,
301 .help = "Sets command line for action 1-10",
304 {.name_space = "Devel",
305 .key = "Timers",
306 .opt = 't',
307 .opt_long = "timers",
308 .opt_has_value = 0,
309 .set = set_opt,
310 .help = "Turns on cpu and wall clock measurement (printed to stdout)",
312 {.name_space = "Devel",
313 .key = "BackendEmulation",
314 .opt_long = "backend-emulation",
315 .opt_has_value = 1,
316 .set = set_emulation,
317 .help = "Emulate different backend pixel type (G1, G2, RGB555, ...)",
319 {.name_space = "Devel",
320 .key = NULL,
321 .opt_long = "print-man",
322 .opt_has_value = 0,
323 .set = man,
324 .help = "Prints spiv man page to stdout",
327 {NULL}
330 int spiv_config_load(const char *path)
332 return cfg_load(spiv_opts, path);
335 int spiv_config_parse_args(int argc, char *argv[])
337 return cfg_getopt(spiv_opts, argc, argv);
340 void spiv_config_print_help(void)
342 cfg_print_help(spiv_opts);
345 void spiv_config_print_man(void)
347 cfg_print_man(spiv_opts);