]> code.delx.au - pulseaudio/blob - polyp/daemon-conf.c
add resample_method option module-combine
[pulseaudio] / 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 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 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 #include <samplerate.h>
32
33 #include "daemon-conf.h"
34 #include "util.h"
35 #include "xmalloc.h"
36 #include "strbuf.h"
37 #include "conf-parser.h"
38
39 #ifndef DEFAULT_SCRIPT_FILE
40 #define DEFAULT_SCRIPT_FILE "/etc/polypaudio/default.pa"
41 #endif
42
43 #ifndef DEFAULT_SCRIPT_FILE_USER
44 #define DEFAULT_SCRIPT_FILE_USER ".polypaudio/default.pa"
45 #endif
46
47 #ifndef DEFAULT_CONFIG_FILE
48 #define DEFAULT_CONFIG_FILE "/etc/polypaudio/daemon.conf"
49 #endif
50
51 #ifndef DEFAULT_CONFIG_FILE_USER
52 #define DEFAULT_CONFIG_FILE_USER ".polypaudio/daemon.conf"
53 #endif
54
55 #define ENV_SCRIPT_FILE "POLYP_SCRIPT"
56 #define ENV_CONFIG_FILE "POLYP_CONFIG"
57 #define ENV_DL_SEARCH_PATH "POLYP_DLPATH"
58
59 static const struct pa_daemon_conf default_conf = {
60 .cmd = PA_CMD_DAEMON,
61 .daemonize = 0,
62 .fail = 1,
63 .verbose = 0,
64 .high_priority = 0,
65 .disallow_module_loading = 0,
66 .exit_idle_time = -1,
67 .module_idle_time = 20,
68 .scache_idle_time = 20,
69 .auto_log_target = 1,
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
75 };
76
77 char* default_file(const char *envvar, const char *global, const char *local) {
78 char *p, *h;
79
80 assert(envvar && global && local);
81
82 if ((p = getenv(envvar)))
83 return pa_xstrdup(p);
84
85 if ((h = getenv("HOME"))) {
86 p = pa_sprintf_malloc("%s/%s", h, local);
87 if (!access(p, F_OK))
88 return p;
89
90 pa_xfree(p);
91 }
92
93 return pa_xstrdup(global);
94 }
95
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);
99 #ifdef DLSEARCHPATH
100 c->dl_search_path = pa_xstrdup(DLSEARCHPATH);
101 #endif
102 return c;
103 }
104
105 void pa_daemon_conf_free(struct pa_daemon_conf *c) {
106 assert(c);
107 pa_xfree(c->script_commands);
108 pa_xfree(c->dl_search_path);
109 pa_xfree(c->default_script_file);
110 pa_xfree(c);
111 }
112
113 int pa_daemon_conf_set_log_target(struct pa_daemon_conf *c, const char *string) {
114 assert(c && string);
115
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;
124 } else
125 return -1;
126
127 return 0;
128 }
129
130 int pa_daemon_conf_set_resample_method(struct pa_daemon_conf *c, const char *string) {
131 int m;
132 assert(c && string);
133
134 if ((m = pa_parse_resample_method(string)) < 0)
135 return -1;
136
137 c->resample_method = m;
138 return 0;
139 }
140
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);
144
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);
147 return -1;
148 }
149
150 return 0;
151 }
152
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);
156
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);
159 return -1;
160 }
161
162 return 0;
163 }
164
165 int pa_daemon_conf_load(struct pa_daemon_conf *c, const char *filename) {
166 char *def = NULL;
167 int r;
168
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 },
183 };
184
185 if (!filename)
186 filename = def = default_file(ENV_CONFIG_FILE, DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER);
187
188 r = pa_config_parse(filename, table, NULL);
189 pa_xfree(def);
190 return r;
191 }
192
193 int pa_daemon_conf_env(struct pa_daemon_conf *c) {
194 char *e;
195
196 if ((e = getenv(ENV_DL_SEARCH_PATH))) {
197 pa_xfree(c->dl_search_path);
198 c->dl_search_path = pa_xstrdup(e);
199 }
200 if ((e = getenv(ENV_SCRIPT_FILE))) {
201 pa_xfree(c->default_script_file);
202 c->default_script_file = pa_xstrdup(e);
203 }
204
205 return 0;
206 }
207
208 char *pa_daemon_conf_dump(struct pa_daemon_conf *c) {
209 struct pa_strbuf *s = pa_strbuf_new();
210 char *d;
211
212 static const char const* resample_methods[] = {
213 "sinc-best-quality",
214 "sinc-medium-quality",
215 "sinc-fastest",
216 "zero-order-hold",
217 "linear"
218 };
219
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);
222
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"));
234
235 assert(c->resample_method <= 4 && c->resample_method >= 0);
236 pa_strbuf_printf(s, "resample-method = %s\n", resample_methods[c->resample_method]);
237
238 pa_xfree(d);
239
240 return pa_strbuf_tostring_free(s);
241 }