]> code.delx.au - pulseaudio/blob - src/pulse/client-conf.c
enable exit-on-idle by default
[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 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
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 .autospawn = TRUE,
60 .disable_shm = FALSE,
61 .cookie_file = NULL,
62 .cookie_valid = FALSE,
63 };
64
65 pa_client_conf *pa_client_conf_new(void) {
66 pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
67
68 c->daemon_binary = pa_xstrdup(PA_BINARY);
69 c->extra_arguments = pa_xstrdup("--log-target=syslog");
70 c->cookie_file = pa_xstrdup(PA_NATIVE_COOKIE_FILE);
71
72 return c;
73 }
74
75 void pa_client_conf_free(pa_client_conf *c) {
76 pa_assert(c);
77 pa_xfree(c->daemon_binary);
78 pa_xfree(c->extra_arguments);
79 pa_xfree(c->default_sink);
80 pa_xfree(c->default_source);
81 pa_xfree(c->default_server);
82 pa_xfree(c->cookie_file);
83 pa_xfree(c);
84 }
85
86 int pa_client_conf_load(pa_client_conf *c, const char *filename) {
87 FILE *f = NULL;
88 char *fn = NULL;
89 int r = -1;
90
91 /* Prepare the configuration parse table */
92 pa_config_item table[] = {
93 { "daemon-binary", pa_config_parse_string, NULL },
94 { "extra-arguments", pa_config_parse_string, NULL },
95 { "default-sink", pa_config_parse_string, NULL },
96 { "default-source", pa_config_parse_string, NULL },
97 { "default-server", pa_config_parse_string, NULL },
98 { "autospawn", pa_config_parse_bool, NULL },
99 { "cookie-file", pa_config_parse_string, NULL },
100 { "disable-shm", pa_config_parse_bool, NULL },
101 { NULL, NULL, NULL },
102 };
103
104 table[0].data = &c->daemon_binary;
105 table[1].data = &c->extra_arguments;
106 table[2].data = &c->default_sink;
107 table[3].data = &c->default_source;
108 table[4].data = &c->default_server;
109 table[5].data = &c->autospawn;
110 table[6].data = &c->cookie_file;
111 table[7].data = &c->disable_shm;
112
113 if (filename) {
114
115 if (!(f = fopen(filename, "r"))) {
116 pa_log("Failed to open configuration file '%s': %s", fn, pa_cstrerror(errno));
117 goto finish;
118 }
119
120 fn = pa_xstrdup(fn);
121
122 } else {
123
124 if (!(f = pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn)))
125 if (errno != ENOENT)
126 goto finish;
127 }
128
129 r = f ? pa_config_parse(fn, f, table, NULL) : 0;
130
131 if (!r)
132 r = pa_client_conf_load_cookie(c);
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 pa_assert(c);
178
179 if (!c->cookie_file)
180 return -1;
181
182 c->cookie_valid = FALSE;
183
184 if (pa_authkey_load_auto(c->cookie_file, c->cookie, sizeof(c->cookie)) < 0)
185 return -1;
186
187 c->cookie_valid = TRUE;
188 return 0;
189 }