summaryrefslogtreecommitdiff
path: root/src/pulsecore/namereg.c
blob: b2320c06d38bd3b07d0b4e172c134e00ef89a980 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/***
  This file is part of PulseAudio.

  Copyright 2004-2006 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
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/

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

#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <stdio.h>

#include <pulse/xmalloc.h>

#include <pulsecore/source.h>
#include <pulsecore/sink.h>
#include <pulsecore/core-subscribe.h>
#include <pulsecore/core-util.h>
#include <pulsecore/macro.h>

#include "namereg.h"

struct namereg_entry {
    pa_namereg_type_t type;
    char *name;
    void *data;
};

static bool is_valid_char(char c) {
    return
        (c >= 'a' && c <= 'z') ||
        (c >= 'A' && c <= 'Z') ||
        (c >= '0' && c <= '9') ||
        c == '.' ||
        c == '-' ||
        c == '_';
}

bool pa_namereg_is_valid_name(const char *name) {
    const char *c;

    pa_assert(name);

    if (*name == 0)
        return false;

    for (c = name; *c && (c-name < PA_NAME_MAX); c++)
        if (!is_valid_char(*c))
            return false;

    if (*c)
        return false;

    return true;
}

bool pa_namereg_is_valid_name_or_wildcard(const char *name, pa_namereg_type_t type) {

    pa_assert(name);

    if (pa_namereg_is_valid_name(name))
        return true;

    if (type == PA_NAMEREG_SINK &&
        pa_streq(name, "@DEFAULT_SINK@"))
        return true;

    if (type == PA_NAMEREG_SOURCE &&
        (pa_streq(name, "@DEFAULT_SOURCE@") ||
         pa_streq(name, "@DEFAULT_MONITOR@")))
        return true;

    return false;
}

char* pa_namereg_make_valid_name(const char *name) {
    const char *a;
    char *b, *n;

    if (*name == 0)
        return NULL;

    n = pa_xnew(char, strlen(name)+1);

    for (a = name, b = n; *a && (a-name < PA_NAME_MAX); a++, b++)
        *b = (char) (is_valid_char(*a) ? *a : '_');

    *b = 0;

    return n;
}

const char *pa_namereg_register(pa_core *c, const char *name, pa_namereg_type_t type, void *data, bool fail) {
    struct namereg_entry *e;
    char *n = NULL;

    pa_assert(c);
    pa_assert(name);
    pa_assert(data);

    if (!*name)
        return NULL;

    if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
        !pa_namereg_is_valid_name(name)) {

        if (fail)
            return NULL;

        if (!(name = n = pa_namereg_make_valid_name(name)))
            return NULL;
    }

    if ((e = pa_hashmap_get(c->namereg, name)) && fail) {
        pa_xfree(n);
        return NULL;
    }

    if (e) {
        unsigned i;
        size_t l = strlen(name);
        char *k;

        if (l+4 > PA_NAME_MAX) {
            pa_xfree(n);
            return NULL;
        }

        k = pa_xmalloc(l+4);

        for (i = 2; i <= 99; i++) {
            pa_snprintf(k, l+4, "%s.%u", name, i);

            if (!(e = pa_hashmap_get(c->namereg, k)))
                break;
        }

        if (e) {
            pa_xfree(n);
            pa_xfree(k);
            return NULL;
        }

        pa_xfree(n);
        n = k;
    }

    e = pa_xnew(struct namereg_entry, 1);
    e->type = type;
    e->name = n ? n : pa_xstrdup(name);
    e->data = data;

    pa_assert_se(pa_hashmap_put(c->namereg, e->name, e) >= 0);

    /* If a sink or source is registered and there was none registered
     * before we inform the clients which then can ask for the default
     * sink/source which is then assigned. We don't adjust the default
     * sink/source here right away to give the module the chance to
     * register more sinks/sources before we choose a new default
     * sink/source. */

    if ((!c->default_sink && type == PA_NAMEREG_SINK) ||
        (!c->default_source && type == PA_NAMEREG_SOURCE))
        pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);

    return e->name;
}

