]> code.delx.au - pulseaudio/blob - src/pulse/client-conf.c
Make sure we print the file name we actually use.
[pulseaudio] / src / pulse / client-conf.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser 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 PulseAudio 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 Lesser General Public License
17 along with PulseAudio; 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 <stdlib.h>
27 #include <assert.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <string.h>
31
32 #include <pulsecore/core-error.h>
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/log.h>
36 #include <pulsecore/conf-parser.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/authkey.h>
39
40 #include "client-conf.h"
41
42 #ifndef DEFAULT_CONFIG_DIR
43 # ifndef OS_IS_WIN32
44 # define DEFAULT_CONFIG_DIR "/etc/pulse"
45 # else
46 # define DEFAULT_CONFIG_DIR "%PULSE_ROOT%"
47 # endif
48 #endif
49
50 #ifndef OS_IS_WIN32
51 # define PATH_SEP "/"
52 #else
53 # define PATH_SEP "\\"
54 #endif
55
56 #define DEFAULT_CLIENT_CONFIG_FILE DEFAULT_CONFIG_DIR PATH_SEP "client.conf"
57 #define DEFAULT_CLIENT_CONFIG_FILE_USER ".pulse" PATH_SEP "client.conf"
58
59 #define ENV_CLIENT_CONFIG_FILE "PULSE_CLIENTCONFIG"
60 #define ENV_DEFAULT_SINK "PULSE_SINK"
61 #define ENV_DEFAULT_SOURCE "PULSE_SOURCE"
62 #define ENV_DEFAULT_SERVER "PULSE_SERVER"
63 #define ENV_DAEMON_BINARY "PULSE_BINARY"
64 #define ENV_COOKIE_FILE "PULSE_COOKIE"
65
66 static const pa_client_conf default_conf = {
67 .daemon_binary = NULL,
68 .extra_arguments = NULL,
69 .default_sink = NULL,
70 .default_source = NULL,
71 .default_server = NULL,
72 .autospawn = 0,
73 .cookie_file = NULL,
74 .cookie_valid = 0
75 };
76
77 pa_client_conf *pa_client_conf_new(void) {
78 pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
79
80 c->daemon_binary = pa_xstrdup(PULSEAUDIO_BINARY);
81 c->extra_arguments = pa_xstrdup("--log-target=syslog --exit-idle-time=5");
82 c->cookie_file = pa_xstrdup(PA_NATIVE_COOKIE_FILE);
83
84 return c;
85 }
86
87 void pa_client_conf_free(pa_client_conf *c) {
88 assert(c);
89 pa_xfree(c->daemon_binary);
90 pa_xfree(c->extra_arguments);
91 pa_xfree(c->default_sink);
92 pa_xfree(c->default_source);
93 pa_xfree(c->default_server);
94 pa_xfree(c->cookie_file);
95 pa_xfree(c);
96 }
97 int pa_client_conf_load(pa_client_conf *c, const char *filename) {
98 FILE *f = NULL;
99 char *fn = NULL;
100 int r = -1;
101
102 /* Prepare the configuration parse table */
103 pa_config_item table[] = {
104 { "daemon-binary", pa_config_parse_string, NULL },
105 { "extra-arguments", pa_config_parse_string, NULL },
106 { "default-sink", pa_config_parse_string, NULL },
107 { "default-source", pa_config_parse_string, NULL },
108 { "default-server", pa_config_parse_string, NULL },
109 { "autospawn", pa_config_parse_bool, NULL },
110 { "cookie-file", pa_config_parse_string, NULL },
111 { NULL, NULL, NULL },
112 };
113
114 table[0].data = &c->daemon_binary;
115 table[1].data = &c->extra_arguments;
116 table[2].data = &c->default_sink;
117 table[3].data = &c->default_source;
118 table[4].data = &c->default_server;
119 table[5].data = &c->autospawn;
120 table[6].data = &c->cookie_file;
121
122 f = filename ?
123 fopen((fn = pa_xstrdup(filename)), "r") :
124 pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn, "r");
125
126 if (!f && errno != EINTR) {
127 pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s", fn, pa_cstrerror(errno));
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
137 finish:
138 pa_xfree(fn);
139
140 if (f)
141 fclose(f);
142
143 return r;
144 }
145
146 int pa_client_conf_env(pa_client_conf *c) {
147 char *e;
148
149 if ((e = getenv(ENV_DEFAULT_SINK))) {
150 pa_xfree(c->default_sink);
151 c->default_sink = pa_xstrdup(e);
152 }
153
154 if ((e = getenv(ENV_DEFAULT_SOURCE))) {
155 pa_xfree(c->default_source);
156 c->default_source = pa_xstrdup(e);
157 }
158
159 if ((e = getenv(ENV_DEFAULT_SERVER))) {
160 pa_xfree(c->default_server);
161 c->default_server = pa_xstrdup(e);
162 }
163
164 if ((e = getenv(ENV_DAEMON_BINARY))) {
165 pa_xfree(c->daemon_binary);
166 c->daemon_binary = pa_xstrdup(e);
167 }
168
169 if ((e = getenv(ENV_COOKIE_FILE))) {
170 pa_xfree(c->cookie_file);
171 c->cookie_file = pa_xstrdup(e);
172
173 return pa_client_conf_load_cookie(c);
174 }
175
176 return 0;
177 }
178
179 int pa_client_conf_load_cookie(pa_client_conf* c) {
180 assert(c);
181
182 c->cookie_valid = 0;
183
184 if (!c->cookie_file)
185 return -1;
186
187 if (pa_authkey_load_auto(c->cookie_file, c->cookie, sizeof(c->cookie)) < 0)
188 return -1;
189
190 c->cookie_valid = 1;
191 return 0;
192 }
193