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