minor fixes
[profile/ivi/pulseaudio.git] / 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 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 "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                 printf("Server: %s\n", t);
101             if (pa_x11_get_prop(d, "POLYP_SOURCE", t, sizeof(t)))
102                 printf("Source: %s\n", t);
103             if (pa_x11_get_prop(d, "POLYP_SINK", t, sizeof(t)))
104                 printf("Sink: %s\n", t);
105             if (pa_x11_get_prop(d, "POLYP_COOKIE", t, sizeof(t)))
106                 printf("Cookie: %s\n", t);
107
108             break;
109         }
110             
111         case IMPORT: {
112             char t[1024];
113             if (pa_x11_get_prop(d, "POLYP_SERVER", t, sizeof(t))) 
114                 printf("POLYP_SERVER='%s'\nexport POLYP_SERVER\n", t);
115             if (pa_x11_get_prop(d, "POLYP_SOURCE", t, sizeof(t)))
116                 printf("POLYP_SOURCE='%s'\nexport POLYP_SOURCE\n", t);
117             if (pa_x11_get_prop(d, "POLYP_SINK", t, sizeof(t)))
118                 printf("POLYP_SINK='%s'\nexport POLYP_SINK\n", t);
119
120             if (pa_x11_get_prop(d, "POLYP_COOKIE", t, sizeof(t))) {
121                 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
122                 size_t l;
123                 if ((l = pa_parsehex(t, cookie, sizeof(cookie))) != sizeof(cookie)) {
124                     fprintf(stderr, "Failed to parse cookie data\n");
125                     goto finish;
126                 }
127
128                 if (pa_authkey_save(cookie_file, cookie, l) < 0) {
129                     fprintf(stderr, "Failed to save cookie data\n");
130                     goto finish;
131                 }
132             }
133
134             break;
135         }
136
137         case EXPORT: {
138             struct pa_client_conf *c = pa_client_conf_new();
139             uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
140             char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
141             assert(c);
142
143             if (pa_client_conf_load(c, NULL) < 0) {
144                 fprintf(stderr, "Failed to load client configuration file.\n");
145                 goto finish;
146             }
147
148             if (pa_client_conf_env(c) < 0) {
149                 fprintf(stderr, "Failed to read environment configuration data.\n");
150                 goto finish;
151             }
152
153             pa_x11_del_prop(d, "POLYP_SERVER");
154             pa_x11_del_prop(d, "POLYP_SINK");
155             pa_x11_del_prop(d, "POLYP_SOURCE");
156             pa_x11_del_prop(d, "POLYP_ID");
157             pa_x11_del_prop(d, "POLYP_COOKIE");
158             
159             if (server)
160                 pa_x11_set_prop(d, "POLYP_SERVER", server);
161             else if (c->default_server)
162                 pa_x11_set_prop(d, "POLYP_SERVER", c->default_server);
163             else {
164                 char hn[256];
165                 if (!pa_get_fqdn(hn, sizeof(hn))) {
166                     fprintf(stderr, "Failed to get FQDN.\n");
167                     goto finish;
168                 }
169                     
170                 pa_x11_set_prop(d, "POLYP_SERVER", hn);
171             }
172
173             if (sink)
174                 pa_x11_set_prop(d, "POLYP_SINK", sink);
175             else if (c->default_sink)
176                 pa_x11_set_prop(d, "POLYP_SINK", c->default_sink);
177
178             if (source)
179                 pa_x11_set_prop(d, "POLYP_SOURCE", source);
180             if (c->default_source)
181                 pa_x11_set_prop(d, "POLYP_SOURCE", c->default_source);
182
183             pa_client_conf_free(c);
184             
185             if (pa_authkey_load_auto(cookie_file, cookie, sizeof(cookie)) < 0) {
186                 fprintf(stderr, "Failed to load cookie data\n");
187                 goto finish;
188             }
189
190             pa_x11_set_prop(d, "POLYP_COOKIE", pa_hexstr(cookie, sizeof(cookie), hx, sizeof(hx)));
191             break;
192         }
193
194         case REMOVE:
195             pa_x11_del_prop(d, "POLYP_SERVER");
196             pa_x11_del_prop(d, "POLYP_SINK");
197             pa_x11_del_prop(d, "POLYP_SOURCE");
198             pa_x11_del_prop(d, "POLYP_ID");
199             pa_x11_del_prop(d, "POLYP_COOKIE");
200             break;
201             
202         default:
203             fprintf(stderr, "No yet implemented.\n");
204             goto finish;
205     }
206
207     ret = 0;
208     
209 finish:
210
211     if (d) {
212         XSync(d, False);
213         XCloseDisplay(d);
214     }
215     
216     return ret;
217 }