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