]> code.delx.au - pulseaudio/blob - src/modules/module-x11-publish.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-x11-publish.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
35
36 #include <pulse/util.h>
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/module.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/core-scache.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/namereg.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/x11wrap.h>
46 #include <pulsecore/core-util.h>
47 #include <pulsecore/native-common.h>
48 #include <pulsecore/authkey-prop.h>
49 #include <pulsecore/authkey.h>
50 #include <pulsecore/x11prop.h>
51 #include <pulsecore/strlist.h>
52 #include <pulsecore/props.h>
53
54 #include "module-x11-publish-symdef.h"
55
56 PA_MODULE_AUTHOR("Lennart Poettering")
57 PA_MODULE_DESCRIPTION("X11 Credential Publisher")
58 PA_MODULE_VERSION(PACKAGE_VERSION)
59 PA_MODULE_USAGE("display=<X11 display>")
60
61 static const char* const valid_modargs[] = {
62 "display",
63 "sink",
64 "source",
65 "cookie",
66 NULL
67 };
68
69 struct userdata {
70 pa_core *core;
71 pa_x11_wrapper *x11_wrapper;
72 char *id;
73 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
74 int auth_cookie_in_property;
75 };
76
77 static int load_key(struct userdata *u, const char*fn) {
78 pa_assert(u);
79
80 u->auth_cookie_in_property = 0;
81
82 if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
83 pa_log_debug("using already loaded auth cookie.");
84 pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
85 u->auth_cookie_in_property = 1;
86 return 0;
87 }
88
89 if (!fn)
90 fn = PA_NATIVE_COOKIE_FILE;
91
92 if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
93 return -1;
94
95 pa_log_debug("Loading cookie from disk.");
96
97 if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
98 u->auth_cookie_in_property = 1;
99
100 return 0;
101 }
102
103 int pa__init(pa_module*m) {
104 struct userdata *u;
105 pa_modargs *ma = NULL;
106 char hn[256], un[128];
107 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
108 const char *t;
109 char *s;
110 pa_strlist *l;
111
112 pa_assert(m);
113
114 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
115 pa_log("failed to parse module arguments");
116 goto fail;
117 }
118
119 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
120 u->core = m->core;
121 u->id = NULL;
122 u->auth_cookie_in_property = 0;
123
124 if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
125 goto fail;
126
127 if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
128 goto fail;
129
130 if (!(l = pa_property_get(m->core, PA_NATIVE_SERVER_PROPERTY_NAME)))
131 goto fail;
132
133 s = pa_strlist_tostring(l);
134 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER", s);
135 pa_xfree(s);
136
137 if (!pa_get_fqdn(hn, sizeof(hn)) || !pa_get_user_name(un, sizeof(un)))
138 goto fail;
139
140 u->id = pa_sprintf_malloc("%s@%s/%u", un, hn, (unsigned) getpid());
141 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", u->id);
142
143 if ((t = pa_modargs_get_value(ma, "source", NULL)))
144 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE", t);
145
146 if ((t = pa_modargs_get_value(ma, "sink", NULL)))
147 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK", t);
148
149 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_COOKIE", pa_hexstr(u->auth_cookie, sizeof(u->auth_cookie), hx, sizeof(hx)));
150
151 pa_modargs_free(ma);
152 return 0;
153
154 fail:
155 if (ma)
156 pa_modargs_free(ma);
157
158 pa__done(m);
159 return -1;
160 }
161
162 void pa__done(pa_module*m) {
163 struct userdata*u;
164
165 pa_assert(m);
166
167 if (!(u = m->userdata))
168 return;
169
170 if (u->x11_wrapper) {
171 char t[256];
172
173 /* Yes, here is a race condition */
174 if (!pa_x11_get_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", t, sizeof(t)) || strcmp(t, u->id))
175 pa_log_warn("PulseAudio information vanished from X11!");
176 else {
177 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID");
178 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER");
179 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK");
180 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE");
181 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_COOKIE");
182 XSync(pa_x11_wrapper_get_display(u->x11_wrapper), False);
183 }
184 }
185
186 if (u->x11_wrapper)
187 pa_x11_wrapper_unref(u->x11_wrapper);
188
189 if (u->auth_cookie_in_property)
190 pa_authkey_prop_unref(m->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
191
192 pa_xfree(u->id);
193 pa_xfree(u);
194 }
195