]> code.delx.au - pulseaudio/blob - polyp/pax11publish.c
implemented pax11publish.c
[pulseaudio] / polyp / pax11publish.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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <assert.h>
30
31 #include <X11/Xlib.h>
32 #include <X11/Xatom.h>
33
34 #include "util.h"
35 #include "log.h"
36 #include "authkey.h"
37 #include "native-common.h"
38 #include "client-conf.h"
39
40 static void set_x11_prop(Display *d, const char *name, const char *data) {
41 Atom a = XInternAtom(d, name, False);
42 XChangeProperty(d, RootWindow(d, 0), a, XA_STRING, 8, PropModeReplace, (unsigned char*) data, strlen(data)+1);
43 }
44
45 static void del_x11_prop(Display *d, const char *name) {
46 Atom a = XInternAtom(d, name, False);
47 XDeleteProperty(d, RootWindow(d, 0), a);
48 }
49
50 static char* get_x11_prop(Display *d, const char *name, char *p, size_t l) {
51 Atom actual_type;
52 int actual_format;
53 unsigned long nitems;
54 unsigned long nbytes_after;
55 unsigned char *prop = NULL;
56 char *ret = NULL;
57
58 Atom a = XInternAtom(d, name, False);
59 if (XGetWindowProperty(d, RootWindow(d, 0), a, 0, (l+2)/4, False, XA_STRING, &actual_type, &actual_format, &nitems, &nbytes_after, &prop) != Success)
60 goto finish;
61
62 if (actual_type != XA_STRING)
63 goto finish;
64
65 memcpy(p, prop, nitems);
66 p[nitems] = 0;
67
68 ret = p;
69
70 finish:
71
72 if (prop)
73 XFree(prop);
74
75 return ret;
76 }
77
78 int main(int argc, char *argv[]) {
79 const char *dname = NULL, *sink = NULL, *source = NULL, *server = NULL, *cookie_file = PA_NATIVE_COOKIE_FILE;
80 int c, ret = 1;
81 Display *d = NULL;
82 enum { DUMP, EXPORT, IMPORT, REMOVE } mode = DUMP;
83
84 while ((c = getopt(argc, argv, "deiD:S:O:I:c:hr")) != -1) {
85 switch (c) {
86 case 'D' :
87 dname = optarg;
88 break;
89 case 'h':
90 printf("%s [-D display] [-S server] [-O sink] [-I source] [-c file] [-d|-e|-i|-r]\n", pa_path_get_filename(argv[0]));
91 ret = 0;
92 goto finish;
93 case 'd':
94 mode = DUMP;
95 break;
96 case 'e':
97 mode = EXPORT;
98 break;
99 case 'i':
100 mode = IMPORT;
101 break;
102 case 'r':
103 mode = REMOVE;
104 break;
105 case 'c':
106 cookie_file = optarg;
107 break;
108 case 'I':
109 source = optarg;
110 break;
111 case 'O':
112 sink = optarg;
113 break;
114 case 'S':
115 server = optarg;
116 break;
117 default:
118 fprintf(stderr, "Failed to parse command line.\n");
119 goto finish;
120 }
121 }
122
123 if (!(d = XOpenDisplay(dname))) {
124 pa_log(__FILE__": XOpenDisplay() failed\n");
125 goto finish;
126 }
127
128 switch (mode) {
129 case DUMP: {
130 char t[1024];
131 if (!get_x11_prop(d, "POLYP_SERVER", t, sizeof(t)))
132 goto finish;
133
134 printf("Server: %s\n", t);
135 if (get_x11_prop(d, "POLYP_SOURCE", t, sizeof(t)))
136 printf("Source: %s\n", t);
137 if (get_x11_prop(d, "POLYP_SINK", t, sizeof(t)))
138 printf("Sink: %s\n", t);
139 if (get_x11_prop(d, "POLYP_COOKIE", t, sizeof(t)))
140 printf("Cookie: %s\n", t);
141
142 break;
143 }
144
145 case IMPORT: {
146 char t[1024];
147 if (!get_x11_prop(d, "POLYP_SERVER", t, sizeof(t)))
148 goto finish;
149
150 printf("POLYP_SERVER='%s'\nexport POLYP_SERVER\n", t);
151
152 if (get_x11_prop(d, "POLYP_SOURCE", t, sizeof(t)))
153 printf("POLYP_SOURCE='%s'\nexport POLYP_SOURCE\n", t);
154 if (get_x11_prop(d, "POLYP_SINK", t, sizeof(t)))
155 printf("POLYP_SINK='%s'\nexport POLYP_SINK\n", t);
156
157 if (get_x11_prop(d, "POLYP_COOKIE", t, sizeof(t))) {
158 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
159 size_t l;
160 if ((l = pa_parsehex(t, cookie, sizeof(cookie))) == (size_t) -1) {
161 fprintf(stderr, "Failed to parse cookie data\n");
162 goto finish;
163 }
164
165 if (pa_authkey_save(cookie_file, cookie, l) < 0) {
166 fprintf(stderr, "Failed to save cookie data\n");
167 goto finish;
168 }
169 }
170
171 break;
172 }
173
174 case EXPORT: {
175 struct pa_client_conf *c = pa_client_conf_new();
176 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
177 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
178 assert(c);
179
180 if (pa_client_conf_load(c, NULL) < 0) {
181 fprintf(stderr, "Failed to load client configuration file.\n");
182 goto finish;
183 }
184
185 if (pa_client_conf_env(c) < 0) {
186 fprintf(stderr, "Failed to read environment configuration data.\n");
187 goto finish;
188 }
189
190 del_x11_prop(d, "POLYP_ID");
191
192 if (server)
193 set_x11_prop(d, "POLYP_SERVER", c->default_server);
194 else if (c->default_server)
195 set_x11_prop(d, "POLYP_SERVER", c->default_server);
196 else {
197 char hn[256];
198 pa_get_host_name(hn, sizeof(hn));
199 set_x11_prop(d, "POLYP_SERVER", hn);
200 }
201
202 if (sink)
203 set_x11_prop(d, "POLYP_SINK", sink);
204 else if (c->default_sink)
205 set_x11_prop(d, "POLYP_SINK", c->default_sink);
206
207 if (source)
208 set_x11_prop(d, "POLYP_SOURCE", source);
209 if (c->default_source)
210 set_x11_prop(d, "POLYP_SOURCE", c->default_source);
211
212 pa_client_conf_free(c);
213
214 if (pa_authkey_load_auto(cookie_file, cookie, sizeof(cookie)) < 0) {
215 fprintf(stderr, "Failed to load cookie data\n");
216 goto finish;
217 }
218
219 set_x11_prop(d, "POLYP_COOKIE", pa_hexstr(cookie, sizeof(cookie), hx, sizeof(hx)));
220 break;
221 }
222
223 case REMOVE:
224 del_x11_prop(d, "POLYP_SERVER");
225 del_x11_prop(d, "POLYP_SINK");
226 del_x11_prop(d, "POLYP_SOURCE");
227 del_x11_prop(d, "POLYP_ID");
228 del_x11_prop(d, "POLYP_COOKIE");
229 break;
230
231 default:
232 fprintf(stderr, "No yet implemented.\n");
233 goto finish;
234 }
235
236 ret = 0;
237
238 finish:
239
240 if (d) {
241 XSync(d, False);
242 XCloseDisplay(d);
243 }
244
245 return ret;
246 }