]> code.delx.au - pulseaudio/blob - src/modules/module-x11-publish.c
s/POLYP/PULSE/g
[pulseaudio] / src / modules / module-x11-publish.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 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 <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include <X11/Xlib.h>
33 #include <X11/Xatom.h>
34
35 #include <pulse/util.h>
36 #include <pulse/xmalloc.h>
37
38 #include <pulsecore/module.h>
39 #include <pulsecore/sink.h>
40 #include <pulsecore/core-scache.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/namereg.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/x11wrap.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/native-common.h>
47 #include <pulsecore/authkey-prop.h>
48 #include <pulsecore/authkey.h>
49 #include <pulsecore/x11prop.h>
50 #include <pulsecore/strlist.h>
51 #include <pulsecore/props.h>
52
53 #include "module-x11-publish-symdef.h"
54
55 PA_MODULE_AUTHOR("Lennart Poettering")
56 PA_MODULE_DESCRIPTION("X11 Credential Publisher")
57 PA_MODULE_VERSION(PACKAGE_VERSION)
58 PA_MODULE_USAGE("display=<X11 display>")
59
60 static const char* const valid_modargs[] = {
61 "display",
62 "sink",
63 "source",
64 "cookie",
65 NULL
66 };
67
68 struct userdata {
69 pa_core *core;
70 pa_x11_wrapper *x11_wrapper;
71 char *id;
72 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
73 int auth_cookie_in_property;
74 };
75
76 static int load_key(struct userdata *u, const char*fn) {
77 assert(u);
78
79 u->auth_cookie_in_property = 0;
80
81 if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
82 pa_log_debug(__FILE__": using already loaded auth cookie.");
83 pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
84 u->auth_cookie_in_property = 1;
85 return 0;
86 }
87
88 if (!fn)
89 fn = PA_NATIVE_COOKIE_FILE;
90
91 if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
92 return -1;
93
94 pa_log_debug(__FILE__": loading cookie from disk.");
95
96 if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
97 u->auth_cookie_in_property = 1;
98
99 return 0;
100 }
101
102 int pa__init(pa_core *c, pa_module*m) {
103 struct userdata *u;
104 pa_modargs *ma = NULL;
105 char hn[256], un[128];
106 char hx[PA_NATIVE_COOKIE_LENGTH*2+1];
107 const char *t;
108 char *s;
109 pa_strlist *l;
110
111 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
112 pa_log(__FILE__": failed to parse module arguments");
113 goto fail;
114 }
115
116 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
117 u->core = c;
118 u->id = NULL;
119 u->auth_cookie_in_property = 0;
120
121 if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
122 goto fail;
123
124 if (!(u->x11_wrapper = pa_x11_wrapper_get(c, pa_modargs_get_value(ma, "display", NULL))))
125 goto fail;
126
127 if (!(l = pa_property_get(c, PA_NATIVE_SERVER_PROPERTY_NAME)))
128 goto fail;
129
130 s = pa_strlist_tostring(l);
131 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER", s);
132 pa_xfree(s);
133
134 if (!pa_get_fqdn(hn, sizeof(hn)) || !pa_get_user_name(un, sizeof(un)))
135 goto fail;
136
137 u->id = pa_sprintf_malloc("%s@%s/%u", un, hn, (unsigned) getpid());
138 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", u->id);
139
140 if ((t = pa_modargs_get_value(ma, "source", NULL)))
141 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE", t);
142
143 if ((t = pa_modargs_get_value(ma, "sink", NULL)))
144 pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK", t);
145
146 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)));
147
148 pa_modargs_free(ma);
149 return 0;
150
151 fail:
152 if (ma)
153 pa_modargs_free(ma);
154
155 pa__done(c, m);
156 return -1;
157 }
158
159 void pa__done(pa_core *c, pa_module*m) {
160 struct userdata*u;
161 assert(c && m);
162
163 if (!(u = m->userdata))
164 return;
165
166 if (u->x11_wrapper) {
167 char t[256];
168
169 /* Yes, here is a race condition */
170 if (!pa_x11_get_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", t, sizeof(t)) || strcmp(t, u->id))
171 pa_log_warn(__FILE__": PulseAudio information vanished from X11!");
172 else {
173 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID");
174 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER");
175 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK");
176 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE");
177 pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_COOKIE");
178 XSync(pa_x11_wrapper_get_display(u->x11_wrapper), False);
179 }
180 }
181
182 if (u->x11_wrapper)
183 pa_x11_wrapper_unref(u->x11_wrapper);
184
185 if (u->auth_cookie_in_property)
186 pa_authkey_prop_unref(c, PA_NATIVE_COOKIE_PROPERTY_NAME);
187
188 pa_xfree(u->id);
189 pa_xfree(u);
190 }
191