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, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include <xcb/xcb.h>
30
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/module.h>
34 #include <pulsecore/modargs.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/x11wrap.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/native-common.h>
39 #include <pulsecore/auth-cookie.h>
40 #include <pulsecore/x11prop.h>
41 #include <pulsecore/strlist.h>
42 #include <pulsecore/protocol-native.h>
43
44 PA_MODULE_AUTHOR("Lennart Poettering");
45 PA_MODULE_DESCRIPTION("X11 credential publisher");
46 PA_MODULE_VERSION(PACKAGE_VERSION);
47 PA_MODULE_LOAD_ONCE(false);
48 PA_MODULE_USAGE(
49 "display=<X11 display> "
50 "sink=<Sink to publish> "
51 "source=<Source to publish> "
52 "cookie=<Cookie file to publish> ");
53
54 static const char* const valid_modargs[] = {
55 "display",
56 "sink",
57 "source",
58 "cookie",
59 "xauthority",
60 NULL
61 };
62
63 struct userdata {
64 pa_core *core;
65 pa_module *module;
66 pa_native_protocol *protocol;
67
68 char *id;
69 pa_auth_cookie *auth_cookie;
70
71 pa_x11_wrapper *x11_wrapper;
72 pa_x11_client *x11_client;
73
74 pa_hook_slot *hook_slot;
75 };
76
publish_servers(struct userdata * u,pa_strlist * l)77 static void publish_servers(struct userdata *u, pa_strlist *l) {
78
79 int screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
80
81 if (l) {
82 char *s;
83
84 l = pa_strlist_reverse(l);
85 s = pa_strlist_to_string(l);
86 pa_strlist_reverse(l);
87
88 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SERVER", s);
89 pa_xfree(s);
90 } else
91 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SERVER");
92 }
93
servers_changed_cb(void * hook_data,void * call_data,void * slot_data)94 static pa_hook_result_t servers_changed_cb(void *hook_data, void *call_data, void *slot_data) {
95 pa_strlist *servers = call_data;
96 struct userdata *u = slot_data;
97 char t[256];
98 int screen;
99
100 pa_assert(u);
101
102 screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
103 if (!pa_x11_get_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID", t, sizeof(t)) || !pa_streq(t, u->id)) {
104 pa_log_warn("PulseAudio information vanished from X11!");
105 return PA_HOOK_OK;
106 }
107
108 publish_servers(u, servers);
109 return PA_HOOK_OK;
110 }
111
x11_kill_cb(pa_x11_wrapper * w,void * userdata)112 static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
113 struct userdata *u = userdata;
114
115 pa_assert(w);
116 pa_assert(u);
117 pa_assert(u->x11_wrapper == w);
118
119 if (u->x11_client)
120 pa_x11_client_free(u->x11_client);
121
122 if (u->x11_wrapper)
123 pa_x11_wrapper_unref(u->x11_wrapper);
124
125 u->x11_client = NULL;
126 u->x11_wrapper = NULL;
127
128 pa_module_unload_request(u->module, true);
129 }
130
pa__init(pa_module * m)131 int pa__init(pa_module*m) {
132 struct userdata *u;
133 pa_modargs *ma = NULL;
134 char *mid, *sid;
135 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
136 const char *t;
137 int screen;
138
139 pa_assert(m);
140
141 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
142 pa_log("failed to parse module arguments");
143 goto fail;
144 }
145
146 m->userdata = u = pa_xnew(struct userdata, 1);
147 u->core = m->core;
148 u->module = m;
149 u->protocol = pa_native_protocol_get(m->core);
150 u->id = NULL;
151 u->auth_cookie = NULL;
152 u->x11_client = NULL;
153 u->x11_wrapper = NULL;
154
155 u->hook_slot = pa_hook_connect(&pa_native_protocol_hooks(u->protocol)[PA_NATIVE_HOOK_SERVERS_CHANGED], PA_HOOK_NORMAL, servers_changed_cb, u);
156
157 if (!(u->auth_cookie = pa_auth_cookie_get(m->core, pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), true, PA_NATIVE_COOKIE_LENGTH)))
158 goto fail;
159
160 if (pa_modargs_get_value(ma, "xauthority", NULL)) {
161 if (setenv("XAUTHORITY", pa_modargs_get_value(ma, "xauthority", NULL), 1)) {
162 pa_log("setenv() for $XAUTHORITY failed");
163 goto fail;
164 }
165 }
166
167 if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
168 goto fail;
169
170 screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
171 mid = pa_machine_id();
172 u->id = pa_sprintf_malloc("%lu@%s/%lu", (unsigned long) getuid(), mid, (unsigned long) getpid());
173 pa_xfree(mid);
174
175 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID", u->id);
176
177 if ((sid = pa_session_id())) {
178 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SESSION_ID", sid);
179 pa_xfree(sid);
180 }
181
182 publish_servers(u, pa_native_protocol_servers(u->protocol));
183
184 if ((t = pa_modargs_get_value(ma, "source", NULL)))
185 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SOURCE", t);
186
187 if ((t = pa_modargs_get_value(ma, "sink", NULL)))
188 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SINK", t);
189
190 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_COOKIE",
191 pa_hexstr(pa_auth_cookie_read(u->auth_cookie, PA_NATIVE_COOKIE_LENGTH), PA_NATIVE_COOKIE_LENGTH, hx, sizeof(hx)));
192
193 u->x11_client = pa_x11_client_new(u->x11_wrapper, NULL, x11_kill_cb, u);
194
195 pa_modargs_free(ma);
196
197 return 0;
198
199 fail:
200 if (ma)
201 pa_modargs_free(ma);
202
203 pa__done(m);
204
205 return -1;
206 }
207
pa__done(pa_module * m)208 void pa__done(pa_module*m) {
209 struct userdata*u;
210
211 pa_assert(m);
212
213 if (!(u = m->userdata))
214 return;
215
216 if (u->x11_client)
217 pa_x11_client_free(u->x11_client);
218
219 if (u->x11_wrapper) {
220 char t[256];
221 int screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
222
223 /* Yes, here is a race condition */
224 if (!pa_x11_get_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID", t, sizeof(t)) || !pa_streq(t, u->id))
225 pa_log_warn("PulseAudio information vanished from X11!");
226 else {
227 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID");
228 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SERVER");
229 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SINK");
230 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SOURCE");
231 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_COOKIE");
232 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SESSION_ID");
233 xcb_flush(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper));
234 }
235
236 pa_x11_wrapper_unref(u->x11_wrapper);
237 }
238
239 if (u->auth_cookie)
240 pa_auth_cookie_unref(u->auth_cookie);
241
242 if (u->hook_slot)
243 pa_hook_slot_free(u->hook_slot);
244
245 if (u->protocol)
246 pa_native_protocol_unref(u->protocol);
247
248 pa_xfree(u->id);
249 pa_xfree(u);
250 }
251