]> code.delx.au - pulseaudio/blob - polyp/pax11publish.c
export FQDN instead of hostname
[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\n"
91 " -d Show current Polypaudio data attached to X11 display (default)\n"
92 " -e Export local Polypaudio data to X11 display\n"
93 " -i Import Polypaudio data from X11 display to local environment variables and cookie file.\n"
94 " -r Remove Polypaudio data from X11 display\n",
95 pa_path_get_filename(argv[0]));
96 ret = 0;
97 goto finish;
98 case 'd':
99 mode = DUMP;
100 break;
101 case 'e':
102 mode = EXPORT;
103 break;
104 case 'i':
105 mode = IMPORT;
106 break;
107 case 'r':
108 mode = REMOVE;
109 break;
110 case 'c':
111 cookie_file = optarg;
112 break;
113 case 'I':
114 source = optarg;
115 break;
116 case 'O':
117 sink = optarg;
118 break;
119 case 'S':
120 server = optarg;
121 break;
122 default:
123 fprintf(stderr, "Failed to parse command line.\n");
124 goto finish;
125 }
126 }
127
128 if (!(d = XOpenDisplay(dname))) {
129 pa_log(__FILE__": XOpenDisplay() failed\n");
130 goto finish;
131 }
132
133 switch (mode) {
134 case DUMP: {
135 char t[1024];
136 if (!get_x11_prop(d, "POLYP_SERVER", t, sizeof(t)))
137 goto finish;
138
139 printf("Server: %s\n", t);
140 if (get_x11_prop(d, "POLYP_SOURCE", t, sizeof(t)))
141 printf("Source: %s\n", t);
142 if (get_x11_prop(d, "POLYP_SINK", t, sizeof(t)))
143 printf("Sink: %s\n", t);
144 if (get_x11_prop(d, "POLYP_COOKIE", t, sizeof(t)))
145 printf("Cookie: %s\n", t);
146
147 break;
148 }
149
150 case IMPORT: {
151 char t[1024];
152 if (!get_x11_prop(d, "POLYP_SERVER", t, sizeof(t)))
153 goto finish;
154
155 printf("POLYP_SERVER='%s'\nexport POLYP_SERVER\n", t);
156
157 if (get_x11_prop(d, "POLYP_SOURCE", t, sizeof(t)))
158 printf("POLYP_SOURCE='%s'\nexport POLYP_SOURCE\n", t);
159 if (get_x11_prop(d, "POLYP_SINK", t, sizeof(t)))
160 printf("POLYP_SINK='%s'\nexport POLYP_SINK\n", t);
161
162 if (get_x11_prop(d, "POLYP_COOKIE", t, sizeof(t))) {
163 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
164 size_t l;
165 if ((l = pa_parsehex(t, cookie, sizeof(cookie))) == (size_t) -1) {
166 fprintf(stderr, "Failed to parse cookie data\n");
167 goto finish;
168 }
169
170 if (pa_authkey_save(cookie_file, cookie, l) < 0) {
171 fprintf(stderr, "Failed to save cookie data\n");
172 goto finish;
173 }
174 }
175
176 break;
177 }
178
179 case EXPORT: {
180 struct pa_client_conf *c = pa_client_conf_new();
181 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
182 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
183 assert(c);
184
185 if (pa_client_conf_load(c, NULL) < 0) {
186 fprintf(stderr, "Failed to load client configuration file.\n");
187 goto finish;
188 }
189
190 if (pa_client_conf_env(c) < 0) {
191 fprintf(stderr, "Failed to read environment configuration data.\n");
192 goto finish;
193 }
194
195 del_x11_prop(d, "POLYP_ID");
196
197 if (server)
198 set_x11_prop(d, "POLYP_SERVER", c->default_server);
199 else if (c->default_server)
200 set_x11_prop(d, "POLYP_SERVER", c->default_server);
201 else {
202 char hn[256];
203 if (!pa_get_fqdn(hn, sizeof(hn))) {
204 fprintf(stderr, "Failed to get FQDN.\n");
205 goto finish;
206 }
207
208 set_x11_prop(d, "POLYP_SERVER", hn);
209 }
210
211 if (sink)
212 set_x11_prop(d, "POLYP_SINK", sink);
213 else if (c->default_sink)
214 set_x11_prop(d, "POLYP_SINK", c->default_sink);
215
216 if (source)
217 set_x11_prop(d, "POLYP_SOURCE", source);
218 if (c->default_source)
219 set_x11_prop(d, "POLYP_SOURCE", c->default_source);
220
221 pa_client_conf_free(c);
222
223 if (pa_authkey_load_auto(cookie_file, cookie, sizeof(cookie)) < 0) {
224 fprintf(stderr, "Failed to load cookie data\n");
225 goto finish;
226 }
227
228 set_x11_prop(d, "POLYP_COOKIE", pa_hexstr(cookie, sizeof(cookie), hx, sizeof(hx)));
229 break;
230 }
231
232 case REMOVE:
233 del_x11_prop(d, "POLYP_SERVER");
234 del_x11_prop(d, "POLYP_SINK");
235 del_x11_prop(d, "POLYP_SOURCE");
236 del_x11_prop(d, "POLYP_ID");
237 del_x11_prop(d, "POLYP_COOKIE");
238 break;
239
240 default:
241 fprintf(stderr, "No yet implemented.\n");
242 goto finish;
243 }
244
245 ret = 0;
246
247 finish:
248
249 if (d) {
250 XSync(d, False);
251 XCloseDisplay(d);
252 }
253
254 return ret;
255 }