vo_corevideo: Make the Quit menu entry work
[mplayer/glamo.git] / m_config.c
blobcdfc3dd8c2c94935435f718547f8ea9f4209c09a
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /// \file
20 /// \ingroup Config
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include "talloc.h"
29 #ifdef MP_DEBUG
30 #include <assert.h>
31 #endif
33 #include "m_config.h"
34 #include "m_option.h"
35 #include "mp_msg.h"
37 #define MAX_PROFILE_DEPTH 20
39 static int
40 parse_profile(const m_option_t *opt, const char *name, const char *param, void *dst, int src);
42 static void
43 set_profile(const m_option_t *opt, void* dst, const void* src);
45 static int
46 show_profile(m_option_t *opt, char* name, char *param);
48 static void
49 m_config_add_option(m_config_t *config, const m_option_t *arg, const char* prefix);
51 static int
52 list_options(m_option_t *opt, char* name, char *param);
54 static void m_option_save(const m_config_t *config, const m_option_t *opt,
55 void *dst)
57 if (opt->type->save) {
58 const void *src = opt->new ? (char*)config->optstruct + opt->offset : opt->p;
59 opt->type->save(opt, dst, src);
63 static void m_option_set(const m_config_t *config, const m_option_t *opt,
64 const void *src)
66 if (opt->type->set) {
67 void *dst = opt->new ? (char*)config->optstruct + opt->offset : opt->p;
68 opt->type->set(opt, dst, src);
74 m_config_t *m_config_new(void *optstruct,
75 int includefunc(m_option_t *conf, char *filename))
77 m_config_t* config;
78 static int initialized = 0;
79 static m_option_type_t profile_opt_type;
80 static const m_option_t ref_opts[] = {
81 { "profile", NULL, &profile_opt_type, CONF_NOSAVE, 0, 0, NULL },
82 { "show-profile", show_profile, CONF_TYPE_PRINT_FUNC, CONF_NOCFG, 0, 0, NULL },
83 { "list-options", list_options, CONF_TYPE_PRINT_FUNC, CONF_NOCFG, 0, 0, NULL },
84 { NULL, NULL, NULL, 0, 0, 0, NULL }
86 int i;
88 config = talloc_zero(NULL, m_config_t);
89 config->lvl = 1; // 0 Is the defaults
90 if(!initialized) {
91 initialized = 1;
92 profile_opt_type = m_option_type_string_list;
93 profile_opt_type.parse = parse_profile;
94 profile_opt_type.set = set_profile;
96 m_option_t *self_opts = talloc_memdup(config, ref_opts, sizeof(ref_opts));
97 for (i = 0; self_opts[i].name; i++)
98 self_opts[i].priv = config;
99 m_config_register_options(config, self_opts);
100 if (includefunc) {
101 struct m_option *p = talloc_ptrtype(config, p);
102 *p = (struct m_option){"include", includefunc, CONF_TYPE_FUNC_PARAM,
103 CONF_NOSAVE, 0, 0, config};
104 m_config_add_option(config, p, NULL);
106 config->optstruct = optstruct;
108 return config;
111 void m_config_free(m_config_t* config)
113 m_config_option_t *opt;
114 for (opt = config->opts; opt; opt = opt->next) {
115 if (opt->flags & M_CFG_OPT_ALIAS)
116 continue;
117 m_config_save_slot_t *sl;
118 for (sl = opt->slots; sl; sl = sl->prev)
119 m_option_free(opt->opt, sl->data);
121 talloc_free(config);
124 void
125 m_config_push(m_config_t* config) {
126 m_config_option_t *co;
127 m_config_save_slot_t *slot;
129 #ifdef MP_DEBUG
130 assert(config != NULL);
131 assert(config->lvl > 0);
132 #endif
134 config->lvl++;
136 for(co = config->opts ; co ; co = co->next ) {
137 if(co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
138 continue;
139 if(co->opt->flags & (M_OPT_GLOBAL|M_OPT_NOSAVE))
140 continue;
141 if((co->opt->flags & M_OPT_OLD) && !(co->flags & M_CFG_OPT_SET))
142 continue;
143 if(co->flags & M_CFG_OPT_ALIAS)
144 continue;
146 // Update the current status
147 m_option_save(config, co->opt, co->slots->data);
149 // Allocate a new slot
150 slot = talloc_zero_size(co, sizeof(m_config_save_slot_t) +
151 co->opt->type->size);
152 slot->lvl = config->lvl;
153 slot->prev = co->slots;
154 co->slots = slot;
155 m_option_copy(co->opt,co->slots->data,co->slots->prev->data);
156 // Reset our set flag
157 co->flags &= ~M_CFG_OPT_SET;
160 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Config pushed level is now %d\n",config->lvl);
163 void
164 m_config_pop(m_config_t* config) {
165 m_config_option_t *co;
166 m_config_save_slot_t *slot;
168 #ifdef MP_DEBUG
169 assert(config != NULL);
170 assert(config->lvl > 1);
171 #endif
173 for(co = config->opts ; co ; co = co->next ) {
174 int pop = 0;
175 if(co->opt->type->flags & M_OPT_TYPE_HAS_CHILD)
176 continue;
177 if(co->opt->flags & (M_OPT_GLOBAL|M_OPT_NOSAVE))
178 continue;
179 if(co->flags & M_CFG_OPT_ALIAS)
180 continue;
181 if(co->slots->lvl > config->lvl)
182 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN,"Save slot found from lvl %d is too old: %d !!!\n",config->lvl,co->slots->lvl);
184 while(co->slots->lvl >= config->lvl) {
185 m_option_free(co->opt,co->slots->data);
186 slot = co->slots;
187 co->slots = slot->prev;
188 talloc_free(slot);
189 pop++;
191 if(pop) // We removed some ctx -> set the previous value
192 m_option_set(config, co->opt, co->slots->data);
195 config->lvl--;
196 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Config poped level=%d\n",config->lvl);
199 static void
200 m_config_add_option(m_config_t *config, const m_option_t *arg, const char* prefix) {
201 m_config_option_t *co;
202 m_config_save_slot_t* sl;
204 #ifdef MP_DEBUG
205 assert(config != NULL);
206 assert(config->lvl > 0);
207 assert(arg != NULL);
208 #endif
210 // Allocate a new entry for this option
211 co = talloc_zero_size(config, sizeof(m_config_option_t) + arg->type->size);
212 co->opt = arg;
214 // Fill in the full name
215 if(prefix && strlen(prefix) > 0) {
216 co->name = talloc_asprintf(co, "%s:%s", prefix, arg->name);
217 } else
218 co->name = arg->name;
220 // Option with children -> add them
221 if(arg->type->flags & M_OPT_TYPE_HAS_CHILD) {
222 const m_option_t *ol = arg->p;
223 int i;
224 co->slots = NULL;
225 for(i = 0 ; ol[i].name != NULL ; i++)
226 m_config_add_option(config,&ol[i], co->name);
227 } else {
228 m_config_option_t *i;
229 // Check if there is already an option pointing to this address
230 if(arg->p || arg->new && arg->offset >= 0) {
231 for(i = config->opts ; i ; i = i->next ) {
232 if (arg->new ? (i->opt->new && i->opt->offset == arg->offset)
233 : (!i->opt->new && i->opt->p == arg->p)) {
234 // So we don't save the same vars more than 1 time
235 co->slots = i->slots;
236 co->flags |= M_CFG_OPT_ALIAS;
237 break;
241 if(!(co->flags & M_CFG_OPT_ALIAS)) {
242 // Allocate a slot for the defaults
243 sl = talloc_zero_size(co, sizeof(m_config_save_slot_t) +
244 arg->type->size);
245 m_option_save(config, arg, sl->data);
246 // Hack to avoid too much trouble with dynamically allocated data :
247 // We always use a dynamic version
248 if ((arg->type->flags & M_OPT_TYPE_DYNAMIC)) {
249 char **hackptr = arg->new ? (char*)config->optstruct + arg->offset
250 : arg->p;
251 if (hackptr && *hackptr) {
252 *hackptr = NULL;
253 m_option_set(config, arg, sl->data);
256 sl->lvl = 0;
257 sl->prev = NULL;
258 co->slots = talloc_zero_size(co, sizeof(m_config_save_slot_t) +
259 arg->type->size);
260 co->slots->prev = sl;
261 co->slots->lvl = config->lvl;
262 m_option_copy(co->opt, co->slots->data, sl->data);
265 co->next = config->opts;
266 config->opts = co;
270 m_config_register_options(m_config_t *config, const m_option_t *args) {
271 int i;
273 #ifdef MP_DEBUG
274 assert(config != NULL);
275 assert(config->lvl > 0);
276 assert(args != NULL);
277 #endif
279 for(i = 0 ; args[i].name != NULL ; i++)
280 m_config_add_option(config,&args[i],NULL);
282 return 1;
285 static m_config_option_t*
286 m_config_get_co(m_config_t *config, char* arg) {
287 m_config_option_t *co;
289 for(co = config->opts ; co ; co = co->next ) {
290 int l = strlen(co->name) - 1;
291 if((co->opt->type->flags & M_OPT_TYPE_ALLOW_WILDCARD) &&
292 (co->name[l] == '*')) {
293 if(strncasecmp(co->name,arg,l) == 0)
294 return co;
295 } else if(strcasecmp(co->name,arg) == 0)
296 return co;
298 return NULL;
301 static int
302 m_config_parse_option(m_config_t *config, char* arg, char* param,int set) {
303 m_config_option_t *co;
304 int r = 0;
306 #ifdef MP_DEBUG
307 assert(config != NULL);
308 assert(config->lvl > 0);
309 assert(arg != NULL);
310 #endif
312 co = m_config_get_co(config,arg);
313 if(!co){
314 // mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Unknown option: %s\n",arg);
315 return M_OPT_UNKNOWN;
318 #ifdef MP_DEBUG
319 // This is the only mandatory function
320 assert(co->opt->type->parse);
321 #endif
323 // Check if this option isn't forbidden in the current mode
324 if((config->mode == M_CONFIG_FILE) && (co->opt->flags & M_OPT_NOCFG)) {
325 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,"The %s option can't be used in a config file.\n",arg);
326 return M_OPT_INVALID;
328 if((config->mode == M_COMMAND_LINE) && (co->opt->flags & M_OPT_NOCMD)) {
329 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,"The %s option can't be used on the command line.\n",arg);
330 return M_OPT_INVALID;
332 // During command line preparse set only pre-parse options
333 // Otherwise only set pre-parse option if they were not already set.
334 if(((config->mode == M_COMMAND_LINE_PRE_PARSE) &&
335 !(co->opt->flags & M_OPT_PRE_PARSE)) ||
336 ((config->mode != M_COMMAND_LINE_PRE_PARSE) &&
337 (co->opt->flags & M_OPT_PRE_PARSE) && (co->flags & M_CFG_OPT_SET)))
338 set = 0;
340 // Option with children are a bit different to parse
341 if(co->opt->type->flags & M_OPT_TYPE_HAS_CHILD) {
342 char** lst = NULL;
343 int i,sr;
344 // Parse the child options
345 r = m_option_parse(co->opt,arg,param,&lst,M_COMMAND_LINE);
346 // Set them now
347 if(r >= 0)
348 for(i = 0 ; lst && lst[2*i] ; i++) {
349 int l = strlen(co->name) + 1 + strlen(lst[2*i]) + 1;
350 if(r >= 0) {
351 // Build the full name
352 char n[l];
353 sprintf(n,"%s:%s",co->name,lst[2*i]);
354 sr = m_config_parse_option(config,n,lst[2*i+1],set);
355 if(sr < 0){
356 if(sr == M_OPT_UNKNOWN){
357 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,"Error: option '%s' has no suboption '%s'.\n",co->name,lst[2*i]);
358 r = M_OPT_INVALID;
359 } else
360 if(sr == M_OPT_MISSING_PARAM){
361 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,"Error: suboption '%s' of '%s' must have a parameter!\n",lst[2*i],co->name);
362 r = M_OPT_INVALID;
363 } else
364 r = sr;
367 free(lst[2*i]);
368 free(lst[2*i+1]);
370 if(lst) free(lst);
371 } else
372 r = m_option_parse(co->opt,arg,param,set ? co->slots->data : NULL,config->mode);
374 // Parsing failed ?
375 if(r < 0)
376 return r;
377 // Set the option
378 if(set) {
379 m_option_set(config, co->opt, co->slots->data);
380 co->flags |= M_CFG_OPT_SET;
383 return r;
387 m_config_set_option(m_config_t *config, char* arg, char* param) {
388 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Setting %s=%s\n",arg,param);
389 return m_config_parse_option(config,arg,param,1);
393 m_config_check_option(m_config_t *config, char* arg, char* param) {
394 int r;
395 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Checking %s=%s\n",arg,param);
396 r=m_config_parse_option(config,arg,param,0);
397 if(r==M_OPT_MISSING_PARAM){
398 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR,"Error: option '%s' must have a parameter!\n",arg);
399 return M_OPT_INVALID;
401 return r;
405 const m_option_t*
406 m_config_get_option(m_config_t *config, char* arg) {
407 m_config_option_t *co;
409 #ifdef MP_DEBUG
410 assert(config != NULL);
411 assert(config->lvl > 0);
412 assert(arg != NULL);
413 #endif
415 co = m_config_get_co(config,arg);
416 if(co)
417 return co->opt;
418 else
419 return NULL;
422 void
423 m_config_print_option_list(m_config_t *config) {
424 char min[50],max[50];
425 m_config_option_t* co;
426 int count = 0;
428 if(!config->opts) return;
430 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "\n Name Type Min Max Global CL Cfg\n\n");
431 for(co = config->opts ; co ; co = co->next) {
432 const m_option_t* opt = co->opt;
433 if(opt->type->flags & M_OPT_TYPE_HAS_CHILD) continue;
434 if(opt->flags & M_OPT_MIN)
435 sprintf(min,"%-8.0f",opt->min);
436 else
437 strcpy(min,"No");
438 if(opt->flags & M_OPT_MAX)
439 sprintf(max,"%-8.0f",opt->max);
440 else
441 strcpy(max,"No");
442 mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %-20.20s %-15.15s %-10.10s %-10.10s %-3.3s %-3.3s %-3.3s\n",
443 co->name,
444 co->opt->type->name,
445 min,
446 max,
447 opt->flags & CONF_GLOBAL ? "Yes" : "No",
448 opt->flags & CONF_NOCMD ? "No" : "Yes",
449 opt->flags & CONF_NOCFG ? "No" : "Yes");
450 count++;
452 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "\nTotal: %d options\n",count);
455 m_profile_t*
456 m_config_get_profile(m_config_t* config, char* name) {
457 m_profile_t* p;
458 for(p = config->profiles ; p ; p = p->next)
459 if(!strcmp(p->name,name)) return p;
460 return NULL;
463 m_profile_t*
464 m_config_add_profile(m_config_t* config, char* name) {
465 m_profile_t* p = m_config_get_profile(config,name);
466 if(p) return p;
467 p = talloc_zero(config, m_profile_t);
468 p->name = talloc_strdup(p, name);
469 p->next = config->profiles;
470 config->profiles = p;
471 return p;
474 void
475 m_profile_set_desc(m_profile_t* p, char* desc) {
476 talloc_free(p->desc);
477 p->desc = talloc_strdup(p, desc);
481 m_config_set_profile_option(m_config_t* config, m_profile_t* p,
482 char* name, char* val) {
483 int i = m_config_check_option(config,name,val);
484 if(i < 0) return i;
485 p->opts = talloc_realloc(p, p->opts, char *, 2*(p->num_opts+2));
486 p->opts[p->num_opts*2] = talloc_strdup(p, name);
487 p->opts[p->num_opts*2+1] = talloc_strdup(p, val);
488 p->num_opts++;
489 p->opts[p->num_opts*2] = p->opts[p->num_opts*2+1] = NULL;
490 return 1;
493 void
494 m_config_set_profile(m_config_t* config, m_profile_t* p) {
495 int i;
496 if(config->profile_depth > MAX_PROFILE_DEPTH) {
497 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN, "WARNING: Profile inclusion too deep.\n");
498 return;
500 config->profile_depth++;
501 for(i = 0 ; i < p->num_opts ; i++)
502 m_config_set_option(config,p->opts[2*i],p->opts[2*i+1]);
503 config->profile_depth--;
506 static int
507 parse_profile(const m_option_t *opt, const char *name, const char *param, void *dst, int src)
509 m_config_t* config = opt->priv;
510 char** list = NULL;
511 int i,r;
512 if(param && !strcmp(param,"help")) {
513 m_profile_t* p;
514 if(!config->profiles) {
515 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "No profiles have been defined.\n");
516 return M_OPT_EXIT-1;
518 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "Available profiles:\n");
519 for(p = config->profiles ; p ; p = p->next)
520 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\t%s\t%s\n",p->name,
521 p->desc ? p->desc : "");
522 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n");
523 return M_OPT_EXIT-1;
526 r = m_option_type_string_list.parse(opt,name,param,&list,src);
527 if(r < 0) return r;
528 if(!list || !list[0]) return M_OPT_INVALID;
529 for(i = 0 ; list[i] ; i++)
530 if(!m_config_get_profile(config,list[i])) {
531 mp_tmsg(MSGT_CFGPARSER, MSGL_WARN, "Unknown profile '%s'.\n",
532 list[i]);
533 r = M_OPT_INVALID;
535 if(dst)
536 m_option_copy(opt,dst,&list);
537 else
538 m_option_free(opt,&list);
539 return r;
542 static void
543 set_profile(const m_option_t *opt, void *dst, const void *src) {
544 m_config_t* config = opt->priv;
545 m_profile_t* p;
546 char** list = NULL;
547 int i;
548 if(!src || !*(char***)src) return;
549 m_option_copy(opt,&list,src);
550 for(i = 0 ; list[i] ; i++) {
551 p = m_config_get_profile(config,list[i]);
552 if(!p) continue;
553 m_config_set_profile(config,p);
555 m_option_free(opt,&list);
558 static int
559 show_profile(m_option_t *opt, char* name, char *param) {
560 m_config_t* config = opt->priv;
561 m_profile_t* p;
562 int i,j;
563 if(!param) return M_OPT_MISSING_PARAM;
564 if(!(p = m_config_get_profile(config,param))) {
565 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "Unknown profile '%s'.\n", param);
566 return M_OPT_EXIT-1;
568 if(!config->profile_depth)
569 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "Profile %s: %s\n", param,
570 p->desc ? p->desc : "");
571 config->profile_depth++;
572 for(i = 0 ; i < p->num_opts ; i++) {
573 char spc[config->profile_depth+1];
574 for(j = 0 ; j < config->profile_depth ; j++)
575 spc[j] = ' ';
576 spc[config->profile_depth] = '\0';
578 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "%s%s=%s\n", spc,
579 p->opts[2*i], p->opts[2*i+1]);
582 if(config->profile_depth < MAX_PROFILE_DEPTH &&
583 !strcmp(p->opts[2*i],"profile")) {
584 char* e,*list = p->opts[2*i+1];
585 while((e = strchr(list,','))) {
586 int l = e-list;
587 char tmp[l+1];
588 if(!l) continue;
589 memcpy(tmp,list,l);
590 tmp[l] = '\0';
591 show_profile(opt,name,tmp);
592 list = e+1;
594 if(list[0] != '\0')
595 show_profile(opt,name,list);
598 config->profile_depth--;
599 if(!config->profile_depth) mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n");
600 return M_OPT_EXIT-1;
603 static int
604 list_options(m_option_t *opt, char* name, char *param) {
605 m_config_t* config = opt->priv;
606 m_config_print_option_list(config);
607 return M_OPT_EXIT;