Merge commit 'elmarco/bt-wip'
[profile/ivi/pulseaudio-panda.git] / 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/proplist.h>
38 #include <pulse/utf8.h>
39 #include <pulse/xmalloc.h>
40 #include <pulse/util.h>
41
42 #include <pulsecore/core-util.h>
43
44 #include "proplist-util.h"
45
46 void pa_init_proplist(pa_proplist *p) {
47     char **e;
48     const char *pp;
49
50     pa_assert(p);
51
52     if (environ) {
53
54         /* Some applications seem to reset environ to NULL for various
55          * reasons, hence we need to check for this explicitly. See
56          * rhbz #473080 */
57
58         for (e = environ; *e; e++) {
59
60             if (pa_startswith(*e, "PULSE_PROP_")) {
61                 size_t kl = strcspn(*e+11, "=");
62                 char *k;
63
64                 if ((*e)[11+kl] != '=')
65                     continue;
66
67                 if (!pa_utf8_valid(*e+11+kl+1))
68                     continue;
69
70                 k = pa_xstrndup(*e+11, kl);
71
72                 pa_proplist_sets(p, k, *e+11+kl+1);
73                 pa_xfree(k);
74             }
75         }
76     }
77
78     if ((pp = getenv("PULSE_PROP"))) {
79         pa_proplist *t;
80
81         if ((t = pa_proplist_from_string(pp))) {
82             pa_proplist_update(p, PA_UPDATE_REPLACE, t);
83             pa_proplist_free(t);
84         }
85     }
86
87     if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_ID)) {
88         char t[32];
89         pa_snprintf(t, sizeof(t), "%lu", (unsigned long) getpid());
90         pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_ID, t);
91     }
92
93     if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_USER)) {
94         char t[64];
95         if (pa_get_user_name(t, sizeof(t))) {
96             char *c = pa_utf8_filter(t);
97             pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_USER, c);
98             pa_xfree(c);
99         }
100     }
101
102     if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_HOST)) {
103         char t[64];
104         if (pa_get_host_name(t, sizeof(t))) {
105             char *c = pa_utf8_filter(t);
106             pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_HOST, c);
107             pa_xfree(c);
108         }
109     }
110
111     if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_BINARY)) {
112         char t[PATH_MAX];
113         if (pa_get_binary_name(t, sizeof(t))) {
114             char *c = pa_utf8_filter(t);
115             pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_BINARY, c);
116             pa_xfree(c);
117         }
118     }
119
120 #ifdef RTLD_NOLOAD
121     if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME)) {
122         void *dl;
123
124         if ((dl = dlopen("libglib-2.0", RTLD_NOLOAD))) {
125             const char *(*_g_get_application_name)(void);
126
127             if ((*(void**) &_g_get_application_name = dlsym(dl, "g_get_application_name")))
128                 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, _g_get_application_name());
129
130             dlclose(dl);
131         }
132     }
133 #endif
134
135     if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME)) {
136         const char *t;
137
138         if ((t = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
139             pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, t);
140     }
141
142     if (!pa_proplist_contains(p, PA_PROP_APPLICATION_LANGUAGE)) {
143         const char *l;
144
145         if ((l = setlocale(LC_MESSAGES, NULL)))
146             pa_proplist_sets(p, PA_PROP_APPLICATION_LANGUAGE, l);
147     }
148
149     if (!pa_proplist_contains(p, PA_PROP_WINDOW_X11_DISPLAY)) {
150         const char *t;
151
152         if ((t = getenv("DISPLAY"))) {
153             char *c = pa_utf8_filter(t);
154             pa_proplist_sets(p, PA_PROP_WINDOW_X11_DISPLAY, c);
155             pa_xfree(c);
156         }
157     }
158
159     if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID)) {
160         char *m;
161
162         if ((m = pa_machine_id())) {
163             pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID, m);
164             pa_xfree(m);
165         }
166     }
167 }