1/***
2  This file is part of PulseAudio.
3
4  PulseAudio is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as published
6  by the Free Software Foundation; either version 2.1 of the License,
7  or (at your option) any later version.
8
9  PulseAudio is distributed in the hope that it will be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  General Public License for more details.
13
14  You should have received a copy of the GNU Lesser General Public License
15  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
16***/
17
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include <string.h>
23#include <stdlib.h>
24#include <stdio.h>
25
26#include <gio/gio.h>
27#include <glib.h>
28
29#include <pulsecore/core-util.h>
30
31#define PA_GSETTINGS_MODULE_GROUP_SCHEMA "org.freedesktop.pulseaudio.module-group"
32#define PA_GSETTINGS_MODULE_GROUPS_SCHEMA "org.freedesktop.pulseaudio.module-groups"
33#define PA_GSETTINGS_MODULE_GROUPS_PATH "/org/freedesktop/pulseaudio/module-groups/"
34
35static void handle_module_group(gchar *name) {
36    GSettings *settings;
37    gchar p[1024];
38    gboolean enabled;
39    int i;
40
41    pa_snprintf(p, sizeof(p), PA_GSETTINGS_MODULE_GROUPS_PATH"%s/", name);
42
43    if (!(settings = g_settings_new_with_path(PA_GSETTINGS_MODULE_GROUP_SCHEMA,
44                                              p)))
45        return;
46
47    enabled = g_settings_get_boolean(settings, "enabled");
48
49    printf("%c%s%c", enabled ? '+' : '-', name, 0);
50
51    if (enabled) {
52        for (i = 0; i < 10; i++) {
53            gchar *n, *a;
54
55            pa_snprintf(p, sizeof(p), "name%d", i);
56            n = g_settings_get_string(settings, p);
57
58            pa_snprintf(p, sizeof(p), "args%i", i);
59            a = g_settings_get_string(settings, p);
60
61            printf("%s%c%s%c", n, 0, a, 0);
62
63            g_free(n);
64            g_free(a);
65        }
66
67        printf("%c", 0);
68    }
69
70    fflush(stdout);
71
72    g_object_unref(G_OBJECT(settings));
73}
74
75static void module_group_callback(GSettings *settings, gchar *key, gpointer user_data) {
76    handle_module_group(user_data);
77}
78
79int main(int argc, char *argv[]) {
80    GMainLoop *g;
81    GSettings *settings;
82    GPtrArray *groups;
83    gchar **group_names, **name;
84
85#if !GLIB_CHECK_VERSION(2,36,0)
86    g_type_init();
87#endif
88#if GLIB_CHECK_VERSION(2,68,0)
89    g_log_writer_default_set_use_stderr(true);
90#endif
91
92    /* gsettings-data-convert copies data from GConf to GSettings. The
93     * conversion is defined in the pulseaudio.convert file. The conversion is
94     * done only once, so running the command every time gsettings-helper
95     * starts is safe. */
96    g_spawn_command_line_sync("gsettings-data-convert", NULL, NULL, NULL, NULL);
97
98    if (!(settings = g_settings_new(PA_GSETTINGS_MODULE_GROUPS_SCHEMA)))
99        goto fail;
100
101    groups = g_ptr_array_new_full(0, g_object_unref);
102    group_names = g_settings_list_children(settings);
103
104    for (name = group_names; *name; name++) {
105        GSettings *child = g_settings_get_child(settings, *name);
106
107        /* The child may have been removed between the
108         * g_settings_list_children() and g_settings_get_child() calls. */
109        if (!child)
110            continue;
111
112        g_ptr_array_add(groups, child);
113        g_signal_connect(child, "changed", (GCallback) module_group_callback, *name);
114        handle_module_group(*name);
115    }
116
117    /* Signal the parent that we are now initialized */
118    printf("!");
119    fflush(stdout);
120
121    g = g_main_loop_new(NULL, FALSE);
122    g_main_loop_run(g);
123    g_main_loop_unref(g);
124
125    g_ptr_array_unref(groups);
126
127    /* group_names can't be freed earlier, because the values are being used as
128     * the user_data for module_group_callback(). */
129    g_strfreev(group_names);
130
131    g_object_unref(G_OBJECT(settings));
132
133    return 0;
134
135fail:
136    return 1;
137}
138