void pa_namereg_unregister(pa_core *c, const char *name) {
    struct namereg_entry *e;

    pa_assert(c);
    pa_assert(name);

    pa_assert_se(e = pa_hashmap_remove(c->namereg, name));

    if (c->default_sink == e->data)
        pa_namereg_set_default_sink(c, NULL);
    else if (c->default_source == e->data)
        pa_namereg_set_default_source(c, NULL);

    pa_xfree(e->name);
    pa_xfree(e);
}

void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
    struct namereg_entry *e;
    uint32_t idx;
    pa_assert(c);

    if (type == PA_NAMEREG_SOURCE && (!name || pa_streq(name, "@DEFAULT_SOURCE@"))) {
        pa_source *s;

        if ((s = pa_namereg_get_default_source(c)))
            return s;

    } else if (type == PA_NAMEREG_SINK && (!name || pa_streq(name, "@DEFAULT_SINK@"))) {
        pa_sink *s;

        if ((s = pa_namereg_get_default_sink(c)))
            return s;

    } else if (type == PA_NAMEREG_SOURCE && name && pa_streq(name, "@DEFAULT_MONITOR@")) {
        pa_sink *s;

        if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)))
            return s->monitor_source;
    }

    if (!name)
        return NULL;

    if ((type == PA_NAMEREG_SINK || type == PA_NAMEREG_SOURCE || type == PA_NAMEREG_CARD) &&
        !pa_namereg_is_valid_name(name))
        return NULL;

    if ((e = pa_hashmap_get(c->namereg, name)))
        if (e->type == type)
            return e->data;

    if (pa_atou(name, &idx) < 0)
        return NULL;

    if (type == PA_NAMEREG_SINK)
        return pa_idxset_get_by_index(c->sinks, idx);
    else if (type == PA_NAMEREG_SOURCE)
        return pa_idxset_get_by_index(c->sources, idx);
    else if (type == PA_NAMEREG_SAMPLE && c->scache)
        return pa_idxset_get_by_index(c->scache, idx);
    else if (type == PA_NAMEREG_CARD)
        return pa_idxset_get_by_index(c->cards, idx);

    return NULL;
}

pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
    pa_assert(c);

    if (s && !PA_SINK_IS_LINKED(pa_sink_get_state(s)))
        return NULL;

    if (c->default_sink != s) {
        c->default_sink = s;
        pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
    }

    return s;
}

pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
    pa_assert(c);

    if (s && !PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
        return NULL;

    if (c->default_source != s) {
        c->default_source = s;
        pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
    }

    return s;
}

pa_sink *pa_namereg_get_default_sink(pa_core *c) {
    pa_sink *s, *best = NULL;
    uint32_t idx;

    pa_assert(c);

    if (c->default_sink && PA_SINK_IS_LINKED(pa_sink_get_state(c->default_sink)))
        return c->default_sink;

    PA_IDXSET_FOREACH(s, c->sinks, idx)
        if (PA_SINK_IS_LINKED(pa_sink_get_state(s)))
            if (!best || s->priority > best->priority)
                best = s;

    return best;
}

pa_source *pa_namereg_get_default_source(pa_core *c) {
    pa_source *s, *best = NULL;
    uint32_t idx;

    pa_assert(c);

    if (c->default_source && PA_SOURCE_IS_LINKED(pa_source_get_state(c->default_source)))
        return c->default_source;

    /* First, try to find one that isn't a monitor */
    PA_IDXSET_FOREACH(s, c->sources, idx)
        if (!s->monitor_of && PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
            if (!best ||
                s->priority > best->priority)
                best = s;

    if (best)
        return best;

    /* Then, fallback to a monitor */
    PA_IDXSET_FOREACH(s, c->sources, idx)
        if (PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
            if (!best ||
                s->priority > best->priority ||
                (s->priority == best->priority &&
                 s->monitor_of &&
                 best->monitor_of &&
                 s->monitor_of->priority > best->monitor_of->priority))
                best = s;

    return best;
}