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