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