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 parse_log_target(const char *filename
, unsigned line
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
114 struct pa_daemon_conf
*c
= data
;
115 assert(filename
&& lvalue
&& rvalue
&& data
);
117 if (!strcmp(rvalue
, "auto"))
118 c
->auto_log_target
= 1;
119 else if (!strcmp(rvalue
, "syslog")) {
120 c
->auto_log_target
= 0;
121 c
->log_target
= PA_LOG_SYSLOG
;
122 } else if (!strcmp(rvalue
, "stderr")) {
123 c
->auto_log_target
= 0;
124 c
->log_target
= PA_LOG_STDERR
;
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 (!strcmp(rvalue
, "sinc-best-quality"))
138 c
->resample_method
= SRC_SINC_BEST_QUALITY
;
139 else if (!strcmp(rvalue
, "sinc-medium-quality"))
140 c
->resample_method
= SRC_SINC_MEDIUM_QUALITY
;
141 else if (!strcmp(rvalue
, "sinc-fastest"))
142 c
->resample_method
= SRC_SINC_FASTEST
;
143 else if (!strcmp(rvalue
, "zero-order-hold"))
144 c
->resample_method
= SRC_ZERO_ORDER_HOLD
;
145 else if (!strcmp(rvalue
, "linear"))
146 c
->resample_method
= SRC_LINEAR
;
148 pa_log(__FILE__
": [%s:%u] Inavalid resample method '%s'.\n", filename
, line
, rvalue
);
155 int pa_daemon_conf_load(struct pa_daemon_conf
*c
, const char *filename
) {
159 const struct pa_config_item table
[] = {
160 { "verbose", pa_config_parse_bool
, &c
->verbose
},
161 { "daemonize", pa_config_parse_bool
, &c
->daemonize
},
162 { "fail", pa_config_parse_bool
, &c
->fail
},
163 { "high-priority", pa_config_parse_bool
, &c
->high_priority
},
164 { "disallow-module-loading", pa_config_parse_bool
, &c
->disallow_module_loading
},
165 { "exit-idle-time", pa_config_parse_int
, &c
->exit_idle_time
},
166 { "module-idle-time", pa_config_parse_int
, &c
->module_idle_time
},
167 { "scache-idle-time", pa_config_parse_int
, &c
->scache_idle_time
},
168 { "dl-search-path", pa_config_parse_string
, &c
->dl_search_path
},
169 { "default-script-file", pa_config_parse_string
, &c
->default_script_file
},
170 { "log-target", parse_log_target
, c
},
171 { "resample-method", parse_resample_method
, c
},
172 { NULL
, NULL
, NULL
},
176 filename
= def
= default_file(ENV_CONFIG_FILE
, DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_USER
);
178 r
= pa_config_parse(filename
, table
, NULL
);
183 int pa_daemon_conf_env(struct pa_daemon_conf
*c
) {
186 if ((e
= getenv(ENV_DL_SEARCH_PATH
))) {
187 pa_xfree(c
->dl_search_path
);
188 c
->dl_search_path
= pa_xstrdup(e
);
190 if ((e
= getenv(ENV_SCRIPT_FILE
))) {
191 pa_xfree(c
->default_script_file
);
192 c
->default_script_file
= pa_xstrdup(e
);
198 char *pa_daemon_conf_dump(struct pa_daemon_conf
*c
) {
199 struct pa_strbuf
*s
= pa_strbuf_new();
202 static const char const* resample_methods
[] = {
204 "sinc-medium-quality",
210 d
= default_file(ENV_CONFIG_FILE
, DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_USER
);
211 pa_strbuf_printf(s
, "### Default configuration file: %s ###\n", d
);
213 pa_strbuf_printf(s
, "verbose = %i\n", !!c
->verbose
);
214 pa_strbuf_printf(s
, "daemonize = %i\n", !!c
->daemonize
);
215 pa_strbuf_printf(s
, "fail = %i\n", !!c
->fail
);
216 pa_strbuf_printf(s
, "high-priority = %i\n", !!c
->high_priority
);
217 pa_strbuf_printf(s
, "disallow-module-loading = %i\n", !!c
->disallow_module_loading
);
218 pa_strbuf_printf(s
, "exit-idle-time = %i\n", c
->exit_idle_time
);
219 pa_strbuf_printf(s
, "module-idle-time = %i\n", c
->module_idle_time
);
220 pa_strbuf_printf(s
, "scache-idle-time = %i\n", c
->scache_idle_time
);
221 pa_strbuf_printf(s
, "dl-search-path = %s\n", c
->dl_search_path
? c
->dl_search_path
: "");
222 pa_strbuf_printf(s
, "default-script-file = %s\n", c
->default_script_file
);
223 pa_strbuf_printf(s
, "log-target = %s\n", c
->auto_log_target
? "auto" : (c
->log_target
== PA_LOG_SYSLOG
? "syslog" : "stderr"));
225 assert(c
->resample_method
<= 4 && c
->resample_method
>= 0);
226 pa_strbuf_printf(s
, "resample-method = %s\n", resample_methods
[c
->resample_method
]);
230 return pa_strbuf_tostring_free(s
);