]> code.delx.au - pulseaudio/blob - src/utils/pax11publish.c
Remove unnecessary #includes
[pulseaudio] / src / utils / pax11publish.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio 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 PulseAudio; 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 <stdio.h>
27 #include <getopt.h>
28 #include <assert.h>
29 #include <locale.h>
30
31 #include <xcb/xcb.h>
32
33 #include <pulse/util.h>
34 #include <pulse/i18n.h>
35 #include <pulse/client-conf.h>
36
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/authkey.h>
40 #include <pulsecore/native-common.h>
41 #include <pulsecore/x11prop.h>
42
43
44 int main(int argc, char *argv[]) {
45 const char *dname = NULL, *sink = NULL, *source = NULL, *server = NULL, *cookie_file = PA_NATIVE_COOKIE_FILE;
46 int c, ret = 1, screen = 0;
47 xcb_connection_t *xcb = NULL;
48 enum { DUMP, EXPORT, IMPORT, REMOVE } mode = DUMP;
49
50 setlocale(LC_ALL, "");
51 bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
52
53 while ((c = getopt(argc, argv, "deiD:S:O:I:c:hr")) != -1) {
54 switch (c) {
55 case 'D' :
56 dname = optarg;
57 break;
58 case 'h':
59 printf(_("%s [-D display] [-S server] [-O sink] [-I source] [-c file] [-d|-e|-i|-r]\n\n"
60 " -d Show current PulseAudio data attached to X11 display (default)\n"
61 " -e Export local PulseAudio data to X11 display\n"
62 " -i Import PulseAudio data from X11 display to local environment variables and cookie file.\n"
63 " -r Remove PulseAudio data from X11 display\n"),
64 pa_path_get_filename(argv[0]));
65 ret = 0;
66 goto finish;
67 case 'd':
68 mode = DUMP;
69 break;
70 case 'e':
71 mode = EXPORT;
72 break;
73 case 'i':
74 mode = IMPORT;
75 break;
76 case 'r':
77 mode = REMOVE;
78 break;
79 case 'c':
80 cookie_file = optarg;
81 break;
82 case 'I':
83 source = optarg;
84 break;
85 case 'O':
86 sink = optarg;
87 break;
88 case 'S':
89 server = optarg;
90 break;
91 default:
92 fprintf(stderr, _("Failed to parse command line.\n"));
93 goto finish;
94 }
95 }
96
97 if (!(xcb = xcb_connect(dname, &screen))) {
98 pa_log(_("xcb_connect() failed"));
99 goto finish;
100 }
101
102 if (xcb_connection_has_error(xcb)) {
103 pa_log(_("xcb_connection_has_error() returned true"));
104 goto finish;
105 }
106
107 switch (mode) {
108 case DUMP: {
109 char t[1024];
110 if (pa_x11_get_prop(xcb, screen, "PULSE_SERVER", t, sizeof(t)))
111 printf(_("Server: %s\n"), t);
112 if (pa_x11_get_prop(xcb, screen, "PULSE_SOURCE", t, sizeof(t)))
113 printf(_("Source: %s\n"), t);
114 if (pa_x11_get_prop(xcb, screen, "PULSE_SINK", t, sizeof(t)))
115 printf(_("Sink: %s\n"), t);
116 if (pa_x11_get_prop(xcb, screen, "PULSE_COOKIE", t, sizeof(t)))
117 printf(_("Cookie: %s\n"), t);
118
119 break;
120 }
121
122 case IMPORT: {
123 char t[1024];
124 if (pa_x11_get_prop(xcb, screen, "PULSE_SERVER", t, sizeof(t)))
125 printf("PULSE_SERVER='%s'\nexport PULSE_SERVER\n", t);
126 if (pa_x11_get_prop(xcb, screen, "PULSE_SOURCE", t, sizeof(t)))
127 printf("PULSE_SOURCE='%s'\nexport PULSE_SOURCE\n", t);
128 if (pa_x11_get_prop(xcb, screen, "PULSE_SINK", t, sizeof(t)))
129 printf("PULSE_SINK='%s'\nexport PULSE_SINK\n", t);
130
131 if (pa_x11_get_prop(xcb, screen, "PULSE_COOKIE", t, sizeof(t))) {
132 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
133 size_t l;
134 if ((l = pa_parsehex(t, cookie, sizeof(cookie))) != sizeof(cookie)) {
135 fprintf(stderr, _("Failed to parse cookie data\n"));
136 goto finish;
137 }
138
139 if (pa_authkey_save(cookie_file, cookie, l) < 0) {
140 fprintf(stderr, _("Failed to save cookie data\n"));
141 goto finish;
142 }
143 }
144
145 break;
146 }
147
148 case EXPORT: {
149 pa_client_conf *conf = pa_client_conf_new();
150 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
151 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
152 assert(conf);
153
154 if (pa_client_conf_load(conf, NULL) < 0) {
155 fprintf(stderr, _("Failed to load client configuration file.\n"));
156 goto finish;
157 }
158
159 if (pa_client_conf_env(conf) < 0) {
160 fprintf(stderr, _("Failed to read environment configuration data.\n"));
161 goto finish;
162 }
163
164 pa_x11_del_prop(xcb, screen, "PULSE_SERVER");
165 pa_x11_del_prop(xcb, screen, "PULSE_SINK");
166 pa_x11_del_prop(xcb, screen, "PULSE_SOURCE");
167 pa_x11_del_prop(xcb, screen, "PULSE_ID");
168 pa_x11_del_prop(xcb, screen, "PULSE_COOKIE");
169
170 if (server)
171 pa_x11_set_prop(xcb, screen, "PULSE_SERVER", server);
172 else if (conf->default_server)
173 pa_x11_set_prop(xcb, screen, "PULSE_SERVER", conf->default_server);
174 else {
175 char hn[256];
176 if (!pa_get_fqdn(hn, sizeof(hn))) {
177 fprintf(stderr, _("Failed to get FQDN.\n"));
178 goto finish;
179 }
180
181 pa_x11_set_prop(xcb, screen, "PULSE_SERVER", hn);
182 }
183
184 if (sink)
185 pa_x11_set_prop(xcb, screen, "PULSE_SINK", sink);
186 else if (conf->default_sink)
187 pa_x11_set_prop(xcb, screen, "PULSE_SINK", conf->default_sink);
188
189 if (source)
190 pa_x11_set_prop(xcb, screen, "PULSE_SOURCE", source);
191 if (conf->default_source)
192 pa_x11_set_prop(xcb, screen, "PULSE_SOURCE", conf->default_source);
193
194 pa_client_conf_free(conf);
195
196 if (pa_authkey_load_auto(cookie_file, cookie, sizeof(cookie)) < 0) {
197 fprintf(stderr, _("Failed to load cookie data\n"));
198 goto finish;
199 }
200
201 pa_x11_set_prop(xcb, screen, "PULSE_COOKIE", pa_hexstr(cookie, sizeof(cookie), hx, sizeof(hx)));
202 break;
203 }
204
205 case REMOVE:
206 pa_x11_del_prop(xcb, screen, "PULSE_SERVER");
207 pa_x11_del_prop(xcb, screen, "PULSE_SINK");
208 pa_x11_del_prop(xcb, screen, "PULSE_SOURCE");
209 pa_x11_del_prop(xcb, screen, "PULSE_ID");
210 pa_x11_del_prop(xcb, screen, "PULSE_COOKIE");
211 pa_x11_del_prop(xcb, screen, "PULSE_SESSION_ID");
212 break;
213
214 default:
215 fprintf(stderr, _("Not yet implemented.\n"));
216 goto finish;
217 }
218
219 ret = 0;
220
221 finish:
222
223 if (xcb) {
224 xcb_flush(xcb);
225 xcb_disconnect(xcb);
226 }
227
228 return ret;
229 }