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