4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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.
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.
16 You should have received a copy of the GNU 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
31 #include <samplerate.h>
33 #include "daemon-conf.h"
37 #include "conf-parser.h"
39 #ifndef DEFAULT_CONFIG_DIR
40 #define DEFAULT_CONFIG_DIR "/etc/polypaudio"
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"
48 #define ENV_SCRIPT_FILE "POLYP_SCRIPT"
49 #define ENV_CONFIG_FILE "POLYP_CONFIG"
50 #define ENV_DL_SEARCH_PATH "POLYP_DLPATH"
52 static const struct pa_daemon_conf default_conf
= {
58 .disallow_module_loading
= 0,
60 .module_idle_time
= 20,
61 .scache_idle_time
= 20,
63 .script_commands
= NULL
,
64 .dl_search_path
= NULL
,
65 .default_script_file
= NULL
,
66 .log_target
= PA_LOG_SYSLOG
,
67 .resample_method
= SRC_SINC_FASTEST
,
71 struct pa_daemon_conf
* pa_daemon_conf_new(void) {
73 struct pa_daemon_conf
*c
= pa_xmemdup(&default_conf
, sizeof(default_conf
));
75 if ((f
= pa_open_config_file(DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_USER
, ENV_SCRIPT_FILE
, &c
->default_script_file
)))
79 c
->dl_search_path
= pa_xstrdup(DLSEARCHPATH
);
84 void pa_daemon_conf_free(struct pa_daemon_conf
*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
);
93 int pa_daemon_conf_set_log_target(struct pa_daemon_conf
*c
, const char *string
) {
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
;
110 int pa_daemon_conf_set_resample_method(struct pa_daemon_conf
*c
, const char *string
) {
114 if ((m
= pa_parse_resample_method(string
)) < 0)
117 c
->resample_method
= m
;
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
);
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
);
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
);
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
);
145 int pa_daemon_conf_load(struct pa_daemon_conf
*c
, const char *filename
) {
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
},
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
;
178 pa_xfree(c
->config_file
);
179 c
->config_file
= NULL
;
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
);
185 if (!f
&& errno
!= ENOENT
) {
186 pa_log(__FILE__
": WARNING: failed to open configuration file '%s': %s\n", filename
, strerror(errno
));
190 r
= f
? pa_config_parse(c
->config_file
, f
, table
, NULL
) : 0;
199 int pa_daemon_conf_env(struct pa_daemon_conf
*c
) {
202 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
203 pa_xfree(c
->dl_search_path
);
204 c
->dl_search_path
= pa_xstrdup(e
);
206 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
207 pa_xfree(c
->default_script_file
);
208 c
->default_script_file
= pa_xstrdup(e
);
214 char *pa_daemon_conf_dump(struct pa_daemon_conf
*c
) {
215 struct pa_strbuf
*s
= pa_strbuf_new();
217 static const char const* resample_methods
[] = {
219 "sinc-medium-quality",
226 pa_strbuf_printf(s
, "### Read from configuration file: %s ###\n", c
->config_file
);
228 pa_strbuf_printf(s
, "verbose = %i\n", !!c
->verbose
);
229 pa_strbuf_printf(s
, "daemonize = %i\n", !!c
->daemonize
);
230 pa_strbuf_printf(s
, "fail = %i\n", !!c
->fail
);
231 pa_strbuf_printf(s
, "high-priority = %i\n", !!c
->high_priority
);
232 pa_strbuf_printf(s
, "disallow-module-loading = %i\n", !!c
->disallow_module_loading
);
233 pa_strbuf_printf(s
, "exit-idle-time = %i\n", c
->exit_idle_time
);
234 pa_strbuf_printf(s
, "module-idle-time = %i\n", c
->module_idle_time
);
235 pa_strbuf_printf(s
, "scache-idle-time = %i\n", c
->scache_idle_time
);
236 pa_strbuf_printf(s
, "dl-search-path = %s\n", c
->dl_search_path
? c
->dl_search_path
: "");
237 pa_strbuf_printf(s
, "default-script-file = %s\n", c
->default_script_file
);
238 pa_strbuf_printf(s
, "log-target = %s\n", c
->auto_log_target
? "auto" : (c
->log_target
== PA_LOG_SYSLOG
? "syslog" : "stderr"));
240 assert(c
->resample_method
<= 4 && c
->resample_method
>= 0);
241 pa_strbuf_printf(s
, "resample-method = %s\n", resample_methods
[c
->resample_method
]);
243 return pa_strbuf_tostring_free(s
);