]> code.delx.au - pulseaudio/blob - polyp/client-conf.c
work around gcc 2.95 limitation
[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 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 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
26 #include "client-conf.h"
27 #include "xmalloc.h"
28 #include "log.h"
29 #include "conf-parser.h"
30 #include "util.h"
31
32 #ifndef DEFAULT_CLIENT_CONFIG_FILE
33 #define DEFAULT_CLIENT_CONFIG_FILE "/etc/polypaudio/client.conf"
34 #endif
35
36 #ifndef DEFAULT_CLIENT_CONFIG_FILE_USER
37 #define DEFAULT_CLIENT_CONFIG_FILE_USER ".polypaudio/client.conf"
38 #endif
39
40 #define ENV_CLIENT_CONFIG_FILE "POLYP_CLIENTCONFIG"
41 #define ENV_DEFAULT_SINK "POLYP_SINK"
42 #define ENV_DEFAULT_SOURCE "POLYP_SOURCE"
43 #define ENV_DEFAULT_SERVER "POLYP_SERVER"
44 #define ENV_DAEMON_BINARY "POLYP_BINARY"
45
46 static const struct pa_client_conf default_conf = {
47 .daemon_binary = NULL,
48 .extra_arguments = NULL,
49 .default_sink = NULL,
50 .default_source = NULL,
51 .default_server = NULL,
52 .autospawn = 0
53 };
54
55 struct pa_client_conf *pa_client_conf_new(void) {
56 struct pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
57
58 c->daemon_binary = pa_xstrdup(POLYPAUDIO_BINARY);
59 c->extra_arguments = pa_xstrdup("--log-target=syslog --exit-idle-time=5");
60
61 return c;
62 }
63
64 void pa_client_conf_free(struct pa_client_conf *c) {
65 assert(c);
66 pa_xfree(c->daemon_binary);
67 pa_xfree(c->extra_arguments);
68 pa_xfree(c->default_sink);
69 pa_xfree(c->default_source);
70 pa_xfree(c->default_server);
71 pa_xfree(c);
72 }
73 int pa_client_conf_load(struct pa_client_conf *c, const char *filename) {
74 char *def = NULL;
75 int r;
76
77 struct pa_config_item table[] = {
78 { "daemon-binary", pa_config_parse_string, NULL },
79 { "extra-arguments", pa_config_parse_string, NULL },
80 { "default-sink", pa_config_parse_string, NULL },
81 { "default-source", pa_config_parse_string, NULL },
82 { "default-server", pa_config_parse_string, NULL },
83 { "autospawn", pa_config_parse_bool, NULL },
84 { NULL, NULL, NULL },
85 };
86
87 table[0].data = &c->daemon_binary;
88 table[1].data = &c->extra_arguments;
89 table[2].data = &c->default_sink;
90 table[3].data = &c->default_source;
91 table[4].data = &c->default_server;
92 table[5].data = &c->autospawn;
93
94 if (!filename)
95 filename = getenv(ENV_CLIENT_CONFIG_FILE);
96
97 if (!filename) {
98 char *h;
99
100 if ((h = getenv("HOME"))) {
101 def = pa_sprintf_malloc("%s/%s", h, DEFAULT_CLIENT_CONFIG_FILE_USER);
102
103 if (!access(def, F_OK))
104 filename = def;
105 else {
106 pa_xfree(def);
107 def = NULL;
108 }
109 }
110 }
111
112 if (!filename)
113 filename = DEFAULT_CLIENT_CONFIG_FILE;
114
115 r = pa_config_parse(filename, table, NULL);
116 pa_xfree(def);
117 return r;
118 }
119
120 int pa_client_conf_env(struct pa_client_conf *c) {
121 char *e;
122
123 if ((e = getenv(ENV_DEFAULT_SINK))) {
124 pa_xfree(c->default_sink);
125 c->default_sink = pa_xstrdup(e);
126 }
127
128 if ((e = getenv(ENV_DEFAULT_SOURCE))) {
129 pa_xfree(c->default_source);
130 c->default_source = pa_xstrdup(e);
131 }
132
133 if ((e = getenv(ENV_DEFAULT_SERVER))) {
134 pa_xfree(c->default_server);
135 c->default_server = pa_xstrdup(e);
136 }
137
138 if ((e = getenv(ENV_DAEMON_BINARY))) {
139 pa_xfree(c->daemon_binary);
140 c->daemon_binary = pa_xstrdup(e);
141 }
142
143 return 0;
144 }