]> code.delx.au - pulseaudio/blob - polyp/client-conf.c
4906383d1efdb25ee24532cacdccf34fa71859a5
[pulseaudio] / 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 #include <stdlib.h>
23 #include <assert.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27
28 #include "client-conf.h"
29 #include "xmalloc.h"
30 #include "log.h"
31 #include "conf-parser.h"
32 #include "util.h"
33 #include "authkey.h"
34
35 #ifndef DEFAULT_CONFIG_DIR
36 #define DEFAULT_CONFIG_DIR "/etc/polypaudio"
37 #endif
38
39 #define DEFAULT_CLIENT_CONFIG_FILE DEFAULT_CONFIG_DIR"/client.conf"
40 #define DEFAULT_CLIENT_CONFIG_FILE_USER ".polypaudio/client.conf"
41
42 #define ENV_CLIENT_CONFIG_FILE "POLYP_CLIENTCONFIG"
43 #define ENV_DEFAULT_SINK "POLYP_SINK"
44 #define ENV_DEFAULT_SOURCE "POLYP_SOURCE"
45 #define ENV_DEFAULT_SERVER "POLYP_SERVER"
46 #define ENV_DAEMON_BINARY "POLYP_BINARY"
47 #define ENV_COOKIE_FILE "POLYP_COOKIE"
48
49 static const struct pa_client_conf default_conf = {
50 .daemon_binary = NULL,
51 .extra_arguments = NULL,
52 .default_sink = NULL,
53 .default_source = NULL,
54 .default_server = NULL,
55 .autospawn = 0,
56 .cookie_file = NULL,
57 .cookie_valid = 0
58 };
59
60 struct pa_client_conf *pa_client_conf_new(void) {
61 struct pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
62
63 c->daemon_binary = pa_xstrdup(POLYPAUDIO_BINARY);
64 c->extra_arguments = pa_xstrdup("--log-target=syslog --exit-idle-time=5");
65 c->cookie_file = pa_xstrdup(PA_NATIVE_COOKIE_FILE);
66
67 return c;
68 }
69
70 void pa_client_conf_free(struct pa_client_conf *c) {
71 assert(c);
72 pa_xfree(c->daemon_binary);
73 pa_xfree(c->extra_arguments);
74 pa_xfree(c->default_sink);
75 pa_xfree(c->default_source);
76 pa_xfree(c->default_server);
77 pa_xfree(c->cookie_file);
78 pa_xfree(c);
79 }
80 int pa_client_conf_load(struct pa_client_conf *c, const char *filename) {
81 FILE *f = NULL;
82 char *fn = NULL;
83 int r = -1;
84
85 /* Prepare the configuration parse table */
86 struct pa_config_item table[] = {
87 { "daemon-binary", pa_config_parse_string, NULL },
88 { "extra-arguments", pa_config_parse_string, NULL },
89 { "default-sink", pa_config_parse_string, NULL },
90 { "default-source", pa_config_parse_string, NULL },
91 { "default-server", pa_config_parse_string, NULL },
92 { "autospawn", pa_config_parse_bool, NULL },
93 { "cookie-file", pa_config_parse_string, NULL },
94 { NULL, NULL, NULL },
95 };
96
97 table[0].data = &c->daemon_binary;
98 table[1].data = &c->extra_arguments;
99 table[2].data = &c->default_sink;
100 table[3].data = &c->default_source;
101 table[4].data = &c->default_server;
102 table[5].data = &c->autospawn;
103 table[6].data = &c->cookie_file;
104
105 f = filename ?
106 fopen((fn = pa_xstrdup(filename)), "r") :
107 pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn);
108
109 if (!f && errno != EINTR) {
110 pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s\n", filename, strerror(errno));
111 goto finish;
112 }
113
114 r = f ? pa_config_parse(fn, f, table, NULL) : 0;
115
116 if (!r)
117 r = pa_client_conf_load_cookie(c);
118
119
120 finish:
121 pa_xfree(fn);
122
123 if (f)
124 fclose(f);
125
126 return r;
127 }
128
129 int pa_client_conf_env(struct pa_client_conf *c) {
130 char *e;
131
132 if ((e = getenv(ENV_DEFAULT_SINK))) {
133 pa_xfree(c->default_sink);
134 c->default_sink = pa_xstrdup(e);
135 }
136
137 if ((e = getenv(ENV_DEFAULT_SOURCE))) {
138 pa_xfree(c->default_source);
139 c->default_source = pa_xstrdup(e);
140 }
141
142 if ((e = getenv(ENV_DEFAULT_SERVER))) {
143 pa_xfree(c->default_server);
144 c->default_server = pa_xstrdup(e);
145 }
146
147 if ((e = getenv(ENV_DAEMON_BINARY))) {
148 pa_xfree(c->daemon_binary);
149 c->daemon_binary = pa_xstrdup(e);
150 }
151
152 if ((e = getenv(ENV_COOKIE_FILE))) {
153 pa_xfree(c->cookie_file);
154 c->cookie_file = pa_xstrdup(e);
155
156 return pa_client_conf_load_cookie(c);
157 }
158
159 return 0;
160 }
161
162 int pa_client_conf_load_cookie(struct pa_client_conf* c) {
163 assert(c);
164
165 c->cookie_valid = 0;
166
167 if (!c->cookie_file)
168 return -1;
169
170 if (pa_authkey_load_auto(c->cookie_file, c->cookie, sizeof(c->cookie)) < 0)
171 return -1;
172
173 c->cookie_valid = 1;
174 return 0;
175 }
176