2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
32 #include <pulse/xmalloc.h>
33 #include <pulse/i18n.h>
35 #include <pulsecore/macro.h>
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/conf-parser.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/authkey.h>
42 #include "client-conf.h"
44 #define DEFAULT_CLIENT_CONFIG_FILE PA_DEFAULT_CONFIG_DIR PA_PATH_SEP "client.conf"
45 #define DEFAULT_CLIENT_CONFIG_FILE_USER "client.conf"
47 #define ENV_CLIENT_CONFIG_FILE "PULSE_CLIENTCONFIG"
48 #define ENV_DEFAULT_SINK "PULSE_SINK"
49 #define ENV_DEFAULT_SOURCE "PULSE_SOURCE"
50 #define ENV_DEFAULT_SERVER "PULSE_SERVER"
51 #define ENV_DAEMON_BINARY "PULSE_BINARY"
52 #define ENV_COOKIE_FILE "PULSE_COOKIE"
54 static const pa_client_conf default_conf
= {
55 .daemon_binary
= NULL
,
56 .extra_arguments
= NULL
,
58 .default_source
= NULL
,
59 .default_server
= NULL
,
63 .cookie_valid
= FALSE
,
66 pa_client_conf
*pa_client_conf_new(void) {
67 pa_client_conf
*c
= pa_xmemdup(&default_conf
, sizeof(default_conf
));
69 c
->daemon_binary
= pa_xstrdup(PA_BINARY
);
70 c
->extra_arguments
= pa_xstrdup("--log-target=syslog");
71 c
->cookie_file
= pa_xstrdup(PA_NATIVE_COOKIE_FILE
);
76 void pa_client_conf_free(pa_client_conf
*c
) {
78 pa_xfree(c
->daemon_binary
);
79 pa_xfree(c
->extra_arguments
);
80 pa_xfree(c
->default_sink
);
81 pa_xfree(c
->default_source
);
82 pa_xfree(c
->default_server
);
83 pa_xfree(c
->cookie_file
);
87 int pa_client_conf_load(pa_client_conf
*c
, const char *filename
) {
92 /* Prepare the configuration parse table */
93 pa_config_item table
[] = {
94 { "daemon-binary", pa_config_parse_string
, NULL
},
95 { "extra-arguments", pa_config_parse_string
, NULL
},
96 { "default-sink", pa_config_parse_string
, NULL
},
97 { "default-source", pa_config_parse_string
, NULL
},
98 { "default-server", pa_config_parse_string
, NULL
},
99 { "autospawn", pa_config_parse_bool
, NULL
},
100 { "cookie-file", pa_config_parse_string
, NULL
},
101 { "disable-shm", pa_config_parse_bool
, NULL
},
102 { NULL
, NULL
, NULL
},
105 table
[0].data
= &c
->daemon_binary
;
106 table
[1].data
= &c
->extra_arguments
;
107 table
[2].data
= &c
->default_sink
;
108 table
[3].data
= &c
->default_source
;
109 table
[4].data
= &c
->default_server
;
110 table
[5].data
= &c
->autospawn
;
111 table
[6].data
= &c
->cookie_file
;
112 table
[7].data
= &c
->disable_shm
;
116 if (!(f
= fopen(filename
, "r"))) {
117 pa_log(_("Failed to open configuration file '%s': %s"), fn
, pa_cstrerror(errno
));
125 if (!(f
= pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE
, DEFAULT_CLIENT_CONFIG_FILE_USER
, ENV_CLIENT_CONFIG_FILE
, &fn
)))
130 r
= f
? pa_config_parse(fn
, f
, table
, NULL
) : 0;
133 r
= pa_client_conf_load_cookie(c
);
144 int pa_client_conf_env(pa_client_conf
*c
) {
147 if ((e
= getenv(ENV_DEFAULT_SINK
))) {
148 pa_xfree(c
->default_sink
);
149 c
->default_sink
= pa_xstrdup(e
);
152 if ((e
= getenv(ENV_DEFAULT_SOURCE
))) {
153 pa_xfree(c
->default_source
);
154 c
->default_source
= pa_xstrdup(e
);
157 if ((e
= getenv(ENV_DEFAULT_SERVER
))) {
158 pa_xfree(c
->default_server
);
159 c
->default_server
= pa_xstrdup(e
);
162 if ((e
= getenv(ENV_DAEMON_BINARY
))) {
163 pa_xfree(c
->daemon_binary
);
164 c
->daemon_binary
= pa_xstrdup(e
);
167 if ((e
= getenv(ENV_COOKIE_FILE
))) {
168 pa_xfree(c
->cookie_file
);
169 c
->cookie_file
= pa_xstrdup(e
);
171 return pa_client_conf_load_cookie(c
);
177 int pa_client_conf_load_cookie(pa_client_conf
* c
) {
183 c
->cookie_valid
= FALSE
;
185 if (pa_authkey_load_auto(c
->cookie_file
, c
->cookie
, sizeof(c
->cookie
)) < 0)
188 c
->cookie_valid
= TRUE
;