]> code.delx.au - pulseaudio/blob - src/pulse/client-conf.c
* add new --system command line parameter to the daemon for running PulseAudio as...
[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 OS_IS_WIN32
43 # define PATH_SEP "/"
44 #else
45 # define PATH_SEP "\\"
46 #endif
47
48 #define DEFAULT_CLIENT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PATH_SEP "client.conf"
49 #define DEFAULT_CLIENT_CONFIG_FILE_USER ".pulse" PATH_SEP "client.conf"
50
51 #define ENV_CLIENT_CONFIG_FILE "PULSE_CLIENTCONFIG"
52 #define ENV_DEFAULT_SINK "PULSE_SINK"
53 #define ENV_DEFAULT_SOURCE "PULSE_SOURCE"
54 #define ENV_DEFAULT_SERVER "PULSE_SERVER"
55 #define ENV_DAEMON_BINARY "PULSE_BINARY"
56 #define ENV_COOKIE_FILE "PULSE_COOKIE"
57
58 static const pa_client_conf default_conf = {
59 .daemon_binary = NULL,
60 .extra_arguments = NULL,
61 .default_sink = NULL,
62 .default_source = NULL,
63 .default_server = NULL,
64 .autospawn = 0,
65 .cookie_file = NULL,
66 .cookie_valid = 0,
67 .access_group = NULL
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 --exit-idle-time=5");
75 c->cookie_file = pa_xstrdup(PA_NATIVE_COOKIE_FILE);
76 c->access_group = pa_xstrdup(PA_ACCESS_GROUP);
77
78 return c;
79 }
80
81 void pa_client_conf_free(pa_client_conf *c) {
82 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->cookie_file);
89 pa_xfree(c->access_group);
90 pa_xfree(c);
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, NULL },
100 { "extra-arguments", pa_config_parse_string, NULL },
101 { "default-sink", pa_config_parse_string, NULL },
102 { "default-source", pa_config_parse_string, NULL },
103 { "default-server", pa_config_parse_string, NULL },
104 { "autospawn", pa_config_parse_bool, NULL },
105 { "cookie-file", pa_config_parse_string, NULL },
106 { "access-group", pa_config_parse_string, NULL },
107 { NULL, NULL, NULL },
108 };
109
110 table[0].data = &c->daemon_binary;
111 table[1].data = &c->extra_arguments;
112 table[2].data = &c->default_sink;
113 table[3].data = &c->default_source;
114 table[4].data = &c->default_server;
115 table[5].data = &c->autospawn;
116 table[6].data = &c->cookie_file;
117 table[7].data = &c->access_group;
118
119 f = filename ?
120 fopen((fn = pa_xstrdup(filename)), "r") :
121 pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn, "r");
122
123 if (!f && errno != EINTR) {
124 pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s", fn, pa_cstrerror(errno));
125 goto finish;
126 }
127
128 r = f ? pa_config_parse(fn, f, table, NULL) : 0;
129
130 if (!r)
131 r = pa_client_conf_load_cookie(c);
132
133
134 finish:
135 pa_xfree(fn);
136
137 if (f)
138 fclose(f);
139
140 return r;
141 }
142
143 int pa_client_conf_env(pa_client_conf *c) {
144 char *e;
145
146 if ((e = getenv(ENV_DEFAULT_SINK))) {
147 pa_xfree(c->default_sink);
148 c->default_sink = pa_xstrdup(e);
149 }
150
151 if ((e = getenv(ENV_DEFAULT_SOURCE))) {
152 pa_xfree(c->default_source);
153 c->default_source = pa_xstrdup(e);
154 }
155
156 if ((e = getenv(ENV_DEFAULT_SERVER))) {
157 pa_xfree(c->default_server);
158 c->default_server = pa_xstrdup(e);
159 }
160
161 if ((e = getenv(ENV_DAEMON_BINARY))) {
162 pa_xfree(c->daemon_binary);
163 c->daemon_binary = pa_xstrdup(e);
164 }
165
166 if ((e = getenv(ENV_COOKIE_FILE))) {
167 pa_xfree(c->cookie_file);
168 c->cookie_file = pa_xstrdup(e);
169
170 return pa_client_conf_load_cookie(c);
171 }
172
173 return 0;
174 }
175
176 int pa_client_conf_load_cookie(pa_client_conf* c) {
177 assert(c);
178
179 c->cookie_valid = 0;
180
181 if (!c->cookie_file)
182 return -1;
183
184 if (pa_authkey_load_auto(c->cookie_file, c->cookie, sizeof(c->cookie)) < 0)
185 return -1;
186
187 c->cookie_valid = 1;
188 return 0;
189 }
190