File: cfg_section.c

package info (click to toggle)
gmerlin 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,108 kB
  • sloc: ansic: 133,761; javascript: 6,967; makefile: 1,400; sh: 702; sed: 16
file content (616 lines) | stat: -rw-r--r-- 17,697 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*****************************************************************
 * gmerlin - a general purpose multimedia framework and applications
 *
 * Copyright (c) 2001 - 2024 Members of the Gmerlin project
 * http://github.com/bplaum
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/



#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <config.h>
#include <gmerlin/translation.h>


#include <gmerlin/cfg_registry.h>
#include <registry_priv.h>
#include <gmerlin/utils.h>

static void merge_func_item(void * priv, const char * name, const gavl_value_t * val)
  {
  gavl_dictionary_t * dst = priv;
  gavl_value_t * val_dst;

  if(*name == '$')
    return;
  
  if((val_dst = gavl_dictionary_get_nc(dst, name)))
    gavl_value_copy(val_dst, val);
  }

static void merge_func_child(void * priv, const char * name, const gavl_value_t * val)
  {
  const gavl_dictionary_t * src;
  gavl_dictionary_t * dst;

  if(*name == '$')
    return;
  
  if((src = gavl_value_get_dictionary(val)) &&
     (dst = gavl_dictionary_get_dictionary_nc(priv, name)))
    bg_cfg_section_merge(dst, src);
  }

void bg_cfg_section_merge(gavl_dictionary_t * dst,
                          const gavl_dictionary_t * src)
  {
  const gavl_dictionary_t * src_children;
  gavl_dictionary_t * dst_children;
  
  gavl_dictionary_foreach(src, merge_func_item, dst);

  if((src_children = gavl_dictionary_get_dictionary(src, BG_CFG_TAG_CHILDREN)) &&
     (dst_children = gavl_dictionary_get_dictionary_nc(dst, BG_CFG_TAG_CHILDREN)))
    gavl_dictionary_foreach(src_children, merge_func_child, dst_children);
  }

gavl_dictionary_t * bg_cfg_section_create(const char * name)
  {
  gavl_dictionary_t * ret = calloc(1, sizeof(*ret));
  gavl_dictionary_set_string(ret, BG_CFG_TAG_NAME, name);
  return ret;
  }

gavl_dictionary_t * bg_cfg_section_find_subsection(gavl_dictionary_t * s,
                                                   const char * name)
  {
  s = gavl_dictionary_get_dictionary_create(s, BG_CFG_TAG_CHILDREN);
  s = gavl_dictionary_get_dictionary_create(s, name);
  gavl_dictionary_set_string(s, BG_CFG_TAG_NAME, name);
  return s;
  }

void bg_cfg_section_delete_subsection_by_name(gavl_dictionary_t * s,
                                              const char * name)
  {
  if((s = gavl_dictionary_get_dictionary_nc(s, BG_CFG_TAG_CHILDREN)))
    gavl_dictionary_set(s, name, NULL);
  return;
  }

const gavl_dictionary_t * bg_cfg_section_find_subsection_c(const gavl_dictionary_t * s,
                                                          const char * name)
  {
  if((s = gavl_dictionary_get_dictionary(s, BG_CFG_TAG_CHILDREN)) &&
     (s = gavl_dictionary_get_dictionary(s, name)))
    return s;
  return NULL;
  }

int bg_cfg_section_has_subsection(const gavl_dictionary_t * s,
                                  const char * name)
  {
  if((s = gavl_dictionary_get_dictionary(s, BG_CFG_TAG_CHILDREN)) &&
     (s = gavl_dictionary_get_dictionary(s, name)))
    return 1;
  return 0;
  }

gavl_value_t * bg_cfg_section_find_item(gavl_dictionary_t * section,
                                         const bg_parameter_info_t * info)
  {
  gavl_value_t * ret;
  if(!(ret = gavl_dictionary_get_nc(section, info->name)))
    {
    gavl_dictionary_t param;
    
    gavl_dictionary_set(section, info->name, &info->val_default);
    ret = gavl_dictionary_get_nc(section, info->name);

    gavl_dictionary_init(&param);
    gavl_parameter_init(&param, info->name, info->long_name, info->type);
    bg_parameter_init_value(&param, ret);
    
    if(ret->type == GAVL_TYPE_UNDEFINED)
      gavl_value_set_type(ret, gavl_parameter_type_to_gavl(info->type));
    }
  
  return ret;
  }

static gavl_value_t * find_item_nocreate(gavl_dictionary_t * section,
                                          const bg_parameter_info_t * info)
  {
  return gavl_dictionary_get_nc(section, info->name);
  }

