]> code.delx.au - pulseaudio/blob - src/pulse/client-conf.c
18fafe34889b76a7b39ce420ad72b6a3c88b357b
[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 #include <string.h>
31
32 #include <pulse/xmalloc.h>
33 #include <pulse/i18n.h>
34
35 #include <pulsecore/macro.h>
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/conf-parser.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/authkey.h>
41
42 #include "client-conf.h"
43
44 #define DEFAULT_CLIENT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "client.conf"
45 #define DEFAULT_CLIENT_CONFIG_FILE_USER "client.conf"
46
47 #define ENV_CLIENT_CONFIG_FILE "PULSE_CLIENTCONFIG"
48 #define ENV_DEFAULT_SINK "PULSE_SINK"
49 #define ENV_DEFAULT_SOURCE "PULSE_SOURCE"
50 #define ENV_DEFAULT_SERVER "PULSE_SERVER"
51 #define ENV_DAEMON_BINARY "PULSE_BINARY"
52 #define ENV_COOKIE_FILE "PULSE_COOKIE"
53
54 static const pa_client_conf default_conf = {
55 .daemon_binary = NULL,
56 .extra_arguments = NULL,
57 .default_sink = NULL,
58 .default_source = NULL,
59 .default_server = NULL,
60 .default_dbus_server = NULL,
61 .autospawn = TRUE,
62 .disable_shm = FALSE,
63 .cookie_file = NULL,
64 .cookie_valid = FALSE,
65 .shm_size = 0,
66 .auto_connect_localhost = FALSE,
67 .auto_connect_display = FALSE
68 };
69
70 pa_client_conf *pa_client_conf_new(void) {
71 pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
72
73 c->daemon_binary = pa_xstrdup(PA_BINARY);
74 c->extra_arguments = pa_xstrdup("--log-target=syslog");
75 c->cookie_file = pa_xstrdup(PA_NATIVE_COOKIE_FILE);
76
77 return c;
78 }
79
80 void pa_client_conf_free(pa_client_conf *c) {
81 pa_assert(c);
82 pa_xfree(c->daemon_binary);
83 pa_xfree(c->extra_arguments);
84 pa_xfree(c->default_sink);
85 pa_xfree(c->default_source);
86 pa_xfree(c->default_server);
87 pa_xfree(c->default_dbus_server);
88 pa_xfree(c->cookie_file);
89 pa_xfree(c);
90 }
91
92 int pa_client_conf_load(pa_client_conf *c, const char *filename) {
93 FILE *f = NULL;
94 char *fn = NULL;
95 int r = -1;
96
97 /* Prepare the configuration parse table */
98 pa_config_item table[] = {
99 { "daemon-binary", pa_config_parse_string, &c->daemon_binary, NULL },
100 { "extra-arguments", pa_config_parse_string, &c->extra_arguments, NULL },
101 { "default-sink", pa_config_parse_string, &c->default_sink, NULL },
102 { "default-source", pa_config_parse_string, &c->default_source, NULL },
103 { "default-server", pa_config_parse_string, &c->default_server, NULL },
104 { "default-dbus-server", pa_config_parse_string, &c->default_dbus_server, NULL },
105 { "autospawn", pa_config_parse_bool, &c->autospawn, NULL },
106 { "cookie-file", pa_config_parse_string, &c->cookie_file, NULL },
107 { "disable-shm", pa_config_parse_bool, &c->disable_shm, NULL },
108 { "enable-shm", pa_config_parse_not_bool, &c->disable_shm, NULL },
109 { "shm-size-bytes", pa_config_parse_size, &c->shm_size, NULL },
110 { "auto-connect-localhost", pa_config_parse_bool, &c->auto_connect_localhost, NULL },
111 { "auto-connect-display", pa_config_parse_bool, &c->auto_connect_display, NULL },
112 { NULL, NULL, NULL, NULL },
113 };
114
115 if (filename) {
116
117 if (!(f = pa_fopen_cloexec(filename, "r"))) {
118 pa_log(_("Failed to open configuration file '%s': %s"), fn, pa_cstrerror(errno));
119 goto finish;
120 }
121
122 fn = pa_xstrdup(fn);
123
124 } else {
125
126 if (!(f = pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn)))
127 if (errno != ENOENT)
128 goto finish;
129 }
130
131 r = f ? pa_config_parse(fn, f, table, NULL) : 0;
132
133 if (!r)
134 r = pa_client_conf_load_cookie(c);
135
136 finish:
137 pa_xfree(fn);
138
139 if (f)
140 fclose(f);
141
142 return r;
143 }
144
145 int pa_client_conf_env(pa_client_conf *c) {
146 char *e;
147
148 if ((e = getenv(ENV_DEFAULT_SINK))) {
149 pa_xfree(c->default_sink);
150 c->default_sink = pa_xstrdup(e);
151 }
152
153 if ((e = getenv(ENV_DEFAULT_SOURCE))) {
154 pa_xfree(c->default_source);
155 c->default_source = pa_xstrdup(e);
156 }
157
158 if ((e = getenv(ENV_DEFAULT_SERVER))) {
159 pa_xfree(c->default_server);
160 c->default_server = pa_xstrdup(e);
161
162 /* We disable autospawning automatically if a specific server was set */
163 c->autospawn = FALSE;
164 }
165
166 if ((e = getenv(ENV_DAEMON_BINARY))) {
167 pa_xfree(c->daemon_binary);
168 c->daemon_binary = pa_xstrdup(e);
169 }
170
171 if ((e = getenv(ENV_COOKIE_FILE))) {
172 pa_xfree(c->cookie_file);
173 c->cookie_file = pa_xstrdup(e);
174
175 return pa_client_conf_load_cookie(c);
176 }
177
178 return 0;
179 }
180
181 int pa_client_conf_load_cookie(pa_client_conf* c) {
182 pa_assert(c);
183
184 if (!c->cookie_file)
185 return -1;
186
187 c->cookie_valid = FALSE;
188
189 if (pa_authkey_load_auto(c->cookie_file, c->cookie, sizeof(c->cookie)) < 0)
190 return -1;
191
192 c->cookie_valid = TRUE;
193 return 0;
194 }