]> code.delx.au - pulseaudio/blob - src/pulse/client-conf.c
client-conf: Remove redundant function parameters
[pulseaudio] / src / pulse / client-conf.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/i18n.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/core-error.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/conf-parser.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/authkey.h>
40
41 #include "client-conf.h"
42
43 #define DEFAULT_CLIENT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "client.conf"
44 #define DEFAULT_CLIENT_CONFIG_FILE_USER "client.conf"
45
46 #define ENV_CLIENT_CONFIG_FILE "PULSE_CLIENTCONFIG"
47 #define ENV_DEFAULT_SINK "PULSE_SINK"
48 #define ENV_DEFAULT_SOURCE "PULSE_SOURCE"
49 #define ENV_DEFAULT_SERVER "PULSE_SERVER"
50 #define ENV_DAEMON_BINARY "PULSE_BINARY"
51 #define ENV_COOKIE_FILE "PULSE_COOKIE"
52
53 static const pa_client_conf default_conf = {
54 .daemon_binary = NULL,
55 .extra_arguments = NULL,
56 .default_sink = NULL,
57 .default_source = NULL,
58 .default_server = NULL,
59 .default_dbus_server = NULL,
60 .autospawn = true,
61 .disable_shm = false,
62 .cookie_file = NULL,
63 .cookie_valid = false,
64 .shm_size = 0,
65 .auto_connect_localhost = false,
66 .auto_connect_display = false
67 };
68
69 static int parse_cookie_file(pa_client_conf* c);
70
71 pa_client_conf *pa_client_conf_new(void) {
72 pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
73
74 c->daemon_binary = pa_xstrdup(PA_BINARY);
75 c->extra_arguments = pa_xstrdup("--log-target=syslog");
76 c->cookie_file = NULL;
77
78 return c;
79 }
80
81 void pa_client_conf_free(pa_client_conf *c) {
82 pa_assert(c);
83 pa_xfree(c->daemon_binary);
84 pa_xfree(c->extra_arguments);
85 pa_xfree(c->default_sink);
86 pa_xfree(c->default_source);
87 pa_xfree(c->default_server);
88 pa_xfree(c->default_dbus_server);
89 pa_xfree(c->cookie_file);
90 pa_xfree(c);
91 }
92
93 int pa_client_conf_load(pa_client_conf *c) {
94 FILE *f = NULL;
95 char *fn = NULL;
96 int r = -1;
97
98 /* Prepare the configuration parse table */
99 pa_config_item table[] = {
100 { "daemon-binary", pa_config_parse_string, &c->daemon_binary, NULL },
101 { "extra-arguments", pa_config_parse_string, &c->extra_arguments, NULL },
102 { "default-sink", pa_config_parse_string, &c->default_sink, NULL },
103 { "default-source", pa_config_parse_string, &c->default_source, NULL },
104 { "default-server", pa_config_parse_string, &c->default_server, NULL },
105 { "default-dbus-server", pa_config_parse_string, &c->default_dbus_server, NULL },
106 { "autospawn", pa_config_parse_bool, &c->autospawn, NULL },
107 { "cookie-file", pa_config_parse_string, &c->cookie_file, NULL },
108 { "disable-shm", pa_config_parse_bool, &c->disable_shm, NULL },
109 { "enable-shm", pa_config_parse_not_bool, &c->disable_shm, NULL },
110 { "shm-size-bytes", pa_config_parse_size, &c->shm_size, NULL },
111 { "auto-connect-localhost", pa_config_parse_bool, &c->auto_connect_localhost, NULL },
112 { "auto-connect-display", pa_config_parse_bool, &c->auto_connect_display, NULL },
113 { NULL, NULL, NULL, NULL },
114 };
115
116 if (!(f = pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn)))
117 if (errno != ENOENT)
118 goto finish;
119
120 r = f ? pa_config_parse(fn, f, table, NULL, NULL) : 0;
121
122 if (!r)
123 r = parse_cookie_file(c);
124
125 finish:
126 pa_xfree(fn);
127
128 if (f)
129 fclose(f);
130
131 return r;
132 }
133
134 int pa_client_conf_env(pa_client_conf *c) {
135 char *e;
136
137 if ((e = getenv(ENV_DEFAULT_SINK))) {
138 pa_xfree(c->default_sink);
139 c->default_sink = pa_xstrdup(e);
140 }
141
142 if ((e = getenv(ENV_DEFAULT_SOURCE))) {
143 pa_xfree(c->default_source);
144 c->default_source = pa_xstrdup(e);
145 }
146
147 if ((e = getenv(ENV_DEFAULT_SERVER))) {
148 pa_xfree(c->default_server);
149 c->default_server = pa_xstrdup(e);
150
151 /* We disable autospawning automatically if a specific server was set */
152 c->autospawn = false;
153 }
154
155 if ((e = getenv(ENV_DAEMON_BINARY))) {
156 pa_xfree(c->daemon_binary);
157 c->daemon_binary = pa_xstrdup(e);
158 }
159
160 if ((e = getenv(ENV_COOKIE_FILE))) {
161 return pa_client_conf_load_cookie_from_file(c, e);
162 }
163
164 return 0;
165 }
166
167 static int parse_cookie_file(pa_client_conf* c) {
168 int k;
169
170 pa_assert(c);
171
172 c->cookie_valid = false;
173
174 if (c->cookie_file)
175 k = pa_authkey_load_auto(c->cookie_file, true, c->cookie, sizeof(c->cookie));
176 else {
177 k = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE, false, c->cookie, sizeof(c->cookie));
178
179 if (k < 0) {
180 k = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE_FALLBACK, false, c->cookie, sizeof(c->cookie));
181
182 if (k < 0)
183 k = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE, true, c->cookie, sizeof(c->cookie));
184 }
185 }
186
187 if (k < 0)
188 return k;
189
190 c->cookie_valid = true;
191 return 0;
192 }
193
194 int pa_client_conf_load_cookie_from_hex(pa_client_conf* c, const char *cookie_in_hex) {
195 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
196
197 pa_assert(c);
198 pa_assert(cookie_in_hex);
199
200 if (pa_parsehex(cookie_in_hex, cookie, sizeof(cookie)) != sizeof(cookie)) {
201 pa_log(_("Failed to parse cookie data"));
202 return -PA_ERR_INVALID;
203 }
204
205 pa_xfree(c->cookie_file);
206 c->cookie_file = NULL;
207
208 return pa_client_conf_set_cookie(c, cookie, PA_NATIVE_COOKIE_LENGTH);
209 }
210
211 int pa_client_conf_load_cookie_from_file(pa_client_conf *c, const char *cookie_file_path) {
212 pa_assert(c);
213 pa_assert(cookie_file_path);
214
215 pa_xfree(c->cookie_file);
216 c->cookie_file = pa_xstrdup(cookie_file_path);
217 return parse_cookie_file(c);
218 }
219
220 int pa_client_conf_set_cookie(pa_client_conf *c, uint8_t *cookie, size_t cookie_size) {
221 pa_assert(c);
222 pa_assert(cookie);
223
224 if (cookie_size != PA_NATIVE_COOKIE_LENGTH)
225 return -PA_ERR_INVALID;
226 memcpy(c->cookie, cookie, cookie_size);
227 c->cookie_valid = true;
228 return 0;
229 }