static const gavl_value_t * find_item_c(const gavl_dictionary_t * section,
                                         const bg_parameter_info_t * info)
  {
  return gavl_dictionary_get(section, info->name);
  }

/*
 *  Get/Set values
 */

void bg_cfg_section_set_parameter(gavl_dictionary_t * section,
                                  const bg_parameter_info_t * info,
                                  const gavl_value_t * value)
  {
  gavl_value_t * item;
  if(!value)
    return;
  item = bg_cfg_section_find_item(section, info);
  if(!item)
    return;
  
  gavl_value_copy(item, value);
  }



void bg_cfg_section_get_parameter(gavl_dictionary_t * section,
                                  const bg_parameter_info_t * info,
                                  gavl_value_t * value)
  {
  gavl_value_t * item;
  item = bg_cfg_section_find_item(section, info);

  if(!value || !item)
    return;

  gavl_value_copy(value, item);
  }

const gavl_value_t * bg_cfg_section_get_parameter_c(const gavl_dictionary_t * section,
                                                            const char * name)
  {
  return gavl_dictionary_get(section, name);
  }

void bg_cfg_section_destroy(gavl_dictionary_t * s)
  {
  gavl_dictionary_free(s);
  free(s);
  }

typedef struct
  {
  bg_set_parameter_func_t func;
  void * callback_data;
  
  }
apply_t;

static void apply_func(void * priv, const char * name,
                       const gavl_value_t * val)
  {
  apply_t * a;
  if(name && (*name == '$'))
    return;

  a = priv;
  a->func(a->callback_data, name, val);
  }

static void do_apply(const gavl_dictionary_t * section,
                     const bg_parameter_info_t * infos,
                     bg_set_parameter_func_t func,
                     void * callback_data, int terminate)
  {
  int num;
  const gavl_value_t * item;
  gavl_type_t type;
  num = 0;

  if(infos)
    {
    while(infos[num].name)
      {
      if(infos[num].type == BG_PARAMETER_BUTTON)
        {
        num++;
        continue;
        }
      
      item = find_item_c(section,&infos[num]);
    
      if(!item)
        {
        num++;

        if(infos[num].val_default.type)
          func(callback_data, infos[num].name, &infos[num].val_default);
      
        continue;
        }

      type = gavl_parameter_type_to_gavl(infos[num].type);

      if((item->type == GAVL_TYPE_STRING) &&
         (type != GAVL_TYPE_STRING))
        {
        gavl_value_t val;
        gavl_value_init(&val);

        val.type = type;
        gavl_value_from_string(&val, gavl_value_get_string(item));
        func(callback_data, infos[num].name, &val);
        gavl_value_free(&val);
        }
      else
        func(callback_data, infos[num].name, item);
      num++;
      }
    }
  else
    {
    apply_t a;
    a.func = func;
    a.callback_data = callback_data;
    gavl_dictionary_foreach(section, apply_func, &a);
    }
  if(terminate)
    func(callback_data, NULL, NULL);
  }

void bg_cfg_section_apply(const gavl_dictionary_t * section,
                          const bg_parameter_info_t * infos,
                          bg_set_parameter_func_t func,
                          void * callback_data)
  {
  do_apply(section, infos, func, callback_data, 1);
  }

void bg_cfg_section_apply_noterminate(gavl_dictionary_t * section,
                                      const bg_parameter_info_t * infos,
                                      bg_set_parameter_func_t func,
                                      void * callback_data)
  {
  do_apply(section, infos, func, callback_data, 0);
  }

/* Get parameter values, return 0 if no such entry */

int bg_cfg_section_get_parameter_int(const gavl_dictionary_t * section,
                                     const char * name, int * value)
  {
  return gavl_dictionary_get_int(section, name, value);
  }

int bg_cfg_section_get_parameter_float(const gavl_dictionary_t * section,
                                       const char * name, float * value)
  {
  double val_dbl = 0.0;
  if(gavl_dictionary_get_float(section, name, &val_dbl))
    {
    *value = val_dbl;
    return 1;
    }
  else
    return 0;
  }

int bg_cfg_section_get_parameter_string(const gavl_dictionary_t * section,
                                        const char * name,
                                        const char ** value)
  {
  if((*value = gavl_dictionary_get_string(section, name)))
    return 1;
  else
    return 0;
  }

int bg_cfg_section_get_parameter_time(const gavl_dictionary_t * section,
                                      const char * name, gavl_time_t * value)
  
  {
  return gavl_dictionary_get_long(section, name, value);
  }

/* Copy one config section to another */

gavl_dictionary_t * bg_cfg_section_copy(const gavl_dictionary_t * src)
  {
  gavl_dictionary_t * ret = calloc(1, sizeof(*ret));
  gavl_dictionary_copy(ret, src);
  return ret;
  }

