summaryrefslogtreecommitdiff
path: root/src/audacious/config.c
blob: 0bc49660ddc1cf31b9a7f1361ba4925682e33a6f (plain)
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
/*
 * config.c
 * Copyright 2011-2013 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include <glib.h>
#include <string.h>

#include <libaudcore/audstrings.h>
#include <libaudcore/hook.h>
#include <libaudcore/inifile.h>
#include <libaudcore/multihash.h>

#include "main.h"
#include "misc.h"

#define DEFAULT_SECTION "audacious"

static const char * const core_defaults[] = {

 /* general */
 "advance_on_delete", "FALSE",
 "clear_playlist", "TRUE",
 "open_to_temporary", "TRUE",
 "resume_playback_on_startup", "FALSE",
 "show_interface", "TRUE",

 /* equalizer */
 "eqpreset_default_file", "",
 "eqpreset_extension", "",
 "equalizer_active", "FALSE",
 "equalizer_autoload", "FALSE",
 "equalizer_bands", "0,0,0,0,0,0,0,0,0,0",
 "equalizer_preamp", "0",

 /* info popup / info window */
 "cover_name_exclude", "back",
 "cover_name_include", "album,cover,front,folder",
 "filepopup_delay", "5",
 "filepopup_showprogressbar", "TRUE",
 "recurse_for_cover", "FALSE",
 "recurse_for_cover_depth", "0",
 "show_filepopup_for_tuple", "TRUE",
 "use_file_cover", "FALSE",

 /* network */
 "use_proxy", "FALSE",
 "use_proxy_auth", "FALSE",

 /* output */
 "default_gain", "0",
 "enable_replay_gain", "TRUE",
 "enable_clipping_prevention", "TRUE",
 "output_bit_depth", "16",
 "output_buffer_size", "500",
 "replay_gain_album", "FALSE",
 "replay_gain_preamp", "0",
 "soft_clipping", "FALSE",
 "software_volume_control", "FALSE",
 "sw_volume_left", "100",
 "sw_volume_right", "100",

 /* playback */
 "no_playlist_advance", "FALSE",
 "repeat", "FALSE",
 "shuffle", "FALSE",
 "stop_after_current_song", "FALSE",

 /* playlist */
 "chardet_fallback", "ISO-8859-1",
#ifdef _WIN32
 "convert_backslash", "TRUE",
#else
 "convert_backslash", "FALSE",
#endif
 "generic_title_format", "${?artist:${artist} - }${?album:${album} - }${title}",
 "leading_zero", "FALSE",
 "metadata_on_play", "FALSE",
 "show_numbers_in_pl", "FALSE",

 NULL};

typedef enum {
    OP_IS_DEFAULT,
    OP_GET,
    OP_SET,
    OP_SET_NO_FLAG,
    OP_CLEAR,
    OP_CLEAR_NO_FLAG
} OpType;

typedef struct {
    const char * section;
    const char * key;
    const char * value;
} ConfigItem;

typedef struct {
    MultihashNode node;
    ConfigItem item;
} ConfigNode;

typedef struct {
    OpType type;
    ConfigItem item;
    unsigned hash;
    bool_t result;
} ConfigOp;

typedef struct {
    char * section;
} LoadState;

typedef struct {
    GArray * list;
} SaveState;

static unsigned item_hash (const ConfigItem * item)
{
    return g_str_hash (item->section) + g_str_hash (item->key);
}

/* assumes pooled strings */
static int item_compare (const ConfigItem * a, const ConfigItem * b)
{
    if (str_equal (a->section, b->section))
        return strcmp (a->key, b->key);
    else
        return strcmp (a->section, b->section);
}

/* assumes pooled strings */
static void item_clear (ConfigItem * item)
{
    str_unref ((char *) item->section);
    str_unref ((char *) item->key);
    str_unref ((char *) item->value);
}

static unsigned config_node_hash (const MultihashNode * node0)
{
    const ConfigNode * node = (const ConfigNode *) node0;

    return item_hash (& node->item);
}

static bool_t config_node_match (const MultihashNode * node0, const void * data, unsigned hash)
{
    const ConfigNode * node = (const ConfigNode *) node0;
    const ConfigItem * item = data;

    return ! strcmp (node->item.section, item->section) && ! strcmp (node->item.key, item->key);
}

static MultihashTable defaults = {
    .hash_func = config_node_hash,
    .match_func = config_node_match
};

static MultihashTable config = {
    .hash_func = config_node_hash,
    .match_func = config_node_match
};

static volatile bool_t modified;

static MultihashNode * add_cb (const void * data, unsigned hash, void * state)
{
    ConfigOp * op = state;

    switch (op->type)
    {
    case OP_IS_DEFAULT:
        op->result = ! op->item.value[0]; /* empty string is default */
        return NULL;

    case OP_SET:
        op->result = TRUE;
        modified = TRUE;

    case OP_SET_NO_FLAG:;
        ConfigNode * node = g_slice_new (ConfigNode);
        node->item.section = str_get (op->item.section);
        node->item.key = str_get (op->item.key);
        node->item.value = str_get (op->item.value);
        return (MultihashNode *) node;

    default:
        return NULL;
    }
}

static bool_t action_cb (MultihashNode * node0, void * state)
{
    ConfigNode * node = (ConfigNode *) node0;
    ConfigOp * op = state;

    switch (op->type)
    {
    case OP_IS_DEFAULT:
        op->result = ! strcmp (node->item.value, op->item.value);
        return FALSE;

    case OP_GET:
        op->item.value = str_ref (node->item.value);
        return FALSE;

    case OP_SET:
        op->result = !! strcmp (node->item.value, op->item.value);
        if (op->result)
            modified = TRUE;

    case OP_SET_NO_FLAG:
        str_unref ((char *) node->item.value);
        node->item.value = str_get (op->item.value);
        return FALSE;

    case OP_CLEAR:
        op->result = TRUE;
        modified = TRUE;

    case OP_CLEAR_NO_FLAG:
        item_clear (& node->item);
        g_slice_free (ConfigNode, node);
        return TRUE;

    default:
        return FALSE;
    }
}

