Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
common/conf.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2010-2011, 2014 Henrik Andersson.
4 Copyright (C) 2010-2013 johannes hanika.
5 Copyright (C) 2010, 2012-2017 Tobias Ellinghaus.
6 Copyright (C) 2012 Christian Tellefsen.
7 Copyright (C) 2012 Jérémy Rosen.
8 Copyright (C) 2012 Richard Wonka.
9 Copyright (C) 2012 Simon Spannagel.
10 Copyright (C) 2012, 2014 Ulrich Pegelow.
11 Copyright (C) 2013 Jean-Sébastien Pédron.
12 Copyright (C) 2013 Jesper Pedersen.
13 Copyright (C) 2014 parafin.
14 Copyright (C) 2014, 2020-2021 Pascal Obry.
15 Copyright (C) 2014, 2016 Roman Lebedev.
16 Copyright (C) 2016-2018 Peter Budai.
17 Copyright (C) 2019 jakubfi.
18 Copyright (C) 2019, 2021 luzpaz.
19 Copyright (C) 2020 Hubert Kowalski.
20 Copyright (C) 2020 Philippe Weyland.
21 Copyright (C) 2021 Aldric Renaudin.
22 Copyright (C) 2021 Marco Carrarini.
23 Copyright (C) 2021 Mark-64.
24 Copyright (C) 2021 Ralf Brown.
25 Copyright (C) 2022 Hanno Schwalm.
26 Copyright (C) 2022 Martin Bařinka.
27 Copyright (C) 2025-2026 Aurélien PIERRE.
28 Copyright (C) 2026 Guillaume Stutin.
29
30 darktable is free software: you can redistribute it and/or modify
31 it under the terms of the GNU General Public License as published by
32 the Free Software Foundation, either version 3 of the License, or
33 (at your option) any later version.
34
35 darktable is distributed in the hope that it will be useful,
36 but WITHOUT ANY WARRANTY; without even the implied warranty of
37 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 GNU General Public License for more details.
39
40 You should have received a copy of the GNU General Public License
41 along with darktable. If not, see <http://www.gnu.org/licenses/>.
42*/
43
44#ifdef HAVE_CONFIG_H
45#include "config.h"
46#endif
47
48#include "math/calculator.h"
49#include "system/atomic.h" // dt_atomic_uint64, for the generation counter
50#include "common/paths.h" // DT_PATH_MAX
51#include "darktable.h"
53#include "math/math.h"
54#include "common/conf.h"
55
56#include <glib.h>
57#include <glib/gstdio.h>
58#include <glib/gprintf.h>
59#include <inttypes.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <strings.h>
63#include "common/utility.h"
64
65typedef struct dt_conf_dreggn_t
66{
67 GSList *result;
68 const char *match;
70
71static void _free_confgen_value(void *value)
72{
74 dt_free(s->def);
75 dt_free(s->min);
76 dt_free(s->max);
79 dt_free(s->longdesc);
80 dt_free(s);
81}
82
89static inline char *_dt_conf_get_var_locked(const char *name)
90{
91 char *str;
92
93 str = (char *)g_hash_table_lookup(darktable.conf->override_entries, name);
94 if(!IS_NULL_PTR(str)) return str;
95
96 str = (char *)g_hash_table_lookup(darktable.conf->table, name);
97 if(!IS_NULL_PTR(str)) return str;
98
99 // not found, try defaults
100 str = (char *)dt_confgen_get(name, DT_DEFAULT);
101 if(!IS_NULL_PTR(str))
102 {
103 char *str_new = g_strdup(str);
104 g_hash_table_insert(darktable.conf->table, g_strdup(name), str_new);
105 return str_new;
106 }
107
108 // FIXME: why insert garbage?
109 // still no luck? insert garbage:
110 str = (char *)g_malloc0(sizeof(int32_t));
111 g_hash_table_insert(darktable.conf->table, g_strdup(name), str);
112 return str;
113}
114
116static inline char *dt_conf_get_var(const char *name)
117{
118 dt_pthread_mutex_lock(&darktable.conf->mutex);
119 char *str = _dt_conf_get_var_locked(name);
121 return str;
122}
123
148/* Advances on every write that actually CHANGES a value. It exists so a consumer on a hot
149 * path can cache what it derived from conf and re-derive only when this number moves: one
150 * atomic load replaces taking the mutex below, which the GUI thread holds constantly.
151 *
152 * It is deliberately one number for the whole table rather than one per key. A per-key
153 * version would need its own lookup -- i.e. the lock again -- to be read, which is the cost
154 * it was meant to avoid. The price is that any conf write invalidates every cache; conf is
155 * written by user action, so that is rare and the re-derivation is what the cache already
156 * did on every call.
157 *
158 * It starts at 1, so a consumer may use 0 as "never read" without a separate flag. */
159static dt_atomic_uint64 _conf_generation = 1;
160
165
166static int dt_conf_set_if_not_overridden(const char *name, char *str)
167{
168 dt_pthread_mutex_lock(&darktable.conf->mutex);
169
170 char *over = (char *)g_hash_table_lookup(darktable.conf->override_entries, name);
171 int not_stored = (over && !strcmp(str, over));
172
173 if(!not_stored)
174 {
175 const char *current = (const char *)g_hash_table_lookup(darktable.conf->table, name);
176 if(!IS_NULL_PTR(current) && !strcmp(current, str))
177 {
178 // Unchanged: storing it would retire a perfectly good string for nothing.
179 not_stored = 1;
180 }
181 else
182 {
183 // Take the old entry out WITHOUT running the destroy functions, so we decide what
184 // happens to each half: the key was never handed to anyone and is ours to free, the
185 // value may still be held by a reader and is retired instead.
186 gpointer old_key = NULL;
187 gpointer old_value = NULL;
188 if(g_hash_table_steal_extended(darktable.conf->table, name, &old_key, &old_value))
189 {
190 dt_free(old_key);
191 if(!IS_NULL_PTR(old_value))
192 g_ptr_array_add(darktable.conf->retired_values, old_value);
193 }
194
195 g_hash_table_insert(darktable.conf->table, g_strdup(name), str);
196
197 /* Bumped under the lock, and only where the value really moved -- the two `not_stored'
198 * branches above are a pinned override and a write of the identical string, neither of
199 * which any consumer needs to re-derive from. */
201 }
202 }
203
205
206 return not_stored;
207}
208
209void dt_conf_set_int(const char *name, int val)
210{
211 char *str = g_strdup_printf("%d", val);
213 {
214 dt_free(str);
215 }
216}
217
218void dt_conf_set_int64(const char *name, int64_t val)
219{
220 char *str = g_strdup_printf("%" PRId64, val);
222 {
223 dt_free(str);
224 }
225}
226
227void dt_conf_set_float(const char *name, float val)
228{
229 char *str = (char *)g_malloc(G_ASCII_DTOSTR_BUF_SIZE);
230 g_ascii_dtostr(str, G_ASCII_DTOSTR_BUF_SIZE, val);
232 {
233 dt_free(str);
234 }
235}
236
237void dt_conf_set_bool(const char *name, int val)
238{
239 char *str = g_strdup(val ? "TRUE" : "FALSE");
241 {
242 dt_free(str);
243 }
244}
245
246void dt_conf_set_string(const char *name, const char *val)
247{
248 char *str = g_strdup(val);
250 {
251 dt_free(str);
252 }
253}
254
255void dt_conf_set_folder_from_file_chooser(const char *name, GtkFileChooser *chooser)
256{
257
258#ifdef WIN32
259 // for Windows native file chooser, gtk_file_chooser_get_current_folder()
260 // does not work, so we workaround
261 if(GTK_IS_FILE_CHOOSER_NATIVE(chooser))
262 {
263 gchar *pathname = gtk_file_chooser_get_filename(chooser);
264 if(pathname)
265 {
266 gchar *folder = g_path_get_dirname(pathname);
268 {
270 }
271 dt_free(pathname);
272 }
273 return;
274 }
275#endif
276
277 gchar *folder = gtk_file_chooser_get_current_folder(chooser);
279 {
281 }
282}
283
285{
286 const char *str = dt_conf_get_var(name);
287 float new_value = dt_calculator_solve(1, str);
288 if(isnan(new_value))
289 {
290 //we've got garbage, check default
291 const char *def_val = dt_confgen_get(name, DT_DEFAULT);
292 if(!IS_NULL_PTR(def_val))
293 {
294 new_value = dt_calculator_solve(1, def_val);
295 if(isnan(new_value))
296 new_value = 0.0;
297 else
298 {
299 char *fix_badval = g_strdup(def_val);
300 if(dt_conf_set_if_not_overridden(name, fix_badval))
301 {
302 dt_free(fix_badval);
303 }
304 }
305 }
306 else
307 {
308 new_value = 0.0;
309 }
310 }
311
312 int val;
313 if(new_value > 0)
314 val = new_value + 0.5;
315 else
316 val = new_value - 0.5;
317 return val;
318}
319
320int dt_conf_get_int(const char *name)
321{
322 const int min = dt_confgen_get_int(name, DT_MIN);
323 const int max = dt_confgen_get_int(name, DT_MAX);
324 const int val = dt_conf_get_int_fast(name);
325 const int ret = CLAMP(val, min, max);
326 return ret;
327}
328
329int64_t dt_conf_get_int64_fast(const char *name)
330{
331 const char *str = dt_conf_get_var(name);
332 float new_value = dt_calculator_solve(1, str);
333 if(isnan(new_value))
334 {
335 //we've got garbage, check default
336 const char *def_val = dt_confgen_get(name, DT_DEFAULT);
337 if(!IS_NULL_PTR(def_val))
338 {
339 new_value = dt_calculator_solve(1, def_val);
340 if(isnan(new_value))
341 new_value = 0.0;
342 else
343 {
344 char *fix_badval = g_strdup(def_val);
345 if(dt_conf_set_if_not_overridden(name, fix_badval))
346 {
347 dt_free(fix_badval);
348 }
349 }
350 }
351 else
352 {
353 new_value = 0.0;
354 }
355 }
356
357 int64_t val;
358 if(new_value > 0)
359 val = new_value + 0.5;
360 else
361 val = new_value - 0.5;
362 return val;
363}
364
365int64_t dt_conf_get_int64(const char *name)
366{
367 const int64_t min = dt_confgen_get_int64(name, DT_MIN);
368 const int64_t max = dt_confgen_get_int64(name, DT_MAX);
369 const int64_t val = dt_conf_get_int64_fast(name);
370 const int64_t ret = CLAMP(val, min, max);
371 return ret;
372}
373
374float dt_conf_get_float_fast(const char *name)
375{
376 const char *str = dt_conf_get_var(name);
377 float new_value = dt_calculator_solve(1, str);
378 if(isnan(new_value))
379 {
380 //we've got garbage, check default
381 const char *def_val = dt_confgen_get(name, DT_DEFAULT);
382 if(!IS_NULL_PTR(def_val))
383 {
384 new_value = dt_calculator_solve(1, def_val);
385 if(isnan(new_value))
386 new_value = 0.0;
387 else
388 {
389 char *fix_badval = g_strdup(def_val);
390 if(dt_conf_set_if_not_overridden(name, fix_badval))
391 {
392 dt_free(fix_badval);
393 }
394 }
395 }
396 else
397 {
398 new_value = 0.0;
399 }
400 }
401 return new_value;
402}
403
404float dt_conf_get_float(const char *name)
405{
406 const float min = dt_confgen_get_float(name, DT_MIN);
407 const float max = dt_confgen_get_float(name, DT_MAX);
408 const float val = dt_conf_get_float_fast(name);
409 const float ret = CLAMP(val, min, max);
410 return ret;
411}
412
413int dt_conf_get_and_sanitize_int(const char *name, int min, int max)
414{
415 const int cmin = dt_confgen_get_int(name, DT_MIN);
416 const int cmax = dt_confgen_get_int(name, DT_MAX);
417 const int val = dt_conf_get_int_fast(name);
418 const int ret = CLAMPS(val, MAX(min, cmin), MIN(max, cmax));
419 dt_conf_set_int(name, ret);
420 return ret;
421}
422
423int64_t dt_conf_get_and_sanitize_int64(const char *name, int64_t min, int64_t max)
424{
425 const int64_t cmin = dt_confgen_get_int64(name, DT_MIN);
426 const int64_t cmax = dt_confgen_get_int64(name, DT_MAX);
427 const int64_t val = dt_conf_get_int64_fast(name);
428 const int64_t ret = CLAMPS(val, MAX(min, cmin), MIN(max, cmax));
430 return ret;
431}
432
433float dt_conf_get_and_sanitize_float(const char *name, float min, float max)
434{
435 const float cmin = dt_confgen_get_float(name, DT_MIN);
436 const float cmax = dt_confgen_get_float(name, DT_MAX);
437 const float val = dt_conf_get_float_fast(name);
438 const float ret = CLAMPS(val, MAX(min, cmin), MIN(max, cmax));
440 return ret;
441}
442
443int dt_conf_get_bool(const char *name)
444{
445 const char *str = dt_conf_get_var(name);
446 const int val = (str[0] == 'T') || (str[0] == 't');
447 return val;
448}
449
450gchar *dt_conf_get_string(const char *name)
451{
452 // Copy the value before releasing the lock: dt_conf_get_var() hands back a pointer straight
453 // into the conf hash table, and a concurrent dt_conf_set_*() on the same key can free it
454 // (g_hash_table_insert() destroys the value it replaces) the moment the lock is released --
455 // a caller strdup'ing that pointer afterwards races the free. This was the mechanism behind a
456 // "write_sidecar_files silently flips to FALSE" report: dt_image_get_xmp_mode() reads this key
457 // from many threads per image, and a torn read masqueraded as an unrecognized/disabled value.
458 dt_pthread_mutex_lock(&darktable.conf->mutex);
459 const char *str = _dt_conf_get_var_locked(name);
460 gchar *result = g_strdup(str);
462 return result;
463}
464
465const char *dt_conf_get_string_const(const char *name)
466{
467 return dt_conf_get_var(name);
468}
469
470gboolean dt_conf_key_not_empty(const char *name)
471{
472 const char *val = dt_conf_get_string_const(name);
473 if(IS_NULL_PTR(val)) return FALSE;
474 if(strlen(val) == 0) return FALSE;
475 return TRUE;
476}
477
478gboolean dt_conf_get_folder_to_file_chooser(const char *name, GtkFileChooser *chooser)
479{
480 const gchar *folder = dt_conf_get_string_const(name);
481 if (!IS_NULL_PTR(folder))
482 {
483 gtk_file_chooser_set_current_folder(chooser, folder);
484 return TRUE;
485 }
486 return FALSE;
487}
488
489gboolean dt_conf_is_equal(const char *name, const char *value)
490{
491 const char *str = dt_conf_get_var(name);
492 return g_strcmp0(str, value) == 0;
493}
494
495static char *_sanitize_confgen(const char *name, const char *value)
496{
497 const dt_confgen_value_t *item = g_hash_table_lookup(darktable.conf->x_confgen, name);
498
499 if(IS_NULL_PTR(item)) return g_strdup(value);
500
501 char *result = NULL;
502
503 switch(item->type)
504 {
505 case DT_INT:
506 {
507 float v = dt_calculator_solve(1, value);
508
509 const int min = item->min ? (int)dt_calculator_solve(1, item->min) : INT_MIN;
510 const int max = item->max ? (int)dt_calculator_solve(1, item->max) : INT_MAX;
511 // if garbage, use default
512 const int val = isnan(v) ? dt_confgen_get_int(name, DT_DEFAULT) : (int)v;
513 result = g_strdup_printf("%d", CLAMP(val, min, max));
514 }
515 break;
516 case DT_INT64:
517 {
518 float v = dt_calculator_solve(1, value);
519
520 const int64_t min = item->min ? (int64_t)dt_calculator_solve(1, item->min) : INT64_MIN;
521 const int64_t max = item->max ? (int64_t)dt_calculator_solve(1, item->max) : INT64_MAX;
522 // if garbage, use default
523 const int64_t val = isnan(v) ? dt_confgen_get_int64(name, DT_DEFAULT) : (int64_t)v;
524 result = g_strdup_printf("%"PRId64, CLAMP(val, min, max));
525 }
526 break;
527 case DT_FLOAT:
528 {
529 float v = dt_calculator_solve(1, value);
530
531 const float min = item->min ? (float)dt_calculator_solve(1, item->min) : -FLT_MAX;
532 const float max = item->max ? (float)dt_calculator_solve(1, item->max) : FLT_MAX;
533 // if garbage, use default
534 const float val = isnan(v) ? dt_confgen_get_float(name, DT_DEFAULT) : v;
535 result = g_strdup_printf("%f", CLAMP(val, min, max));
536 }
537 break;
538 case DT_BOOL:
539 {
540 if(strcasecmp(value, "true") && strcasecmp(value, "false"))
541 result = g_strdup(dt_confgen_get(name, DT_DEFAULT));
542 else
543 result = g_strdup(value);
544 }
545 break;
546 case DT_ENUM:
547 {
548 char *v = g_strdup_printf("[%s]", value);
549 if(!strstr(item->enum_values, v))
550 result = g_strdup(dt_confgen_get(name, DT_DEFAULT));
551 else
552 result = g_strdup(value);
553 dt_free(v);
554 }
555 break;
556 default:
557 result = g_strdup(value);
558 break;
559 }
560
561 return result;
562}
563
564void dt_conf_init(dt_conf_t *cf, const char *filename, GSList *override_entries)
565{
566 cf->x_confgen = g_hash_table_new_full(g_str_hash, g_str_equal, dt_free_gpointer, _free_confgen_value);
567
568 cf->table = g_hash_table_new_full(g_str_hash, g_str_equal, dt_free_gpointer, dt_free_gpointer);
569 // Values replaced by a later write live on here until shutdown, because
570 // dt_conf_get_string_const() may have handed one out. See dt_conf_set_if_not_overridden().
571 cf->retired_values = g_ptr_array_new_with_free_func(g_free);
572 cf->override_entries = g_hash_table_new_full(g_str_hash, g_str_equal, dt_free_gpointer, dt_free_gpointer);
573 dt_pthread_mutex_init(&darktable.conf->mutex, NULL);
574
575 // init conf filename
576 g_strlcpy(darktable.conf->filename, filename, sizeof(darktable.conf->filename));
577
578#define LINE_SIZE 1023
579
580 char line[LINE_SIZE + 1];
581
582 FILE *f = NULL;
583
584 // check for user config
585 f = g_fopen(filename, "rb");
586
587 // if file has been found, parse it
588
589 if(!IS_NULL_PTR(f))
590 {
591 while(!feof(f))
592 {
593 const int read = fscanf(f, "%" STR(LINE_SIZE) "[^\r\n]\r\n", line);
594 if(read > 0)
595 {
596 char *c = line;
597 char *end = line + strlen(line);
598 // check for '=' which is separator between the conf name and value
599 while(*c != '=' && c < end) c++;
600
601 if(*c == '=')
602 {
603 *c = '\0';
604
605 char *name = g_strdup(line);
606 // ensure that numbers are properly clamped if min/max
607 // defined and if not and garbage is read then the default
608 // value is returned.
609 char *value = _sanitize_confgen(name, (const char *)(c + 1));
610
611 g_hash_table_insert(darktable.conf->table, name, value);
612 }
613 }
614 }
615 fclose(f);
616 }
617 else
618 {
619 // we initialize the conf table with default values
620 GHashTableIter iter;
621 gpointer key, value;
622
623 g_hash_table_iter_init (&iter, darktable.conf->x_confgen);
624 while (g_hash_table_iter_next (&iter, &key, &value))
625 {
626 const char *name = (const char *)key;
628 g_hash_table_insert(darktable.conf->table, g_strdup(name), g_strdup(entry->def));
629 }
630 }
631
632 if(!IS_NULL_PTR(override_entries))
633 {
634 for(GSList *p = override_entries; p; p = g_slist_next(p))
635 {
637 g_hash_table_insert(darktable.conf->override_entries, entry->key, entry->value);
638 }
639 }
640
641#undef LINE_SIZE
642
643 return;
644}
645
647int dt_conf_key_exists(const char *key)
648{
649 dt_pthread_mutex_lock(&darktable.conf->mutex);
650 const int res = (g_hash_table_lookup(darktable.conf->table, key) != NULL)
651 || (g_hash_table_lookup(darktable.conf->override_entries, key) != NULL);
654}
655
656static void _conf_add(char *key, char *val, dt_conf_dreggn_t *d)
657{
658 if(strncmp(key, d->match, strlen(d->match)) == 0)
659 {
661 nv->key = g_strdup(key + strlen(d->match) + 1);
662 nv->value = g_strdup(val);
663 d->result = g_slist_append(d->result, nv);
664 }
665}
666
668GSList *dt_conf_all_string_entries(const char *dir)
669{
670 dt_pthread_mutex_lock(&darktable.conf->mutex);
672 d.result = NULL;
673 d.match = dir;
674 g_hash_table_foreach(darktable.conf->table, (GHFunc)_conf_add, &d);
676 return d.result;
677}
678
680{
682 dt_free(nv->key);
683 dt_free(nv->value);
684 nv->key = NULL;
685 nv->value = NULL;
686 dt_free(nv);
687}
688
689gboolean dt_confgen_exists(const char *name)
690{
691 return g_hash_table_lookup(darktable.conf->x_confgen, name) != NULL;
692}
693
695{
696 const dt_confgen_value_t *item = g_hash_table_lookup(darktable.conf->x_confgen, name);
697
698 if(!IS_NULL_PTR(item))
699 return item->type;
700 else
701 return DT_STRING;
702}
703
705{
706 const dt_confgen_value_t *item = g_hash_table_lookup(darktable.conf->x_confgen, name);
707 if(IS_NULL_PTR(item))
708 return FALSE;
709
710 switch(kind)
711 {
712 case DT_DEFAULT:
713 return !IS_NULL_PTR(item->def);
714 case DT_MIN:
715 return !IS_NULL_PTR(item->min);
716 case DT_MAX:
717 return !IS_NULL_PTR(item->max);
718 case DT_VALUES:
719 return !IS_NULL_PTR(item->enum_values);
720 }
721 return FALSE;
722}
723
725{
726 const dt_confgen_value_t *item = g_hash_table_lookup(darktable.conf->x_confgen, name);
727
728 if(!IS_NULL_PTR(item))
729 {
730 switch(kind)
731 {
732 case DT_DEFAULT:
733 return item->def;
734 case DT_MIN:
735 return item->min;
736 case DT_MAX:
737 return item->max;
738 case DT_VALUES:
739 return item->enum_values;
740 }
741 }
742
743 return "";
744}
745
746const char *dt_confgen_get_label(const char *name)
747{
748 const dt_confgen_value_t *item = g_hash_table_lookup(darktable.conf->x_confgen, name);
749
750 if(!IS_NULL_PTR(item))
751 {
752 return item->shortdesc;
753 }
754
755 return "";
756}
757
758const char *dt_confgen_get_tooltip(const char *name)
759{
760 const dt_confgen_value_t *item = g_hash_table_lookup(darktable.conf->x_confgen, name);
761
762 if(!IS_NULL_PTR(item))
763 {
764 return item->longdesc;
765 }
766
767 return "";
768}
769
771{
773 {
774 //early bail
775 switch (kind)
776 {
777 case DT_MIN:
778 return INT_MIN;
779 break;
780 case DT_MAX:
781 return INT_MAX;
782 break;
783 default:
784 return 0;
785 break;
786 }
787 }
788 const char *str = dt_confgen_get(name, kind);
789
790 //if str is NULL or empty, dt_calculator_solve will return NAN
791 const float value = dt_calculator_solve(1, str);
792
793 switch (kind)
794 {
795 case DT_MIN:
796 return isnan(value) ? INT_MIN : (value > 0 ? value + 0.5f : value - 0.5f);
797 break;
798 case DT_MAX:
799 return isnan(value) ? INT_MAX : (value > 0 ? value + 0.5f : value - 0.5f);
800 break;
801 default:
802 return isnan(value) ? 0.0f : (value > 0 ? value + 0.5f : value - 0.5f);
803 break;
804 }
805 return (int)value;
806}
807
809{
811 {
812 //early bail
813 switch (kind)
814 {
815 case DT_MIN:
816 return INT64_MIN;
817 break;
818 case DT_MAX:
819 return INT64_MAX;
820 break;
821 default:
822 return 0;
823 break;
824 }
825 }
826 const char *str = dt_confgen_get(name, kind);
827
828 //if str is NULL or empty, dt_calculator_solve will return NAN
829 const float value = dt_calculator_solve(1, str);
830
831 switch (kind)
832 {
833 case DT_MIN:
834 return isnan(value) ? INT64_MIN : (value > 0 ? value + 0.5f : value - 0.5f);
835 break;
836 case DT_MAX:
837 return isnan(value) ? INT64_MAX : (value > 0 ? value + 0.5f : value - 0.5f);
838 break;
839 default:
840 return isnan(value) ? 0.0f : (value > 0 ? value + 0.5f : value - 0.5f);
841 break;
842 }
843 return (int64_t)value;
844}
845
847{
848 const char *str = dt_confgen_get(name, kind);
849 return !strcmp(str, "true");
850}
851
853{
855 {
856 //early bail
857 switch (kind)
858 {
859 case DT_MIN:
860 return -FLT_MAX;
861 break;
862 case DT_MAX:
863 return FLT_MAX;
864 break;
865 default:
866 break;
867 }
868 return 0.0f;
869 }
870
871 const char *str = dt_confgen_get(name, kind);
872
873 //if str is NULL or empty, dt_calculator_solve will return NAN
874 const float value = dt_calculator_solve(1, str);
875
876 switch (kind)
877 {
878 case DT_MIN:
879 // to anyone askig FLT_MIN is superclose to 0, not furthest value from 0 possible in float
880 return isnan(value) ? -FLT_MAX : value;
881 break;
882 case DT_MAX:
883 return isnan(value) ? FLT_MAX : value;
884 break;
885 default:
886 break;
887 }
888 return isnan(value) ? 0.0f : value;
889}
890
891gboolean dt_conf_is_default(const char *name)
892{
894 return TRUE; // well if default doesn't know about it, it's default
895
896 switch(dt_confgen_type(name))
897 {
898 case DT_INT:
900 break;
901 case DT_INT64:
903 break;
904 case DT_FLOAT:
906 break;
907 case DT_BOOL:
909 break;
910 case DT_PATH:
911 case DT_STRING:
912 case DT_ENUM:
913 default:
914 {
915 const char *def_val = dt_confgen_get(name, DT_DEFAULT);
916 const char *cur_val = dt_conf_get_var(name);
917 return g_strcmp0(def_val, cur_val) == 0;
918 break;
919 }
920 }
921}
922
923gchar* dt_conf_expand_default_dir(const char *dir)
924{
925 // expand special dirs
926#define CONFIG_DIR "$(config)"
927#define HOME_DIR "$(home)"
928
929 gchar *path = NULL;
930 if(g_str_has_prefix(dir, CONFIG_DIR))
931 {
932 gchar configdir[DT_PATH_MAX] = { 0 };
933 dt_loc_get_user_config_dir(configdir, sizeof(configdir));
934 path = g_strdup_printf("%s%s", configdir, dir + strlen(CONFIG_DIR));
935 }
936 else if(g_str_has_prefix(dir, HOME_DIR))
937 {
938 gchar *homedir = dt_loc_get_home_dir(NULL);
939 path = g_strdup_printf("%s%s", homedir, dir + strlen(HOME_DIR));
940 dt_free(homedir);
941 }
942 else path = g_strdup(dir);
943
944 gchar *normalized_path = dt_util_normalize_path(path);
945 dt_free(path);
946
947 return normalized_path;
948}
949
960static int _conf_save(dt_conf_t *cf)
961{
963
964 GList *sorted = g_list_sort(g_hash_table_get_keys(cf->table), (GCompareFunc)g_strcmp0);
965 GString *contents = g_string_new(NULL);
966 for(GList *iter = sorted; iter; iter = g_list_next(iter))
967 {
968 const gchar *key = (const gchar *)iter->data;
969 g_string_append_printf(contents, "%s=%s\n", key, (const gchar *)g_hash_table_lookup(cf->table, key));
970 }
971 g_list_free(sorted);
972
973 GError *error = NULL;
974 const gboolean written = g_file_set_contents(cf->filename, contents->str, contents->len, &error);
975 if(!written)
976 {
977 fprintf(stderr, "[conf] could not save the settings to `%s': %s\n", cf->filename, error->message);
978 g_error_free(error);
979 }
980
982 g_string_free(contents, TRUE);
983 return !written;
984}
985
987{
988 return _conf_save(darktable.conf);
989}
990
992{
993 _conf_save(cf);
994 g_hash_table_unref(cf->table);
995 g_hash_table_unref(cf->override_entries);
996 // Released last: every string here was handed out by dt_conf_get_string_const() at some
997 // point, so it stays alive for as long as any of the conf state does.
998 g_ptr_array_unref(cf->retired_values);
999 cf->retired_values = NULL;
1000 g_hash_table_unref(cf->x_confgen);
1001 dt_pthread_mutex_destroy(&darktable.conf->mutex);
1002}
1003
1004// clang-format off
1005// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
1006// vim: shiftwidth=2 expandtab tabstop=2 cindent
1007// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
1008// clang-format on
static void error(char *msg)
Definition ashift_lsd.c:202
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
uint64_t dt_atomic_get_uint64(const dt_atomic_uint64 *var)
void dt_atomic_set_uint64(dt_atomic_uint64 *var, uint64_t value)
float dt_calculator_solve(const float x, const char *formula)
Definition calculator.c:323
static const dt_adaptation_t kind
const float f
const float v
static const float const float const float min
const float max
void dt_conf_cleanup(dt_conf_t *cf)
Free everything cf owns. No dt_conf_*() call is valid afterwards.
void dt_conf_string_entry_free(gpointer data)
Free one dt_conf_string_entry_t. Shaped as a GDestroyNotify so it can be handed to g_slist_free_full(...
void dt_conf_set_bool(const char *name, int val)
int dt_conf_flush(void)
Write the settings to their file now. Overridden keys keep their stored value.
int dt_conf_get_bool(const char *name)
#define HOME_DIR
int dt_conf_key_exists(const char *key)
Does key have a value?
float dt_confgen_get_float(const char *name, dt_confgen_value_kind_t kind)
int64_t dt_conf_get_int64_fast(const char *name)
Stored 64-bit integer for name, NOT clamped.
static char * _sanitize_confgen(const char *name, const char *value)
void dt_conf_init(dt_conf_t *cf, const char *filename, GSList *override_entries)
Populate cf from filename and from the generated defaults.
void dt_conf_set_float(const char *name, float val)
float dt_conf_get_float(const char *name)
Float for name, clamped to its declared bounds.
const char * dt_confgen_get_tooltip(const char *name)
Translated long description of name, borrowed and valid for the process lifetime. NULL when name is n...
uint64_t dt_conf_generation(void)
A counter that advances whenever a conf value actually changes.
static void _conf_add(char *key, char *val, dt_conf_dreggn_t *d)
static int dt_conf_set_if_not_overridden(const char *name, char *str)
gchar * dt_conf_get_string(const char *name)
Read the stored string for name as a private copy.
int dt_conf_get_int_fast(const char *name)
Stored integer for name, NOT clamped to its declared bounds.
int dt_conf_get_and_sanitize_int(const char *name, int min, int max)
static void _free_confgen_value(void *value)
Definition common/conf.c:71
void dt_conf_set_int(const char *name, int val)
gboolean dt_confgen_get_bool(const char *name, dt_confgen_value_kind_t kind)
void dt_conf_set_int64(const char *name, int64_t val)
gchar * dt_conf_expand_default_dir(const char *dir)
Expand the placeholders in a default directory string.
GSList * dt_conf_all_string_entries(const char *dir)
Every key under dir, as a list of copies.
gboolean dt_conf_is_default(const char *name)
Is name still at the value declared in the XML? FALSE for an undeclared key.
static char * _dt_conf_get_var_locked(const char *name)
Definition common/conf.c:89
static dt_atomic_uint64 _conf_generation
Store str for name unless the command line pinned that key.
int dt_conf_get_int(const char *name)
Integer for name, clamped to the bounds declared in the XML.
int64_t dt_conf_get_int64(const char *name)
64-bit integer for name, clamped to its declared bounds.
gboolean dt_confgen_value_exists(const char *name, dt_confgen_value_kind_t kind)
int64_t dt_confgen_get_int64(const char *name, dt_confgen_value_kind_t kind)
gboolean dt_confgen_exists(const char *name)
int dt_confgen_get_int(const char *name, dt_confgen_value_kind_t kind)
#define LINE_SIZE
void dt_conf_set_string(const char *name, const char *val)
float dt_conf_get_float_fast(const char *name)
Stored float for name, NOT clamped.
const char * dt_conf_get_string_const(const char *name)
Borrow the stored string for name without copying it.
static char * dt_conf_get_var(const char *name)
const char * dt_confgen_get(const char *name, dt_confgen_value_kind_t kind)
One declared attribute of name from the generated XML, as text.
#define CONFIG_DIR
int64_t dt_conf_get_and_sanitize_int64(const char *name, int64_t min, int64_t max)
static int _conf_save(dt_conf_t *cf)
Write the whole table to cf->filename.
void dt_conf_set_folder_from_file_chooser(const char *name, GtkFileChooser *chooser)
dt_confgen_type_t dt_confgen_type(const char *name)
gboolean dt_conf_is_equal(const char *name, const char *value)
const char * dt_confgen_get_label(const char *name)
Translated short description of name, borrowed and valid for the process lifetime....
gboolean dt_conf_get_folder_to_file_chooser(const char *name, GtkFileChooser *chooser)
float dt_conf_get_and_sanitize_float(const char *name, float min, float max)
gboolean dt_conf_key_not_empty(const char *name)
dt_confgen_value_kind_t
@ DT_DEFAULT
@ DT_MAX
@ DT_MIN
@ DT_VALUES
dt_confgen_type_t
Definition common/conf.h:62
@ DT_BOOL
Definition common/conf.h:66
@ DT_STRING
Definition common/conf.h:68
@ DT_FLOAT
Definition common/conf.h:65
@ DT_INT64
Definition common/conf.h:64
@ DT_ENUM
Definition common/conf.h:69
@ DT_PATH
Definition common/conf.h:67
@ DT_INT
Definition common/conf.h:63
darktable_t darktable
Definition darktable.c:213
dev roi_request value
GtkWidget * folder
destination folder chooser
const int res
Definition dtpthread.h:351
static int dt_pthread_mutex_unlock(dt_pthread_mutex_t *mutex) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:127
static int dt_pthread_mutex_init(dt_pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
Initialise a mutex. With mutexattr NULL – which is how 54 of the 56 call sites in this tree spell it ...
Definition dtpthread.h:104
static int dt_pthread_mutex_destroy(dt_pthread_mutex_t *mutex)
Definition dtpthread.h:132
static int dt_pthread_mutex_lock(dt_pthread_mutex_t *mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:117
gchar * dt_loc_get_home_dir(const gchar *user)
void dt_loc_get_user_config_dir(char *configdir, size_t bufsize)
void * data
Definition geometry.h:111
#define STR(x)
Stringify x after macro-expanding it, e.g. STR(LINE) gives "42".
Definition macros.h:49
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
#define CLAMPS(A, L, H)
Definition math.h:78
static void dt_free_gpointer(gpointer ptr)
g_free() one pointer, with the signature GDestroyNotify wants.
Definition mem_alloc.h:184
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
Definition mem_alloc.h:171
char * key
g_ptr_array_add(names, NULL)
g_free(manifest_path)
#define DT_PATH_MAX
Buffer size for a filesystem path anywhere in Ansel.
Definition paths.h:57
const char * name
Definition pdf.h:90
unsigned __int64 uint64_t
Definition strptime.c:75
const char * match
Definition common/conf.c:68
One key/value pair handed back by dt_conf_all_string_entries().
char * key
char * value
The whole configuration state. One instance lives at darktable.conf.
GHashTable * override_entries
GPtrArray * retired_values
dt_pthread_mutex_t mutex
GHashTable * x_confgen
GHashTable * table
char filename[DT_PATH_MAX]
dt_confgen_type_t type
Definition common/conf.h:74
#define MIN(a, b)
Definition thinplate.c:32
#define MAX(a, b)
Definition thinplate.c:29
gchar * dt_util_normalize_path(const gchar *_input)
Definition utility.c:706