diff --git a/src/modules/alsa/alsa-mixer.c b/src/modules/alsa/alsa-mixer.c index d184aec7a..f2fe20057 100644 --- a/src/modules/alsa/alsa-mixer.c +++ b/src/modules/alsa/alsa-mixer.c @@ -249,10 +249,23 @@ void pa_alsa_jack_set_plugged_in(pa_alsa_jack *jack, bool plugged_in) { } void pa_alsa_jack_add_ucm_device(pa_alsa_jack *jack, pa_alsa_ucm_device *device) { + pa_alsa_ucm_device *idevice; + unsigned idx, prio, iprio; + pa_assert(jack); pa_assert(device); - pa_dynarray_append(jack->ucm_devices, device); + /* store the ucm device with the sequence of priority from low to high. this + * could guarantee when the jack state is changed, the device with highest + * priority will send to the module-switch-on-port-available last */ + prio = device->playback_priority ? device->playback_priority : device->capture_priority; + + PA_DYNARRAY_FOREACH(idevice, jack->ucm_devices, idx) { + iprio = idevice->playback_priority ? idevice->playback_priority : idevice->capture_priority; + if (iprio > prio) + break; + } + pa_dynarray_insert_by_index(jack->ucm_devices, device, idx); } void pa_alsa_jack_add_ucm_hw_mute_device(pa_alsa_jack *jack, pa_alsa_ucm_device *device) { diff --git a/src/pulsecore/dynarray.c b/src/pulsecore/dynarray.c index 6a3eb5f04..a948f2671 100644 --- a/src/pulsecore/dynarray.c +++ b/src/pulsecore/dynarray.c @@ -140,3 +140,26 @@ unsigned pa_dynarray_size(pa_dynarray *array) { return array->n_entries; } + +int pa_dynarray_insert_by_index(pa_dynarray *array, void *p, unsigned i) { + void *entry; + unsigned j; + + pa_assert(array); + + if (i > array->n_entries) + return -PA_ERR_NOENTITY; + + if (i == array->n_entries) + pa_dynarray_append(array, p); + else { + entry = pa_dynarray_last(array); + pa_dynarray_append(array, entry); + j = array->n_entries - 2; + for (;j > i; j--) + array->data[j] = array->data[j-1]; + array->data[i] = p; + } + + return 0; +} diff --git a/src/pulsecore/dynarray.h b/src/pulsecore/dynarray.h index bf7dddc12..4c0925e7c 100644 --- a/src/pulsecore/dynarray.h +++ b/src/pulsecore/dynarray.h @@ -63,6 +63,10 @@ void *pa_dynarray_steal_last(pa_dynarray *array); unsigned pa_dynarray_size(pa_dynarray *array); +/* Returns -PA_ERR_NOENTITY if i is out of bounds, and zero otherwise. + * Here i is the location index in the array like 0, ..., array->entries */ +int pa_dynarray_insert_by_index(pa_dynarray *array, void *p, unsigned i); + #define PA_DYNARRAY_FOREACH(elem, array, idx) \ for ((idx) = 0; ((elem) = pa_dynarray_get(array, idx)); (idx)++)