191e8d81e1aee4a4b1cc01d15676e48760a57703
[profile/ivi/pulseaudio-panda.git] / polyp / daemon-conf.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <unistd.h>
31
32 #include "daemon-conf.h"
33 #include "util.h"
34 #include "xmalloc.h"
35 #include "strbuf.h"
36 #include "conf-parser.h"
37 #include "resampler.h"
38
39 #ifndef DEFAULT_CONFIG_DIR
40 #define DEFAULT_CONFIG_DIR "/etc/polypaudio"
41 #endif
42
43 #define DEFAULT_SCRIPT_FILE DEFAULT_CONFIG_DIR"/default.pa"
44 #define DEFAULT_SCRIPT_FILE_USER ".polypaudio/default.pa"
45 #define DEFAULT_CONFIG_FILE DEFAULT_CONFIG_DIR"/daemon.conf"
46 #define DEFAULT_CONFIG_FILE_USER ".polypaudio/daemon.conf"
47
48 #define ENV_SCRIPT_FILE "POLYP_SCRIPT"
49 #define ENV_CONFIG_FILE "POLYP_CONFIG"
50 #define ENV_DL_SEARCH_PATH "POLYP_DLPATH"
51
52 static const struct pa_daemon_conf default_conf = {
53     .cmd = PA_CMD_DAEMON,
54     .daemonize = 0,
55     .fail = 1,
56     .verbose = 0,
57     .high_priority = 0,
58     .disallow_module_loading = 0,
59     .exit_idle_time = -1,
60     .module_idle_time = 20,
61     .scache_idle_time = 20,
62     .auto_log_target = 1,
63     .script_commands = NULL,
64     .dl_search_path = NULL,
65     .default_script_file = NULL,
66     .log_target = PA_LOG_SYSLOG,
67     .resample_method = PA_RESAMPLER_SRC_SINC_FASTEST,
68     .config_file = NULL,
69 };
70
71 struct pa_daemon_conf* pa_daemon_conf_new(void) {
72     FILE *f;
73     struct pa_daemon_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
74
75     if ((f = pa_open_config_file(DEFAULT_SCRIPT_FILE, DEFAULT_SCRIPT_FILE_USER, ENV_SCRIPT_FILE, &c->default_script_file)))
76         fclose(f);
77
78 #ifdef DLSEARCHPATH
79     c->dl_search_path = pa_xstrdup(DLSEARCHPATH);
80 #endif
81     return c;
82 }
83
84 void pa_daemon_conf_free(struct pa_daemon_conf *c) {
85     assert(c);
86     pa_xfree(c->script_commands);
87     pa_xfree(c->dl_search_path);
88     pa_xfree(c->default_script_file);
89     pa_xfree(c->config_file);
90     pa_xfree(c);
91 }
92
93 int pa_daemon_conf_set_log_target(struct pa_daemon_conf *c, const char *string) {
94     assert(c && string);
95
96     if (!strcmp(string, "auto"))
97         c->auto_log_target = 1;
98     else if (!strcmp(string, "syslog")) {
99         c->auto_log_target = 0;
100         c->log_target = PA_LOG_SYSLOG;
101     } else if (!strcmp(string, "stderr")) {
102         c->auto_log_target = 0;
103         c->log_target = PA_LOG_STDERR;
104     } else
105         return -1;
106
107     return 0;
108 }
109
110 int pa_daemon_conf_set_resample_method(struct pa_daemon_conf *c, const char *string) {
111     int m;
112     assert(c && string);
113
114     if ((m = pa_parse_resample_method(string)) < 0)
115         return -1;
116
117     c->resample_method = m;
118     return 0;
119 }
120
121 int parse_log_target(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, void *userdata) {
122     struct pa_daemon_conf *c = data;
123     assert(filename && lvalue && rvalue && data);
124
125     if (pa_daemon_conf_set_log_target(c, rvalue) < 0) {
126         pa_log(__FILE__": [%s:%u] Invalid log target '%s'.\n", filename, line, rvalue);
127         return -1;
128     }
129
130     return 0;
131 }
132
133 int parse_resample_method(const char *filename, unsigned line, const char *lvalue, const char *rvalue, void *data, void *userdata) {
134     struct pa_daemon_conf *c = data;
135     assert(filename && lvalue && rvalue && data);
136
137     if (pa_daemon_conf_set_resample_method(c, rvalue) < 0) {
138         pa_log(__FILE__": [%s:%u] Inavalid resample method '%s'.\n", filename, line, rvalue);
139         return -1;
140     }
141
142     return 0;
143 }
144
145 int pa_daemon_conf_load(struct pa_daemon_conf *c, const char *filename) {
146     int r = -1;
147     FILE *f = NULL;
148     
149     struct pa_config_item table[] = {
150         { "verbose",                 pa_config_parse_bool,    NULL },
151         { "daemonize",               pa_config_parse_bool,    NULL },
152         { "fail",                    pa_config_parse_bool,    NULL },
153         { "high-priority",           pa_config_parse_bool,    NULL },
154         { "disallow-module-loading", pa_config_parse_bool,    NULL },
155         { "exit-idle-time",          pa_config_parse_int,     NULL },
156         { "module-idle-time",        pa_config_parse_int,     NULL },
157         { "scache-idle-time",        pa_config_parse_int,     NULL },
158         { "dl-search-path",          pa_config_parse_string,  NULL },
159         { "default-script-file",     pa_config_parse_string,  NULL },
160         { "log-target",              parse_log_target,        NULL },
161         { "resample-method",         parse_resample_method,   NULL },
162         { NULL,                      NULL,                    NULL },
163     };
164     
165     table[0].data = &c->verbose;
166     table[1].data = &c->daemonize;
167     table[2].data = &c->fail;
168     table[3].data = &c->high_priority;
169     table[4].data = &c->disallow_module_loading;
170     table[5].data = &c->exit_idle_time;
171     table[6].data = &c->module_idle_time;
172     table[7].data = &c->scache_idle_time;
173     table[8].data = &c->dl_search_path;
174     table[9].data = &c->default_script_file;
175     table[10].data = c;
176     table[11].data = c;
177     
178     pa_xfree(c->config_file);
179     c->config_file = NULL;
180
181     f = filename ?
182         fopen(c->config_file = pa_xstrdup(filename), "r") :
183         pa_open_config_file(DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER, ENV_CONFIG_FILE, &c->config_file);
184
185     if (!f && errno != ENOENT) {
186         pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s\n", filename, strerror(errno));
187         goto finish;
188     }
189
190     r = f ? pa_config_parse(c->config_file, f, table, NULL) : 0;
191     
192 finish:
193     if (f)
194         fclose(f);
195     
196     return r;
197 }
198
199 int pa_daemon_conf_env(struct pa_daemon_conf *c) {
200     char *e;
201
202     if ((e = getenv(ENV_DL_SEARCH_PATH))) {
203         pa_xfree(c->dl_search_path);
204         c->dl_search_path = pa_xstrdup(e);
205     }
206     if ((e = getenv(ENV_SCRIPT_FILE))) {
207         pa_xfree(c->default_script_file);
208         c->default_script_file = pa_xstrdup(e);
209     }
210
211     return 0;
212 }
213
214 char *pa_daemon_conf_dump(struct pa_daemon_conf *c) {
215     struct pa_strbuf *s = pa_strbuf_new();
216
217     if (c->config_file)
218         pa_strbuf_printf(s, "### Read from configuration file: %s ###\n", c->config_file);
219     
220     pa_strbuf_printf(s, "verbose = %i\n", !!c->verbose);
221     pa_strbuf_printf(s, "daemonize = %i\n", !!c->daemonize);
222     pa_strbuf_printf(s, "fail = %i\n", !!c->fail);
223     pa_strbuf_printf(s, "high-priority = %i\n", !!c->high_priority);
224     pa_strbuf_printf(s, "disallow-module-loading = %i\n", !!c->disallow_module_loading);
225     pa_strbuf_printf(s, "exit-idle-time = %i\n", c->exit_idle_time);
226     pa_strbuf_printf(s, "module-idle-time = %i\n", c->module_idle_time);
227     pa_strbuf_printf(s, "scache-idle-time = %i\n", c->scache_idle_time);
228     pa_strbuf_printf(s, "dl-search-path = %s\n", c->dl_search_path ? c->dl_search_path : "");
229     pa_strbuf_printf(s, "default-script-file = %s\n", c->default_script_file);
230     pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr"));
231
232     assert(c->resample_method <= 4 && c->resample_method >= 0);
233     pa_strbuf_printf(s, "resample-method = %s\n", pa_resample_method_to_string(c->resample_method));
234     
235     return pa_strbuf_tostring_free(s);
236 }