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_SCRIPT_FILE
40 #define DEFAULT_SCRIPT_FILE "/etc/polypaudio/default.pa"
43 #ifndef DEFAULT_SCRIPT_FILE_USER
44 #define DEFAULT_SCRIPT_FILE_USER ".polypaudio/default.pa"
47 #ifndef DEFAULT_CONFIG_FILE
48 #define DEFAULT_CONFIG_FILE "/etc/polypaudio/daemon.conf"
51 #ifndef DEFAULT_CONFIG_FILE_USER
52 #define DEFAULT_CONFIG_FILE_USER ".polypaudio/daemon.conf"
55 #define ENV_SCRIPT_FILE "POLYP_SCRIPT"
56 #define ENV_CONFIG_FILE "POLYP_CONFIG"
57 #define ENV_DL_SEARCH_PATH "POLYP_DLPATH"
59 static const struct pa_daemon_conf default_conf
= {
65 .disallow_module_loading
= 0,
67 .module_idle_time
= 20,
68 .scache_idle_time
= 20,
70 .script_commands
= NULL
,
71 .dl_search_path
= NULL
,
72 .default_script_file
= NULL
,
73 .log_target
= PA_LOG_SYSLOG
,
74 .resample_method
= SRC_SINC_FASTEST
77 char* default_file(const char *envvar
, const char *global
, const char *local
) {
80 assert(envvar
&& global
&& local
);
82 if ((p
= getenv(envvar
)))
85 if ((h
= getenv("HOME"))) {
86 p
= pa_sprintf_malloc("%s/%s", h
, local
);
93 return pa_xstrdup(global
);
96 struct pa_daemon_conf
* pa_daemon_conf_new(void) {
97 struct pa_daemon_conf
*c
= pa_xmemdup(&default_conf
, sizeof(default_conf
));
98 c
->default_script_file
= default_file(ENV_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_USER
);
100 c
->dl_search_path
= pa_xstrdup(DLSEARCHPATH
);
105 void pa_daemon_conf_free(struct pa_daemon_conf
*c
) {
107 pa_xfree(c
->script_commands
);
108 pa_xfree(c
->dl_search_path
);
109 pa_xfree(c
->default_script_file
);
113 int pa_daemon_conf_set_log_target(struct pa_daemon_conf
*c
, const char *string
) {
116 if (!strcmp(string
, "auto"))
117 c
->auto_log_target
= 1;
118 else if (!strcmp(string
, "syslog")) {
119 c
->auto_log_target
= 0;
120 c
->log_target
= PA_LOG_SYSLOG
;
121 } else if (!strcmp(string
, "stderr")) {
122 c
->auto_log_target
= 0;
123 c
->log_target
= PA_LOG_STDERR
;
130 int pa_daemon_conf_set_resample_method(struct pa_daemon_conf
*c
, const char *string
) {
134 if ((m
= pa_parse_resample_method(string
)) < 0)
137 c
->resample_method
= m
;
141 int parse_log_target(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
142 struct pa_daemon_conf
*c
= data
;
143 assert(filename
&& lvalue
&& rvalue
&& data
);
145 if (pa_daemon_conf_set_log_target(c
, rvalue
) < 0) {
146 pa_log(__FILE__
": [%s:%u] Invalid log target '%s'.\n", filename
, line
, rvalue
);
153 int parse_resample_method(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
154 struct pa_daemon_conf
*c
= data
;
155 assert(filename
&& lvalue
&& rvalue
&& data
);
157 if (pa_daemon_conf_set_resample_method(c
, rvalue
) < 0) {
158 pa_log(__FILE__
": [%s:%u] Inavalid resample method '%s'.\n", filename
, line
, rvalue
);
165 int pa_daemon_conf_load(struct pa_daemon_conf
*c
, const char *filename
) {
169 const struct pa_config_item table
[] = {
170 { "verbose", pa_config_parse_bool
, &c
->verbose
},
171 { "daemonize", pa_config_parse_bool
, &c
->daemonize
},
172 { "fail", pa_config_parse_bool
, &c
->fail
},
173 { "high-priority", pa_config_parse_bool
, &c
->high_priority
},
174 { "disallow-module-loading", pa_config_parse_bool
, &c
->disallow_module_loading
},
175 { "exit-idle-time", pa_config_parse_int
, &c
->exit_idle_time
},
176 { "module-idle-time", pa_config_parse_int
, &c
->module_idle_time
},
177 { "scache-idle-time", pa_config_parse_int
, &c
->scache_idle_time
},
178 { "dl-search-path", pa_config_parse_string
, &c
->dl_search_path
},
179 { "default-script-file", pa_config_parse_string
, &c
->default_script_file
},
180 { "log-target", parse_log_target
, c
},
181 { "resample-method", parse_resample_method
, c
},
182 { NULL
, NULL
, NULL
},
186 filename
= def
= default_file(ENV_CONFIG_FILE
, DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_USER
);
188 r
= pa_config_parse(filename
, table
, NULL
);
193 int pa_daemon_conf_env(struct pa_daemon_conf
*c
) {
196 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
197 pa_xfree(c
->dl_search_path
);
198 c
->dl_search_path
= pa_xstrdup(e
);
200 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
201 pa_xfree(c
->default_script_file
);
202 c
->default_script_file
= pa_xstrdup(e
);
208 char *pa_daemon_conf_dump(struct pa_daemon_conf
*c
) {
209 struct pa_strbuf
*s
= pa_strbuf_new();
212 static const char const* resample_methods
[] = {
214 "sinc-medium-quality",
220 d
= default_file(ENV_CONFIG_FILE
, DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_USER
);
221 pa_strbuf_printf(s
, "### Default configuration file: %s ###\n", d
);
223 pa_strbuf_printf(s
, "verbose = %i\n", !!c
->verbose
);
224 pa_strbuf_printf(s
, "daemonize = %i\n", !!c
->daemonize
);
225 pa_strbuf_printf(s
, "fail = %i\n", !!c
->fail
);
226 pa_strbuf_printf(s
, "high-priority = %i\n", !!c
->high_priority
);
227 pa_strbuf_printf(s
, "disallow-module-loading = %i\n", !!c
->disallow_module_loading
);
228 pa_strbuf_printf(s
, "exit-idle-time = %i\n", c
->exit_idle_time
);
229 pa_strbuf_printf(s
, "module-idle-time = %i\n", c
->module_idle_time
);
230 pa_strbuf_printf(s
, "scache-idle-time = %i\n", c
->scache_idle_time
);
231 pa_strbuf_printf(s
, "dl-search-path = %s\n", c
->dl_search_path
? c
->dl_search_path
: "");
232 pa_strbuf_printf(s
, "default-script-file = %s\n", c
->default_script_file
);
233 pa_strbuf_printf(s
, "log-target = %s\n", c
->auto_log_target
? "auto" : (c
->log_target
== PA_LOG_SYSLOG
? "syslog" : "stderr"));
235 assert(c
->resample_method
<= 4 && c
->resample_method
>= 0);
236 pa_strbuf_printf(s
, "resample-method = %s\n", resample_methods
[c
->resample_method
]);
240 return pa_strbuf_tostring_free(s
);