]> code.delx.au - pulseaudio/blob - src/pulsecore/proplist-util.c
Merge commit 'origin/master-tx'
[pulseaudio] / src / pulsecore / proplist-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 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
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License 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 <string.h>
27 #include <locale.h>
28 #include <dlfcn.h>
29
30 #ifdef __APPLE__
31 #include <crt_externs.h>
32 #define environ (*_NSGetEnviron())
33 #elif !HAVE_DECL_ENVIRON
34 extern char **environ;
35 #endif
36
37 #include <pulse/gccmacro.h>
38 #include <pulse/proplist.h>
39 #include <pulse/utf8.h>
40 #include <pulse/xmalloc.h>
41 #include <pulse/util.h>
42
43 #include <pulsecore/core-util.h>
44
45 #if defined(HAVE_GLIB) && defined(PA_GCC_WEAKREF)
46 #include <glib.h>
47 static G_CONST_RETURN gchar* _g_get_application_name(void) PA_GCC_WEAKREF(g_get_application_name);
48 #endif
49
50 #if defined(HAVE_GTK) && defined(PA_GCC_WEAKREF)
51 #include <gtk/gtk.h>
52 #include <gdk/gdkx.h>
53 static G_CONST_RETURN gchar* _gtk_window_get_default_icon_name(void) PA_GCC_WEAKREF(gtk_window_get_default_icon_name);
54 static Display *_gdk_display PA_GCC_WEAKREF(gdk_display);
55 #endif
56
57 #include "proplist-util.h"
58
59 static void add_glib_properties(pa_proplist *p) {
60
61 #if defined(HAVE_GLIB) && defined(PA_GCC_WEAKREF)
62
63 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME))
64 if (_g_get_application_name) {
65 const gchar *t;
66
67 /* We ignore the tiny race condition here. */
68
69 if ((t = _g_get_application_name()))
70 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, t);
71 }
72
73 #endif
74 }
75
76 static void add_gtk_properties(pa_proplist *p) {
77
78 #if defined(HAVE_GTK) && defined(PA_GCC_WEAKREF)
79
80 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_ICON_NAME))
81 if (_gtk_window_get_default_icon_name) {
82 const gchar *t;
83
84 /* We ignore the tiny race condition here. */
85
86 if ((t = _gtk_window_get_default_icon_name()))
87 pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, t);
88 }
89
90 if (!pa_proplist_contains(p, PA_PROP_WINDOW_X11_DISPLAY))
91 if (&_gdk_display && _gdk_display) {
92 const char *t;
93
94 /* We ignore the tiny race condition here. */
95
96 if ((t = DisplayString(_gdk_display)))
97 pa_proplist_sets(p, PA_PROP_WINDOW_X11_DISPLAY, t);
98 }
99
100 #endif
101 }
102
103 void pa_init_proplist(pa_proplist *p) {
104 char **e;
105 const char *pp;
106
107 pa_assert(p);
108
109 if (environ) {
110
111 /* Some applications seem to reset environ to NULL for various
112 * reasons, hence we need to check for this explicitly. See
113 * rhbz #473080 */
114
115 for (e = environ; *e; e++) {
116
117 if (pa_startswith(*e, "PULSE_PROP_")) {
118 size_t kl, skip;
119 char *k;
120 pa_bool_t override;
121
122 if (pa_startswith(*e, "PULSE_PROP_OVERRIDE_")) {
123 skip = 20;
124 override = TRUE;
125 } else {
126 skip = 11;
127 override = FALSE;
128 }
129
130 kl = strcspn(*e+skip, "=");
131
132 if ((*e)[skip+kl] != '=')
133 continue;
134
135 k = pa_xstrndup(*e+skip, kl);
136
137 if (override || !pa_proplist_contains(p, k))
138 pa_proplist_sets(p, k, *e+skip+kl+1);
139 pa_xfree(k);
140 }
141 }
142 }
143
144 if ((pp = getenv("PULSE_PROP"))) {
145 pa_proplist *t;
146
147 if ((t = pa_proplist_from_string(pp))) {
148 pa_proplist_update(p, PA_UPDATE_MERGE, t);
149 pa_proplist_free(t);
150 }
151 }
152
153 if ((pp = getenv("PULSE_PROP_OVERRIDE"))) {
154 pa_proplist *t;
155
156 if ((t = pa_proplist_from_string(pp))) {
157 pa_proplist_update(p, PA_UPDATE_REPLACE, t);
158 pa_proplist_free(t);
159 }
160 }
161
162 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_ID)) {
163 char t[32];
164 pa_snprintf(t, sizeof(t), "%lu", (unsigned long) getpid());
165 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_ID, t);
166 }
167
168 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_USER)) {
169 char t[64];
170 if (pa_get_user_name(t, sizeof(t))) {
171 char *c = pa_utf8_filter(t);
172 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_USER, c);
173 pa_xfree(c);
174 }
175 }
176
177 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_HOST)) {
178 char t[64];
179 if (pa_get_host_name(t, sizeof(t))) {
180 char *c = pa_utf8_filter(t);
181 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_HOST, c);
182 pa_xfree(c);
183 }
184 }
185
186 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_BINARY)) {
187 char t[PATH_MAX];
188 if (pa_get_binary_name(t, sizeof(t))) {
189 char *c = pa_utf8_filter(t);
190 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_BINARY, c);
191 pa_xfree(c);
192 }
193 }
194
195 add_glib_properties(p);
196 add_gtk_properties(p);
197
198 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME)) {
199 const char *t;
200
201 if ((t = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
202 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, t);
203 }
204
205 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_LANGUAGE)) {
206 const char *l;
207
208 if ((l = setlocale(LC_MESSAGES, NULL)))
209 pa_proplist_sets(p, PA_PROP_APPLICATION_LANGUAGE, l);
210 }
211
212 if (!pa_proplist_contains(p, PA_PROP_WINDOW_X11_DISPLAY)) {
213 const char *t;
214
215 if ((t = getenv("DISPLAY"))) {
216 char *c = pa_utf8_filter(t);
217 pa_proplist_sets(p, PA_PROP_WINDOW_X11_DISPLAY, c);
218 pa_xfree(c);
219 }
220 }
221
222 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID)) {
223 char *m;
224
225 if ((m = pa_machine_id())) {
226 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID, m);
227 pa_xfree(m);
228 }
229 }
230 }