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 pa_log_debug("X11 client kill callback called");
120
121 if (u->x11_client)
122 pa_x11_client_free(u->x11_client);
123
124 if (u->x11_wrapper)
125 pa_x11_wrapper_unref(u->x11_wrapper);
126
127 u->x11_client = NULL;
128 u->x11_wrapper = NULL;
129
130 pa_module_unload_request(u->module, true);
131 }
132
pa__init(pa_module * m)133 int pa__init(pa_module*m) {
134 struct userdata *u;
135 pa_modargs *ma = NULL;
136 char *mid, *sid;
137 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
138 const char *t;
139 int screen;
140
141 pa_assert(m);
142
143 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
144 pa_log("failed to parse module arguments");
145 goto fail;
146 }
147
148 m->userdata = u = pa_xnew(struct userdata, 1);
149 u->core = m->core;
150 u->module = m;
151 u->protocol = pa_native_protocol_get(m->core);
152 u->id = NULL;
153 u->auth_cookie = NULL;
154 u->x11_client = NULL;
155 u->x11_wrapper = NULL;
156
157 u->hook_slot = pa_hook_connect(&pa_native_protocol_hooks(u->protocol)[PA_NATIVE_HOOK_SERVERS_CHANGED], PA_HOOK_NORMAL, servers_changed_cb, u);
158
159 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)))
160 goto fail;
161
162 if (pa_modargs_get_value(ma, "xauthority", NULL)) {
163 if (setenv("XAUTHORITY", pa_modargs_get_value(ma, "xauthority", NULL), 1)) {
164 pa_log("setenv() for $XAUTHORITY failed");
165 goto fail;
166 }
167 }
168
169 if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
170 goto fail;
171
172 screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
173 mid = pa_machine_id();
174 u->id = pa_sprintf_malloc("%lu@%s/%lu", (unsigned long) getuid(), mid, (unsigned long) getpid());
175 pa_xfree(mid);
176
177 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID", u->id);
178
179 if ((sid = pa_session_id())) {
180 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SESSION_ID", sid);
181 pa_xfree(sid);
182 }
183
184 publish_servers(u, pa_native_protocol_servers(u->protocol));
185
186 if ((t = pa_modargs_get_value(ma, "source", NULL)))
187 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SOURCE", t);
188
189 if ((t = pa_modargs_get_value(ma, "sink", NULL)))
190 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SINK", t);
191
192 pa_x11_set_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_COOKIE",
193 pa_hexstr(pa_auth_cookie_read(u->auth_cookie, PA_NATIVE_COOKIE_LENGTH), PA_NATIVE_COOKIE_LENGTH, hx, sizeof(hx)));
194
195 u->x11_client = pa_x11_client_new(u->x11_wrapper, NULL, x11_kill_cb, u);
196
197 pa_modargs_free(ma);
198
199 return 0;
200
201 fail:
202 if (ma)
203 pa_modargs_free(ma);
204
205 pa__done(m);
206
207 return -1;
208 }
209
pa__done(pa_module * m)210 void pa__done(pa_module*m) {
211 struct userdata*u;
212
213 pa_assert(m);
214
215 if (!(u = m->userdata))
216 return;
217
218 if (u->x11_client)
219 pa_x11_client_free(u->x11_client);
220
221 if (u->x11_wrapper) {
222 char t[256];
223 int screen = DefaultScreen(pa_x11_wrapper_get_display(u->x11_wrapper));
224
225 /* Yes, here is a race condition */
226 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))
227 pa_log_warn("PulseAudio information vanished from X11!");
228 else {
229 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_ID");
230 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SERVER");
231 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SINK");
232 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SOURCE");
233 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_COOKIE");
234 pa_x11_del_prop(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper), screen, "PULSE_SESSION_ID");
235 xcb_flush(pa_x11_wrapper_get_xcb_connection(u->x11_wrapper));
236 }
237
238 pa_x11_wrapper_unref(u->x11_wrapper);
239 }
240
241 if (u->auth_cookie)
242 pa_auth_cookie_unref(u->auth_cookie);
243
244 if (u->hook_slot)
245 pa_hook_slot_free(u->hook_slot);
246
247 if (u->protocol)
248 pa_native_protocol_unref(u->protocol);
249
250 pa_xfree(u->id);
251 pa_xfree(u);
252 }
253