void bg_cfg_section_transfer(gavl_dictionary_t * src, gavl_dictionary_t * dst)
  {
  /* Copy items */
  gavl_dictionary_copy(dst, src);
  }

const char * bg_cfg_section_get_name(gavl_dictionary_t * s)
  {
  if(!s)
    return NULL;
  return gavl_dictionary_get_string(s, BG_CFG_TAG_NAME);
  }


gavl_dictionary_t *
bg_cfg_section_create_from_parameters(const char * name,
                                      const bg_parameter_info_t * parameters)
  {
  gavl_dictionary_t * ret;
  ret = bg_cfg_section_create(name);
  bg_cfg_section_create_items(ret, parameters);
  return ret;
  }

void bg_cfg_section_create_items(gavl_dictionary_t * section,
                                 const bg_parameter_info_t * info)
  {
  gavl_dictionary_t params;
  gavl_dictionary_init(&params);
  gavl_parameter_info_append_static(&params, info);
  bg_cfg_section_set_from_params(section, &params);
  gavl_dictionary_free(&params);
  // create_items(section, info, 0);
  }

void bg_cfg_section_delete_subsection(gavl_dictionary_t * section,
                                      gavl_dictionary_t * subsection)
  {
  const char * name;
  if(!(section = gavl_dictionary_get_dictionary_create(section, BG_CFG_TAG_CHILDREN)) ||
     !(name = gavl_dictionary_get_string(subsection, BG_CFG_TAG_NAME)))
    return;
  gavl_dictionary_set(section, name, NULL);
  }

void bg_cfg_section_delete_subsections(gavl_dictionary_t * section)
  {
  if(gavl_dictionary_set(section, BG_CFG_TAG_CHILDREN, NULL))
    return;
  }

void bg_cfg_section_restore_defaults(gavl_dictionary_t * s,
                                     const bg_parameter_info_t * info)
  {
  gavl_value_t * item;
  int i;
  gavl_dictionary_t * subsection;
  gavl_dictionary_t * subsubsection;
  
  while(info->name)
    {
    if(info->flags & BG_PARAMETER_HIDE_DIALOG)
      {
      info++;
      continue;
      }
    item = find_item_nocreate(s, info);

    if(!item)
      {
      info++;
      continue;
      }
    
    gavl_value_copy(item, &info->val_default);
    
    if(info->multi_parameters && bg_cfg_section_has_subsection(s, info->name))
      {
      subsection = bg_cfg_section_find_subsection(s, info->name);
      i = 0;
      
      while(info->multi_names[i])
        {
        if(info->multi_parameters[i] &&
           bg_cfg_section_has_subsection(subsection, info->multi_names[i]))
          {
          subsubsection =
            bg_cfg_section_find_subsection(subsection, info->multi_names[i]);
          bg_cfg_section_restore_defaults(subsubsection,
                                          info->multi_parameters[i]);
          }
        i++;
        }
      }
    info++;
    }
  }

void bg_cfg_section_set_parameter_func(void * data,
                                       const char * name,
                                       const gavl_value_t * value)
  {
  if(name)
    gavl_dictionary_set(data, name, value);
  }

void bg_cfg_section_set_from_params(gavl_dictionary_t * section,
                                    const gavl_dictionary_t * params)
  {
  int num;
  int i;

  num = gavl_parameter_num_params(params);

  gavl_dictionary_set_string(section, BG_CFG_TAG_NAME,
                             gavl_dictionary_get_string(params, GAVL_META_NAME));
  
  for(i = 0; i < num; i++)
    {
    const char * name;
    const gavl_dictionary_t * param = gavl_parameter_get_param(params, i);
    gavl_value_t val;
    
    if(!(name = gavl_dictionary_get_string(param, GAVL_META_NAME)))
      continue;

    gavl_value_init(&val);
    
    if(bg_parameter_init_value(param, &val))
      gavl_dictionary_set(section, name, &val);
    
    }
  
  num = gavl_parameter_num_sections(params);
  for(i = 0; i < num; i++)
    {
    const char * name;
    gavl_dictionary_t * subsection;
    const gavl_dictionary_t * subparams = gavl_parameter_get_section(params, i);

    if(!(name = gavl_dictionary_get_string(subparams, GAVL_META_NAME)))
      continue;
    
    subsection =
      bg_cfg_section_find_subsection(section, name);
    
    bg_cfg_section_set_from_params(subsection, subparams);
    }
  
  }

