]> code.delx.au - pulseaudio/blob - polyp/client-conf.c
rename some stuff
[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 "confparser.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
47 static const struct pa_client_conf default_conf = {
48 .daemon_binary = NULL,
49 .extra_arguments = NULL,
50 .default_sink = NULL,
51 .default_source = NULL,
52 .default_server = NULL,
53 .autospawn = 0
54 };
55
56 struct pa_client_conf *pa_client_conf_new(void) {
57 struct pa_client_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
58
59 c->daemon_binary = pa_xstrdup(POLYPAUDIO_BINARY);
60 c->extra_arguments = pa_xstrdup("--daemonize=yes --log-target=syslog");
61
62 return c;
63 }
64
65 void pa_client_conf_free(struct pa_client_conf *c) {
66 assert(c);
67 pa_xfree(c->daemon_binary);
68 pa_xfree(c->extra_arguments);
69 pa_xfree(c->default_sink);
70 pa_xfree(c->default_source);
71 pa_xfree(c->default_server);
72 pa_xfree(c);
73 }
74 int pa_client_conf_load(struct pa_client_conf *c, const char *filename) {
75 char *def = NULL;
76 int r;
77
78 const struct pa_config_item table[] = {
79 { "daemon-binary", pa_config_parse_string, &c->daemon_binary },
80 { "extra-arguments", pa_config_parse_string, &c->extra_arguments },
81 { "default-sink", pa_config_parse_string, &c->default_sink },
82 { "default-source", pa_config_parse_string, &c->default_source },
83 { "default-server", pa_config_parse_string, &c->default_server },
84 { "autospawn", pa_config_parse_bool, &c->autospawn },
85 { NULL, NULL, NULL },
86 };
87
88 if (!filename)
89 filename = getenv(ENV_CLIENT_CONFIG_FILE);
90
91 if (!filename) {
92 char *h;
93
94 if ((h = getenv("HOME"))) {
95 def = pa_sprintf_malloc("%s/%s", h, DEFAULT_CLIENT_CONFIG_FILE_USER);
96
97 if (!access(def, F_OK))
98 filename = def;
99 else {
100 pa_xfree(def);
101 def = NULL;
102 }
103 }
104 }
105
106 if (!filename)
107 filename = DEFAULT_CLIENT_CONFIG_FILE;
108
109 r = pa_config_parse(filename, table, NULL);
110 pa_xfree(def);
111 return r;
112 }
113
114 int pa_client_conf_env(struct pa_client_conf *c) {
115 char *e;
116
117 if ((e = getenv(ENV_DEFAULT_SINK))) {
118 pa_xfree(c->default_sink);
119 c->default_sink = pa_xstrdup(e);
120 }
121
122 if ((e = getenv(ENV_DEFAULT_SOURCE))) {
123 pa_xfree(c->default_source);
124 c->default_source = pa_xstrdup(e);
125 }
126
127 if ((e = getenv(ENV_DEFAULT_SERVER))) {
128 pa_xfree(c->default_server);
129 c->default_server = pa_xstrdup(e);
130 }
131
132 if ((e = getenv(ENV_DAEMON_BINARY))) {
133 pa_xfree(c->daemon_binary);
134 c->daemon_binary = pa_xstrdup(e);
135 }
136
137 return 0;
138 }