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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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/>.
***/
#ifndef PULSE_COMPAT_H
#define PULSE_COMPAT_H
#ifdef __cplusplus
extern "C" {
#else
#include <stdbool.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <spa/utils/string.h>
typedef struct pa_core pa_core;
typedef void *(*pa_copy_func_t)(const void *p);
typedef void (*pa_free_cb_t)(void *p);
#ifdef __GNUC__
#define PA_LIKELY(x) (__builtin_expect(!!(x),1))
#define PA_UNLIKELY(x) (__builtin_expect(!!(x),0))
#define PA_PRINTF_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
#else
#define PA_LIKELY(x) (x)
#define PA_UNLIKELY(x) (x)
#define PA_PRINTF_FUNC(fmt, arg1)
#endif
#define PA_MIN(a,b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
PA_LIKELY(_a < _b) ? _a : _b; \
})
#define PA_MAX(a,b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
PA_LIKELY(_a > _b) ? _a : _b; \
})
#define PA_CLAMP_UNLIKELY(v,low,high) \
({ \
__typeof__(v) _v = (v); \
__typeof__(low) _low = (low); \
__typeof__(high) _high = (high); \
PA_MIN(PA_MAX(_v, _low), _high); \
})
#define PA_PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
#define PA_UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
#include "array.h"
#include "llist.h"
#include "hashmap.h"
#include "dynarray.h"
#include "idxset.h"
#include "proplist.h"
typedef enum pa_direction {
PA_DIRECTION_OUTPUT = 0x0001U, /**< Output direction */
PA_DIRECTION_INPUT = 0x0002U /**< Input direction */
} pa_direction_t;
/* This enum replaces pa_port_available_t (defined in pulse/def.h) for
* internal use, so make sure both enum types stay in sync. */
typedef enum pa_available {
PA_AVAILABLE_UNKNOWN = 0,
PA_AVAILABLE_NO = 1,
PA_AVAILABLE_YES = 2,
} pa_available_t;
#define PA_RATE_MAX (48000U*8U)
typedef enum pa_sample_format {
PA_SAMPLE_U8, /**< Unsigned 8 Bit PCM */
PA_SAMPLE_ALAW, /**< 8 Bit a-Law */
PA_SAMPLE_ULAW, /**< 8 Bit mu-Law */
PA_SAMPLE_S16LE, /**< Signed 16 Bit PCM, little endian (PC) */
PA_SAMPLE_S16BE, /**< Signed 16 Bit PCM, big endian */
PA_SAMPLE_FLOAT32LE, /**< 32 Bit IEEE floating point, little endian (PC), range -1.0 to 1.0 */
PA_SAMPLE_FLOAT32BE, /**< 32 Bit IEEE floating point, big endian, range -1.0 to 1.0 */
PA_SAMPLE_S32LE, /**< Signed 32 Bit PCM, little endian (PC) */
PA_SAMPLE_S32BE, /**< Signed 32 Bit PCM, big endian */
PA_SAMPLE_S24LE, /**< Signed 24 Bit PCM packed, little endian (PC). \since 0.9.15 */
PA_SAMPLE_S24BE, /**< Signed 24 Bit PCM packed, big endian. \since 0.9.15 */
PA_SAMPLE_S24_32LE, /**< Signed 24 Bit PCM in LSB of 32 Bit words, little endian (PC). \since 0.9.15 */
PA_SAMPLE_S24_32BE, /**< Signed 24 Bit PCM in LSB of 32 Bit words, big endian. \since 0.9.15 */
PA_SAMPLE_MAX, /**< Upper limit of valid sample types */
PA_SAMPLE_INVALID = -1 /**< An invalid value */
} pa_sample_format_t;
static inline int pa_sample_format_valid(unsigned format)
{
return format < PA_SAMPLE_MAX;
}
#ifdef WORDS_BIGENDIAN
#define PA_SAMPLE_S16NE PA_SAMPLE_S16BE
#define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32BE
#define PA_SAMPLE_S32NE PA_SAMPLE_S32BE
#define PA_SAMPLE_S24NE PA_SAMPLE_S24BE
#define PA_SAMPLE_S24_32NE PA_SAMPLE_S24_32BE
#define PA_SAMPLE_S16RE PA_SAMPLE_S16LE
#define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32LE
#define PA_SAMPLE_S32RE PA_SAMPLE_S32LE
#define PA_SAMPLE_S24RE PA_SAMPLE_S24LE
#define PA_SAMPLE_S24_32RE PA_SAMPLE_S24_32LE
#else
#define PA_SAMPLE_S16NE PA_SAMPLE_S16LE
#define PA_SAMPLE_FLOAT32NE PA_SAMPLE_FLOAT32LE
#define PA_SAMPLE_S32NE PA_SAMPLE_S32LE
#define PA_SAMPLE_S24NE PA_SAMPLE_S24LE
#define PA_SAMPLE_S24_32NE PA_SAMPLE_S24_32LE
#define PA_SAMPLE_S16RE PA_SAMPLE_S16BE
#define PA_SAMPLE_FLOAT32RE PA_SAMPLE_FLOAT32BE
#define PA_SAMPLE_S32RE PA_SAMPLE_S32BE
#define PA_SAMPLE_S24RE PA_SAMPLE_S24BE
#define PA_SAMPLE_S24_32RE PA_SAMPLE_S24_32BE
#endif
static const size_t pa_sample_size_table[] = {
[PA_SAMPLE_U8] = 1,
[PA_SAMPLE_ULAW] = 1,
[PA_SAMPLE_ALAW] = 1,
[PA_SAMPLE_S16LE] = 2,
[PA_SAMPLE_S16BE] = 2,
[PA_SAMPLE_FLOAT32LE] = 4,
[PA_SAMPLE_FLOAT32BE] = 4,
[PA_SAMPLE_S32LE] = 4,
[PA_SAMPLE_S32BE] = 4,
[PA_SAMPLE_S24LE] = 3,
[PA_SAMPLE_S24BE] = 3,
[PA_SAMPLE_S24_32LE] = 4,
[PA_SAMPLE_S24_32BE] = 4
};
static inline const char *pa_sample_format_to_string(pa_sample_format_t f)
{
static const char* const table[]= {
[PA_SAMPLE_U8] = "u8",
[PA_SAMPLE_ALAW] = "aLaw",
[PA_SAMPLE_ULAW] = "uLaw",
[PA_SAMPLE_S16LE] = "s16le",
[PA_SAMPLE_S16BE] = "s16be",
[PA_SAMPLE_FLOAT32LE] = "float32le",
[PA_SAMPLE_FLOAT32BE] = "float32be",
[PA_SAMPLE_S32LE] = "s32le",
[PA_SAMPLE_S32BE] = "s32be",
[PA_SAMPLE_S24LE] = "s24le",
[PA_SAMPLE_S24BE] = "s24be",
[PA_SAMPLE_S24_32LE] = "s24-32le",
[PA_SAMPLE_S24_32BE] = "s24-32be",
};
if (!pa_sample_format_valid(f))
return NULL;
return table[f];
}
typedef struct pa_sample_spec {
pa_sample_format_t format;
uint32_t rate;
uint8_t channels;
} pa_sample_spec;
typedef uint64_t pa_usec_t;
#define PA_MSEC_PER_SEC ((pa_usec_t) 1000ULL)
#define PA_USEC_PER_SEC ((pa_usec_t) 1000000ULL)
#define PA_USEC_PER_MSEC ((pa_usec_t) 1000ULL)
static inline size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) {
return (size_t) (((t * spec->rate) / PA_USEC_PER_SEC)) *
(pa_sample_size_table[spec->format] * spec->channels);
}
static inline int pa_sample_rate_valid(uint32_t rate) {
return rate > 0 && rate <= PA_RATE_MAX * 101 / 100;
}
static inline size_t pa_frame_size(const pa_sample_spec *spec) {
return pa_sample_size_table[spec->format] * spec->channels;
}
typedef enum pa_log_level {
PA_LOG_ERROR = 0, /* Error messages */
PA_LOG_WARN = 1, /* Warning messages */
PA_LOG_NOTICE = 2, /* Notice messages */
PA_LOG_INFO = 3, /* Info messages */
PA_LOG_DEBUG = 4, /* Debug messages */
PA_LOG_LEVEL_MAX
} pa_log_level_t;
extern int _acp_log_level;
extern acp_log_func _acp_log_func;
extern void * _acp_log_data;
#define pa_log_level_enabled(lev) (_acp_log_level >= (int)(lev))
#define pa_log_levelv_meta(lev,f,l,func,fmt,ap) \
({ \
if (pa_log_level_enabled (lev) && _acp_log_func) \
_acp_log_func(_acp_log_data,lev,f,l,func,fmt,ap); \
})
static inline PA_PRINTF_FUNC(5, 6) void pa_log_level_meta(enum pa_log_level level,
const char *file, int line, const char *func,
const char *fmt, ...)
{
va_list args;
va_start(args,fmt);
pa_log_levelv_meta(level,file,line,func,fmt,args);
va_end(args);
}
#define pa_logl(lev,fmt,...) pa_log_level_meta(lev,__FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__)
#define pa_log_error(fmt,...) pa_logl(PA_LOG_ERROR, fmt, ##__VA_ARGS__)
#define pa_log_warn(fmt,...) pa_logl(PA_LOG_WARN, fmt, ##__VA_ARGS__)
#define pa_log_notice(fmt,...) pa_logl(PA_LOG_NOTICE, fmt, ##__VA_ARGS__)
#define pa_log_info(fmt,...) pa_logl(PA_LOG_INFO, fmt, ##__VA_ARGS__)
#define pa_log_debug(fmt,...) pa_logl(PA_LOG_DEBUG, fmt, ##__VA_ARGS__)
#define pa_log pa_log_error
#define pa_assert_se(expr) \
do { \
if (PA_UNLIKELY(!(expr))) { \
fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
#expr , __FILE__, __LINE__, __func__); \
abort(); \
} \
} while (false)
#define pa_assert(expr) \
do { \
if (PA_UNLIKELY(!(expr))) { \
fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
#expr , __FILE__, __LINE__, __func__); \
abort(); \
} \
} while (false)
#define pa_assert_not_reached() \
do { \
fprintf(stderr, "Code should not be reached at %s:%u %s()\n", \
__FILE__, __LINE__, __func__); \
abort(); \
} while (false)
#define pa_memzero(x,l) (memset((x), 0, (l)))
#define pa_zero(x) (pa_memzero(&(x), sizeof(x)))
#define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
#define pa_streq(a,b) (!strcmp((a),(b)))
#define pa_strneq(a,b,n) (!strncmp((a),(b),(n)))
#define pa_strnull(s) ((s) ? (s) : "null")
#define pa_startswith(s,pfx) (strstr(s, pfx) == s)
PA_PRINTF_FUNC(3, 0)
static inline size_t pa_vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
int ret;
pa_assert(str);
pa_assert(size > 0);
pa_assert(format);
ret = vsnprintf(str, size, format, ap);
str[size-1] = 0;
if (ret < 0)
return strlen(str);
if ((size_t) ret > size-1)
return size-1;
return (size_t) ret;
}
PA_PRINTF_FUNC(3, 4)
static inline size_t pa_snprintf(char *str, size_t size, const char *format, ...)
{
size_t ret;
va_list ap;
pa_assert(str);
pa_assert(size > 0);
pa_assert(format);
va_start(ap, format);
ret = pa_vsnprintf(str, size, format, ap);
va_end(ap);
return ret;
}
#define pa_xstrdup(s) ((s) != NULL ? strdup(s) : NULL)
#define pa_xstrndup(s,n) ((s) != NULL ? strndup(s,n) : NULL)
#define pa_xfree free
#define pa_xmalloc malloc
#define pa_xnew0(t,n) calloc(n, sizeof(t))
#define pa_xnew(t,n) pa_xnew0(t,n)
#define pa_xrealloc realloc
#define pa_xrenew(t,p,n) ((t*) realloc(p, (n)*sizeof(t)))
static inline void* pa_xmemdup(const void *p, size_t l) {
return memcpy(malloc(l), p, l);
}
#define pa_xnewdup(t,p,n) ((t*) pa_xmemdup((p), (n)*sizeof(t)))
static inline void pa_xfreev(void**a)
{
int i;
for (i = 0; a && a[i]; i++)
free(a[i]);
free(a);
}
static inline void pa_xstrfreev(char **a) {
pa_xfreev((void**)a);
}
#define pa_cstrerror strerror
#define PA_PATH_SEP "/"
#define PA_PATH_SEP_CHAR '/'
#define PA_WHITESPACE "\n\r \t"
static PA_PRINTF_FUNC(1,2) inline char *pa_sprintf_malloc(const char *fmt, ...)
{
char *res;
va_list args;
va_start(args, fmt);
if (vasprintf(&res, fmt, args) < 0)
res = NULL;
va_end(args);
return res;
}
#define pa_fopen_cloexec(f,m) fopen(f,m"e")
static inline char *pa_path_get_filename(const char *p)
{
char *fn;
if (!p)
return NULL;
if ((fn = strrchr(p, PA_PATH_SEP_CHAR)))
return fn+1;
return (char*) p;
}
static inline bool pa_is_path_absolute(const char *fn)
{
return *fn == PA_PATH_SEP_CHAR;
}
static inline char* pa_maybe_prefix_path(const char *path, const char *prefix)
{
if (pa_is_path_absolute(path))
return pa_xstrdup(path);
return pa_sprintf_malloc("%s" PA_PATH_SEP "%s", prefix, path);
}
static inline bool pa_endswith(const char *s, const char *sfx)
{
size_t l1, l2;
l1 = strlen(s);
l2 = strlen(sfx);
return l1 >= l2 && pa_streq(s + l1 - l2, sfx);
}
static inline char *pa_replace(const char*s, const char*a, const char *b)
{
struct pa_array res;
size_t an, bn;
an = strlen(a);
bn = strlen(b);
pa_array_init(&res, an);
for (;;) {
const char *p;
if (!(p = strstr(s, a)))
break;
pa_array_add_data(&res, s, p-s);
pa_array_add_data(&res, b, bn);
s = p + an;
}
pa_array_add_data(&res, s, strlen(s) + 1);
return res.data;
}
static inline char *pa_split(const char *c, const char *delimiter, const char**state)
{
const char *current = *state ? *state : c;
size_t l;
if (!*current)
return NULL;
l = strcspn(current, delimiter);
*state = current+l;
if (**state)
(*state)++;
return pa_xstrndup(current, l);
}
static inline char *pa_split_spaces(const char *c, const char **state)
{
const char *current = *state ? *state : c;
size_t l;
if (!*current || *c == 0)
return NULL;
current += strspn(current, PA_WHITESPACE);
l = strcspn(current, PA_WHITESPACE);
*state = current+l;
return pa_xstrndup(current, l);
}
static inline char **pa_split_spaces_strv(const char *s)
{
char **t, *e;
unsigned i = 0, n = 8;
const char *state = NULL;
t = pa_xnew(char*, n);
while ((e = pa_split_spaces(s, &state))) {
t[i++] = e;
if (i >= n) {
n *= 2;
t = pa_xrenew(char*, t, n);
}
}
if (i <= 0) {
pa_xfree(t);
return NULL;
}
t[i] = NULL;
return t;
}
static inline char* pa_str_strip_suffix(const char *str, const char *suffix)
{
size_t str_l, suf_l, prefix;
char *ret;
str_l = strlen(str);
suf_l = strlen(suffix);
if (str_l < suf_l)
return NULL;
prefix = str_l - suf_l;
if (!pa_streq(&str[prefix], suffix))
return NULL;
ret = pa_xmalloc(prefix + 1);
memcpy(ret, str, prefix);
ret[prefix] = '\0';
return ret;
}
static inline const char *pa_split_in_place(const char *c, const char *delimiter, size_t *n, const char**state)
{
const char *current = *state ? *state : c;
size_t l;
if (!*current)
return NULL;
l = strcspn(current, delimiter);
*state = current+l;
if (**state)
(*state)++;
*n = l;
return current;
}
static inline const char *pa_split_spaces_in_place(const char *c, size_t *n, const char **state)
{
const char *current = *state ? *state : c;
size_t l;
if (!*current || *c == 0)
return NULL;
current += strspn(current, PA_WHITESPACE);
l = strcspn(current, PA_WHITESPACE);
*state = current+l;
*n = l;
return current;
}
static inline bool pa_str_in_list_spaces(const char *haystack, const char *needle)
{
const char *s;
size_t n;
const char *state = NULL;
if (!haystack || !needle)
return false;
while ((s = pa_split_spaces_in_place(haystack, &n, &state))) {
if (pa_strneq(needle, s, n))
return true;
}
return false;
}
static inline char *pa_strip(char *s)
{
char *e, *l = NULL;
s += strspn(s, PA_WHITESPACE);
for (e = s; *e; e++)
if (!strchr(PA_WHITESPACE, *e))
l = e;
if (l)
*(l+1) = 0;
else
*s = 0;
return s;
}
static inline int pa_atod(const char *s, double *ret_d)
{
if (spa_atod(s, ret_d) && !isnan(*ret_d))
return 0;
errno = EINVAL;
return -1;
}
static inline int pa_atoi(const char *s, int32_t *ret_i)
{
if (spa_atoi32(s, ret_i, 0))
return 0;
errno = EINVAL;
return -1;
}
static inline int pa_atou(const char *s, uint32_t *ret_u)
{
if (spa_atou32(s, ret_u, 0))
return 0;
errno = EINVAL;
return -1;
}
static inline int pa_atol(const char *s, long *ret_l)
{
int64_t res;
if (spa_atoi64(s, &res, 0)) {
*ret_l = res;
if (*ret_l == res)
return 0;
}
errno = EINVAL;
return -1;
}
static inline int pa_parse_boolean(const char *v)
{
if (pa_streq(v, "1") || !strcasecmp(v, "y") || !strcasecmp(v, "t")
|| !strcasecmp(v, "yes") || !strcasecmp(v, "true") || !strcasecmp(v, "on"))
return 1;
else if (pa_streq(v, "0") || !strcasecmp(v, "n") || !strcasecmp(v, "f")
|| !strcasecmp(v, "no") || !strcasecmp(v, "false") || !strcasecmp(v, "off"))
return 0;
errno = EINVAL;
return -1;
}
static inline const char *pa_yes_no(bool b) {
return b ? "yes" : "no";
}
static inline const char *pa_strna(const char *x) {
return x ? x : "n/a";
}
static inline pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec)
{
spec->format = PA_SAMPLE_INVALID;
spec->rate = 0;
spec->channels = 0;
return spec;
}
static inline char *pa_readlink(const char *p) {
#ifdef HAVE_READLINK
size_t l = 100;
for (;;) {
char *c;
ssize_t n;
c = pa_xmalloc(l);
if ((n = readlink(p, c, l-1)) < 0) {
pa_xfree(c);
return NULL;
}
if ((size_t) n < l-1) {
c[n] = 0;
return c;
}
pa_xfree(c);
l *= 2;
}
#else
return NULL;
#endif
}
#include <spa/support/i18n.h>
extern struct spa_i18n *acp_i18n;
#define _(String) spa_i18n_text(acp_i18n, String)
#ifdef gettext_noop
#define N_(String) gettext_noop(String)
#else
#define N_(String) (String)
#endif
#include "channelmap.h"
#include "volume.h"
#ifdef __cplusplus
}
#endif
#endif /* PULSE_COMPAT_H */
|