Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / 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 <assert.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include <pulsecore/core-error.h>
36 #include <pulse/xmalloc.h>
37
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 #ifndef OS_IS_WIN32
46 # define PATH_SEP "/"
47 #else
48 # define PATH_SEP "\\"
49 #endif
50
51 #define DEFAULT_CLIENT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PATH_SEP "client.conf"
52 #define DEFAULT_CLIENT_CONFIG_FILE_USER "client.conf"
53
54 #define ENV_CLIENT_CONFIG_FILE "PULSE_CLIENTCONFIG"
55 #define ENV_DEFAULT_SINK "PULSE_SINK"
56 #define ENV_DEFAULT_SOURCE "PULSE_SOURCE"
57 #define ENV_DEFAULT_SERVER "PULSE_SERVER"
58 #define ENV_DAEMON_BINARY "PULSE_BINARY"
59 #define ENV_COOKIE_FILE "PULSE_COOKIE"
60
61 static const pa_client_conf default_conf = {
62     .daemon_binary = NULL,
63     .extra_arguments = NULL,
64     .default_sink = NULL,
65     .default_source = NULL,
66     .default_server = NULL,
67     .autospawn = 0,
68     .disable_shm = 0,
69     .cookie_file = NULL,
70     .cookie_valid = 0,
71 };
72
73 pa_client_conf *pa_client_conf_new(void) {
74     pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
75
76     c->daemon_binary = pa_xstrdup(PA_BINARY);
77     c->extra_arguments = pa_xstrdup("--log-target=syslog --exit-idle-time=5");
78     c->cookie_file = pa_xstrdup(PA_NATIVE_COOKIE_FILE);
79
80     return c;
81 }
82
83 void pa_client_conf_free(pa_client_conf *c) {
84     assert(c);
85     pa_xfree(c->daemon_binary);
86     pa_xfree(c->extra_arguments);
87     pa_xfree(c->default_sink);
88     pa_xfree(c->default_source);
89     pa_xfree(c->default_server);
90     pa_xfree(c->cookie_file);
91     pa_xfree(c);
92 }
93 int pa_client_conf_load(pa_client_conf *c, const char *filename) {
94     FILE *f = NULL;
95     char *fn = NULL;
96     int r = -1;
97
98     /* Prepare the configuration parse table */
99     pa_config_item table[] = {
100         { "daemon-binary",          pa_config_parse_string,  NULL },
101         { "extra-arguments",        pa_config_parse_string,  NULL },
102         { "default-sink",           pa_config_parse_string,  NULL },
103         { "default-source",         pa_config_parse_string,  NULL },
104         { "default-server",         pa_config_parse_string,  NULL },
105         { "autospawn",              pa_config_parse_bool,    NULL },
106         { "cookie-file",            pa_config_parse_string,  NULL },
107         { "disable-shm",            pa_config_parse_bool,    NULL },
108         { NULL,                     NULL,                    NULL },
109     };
110
111     table[0].data = &c->daemon_binary;
112     table[1].data = &c->extra_arguments;
113     table[2].data = &c->default_sink;
114     table[3].data = &c->default_source;
115     table[4].data = &c->default_server;
116     table[5].data = &c->autospawn;
117     table[6].data = &c->cookie_file;
118     table[7].data = &c->disable_shm;
119
120     f = filename ?
121         fopen((fn = pa_xstrdup(filename)), "r") :
122         pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn, "r");
123
124     if (!f && errno != EINTR) {
125         pa_log("WARNING: failed to open configuration file '%s': %s", fn, pa_cstrerror(errno));
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
135 finish:
136     pa_xfree(fn);
137
138     if (f)
139         fclose(f);
140
141     return r;
142 }
143
144 int pa_client_conf_env(pa_client_conf *c) {
145     char *e;
146
147     if ((e = getenv(ENV_DEFAULT_SINK))) {
148         pa_xfree(c->default_sink);
149         c->default_sink = pa_xstrdup(e);
150     }
151
152     if ((e = getenv(ENV_DEFAULT_SOURCE))) {
153         pa_xfree(c->default_source);
154         c->default_source = pa_xstrdup(e);
155     }
156
157     if ((e = getenv(ENV_DEFAULT_SERVER))) {
158         pa_xfree(c->default_server);
159         c->default_server = pa_xstrdup(e);
160     }
161
162     if ((e = getenv(ENV_DAEMON_BINARY))) {
163         pa_xfree(c->daemon_binary);
164         c->daemon_binary = pa_xstrdup(e);
165     }
166
167     if ((e = getenv(ENV_COOKIE_FILE))) {
168         pa_xfree(c->cookie_file);
169         c->cookie_file = pa_xstrdup(e);
170
171         return pa_client_conf_load_cookie(c);
172     }
173
174     return 0;
175 }
176
177 int pa_client_conf_load_cookie(pa_client_conf* c) {
178     assert(c);
179
180     c->cookie_valid = 0;
181
182     if (!c->cookie_file)
183         return -1;
184
185     if (pa_authkey_load_auto(c->cookie_file, c->cookie, sizeof(c->cookie)) < 0)
186         return -1;
187
188     c->cookie_valid = 1;
189     return 0;
190 }
191