]> code.delx.au - pulseaudio/blob - polyp/daemon-conf.c
rename some more
[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 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);
116
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;
125 } else {
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 (!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;
147 else {
148 pa_log(__FILE__": [%s:%u] Inavalid resample method '%s'.\n", filename, line, rvalue);
149 return -1;
150 }
151
152 return 0;
153 }
154
155 int pa_daemon_conf_load(struct pa_daemon_conf *c, const char *filename) {
156 char *def = NULL;
157 int r;
158
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 },
173 };
174
175 if (!filename)
176 filename = def = default_file(ENV_CONFIG_FILE, DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER);
177
178 r = pa_config_parse(filename, table, NULL);
179 pa_xfree(def);
180 return r;
181 }
182
183 int pa_daemon_conf_env(struct pa_daemon_conf *c) {
184 char *e;
185
186 if ((e = getenv(ENV_DL_SEARCH_PATH))) {
187 pa_xfree(c->dl_search_path);
188 c->dl_search_path = pa_xstrdup(e);
189 }
190 if ((e = getenv(ENV_SCRIPT_FILE))) {
191 pa_xfree(c->default_script_file);
192 c->default_script_file = pa_xstrdup(e);
193 }
194
195 return 0;
196 }
197
198 char *pa_daemon_conf_dump(struct pa_daemon_conf *c) {
199 struct pa_strbuf *s = pa_strbuf_new();
200 char *d;
201
202 static const char const* resample_methods[] = {
203 "sinc-best-quality",
204 "sinc-medium-quality",
205 "sinc-fastest",
206 "zero-order-hold",
207 "linear"
208 };
209
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);
212
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"));
224
225 assert(c->resample_method <= 4 && c->resample_method >= 0);
226 pa_strbuf_printf(s, "resample-method = %s\n", resample_methods[c->resample_method]);
227
228 pa_xfree(d);
229
230 return pa_strbuf_tostring_free(s);
231 }