]> code.delx.au - pulseaudio/blob - src/pulse/client-conf.c
drop a couple of WARNING prefixes in log messages, since we have pa_log_warn anyway...
[pulseaudio] / src / pulse / client-conf.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <string.h>
33
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/macro.h>
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/conf-parser.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/authkey.h>
42
43 #include "client-conf.h"
44
45 #define DEFAULT_CLIENT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "client.conf"
46 #define DEFAULT_CLIENT_CONFIG_FILE_USER "client.conf"
47
48 #define ENV_CLIENT_CONFIG_FILE "PULSE_CLIENTCONFIG"
49 #define ENV_DEFAULT_SINK "PULSE_SINK"
50 #define ENV_DEFAULT_SOURCE "PULSE_SOURCE"
51 #define ENV_DEFAULT_SERVER "PULSE_SERVER"
52 #define ENV_DAEMON_BINARY "PULSE_BINARY"
53 #define ENV_COOKIE_FILE "PULSE_COOKIE"
54
55 static const pa_client_conf default_conf = {
56 .daemon_binary = NULL,
57 .extra_arguments = NULL,
58 .default_sink = NULL,
59 .default_source = NULL,
60 .default_server = NULL,
61 .autospawn = 0,
62 .disable_shm = 0,
63 .cookie_file = NULL,
64 .cookie_valid = 0,
65 };
66
67 pa_client_conf *pa_client_conf_new(void) {
68 pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
69
70 c->daemon_binary = pa_xstrdup(PA_BINARY);
71 c->extra_arguments = pa_xstrdup("--log-target=syslog --exit-idle-time=5");
72 c->cookie_file = pa_xstrdup(PA_NATIVE_COOKIE_FILE);
73
74 return c;
75 }
76
77 void pa_client_conf_free(pa_client_conf *c) {
78 pa_assert(c);
79 pa_xfree(c->daemon_binary);
80 pa_xfree(c->extra_arguments);
81 pa_xfree(c->default_sink);
82 pa_xfree(c->default_source);
83 pa_xfree(c->default_server);
84 pa_xfree(c->cookie_file);
85 pa_xfree(c);
86 }
87
88 int pa_client_conf_load(pa_client_conf *c, const char *filename) {
89 FILE *f = NULL;
90 char *fn = NULL;
91 int r = -1;
92
93 /* Prepare the configuration parse table */
94 pa_config_item table[] = {
95 { "daemon-binary", pa_config_parse_string, NULL },
96 { "extra-arguments", pa_config_parse_string, NULL },
97 { "default-sink", pa_config_parse_string, NULL },
98 { "default-source", pa_config_parse_string, NULL },
99 { "default-server", pa_config_parse_string, NULL },
100 { "autospawn", pa_config_parse_bool, NULL },
101 { "cookie-file", pa_config_parse_string, NULL },
102 { "disable-shm", pa_config_parse_bool, NULL },
103 { NULL, NULL, NULL },
104 };
105
106 table[0].data = &c->daemon_binary;
107 table[1].data = &c->extra_arguments;
108 table[2].data = &c->default_sink;
109 table[3].data = &c->default_source;
110 table[4].data = &c->default_server;
111 table[5].data = &c->autospawn;
112 table[6].data = &c->cookie_file;
113 table[7].data = &c->disable_shm;
114
115 f = filename ?
116 fopen((fn = pa_xstrdup(filename)), "r") :
117 pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn, "r");
118
119 if (!f && errno != EINTR) {
120 pa_log_warn("Failed to open configuration file '%s': %s", fn, pa_cstrerror(errno));
121 goto finish;
122 }
123
124 r = f ? pa_config_parse(fn, f, table, NULL) : 0;
125
126 if (!r)
127 r = pa_client_conf_load_cookie(c);
128
129
130 finish:
131 pa_xfree(fn);
132
133 if (f)
134 fclose(f);
135
136 return r;
137 }
138
139 int pa_client_conf_env(pa_client_conf *c) {
140 char *e;
141
142 if ((e = getenv(ENV_DEFAULT_SINK))) {
143 pa_xfree(c->default_sink);
144 c->default_sink = pa_xstrdup(e);
145 }
146
147 if ((e = getenv(ENV_DEFAULT_SOURCE))) {
148 pa_xfree(c->default_source);
149 c->default_source = pa_xstrdup(e);
150 }
151
152 if ((e = getenv(ENV_DEFAULT_SERVER))) {
153 pa_xfree(c->default_server);
154 c->default_server = pa_xstrdup(e);
155 }
156
157 if ((e = getenv(ENV_DAEMON_BINARY))) {
158 pa_xfree(c->daemon_binary);
159 c->daemon_binary = pa_xstrdup(e);
160 }
161
162 if ((e = getenv(ENV_COOKIE_FILE))) {
163 pa_xfree(c->cookie_file);
164 c->cookie_file = pa_xstrdup(e);
165
166 return pa_client_conf_load_cookie(c);
167 }
168
169 return 0;
170 }
171
172 int pa_client_conf_load_cookie(pa_client_conf* c) {
173 pa_assert(c);
174
175 c->cookie_valid = 0;
176
177 if (!c->cookie_file)
178 return -1;
179
180 if (pa_authkey_load_auto(c->cookie_file, c->cookie, sizeof(c->cookie)) < 0)
181 return -1;
182
183 c->cookie_valid = 1;
184 return 0;
185 }
186