static bool_t config_op_run (ConfigOp * op, OpType type, MultihashTable * table)
{
    if (! op->hash)
        op->hash = item_hash (& op->item);

    op->type = type;
    op->result = FALSE;
    multihash_lookup (table, & op->item, op->hash, add_cb, action_cb, op);
    return op->result;
}

static void load_heading (const char * section, void * data)
{
    LoadState * state = data;

    str_unref (state->section);
    state->section = str_get (section);
}

static void load_entry (const char * key, const char * value, void * data)
{
    LoadState * state = data;
    g_return_if_fail (state->section);

    ConfigOp op = {.item = {state->section, key, value}};
    config_op_run (& op, OP_SET_NO_FLAG, & config);
}

void config_load (void)
{
    char * folder = filename_to_uri (get_path (AUD_PATH_USER_DIR));
    SCONCAT2 (path, folder, "/config");
    str_unref (folder);

    if (vfs_file_test (path, VFS_EXISTS))
    {
        VFSFile * file = vfs_fopen (path, "r");

        if (file)
        {
            LoadState state = {0};

            inifile_parse (file, load_heading, load_entry, & state);

            str_unref (state.section);
            vfs_fclose (file);
        }
    }

    config_set_defaults (NULL, core_defaults);
}

static bool_t add_to_save_list (MultihashNode * node0, void * state0)
{
    ConfigNode * node = (ConfigNode *) node0;
    SaveState * state = state0;

    int pos = state->list->len;
    g_array_set_size (state->list, pos + 1);

    ConfigItem * copy = & g_array_index (state->list, ConfigItem, pos);
    copy->section = str_ref (node->item.section);
    copy->key = str_ref (node->item.key);
    copy->value = str_ref (node->item.value);

    modified = FALSE;

    return FALSE;
}

void config_save (void)
{
    if (! modified)
        return;

    SaveState state = {.list = g_array_new (FALSE, FALSE, sizeof (ConfigItem))};

    multihash_iterate (& config, add_to_save_list, & state);
    g_array_sort (state.list, (GCompareFunc) item_compare);

    char * folder = filename_to_uri (get_path (AUD_PATH_USER_DIR));
    SCONCAT2 (path, folder, "/config");
    str_unref (folder);

    VFSFile * file = vfs_fopen (path, "w");

    if (file)
    {
        const char * current_heading = NULL;

        for (int i = 0; i < state.list->len; i ++)
        {
            ConfigItem * item = & g_array_index (state.list, ConfigItem, i);

            if (! str_equal (item->section, current_heading))
            {
                inifile_write_heading (file, item->section);
                current_heading = item->section;
            }

            inifile_write_entry (file, item->key, item->value);
        }

        vfs_fclose (file);
    }

    g_array_set_clear_func (state.list, (GDestroyNotify) item_clear);
    g_array_free (state.list, TRUE);
}

void config_set_defaults (const char * section, const char * const * entries)
{
    if (! section)
        section = DEFAULT_SECTION;

    while (1)
    {
        const char * name = * entries ++;
        const char * value = * entries ++;
        if (! name || ! value)
            break;

        ConfigOp op = {.item = {section, name, value}};
        config_op_run (& op, OP_SET_NO_FLAG, & defaults);
    }
}

void config_cleanup (void)
{
    ConfigOp op = {.type = OP_CLEAR_NO_FLAG};
    multihash_iterate (& config, action_cb, & op);
    multihash_iterate (& defaults, action_cb, & op);
}

void set_str (const char * section, const char * name, const char * value)
{
    g_return_if_fail (name && value);

    ConfigOp op = {.item = {section ? section : DEFAULT_SECTION, name, value}};

    bool_t is_default = config_op_run (& op, OP_IS_DEFAULT, & defaults);
    bool_t changed = config_op_run (& op, is_default ? OP_CLEAR : OP_SET, & config);

    if (changed && ! section)
    {
        SCONCAT2 (event, "set ", name);
        event_queue (event, NULL);
    }
}

char * get_str (const char * section, const char * name)
{
    g_return_val_if_fail (name, NULL);

    ConfigOp op = {.item = {section ? section : DEFAULT_SECTION, name, NULL}};

    config_op_run (& op, OP_GET, & config);

    if (! op.item.value)
        config_op_run (& op, OP_GET, & defaults);

    return op.item.value ? (char *) op.item.value : str_get ("");
}

void set_bool (const char * section, const char * name, bool_t value)
{
    set_str (section, name, value ? "TRUE" : "FALSE");
}

bool_t get_bool (const char * section, const char * name)
{
    char * string = get_str (section, name);
    bool_t value = ! strcmp (string, "TRUE");
    str_unref (string);
    return value;
}

void set_int (const char * section, const char * name, int value)
{
    char * string = int_to_str (value);
    g_return_if_fail (string);
    set_str (section, name, string);
    str_unref (string);
}

int get_int (const char * section, const char * name)
{
    char * string = get_str (section, name);
    int value = str_to_int (string);
    str_unref (string);
    return value;
}

void set_double (const char * section, const char * name, double value)
{
    char * string = double_to_str (value);
    g_return_if_fail (string);
    set_str (section, name, string);
    str_unref (string);
}

double get_double (const char * section, const char * name)
{
    char * string = get_str (section, name);
    double value = str_to_double (string);
    str_unref (string);
    return value;
}