int
bg_parameter_init_value(const gavl_dictionary_t * param,
                        gavl_value_t * val)
  {
  const gavl_value_t * val_default;
  int type = 0;
  
  if(!gavl_dictionary_get_int(param, GAVL_PARAMETER_TYPE, &type) ||
     !type)
    return 0;
    
  if((val_default = gavl_dictionary_get(param, GAVL_PARAMETER_DEFAULT)))
    {
    gavl_value_copy(val, val_default);
    return 1;
    }
  
  switch((gavl_parameter_type_t)type)
    {
    case GAVL_PARAMETER_SECTION:       //!< Dummy type. It contains no data but acts as a separator in notebook style configuration windows
      break;
    case GAVL_PARAMETER_CHECKBUTTON:   //!< Bool
    case GAVL_PARAMETER_INT:           //!< Integer spinbutton
    case GAVL_PARAMETER_SLIDER_INT:    //!< Integer slider
      gavl_value_set_int(val, 0);
      return 1;
      break;
    case GAVL_PARAMETER_FLOAT:         //!< Float spinbutton
    case GAVL_PARAMETER_SLIDER_FLOAT:  //!< Float slider
      gavl_value_set_float(val, 0.0);
      return 1;
      break;
    case GAVL_PARAMETER_STRING:        //!< String (one line only)
    case GAVL_PARAMETER_STRING_HIDDEN: //!< Encrypted string (displays as ***)
    case GAVL_PARAMETER_FONT:          //!< Font (contains fontconfig compatible fontname)
    case GAVL_PARAMETER_FILE:          //!< File
    case GAVL_PARAMETER_DIRECTORY:     //!< Directory
      gavl_value_set_string(val, NULL);
      return 1;
      break;
    case GAVL_PARAMETER_STRINGLIST:    //!< Popdown menu with string values
      /* Select first */
      {
      const gavl_dictionary_t * opt;
      if((opt = gavl_parameter_get_option(param, 0)))
        gavl_value_set_string(val, gavl_dictionary_get_string(opt, GAVL_META_NAME));
      return 1;
      }
      break;
    case GAVL_PARAMETER_DIRLIST:       //!< List of directories
    case GAVL_PARAMETER_MULTI_CHAIN:   //!< Several subitems (including suboptions) can be arranged in a chain
      gavl_value_set_array(val);      /* Empty array */
      return 1;
      break;
    case GAVL_PARAMETER_COLOR_RGB:     //!< RGB Color
      gavl_value_set_color_rgb(val);      /* Empty array */
      return 1;
      break;
    case GAVL_PARAMETER_COLOR_RGBA:    //!< RGBA Color
      gavl_value_set_color_rgba(val);      /* Empty array */
      return 1;
      break;
    case GAVL_PARAMETER_MULTI_MENU:    //!< Menu with config- and infobutton
      {
      /* Select first */
      gavl_dictionary_t * dict;
      const gavl_dictionary_t * opt;

      if((opt = gavl_parameter_get_option(param, 0)))
        {
        dict = gavl_value_set_dictionary(val);

        bg_cfg_section_set_from_params(dict, opt);
#if 0
        fprintf(stderr, "Got options\n");
        gavl_dictionary_dump(dict, 2);
        fprintf(stderr, "\nOPT:\n");
        gavl_dictionary_dump(opt, 2);
        fprintf(stderr, "\nParam:\n");
        gavl_dictionary_dump(param, 2);
        fprintf(stderr, "\n");
#endif
        return 1;
        }
      }
      break;
    case GAVL_PARAMETER_MULTI_LIST:    //!< List with config- and infobutton
      {
      /* Build array and parameters */
      gavl_array_t * arr;
      gavl_dictionary_t * dict;
      const gavl_dictionary_t * opt;
      int j;
      int num_options = gavl_parameter_num_options(param);
        
      arr = gavl_value_set_array(val);
        
      for(j = 0; j < num_options; j++)
        {
        if(!(opt = gavl_parameter_get_option(param, j)))
          break;

        dict = gavl_array_append_dictionary(arr);
            
        bg_cfg_section_set_from_params(dict, opt);
        }
#if 0
      fprintf(stderr, "Got multi list\n");
      gavl_array_dump(arr, 2);
      fprintf(stderr, "\n");
#endif
      return 1;
      }
      break;
    case GAVL_PARAMETER_TIME:          //!< Time
      gavl_value_set_long(val, 0);
      return 1;
      break;
    case GAVL_PARAMETER_POSITION:      //!< Position (x/y coordinates, scaled 0..1)
      gavl_value_set_position(val);
      return 1;
      break;
    case GAVL_PARAMETER_BUTTON:        //!< Pressing the button causes set_parameter to be called with NULL value
      gavl_value_set_string(val, NULL);
      return 1;
      break;
    }
  return 0;
  }