]> code.delx.au - pulseaudio/blob - polyp/client-conf.c
some commenting
[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 #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
34 #ifndef DEFAULT_CONFIG_DIR
35 #define DEFAULT_CONFIG_DIR "/etc/polypaudio"
36 #endif
37
38 #define DEFAULT_CLIENT_CONFIG_FILE DEFAULT_CONFIG_DIR"/client.conf"
39 #define DEFAULT_CLIENT_CONFIG_FILE_USER ".polypaudio/client.conf"
40
41 #define ENV_CLIENT_CONFIG_FILE "POLYP_CLIENTCONFIG"
42 #define ENV_DEFAULT_SINK "POLYP_SINK"
43 #define ENV_DEFAULT_SOURCE "POLYP_SOURCE"
44 #define ENV_DEFAULT_SERVER "POLYP_SERVER"
45 #define ENV_DAEMON_BINARY "POLYP_BINARY"
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("--log-target=syslog --exit-idle-time=5");
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 FILE *f = NULL;
76 char *fn = NULL;
77 int r = -1;
78
79 struct pa_config_item table[] = {
80 { "daemon-binary", pa_config_parse_string, NULL },
81 { "extra-arguments", pa_config_parse_string, NULL },
82 { "default-sink", pa_config_parse_string, NULL },
83 { "default-source", pa_config_parse_string, NULL },
84 { "default-server", pa_config_parse_string, NULL },
85 { "autospawn", pa_config_parse_bool, NULL },
86 { NULL, NULL, NULL },
87 };
88
89 table[0].data = &c->daemon_binary;
90 table[1].data = &c->extra_arguments;
91 table[2].data = &c->default_sink;
92 table[3].data = &c->default_source;
93 table[4].data = &c->default_server;
94 table[5].data = &c->autospawn;
95
96 f = filename ?
97 fopen((fn = pa_xstrdup(filename)), "r") :
98 pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn);
99
100 if (!f && errno != EINTR) {
101 pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s\n", filename, strerror(errno));
102 goto finish;
103 }
104
105 r = pa_config_parse(fn, f, table, NULL);
106
107 finish:
108 pa_xfree(fn);
109
110 if (f)
111 fclose(f);
112
113 return r;
114 }
115
116 int pa_client_conf_env(struct pa_client_conf *c) {
117 char *e;
118
119 if ((e = getenv(ENV_DEFAULT_SINK))) {
120 pa_xfree(c->default_sink);
121 c->default_sink = pa_xstrdup(e);
122 }
123
124 if ((e = getenv(ENV_DEFAULT_SOURCE))) {
125 pa_xfree(c->default_source);
126 c->default_source = pa_xstrdup(e);
127 }
128
129 if ((e = getenv(ENV_DEFAULT_SERVER))) {
130 pa_xfree(c->default_server);
131 c->default_server = pa_xstrdup(e);
132 }
133
134 if ((e = getenv(ENV_DAEMON_BINARY))) {
135 pa_xfree(c->daemon_binary);
136 c->daemon_binary = pa_xstrdup(e);
137 }
138
139 return 0;
140 }