]> code.delx.au - pulseaudio/blob - src/polyp/client-conf.c
remove a bunch of log messages
[pulseaudio] / src / polyp / client-conf.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 polypaudio 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 polypaudio; 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 <polypcore/xmalloc.h>
33 #include <polypcore/log.h>
34 #include <polypcore/conf-parser.h>
35 #include <polypcore/util.h>
36 #include <polypcore/authkey.h>
37
38 #include "client-conf.h"
39
40 #ifndef DEFAULT_CONFIG_DIR
41 # ifndef OS_IS_WIN32
42 # define DEFAULT_CONFIG_DIR "/etc/polypaudio"
43 # else
44 # define DEFAULT_CONFIG_DIR "%POLYP_ROOT%"
45 # endif
46 #endif
47
48 #ifndef OS_IS_WIN32
49 # define PATH_SEP "/"
50 #else
51 # define PATH_SEP "\\"
52 #endif
53
54 #define DEFAULT_CLIENT_CONFIG_FILE DEFAULT_CONFIG_DIR PATH_SEP "client.conf"
55 #define DEFAULT_CLIENT_CONFIG_FILE_USER ".polypaudio" PATH_SEP "client.conf"
56
57 #define ENV_CLIENT_CONFIG_FILE "POLYP_CLIENTCONFIG"
58 #define ENV_DEFAULT_SINK "POLYP_SINK"
59 #define ENV_DEFAULT_SOURCE "POLYP_SOURCE"
60 #define ENV_DEFAULT_SERVER "POLYP_SERVER"
61 #define ENV_DAEMON_BINARY "POLYP_BINARY"
62 #define ENV_COOKIE_FILE "POLYP_COOKIE"
63
64 static const pa_client_conf default_conf = {
65 .daemon_binary = NULL,
66 .extra_arguments = NULL,
67 .default_sink = NULL,
68 .default_source = NULL,
69 .default_server = NULL,
70 .autospawn = 0,
71 .cookie_file = NULL,
72 .cookie_valid = 0
73 };
74
75 pa_client_conf *pa_client_conf_new(void) {
76 pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
77
78 c->daemon_binary = pa_xstrdup(POLYPAUDIO_BINARY);
79 c->extra_arguments = pa_xstrdup("--log-target=syslog --exit-idle-time=5");
80 c->cookie_file = pa_xstrdup(PA_NATIVE_COOKIE_FILE);
81
82 return c;
83 }
84
85 void pa_client_conf_free(pa_client_conf *c) {
86 assert(c);
87 pa_xfree(c->daemon_binary);
88 pa_xfree(c->extra_arguments);
89 pa_xfree(c->default_sink);
90 pa_xfree(c->default_source);
91 pa_xfree(c->default_server);
92 pa_xfree(c->cookie_file);
93 pa_xfree(c);
94 }
95 int pa_client_conf_load(pa_client_conf *c, const char *filename) {
96 FILE *f = NULL;
97 char *fn = NULL;
98 int r = -1;
99
100 /* Prepare the configuration parse table */
101 pa_config_item table[] = {
102 { "daemon-binary", pa_config_parse_string, NULL },
103 { "extra-arguments", pa_config_parse_string, NULL },
104 { "default-sink", pa_config_parse_string, NULL },
105 { "default-source", pa_config_parse_string, NULL },
106 { "default-server", pa_config_parse_string, NULL },
107 { "autospawn", pa_config_parse_bool, NULL },
108 { "cookie-file", pa_config_parse_string, NULL },
109 { NULL, NULL, NULL },
110 };
111
112 table[0].data = &c->daemon_binary;
113 table[1].data = &c->extra_arguments;
114 table[2].data = &c->default_sink;
115 table[3].data = &c->default_source;
116 table[4].data = &c->default_server;
117 table[5].data = &c->autospawn;
118 table[6].data = &c->cookie_file;
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);
123
124 if (!f && errno != EINTR) {
125 pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s", filename, strerror(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