summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/proplist-util.c
blob: eac8927cceba84c5904ccc04077d8874a17fc6f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/***
  This file is part of PulseAudio.

  Copyright 2008 Lennart Poettering

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <locale.h>
#include <dlfcn.h>

#ifdef __APPLE__
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#elif !HAVE_DECL_ENVIRON
extern char **environ;
#endif

#include <pulse/gccmacro.h>
#include <pulse/proplist.h>
#include <pulse/utf8.h>
#include <pulse/xmalloc.h>
#include <pulse/util.h>

#include <pulsecore/core-util.h>

#if defined(HAVE_GLIB) && defined(PA_GCC_WEAKREF)
#include <glib.h>
static G_CONST_RETURN gchar* _g_get_application_name(void) PA_GCC_WEAKREF(g_get_application_name);
#endif

#if defined(HAVE_GTK) && defined(PA_GCC_WEAKREF)
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
static G_CONST_RETURN gchar* _gtk_window_get_default_icon_name(void) PA_GCC_WEAKREF(gtk_window_get_default_icon_name);
static Display *_gdk_display PA_GCC_WEAKREF(gdk_display);
#endif

#include "proplist-util.h"

static void add_glib_properties(pa_proplist *p) {

#if defined(HAVE_GLIB) && defined(PA_GCC_WEAKREF)

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME))
        if (_g_get_application_name) {
            const gchar *t;

            /* We ignore the tiny race condition here. */

            if ((t = _g_get_application_name()))
                pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, t);
        }

#endif
}

static void add_gtk_properties(pa_proplist *p) {

#if defined(HAVE_GTK) && defined(PA_GCC_WEAKREF)

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_ICON_NAME))
        if (_gtk_window_get_default_icon_name) {
            const gchar *t;

            /* We ignore the tiny race condition here. */

            if ((t = _gtk_window_get_default_icon_name()))
                pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, t);
        }

    if (!pa_proplist_contains(p, PA_PROP_WINDOW_X11_DISPLAY))
        if (&_gdk_display && _gdk_display) {
            const char *t;

            /* We ignore the tiny race condition here. */

            if ((t = DisplayString(_gdk_display)))
                pa_proplist_sets(p, PA_PROP_WINDOW_X11_DISPLAY, t);
        }

#endif
}

void pa_init_proplist(pa_proplist *p) {
    char **e;
    const char *pp;

    pa_assert(p);

    if (environ) {

        /* Some applications seem to reset environ to NULL for various
         * reasons, hence we need to check for this explicitly. See
         * rhbz #473080 */

        for (e = environ; *e; e++) {

            if (pa_startswith(*e, "PULSE_PROP_")) {
                size_t kl, skip;
                char *k;
                pa_bool_t override;

                if (pa_startswith(*e, "PULSE_PROP_OVERRIDE_")) {
                    skip = 20;
                    override = TRUE;
                } else {
                    skip = 11;
                    override = FALSE;
                }

                kl = strcspn(*e+skip, "=");

                if ((*e)[skip+kl] != '=')
                    continue;

                k = pa_xstrndup(*e+skip, kl);

                if (!pa_streq(k, "OVERRIDE"))
                    if (override || !pa_proplist_contains(p, k))
                        pa_proplist_sets(p, k, *e+skip+kl+1);
                pa_xfree(k);
            }
        }
    }

    if ((pp = getenv("PULSE_PROP"))) {
        pa_proplist *t;

        if ((t = pa_proplist_from_string(pp))) {
            pa_proplist_update(p, PA_UPDATE_MERGE, t);
            pa_proplist_free(t);
        }
    }

    if ((pp = getenv("PULSE_PROP_OVERRIDE"))) {
        pa_proplist *t;

        if ((t = pa_proplist_from_string(pp))) {
            pa_proplist_update(p, PA_UPDATE_REPLACE, t);
            pa_proplist_free(t);
        }
    }

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_ID)) {
        char t[32];
        pa_snprintf(t, sizeof(t), "%lu", (unsigned long) getpid());
        pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_ID, t);
    }

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_USER)) {
        char t[64];
        if (pa_get_user_name(t, sizeof(t))) {
            char *c = pa_utf8_filter(t);
            pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_USER, c);
            pa_xfree(c);
        }
    }

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_HOST)) {
        char t[64];
        if (pa_get_host_name(t, sizeof(t))) {
            char *c = pa_utf8_filter(t);
            pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_HOST, c);
            pa_xfree(c);
        }
    }

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_BINARY)) {
        char t[PATH_MAX];
        if (pa_get_binary_name(t, sizeof(t))) {
            char *c = pa_utf8_filter(t);
            pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_BINARY, c);
            pa_xfree(c);
        }
    }

    add_glib_properties(p);
    add_gtk_properties(p);

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_NAME)) {
        const char *t;

        if ((t = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
            pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, t);
    }

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_LANGUAGE)) {
        const char *l;

        if ((l = setlocale(LC_MESSAGES, NULL)))
            pa_proplist_sets(p, PA_PROP_APPLICATION_LANGUAGE, l);
    }

    if (!pa_proplist_contains(p, PA_PROP_WINDOW_X11_DISPLAY)) {
        const char *t;

        if ((t = getenv("DISPLAY"))) {
            char *c = pa_utf8_filter(t);
            pa_proplist_sets(p, PA_PROP_WINDOW_X11_DISPLAY, c);
            pa_xfree(c);
        }
    }

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID)) {
        char *m;

        if ((m = pa_machine_id())) {
            pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_MACHINE_ID, m);
            pa_xfree(m);
        }
    }

    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_SESSION_ID)) {
        char *s;

        if ((s = pa_session_id())) {
            pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_SESSION_ID, s);
            pa_xfree(s);
        }
    }
}