]> code.delx.au - pulseaudio/blob - src/pulsecore/proplist-util.c
add new property PA_PROP_APPLICATION_PROCESS_SESSION_ID and initialize it by default
[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 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
52 #include <gtk/gtk.h>
53 #include <gdk/gdkx.h>
54 static G_CONST_RETURN gchar* _gtk_window_get_default_icon_name(void) PA_GCC_WEAKREF(gtk_window_get_default_icon_name);
55 static Display *_gdk_display PA_GCC_WEAKREF(gdk_display);
56 #endif
57
58 #include "proplist-util.h"
59
60 static void add_glib_properties(pa_proplist *p) {
61
62 #if defined(HAVE_GLIB) && defined(PA_GCC_WEAKREF)
63
64 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME))
65 if (_g_get_application_name) {
66 const gchar *t;
67
68 /* We ignore the tiny race condition here. */
69
70 if ((t = _g_get_application_name()))
71 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, t);
72 }
73
74 #endif
75 }
76
77 static void add_gtk_properties(pa_proplist *p) {
78
79 #if defined(HAVE_GTK) && defined(PA_GCC_WEAKREF)
80
81 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_ICON_NAME))
82 if (_gtk_window_get_default_icon_name) {
83 const gchar *t;
84
85 /* We ignore the tiny race condition here. */
86
87 if ((t = _gtk_window_get_default_icon_name()))
88 pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, t);
89 }
90
91 if (!pa_proplist_contains(p, PA_PROP_WINDOW_X11_DISPLAY))
92 if (&_gdk_display && _gdk_display) {
93 const char *t;
94
95 /* We ignore the tiny race condition here. */
96
97 if ((t = DisplayString(_gdk_display)))
98 pa_proplist_sets(p, PA_PROP_WINDOW_X11_DISPLAY, t);
99 }
100
101 #endif
102 }
103
104 void pa_init_proplist(pa_proplist *p) {
105 char **e;
106 const char *pp;
107
108 pa_assert(p);
109
110 if (environ) {
111
112 /* Some applications seem to reset environ to NULL for various
113 * reasons, hence we need to check for this explicitly. See
114 * rhbz #473080 */
115
116 for (e = environ; *e; e++) {
117
118 if (pa_startswith(*e, "PULSE_PROP_")) {
119 size_t kl, skip;
120 char *k;
121 pa_bool_t override;
122
123 if (pa_startswith(*e, "PULSE_PROP_OVERRIDE_")) {
124 skip = 20;
125 override = TRUE;
126 } else {
127 skip = 11;
128 override = FALSE;
129 }
130
131 kl = strcspn(*e+skip, "=");
132
133 if ((*e)[skip+kl] != '=')
134 continue;
135
136 k = pa_xstrndup(*e+skip, kl);
137
138 if (override || !pa_proplist_contains(p, k))
139 pa_proplist_sets(p, k, *e+skip+kl+1);
140 pa_xfree(k);
141 }
142 }
143 }
144
145 if ((pp = getenv("PULSE_PROP"))) {
146 pa_proplist *t;
147
148 if ((t = pa_proplist_from_string(pp))) {
149 pa_proplist_update(p, PA_UPDATE_MERGE, t);
150 pa_proplist_free(t);
151 }
152 }
153
154 if ((pp = getenv("PULSE_PROP_OVERRIDE"))) {
155 pa_proplist *t;
156
157 if ((t = pa_proplist_from_string(pp))) {
158 pa_proplist_update(p, PA_UPDATE_REPLACE, t);
159 pa_proplist_free(t);
160 }
161 }
162
163 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_ID)) {
164 char t[32];
165 pa_snprintf(t, sizeof(t), "%lu", (unsigned long) getpid());
166 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_ID, t);
167 }
168
169 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_USER)) {
170 char t[64];
171 if (pa_get_user_name(t, sizeof(t))) {
172 char *c = pa_utf8_filter(t);
173 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_USER, c);
174 pa_xfree(c);
175 }
176 }
177
178 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_HOST)) {
179 char t[64];
180 if (pa_get_host_name(t, sizeof(t))) {
181 char *c = pa_utf8_filter(t);
182 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_HOST, c);
183 pa_xfree(c);
184 }
185 }
186
187 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_BINARY)) {
188 char t[PATH_MAX];
189 if (pa_get_binary_name(t, sizeof(t))) {
190 char *c = pa_utf8_filter(t);
191 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_BINARY, c);
192 pa_xfree(c);
193 }
194 }
195
196 add_glib_properties(p);
197 add_gtk_properties(p);
198
199 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME)) {
200 const char *t;
201
202 if ((t = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
203 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, t);
204 }
205
206 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_LANGUAGE)) {
207 const char *l;
208
209 if ((l = setlocale(LC_MESSAGES, NULL)))
210 pa_proplist_sets(p, PA_PROP_APPLICATION_LANGUAGE, l);
211 }
212
213 if (!pa_proplist_contains(p, PA_PROP_WINDOW_X11_DISPLAY)) {
214 const char *t;
215
216 if ((t = getenv("DISPLAY"))) {
217 char *c = pa_utf8_filter(t);
218 pa_proplist_sets(p, PA_PROP_WINDOW_X11_DISPLAY, c);
219 pa_xfree(c);
220 }
221 }
222
223 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID)) {
224 char *m;
225
226 if ((m = pa_machine_id())) {
227 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID, m);
228 pa_xfree(m);
229 }
230 }
231
232 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_SESSION_ID)) {
233 const char *t;
234
235 if ((t = getenv("XDG_SESSION_COOKIE"))) {
236 char *c = pa_utf8_filter(t);
237 pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_SESSION_ID, c);
238 pa_xfree(c);
239 }
240 }
241 }