]> code.delx.au - pulseaudio/blob - src/utils/pax11publish.c
Cleaned up the includes after the restructuring. Indicate which headers are
[pulseaudio] / src / utils / 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 Lesser 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 Lesser 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 <polypcore/util.h>
35 #include <polypcore/log.h>
36 #include <polypcore/authkey.h>
37 #include <polypcore/native-common.h>
38 #include <polypcore/x11prop.h>
39
40 #include "../polyp/client-conf.h"
41
42 int main(int argc, char *argv[]) {
43 const char *dname = NULL, *sink = NULL, *source = NULL, *server = NULL, *cookie_file = PA_NATIVE_COOKIE_FILE;
44 int c, ret = 1;
45 Display *d = NULL;
46 enum { DUMP, EXPORT, IMPORT, REMOVE } mode = DUMP;
47
48 while ((c = getopt(argc, argv, "deiD:S:O:I:c:hr")) != -1) {
49 switch (c) {
50 case 'D' :
51 dname = optarg;
52 break;
53 case 'h':
54 printf("%s [-D display] [-S server] [-O sink] [-I source] [-c file] [-d|-e|-i|-r]\n\n"
55 " -d Show current Polypaudio data attached to X11 display (default)\n"
56 " -e Export local Polypaudio data to X11 display\n"
57 " -i Import Polypaudio data from X11 display to local environment variables and cookie file.\n"
58 " -r Remove Polypaudio data from X11 display\n",
59 pa_path_get_filename(argv[0]));
60 ret = 0;
61 goto finish;
62 case 'd':
63 mode = DUMP;
64 break;
65 case 'e':
66 mode = EXPORT;
67 break;
68 case 'i':
69 mode = IMPORT;
70 break;
71 case 'r':
72 mode = REMOVE;
73 break;
74 case 'c':
75 cookie_file = optarg;
76 break;
77 case 'I':
78 source = optarg;
79 break;
80 case 'O':
81 sink = optarg;
82 break;
83 case 'S':
84 server = optarg;
85 break;
86 default:
87 fprintf(stderr, "Failed to parse command line.\n");
88 goto finish;
89 }
90 }
91
92 if (!(d = XOpenDisplay(dname))) {
93 pa_log(__FILE__": XOpenDisplay() failed\n");
94 goto finish;
95 }
96
97 switch (mode) {
98 case DUMP: {
99 char t[1024];
100 if (pa_x11_get_prop(d, "POLYP_SERVER", t, sizeof(t)))
101 printf("Server: %s\n", t);
102 if (pa_x11_get_prop(d, "POLYP_SOURCE", t, sizeof(t)))
103 printf("Source: %s\n", t);
104 if (pa_x11_get_prop(d, "POLYP_SINK", t, sizeof(t)))
105 printf("Sink: %s\n", t);
106 if (pa_x11_get_prop(d, "POLYP_COOKIE", t, sizeof(t)))
107 printf("Cookie: %s\n", t);
108
109 break;
110 }
111
112 case IMPORT: {
113 char t[1024];
114 if (pa_x11_get_prop(d, "POLYP_SERVER", t, sizeof(t)))
115 printf("POLYP_SERVER='%s'\nexport POLYP_SERVER\n", t);
116 if (pa_x11_get_prop(d, "POLYP_SOURCE", t, sizeof(t)))
117 printf("POLYP_SOURCE='%s'\nexport POLYP_SOURCE\n", t);
118 if (pa_x11_get_prop(d, "POLYP_SINK", t, sizeof(t)))
119 printf("POLYP_SINK='%s'\nexport POLYP_SINK\n", t);
120
121 if (pa_x11_get_prop(d, "POLYP_COOKIE", t, sizeof(t))) {
122 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
123 size_t l;
124 if ((l = pa_parsehex(t, cookie, sizeof(cookie))) != sizeof(cookie)) {
125 fprintf(stderr, "Failed to parse cookie data\n");
126 goto finish;
127 }
128
129 if (pa_authkey_save(cookie_file, cookie, l) < 0) {
130 fprintf(stderr, "Failed to save cookie data\n");
131 goto finish;
132 }
133 }
134
135 break;
136 }
137
138 case EXPORT: {
139 pa_client_conf *conf = pa_client_conf_new();
140 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
141 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
142 assert(conf);
143
144 if (pa_client_conf_load(conf, NULL) < 0) {
145 fprintf(stderr, "Failed to load client configuration file.\n");
146 goto finish;
147 }
148
149 if (pa_client_conf_env(conf) < 0) {
150 fprintf(stderr, "Failed to read environment configuration data.\n");
151 goto finish;
152 }
153
154 pa_x11_del_prop(d, "POLYP_SERVER");
155 pa_x11_del_prop(d, "POLYP_SINK");
156 pa_x11_del_prop(d, "POLYP_SOURCE");
157 pa_x11_del_prop(d, "POLYP_ID");
158 pa_x11_del_prop(d, "POLYP_COOKIE");
159
160 if (server)
161 pa_x11_set_prop(d, "POLYP_SERVER", server);
162 else if (conf->default_server)
163 pa_x11_set_prop(d, "POLYP_SERVER", conf->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 (conf->default_sink)
177 pa_x11_set_prop(d, "POLYP_SINK", conf->default_sink);
178
179 if (source)
180 pa_x11_set_prop(d, "POLYP_SOURCE", source);
181 if (conf->default_source)
182 pa_x11_set_prop(d, "POLYP_SOURCE", conf->default_source);
183
184 pa_client_conf_free(conf);
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 }