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