2 This file is part of PulseAudio.
4 Copyright 2004-2009 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/types.h>
28 #include <asoundlib.h>
31 #ifdef HAVE_VALGRIND_MEMCHECK_H
32 #include <valgrind/memcheck.h>
35 #include <pulse/mainloop-api.h>
36 #include <pulse/sample.h>
37 #include <pulse/timeval.h>
38 #include <pulse/util.h>
39 #include <pulse/volume.h>
40 #include <pulse/xmalloc.h>
41 #include <pulse/utf8.h>
43 #include <pulsecore/i18n.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/macro.h>
46 #include <pulsecore/core-util.h>
47 #include <pulsecore/conf-parser.h>
48 #include <pulsecore/strbuf.h>
50 #include "alsa-mixer.h"
51 #include "alsa-util.h"
53 static int setting_select(pa_alsa_setting
*s
, snd_mixer_t
*m
);
55 struct description_map
{
57 const char *description
;
60 static const char *lookup_description(const char *name
, const struct description_map dm
[], unsigned n
) {
63 for (i
= 0; i
< n
; i
++)
64 if (pa_streq(dm
[i
].name
, name
))
65 return _(dm
[i
].description
);
70 struct pa_alsa_fdlist
{
73 /* This is a temporary buffer used to avoid lots of mallocs */
74 struct pollfd
*work_fds
;
80 pa_defer_event
*defer
;
85 void (*cb
)(void *userdata
);
89 static void io_cb(pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t events
, void *userdata
) {
91 struct pa_alsa_fdlist
*fdl
= userdata
;
94 unsigned short revents
;
98 pa_assert(fdl
->mixer
|| fdl
->hctl
);
100 pa_assert(fdl
->work_fds
);
107 memcpy(fdl
->work_fds
, fdl
->fds
, sizeof(struct pollfd
) * fdl
->num_fds
);
109 for (i
= 0; i
< fdl
->num_fds
; i
++) {
110 if (e
== fdl
->ios
[i
]) {
111 if (events
& PA_IO_EVENT_INPUT
)
112 fdl
->work_fds
[i
].revents
|= POLLIN
;
113 if (events
& PA_IO_EVENT_OUTPUT
)
114 fdl
->work_fds
[i
].revents
|= POLLOUT
;
115 if (events
& PA_IO_EVENT_ERROR
)
116 fdl
->work_fds
[i
].revents
|= POLLERR
;
117 if (events
& PA_IO_EVENT_HANGUP
)
118 fdl
->work_fds
[i
].revents
|= POLLHUP
;
123 pa_assert(i
!= fdl
->num_fds
);
126 err
= snd_hctl_poll_descriptors_revents(fdl
->hctl
, fdl
->work_fds
, fdl
->num_fds
, &revents
);
128 err
= snd_mixer_poll_descriptors_revents(fdl
->mixer
, fdl
->work_fds
, fdl
->num_fds
, &revents
);
131 pa_log_error("Unable to get poll revent: %s", pa_alsa_strerror(err
));
135 a
->defer_enable(fdl
->defer
, 1);
139 snd_hctl_handle_events(fdl
->hctl
);
141 snd_mixer_handle_events(fdl
->mixer
);
145 static void defer_cb(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
146 struct pa_alsa_fdlist
*fdl
= userdata
;
153 pa_assert(fdl
->mixer
|| fdl
->hctl
);
155 a
->defer_enable(fdl
->defer
, 0);
158 n
= snd_hctl_poll_descriptors_count(fdl
->hctl
);
160 n
= snd_mixer_poll_descriptors_count(fdl
->mixer
);
163 pa_log("snd_mixer_poll_descriptors_count() failed: %s", pa_alsa_strerror(n
));
166 num_fds
= (unsigned) n
;
168 if (num_fds
!= fdl
->num_fds
) {
172 pa_xfree(fdl
->work_fds
);
173 fdl
->fds
= pa_xnew0(struct pollfd
, num_fds
);
174 fdl
->work_fds
= pa_xnew(struct pollfd
, num_fds
);
177 memset(fdl
->work_fds
, 0, sizeof(struct pollfd
) * num_fds
);
180 err
= snd_hctl_poll_descriptors(fdl
->hctl
, fdl
->work_fds
, num_fds
);
182 err
= snd_mixer_poll_descriptors(fdl
->mixer
, fdl
->work_fds
, num_fds
);
185 pa_log_error("Unable to get poll descriptors: %s", pa_alsa_strerror(err
));
191 if (memcmp(fdl
->fds
, fdl
->work_fds
, sizeof(struct pollfd
) * num_fds
) == 0)
195 for (i
= 0; i
< fdl
->num_fds
; i
++)
196 a
->io_free(fdl
->ios
[i
]);
198 if (num_fds
!= fdl
->num_fds
) {
205 fdl
->ios
= pa_xnew(pa_io_event
*, num_fds
);
208 temp
= fdl
->work_fds
;
209 fdl
->work_fds
= fdl
->fds
;
212 fdl
->num_fds
= num_fds
;
214 for (i
= 0;i
< num_fds
;i
++)
215 fdl
->ios
[i
] = a
->io_new(a
, fdl
->fds
[i
].fd
,
216 ((fdl
->fds
[i
].events
& POLLIN
) ? PA_IO_EVENT_INPUT
: 0) |
217 ((fdl
->fds
[i
].events
& POLLOUT
) ? PA_IO_EVENT_OUTPUT
: 0),
221 struct pa_alsa_fdlist
*pa_alsa_fdlist_new(void) {
222 struct pa_alsa_fdlist
*fdl
;
224 fdl
= pa_xnew0(struct pa_alsa_fdlist
, 1);
229 void pa_alsa_fdlist_free(struct pa_alsa_fdlist
*fdl
) {
234 fdl
->m
->defer_free(fdl
->defer
);
240 for (i
= 0; i
< fdl
->num_fds
; i
++)
241 fdl
->m
->io_free(fdl
->ios
[i
]);
248 pa_xfree(fdl
->work_fds
);
253 /* We can listen to either a snd_hctl_t or a snd_mixer_t, but not both */
254 int pa_alsa_fdlist_set_handle(struct pa_alsa_fdlist
*fdl
, snd_mixer_t
*mixer_handle
, snd_hctl_t
*hctl_handle
, pa_mainloop_api
*m
) {
256 pa_assert(hctl_handle
|| mixer_handle
);
257 pa_assert(!(hctl_handle
&& mixer_handle
));
261 fdl
->hctl
= hctl_handle
;
262 fdl
->mixer
= mixer_handle
;
264 fdl
->defer
= m
->defer_new(m
, defer_cb
, fdl
);
269 struct pa_alsa_mixer_pdata
{
271 pa_rtpoll_item
*poll_item
;
276 struct pa_alsa_mixer_pdata
*pa_alsa_mixer_pdata_new(void) {
277 struct pa_alsa_mixer_pdata
*pd
;
279 pd
= pa_xnew0(struct pa_alsa_mixer_pdata
, 1);
284 void pa_alsa_mixer_pdata_free(struct pa_alsa_mixer_pdata
*pd
) {
288 pa_rtpoll_item_free(pd
->poll_item
);
294 static int rtpoll_work_cb(pa_rtpoll_item
*i
) {
295 struct pa_alsa_mixer_pdata
*pd
;
298 unsigned short revents
= 0;
301 pd
= pa_rtpoll_item_get_userdata(i
);
303 pa_assert_fp(i
== pd
->poll_item
);
305 p
= pa_rtpoll_item_get_pollfd(i
, &n_fds
);
307 if ((err
= snd_mixer_poll_descriptors_revents(pd
->mixer
, p
, n_fds
, &revents
)) < 0) {
308 pa_log_error("Unable to get poll revent: %s", pa_alsa_strerror(err
));
314 if (revents
& (POLLNVAL
| POLLERR
)) {
315 pa_log_debug("Device disconnected, stopping poll on mixer");
317 } else if (revents
& POLLERR
) {
318 /* This shouldn't happen. */
319 pa_log_error("Got a POLLERR (revents = %04x), stopping poll on mixer", revents
);
323 err
= snd_mixer_handle_events(pd
->mixer
);
325 if (PA_LIKELY(err
>= 0)) {
326 pa_rtpoll_item_free(i
);
327 pa_alsa_set_mixer_rtpoll(pd
, pd
->mixer
, pd
->rtpoll
);
329 pa_log_error("Error handling mixer event: %s", pa_alsa_strerror(err
));
338 pa_rtpoll_item_free(i
);
340 pd
->poll_item
= NULL
;
347 int pa_alsa_set_mixer_rtpoll(struct pa_alsa_mixer_pdata
*pd
, snd_mixer_t
*mixer
, pa_rtpoll
*rtp
) {
356 if ((n
= snd_mixer_poll_descriptors_count(mixer
)) < 0) {
357 pa_log("snd_mixer_poll_descriptors_count() failed: %s", pa_alsa_strerror(n
));
361 i
= pa_rtpoll_item_new(rtp
, PA_RTPOLL_LATE
, (unsigned) n
);
363 p
= pa_rtpoll_item_get_pollfd(i
, NULL
);
365 memset(p
, 0, sizeof(struct pollfd
) * n
);
367 if ((err
= snd_mixer_poll_descriptors(mixer
, p
, (unsigned) n
)) < 0) {
368 pa_log_error("Unable to get poll descriptors: %s", pa_alsa_strerror(err
));
369 pa_rtpoll_item_free(i
);
377 pa_rtpoll_item_set_userdata(i
, pd
);
378 pa_rtpoll_item_set_work_callback(i
, rtpoll_work_cb
);
385 static const snd_mixer_selem_channel_id_t alsa_channel_ids
[PA_CHANNEL_POSITION_MAX
] = {
386 [PA_CHANNEL_POSITION_MONO
] = SND_MIXER_SCHN_MONO
, /* The ALSA name is just an alias! */
388 [PA_CHANNEL_POSITION_FRONT_CENTER
] = SND_MIXER_SCHN_FRONT_CENTER
,
389 [PA_CHANNEL_POSITION_FRONT_LEFT
] = SND_MIXER_SCHN_FRONT_LEFT
,
390 [PA_CHANNEL_POSITION_FRONT_RIGHT
] = SND_MIXER_SCHN_FRONT_RIGHT
,
392 [PA_CHANNEL_POSITION_REAR_CENTER
] = SND_MIXER_SCHN_REAR_CENTER
,
393 [PA_CHANNEL_POSITION_REAR_LEFT
] = SND_MIXER_SCHN_REAR_LEFT
,
394 [PA_CHANNEL_POSITION_REAR_RIGHT
] = SND_MIXER_SCHN_REAR_RIGHT
,
396 [PA_CHANNEL_POSITION_LFE
] = SND_MIXER_SCHN_WOOFER
,
398 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
399 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
401 [PA_CHANNEL_POSITION_SIDE_LEFT
] = SND_MIXER_SCHN_SIDE_LEFT
,
402 [PA_CHANNEL_POSITION_SIDE_RIGHT
] = SND_MIXER_SCHN_SIDE_RIGHT
,
404 [PA_CHANNEL_POSITION_AUX0
] = SND_MIXER_SCHN_UNKNOWN
,
405 [PA_CHANNEL_POSITION_AUX1
] = SND_MIXER_SCHN_UNKNOWN
,
406 [PA_CHANNEL_POSITION_AUX2
] = SND_MIXER_SCHN_UNKNOWN
,
407 [PA_CHANNEL_POSITION_AUX3
] = SND_MIXER_SCHN_UNKNOWN
,
408 [PA_CHANNEL_POSITION_AUX4
] = SND_MIXER_SCHN_UNKNOWN
,
409 [PA_CHANNEL_POSITION_AUX5
] = SND_MIXER_SCHN_UNKNOWN
,
410 [PA_CHANNEL_POSITION_AUX6
] = SND_MIXER_SCHN_UNKNOWN
,
411 [PA_CHANNEL_POSITION_AUX7
] = SND_MIXER_SCHN_UNKNOWN
,
412 [PA_CHANNEL_POSITION_AUX8
] = SND_MIXER_SCHN_UNKNOWN
,
413 [PA_CHANNEL_POSITION_AUX9
] = SND_MIXER_SCHN_UNKNOWN
,
414 [PA_CHANNEL_POSITION_AUX10
] = SND_MIXER_SCHN_UNKNOWN
,
415 [PA_CHANNEL_POSITION_AUX11
] = SND_MIXER_SCHN_UNKNOWN
,
416 [PA_CHANNEL_POSITION_AUX12
] = SND_MIXER_SCHN_UNKNOWN
,
417 [PA_CHANNEL_POSITION_AUX13
] = SND_MIXER_SCHN_UNKNOWN
,
418 [PA_CHANNEL_POSITION_AUX14
] = SND_MIXER_SCHN_UNKNOWN
,
419 [PA_CHANNEL_POSITION_AUX15
] = SND_MIXER_SCHN_UNKNOWN
,
420 [PA_CHANNEL_POSITION_AUX16
] = SND_MIXER_SCHN_UNKNOWN
,
421 [PA_CHANNEL_POSITION_AUX17
] = SND_MIXER_SCHN_UNKNOWN
,
422 [PA_CHANNEL_POSITION_AUX18
] = SND_MIXER_SCHN_UNKNOWN
,
423 [PA_CHANNEL_POSITION_AUX19
] = SND_MIXER_SCHN_UNKNOWN
,
424 [PA_CHANNEL_POSITION_AUX20
] = SND_MIXER_SCHN_UNKNOWN
,
425 [PA_CHANNEL_POSITION_AUX21
] = SND_MIXER_SCHN_UNKNOWN
,
426 [PA_CHANNEL_POSITION_AUX22
] = SND_MIXER_SCHN_UNKNOWN
,
427 [PA_CHANNEL_POSITION_AUX23
] = SND_MIXER_SCHN_UNKNOWN
,
428 [PA_CHANNEL_POSITION_AUX24
] = SND_MIXER_SCHN_UNKNOWN
,
429 [PA_CHANNEL_POSITION_AUX25
] = SND_MIXER_SCHN_UNKNOWN
,
430 [PA_CHANNEL_POSITION_AUX26
] = SND_MIXER_SCHN_UNKNOWN
,
431 [PA_CHANNEL_POSITION_AUX27
] = SND_MIXER_SCHN_UNKNOWN
,
432 [PA_CHANNEL_POSITION_AUX28
] = SND_MIXER_SCHN_UNKNOWN
,
433 [PA_CHANNEL_POSITION_AUX29
] = SND_MIXER_SCHN_UNKNOWN
,
434 [PA_CHANNEL_POSITION_AUX30
] = SND_MIXER_SCHN_UNKNOWN
,
435 [PA_CHANNEL_POSITION_AUX31
] = SND_MIXER_SCHN_UNKNOWN
,
437 [PA_CHANNEL_POSITION_TOP_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
439 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
440 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT
] = SND_MIXER_SCHN_UNKNOWN
,
441 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
] = SND_MIXER_SCHN_UNKNOWN
,
443 [PA_CHANNEL_POSITION_TOP_REAR_CENTER
] = SND_MIXER_SCHN_UNKNOWN
,
444 [PA_CHANNEL_POSITION_TOP_REAR_LEFT
] = SND_MIXER_SCHN_UNKNOWN
,
445 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT
] = SND_MIXER_SCHN_UNKNOWN
448 static void setting_free(pa_alsa_setting
*s
) {
452 pa_idxset_free(s
->options
, NULL
, NULL
);
455 pa_xfree(s
->description
);
459 static void option_free(pa_alsa_option
*o
) {
462 pa_xfree(o
->alsa_name
);
464 pa_xfree(o
->description
);
468 static void decibel_fix_free(pa_alsa_decibel_fix
*db_fix
) {
471 pa_xfree(db_fix
->name
);
472 pa_xfree(db_fix
->db_values
);
477 static void jack_free(pa_alsa_jack
*j
) {
480 pa_xfree(j
->alsa_name
);
485 static void element_free(pa_alsa_element
*e
) {
489 while ((o
= e
->options
)) {
490 PA_LLIST_REMOVE(pa_alsa_option
, e
->options
, o
);
495 decibel_fix_free(e
->db_fix
);
497 pa_xfree(e
->alsa_name
);
501 void pa_alsa_path_free(pa_alsa_path
*p
) {
508 while ((j
= p
->jacks
)) {
509 PA_LLIST_REMOVE(pa_alsa_jack
, p
->jacks
, j
);
513 while ((e
= p
->elements
)) {
514 PA_LLIST_REMOVE(pa_alsa_element
, p
->elements
, e
);
518 while ((s
= p
->settings
)) {
519 PA_LLIST_REMOVE(pa_alsa_setting
, p
->settings
, s
);
523 pa_proplist_free(p
->proplist
);
525 pa_xfree(p
->description
);
529 void pa_alsa_path_set_free(pa_alsa_path_set
*ps
) {
533 pa_hashmap_free(ps
->paths
, NULL
, NULL
);
538 static long to_alsa_dB(pa_volume_t v
) {
539 return (long) (pa_sw_volume_to_dB(v
) * 100.0);
542 static pa_volume_t
from_alsa_dB(long v
) {
543 return pa_sw_volume_from_dB((double) v
/ 100.0);
546 static long to_alsa_volume(pa_volume_t v
, long min
, long max
) {
549 w
= (long) round(((double) v
* (double) (max
- min
)) / PA_VOLUME_NORM
) + min
;
550 return PA_CLAMP_UNLIKELY(w
, min
, max
);
553 static pa_volume_t
from_alsa_volume(long v
, long min
, long max
) {
554 return (pa_volume_t
) round(((double) (v
- min
) * PA_VOLUME_NORM
) / (double) (max
- min
));
557 #define SELEM_INIT(sid, name) \
559 snd_mixer_selem_id_alloca(&(sid)); \
560 snd_mixer_selem_id_set_name((sid), (name)); \
561 snd_mixer_selem_id_set_index((sid), 0); \
564 static int element_get_volume(pa_alsa_element
*e
, snd_mixer_t
*m
, const pa_channel_map
*cm
, pa_cvolume
*v
) {
565 snd_mixer_selem_id_t
*sid
;
566 snd_mixer_elem_t
*me
;
567 snd_mixer_selem_channel_id_t c
;
568 pa_channel_position_mask_t mask
= 0;
576 SELEM_INIT(sid
, e
->alsa_name
);
577 if (!(me
= snd_mixer_find_selem(m
, sid
))) {
578 pa_log_warn("Element %s seems to have disappeared.", e
->alsa_name
);
582 pa_cvolume_mute(v
, cm
->channels
);
584 /* We take the highest volume of all channels that match */
586 for (c
= 0; c
<= SND_MIXER_SCHN_LAST
; c
++) {
593 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
594 if (snd_mixer_selem_has_playback_channel(me
, c
)) {
596 if ((r
= snd_mixer_selem_get_playback_volume(me
, c
, &value
)) >= 0) {
597 /* If the channel volume is outside the limits set
598 * by the dB fix, we clamp the hw volume to be
599 * within the limits. */
600 if (value
< e
->db_fix
->min_step
) {
601 value
= e
->db_fix
->min_step
;
602 snd_mixer_selem_set_playback_volume(me
, c
, value
);
603 pa_log_debug("Playback volume for element %s channel %i was below the dB fix limit. "
604 "Volume reset to %0.2f dB.", e
->alsa_name
, c
,
605 e
->db_fix
->db_values
[value
- e
->db_fix
->min_step
] / 100.0);
606 } else if (value
> e
->db_fix
->max_step
) {
607 value
= e
->db_fix
->max_step
;
608 snd_mixer_selem_set_playback_volume(me
, c
, value
);
609 pa_log_debug("Playback volume for element %s channel %i was over the dB fix limit. "
610 "Volume reset to %0.2f dB.", e
->alsa_name
, c
,
611 e
->db_fix
->db_values
[value
- e
->db_fix
->min_step
] / 100.0);
614 /* Volume step -> dB value conversion. */
615 value
= e
->db_fix
->db_values
[value
- e
->db_fix
->min_step
];
618 r
= snd_mixer_selem_get_playback_dB(me
, c
, &value
);
622 if (snd_mixer_selem_has_capture_channel(me
, c
)) {
624 if ((r
= snd_mixer_selem_get_capture_volume(me
, c
, &value
)) >= 0) {
625 /* If the channel volume is outside the limits set
626 * by the dB fix, we clamp the hw volume to be
627 * within the limits. */
628 if (value
< e
->db_fix
->min_step
) {
629 value
= e
->db_fix
->min_step
;
630 snd_mixer_selem_set_capture_volume(me
, c
, value
);
631 pa_log_debug("Capture volume for element %s channel %i was below the dB fix limit. "
632 "Volume reset to %0.2f dB.", e
->alsa_name
, c
,
633 e
->db_fix
->db_values
[value
- e
->db_fix
->min_step
] / 100.0);
634 } else if (value
> e
->db_fix
->max_step
) {
635 value
= e
->db_fix
->max_step
;
636 snd_mixer_selem_set_capture_volume(me
, c
, value
);
637 pa_log_debug("Capture volume for element %s channel %i was over the dB fix limit. "
638 "Volume reset to %0.2f dB.", e
->alsa_name
, c
,
639 e
->db_fix
->db_values
[value
- e
->db_fix
->min_step
] / 100.0);
642 /* Volume step -> dB value conversion. */
643 value
= e
->db_fix
->db_values
[value
- e
->db_fix
->min_step
];
646 r
= snd_mixer_selem_get_capture_dB(me
, c
, &value
);
654 #ifdef HAVE_VALGRIND_MEMCHECK_H
655 VALGRIND_MAKE_MEM_DEFINED(&value
, sizeof(value
));
658 f
= from_alsa_dB(value
);
663 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
664 if (snd_mixer_selem_has_playback_channel(me
, c
))
665 r
= snd_mixer_selem_get_playback_volume(me
, c
, &value
);
669 if (snd_mixer_selem_has_capture_channel(me
, c
))
670 r
= snd_mixer_selem_get_capture_volume(me
, c
, &value
);
678 f
= from_alsa_volume(value
, e
->min_volume
, e
->max_volume
);
681 for (k
= 0; k
< cm
->channels
; k
++)
682 if (e
->masks
[c
][e
->n_channels
-1] & PA_CHANNEL_POSITION_MASK(cm
->map
[k
]))
683 if (v
->values
[k
] < f
)
686 mask
|= e
->masks
[c
][e
->n_channels
-1];
689 for (k
= 0; k
< cm
->channels
; k
++)
690 if (!(mask
& PA_CHANNEL_POSITION_MASK(cm
->map
[k
])))
691 v
->values
[k
] = PA_VOLUME_NORM
;
696 int pa_alsa_path_get_volume(pa_alsa_path
*p
, snd_mixer_t
*m
, const pa_channel_map
*cm
, pa_cvolume
*v
) {
707 pa_cvolume_reset(v
, cm
->channels
);
709 PA_LLIST_FOREACH(e
, p
->elements
) {
712 if (e
->volume_use
!= PA_ALSA_VOLUME_MERGE
)
715 pa_assert(!p
->has_dB
|| e
->has_dB
);
717 if (element_get_volume(e
, m
, cm
, &ev
) < 0)
720 /* If we have no dB information all we can do is take the first element and leave */
726 pa_sw_cvolume_multiply(v
, v
, &ev
);
732 static int element_get_switch(pa_alsa_element
*e
, snd_mixer_t
*m
, pa_bool_t
*b
) {
733 snd_mixer_selem_id_t
*sid
;
734 snd_mixer_elem_t
*me
;
735 snd_mixer_selem_channel_id_t c
;
741 SELEM_INIT(sid
, e
->alsa_name
);
742 if (!(me
= snd_mixer_find_selem(m
, sid
))) {
743 pa_log_warn("Element %s seems to have disappeared.", e
->alsa_name
);
747 /* We return muted if at least one channel is muted */
749 for (c
= 0; c
<= SND_MIXER_SCHN_LAST
; c
++) {
753 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
754 if (snd_mixer_selem_has_playback_channel(me
, c
))
755 r
= snd_mixer_selem_get_playback_switch(me
, c
, &value
);
759 if (snd_mixer_selem_has_capture_channel(me
, c
))
760 r
= snd_mixer_selem_get_capture_switch(me
, c
, &value
);
778 int pa_alsa_path_get_mute(pa_alsa_path
*p
, snd_mixer_t
*m
, pa_bool_t
*muted
) {
788 PA_LLIST_FOREACH(e
, p
->elements
) {
791 if (e
->switch_use
!= PA_ALSA_SWITCH_MUTE
)
794 if (element_get_switch(e
, m
, &b
) < 0)
807 /* Finds the closest item in db_fix->db_values and returns the corresponding
808 * step. *db_value is replaced with the value from the db_values table.
809 * Rounding is done based on the rounding parameter: -1 means rounding down and
810 * +1 means rounding up. */
811 static long decibel_fix_get_step(pa_alsa_decibel_fix
*db_fix
, long *db_value
, int rounding
) {
817 pa_assert(rounding
!= 0);
819 max_i
= db_fix
->max_step
- db_fix
->min_step
;
822 for (i
= 0; i
< max_i
; i
++) {
823 if (db_fix
->db_values
[i
] >= *db_value
)
827 for (i
= 0; i
< max_i
; i
++) {
828 if (db_fix
->db_values
[i
+ 1] > *db_value
)
833 *db_value
= db_fix
->db_values
[i
];
835 return i
+ db_fix
->min_step
;
838 /* Alsa lib documentation says for snd_mixer_selem_set_playback_dB() direction argument,
839 * that "-1 = accurate or first below, 0 = accurate, 1 = accurate or first above".
840 * But even with accurate nearest dB volume step is not selected, so that is why we need
841 * this function. Returns 0 and nearest selectable volume in *value_dB on success or
842 * negative error code if fails. */
843 static int element_get_nearest_alsa_dB(snd_mixer_elem_t
*me
, snd_mixer_selem_channel_id_t c
, pa_alsa_direction_t d
, long *value_dB
) {
853 if (d
== PA_ALSA_DIRECTION_OUTPUT
) {
854 if ((r
= snd_mixer_selem_ask_playback_dB_vol(me
, *value_dB
, +1, &alsa_val
)) >= 0)
855 r
= snd_mixer_selem_ask_playback_vol_dB(me
, alsa_val
, &value_high
);
860 if (value_high
== *value_dB
)
863 if ((r
= snd_mixer_selem_ask_playback_dB_vol(me
, *value_dB
, -1, &alsa_val
)) >= 0)
864 r
= snd_mixer_selem_ask_playback_vol_dB(me
, alsa_val
, &value_low
);
866 if ((r
= snd_mixer_selem_ask_capture_dB_vol(me
, *value_dB
, +1, &alsa_val
)) >= 0)
867 r
= snd_mixer_selem_ask_capture_vol_dB(me
, alsa_val
, &value_high
);
872 if (value_high
== *value_dB
)
875 if ((r
= snd_mixer_selem_ask_capture_dB_vol(me
, *value_dB
, -1, &alsa_val
)) >= 0)
876 r
= snd_mixer_selem_ask_capture_vol_dB(me
, alsa_val
, &value_low
);
882 if (labs(value_high
- *value_dB
) < labs(value_low
- *value_dB
))
883 *value_dB
= value_high
;
885 *value_dB
= value_low
;
890 static int element_set_volume(pa_alsa_element
*e
, snd_mixer_t
*m
, const pa_channel_map
*cm
, pa_cvolume
*v
, pa_bool_t deferred_volume
, pa_bool_t write_to_hw
) {
892 snd_mixer_selem_id_t
*sid
;
894 snd_mixer_elem_t
*me
;
895 snd_mixer_selem_channel_id_t c
;
896 pa_channel_position_mask_t mask
= 0;
903 pa_assert(pa_cvolume_compatible_with_channel_map(v
, cm
));
905 SELEM_INIT(sid
, e
->alsa_name
);
906 if (!(me
= snd_mixer_find_selem(m
, sid
))) {
907 pa_log_warn("Element %s seems to have disappeared.", e
->alsa_name
);
911 pa_cvolume_mute(&rv
, cm
->channels
);
913 for (c
= 0; c
<= SND_MIXER_SCHN_LAST
; c
++) {
915 pa_volume_t f
= PA_VOLUME_MUTED
;
916 pa_bool_t found
= FALSE
;
918 for (k
= 0; k
< cm
->channels
; k
++)
919 if (e
->masks
[c
][e
->n_channels
-1] & PA_CHANNEL_POSITION_MASK(cm
->map
[k
])) {
921 if (v
->values
[k
] > f
)
926 /* Hmm, so this channel does not exist in the volume
927 * struct, so let's bind it to the overall max of the
929 f
= pa_cvolume_max(v
);
933 long value
= to_alsa_dB(f
);
936 if (e
->volume_limit
>= 0 && value
> (e
->max_dB
* 100))
937 value
= e
->max_dB
* 100;
939 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
940 /* If we call set_playback_volume() without checking first
941 * if the channel is available, ALSA behaves very
942 * strangely and doesn't fail the call */
943 if (snd_mixer_selem_has_playback_channel(me
, c
)) {
947 r
= snd_mixer_selem_set_playback_volume(me
, c
, decibel_fix_get_step(e
->db_fix
, &value
, rounding
));
949 decibel_fix_get_step(e
->db_fix
, &value
, rounding
);
955 if (deferred_volume
) {
956 if ((r
= element_get_nearest_alsa_dB(me
, c
, PA_ALSA_DIRECTION_OUTPUT
, &value
)) >= 0)
957 r
= snd_mixer_selem_set_playback_dB(me
, c
, value
, 0);
959 if ((r
= snd_mixer_selem_set_playback_dB(me
, c
, value
, rounding
)) >= 0)
960 r
= snd_mixer_selem_get_playback_dB(me
, c
, &value
);
964 if ((r
= snd_mixer_selem_ask_playback_dB_vol(me
, value
, rounding
, &alsa_val
)) >= 0)
965 r
= snd_mixer_selem_ask_playback_vol_dB(me
, alsa_val
, &value
);
971 if (snd_mixer_selem_has_capture_channel(me
, c
)) {
975 r
= snd_mixer_selem_set_capture_volume(me
, c
, decibel_fix_get_step(e
->db_fix
, &value
, rounding
));
977 decibel_fix_get_step(e
->db_fix
, &value
, rounding
);
983 if (deferred_volume
) {
984 if ((r
= element_get_nearest_alsa_dB(me
, c
, PA_ALSA_DIRECTION_INPUT
, &value
)) >= 0)
985 r
= snd_mixer_selem_set_capture_dB(me
, c
, value
, 0);
987 if ((r
= snd_mixer_selem_set_capture_dB(me
, c
, value
, rounding
)) >= 0)
988 r
= snd_mixer_selem_get_capture_dB(me
, c
, &value
);
992 if ((r
= snd_mixer_selem_ask_capture_dB_vol(me
, value
, rounding
, &alsa_val
)) >= 0)
993 r
= snd_mixer_selem_ask_capture_vol_dB(me
, alsa_val
, &value
);
1003 #ifdef HAVE_VALGRIND_MEMCHECK_H
1004 VALGRIND_MAKE_MEM_DEFINED(&value
, sizeof(value
));
1007 f
= from_alsa_dB(value
);
1012 value
= to_alsa_volume(f
, e
->min_volume
, e
->max_volume
);
1014 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
1015 if (snd_mixer_selem_has_playback_channel(me
, c
)) {
1016 if ((r
= snd_mixer_selem_set_playback_volume(me
, c
, value
)) >= 0)
1017 r
= snd_mixer_selem_get_playback_volume(me
, c
, &value
);
1021 if (snd_mixer_selem_has_capture_channel(me
, c
)) {
1022 if ((r
= snd_mixer_selem_set_capture_volume(me
, c
, value
)) >= 0)
1023 r
= snd_mixer_selem_get_capture_volume(me
, c
, &value
);
1031 f
= from_alsa_volume(value
, e
->min_volume
, e
->max_volume
);
1034 for (k
= 0; k
< cm
->channels
; k
++)
1035 if (e
->masks
[c
][e
->n_channels
-1] & PA_CHANNEL_POSITION_MASK(cm
->map
[k
]))
1036 if (rv
.values
[k
] < f
)
1039 mask
|= e
->masks
[c
][e
->n_channels
-1];
1042 for (k
= 0; k
< cm
->channels
; k
++)
1043 if (!(mask
& PA_CHANNEL_POSITION_MASK(cm
->map
[k
])))
1044 rv
.values
[k
] = PA_VOLUME_NORM
;
1050 int pa_alsa_path_set_volume(pa_alsa_path
*p
, snd_mixer_t
*m
, const pa_channel_map
*cm
, pa_cvolume
*v
, pa_bool_t deferred_volume
, pa_bool_t write_to_hw
) {
1059 pa_assert(pa_cvolume_compatible_with_channel_map(v
, cm
));
1064 rv
= *v
; /* Remaining adjustment */
1065 pa_cvolume_reset(v
, cm
->channels
); /* Adjustment done */
1067 PA_LLIST_FOREACH(e
, p
->elements
) {
1070 if (e
->volume_use
!= PA_ALSA_VOLUME_MERGE
)
1073 pa_assert(!p
->has_dB
|| e
->has_dB
);
1076 if (element_set_volume(e
, m
, cm
, &ev
, deferred_volume
, write_to_hw
) < 0)
1084 pa_sw_cvolume_multiply(v
, v
, &ev
);
1085 pa_sw_cvolume_divide(&rv
, &rv
, &ev
);
1091 static int element_set_switch(pa_alsa_element
*e
, snd_mixer_t
*m
, pa_bool_t b
) {
1092 snd_mixer_elem_t
*me
;
1093 snd_mixer_selem_id_t
*sid
;
1099 SELEM_INIT(sid
, e
->alsa_name
);
1100 if (!(me
= snd_mixer_find_selem(m
, sid
))) {
1101 pa_log_warn("Element %s seems to have disappeared.", e
->alsa_name
);
1105 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1106 r
= snd_mixer_selem_set_playback_switch_all(me
, b
);
1108 r
= snd_mixer_selem_set_capture_switch_all(me
, b
);
1111 pa_log_warn("Failed to set switch of %s: %s", e
->alsa_name
, pa_alsa_strerror(errno
));
1116 int pa_alsa_path_set_mute(pa_alsa_path
*p
, snd_mixer_t
*m
, pa_bool_t muted
) {
1125 PA_LLIST_FOREACH(e
, p
->elements
) {
1127 if (e
->switch_use
!= PA_ALSA_SWITCH_MUTE
)
1130 if (element_set_switch(e
, m
, !muted
) < 0)
1137 /* Depending on whether e->volume_use is _OFF, _ZERO or _CONSTANT, this
1138 * function sets all channels of the volume element to e->min_volume, 0 dB or
1139 * e->constant_volume. */
1140 static int element_set_constant_volume(pa_alsa_element
*e
, snd_mixer_t
*m
) {
1141 snd_mixer_elem_t
*me
= NULL
;
1142 snd_mixer_selem_id_t
*sid
= NULL
;
1145 pa_bool_t volume_set
= FALSE
;
1150 SELEM_INIT(sid
, e
->alsa_name
);
1151 if (!(me
= snd_mixer_find_selem(m
, sid
))) {
1152 pa_log_warn("Element %s seems to have disappeared.", e
->alsa_name
);
1156 switch (e
->volume_use
) {
1157 case PA_ALSA_VOLUME_OFF
:
1158 volume
= e
->min_volume
;
1162 case PA_ALSA_VOLUME_ZERO
:
1166 volume
= decibel_fix_get_step(e
->db_fix
, &dB
, (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
? +1 : -1));
1171 case PA_ALSA_VOLUME_CONSTANT
:
1172 volume
= e
->constant_volume
;
1177 pa_assert_not_reached();
1181 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1182 r
= snd_mixer_selem_set_playback_volume_all(me
, volume
);
1184 r
= snd_mixer_selem_set_capture_volume_all(me
, volume
);
1186 pa_assert(e
->volume_use
== PA_ALSA_VOLUME_ZERO
);
1187 pa_assert(!e
->db_fix
);
1189 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1190 r
= snd_mixer_selem_set_playback_dB_all(me
, 0, +1);
1192 r
= snd_mixer_selem_set_capture_dB_all(me
, 0, -1);
1196 pa_log_warn("Failed to set volume of %s: %s", e
->alsa_name
, pa_alsa_strerror(errno
));
1201 int pa_alsa_path_select(pa_alsa_path
*p
, pa_alsa_setting
*s
, snd_mixer_t
*m
, bool device_is_muted
) {
1208 pa_log_debug("Activating path %s", p
->name
);
1209 pa_alsa_path_dump(p
);
1211 /* First turn on hw mute if available, to avoid noise
1212 * when setting the mixer controls. */
1213 if (p
->mute_during_activation
) {
1214 PA_LLIST_FOREACH(e
, p
->elements
) {
1215 if (e
->switch_use
== PA_ALSA_SWITCH_MUTE
)
1216 /* If the muting fails here, that's not a critical problem for
1217 * selecting a path, so we ignore the return value.
1218 * element_set_switch() will print a warning anyway, so this
1219 * won't be a silent failure either. */
1220 (void) element_set_switch(e
, m
, FALSE
);
1224 PA_LLIST_FOREACH(e
, p
->elements
) {
1226 switch (e
->switch_use
) {
1227 case PA_ALSA_SWITCH_OFF
:
1228 r
= element_set_switch(e
, m
, FALSE
);
1231 case PA_ALSA_SWITCH_ON
:
1232 r
= element_set_switch(e
, m
, TRUE
);
1235 case PA_ALSA_SWITCH_MUTE
:
1236 case PA_ALSA_SWITCH_IGNORE
:
1237 case PA_ALSA_SWITCH_SELECT
:
1245 switch (e
->volume_use
) {
1246 case PA_ALSA_VOLUME_OFF
:
1247 case PA_ALSA_VOLUME_ZERO
:
1248 case PA_ALSA_VOLUME_CONSTANT
:
1249 r
= element_set_constant_volume(e
, m
);
1252 case PA_ALSA_VOLUME_MERGE
:
1253 case PA_ALSA_VOLUME_IGNORE
:
1263 setting_select(s
, m
);
1265 /* Finally restore hw mute to the device mute status. */
1266 if (p
->mute_during_activation
) {
1267 PA_LLIST_FOREACH(e
, p
->elements
) {
1268 if (e
->switch_use
== PA_ALSA_SWITCH_MUTE
) {
1269 if (element_set_switch(e
, m
, !device_is_muted
) < 0)
1278 static int check_required(pa_alsa_element
*e
, snd_mixer_elem_t
*me
) {
1279 pa_bool_t has_switch
;
1280 pa_bool_t has_enumeration
;
1281 pa_bool_t has_volume
;
1286 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
1288 snd_mixer_selem_has_playback_switch(me
) ||
1289 (e
->direction_try_other
&& snd_mixer_selem_has_capture_switch(me
));
1292 snd_mixer_selem_has_capture_switch(me
) ||
1293 (e
->direction_try_other
&& snd_mixer_selem_has_playback_switch(me
));
1296 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
1298 snd_mixer_selem_has_playback_volume(me
) ||
1299 (e
->direction_try_other
&& snd_mixer_selem_has_capture_volume(me
));
1302 snd_mixer_selem_has_capture_volume(me
) ||
1303 (e
->direction_try_other
&& snd_mixer_selem_has_playback_volume(me
));
1306 has_enumeration
= snd_mixer_selem_is_enumerated(me
);
1308 if ((e
->required
== PA_ALSA_REQUIRED_SWITCH
&& !has_switch
) ||
1309 (e
->required
== PA_ALSA_REQUIRED_VOLUME
&& !has_volume
) ||
1310 (e
->required
== PA_ALSA_REQUIRED_ENUMERATION
&& !has_enumeration
))
1313 if (e
->required
== PA_ALSA_REQUIRED_ANY
&& !(has_switch
|| has_volume
|| has_enumeration
))
1316 if ((e
->required_absent
== PA_ALSA_REQUIRED_SWITCH
&& has_switch
) ||
1317 (e
->required_absent
== PA_ALSA_REQUIRED_VOLUME
&& has_volume
) ||
1318 (e
->required_absent
== PA_ALSA_REQUIRED_ENUMERATION
&& has_enumeration
))
1321 if (e
->required_absent
== PA_ALSA_REQUIRED_ANY
&& (has_switch
|| has_volume
|| has_enumeration
))
1324 if (e
->required_any
!= PA_ALSA_REQUIRED_IGNORE
) {
1325 switch (e
->required_any
) {
1326 case PA_ALSA_REQUIRED_VOLUME
:
1327 e
->path
->req_any_present
|= (e
->volume_use
!= PA_ALSA_VOLUME_IGNORE
);
1329 case PA_ALSA_REQUIRED_SWITCH
:
1330 e
->path
->req_any_present
|= (e
->switch_use
!= PA_ALSA_SWITCH_IGNORE
);
1332 case PA_ALSA_REQUIRED_ENUMERATION
:
1333 e
->path
->req_any_present
|= (e
->enumeration_use
!= PA_ALSA_ENUMERATION_IGNORE
);
1335 case PA_ALSA_REQUIRED_ANY
:
1336 e
->path
->req_any_present
|=
1337 (e
->volume_use
!= PA_ALSA_VOLUME_IGNORE
) ||
1338 (e
->switch_use
!= PA_ALSA_SWITCH_IGNORE
) ||
1339 (e
->enumeration_use
!= PA_ALSA_ENUMERATION_IGNORE
);
1342 pa_assert_not_reached();
1346 if (e
->enumeration_use
== PA_ALSA_ENUMERATION_SELECT
) {
1348 PA_LLIST_FOREACH(o
, e
->options
) {
1349 e
->path
->req_any_present
|= (o
->required_any
!= PA_ALSA_REQUIRED_IGNORE
) &&
1351 if (o
->required
!= PA_ALSA_REQUIRED_IGNORE
&& o
->alsa_idx
< 0)
1353 if (o
->required_absent
!= PA_ALSA_REQUIRED_IGNORE
&& o
->alsa_idx
>= 0)
1361 static int element_probe(pa_alsa_element
*e
, snd_mixer_t
*m
) {
1362 snd_mixer_selem_id_t
*sid
;
1363 snd_mixer_elem_t
*me
;
1369 SELEM_INIT(sid
, e
->alsa_name
);
1371 if (!(me
= snd_mixer_find_selem(m
, sid
))) {
1373 if (e
->required
!= PA_ALSA_REQUIRED_IGNORE
)
1376 e
->switch_use
= PA_ALSA_SWITCH_IGNORE
;
1377 e
->volume_use
= PA_ALSA_VOLUME_IGNORE
;
1378 e
->enumeration_use
= PA_ALSA_ENUMERATION_IGNORE
;
1383 if (e
->switch_use
!= PA_ALSA_SWITCH_IGNORE
) {
1384 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
1386 if (!snd_mixer_selem_has_playback_switch(me
)) {
1387 if (e
->direction_try_other
&& snd_mixer_selem_has_capture_switch(me
))
1388 e
->direction
= PA_ALSA_DIRECTION_INPUT
;
1390 e
->switch_use
= PA_ALSA_SWITCH_IGNORE
;
1395 if (!snd_mixer_selem_has_capture_switch(me
)) {
1396 if (e
->direction_try_other
&& snd_mixer_selem_has_playback_switch(me
))
1397 e
->direction
= PA_ALSA_DIRECTION_OUTPUT
;
1399 e
->switch_use
= PA_ALSA_SWITCH_IGNORE
;
1403 if (e
->switch_use
!= PA_ALSA_SWITCH_IGNORE
)
1404 e
->direction_try_other
= FALSE
;
1407 if (e
->volume_use
!= PA_ALSA_VOLUME_IGNORE
) {
1409 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
1411 if (!snd_mixer_selem_has_playback_volume(me
)) {
1412 if (e
->direction_try_other
&& snd_mixer_selem_has_capture_volume(me
))
1413 e
->direction
= PA_ALSA_DIRECTION_INPUT
;
1415 e
->volume_use
= PA_ALSA_VOLUME_IGNORE
;
1420 if (!snd_mixer_selem_has_capture_volume(me
)) {
1421 if (e
->direction_try_other
&& snd_mixer_selem_has_playback_volume(me
))
1422 e
->direction
= PA_ALSA_DIRECTION_OUTPUT
;
1424 e
->volume_use
= PA_ALSA_VOLUME_IGNORE
;
1428 if (e
->volume_use
!= PA_ALSA_VOLUME_IGNORE
) {
1429 long min_dB
= 0, max_dB
= 0;
1432 e
->direction_try_other
= FALSE
;
1434 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1435 r
= snd_mixer_selem_get_playback_volume_range(me
, &e
->min_volume
, &e
->max_volume
);
1437 r
= snd_mixer_selem_get_capture_volume_range(me
, &e
->min_volume
, &e
->max_volume
);
1440 pa_log_warn("Failed to get volume range of %s: %s", e
->alsa_name
, pa_alsa_strerror(r
));
1444 if (e
->min_volume
>= e
->max_volume
) {
1445 pa_log_warn("Your kernel driver is broken: it reports a volume range from %li to %li which makes no sense.", e
->min_volume
, e
->max_volume
);
1446 e
->volume_use
= PA_ALSA_VOLUME_IGNORE
;
1448 } else if (e
->volume_use
== PA_ALSA_VOLUME_CONSTANT
&&
1449 (e
->min_volume
> e
->constant_volume
|| e
->max_volume
< e
->constant_volume
)) {
1450 pa_log_warn("Constant volume %li configured for element %s, but the available range is from %li to %li.",
1451 e
->constant_volume
, e
->alsa_name
, e
->min_volume
, e
->max_volume
);
1452 e
->volume_use
= PA_ALSA_VOLUME_IGNORE
;
1456 pa_channel_position_t p
;
1459 ((e
->min_volume
> e
->db_fix
->min_step
) ||
1460 (e
->max_volume
< e
->db_fix
->max_step
))) {
1461 pa_log_warn("The step range of the decibel fix for element %s (%li-%li) doesn't fit to the "
1462 "real hardware range (%li-%li). Disabling the decibel fix.", e
->alsa_name
,
1463 e
->db_fix
->min_step
, e
->db_fix
->max_step
,
1464 e
->min_volume
, e
->max_volume
);
1466 decibel_fix_free(e
->db_fix
);
1472 e
->min_volume
= e
->db_fix
->min_step
;
1473 e
->max_volume
= e
->db_fix
->max_step
;
1474 min_dB
= e
->db_fix
->db_values
[0];
1475 max_dB
= e
->db_fix
->db_values
[e
->db_fix
->max_step
- e
->db_fix
->min_step
];
1476 } else if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1477 e
->has_dB
= snd_mixer_selem_get_playback_dB_range(me
, &min_dB
, &max_dB
) >= 0;
1479 e
->has_dB
= snd_mixer_selem_get_capture_dB_range(me
, &min_dB
, &max_dB
) >= 0;
1481 /* Check that the kernel driver returns consistent limits with
1482 * both _get_*_dB_range() and _ask_*_vol_dB(). */
1483 if (e
->has_dB
&& !e
->db_fix
) {
1484 long min_dB_checked
= 0;
1485 long max_dB_checked
= 0;
1487 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1488 r
= snd_mixer_selem_ask_playback_vol_dB(me
, e
->min_volume
, &min_dB_checked
);
1490 r
= snd_mixer_selem_ask_capture_vol_dB(me
, e
->min_volume
, &min_dB_checked
);
1493 pa_log_warn("Failed to query the dB value for %s at volume level %li", e
->alsa_name
, e
->min_volume
);
1497 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1498 r
= snd_mixer_selem_ask_playback_vol_dB(me
, e
->max_volume
, &max_dB_checked
);
1500 r
= snd_mixer_selem_ask_capture_vol_dB(me
, e
->max_volume
, &max_dB_checked
);
1503 pa_log_warn("Failed to query the dB value for %s at volume level %li", e
->alsa_name
, e
->max_volume
);
1507 if (min_dB
!= min_dB_checked
|| max_dB
!= max_dB_checked
) {
1508 pa_log_warn("Your kernel driver is broken: the reported dB range for %s (from %0.2f dB to %0.2f dB) "
1509 "doesn't match the dB values at minimum and maximum volume levels: %0.2f dB at level %li, "
1510 "%0.2f dB at level %li.",
1512 min_dB
/ 100.0, max_dB
/ 100.0,
1513 min_dB_checked
/ 100.0, e
->min_volume
, max_dB_checked
/ 100.0, e
->max_volume
);
1519 #ifdef HAVE_VALGRIND_MEMCHECK_H
1520 VALGRIND_MAKE_MEM_DEFINED(&min_dB
, sizeof(min_dB
));
1521 VALGRIND_MAKE_MEM_DEFINED(&max_dB
, sizeof(max_dB
));
1524 e
->min_dB
= ((double) min_dB
) / 100.0;
1525 e
->max_dB
= ((double) max_dB
) / 100.0;
1527 if (min_dB
>= max_dB
) {
1528 pa_assert(!e
->db_fix
);
1529 pa_log_warn("Your kernel driver is broken: it reports a volume range from %0.2f dB to %0.2f dB which makes no sense.", e
->min_dB
, e
->max_dB
);
1534 if (e
->volume_limit
>= 0) {
1535 if (e
->volume_limit
<= e
->min_volume
|| e
->volume_limit
> e
->max_volume
)
1536 pa_log_warn("Volume limit for element %s of path %s is invalid: %li isn't within the valid range "
1537 "%li-%li. The volume limit is ignored.",
1538 e
->alsa_name
, e
->path
->name
, e
->volume_limit
, e
->min_volume
+ 1, e
->max_volume
);
1541 e
->max_volume
= e
->volume_limit
;
1545 e
->db_fix
->max_step
= e
->max_volume
;
1546 e
->max_dB
= ((double) e
->db_fix
->db_values
[e
->db_fix
->max_step
- e
->db_fix
->min_step
]) / 100.0;
1549 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1550 r
= snd_mixer_selem_ask_playback_vol_dB(me
, e
->max_volume
, &max_dB
);
1552 r
= snd_mixer_selem_ask_capture_vol_dB(me
, e
->max_volume
, &max_dB
);
1555 pa_log_warn("Failed to get dB value of %s: %s", e
->alsa_name
, pa_alsa_strerror(r
));
1558 e
->max_dB
= ((double) max_dB
) / 100.0;
1564 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1565 is_mono
= snd_mixer_selem_is_playback_mono(me
) > 0;
1567 is_mono
= snd_mixer_selem_is_capture_mono(me
) > 0;
1572 if (!e
->override_map
) {
1573 for (p
= PA_CHANNEL_POSITION_FRONT_LEFT
; p
< PA_CHANNEL_POSITION_MAX
; p
++) {
1574 if (alsa_channel_ids
[p
] == SND_MIXER_SCHN_UNKNOWN
)
1577 e
->masks
[alsa_channel_ids
[p
]][e
->n_channels
-1] = 0;
1580 e
->masks
[SND_MIXER_SCHN_MONO
][e
->n_channels
-1] = PA_CHANNEL_POSITION_MASK_ALL
;
1583 e
->merged_mask
= e
->masks
[SND_MIXER_SCHN_MONO
][e
->n_channels
-1];
1586 for (p
= PA_CHANNEL_POSITION_FRONT_LEFT
; p
< PA_CHANNEL_POSITION_MAX
; p
++) {
1588 if (alsa_channel_ids
[p
] == SND_MIXER_SCHN_UNKNOWN
)
1591 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1592 e
->n_channels
+= snd_mixer_selem_has_playback_channel(me
, alsa_channel_ids
[p
]) > 0;
1594 e
->n_channels
+= snd_mixer_selem_has_capture_channel(me
, alsa_channel_ids
[p
]) > 0;
1597 if (e
->n_channels
<= 0) {
1598 pa_log_warn("Volume element %s with no channels?", e
->alsa_name
);
1602 if (e
->n_channels
> 2) {
1603 /* FIXME: In some places code like this is used:
1605 * e->masks[alsa_channel_ids[p]][e->n_channels-1]
1607 * The definition of e->masks is
1609 * pa_channel_position_mask_t masks[SND_MIXER_SCHN_LAST + 1][2];
1611 * Since the array size is fixed at 2, we obviously
1612 * don't support elements with more than two
1614 pa_log_warn("Volume element %s has %u channels. That's too much! I can't handle that!", e
->alsa_name
, e
->n_channels
);
1618 if (!e
->override_map
) {
1619 for (p
= PA_CHANNEL_POSITION_FRONT_LEFT
; p
< PA_CHANNEL_POSITION_MAX
; p
++) {
1620 pa_bool_t has_channel
;
1622 if (alsa_channel_ids
[p
] == SND_MIXER_SCHN_UNKNOWN
)
1625 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
1626 has_channel
= snd_mixer_selem_has_playback_channel(me
, alsa_channel_ids
[p
]) > 0;
1628 has_channel
= snd_mixer_selem_has_capture_channel(me
, alsa_channel_ids
[p
]) > 0;
1630 e
->masks
[alsa_channel_ids
[p
]][e
->n_channels
-1] = has_channel
? PA_CHANNEL_POSITION_MASK(p
) : 0;
1635 for (p
= PA_CHANNEL_POSITION_FRONT_LEFT
; p
< PA_CHANNEL_POSITION_MAX
; p
++) {
1636 if (alsa_channel_ids
[p
] == SND_MIXER_SCHN_UNKNOWN
)
1639 e
->merged_mask
|= e
->masks
[alsa_channel_ids
[p
]][e
->n_channels
-1];
1647 if (e
->switch_use
== PA_ALSA_SWITCH_SELECT
) {
1650 PA_LLIST_FOREACH(o
, e
->options
)
1651 o
->alsa_idx
= pa_streq(o
->alsa_name
, "on") ? 1 : 0;
1652 } else if (e
->enumeration_use
== PA_ALSA_ENUMERATION_SELECT
) {
1656 if ((n
= snd_mixer_selem_get_enum_items(me
)) < 0) {
1657 pa_log("snd_mixer_selem_get_enum_items() failed: %s", pa_alsa_strerror(n
));
1661 PA_LLIST_FOREACH(o
, e
->options
) {
1664 for (i
= 0; i
< n
; i
++) {
1667 if (snd_mixer_selem_get_enum_item_name(me
, i
, sizeof(buf
), buf
) < 0)
1670 if (!pa_streq(buf
, o
->alsa_name
))
1678 if (check_required(e
, me
) < 0)
1684 static int jack_probe(pa_alsa_jack
*j
, snd_hctl_t
*h
) {
1689 j
->has_control
= pa_alsa_find_jack(h
, j
->alsa_name
) != NULL
;
1691 if (j
->has_control
) {
1692 if (j
->required_absent
!= PA_ALSA_REQUIRED_IGNORE
)
1694 if (j
->required_any
!= PA_ALSA_REQUIRED_IGNORE
)
1695 j
->path
->req_any_present
= TRUE
;
1697 if (j
->required
!= PA_ALSA_REQUIRED_IGNORE
)
1704 static pa_alsa_element
* element_get(pa_alsa_path
*p
, const char *section
, pa_bool_t prefixed
) {
1711 if (!pa_startswith(section
, "Element "))
1717 /* This is not an element section, but an enum section? */
1718 if (strchr(section
, ':'))
1721 if (p
->last_element
&& pa_streq(p
->last_element
->alsa_name
, section
))
1722 return p
->last_element
;
1724 PA_LLIST_FOREACH(e
, p
->elements
)
1725 if (pa_streq(e
->alsa_name
, section
))
1728 e
= pa_xnew0(pa_alsa_element
, 1);
1730 e
->alsa_name
= pa_xstrdup(section
);
1731 e
->direction
= p
->direction
;
1732 e
->volume_limit
= -1;
1734 PA_LLIST_INSERT_AFTER(pa_alsa_element
, p
->elements
, p
->last_element
, e
);
1737 p
->last_element
= e
;
1741 static pa_alsa_jack
* jack_get(pa_alsa_path
*p
, const char *section
) {
1744 if (!pa_startswith(section
, "Jack "))
1748 if (p
->last_jack
&& pa_streq(p
->last_jack
->name
, section
))
1749 return p
->last_jack
;
1751 PA_LLIST_FOREACH(j
, p
->jacks
)
1752 if (pa_streq(j
->name
, section
))
1755 j
= pa_xnew0(pa_alsa_jack
, 1);
1756 j
->state_unplugged
= PA_PORT_AVAILABLE_NO
;
1757 j
->state_plugged
= PA_PORT_AVAILABLE_YES
;
1759 j
->name
= pa_xstrdup(section
);
1760 j
->alsa_name
= pa_sprintf_malloc("%s Jack", section
);
1761 PA_LLIST_INSERT_AFTER(pa_alsa_jack
, p
->jacks
, p
->last_jack
, j
);
1769 static pa_alsa_option
* option_get(pa_alsa_path
*p
, const char *section
) {
1775 if (!pa_startswith(section
, "Option "))
1780 /* This is not an enum section, but an element section? */
1781 if (!(on
= strchr(section
, ':')))
1784 en
= pa_xstrndup(section
, on
- section
);
1787 if (p
->last_option
&&
1788 pa_streq(p
->last_option
->element
->alsa_name
, en
) &&
1789 pa_streq(p
->last_option
->alsa_name
, on
)) {
1791 return p
->last_option
;
1794 pa_assert_se(e
= element_get(p
, en
, FALSE
));
1797 PA_LLIST_FOREACH(o
, e
->options
)
1798 if (pa_streq(o
->alsa_name
, on
))
1801 o
= pa_xnew0(pa_alsa_option
, 1);
1803 o
->alsa_name
= pa_xstrdup(on
);
1806 if (p
->last_option
&& p
->last_option
->element
== e
)
1807 PA_LLIST_INSERT_AFTER(pa_alsa_option
, e
->options
, p
->last_option
, o
);
1809 PA_LLIST_PREPEND(pa_alsa_option
, e
->options
, o
);
1816 static int element_parse_switch(pa_config_parser_state
*state
) {
1822 p
= state
->userdata
;
1824 if (!(e
= element_get(p
, state
->section
, TRUE
))) {
1825 pa_log("[%s:%u] Switch makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
1829 if (pa_streq(state
->rvalue
, "ignore"))
1830 e
->switch_use
= PA_ALSA_SWITCH_IGNORE
;
1831 else if (pa_streq(state
->rvalue
, "mute"))
1832 e
->switch_use
= PA_ALSA_SWITCH_MUTE
;
1833 else if (pa_streq(state
->rvalue
, "off"))
1834 e
->switch_use
= PA_ALSA_SWITCH_OFF
;
1835 else if (pa_streq(state
->rvalue
, "on"))
1836 e
->switch_use
= PA_ALSA_SWITCH_ON
;
1837 else if (pa_streq(state
->rvalue
, "select"))
1838 e
->switch_use
= PA_ALSA_SWITCH_SELECT
;
1840 pa_log("[%s:%u] Switch invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
1847 static int element_parse_volume(pa_config_parser_state
*state
) {
1853 p
= state
->userdata
;
1855 if (!(e
= element_get(p
, state
->section
, TRUE
))) {
1856 pa_log("[%s:%u] Volume makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
1860 if (pa_streq(state
->rvalue
, "ignore"))
1861 e
->volume_use
= PA_ALSA_VOLUME_IGNORE
;
1862 else if (pa_streq(state
->rvalue
, "merge"))
1863 e
->volume_use
= PA_ALSA_VOLUME_MERGE
;
1864 else if (pa_streq(state
->rvalue
, "off"))
1865 e
->volume_use
= PA_ALSA_VOLUME_OFF
;
1866 else if (pa_streq(state
->rvalue
, "zero"))
1867 e
->volume_use
= PA_ALSA_VOLUME_ZERO
;
1871 if (pa_atou(state
->rvalue
, &constant
) >= 0) {
1872 e
->volume_use
= PA_ALSA_VOLUME_CONSTANT
;
1873 e
->constant_volume
= constant
;
1875 pa_log("[%s:%u] Volume invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
1883 static int element_parse_enumeration(pa_config_parser_state
*state
) {
1889 p
= state
->userdata
;
1891 if (!(e
= element_get(p
, state
->section
, TRUE
))) {
1892 pa_log("[%s:%u] Enumeration makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
1896 if (pa_streq(state
->rvalue
, "ignore"))
1897 e
->enumeration_use
= PA_ALSA_ENUMERATION_IGNORE
;
1898 else if (pa_streq(state
->rvalue
, "select"))
1899 e
->enumeration_use
= PA_ALSA_ENUMERATION_SELECT
;
1901 pa_log("[%s:%u] Enumeration invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
1908 static int option_parse_priority(pa_config_parser_state
*state
) {
1915 p
= state
->userdata
;
1917 if (!(o
= option_get(p
, state
->section
))) {
1918 pa_log("[%s:%u] Priority makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
1922 if (pa_atou(state
->rvalue
, &prio
) < 0) {
1923 pa_log("[%s:%u] Priority invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
1931 static int option_parse_name(pa_config_parser_state
*state
) {
1937 p
= state
->userdata
;
1939 if (!(o
= option_get(p
, state
->section
))) {
1940 pa_log("[%s:%u] Name makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
1945 o
->name
= pa_xstrdup(state
->rvalue
);
1950 static int element_parse_required(pa_config_parser_state
*state
) {
1955 pa_alsa_required_t req
;
1959 p
= state
->userdata
;
1961 e
= element_get(p
, state
->section
, TRUE
);
1962 o
= option_get(p
, state
->section
);
1963 j
= jack_get(p
, state
->section
);
1964 if (!e
&& !o
&& !j
) {
1965 pa_log("[%s:%u] Required makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
1969 if (pa_streq(state
->rvalue
, "ignore"))
1970 req
= PA_ALSA_REQUIRED_IGNORE
;
1971 else if (pa_streq(state
->rvalue
, "switch") && e
)
1972 req
= PA_ALSA_REQUIRED_SWITCH
;
1973 else if (pa_streq(state
->rvalue
, "volume") && e
)
1974 req
= PA_ALSA_REQUIRED_VOLUME
;
1975 else if (pa_streq(state
->rvalue
, "enumeration"))
1976 req
= PA_ALSA_REQUIRED_ENUMERATION
;
1977 else if (pa_streq(state
->rvalue
, "any"))
1978 req
= PA_ALSA_REQUIRED_ANY
;
1980 pa_log("[%s:%u] Required invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
1984 if (pa_streq(state
->lvalue
, "required-absent")) {
1986 e
->required_absent
= req
;
1988 o
->required_absent
= req
;
1990 j
->required_absent
= req
;
1992 else if (pa_streq(state
->lvalue
, "required-any")) {
1994 e
->required_any
= req
;
1995 e
->path
->has_req_any
|= (req
!= PA_ALSA_REQUIRED_IGNORE
);
1998 o
->required_any
= req
;
1999 o
->element
->path
->has_req_any
|= (req
!= PA_ALSA_REQUIRED_IGNORE
);
2002 j
->required_any
= req
;
2003 j
->path
->has_req_any
|= (req
!= PA_ALSA_REQUIRED_IGNORE
);
2019 static int element_parse_direction(pa_config_parser_state
*state
) {
2025 p
= state
->userdata
;
2027 if (!(e
= element_get(p
, state
->section
, TRUE
))) {
2028 pa_log("[%s:%u] Direction makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
2032 if (pa_streq(state
->rvalue
, "playback"))
2033 e
->direction
= PA_ALSA_DIRECTION_OUTPUT
;
2034 else if (pa_streq(state
->rvalue
, "capture"))
2035 e
->direction
= PA_ALSA_DIRECTION_INPUT
;
2037 pa_log("[%s:%u] Direction invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
2044 static int element_parse_direction_try_other(pa_config_parser_state
*state
) {
2051 p
= state
->userdata
;
2053 if (!(e
= element_get(p
, state
->section
, TRUE
))) {
2054 pa_log("[%s:%u] Direction makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
2058 if ((yes
= pa_parse_boolean(state
->rvalue
)) < 0) {
2059 pa_log("[%s:%u] Direction invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
2063 e
->direction_try_other
= !!yes
;
2067 static int element_parse_volume_limit(pa_config_parser_state
*state
) {
2074 p
= state
->userdata
;
2076 if (!(e
= element_get(p
, state
->section
, TRUE
))) {
2077 pa_log("[%s:%u] volume-limit makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
2081 if (pa_atol(state
->rvalue
, &volume_limit
) < 0 || volume_limit
< 0) {
2082 pa_log("[%s:%u] Invalid value for volume-limit", state
->filename
, state
->lineno
);
2086 e
->volume_limit
= volume_limit
;
2090 static pa_channel_position_mask_t
parse_mask(const char *m
) {
2091 pa_channel_position_mask_t v
;
2093 if (pa_streq(m
, "all-left"))
2094 v
= PA_CHANNEL_POSITION_MASK_LEFT
;
2095 else if (pa_streq(m
, "all-right"))
2096 v
= PA_CHANNEL_POSITION_MASK_RIGHT
;
2097 else if (pa_streq(m
, "all-center"))
2098 v
= PA_CHANNEL_POSITION_MASK_CENTER
;
2099 else if (pa_streq(m
, "all-front"))
2100 v
= PA_CHANNEL_POSITION_MASK_FRONT
;
2101 else if (pa_streq(m
, "all-rear"))
2102 v
= PA_CHANNEL_POSITION_MASK_REAR
;
2103 else if (pa_streq(m
, "all-side"))
2104 v
= PA_CHANNEL_POSITION_MASK_SIDE_OR_TOP_CENTER
;
2105 else if (pa_streq(m
, "all-top"))
2106 v
= PA_CHANNEL_POSITION_MASK_TOP
;
2107 else if (pa_streq(m
, "all-no-lfe"))
2108 v
= PA_CHANNEL_POSITION_MASK_ALL
^ PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_LFE
);
2109 else if (pa_streq(m
, "all"))
2110 v
= PA_CHANNEL_POSITION_MASK_ALL
;
2112 pa_channel_position_t p
;
2114 if ((p
= pa_channel_position_from_string(m
)) == PA_CHANNEL_POSITION_INVALID
)
2117 v
= PA_CHANNEL_POSITION_MASK(p
);
2123 static int element_parse_override_map(pa_config_parser_state
*state
) {
2126 const char *split_state
= NULL
;
2132 p
= state
->userdata
;
2134 if (!(e
= element_get(p
, state
->section
, TRUE
))) {
2135 pa_log("[%s:%u] Override map makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
2139 while ((n
= pa_split(state
->rvalue
, ",", &split_state
))) {
2140 pa_channel_position_mask_t m
;
2145 if ((m
= parse_mask(n
)) == 0) {
2146 pa_log("[%s:%u] Override map '%s' invalid in '%s'", state
->filename
, state
->lineno
, n
, state
->section
);
2152 if (pa_streq(state
->lvalue
, "override-map.1"))
2153 e
->masks
[i
++][0] = m
;
2155 e
->masks
[i
++][1] = m
;
2157 /* Later on we might add override-map.3 and so on here ... */
2162 e
->override_map
= TRUE
;
2167 static int jack_parse_state(pa_config_parser_state
*state
) {
2170 pa_port_available_t pa
;
2174 p
= state
->userdata
;
2176 if (!(j
= jack_get(p
, state
->section
))) {
2177 pa_log("[%s:%u] state makes no sense in '%s'", state
->filename
, state
->lineno
, state
->section
);
2181 if (pa_streq(state
->rvalue
, "yes"))
2182 pa
= PA_PORT_AVAILABLE_YES
;
2183 else if (pa_streq(state
->rvalue
, "no"))
2184 pa
= PA_PORT_AVAILABLE_NO
;
2185 else if (pa_streq(state
->rvalue
, "unknown"))
2186 pa
= PA_PORT_AVAILABLE_UNKNOWN
;
2188 pa_log("[%s:%u] state must be 'yes', 'no' or 'unknown' in '%s'", state
->filename
, state
->lineno
, state
->section
);
2192 if (pa_streq(state
->lvalue
, "state.unplugged"))
2193 j
->state_unplugged
= pa
;
2195 j
->state_plugged
= pa
;
2196 pa_assert(pa_streq(state
->lvalue
, "state.plugged"));
2202 static int element_set_option(pa_alsa_element
*e
, snd_mixer_t
*m
, int alsa_idx
) {
2203 snd_mixer_selem_id_t
*sid
;
2204 snd_mixer_elem_t
*me
;
2210 SELEM_INIT(sid
, e
->alsa_name
);
2211 if (!(me
= snd_mixer_find_selem(m
, sid
))) {
2212 pa_log_warn("Element %s seems to have disappeared.", e
->alsa_name
);
2216 if (e
->switch_use
== PA_ALSA_SWITCH_SELECT
) {
2218 if (e
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
2219 r
= snd_mixer_selem_set_playback_switch_all(me
, alsa_idx
);
2221 r
= snd_mixer_selem_set_capture_switch_all(me
, alsa_idx
);
2224 pa_log_warn("Failed to set switch of %s: %s", e
->alsa_name
, pa_alsa_strerror(errno
));
2227 pa_assert(e
->enumeration_use
== PA_ALSA_ENUMERATION_SELECT
);
2229 if ((r
= snd_mixer_selem_set_enum_item(me
, 0, alsa_idx
)) < 0)
2230 pa_log_warn("Failed to set enumeration of %s: %s", e
->alsa_name
, pa_alsa_strerror(errno
));
2236 static int setting_select(pa_alsa_setting
*s
, snd_mixer_t
*m
) {
2243 PA_IDXSET_FOREACH(o
, s
->options
, idx
)
2244 element_set_option(o
->element
, m
, o
->alsa_idx
);
2249 static int option_verify(pa_alsa_option
*o
) {
2250 static const struct description_map well_known_descriptions
[] = {
2251 { "input", N_("Input") },
2252 { "input-docking", N_("Docking Station Input") },
2253 { "input-docking-microphone", N_("Docking Station Microphone") },
2254 { "input-docking-linein", N_("Docking Station Line In") },
2255 { "input-linein", N_("Line In") },
2256 { "input-microphone", N_("Microphone") },
2257 { "input-microphone-front", N_("Front Microphone") },
2258 { "input-microphone-rear", N_("Rear Microphone") },
2259 { "input-microphone-external", N_("External Microphone") },
2260 { "input-microphone-internal", N_("Internal Microphone") },
2261 { "input-radio", N_("Radio") },
2262 { "input-video", N_("Video") },
2263 { "input-agc-on", N_("Automatic Gain Control") },
2264 { "input-agc-off", N_("No Automatic Gain Control") },
2265 { "input-boost-on", N_("Boost") },
2266 { "input-boost-off", N_("No Boost") },
2267 { "output-amplifier-on", N_("Amplifier") },
2268 { "output-amplifier-off", N_("No Amplifier") },
2269 { "output-bass-boost-on", N_("Bass Boost") },
2270 { "output-bass-boost-off", N_("No Bass Boost") },
2271 { "output-speaker", N_("Speaker") },
2272 { "output-headphones", N_("Headphones") }
2278 pa_log("No name set for option %s", o
->alsa_name
);
2282 if (o
->element
->enumeration_use
!= PA_ALSA_ENUMERATION_SELECT
&&
2283 o
->element
->switch_use
!= PA_ALSA_SWITCH_SELECT
) {
2284 pa_log("Element %s of option %s not set for select.", o
->element
->alsa_name
, o
->name
);
2288 if (o
->element
->switch_use
== PA_ALSA_SWITCH_SELECT
&&
2289 !pa_streq(o
->alsa_name
, "on") &&
2290 !pa_streq(o
->alsa_name
, "off")) {
2291 pa_log("Switch %s options need be named off or on ", o
->element
->alsa_name
);
2295 if (!o
->description
)
2296 o
->description
= pa_xstrdup(lookup_description(o
->name
,
2297 well_known_descriptions
,
2298 PA_ELEMENTSOF(well_known_descriptions
)));
2299 if (!o
->description
)
2300 o
->description
= pa_xstrdup(o
->name
);
2305 static int element_verify(pa_alsa_element
*e
) {
2310 // pa_log_debug("Element %s, path %s: r=%d, r-any=%d, r-abs=%d", e->alsa_name, e->path->name, e->required, e->required_any, e->required_absent);
2311 if ((e
->required
!= PA_ALSA_REQUIRED_IGNORE
&& e
->required
== e
->required_absent
) ||
2312 (e
->required_any
!= PA_ALSA_REQUIRED_IGNORE
&& e
->required_any
== e
->required_absent
) ||
2313 (e
->required_absent
== PA_ALSA_REQUIRED_ANY
&& e
->required_any
!= PA_ALSA_REQUIRED_IGNORE
) ||
2314 (e
->required_absent
== PA_ALSA_REQUIRED_ANY
&& e
->required
!= PA_ALSA_REQUIRED_IGNORE
)) {
2315 pa_log("Element %s cannot be required and absent at the same time.", e
->alsa_name
);
2319 if (e
->switch_use
== PA_ALSA_SWITCH_SELECT
&& e
->enumeration_use
== PA_ALSA_ENUMERATION_SELECT
) {
2320 pa_log("Element %s cannot set select for both switch and enumeration.", e
->alsa_name
);
2324 PA_LLIST_FOREACH(o
, e
->options
)
2325 if (option_verify(o
) < 0)
2331 static int path_verify(pa_alsa_path
*p
) {
2332 static const struct description_map well_known_descriptions
[] = {
2333 { "analog-input", N_("Analog Input") },
2334 { "analog-input-microphone", N_("Microphone") },
2335 { "analog-input-microphone-front", N_("Front Microphone") },
2336 { "analog-input-microphone-rear", N_("Rear Microphone") },
2337 { "analog-input-microphone-dock", N_("Dock Microphone") },
2338 { "analog-input-microphone-internal", N_("Internal Microphone") },
2339 { "analog-input-linein", N_("Line In") },
2340 { "analog-input-radio", N_("Radio") },
2341 { "analog-input-video", N_("Video") },
2342 { "analog-output", N_("Analog Output") },
2343 { "analog-output-headphones", N_("Headphones") },
2344 { "analog-output-lfe-on-mono", N_("LFE on Separate Mono Output") },
2345 { "analog-output-lineout", N_("Line Out") },
2346 { "analog-output-mono", N_("Analog Mono Output") },
2347 { "analog-output-speaker", N_("Speakers") },
2348 { "hdmi-output", N_("HDMI / DisplayPort") },
2349 { "iec958-stereo-output", N_("Digital Output (S/PDIF)") },
2350 { "iec958-passthrough-output", N_("Digital Passthrough (S/PDIF)") }
2357 PA_LLIST_FOREACH(e
, p
->elements
)
2358 if (element_verify(e
) < 0)
2361 if (!p
->description
)
2362 p
->description
= pa_xstrdup(lookup_description(p
->name
,
2363 well_known_descriptions
,
2364 PA_ELEMENTSOF(well_known_descriptions
)));
2366 if (!p
->description
)
2367 p
->description
= pa_xstrdup(p
->name
);
2372 static const char *get_default_paths_dir(void) {
2373 if (pa_run_from_build_tree())
2374 return PA_BUILDDIR
"/modules/alsa/mixer/paths/";
2376 return PA_ALSA_PATHS_DIR
;
2379 pa_alsa_path
* pa_alsa_path_new(const char *paths_dir
, const char *fname
, pa_alsa_direction_t direction
) {
2384 bool mute_during_activation
= false;
2386 pa_config_item items
[] = {
2388 { "priority", pa_config_parse_unsigned
, NULL
, "General" },
2389 { "description", pa_config_parse_string
, NULL
, "General" },
2390 { "name", pa_config_parse_string
, NULL
, "General" },
2391 { "mute-during-activation", pa_config_parse_bool
, NULL
, "General" },
2394 { "priority", option_parse_priority
, NULL
, NULL
},
2395 { "name", option_parse_name
, NULL
, NULL
},
2398 { "state.plugged", jack_parse_state
, NULL
, NULL
},
2399 { "state.unplugged", jack_parse_state
, NULL
, NULL
},
2402 { "switch", element_parse_switch
, NULL
, NULL
},
2403 { "volume", element_parse_volume
, NULL
, NULL
},
2404 { "enumeration", element_parse_enumeration
, NULL
, NULL
},
2405 { "override-map.1", element_parse_override_map
, NULL
, NULL
},
2406 { "override-map.2", element_parse_override_map
, NULL
, NULL
},
2407 /* ... later on we might add override-map.3 and so on here ... */
2408 { "required", element_parse_required
, NULL
, NULL
},
2409 { "required-any", element_parse_required
, NULL
, NULL
},
2410 { "required-absent", element_parse_required
, NULL
, NULL
},
2411 { "direction", element_parse_direction
, NULL
, NULL
},
2412 { "direction-try-other", element_parse_direction_try_other
, NULL
, NULL
},
2413 { "volume-limit", element_parse_volume_limit
, NULL
, NULL
},
2414 { NULL
, NULL
, NULL
, NULL
}
2419 p
= pa_xnew0(pa_alsa_path
, 1);
2420 n
= pa_path_get_filename(fname
);
2421 p
->name
= pa_xstrndup(n
, strcspn(n
, "."));
2422 p
->proplist
= pa_proplist_new();
2423 p
->direction
= direction
;
2425 items
[0].data
= &p
->priority
;
2426 items
[1].data
= &p
->description
;
2427 items
[2].data
= &p
->name
;
2428 items
[3].data
= &mute_during_activation
;
2431 paths_dir
= get_default_paths_dir();
2433 fn
= pa_maybe_prefix_path(fname
, paths_dir
);
2435 r
= pa_config_parse(fn
, NULL
, items
, p
->proplist
, p
);
2441 p
->mute_during_activation
= mute_during_activation
;
2443 if (path_verify(p
) < 0)
2449 pa_alsa_path_free(p
);
2453 pa_alsa_path
*pa_alsa_path_synthesize(const char *element
, pa_alsa_direction_t direction
) {
2459 p
= pa_xnew0(pa_alsa_path
, 1);
2460 p
->name
= pa_xstrdup(element
);
2461 p
->direction
= direction
;
2463 e
= pa_xnew0(pa_alsa_element
, 1);
2465 e
->alsa_name
= pa_xstrdup(element
);
2466 e
->direction
= direction
;
2467 e
->volume_limit
= -1;
2469 e
->switch_use
= PA_ALSA_SWITCH_MUTE
;
2470 e
->volume_use
= PA_ALSA_VOLUME_MERGE
;
2472 PA_LLIST_PREPEND(pa_alsa_element
, p
->elements
, e
);
2473 p
->last_element
= e
;
2477 static pa_bool_t
element_drop_unsupported(pa_alsa_element
*e
) {
2478 pa_alsa_option
*o
, *n
;
2482 for (o
= e
->options
; o
; o
= n
) {
2485 if (o
->alsa_idx
< 0) {
2486 PA_LLIST_REMOVE(pa_alsa_option
, e
->options
, o
);
2492 e
->switch_use
!= PA_ALSA_SWITCH_IGNORE
||
2493 e
->volume_use
!= PA_ALSA_VOLUME_IGNORE
||
2494 e
->enumeration_use
!= PA_ALSA_ENUMERATION_IGNORE
;
2497 static void path_drop_unsupported(pa_alsa_path
*p
) {
2498 pa_alsa_element
*e
, *n
;
2502 for (e
= p
->elements
; e
; e
= n
) {
2505 if (!element_drop_unsupported(e
)) {
2506 PA_LLIST_REMOVE(pa_alsa_element
, p
->elements
, e
);
2512 static void path_make_options_unique(pa_alsa_path
*p
) {
2514 pa_alsa_option
*o
, *u
;
2516 PA_LLIST_FOREACH(e
, p
->elements
) {
2517 PA_LLIST_FOREACH(o
, e
->options
) {
2521 for (u
= o
->next
; u
; u
= u
->next
)
2522 if (pa_streq(u
->name
, o
->name
))
2528 m
= pa_xstrdup(o
->name
);
2530 /* OK, this name is not unique, hence let's rename */
2531 for (i
= 1, u
= o
; u
; u
= u
->next
) {
2534 if (!pa_streq(u
->name
, m
))
2537 nn
= pa_sprintf_malloc("%s-%u", m
, i
);
2541 nd
= pa_sprintf_malloc("%s %u", u
->description
, i
);
2542 pa_xfree(u
->description
);
2543 u
->description
= nd
;
2553 static pa_bool_t
element_create_settings(pa_alsa_element
*e
, pa_alsa_setting
*template) {
2556 for (; e
; e
= e
->next
)
2557 if (e
->switch_use
== PA_ALSA_SWITCH_SELECT
||
2558 e
->enumeration_use
== PA_ALSA_ENUMERATION_SELECT
)
2564 for (o
= e
->options
; o
; o
= o
->next
) {
2568 s
= pa_xnewdup(pa_alsa_setting
, template, 1);
2569 s
->options
= pa_idxset_copy(template->options
);
2570 s
->name
= pa_sprintf_malloc("%s+%s", template->name
, o
->name
);
2572 (template->description
[0] && o
->description
[0])
2573 ? pa_sprintf_malloc("%s / %s", template->description
, o
->description
)
2574 : (template->description
[0]
2575 ? pa_xstrdup(template->description
)
2576 : pa_xstrdup(o
->description
));
2578 s
->priority
= PA_MAX(template->priority
, o
->priority
);
2580 s
= pa_xnew0(pa_alsa_setting
, 1);
2581 s
->options
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
2582 s
->name
= pa_xstrdup(o
->name
);
2583 s
->description
= pa_xstrdup(o
->description
);
2584 s
->priority
= o
->priority
;
2587 pa_idxset_put(s
->options
, o
, NULL
);
2589 if (element_create_settings(e
->next
, s
))
2590 /* This is not a leaf, so let's get rid of it */
2593 /* This is a leaf, so let's add it */
2594 PA_LLIST_INSERT_AFTER(pa_alsa_setting
, e
->path
->settings
, e
->path
->last_setting
, s
);
2596 e
->path
->last_setting
= s
;
2603 static void path_create_settings(pa_alsa_path
*p
) {
2606 element_create_settings(p
->elements
, NULL
);
2609 int pa_alsa_path_probe(pa_alsa_path
*p
, snd_mixer_t
*m
, snd_hctl_t
*hctl
, pa_bool_t ignore_dB
) {
2612 double min_dB
[PA_CHANNEL_POSITION_MAX
], max_dB
[PA_CHANNEL_POSITION_MAX
];
2613 pa_channel_position_t t
;
2614 pa_channel_position_mask_t path_volume_channels
= 0;
2620 return p
->supported
? 0 : -1;
2626 pa_log_debug("Probing path '%s'", p
->name
);
2628 PA_LLIST_FOREACH(j
, p
->jacks
) {
2629 if (jack_probe(j
, hctl
) < 0) {
2630 p
->supported
= FALSE
;
2631 pa_log_debug("Probe of jack '%s' failed.", j
->alsa_name
);
2634 pa_log_debug("Probe of jack '%s' succeeded (%s)", j
->alsa_name
, j
->has_control
? "found!" : "not found");
2637 PA_LLIST_FOREACH(e
, p
->elements
) {
2638 if (element_probe(e
, m
) < 0) {
2639 p
->supported
= FALSE
;
2640 pa_log_debug("Probe of element '%s' failed.", e
->alsa_name
);
2643 pa_log_debug("Probe of element '%s' succeeded (volume=%d, switch=%d, enumeration=%d).", e
->alsa_name
, e
->volume_use
, e
->switch_use
, e
->enumeration_use
);
2648 if (e
->volume_use
== PA_ALSA_VOLUME_MERGE
) {
2650 if (!p
->has_volume
) {
2651 p
->min_volume
= e
->min_volume
;
2652 p
->max_volume
= e
->max_volume
;
2656 if (!p
->has_volume
) {
2657 for (t
= 0; t
< PA_CHANNEL_POSITION_MAX
; t
++)
2658 if (PA_CHANNEL_POSITION_MASK(t
) & e
->merged_mask
) {
2659 min_dB
[t
] = e
->min_dB
;
2660 max_dB
[t
] = e
->max_dB
;
2661 path_volume_channels
|= PA_CHANNEL_POSITION_MASK(t
);
2668 for (t
= 0; t
< PA_CHANNEL_POSITION_MAX
; t
++)
2669 if (PA_CHANNEL_POSITION_MASK(t
) & e
->merged_mask
) {
2670 min_dB
[t
] += e
->min_dB
;
2671 max_dB
[t
] += e
->max_dB
;
2672 path_volume_channels
|= PA_CHANNEL_POSITION_MASK(t
);
2675 /* Hmm, there's another element before us
2676 * which cannot do dB volumes, so we we need
2677 * to 'neutralize' this slider */
2678 e
->volume_use
= PA_ALSA_VOLUME_ZERO
;
2679 pa_log_info("Zeroing volume of '%s' on path '%s'", e
->alsa_name
, p
->name
);
2682 } else if (p
->has_volume
) {
2683 /* We can't use this volume, so let's ignore it */
2684 e
->volume_use
= PA_ALSA_VOLUME_IGNORE
;
2685 pa_log_info("Ignoring volume of '%s' on path '%s' (missing dB info)", e
->alsa_name
, p
->name
);
2687 p
->has_volume
= TRUE
;
2690 if (e
->switch_use
== PA_ALSA_SWITCH_MUTE
)
2694 if (p
->has_req_any
&& !p
->req_any_present
) {
2695 p
->supported
= FALSE
;
2696 pa_log_debug("Skipping path '%s', none of required-any elements preset.", p
->name
);
2700 path_drop_unsupported(p
);
2701 path_make_options_unique(p
);
2702 path_create_settings(p
);
2704 p
->supported
= TRUE
;
2706 p
->min_dB
= INFINITY
;
2707 p
->max_dB
= -INFINITY
;
2709 for (t
= 0; t
< PA_CHANNEL_POSITION_MAX
; t
++) {
2710 if (path_volume_channels
& PA_CHANNEL_POSITION_MASK(t
)) {
2711 if (p
->min_dB
> min_dB
[t
])
2712 p
->min_dB
= min_dB
[t
];
2714 if (p
->max_dB
< max_dB
[t
])
2715 p
->max_dB
= max_dB
[t
];
2722 void pa_alsa_setting_dump(pa_alsa_setting
*s
) {
2725 pa_log_debug("Setting %s (%s) priority=%u",
2727 pa_strnull(s
->description
),
2731 void pa_alsa_jack_dump(pa_alsa_jack
*j
) {
2734 pa_log_debug("Jack %s, alsa_name='%s', detection %s", j
->name
, j
->alsa_name
, j
->has_control
? "possible" : "unavailable");
2737 void pa_alsa_option_dump(pa_alsa_option
*o
) {
2740 pa_log_debug("Option %s (%s/%s) index=%i, priority=%u",
2742 pa_strnull(o
->name
),
2743 pa_strnull(o
->description
),
2748 void pa_alsa_element_dump(pa_alsa_element
*e
) {
2752 pa_log_debug("Element %s, direction=%i, switch=%i, volume=%i, volume_limit=%li, enumeration=%i, required=%i, required_any=%i, required_absent=%i, mask=0x%llx, n_channels=%u, override_map=%s",
2762 (long long unsigned) e
->merged_mask
,
2764 pa_yes_no(e
->override_map
));
2766 PA_LLIST_FOREACH(o
, e
->options
)
2767 pa_alsa_option_dump(o
);
2770 void pa_alsa_path_dump(pa_alsa_path
*p
) {
2776 pa_log_debug("Path %s (%s), direction=%i, priority=%u, probed=%s, supported=%s, has_mute=%s, has_volume=%s, "
2777 "has_dB=%s, min_volume=%li, max_volume=%li, min_dB=%g, max_dB=%g",
2779 pa_strnull(p
->description
),
2782 pa_yes_no(p
->probed
),
2783 pa_yes_no(p
->supported
),
2784 pa_yes_no(p
->has_mute
),
2785 pa_yes_no(p
->has_volume
),
2786 pa_yes_no(p
->has_dB
),
2787 p
->min_volume
, p
->max_volume
,
2788 p
->min_dB
, p
->max_dB
);
2790 PA_LLIST_FOREACH(e
, p
->elements
)
2791 pa_alsa_element_dump(e
);
2793 PA_LLIST_FOREACH(j
, p
->jacks
)
2794 pa_alsa_jack_dump(j
);
2796 PA_LLIST_FOREACH(s
, p
->settings
)
2797 pa_alsa_setting_dump(s
);
2800 static void element_set_callback(pa_alsa_element
*e
, snd_mixer_t
*m
, snd_mixer_elem_callback_t cb
, void *userdata
) {
2801 snd_mixer_selem_id_t
*sid
;
2802 snd_mixer_elem_t
*me
;
2808 SELEM_INIT(sid
, e
->alsa_name
);
2809 if (!(me
= snd_mixer_find_selem(m
, sid
))) {
2810 pa_log_warn("Element %s seems to have disappeared.", e
->alsa_name
);
2814 snd_mixer_elem_set_callback(me
, cb
);
2815 snd_mixer_elem_set_callback_private(me
, userdata
);
2818 void pa_alsa_path_set_callback(pa_alsa_path
*p
, snd_mixer_t
*m
, snd_mixer_elem_callback_t cb
, void *userdata
) {
2825 PA_LLIST_FOREACH(e
, p
->elements
)
2826 element_set_callback(e
, m
, cb
, userdata
);
2829 void pa_alsa_path_set_set_callback(pa_alsa_path_set
*ps
, snd_mixer_t
*m
, snd_mixer_elem_callback_t cb
, void *userdata
) {
2837 PA_HASHMAP_FOREACH(p
, ps
->paths
, state
)
2838 pa_alsa_path_set_callback(p
, m
, cb
, userdata
);
2841 pa_alsa_path_set
*pa_alsa_path_set_new(pa_alsa_mapping
*m
, pa_alsa_direction_t direction
, const char *paths_dir
) {
2842 pa_alsa_path_set
*ps
;
2843 char **pn
= NULL
, **en
= NULL
, **ie
;
2844 pa_alsa_decibel_fix
*db_fix
;
2845 void *state
, *state2
;
2849 pa_assert(m
->profile_set
);
2850 pa_assert(m
->profile_set
->decibel_fixes
);
2851 pa_assert(direction
== PA_ALSA_DIRECTION_OUTPUT
|| direction
== PA_ALSA_DIRECTION_INPUT
);
2853 if (m
->direction
!= PA_ALSA_DIRECTION_ANY
&& m
->direction
!= direction
)
2856 ps
= pa_xnew0(pa_alsa_path_set
, 1);
2857 ps
->direction
= direction
;
2858 ps
->paths
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
2860 if (direction
== PA_ALSA_DIRECTION_OUTPUT
) {
2861 pn
= m
->output_path_names
;
2862 cache
= m
->profile_set
->output_paths
;
2864 else if (direction
== PA_ALSA_DIRECTION_INPUT
) {
2865 pn
= m
->input_path_names
;
2866 cache
= m
->profile_set
->input_paths
;
2872 for (in
= pn
; *in
; in
++) {
2873 pa_alsa_path
*p
= NULL
;
2874 pa_bool_t duplicate
= FALSE
;
2877 for (kn
= pn
; kn
< in
; kn
++)
2878 if (pa_streq(*kn
, *in
)) {
2886 p
= pa_hashmap_get(cache
, *in
);
2888 char *fn
= pa_sprintf_malloc("%s.conf", *in
);
2889 p
= pa_alsa_path_new(paths_dir
, fn
, direction
);
2892 pa_hashmap_put(cache
, *in
, p
);
2894 pa_assert(pa_hashmap_get(cache
, *in
) == p
);
2896 pa_hashmap_put(ps
->paths
, p
, p
);
2903 if (direction
== PA_ALSA_DIRECTION_OUTPUT
)
2904 en
= m
->output_element
;
2905 else if (direction
== PA_ALSA_DIRECTION_INPUT
)
2906 en
= m
->input_element
;
2909 pa_alsa_path_set_free(ps
);
2913 for (ie
= en
; *ie
; ie
++) {
2917 p
= pa_alsa_path_synthesize(*ie
, direction
);
2919 /* Mark all other passed elements for require-absent */
2920 for (je
= en
; *je
; je
++) {
2926 e
= pa_xnew0(pa_alsa_element
, 1);
2928 e
->alsa_name
= pa_xstrdup(*je
);
2929 e
->direction
= direction
;
2930 e
->required_absent
= PA_ALSA_REQUIRED_ANY
;
2931 e
->volume_limit
= -1;
2933 PA_LLIST_INSERT_AFTER(pa_alsa_element
, p
->elements
, p
->last_element
, e
);
2934 p
->last_element
= e
;
2937 pa_hashmap_put(ps
->paths
, *ie
, p
);
2941 /* Assign decibel fixes to elements. */
2942 PA_HASHMAP_FOREACH(db_fix
, m
->profile_set
->decibel_fixes
, state
) {
2945 PA_HASHMAP_FOREACH(p
, ps
->paths
, state2
) {
2948 PA_LLIST_FOREACH(e
, p
->elements
) {
2949 if (e
->volume_use
!= PA_ALSA_VOLUME_IGNORE
&& pa_streq(db_fix
->name
, e
->alsa_name
)) {
2950 /* The profile set that contains the dB fix may be freed
2951 * before the element, so we have to copy the dB fix
2953 e
->db_fix
= pa_xnewdup(pa_alsa_decibel_fix
, db_fix
, 1);
2954 e
->db_fix
->profile_set
= NULL
;
2955 e
->db_fix
->name
= pa_xstrdup(db_fix
->name
);
2956 e
->db_fix
->db_values
= pa_xmemdup(db_fix
->db_values
, (db_fix
->max_step
- db_fix
->min_step
+ 1) * sizeof(long));
2965 void pa_alsa_path_set_dump(pa_alsa_path_set
*ps
) {
2970 pa_log_debug("Path Set %p, direction=%i",
2974 PA_HASHMAP_FOREACH(p
, ps
->paths
, state
)
2975 pa_alsa_path_dump(p
);
2979 static pa_bool_t
options_have_option(pa_alsa_option
*options
, const char *alsa_name
) {
2983 pa_assert(alsa_name
);
2985 PA_LLIST_FOREACH(o
, options
) {
2986 if (pa_streq(o
->alsa_name
, alsa_name
))
2992 static pa_bool_t
enumeration_is_subset(pa_alsa_option
*a_options
, pa_alsa_option
*b_options
) {
2993 pa_alsa_option
*oa
, *ob
;
2995 if (!a_options
) return TRUE
;
2996 if (!b_options
) return FALSE
;
2998 /* If there is an option A offers that B does not, then A is not a subset of B. */
2999 PA_LLIST_FOREACH(oa
, a_options
) {
3000 pa_bool_t found
= FALSE
;
3001 PA_LLIST_FOREACH(ob
, b_options
) {
3002 if (pa_streq(oa
->alsa_name
, ob
->alsa_name
)) {
3014 * Compares two elements to see if a is a subset of b
3016 static pa_bool_t
element_is_subset(pa_alsa_element
*a
, pa_alsa_element
*b
, snd_mixer_t
*m
) {
3022 * Every state is a subset of itself (with caveats for volume_limits and options)
3023 * IGNORE is a subset of every other state */
3025 /* Check the volume_use */
3026 if (a
->volume_use
!= PA_ALSA_VOLUME_IGNORE
) {
3028 /* "Constant" is subset of "Constant" only when their constant values are equal */
3029 if (a
->volume_use
== PA_ALSA_VOLUME_CONSTANT
&& b
->volume_use
== PA_ALSA_VOLUME_CONSTANT
&& a
->constant_volume
!= b
->constant_volume
)
3032 /* Different volume uses when b is not "Merge" means we are definitely not a subset */
3033 if (a
->volume_use
!= b
->volume_use
&& b
->volume_use
!= PA_ALSA_VOLUME_MERGE
)
3036 /* "Constant" is a subset of "Merge", if there is not a "volume-limit" in "Merge" below the actual constant.
3037 * "Zero" and "Off" are just special cases of "Constant" when comparing to "Merge"
3038 * "Merge" with a "volume-limit" is a subset of "Merge" without a "volume-limit" or with a higher "volume-limit" */
3039 if (b
->volume_use
== PA_ALSA_VOLUME_MERGE
&& b
->volume_limit
>= 0) {
3042 if (a
->volume_use
== PA_ALSA_VOLUME_CONSTANT
)
3043 a_limit
= a
->constant_volume
;
3044 else if (a
->volume_use
== PA_ALSA_VOLUME_ZERO
) {
3048 int rounding
= (a
->direction
== PA_ALSA_DIRECTION_OUTPUT
? +1 : -1);
3049 a_limit
= decibel_fix_get_step(a
->db_fix
, &dB
, rounding
);
3051 snd_mixer_selem_id_t
*sid
;
3052 snd_mixer_elem_t
*me
;
3054 SELEM_INIT(sid
, a
->alsa_name
);
3055 if (!(me
= snd_mixer_find_selem(m
, sid
))) {
3056 pa_log_warn("Element %s seems to have disappeared.", a
->alsa_name
);
3060 if (a
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
3061 if (snd_mixer_selem_ask_playback_dB_vol(me
, dB
, +1, &a_limit
) < 0)
3064 if (snd_mixer_selem_ask_capture_dB_vol(me
, dB
, -1, &a_limit
) < 0)
3068 } else if (a
->volume_use
== PA_ALSA_VOLUME_OFF
)
3069 a_limit
= a
->min_volume
;
3070 else if (a
->volume_use
== PA_ALSA_VOLUME_MERGE
)
3071 a_limit
= a
->volume_limit
;
3073 /* This should never be reached */
3076 if (a_limit
> b
->volume_limit
)
3080 if (a
->volume_use
== PA_ALSA_VOLUME_MERGE
) {
3082 /* If override-maps are different, they're not subsets */
3083 if (a
->n_channels
!= b
->n_channels
)
3085 for (s
= 0; s
<= SND_MIXER_SCHN_LAST
; s
++)
3086 if (a
->masks
[s
][a
->n_channels
-1] != b
->masks
[s
][b
->n_channels
-1]) {
3087 pa_log_debug("Element %s is not a subset - mask a: 0x%" PRIx64
", mask b: 0x%" PRIx64
", at channel %d",
3088 a
->alsa_name
, a
->masks
[s
][a
->n_channels
-1], b
->masks
[s
][b
->n_channels
-1], s
);
3094 if (a
->switch_use
!= PA_ALSA_SWITCH_IGNORE
) {
3095 /* "On" is a subset of "Mute".
3096 * "Off" is a subset of "Mute".
3097 * "On" is a subset of "Select", if there is an "Option:On" in B.
3098 * "Off" is a subset of "Select", if there is an "Option:Off" in B.
3099 * "Select" is a subset of "Select", if they have the same options (is this always true?). */
3101 if (a
->switch_use
!= b
->switch_use
) {
3103 if (a
->switch_use
== PA_ALSA_SWITCH_SELECT
|| a
->switch_use
== PA_ALSA_SWITCH_MUTE
3104 || b
->switch_use
== PA_ALSA_SWITCH_OFF
|| b
->switch_use
== PA_ALSA_SWITCH_ON
)
3107 if (b
->switch_use
== PA_ALSA_SWITCH_SELECT
) {
3108 if (a
->switch_use
== PA_ALSA_SWITCH_ON
) {
3109 if (!options_have_option(b
->options
, "on"))
3111 } else if (a
->switch_use
== PA_ALSA_SWITCH_OFF
) {
3112 if (!options_have_option(b
->options
, "off"))
3116 } else if (a
->switch_use
== PA_ALSA_SWITCH_SELECT
) {
3117 if (!enumeration_is_subset(a
->options
, b
->options
))
3122 if (a
->enumeration_use
!= PA_ALSA_ENUMERATION_IGNORE
) {
3123 if (b
->enumeration_use
== PA_ALSA_ENUMERATION_IGNORE
)
3125 if (!enumeration_is_subset(a
->options
, b
->options
))
3132 static void path_set_condense(pa_alsa_path_set
*ps
, snd_mixer_t
*m
) {
3139 /* If we only have one path, then don't bother */
3140 if (pa_hashmap_size(ps
->paths
) < 2)
3143 PA_HASHMAP_FOREACH(p
, ps
->paths
, state
) {
3147 PA_HASHMAP_FOREACH(p2
, ps
->paths
, state2
) {
3148 pa_alsa_element
*ea
, *eb
;
3149 pa_alsa_jack
*ja
, *jb
;
3150 pa_bool_t is_subset
= TRUE
;
3155 /* If a has a jack that b does not have, a is not a subset */
3156 PA_LLIST_FOREACH(ja
, p
->jacks
) {
3157 pa_bool_t exists
= FALSE
;
3159 if (!ja
->has_control
)
3162 PA_LLIST_FOREACH(jb
, p2
->jacks
) {
3163 if (jb
->has_control
&& pa_streq(jb
->alsa_name
, ja
->alsa_name
) &&
3164 (ja
->state_plugged
== jb
->state_plugged
) &&
3165 (ja
->state_unplugged
== jb
->state_unplugged
)) {
3177 /* Compare the elements of each set... */
3178 pa_assert_se(ea
= p
->elements
);
3179 pa_assert_se(eb
= p2
->elements
);
3182 if (pa_streq(ea
->alsa_name
, eb
->alsa_name
)) {
3183 if (element_is_subset(ea
, eb
, m
)) {
3186 if ((ea
&& !eb
) || (!ea
&& eb
))
3188 else if (!ea
&& !eb
)
3198 pa_log_debug("Removing path '%s' as it is a subset of '%s'.", p
->name
, p2
->name
);
3199 pa_hashmap_remove(ps
->paths
, p
);
3206 static pa_alsa_path
* path_set_find_path_by_name(pa_alsa_path_set
*ps
, const char* name
, pa_alsa_path
*ignore
)
3211 PA_HASHMAP_FOREACH(p
, ps
->paths
, state
)
3212 if (p
!= ignore
&& pa_streq(p
->name
, name
))
3217 static void path_set_make_paths_unique(pa_alsa_path_set
*ps
) {
3218 pa_alsa_path
*p
, *q
;
3219 void *state
, *state2
;
3221 PA_HASHMAP_FOREACH(p
, ps
->paths
, state
) {
3225 q
= path_set_find_path_by_name(ps
, p
->name
, p
);
3230 m
= pa_xstrdup(p
->name
);
3232 /* OK, this name is not unique, hence let's rename */
3234 PA_HASHMAP_FOREACH(q
, ps
->paths
, state2
) {
3237 if (!pa_streq(q
->name
, m
))
3240 nn
= pa_sprintf_malloc("%s-%u", m
, i
);
3244 nd
= pa_sprintf_malloc("%s %u", q
->description
, i
);
3245 pa_xfree(q
->description
);
3246 q
->description
= nd
;
3255 static void mapping_free(pa_alsa_mapping
*m
) {
3259 pa_xfree(m
->description
);
3261 pa_xstrfreev(m
->device_strings
);
3262 pa_xstrfreev(m
->input_path_names
);
3263 pa_xstrfreev(m
->output_path_names
);
3264 pa_xstrfreev(m
->input_element
);
3265 pa_xstrfreev(m
->output_element
);
3266 if (m
->input_path_set
)
3267 pa_alsa_path_set_free(m
->input_path_set
);
3268 if (m
->output_path_set
)
3269 pa_alsa_path_set_free(m
->output_path_set
);
3271 pa_assert(!m
->input_pcm
);
3272 pa_assert(!m
->output_pcm
);
3274 pa_alsa_ucm_mapping_context_free(&m
->ucm_context
);
3279 static void profile_free(pa_alsa_profile
*p
) {
3283 pa_xfree(p
->description
);
3285 pa_xstrfreev(p
->input_mapping_names
);
3286 pa_xstrfreev(p
->output_mapping_names
);
3288 if (p
->input_mappings
)
3289 pa_idxset_free(p
->input_mappings
, NULL
, NULL
);
3291 if (p
->output_mappings
)
3292 pa_idxset_free(p
->output_mappings
, NULL
, NULL
);
3297 void pa_alsa_profile_set_free(pa_alsa_profile_set
*ps
) {
3300 if (ps
->input_paths
) {
3303 while ((p
= pa_hashmap_steal_first(ps
->input_paths
)))
3304 pa_alsa_path_free(p
);
3306 pa_hashmap_free(ps
->input_paths
, NULL
, NULL
);
3309 if (ps
->output_paths
) {
3312 while ((p
= pa_hashmap_steal_first(ps
->output_paths
)))
3313 pa_alsa_path_free(p
);
3315 pa_hashmap_free(ps
->output_paths
, NULL
, NULL
);
3321 while ((p
= pa_hashmap_steal_first(ps
->profiles
)))
3324 pa_hashmap_free(ps
->profiles
, NULL
, NULL
);
3330 while ((m
= pa_hashmap_steal_first(ps
->mappings
)))
3333 pa_hashmap_free(ps
->mappings
, NULL
, NULL
);
3336 if (ps
->decibel_fixes
) {
3337 pa_alsa_decibel_fix
*db_fix
;
3339 while ((db_fix
= pa_hashmap_steal_first(ps
->decibel_fixes
)))
3340 decibel_fix_free(db_fix
);
3342 pa_hashmap_free(ps
->decibel_fixes
, NULL
, NULL
);
3348 pa_alsa_mapping
*pa_alsa_mapping_get(pa_alsa_profile_set
*ps
, const char *name
) {
3351 if (!pa_startswith(name
, "Mapping "))
3356 if ((m
= pa_hashmap_get(ps
->mappings
, name
)))
3359 m
= pa_xnew0(pa_alsa_mapping
, 1);
3360 m
->profile_set
= ps
;
3361 m
->name
= pa_xstrdup(name
);
3362 pa_channel_map_init(&m
->channel_map
);
3364 pa_hashmap_put(ps
->mappings
, m
->name
, m
);
3369 static pa_alsa_profile
*profile_get(pa_alsa_profile_set
*ps
, const char *name
) {
3372 if (!pa_startswith(name
, "Profile "))
3377 if ((p
= pa_hashmap_get(ps
->profiles
, name
)))
3380 p
= pa_xnew0(pa_alsa_profile
, 1);
3381 p
->profile_set
= ps
;
3382 p
->name
= pa_xstrdup(name
);
3384 pa_hashmap_put(ps
->profiles
, p
->name
, p
);
3389 static pa_alsa_decibel_fix
*decibel_fix_get(pa_alsa_profile_set
*ps
, const char *name
) {
3390 pa_alsa_decibel_fix
*db_fix
;
3392 if (!pa_startswith(name
, "DecibelFix "))
3397 if ((db_fix
= pa_hashmap_get(ps
->decibel_fixes
, name
)))
3400 db_fix
= pa_xnew0(pa_alsa_decibel_fix
, 1);
3401 db_fix
->profile_set
= ps
;
3402 db_fix
->name
= pa_xstrdup(name
);
3404 pa_hashmap_put(ps
->decibel_fixes
, db_fix
->name
, db_fix
);
3409 static int mapping_parse_device_strings(pa_config_parser_state
*state
) {
3410 pa_alsa_profile_set
*ps
;
3415 ps
= state
->userdata
;
3417 if (!(m
= pa_alsa_mapping_get(ps
, state
->section
))) {
3418 pa_log("[%s:%u] %s invalid in section %s", state
->filename
, state
->lineno
, state
->lvalue
, state
->section
);
3422 pa_xstrfreev(m
->device_strings
);
3423 if (!(m
->device_strings
= pa_split_spaces_strv(state
->rvalue
))) {
3424 pa_log("[%s:%u] Device string list empty of '%s'", state
->filename
, state
->lineno
, state
->section
);
3431 static int mapping_parse_channel_map(pa_config_parser_state
*state
) {
3432 pa_alsa_profile_set
*ps
;
3437 ps
= state
->userdata
;
3439 if (!(m
= pa_alsa_mapping_get(ps
, state
->section
))) {
3440 pa_log("[%s:%u] %s invalid in section %s", state
->filename
, state
->lineno
, state
->lvalue
, state
->section
);
3444 if (!(pa_channel_map_parse(&m
->channel_map
, state
->rvalue
))) {
3445 pa_log("[%s:%u] Channel map invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
3452 static int mapping_parse_paths(pa_config_parser_state
*state
) {
3453 pa_alsa_profile_set
*ps
;
3458 ps
= state
->userdata
;
3460 if (!(m
= pa_alsa_mapping_get(ps
, state
->section
))) {
3461 pa_log("[%s:%u] %s invalid in section %s", state
->filename
, state
->lineno
, state
->lvalue
, state
->section
);
3465 if (pa_streq(state
->lvalue
, "paths-input")) {
3466 pa_xstrfreev(m
->input_path_names
);
3467 m
->input_path_names
= pa_split_spaces_strv(state
->rvalue
);
3469 pa_xstrfreev(m
->output_path_names
);
3470 m
->output_path_names
= pa_split_spaces_strv(state
->rvalue
);
3476 static int mapping_parse_element(pa_config_parser_state
*state
) {
3477 pa_alsa_profile_set
*ps
;
3482 ps
= state
->userdata
;
3484 if (!(m
= pa_alsa_mapping_get(ps
, state
->section
))) {
3485 pa_log("[%s:%u] %s invalid in section %s", state
->filename
, state
->lineno
, state
->lvalue
, state
->section
);
3489 if (pa_streq(state
->lvalue
, "element-input")) {
3490 pa_xstrfreev(m
->input_element
);
3491 m
->input_element
= pa_split_spaces_strv(state
->rvalue
);
3493 pa_xstrfreev(m
->output_element
);
3494 m
->output_element
= pa_split_spaces_strv(state
->rvalue
);
3500 static int mapping_parse_direction(pa_config_parser_state
*state
) {
3501 pa_alsa_profile_set
*ps
;
3506 ps
= state
->userdata
;
3508 if (!(m
= pa_alsa_mapping_get(ps
, state
->section
))) {
3509 pa_log("[%s:%u] Section name %s invalid.", state
->filename
, state
->lineno
, state
->section
);
3513 if (pa_streq(state
->rvalue
, "input"))
3514 m
->direction
= PA_ALSA_DIRECTION_INPUT
;
3515 else if (pa_streq(state
->rvalue
, "output"))
3516 m
->direction
= PA_ALSA_DIRECTION_OUTPUT
;
3517 else if (pa_streq(state
->rvalue
, "any"))
3518 m
->direction
= PA_ALSA_DIRECTION_ANY
;
3520 pa_log("[%s:%u] Direction %s invalid.", state
->filename
, state
->lineno
, state
->rvalue
);
3527 static int mapping_parse_description(pa_config_parser_state
*state
) {
3528 pa_alsa_profile_set
*ps
;
3534 ps
= state
->userdata
;
3536 if ((m
= pa_alsa_mapping_get(ps
, state
->section
))) {
3537 pa_xfree(m
->description
);
3538 m
->description
= pa_xstrdup(state
->rvalue
);
3539 } else if ((p
= profile_get(ps
, state
->section
))) {
3540 pa_xfree(p
->description
);
3541 p
->description
= pa_xstrdup(state
->rvalue
);
3543 pa_log("[%s:%u] Section name %s invalid.", state
->filename
, state
->lineno
, state
->section
);
3550 static int mapping_parse_priority(pa_config_parser_state
*state
) {
3551 pa_alsa_profile_set
*ps
;
3558 ps
= state
->userdata
;
3560 if (pa_atou(state
->rvalue
, &prio
) < 0) {
3561 pa_log("[%s:%u] Priority invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
3565 if ((m
= pa_alsa_mapping_get(ps
, state
->section
)))
3567 else if ((p
= profile_get(ps
, state
->section
)))
3570 pa_log("[%s:%u] Section name %s invalid.", state
->filename
, state
->lineno
, state
->section
);
3577 static int profile_parse_mappings(pa_config_parser_state
*state
) {
3578 pa_alsa_profile_set
*ps
;
3583 ps
= state
->userdata
;
3585 if (!(p
= profile_get(ps
, state
->section
))) {
3586 pa_log("[%s:%u] %s invalid in section %s", state
->filename
, state
->lineno
, state
->lvalue
, state
->section
);
3590 if (pa_streq(state
->lvalue
, "input-mappings")) {
3591 pa_xstrfreev(p
->input_mapping_names
);
3592 p
->input_mapping_names
= pa_split_spaces_strv(state
->rvalue
);
3594 pa_xstrfreev(p
->output_mapping_names
);
3595 p
->output_mapping_names
= pa_split_spaces_strv(state
->rvalue
);
3601 static int profile_parse_skip_probe(pa_config_parser_state
*state
) {
3602 pa_alsa_profile_set
*ps
;
3608 ps
= state
->userdata
;
3610 if (!(p
= profile_get(ps
, state
->section
))) {
3611 pa_log("[%s:%u] %s invalid in section %s", state
->filename
, state
->lineno
, state
->lvalue
, state
->section
);
3615 if ((b
= pa_parse_boolean(state
->rvalue
)) < 0) {
3616 pa_log("[%s:%u] Skip probe invalid of '%s'", state
->filename
, state
->lineno
, state
->section
);
3625 static int decibel_fix_parse_db_values(pa_config_parser_state
*state
) {
3626 pa_alsa_profile_set
*ps
;
3627 pa_alsa_decibel_fix
*db_fix
;
3631 unsigned n
= 8; /* Current size of the db_values table. */
3632 unsigned min_step
= 0;
3633 unsigned max_step
= 0;
3634 unsigned i
= 0; /* Index to the items table. */
3635 unsigned prev_step
= 0;
3640 ps
= state
->userdata
;
3642 if (!(db_fix
= decibel_fix_get(ps
, state
->section
))) {
3643 pa_log("[%s:%u] %s invalid in section %s", state
->filename
, state
->lineno
, state
->lvalue
, state
->section
);
3647 if (!(items
= pa_split_spaces_strv(state
->rvalue
))) {
3648 pa_log("[%s:%u] Value missing", state
->filename
, state
->lineno
);
3652 db_values
= pa_xnew(long, n
);
3654 while ((item
= items
[i
++])) {
3655 char *s
= item
; /* Step value string. */
3656 char *d
= item
; /* dB value string. */
3660 /* Move d forward until it points to a colon or to the end of the item. */
3661 for (; *d
&& *d
!= ':'; ++d
);
3664 /* item started with colon. */
3665 pa_log("[%s:%u] No step value found in %s", state
->filename
, state
->lineno
, item
);
3669 if (!*d
|| !*(d
+ 1)) {
3670 /* No colon found, or it was the last character in item. */
3671 pa_log("[%s:%u] No dB value found in %s", state
->filename
, state
->lineno
, item
);
3675 /* pa_atou() needs a null-terminating string. Let's replace the colon
3676 * with a zero byte. */
3679 if (pa_atou(s
, &step
) < 0) {
3680 pa_log("[%s:%u] Invalid step value: %s", state
->filename
, state
->lineno
, s
);
3684 if (pa_atod(d
, &db
) < 0) {
3685 pa_log("[%s:%u] Invalid dB value: %s", state
->filename
, state
->lineno
, d
);
3689 if (step
<= prev_step
&& i
!= 1) {
3690 pa_log("[%s:%u] Step value %u not greater than the previous value %u", state
->filename
, state
->lineno
, step
, prev_step
);
3694 if (db
< prev_db
&& i
!= 1) {
3695 pa_log("[%s:%u] Decibel value %0.2f less than the previous value %0.2f", state
->filename
, state
->lineno
, db
, prev_db
);
3701 db_values
[0] = (long) (db
* 100.0);
3705 /* Interpolate linearly. */
3706 double db_increment
= (db
- prev_db
) / (step
- prev_step
);
3708 for (; prev_step
< step
; ++prev_step
, prev_db
+= db_increment
) {
3710 /* Reallocate the db_values table if it's about to overflow. */
3711 if (prev_step
+ 1 - min_step
== n
) {
3713 db_values
= pa_xrenew(long, db_values
, n
);
3716 db_values
[prev_step
+ 1 - min_step
] = (long) ((prev_db
+ db_increment
) * 100.0);
3723 db_fix
->min_step
= min_step
;
3724 db_fix
->max_step
= max_step
;
3725 pa_xfree(db_fix
->db_values
);
3726 db_fix
->db_values
= db_values
;
3728 pa_xstrfreev(items
);
3733 pa_xstrfreev(items
);
3734 pa_xfree(db_values
);
3739 static void mapping_paths_probe(pa_alsa_mapping
*m
, pa_alsa_profile
*profile
,
3740 pa_alsa_direction_t direction
) {
3744 snd_pcm_t
*pcm_handle
;
3745 pa_alsa_path_set
*ps
;
3746 snd_mixer_t
*mixer_handle
;
3747 snd_hctl_t
*hctl_handle
;
3749 if (direction
== PA_ALSA_DIRECTION_OUTPUT
) {
3750 if (m
->output_path_set
)
3751 return; /* Already probed */
3752 m
->output_path_set
= ps
= pa_alsa_path_set_new(m
, direction
, NULL
); /* FIXME: Handle paths_dir */
3753 pcm_handle
= m
->output_pcm
;
3755 if (m
->input_path_set
)
3756 return; /* Already probed */
3757 m
->input_path_set
= ps
= pa_alsa_path_set_new(m
, direction
, NULL
); /* FIXME: Handle paths_dir */
3758 pcm_handle
= m
->input_pcm
;
3762 return; /* No paths */
3764 pa_assert(pcm_handle
);
3766 mixer_handle
= pa_alsa_open_mixer_for_pcm(pcm_handle
, NULL
, &hctl_handle
);
3767 if (!mixer_handle
|| !hctl_handle
) {
3768 /* Cannot open mixer, remove all entries */
3769 while (pa_hashmap_steal_first(ps
->paths
));
3774 PA_HASHMAP_FOREACH(p
, ps
->paths
, state
) {
3775 if (pa_alsa_path_probe(p
, mixer_handle
, hctl_handle
, m
->profile_set
->ignore_dB
) < 0) {
3776 pa_hashmap_remove(ps
->paths
, p
);
3780 path_set_condense(ps
, mixer_handle
);
3781 path_set_make_paths_unique(ps
);
3784 snd_mixer_close(mixer_handle
);
3786 pa_log_debug("Available mixer paths (after tidying):");
3787 pa_alsa_path_set_dump(ps
);
3790 static int mapping_verify(pa_alsa_mapping
*m
, const pa_channel_map
*bonus
) {
3792 static const struct description_map well_known_descriptions
[] = {
3793 { "analog-mono", N_("Analog Mono") },
3794 { "analog-stereo", N_("Analog Stereo") },
3795 { "analog-surround-21", N_("Analog Surround 2.1") },
3796 { "analog-surround-30", N_("Analog Surround 3.0") },
3797 { "analog-surround-31", N_("Analog Surround 3.1") },
3798 { "analog-surround-40", N_("Analog Surround 4.0") },
3799 { "analog-surround-41", N_("Analog Surround 4.1") },
3800 { "analog-surround-50", N_("Analog Surround 5.0") },
3801 { "analog-surround-51", N_("Analog Surround 5.1") },
3802 { "analog-surround-61", N_("Analog Surround 6.0") },
3803 { "analog-surround-61", N_("Analog Surround 6.1") },
3804 { "analog-surround-70", N_("Analog Surround 7.0") },
3805 { "analog-surround-71", N_("Analog Surround 7.1") },
3806 { "analog-4-channel-input", N_("Analog 4-channel Input") },
3807 { "iec958-stereo", N_("Digital Stereo (IEC958)") },
3808 { "iec958-passthrough", N_("Digital Passthrough (IEC958)") },
3809 { "iec958-ac3-surround-40", N_("Digital Surround 4.0 (IEC958/AC3)") },
3810 { "iec958-ac3-surround-51", N_("Digital Surround 5.1 (IEC958/AC3)") },
3811 { "iec958-dts-surround-51", N_("Digital Surround 5.1 (IEC958/DTS)") },
3812 { "hdmi-stereo", N_("Digital Stereo (HDMI)") },
3813 { "hdmi-surround-51", N_("Digital Surround 5.1 (HDMI)") }
3818 if (!pa_channel_map_valid(&m
->channel_map
)) {
3819 pa_log("Mapping %s is missing channel map.", m
->name
);
3823 if (!m
->device_strings
) {
3824 pa_log("Mapping %s is missing device strings.", m
->name
);
3828 if ((m
->input_path_names
&& m
->input_element
) ||
3829 (m
->output_path_names
&& m
->output_element
)) {
3830 pa_log("Mapping %s must have either mixer path or mixer element, not both.", m
->name
);
3834 if (!m
->description
)
3835 m
->description
= pa_xstrdup(lookup_description(m
->name
,
3836 well_known_descriptions
,
3837 PA_ELEMENTSOF(well_known_descriptions
)));
3839 if (!m
->description
)
3840 m
->description
= pa_xstrdup(m
->name
);
3843 if (pa_channel_map_equal(&m
->channel_map
, bonus
))
3845 else if (m
->channel_map
.channels
== bonus
->channels
)
3852 void pa_alsa_mapping_dump(pa_alsa_mapping
*m
) {
3853 char cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
3857 pa_log_debug("Mapping %s (%s), priority=%u, channel_map=%s, supported=%s, direction=%i",
3859 pa_strnull(m
->description
),
3861 pa_channel_map_snprint(cm
, sizeof(cm
), &m
->channel_map
),
3862 pa_yes_no(m
->supported
),
3866 static void profile_set_add_auto_pair(
3867 pa_alsa_profile_set
*ps
,
3868 pa_alsa_mapping
*m
, /* output */
3869 pa_alsa_mapping
*n
/* input */) {
3877 if (m
&& m
->direction
== PA_ALSA_DIRECTION_INPUT
)
3880 if (n
&& n
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
3884 name
= pa_sprintf_malloc("output:%s+input:%s", m
->name
, n
->name
);
3886 name
= pa_sprintf_malloc("output:%s", m
->name
);
3888 name
= pa_sprintf_malloc("input:%s", n
->name
);
3890 if (pa_hashmap_get(ps
->profiles
, name
)) {
3895 p
= pa_xnew0(pa_alsa_profile
, 1);
3896 p
->profile_set
= ps
;
3900 p
->output_mappings
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
3901 pa_idxset_put(p
->output_mappings
, m
, NULL
);
3902 p
->priority
+= m
->priority
* 100;
3906 p
->input_mappings
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
3907 pa_idxset_put(p
->input_mappings
, n
, NULL
);
3908 p
->priority
+= n
->priority
;
3911 pa_hashmap_put(ps
->profiles
, p
->name
, p
);
3914 static void profile_set_add_auto(pa_alsa_profile_set
*ps
) {
3915 pa_alsa_mapping
*m
, *n
;
3916 void *m_state
, *n_state
;
3920 PA_HASHMAP_FOREACH(m
, ps
->mappings
, m_state
) {
3921 profile_set_add_auto_pair(ps
, m
, NULL
);
3923 PA_HASHMAP_FOREACH(n
, ps
->mappings
, n_state
)
3924 profile_set_add_auto_pair(ps
, m
, n
);
3927 PA_HASHMAP_FOREACH(n
, ps
->mappings
, n_state
)
3928 profile_set_add_auto_pair(ps
, NULL
, n
);
3931 static int profile_verify(pa_alsa_profile
*p
) {
3933 static const struct description_map well_known_descriptions
[] = {
3934 { "output:analog-mono+input:analog-mono", N_("Analog Mono Duplex") },
3935 { "output:analog-stereo+input:analog-stereo", N_("Analog Stereo Duplex") },
3936 { "output:iec958-stereo+input:iec958-stereo", N_("Digital Stereo Duplex (IEC958)") },
3937 { "off", N_("Off") }
3942 /* Replace the output mapping names by the actual mappings */
3943 if (p
->output_mapping_names
) {
3946 pa_assert(!p
->output_mappings
);
3947 p
->output_mappings
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
3949 for (name
= p
->output_mapping_names
; *name
; name
++) {
3952 pa_bool_t duplicate
= FALSE
;
3954 for (in
= name
+ 1; *in
; in
++)
3955 if (pa_streq(*name
, *in
)) {
3963 if (!(m
= pa_hashmap_get(p
->profile_set
->mappings
, *name
)) || m
->direction
== PA_ALSA_DIRECTION_INPUT
) {
3964 pa_log("Profile '%s' refers to nonexistent mapping '%s'.", p
->name
, *name
);
3968 pa_idxset_put(p
->output_mappings
, m
, NULL
);
3974 pa_xstrfreev(p
->output_mapping_names
);
3975 p
->output_mapping_names
= NULL
;
3978 /* Replace the input mapping names by the actual mappings */
3979 if (p
->input_mapping_names
) {
3982 pa_assert(!p
->input_mappings
);
3983 p
->input_mappings
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
3985 for (name
= p
->input_mapping_names
; *name
; name
++) {
3988 pa_bool_t duplicate
= FALSE
;
3990 for (in
= name
+ 1; *in
; in
++)
3991 if (pa_streq(*name
, *in
)) {
3999 if (!(m
= pa_hashmap_get(p
->profile_set
->mappings
, *name
)) || m
->direction
== PA_ALSA_DIRECTION_OUTPUT
) {
4000 pa_log("Profile '%s' refers to nonexistent mapping '%s'.", p
->name
, *name
);
4004 pa_idxset_put(p
->input_mappings
, m
, NULL
);
4010 pa_xstrfreev(p
->input_mapping_names
);
4011 p
->input_mapping_names
= NULL
;
4014 if (!p
->input_mappings
&& !p
->output_mappings
) {
4015 pa_log("Profile '%s' lacks mappings.", p
->name
);
4019 if (!p
->description
)
4020 p
->description
= pa_xstrdup(lookup_description(p
->name
,
4021 well_known_descriptions
,
4022 PA_ELEMENTSOF(well_known_descriptions
)));
4024 if (!p
->description
) {
4029 sb
= pa_strbuf_new();
4031 if (p
->output_mappings
)
4032 PA_IDXSET_FOREACH(m
, p
->output_mappings
, idx
) {
4033 if (!pa_strbuf_isempty(sb
))
4034 pa_strbuf_puts(sb
, " + ");
4036 pa_strbuf_printf(sb
, _("%s Output"), m
->description
);
4039 if (p
->input_mappings
)
4040 PA_IDXSET_FOREACH(m
, p
->input_mappings
, idx
) {
4041 if (!pa_strbuf_isempty(sb
))
4042 pa_strbuf_puts(sb
, " + ");
4044 pa_strbuf_printf(sb
, _("%s Input"), m
->description
);
4047 p
->description
= pa_strbuf_tostring_free(sb
);
4053 void pa_alsa_profile_dump(pa_alsa_profile
*p
) {
4058 pa_log_debug("Profile %s (%s), priority=%u, supported=%s n_input_mappings=%u, n_output_mappings=%u",
4060 pa_strnull(p
->description
),
4062 pa_yes_no(p
->supported
),
4063 p
->input_mappings
? pa_idxset_size(p
->input_mappings
) : 0,
4064 p
->output_mappings
? pa_idxset_size(p
->output_mappings
) : 0);
4066 if (p
->input_mappings
)
4067 PA_IDXSET_FOREACH(m
, p
->input_mappings
, idx
)
4068 pa_log_debug("Input %s", m
->name
);
4070 if (p
->output_mappings
)
4071 PA_IDXSET_FOREACH(m
, p
->output_mappings
, idx
)
4072 pa_log_debug("Output %s", m
->name
);
4075 static int decibel_fix_verify(pa_alsa_decibel_fix
*db_fix
) {
4078 /* Check that the dB mapping has been configured. Since "db-values" is
4079 * currently the only option in the DecibelFix section, and decibel fix
4080 * objects don't get created if a DecibelFix section is empty, this is
4081 * actually a redundant check. Having this may prevent future bugs,
4083 if (!db_fix
->db_values
) {
4084 pa_log("Decibel fix for element %s lacks the dB values.", db_fix
->name
);
4091 void pa_alsa_decibel_fix_dump(pa_alsa_decibel_fix
*db_fix
) {
4092 char *db_values
= NULL
;
4096 if (db_fix
->db_values
) {
4098 unsigned long i
, nsteps
;
4100 pa_assert(db_fix
->min_step
<= db_fix
->max_step
);
4101 nsteps
= db_fix
->max_step
- db_fix
->min_step
+ 1;
4103 buf
= pa_strbuf_new();
4104 for (i
= 0; i
< nsteps
; ++i
)
4105 pa_strbuf_printf(buf
, "[%li]:%0.2f ", i
+ db_fix
->min_step
, db_fix
->db_values
[i
] / 100.0);
4107 db_values
= pa_strbuf_tostring_free(buf
);
4110 pa_log_debug("Decibel fix %s, min_step=%li, max_step=%li, db_values=%s",
4111 db_fix
->name
, db_fix
->min_step
, db_fix
->max_step
, pa_strnull(db_values
));
4113 pa_xfree(db_values
);
4116 pa_alsa_profile_set
* pa_alsa_profile_set_new(const char *fname
, const pa_channel_map
*bonus
) {
4117 pa_alsa_profile_set
*ps
;
4120 pa_alsa_decibel_fix
*db_fix
;
4125 static pa_config_item items
[] = {
4127 { "auto-profiles", pa_config_parse_bool
, NULL
, "General" },
4130 { "device-strings", mapping_parse_device_strings
, NULL
, NULL
},
4131 { "channel-map", mapping_parse_channel_map
, NULL
, NULL
},
4132 { "paths-input", mapping_parse_paths
, NULL
, NULL
},
4133 { "paths-output", mapping_parse_paths
, NULL
, NULL
},
4134 { "element-input", mapping_parse_element
, NULL
, NULL
},
4135 { "element-output", mapping_parse_element
, NULL
, NULL
},
4136 { "direction", mapping_parse_direction
, NULL
, NULL
},
4138 /* Shared by [Mapping ...] and [Profile ...] */
4139 { "description", mapping_parse_description
, NULL
, NULL
},
4140 { "priority", mapping_parse_priority
, NULL
, NULL
},
4143 { "input-mappings", profile_parse_mappings
, NULL
, NULL
},
4144 { "output-mappings", profile_parse_mappings
, NULL
, NULL
},
4145 { "skip-probe", profile_parse_skip_probe
, NULL
, NULL
},
4147 /* [DecibelFix ...] */
4148 { "db-values", decibel_fix_parse_db_values
, NULL
, NULL
},
4149 { NULL
, NULL
, NULL
, NULL
}
4152 ps
= pa_xnew0(pa_alsa_profile_set
, 1);
4153 ps
->mappings
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
4154 ps
->profiles
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
4155 ps
->decibel_fixes
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
4156 ps
->input_paths
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
4157 ps
->output_paths
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
4159 items
[0].data
= &ps
->auto_profiles
;
4162 fname
= "default.conf";
4164 fn
= pa_maybe_prefix_path(fname
,
4165 pa_run_from_build_tree() ? PA_BUILDDIR
"/modules/alsa/mixer/profile-sets/" :
4166 PA_ALSA_PROFILE_SETS_DIR
);
4168 r
= pa_config_parse(fn
, NULL
, items
, NULL
, ps
);
4174 PA_HASHMAP_FOREACH(m
, ps
->mappings
, state
)
4175 if (mapping_verify(m
, bonus
) < 0)
4178 if (ps
->auto_profiles
)
4179 profile_set_add_auto(ps
);
4181 PA_HASHMAP_FOREACH(p
, ps
->profiles
, state
)
4182 if (profile_verify(p
) < 0)
4185 PA_HASHMAP_FOREACH(db_fix
, ps
->decibel_fixes
, state
)
4186 if (decibel_fix_verify(db_fix
) < 0)
4192 pa_alsa_profile_set_free(ps
);
4196 static void profile_finalize_probing(pa_alsa_profile
*to_be_finalized
, pa_alsa_profile
*next
) {
4200 if (!to_be_finalized
)
4203 if (to_be_finalized
->output_mappings
)
4204 PA_IDXSET_FOREACH(m
, to_be_finalized
->output_mappings
, idx
) {
4209 if (to_be_finalized
->supported
)
4212 /* If this mapping is also in the next profile, we won't close the
4213 * pcm handle here, because it would get immediately reopened
4215 if (next
&& next
->output_mappings
&& pa_idxset_get_by_data(next
->output_mappings
, m
, NULL
))
4218 snd_pcm_close(m
->output_pcm
);
4219 m
->output_pcm
= NULL
;
4222 if (to_be_finalized
->input_mappings
)
4223 PA_IDXSET_FOREACH(m
, to_be_finalized
->input_mappings
, idx
) {
4228 if (to_be_finalized
->supported
)
4231 /* If this mapping is also in the next profile, we won't close the
4232 * pcm handle here, because it would get immediately reopened
4234 if (next
&& next
->input_mappings
&& pa_idxset_get_by_data(next
->input_mappings
, m
, NULL
))
4237 snd_pcm_close(m
->input_pcm
);
4238 m
->input_pcm
= NULL
;
4242 static snd_pcm_t
* mapping_open_pcm(pa_alsa_mapping
*m
,
4243 const pa_sample_spec
*ss
,
4246 unsigned default_n_fragments
,
4247 unsigned default_fragment_size_msec
) {
4249 pa_sample_spec try_ss
= *ss
;
4250 pa_channel_map try_map
= m
->channel_map
;
4251 snd_pcm_uframes_t try_period_size
, try_buffer_size
;
4253 try_ss
.channels
= try_map
.channels
;
4256 pa_usec_to_bytes(default_fragment_size_msec
* PA_USEC_PER_MSEC
, &try_ss
) /
4257 pa_frame_size(&try_ss
);
4258 try_buffer_size
= default_n_fragments
* try_period_size
;
4260 return pa_alsa_open_by_template(
4261 m
->device_strings
, dev_id
, NULL
, &try_ss
,
4262 &try_map
, mode
, &try_period_size
,
4263 &try_buffer_size
, 0, NULL
, NULL
, TRUE
);
4266 static void paths_drop_unsupported(pa_hashmap
* h
) {
4273 p
= pa_hashmap_iterate(h
, &state
, &key
);
4275 if (p
->supported
<= 0) {
4276 pa_hashmap_remove(h
, key
);
4277 pa_alsa_path_free(p
);
4279 p
= pa_hashmap_iterate(h
, &state
, &key
);
4283 void pa_alsa_profile_set_probe(
4284 pa_alsa_profile_set
*ps
,
4286 const pa_sample_spec
*ss
,
4287 unsigned default_n_fragments
,
4288 unsigned default_fragment_size_msec
) {
4291 pa_alsa_profile
*p
, *last
= NULL
;
4301 PA_HASHMAP_FOREACH(p
, ps
->profiles
, state
) {
4304 /* Skip if this is already marked that it is supported (i.e. from the config file) */
4305 if (!p
->supported
) {
4307 pa_log_debug("Looking at profile %s", p
->name
);
4308 profile_finalize_probing(last
, p
);
4309 p
->supported
= TRUE
;
4311 /* Check if we can open all new ones */
4312 if (p
->output_mappings
)
4313 PA_IDXSET_FOREACH(m
, p
->output_mappings
, idx
) {
4318 pa_log_debug("Checking for playback on %s (%s)", m
->description
, m
->name
);
4319 if (!(m
->output_pcm
= mapping_open_pcm(m
, ss
, dev_id
,
4320 SND_PCM_STREAM_PLAYBACK
,
4321 default_n_fragments
,
4322 default_fragment_size_msec
))) {
4323 p
->supported
= FALSE
;
4328 if (p
->input_mappings
&& p
->supported
)
4329 PA_IDXSET_FOREACH(m
, p
->input_mappings
, idx
) {
4334 pa_log_debug("Checking for recording on %s (%s)", m
->description
, m
->name
);
4335 if (!(m
->input_pcm
= mapping_open_pcm(m
, ss
, dev_id
,
4336 SND_PCM_STREAM_CAPTURE
,
4337 default_n_fragments
,
4338 default_fragment_size_msec
))) {
4339 p
->supported
= FALSE
;
4350 pa_log_debug("Profile %s supported.", p
->name
);
4352 if (p
->output_mappings
)
4353 PA_IDXSET_FOREACH(m
, p
->output_mappings
, idx
)
4355 mapping_paths_probe(m
, p
, PA_ALSA_DIRECTION_OUTPUT
);
4357 if (p
->input_mappings
)
4358 PA_IDXSET_FOREACH(m
, p
->input_mappings
, idx
)
4360 mapping_paths_probe(m
, p
, PA_ALSA_DIRECTION_INPUT
);
4364 profile_finalize_probing(last
, NULL
);
4366 pa_alsa_profile_set_drop_unsupported(ps
);
4368 paths_drop_unsupported(ps
->input_paths
);
4369 paths_drop_unsupported(ps
->output_paths
);
4374 void pa_alsa_profile_set_dump(pa_alsa_profile_set
*ps
) {
4377 pa_alsa_decibel_fix
*db_fix
;
4382 pa_log_debug("Profile set %p, auto_profiles=%s, probed=%s, n_mappings=%u, n_profiles=%u, n_decibel_fixes=%u",
4385 pa_yes_no(ps
->auto_profiles
),
4386 pa_yes_no(ps
->probed
),
4387 pa_hashmap_size(ps
->mappings
),
4388 pa_hashmap_size(ps
->profiles
),
4389 pa_hashmap_size(ps
->decibel_fixes
));
4391 PA_HASHMAP_FOREACH(m
, ps
->mappings
, state
)
4392 pa_alsa_mapping_dump(m
);
4394 PA_HASHMAP_FOREACH(p
, ps
->profiles
, state
)
4395 pa_alsa_profile_dump(p
);
4397 PA_HASHMAP_FOREACH(db_fix
, ps
->decibel_fixes
, state
)
4398 pa_alsa_decibel_fix_dump(db_fix
);
4401 void pa_alsa_profile_set_drop_unsupported(pa_alsa_profile_set
*ps
) {
4406 PA_HASHMAP_FOREACH(p
, ps
->profiles
, state
) {
4407 if (!p
->supported
) {
4408 pa_hashmap_remove(ps
->profiles
, p
->name
);
4413 PA_HASHMAP_FOREACH(m
, ps
->mappings
, state
) {
4414 if (m
->supported
<= 0) {
4415 pa_hashmap_remove(ps
->mappings
, m
->name
);
4421 static pa_device_port
* device_port_alsa_init(pa_hashmap
*ports
,
4423 const char* description
,
4425 pa_alsa_setting
*setting
,
4426 pa_card_profile
*cp
,
4434 p
= pa_hashmap_get(ports
, name
);
4437 pa_alsa_port_data
*data
;
4439 p
= pa_device_port_new(core
, name
, description
, sizeof(pa_alsa_port_data
));
4441 pa_hashmap_put(ports
, p
->name
, p
);
4442 pa_proplist_update(p
->proplist
, PA_UPDATE_REPLACE
, path
->proplist
);
4444 data
= PA_DEVICE_PORT_DATA(p
);
4446 data
->setting
= setting
;
4450 p
->is_input
|= path
->direction
== PA_ALSA_DIRECTION_ANY
|| path
->direction
== PA_ALSA_DIRECTION_INPUT
;
4451 p
->is_output
|= path
->direction
== PA_ALSA_DIRECTION_ANY
|| path
->direction
== PA_ALSA_DIRECTION_OUTPUT
;
4454 pa_hashmap_put(p
->profiles
, cp
->name
, cp
);
4457 pa_hashmap_put(extra
, p
->name
, p
);
4458 pa_device_port_ref(p
);
4464 void pa_alsa_path_set_add_ports(
4465 pa_alsa_path_set
*ps
,
4466 pa_card_profile
*cp
,
4479 PA_HASHMAP_FOREACH(path
, ps
->paths
, state
) {
4480 if (!path
->settings
|| !path
->settings
->next
) {
4481 /* If there is no or just one setting we only need a
4483 pa_device_port
*port
= device_port_alsa_init(ports
, path
->name
,
4484 path
->description
, path
, path
->settings
, cp
, extra
, core
);
4485 port
->priority
= path
->priority
* 100;
4489 PA_LLIST_FOREACH(s
, path
->settings
) {
4490 pa_device_port
*port
;
4493 n
= pa_sprintf_malloc("%s;%s", path
->name
, s
->name
);
4495 if (s
->description
[0])
4496 d
= pa_sprintf_malloc("%s / %s", path
->description
, s
->description
);
4498 d
= pa_xstrdup(path
->description
);
4500 port
= device_port_alsa_init(ports
, n
, d
, path
, s
, cp
, extra
, core
);
4501 port
->priority
= path
->priority
* 100 + s
->priority
;
4510 void pa_alsa_add_ports(void *sink_or_source_new_data
, pa_alsa_path_set
*ps
, pa_card
*card
) {
4513 pa_assert(sink_or_source_new_data
);
4516 if (ps
->direction
== PA_ALSA_DIRECTION_OUTPUT
)
4517 ports
= ((pa_sink_new_data
*) sink_or_source_new_data
)->ports
;
4519 ports
= ((pa_source_new_data
*) sink_or_source_new_data
)->ports
;
4521 if (ps
->paths
&& pa_hashmap_size(ps
->paths
) > 0) {
4523 pa_alsa_path_set_add_ports(ps
, NULL
, card
->ports
, ports
, card
->core
);
4526 pa_log_debug("Added %u ports", pa_hashmap_size(ports
));