Re: [LAD] jack client autoconnection

From: Fons Adriaensen <fons@email-addr-hidden>
Date: Mon Feb 04 2008 - 18:46:14 EET

On Mon, Feb 04, 2008 at 03:32:23PM +0100, Fons Adriaensen wrote:

> Where jack_autoconnect() would use environment variables,
> read .rc files, invoke the Buddha, or consult Paul Davis or
> the oracle of Delphi to find out which ports to use, but
> without requiring any special support from jackd itself.

The code required is much shorter than the length of this
thread so far, so I take the liberty to post it.
Quickly but not fully tested.

Enjoy.

(C) Fons Adriaensen, License: GPL

--- jack_autoconnect.h

#ifndef __JACK_AUTOCONNECT_H
#define __JACK_AUTOCONNECT_H

#ifdef __cplusplus
extern "C" {
#endif

#include <jack/jack.h>

/*

Connect to JACK ports specified by an environment variable.

Args:

   client: Pointer to jack client_t of your application.
   nport: The number of ports to connect.
   ports: An array of existing jack ports you want to connect.
   envvar: The name of the environment variable containing a comma-separated list of the full
            names of the ports you want your ports connected to.

Returns:

   -1 if 'envvar' does not exist, the number of connections made otherwise.

Ports can be input or output in any mix, but of course directions should match
those of the ports specified by 'envvar'.

The name 'envvar' can be application-specific, e.g. MY_VERY_SPECIAL_PLAYER_PORTS, or
general, e.g. JACK_STEREO_PLAYBACK_PORTS, JACK_5DOT1_PLAYBACK_PORTS, etc.

*/

extern int jack_autoconnect (jack_client_t *client, int nport, jack_port_t **ports, const char *envvar);

#ifdef __cplusplus
}
#endif

#endif

--- jack_autoconnect.c

#include <stdlib.h>
#include <string.h>
#include <jack/jack.h>

#define BUFFSIZE 4096

int jack_autoconnect (jack_client_t *client, int nport, jack_port_t **ports, const char *envvar)
{
    int n, r;
    char *t;
    const char *p, *q;
    char buff [BUFFSIZE];

    p = getenv (envvar);
    if (p == 0) return -1;
    strncpy (buff, p, BUFFSIZE);
    buff [BUFFSIZE - 1] = 0;

    p = buff;
    for (n = 0; n < nport; n++)
    {
        if ((p == 0) || (ports [n] == 0)) return n;
        t = strchr (p, ',');
        if (t) *t = 0;
        q = jack_port_name (ports [n]);
        if (jack_port_flags (ports [n]) & JackPortIsInput)
        {
            r = jack_connect (client, p, q);
        }
        else
        {
            r = jack_connect (client, q, p);
        }
        if (r) return n;
        p = t ? (t + 1) : 0;
    }

    return n;
}

--- end of code

-- 
FA
Laboratorio di Acustica ed Elettroacustica
Parma, Italia
Lascia la spina, cogli la rosa.
_______________________________________________
Linux-audio-dev mailing list
Linux-audio-dev@email-addr-hidden
http://lists.linuxaudio.org/mailman/listinfo/linux-audio-dev
Received on Mon Feb 4 20:15:05 2008

This archive was generated by hypermail 2.1.8 : Mon Feb 04 2008 - 20:15:05 EET