]> code.delx.au - pulseaudio/blob - src/modules/module-x11-publish.c
Use LGPL 2.1 on all files previously using LGPL 2
[pulseaudio] / src / modules / module-x11-publish.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio 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 PulseAudio; 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 <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <X11/Xlib.h>
32 #include <X11/Xatom.h>
33
34 #include <pulse/util.h>
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/module.h>
38 #include <pulsecore/sink.h>
39 #include <pulsecore/core-scache.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/namereg.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/x11wrap.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/native-common.h>
46 #include <pulsecore/auth-cookie.h>
47 #include <pulsecore/x11prop.h>
48 #include <pulsecore/strlist.h>
49 #include <pulsecore/shared.h>
50 #include <pulsecore/protocol-native.h>
51
52 #include "module-x11-publish-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering");
55 PA_MODULE_DESCRIPTION("X11 credential publisher");
56 PA_MODULE_VERSION(PACKAGE_VERSION);
57 PA_MODULE_LOAD_ONCE(FALSE);
58 PA_MODULE_USAGE(
59 "display=<X11 display> "
60 "sink=<Sink to publish> "
61 "source=<Source to publish> "
62 "cookie=<Cookie file to publish> ");
63
64 static const char* const valid_modargs[] = {
65 "display",
66 "sink",
67 "source",
68 "cookie",
69 NULL
70 };
71
72 struct userdata {
73 pa_core *core;
74 pa_module *module;
75 pa_native_protocol *protocol;
76
77 char *id;
78 pa_auth_cookie *auth_cookie;
79
80 pa_x11_wrapper *x11_wrapper;
81 pa_x11_client *x11_client;
82
83 pa_hook_slot *hook_slot;
84 };
85
86 static void publish_servers(struct userdata *u, pa_strlist *l) {
87
88 if (l) {
89 char *s;
90
91 l = pa_strlist_reverse(l);
92 s = pa_strlist_tostring(l);
93 l = pa_strlist_reverse(l);
94
95 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER", s);
96 pa_xfree(s);
97 } else
98 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER");
99 }
100
101 static pa_hook_result_t servers_changed_cb(void *hook_data, void *call_data, void *slot_data) {
102 pa_strlist *servers = call_data;
103 struct userdata *u = slot_data;
104 char t[256];
105
106 pa_assert(u);
107
108 if (!pa_x11_get_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", t, sizeof(t)) || strcmp(t, u->id)) {
109 pa_log_warn("PulseAudio information vanished from X11!");
110 return PA_HOOK_OK;
111 }
112
113 publish_servers(u, servers);
114 return PA_HOOK_OK;
115 }
116
117 static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
118 struct userdata *u = userdata;
119
120 pa_assert(w);
121 pa_assert(u);
122 pa_assert(u->x11_wrapper == w);
123
124 if (u->x11_client)
125 pa_x11_client_free(u->x11_client);
126
127 if (u->x11_wrapper)
128 pa_x11_wrapper_unref(u->x11_wrapper);
129
130 u->x11_client = NULL;
131 u->x11_wrapper = NULL;
132
133 pa_module_unload_request(u->module, TRUE);
134 }
135
136 int pa__init(pa_module*m) {
137 struct userdata *u;
138 pa_modargs *ma = NULL;
139 char hn[256], un[128];
140 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
141 const char *t;
142
143 pa_assert(m);
144
145 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
146 pa_log("failed to parse module arguments");
147 goto fail;
148 }
149
150 m->userdata = u = pa_xnew(struct userdata, 1);
151 u->core = m->core;
152 u->module = m;
153 u->protocol = pa_native_protocol_get(m->core);
154 u->id = NULL;
155 u->auth_cookie = NULL;
156 u->x11_client = NULL;
157 u->x11_wrapper = NULL;
158
159 u->hook_slot = pa_hook_connect(&pa_native_protocol_hooks(u->protocol)[PA_NATIVE_HOOK_SERVERS_CHANGED], PA_HOOK_NORMAL, servers_changed_cb, u);
160
161 if (!(u->auth_cookie = pa_auth_cookie_get(m->core, pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), PA_NATIVE_COOKIE_LENGTH)))
162 goto fail;
163
164 if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
165 goto fail;
166
167 if (!pa_get_fqdn(hn, sizeof(hn)) || !pa_get_user_name(un, sizeof(un)))
168 goto fail;
169
170 u->id = pa_sprintf_malloc("%s@%s/%u", un, hn, (unsigned) getpid());
171 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", u->id);
172
173 publish_servers(u, pa_native_protocol_servers(u->protocol));
174
175 if ((t = pa_modargs_get_value(ma, "source", NULL)))
176 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE", t);
177
178 if ((t = pa_modargs_get_value(ma, "sink", NULL)))
179 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK", t);
180
181 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_COOKIE",
182 pa_hexstr(pa_auth_cookie_read(u->auth_cookie, PA_NATIVE_COOKIE_LENGTH), PA_NATIVE_COOKIE_LENGTH, hx, sizeof(hx)));
183
184 u->x11_client = pa_x11_client_new(u->x11_wrapper, NULL, x11_kill_cb, u);
185
186 pa_modargs_free(ma);
187
188 return 0;
189
190 fail:
191 if (ma)
192 pa_modargs_free(ma);
193
194 pa__done(m);
195
196 return -1;
197 }
198
199 void pa__done(pa_module*m) {
200 struct userdata*u;
201
202 pa_assert(m);
203
204 if (!(u = m->userdata))
205 return;
206
207 if (u->x11_client)
208 pa_x11_client_free(u->x11_client);
209
210 if (u->x11_wrapper) {
211 char t[256];
212
213 /* Yes, here is a race condition */
214 if (!pa_x11_get_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", t, sizeof(t)) || strcmp(t, u->id))
215 pa_log_warn("PulseAudio information vanished from X11!");
216 else {
217 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID");
218 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER");
219 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK");
220 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE");
221 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_COOKIE");
222 XSync(pa_x11_wrapper_get_display(u->x11_wrapper), False);
223 }
224
225 pa_x11_wrapper_unref(u->x11_wrapper);
226 }
227
228 if (u->auth_cookie)
229 pa_auth_cookie_unref(u->auth_cookie);
230
231 if (u->hook_slot)
232 pa_hook_slot_free(u->hook_slot);
233
234 if (u->protocol)
235 pa_native_protocol_unref(u->protocol);
236
237 pa_xfree(u->id);
238 pa_xfree(u);
239 }