From: gineera Date: Tue, 27 Aug 2013 17:14:51 +0000 (+0000) Subject: Alsa: Add function to prevent duplicate parts in the visible pa device name string... X-Git-Tag: pa_stable_v19_20140130_r1919~10 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=3029485a0e056a34a61b235be9ef283afa38811e;p=portaudio Alsa: Add function to prevent duplicate parts in the visible pa device name string, that occur when the pcm-device name starts with the card name (since they are both used). Achieved by trimming the pcm-device string beginning. --- diff --git a/src/hostapi/alsa/pa_linux_alsa.c b/src/hostapi/alsa/pa_linux_alsa.c index 32c1f76..87aeb2b 100644 --- a/src/hostapi/alsa/pa_linux_alsa.c +++ b/src/hostapi/alsa/pa_linux_alsa.c @@ -1078,6 +1078,38 @@ static int IgnorePlugin( const char *pluginId ) return 0; } +/* Skip past parts at the beginning of a (pcm) info name that are already in the card name, to avoid duplication */ +static char *SkipCardDetailsInName( char *infoSkipName, char *cardRefName ) +{ + char *lastSpacePosn = infoSkipName; + + /* Skip matching chars; but only in chunks separated by ' ' (not part words etc), so track lastSpacePosn */ + while( *cardRefName ) + { + while( *infoSkipName && *cardRefName && *infoSkipName == *cardRefName) + { + infoSkipName++; + cardRefName++; + if( *infoSkipName == ' ' || *infoSkipName == '\0' ) + lastSpacePosn = infoSkipName; + } + infoSkipName = lastSpacePosn; + /* Look for another chunk; post-increment means ends pointing to next char */ + while( *cardRefName && ( *cardRefName++ != ' ' )); + } + if( *infoSkipName == '\0' ) + return "-"; /* The 2 names were identical; instead of a nul-string, return a marker string */ + + /* Now want to move to the first char after any spaces */ + while( *lastSpacePosn && *lastSpacePosn == ' ' ) + lastSpacePosn++; + /* Skip a single separator char if present in the remaining pcm name; (pa will add its own) */ + if(( *lastSpacePosn == '-' || *lastSpacePosn == ':' ) && *(lastSpacePosn + 1) == ' ' ) + lastSpacePosn += 2; + + return lastSpacePosn; +} + /** Open PCM device. * * Wrapper around alsa_snd_pcm_open which may repeatedly retry opening a device if it is busy, for @@ -1248,7 +1280,7 @@ static PaError BuildDeviceList( PaAlsaHostApiRepresentation *alsaApi ) while( alsa_snd_ctl_pcm_next_device( ctl, &devIdx ) == 0 && devIdx >= 0 ) { - char *alsaDeviceName, *deviceName; + char *alsaDeviceName, *deviceName, *infoName; size_t len; int hasPlayback = 0, hasCapture = 0; snprintf( buf, sizeof (buf), "hw:%d,%d", cardIdx, devIdx ); @@ -1274,12 +1306,13 @@ static PaError BuildDeviceList( PaAlsaHostApiRepresentation *alsaApi ) continue; } + infoName = SkipCardDetailsInName( (char *)alsa_snd_pcm_info_get_name( pcmInfo ), cardName ); + /* The length of the string written by snprintf plus terminating 0 */ - len = snprintf( NULL, 0, "%s: %s (%s)", cardName, alsa_snd_pcm_info_get_name( pcmInfo ), buf ) + 1; + len = snprintf( NULL, 0, "%s: %s (%s)", cardName, infoName, buf ) + 1; PA_UNLESS( deviceName = (char *)PaUtil_GroupAllocateMemory( alsaApi->allocations, len ), paInsufficientMemory ); - snprintf( deviceName, len, "%s: %s (%s)", cardName, - alsa_snd_pcm_info_get_name( pcmInfo ), buf ); + snprintf( deviceName, len, "%s: %s (%s)", cardName, infoName, buf ); ++numDeviceNames; if( !hwDevInfos || numDeviceNames > maxDeviceNames )