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