add new module module-zeroconf-discover
[pulseaudio.git] / src / utils / pax11publish.c
blob9a50f8ef0e2b73306932f654cd018fdbc3552b88
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <assert.h>
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
36 #include <pulse/util.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/authkey.h>
41 #include <pulsecore/native-common.h>
42 #include <pulsecore/x11prop.h>
44 #include "../pulse/client-conf.h"
46 int main(int argc, char *argv[]) {
47 const char *dname = NULL, *sink = NULL, *source = NULL, *server = NULL, *cookie_file = PA_NATIVE_COOKIE_FILE;
48 int c, ret = 1;
49 Display *d = NULL;
50 enum { DUMP, EXPORT, IMPORT, REMOVE } mode = DUMP;
52 while ((c = getopt(argc, argv, "deiD:S:O:I:c:hr")) != -1) {
53 switch (c) {
54 case 'D' :
55 dname = optarg;
56 break;
57 case 'h':
58 printf("%s [-D display] [-S server] [-O sink] [-I source] [-c file] [-d|-e|-i|-r]\n\n"
59 " -d Show current PulseAudio data attached to X11 display (default)\n"
60 " -e Export local PulseAudio data to X11 display\n"
61 " -i Import PulseAudio data from X11 display to local environment variables and cookie file.\n"
62 " -r Remove PulseAudio data from X11 display\n",
63 pa_path_get_filename(argv[0]));
64 ret = 0;
65 goto finish;
66 case 'd':
67 mode = DUMP;
68 break;
69 case 'e':
70 mode = EXPORT;
71 break;
72 case 'i':
73 mode = IMPORT;
74 break;
75 case 'r':
76 mode = REMOVE;
77 break;
78 case 'c':
79 cookie_file = optarg;
80 break;
81 case 'I':
82 source = optarg;
83 break;
84 case 'O':
85 sink = optarg;
86 break;
87 case 'S':
88 server = optarg;
89 break;
90 default:
91 fprintf(stderr, "Failed to parse command line.\n");
92 goto finish;
96 if (!(d = XOpenDisplay(dname))) {
97 pa_log("XOpenDisplay() failed");
98 goto finish;
101 switch (mode) {
102 case DUMP: {
103 char t[1024];
104 if (pa_x11_get_prop(d, "PULSE_SERVER", t, sizeof(t)))
105 printf("Server: %s\n", t);
106 if (pa_x11_get_prop(d, "PULSE_SOURCE", t, sizeof(t)))
107 printf("Source: %s\n", t);
108 if (pa_x11_get_prop(d, "PULSE_SINK", t, sizeof(t)))
109 printf("Sink: %s\n", t);
110 if (pa_x11_get_prop(d, "PULSE_COOKIE", t, sizeof(t)))
111 printf("Cookie: %s\n", t);
113 break;
116 case IMPORT: {
117 char t[1024];
118 if (pa_x11_get_prop(d, "PULSE_SERVER", t, sizeof(t)))
119 printf("PULSE_SERVER='%s'\nexport PULSE_SERVER\n", t);
120 if (pa_x11_get_prop(d, "PULSE_SOURCE", t, sizeof(t)))
121 printf("PULSE_SOURCE='%s'\nexport PULSE_SOURCE\n", t);
122 if (pa_x11_get_prop(d, "PULSE_SINK", t, sizeof(t)))
123 printf("PULSE_SINK='%s'\nexport PULSE_SINK\n", t);
125 if (pa_x11_get_prop(d, "PULSE_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;
133 if (pa_authkey_save(cookie_file, cookie, l) < 0) {
134 fprintf(stderr, "Failed to save cookie data\n");
135 goto finish;
139 break;
142 case EXPORT: {
143 pa_client_conf *conf = pa_client_conf_new();
144 uint8_t cookie[PA_NATIVE_COOKIE_LENGTH];
145 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
146 assert(conf);
148 if (pa_client_conf_load(conf, NULL) < 0) {
149 fprintf(stderr, "Failed to load client configuration file.\n");
150 goto finish;
153 if (pa_client_conf_env(conf) < 0) {
154 fprintf(stderr, "Failed to read environment configuration data.\n");
155 goto finish;
158 pa_x11_del_prop(d, "PULSE_SERVER");
159 pa_x11_del_prop(d, "PULSE_SINK");
160 pa_x11_del_prop(d, "PULSE_SOURCE");
161 pa_x11_del_prop(d, "PULSE_ID");
162 pa_x11_del_prop(d, "PULSE_COOKIE");
164 if (server)
165 pa_x11_set_prop(d, "PULSE_SERVER", server);
166 else if (conf->default_server)
167 pa_x11_set_prop(d, "PULSE_SERVER", conf->default_server);
168 else {
169 char hn[256];
170 if (!pa_get_fqdn(hn, sizeof(hn))) {
171 fprintf(stderr, "Failed to get FQDN.\n");
172 goto finish;
175 pa_x11_set_prop(d, "PULSE_SERVER", hn);
178 if (sink)
179 pa_x11_set_prop(d, "PULSE_SINK", sink);
180 else if (conf->default_sink)
181 pa_x11_set_prop(d, "PULSE_SINK", conf->default_sink);
183 if (source)
184 pa_x11_set_prop(d, "PULSE_SOURCE", source);
185 if (conf->default_source)
186 pa_x11_set_prop(d, "PULSE_SOURCE", conf->default_source);
188 pa_client_conf_free(conf);
190 if (pa_authkey_load_auto(cookie_file, cookie, sizeof(cookie)) < 0) {
191 fprintf(stderr, "Failed to load cookie data\n");
192 goto finish;
195 pa_x11_set_prop(d, "PULSE_COOKIE", pa_hexstr(cookie, sizeof(cookie), hx, sizeof(hx)));
196 break;
199 case REMOVE:
200 pa_x11_del_prop(d, "PULSE_SERVER");
201 pa_x11_del_prop(d, "PULSE_SINK");
202 pa_x11_del_prop(d, "PULSE_SOURCE");
203 pa_x11_del_prop(d, "PULSE_ID");
204 pa_x11_del_prop(d, "PULSE_COOKIE");
205 break;
207 default:
208 fprintf(stderr, "No yet implemented.\n");
209 goto finish;
212 ret = 0;
214 finish:
216 if (d) {
217 XSync(d, False);
218 XCloseDisplay(d);
221 return ret;