2012-02-29 08:02:10 +01:00
|
|
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
/* Copyright (C) 2008 Jonathan Moore Liles */
|
|
|
|
/* */
|
|
|
|
/* This program is free software; you can redistribute it and/or modify it */
|
|
|
|
/* under the terms of the GNU General Public License as published by the */
|
|
|
|
/* Free Software Foundation; either version 2 of the License, or (at your */
|
|
|
|
/* option) any later version. */
|
|
|
|
/* */
|
|
|
|
/* This program is distributed in the hope that it will be useful, but WITHOUT */
|
|
|
|
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
|
|
|
|
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
|
|
|
|
/* more details. */
|
|
|
|
/* */
|
|
|
|
/* You should have received a copy of the GNU General Public License along */
|
|
|
|
/* with This program; see the file COPYING. If not,write to the Free Software */
|
|
|
|
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
|
|
/* jackpatch.c
|
|
|
|
|
|
|
|
This program is just like ASSPatch, except that it works with Jack ports (audio and MIDI).
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
2012-02-29 08:02:10 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <errno.h>
|
2013-07-20 03:13:50 +02:00
|
|
|
#include <stdlib.h>
|
2012-02-29 08:02:10 +01:00
|
|
|
#include <jack/jack.h>
|
|
|
|
|
|
|
|
#include <lo/lo.h>
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
#include <pthread.h>
|
2012-02-29 08:02:10 +01:00
|
|
|
|
2013-04-11 02:23:29 +02:00
|
|
|
int client_active = 0;
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
jack_client_t *client;
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
pthread_mutex_t port_lock;
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
lo_server losrv;
|
|
|
|
lo_address nsm_addr;
|
|
|
|
int nsm_is_active;
|
|
|
|
|
|
|
|
char *project_file;
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
#undef VERSION
|
2012-02-29 08:02:10 +01:00
|
|
|
#define APP_TITLE "JACKPatch"
|
|
|
|
#define VERSION "0.2"
|
|
|
|
|
|
|
|
struct patch_record {
|
|
|
|
struct {
|
|
|
|
char *client;
|
|
|
|
char *port;
|
|
|
|
} src , dst;
|
2012-03-26 00:05:17 +02:00
|
|
|
int active; /* true if patch has already been activate (by us) */
|
|
|
|
struct patch_record *next;
|
2012-02-29 08:02:10 +01:00
|
|
|
};
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
|
|
|
|
struct port_record {
|
|
|
|
char *port;
|
|
|
|
int reg; /* true if registered, false if unregistered */
|
|
|
|
struct port_record *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct port_record *new_ports = NULL;
|
|
|
|
static struct port_record *known_ports = NULL;
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
static struct patch_record *patch_list = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pretty-print patch relationship of /pr/
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
print_patch ( struct patch_record *pr, int mode )
|
|
|
|
{
|
|
|
|
printf( "%s from '%s:%s' to '%s:%s'\n", mode ? ">>" : "::",
|
|
|
|
pr->src.client, pr->src.port, pr->dst.client, pr->dst.port );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
enqueue ( struct patch_record *p )
|
|
|
|
{
|
|
|
|
p->next = patch_list;
|
|
|
|
patch_list = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dequeue ( struct patch_record *pr )
|
|
|
|
{
|
|
|
|
if ( !pr )
|
|
|
|
return;
|
|
|
|
|
|
|
|
free( pr->src.port );
|
|
|
|
free( pr->dst.port );
|
|
|
|
free( pr->src.client );
|
|
|
|
free( pr->dst.client );
|
|
|
|
|
|
|
|
free( pr );
|
|
|
|
}
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
void
|
|
|
|
enqueue_port ( struct port_record **q, const char *port, int reg )
|
|
|
|
{
|
|
|
|
struct port_record *p = malloc( sizeof( struct port_record ));
|
|
|
|
|
|
|
|
p->port = strdup( port );
|
|
|
|
p->reg = reg;
|
|
|
|
p->next = *q;
|
|
|
|
*q = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct port_record *
|
|
|
|
dequeue_port ( struct port_record **q )
|
|
|
|
{
|
|
|
|
if ( *q )
|
|
|
|
{
|
|
|
|
struct port_record *p = *q;
|
|
|
|
|
|
|
|
*q = p->next;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enqueue_known_port ( const char *port )
|
|
|
|
{
|
|
|
|
enqueue_port( &known_ports, port, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * find_known_port ( const char *port )
|
|
|
|
{
|
|
|
|
struct port_record *pr;
|
|
|
|
|
|
|
|
for ( pr = known_ports; pr; pr = pr->next )
|
|
|
|
if ( !strcmp( port, pr->port ) )
|
|
|
|
return pr->port;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
enqueue_new_port ( const char *port, int reg )
|
|
|
|
{
|
|
|
|
pthread_mutex_lock( &port_lock );
|
|
|
|
|
|
|
|
enqueue_port( &new_ports, port, reg );
|
|
|
|
|
|
|
|
pthread_mutex_unlock( &port_lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct port_record *
|
|
|
|
dequeue_new_port ( void )
|
|
|
|
{
|
|
|
|
pthread_mutex_lock( &port_lock );
|
|
|
|
|
|
|
|
struct port_record *p = dequeue_port( &new_ports );
|
|
|
|
|
|
|
|
pthread_mutex_unlock( &port_lock );
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
int
|
|
|
|
process_patch ( const char *patch )
|
|
|
|
{
|
|
|
|
struct patch_record *pr;
|
|
|
|
char *leftc, *rightc, *leftp, *rightp;
|
|
|
|
char dir[3];
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = sscanf( patch, " %a[^:]:%a[^|] |%1[<>|] %a[^:]:%a[^\n]",
|
|
|
|
&leftc, &leftp, dir, &rightc, &rightp );
|
|
|
|
|
|
|
|
if ( retval == EOF )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ( retval != 5 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* trim space */
|
|
|
|
int j;
|
|
|
|
for ( j = strlen( leftp ) - 1; j > 0; j-- )
|
|
|
|
{
|
|
|
|
if ( leftp[j] == ' ' || leftp[j] == '\t' )
|
|
|
|
leftp[j] = 0;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir[2] = 0;
|
|
|
|
|
|
|
|
pr = malloc( sizeof( struct patch_record ) );
|
|
|
|
|
|
|
|
switch ( *dir )
|
|
|
|
{
|
|
|
|
case '<':
|
|
|
|
pr->src.client = rightc;
|
|
|
|
pr->src.port = rightp;
|
|
|
|
|
|
|
|
pr->dst.client = leftc;
|
|
|
|
pr->dst.port = leftp;
|
|
|
|
|
|
|
|
enqueue( pr );
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
pr->src.client = leftc;
|
|
|
|
pr->src.port = leftp;
|
|
|
|
|
|
|
|
pr->dst.client = rightc;
|
|
|
|
pr->dst.port = rightp;
|
|
|
|
|
|
|
|
enqueue( pr );
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
pr->src.client = rightc;
|
|
|
|
pr->src.port = rightp;
|
|
|
|
|
|
|
|
pr->dst.client = leftc;
|
|
|
|
pr->dst.port = leftp;
|
|
|
|
|
|
|
|
enqueue( pr );
|
|
|
|
|
|
|
|
pr = malloc( sizeof( struct patch_record ) );
|
|
|
|
|
|
|
|
pr->src.client = strdup( leftc );
|
|
|
|
pr->src.port = strdup( leftp );
|
|
|
|
|
|
|
|
pr->dst.client = strdup( rightc );
|
|
|
|
pr->dst.port = strdup( rightp );
|
|
|
|
|
|
|
|
enqueue( pr );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// fprintf( stderr, "Invalid token '|%s' at line %i of %s!", dir, i, file );
|
|
|
|
free( pr );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
pr->active = 0;
|
2012-02-29 08:02:10 +01:00
|
|
|
|
|
|
|
print_patch( pr, 1 );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
clear_all_patches ( )
|
|
|
|
{
|
|
|
|
struct patch_record *pr;
|
|
|
|
|
|
|
|
while ( patch_list )
|
|
|
|
{
|
|
|
|
pr = patch_list;
|
|
|
|
patch_list = pr->next;
|
|
|
|
dequeue( pr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Crudely parse configuration file named by /file/ using fscanf
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
read_config ( const char *file )
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if ( NULL == ( fp = fopen( file, "r" ) ) )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
clear_all_patches();
|
|
|
|
|
|
|
|
while ( !feof( fp ) && !ferror( fp ) )
|
|
|
|
{
|
|
|
|
int retval;
|
2012-03-26 00:05:17 +02:00
|
|
|
unsigned int k;
|
2012-02-29 08:02:10 +01:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
for ( k = 0; k < sizeof( buf ) - 1; k++ )
|
|
|
|
{
|
|
|
|
retval = fread( buf + k, 1, 1, fp );
|
|
|
|
if ( retval != 1 )
|
|
|
|
break;
|
|
|
|
if ( buf[k] == '\n' )
|
|
|
|
{
|
|
|
|
if ( k == 0 )
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( retval == 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
retval = process_patch( buf );
|
|
|
|
|
|
|
|
if ( retval < 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ( retval == 0 )
|
|
|
|
{
|
|
|
|
printf( "bad line %i.\n", i );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* returns 0 if connection failed, true if succeeded. Already connected
|
|
|
|
* is not considered failure */
|
2012-03-26 00:05:17 +02:00
|
|
|
void
|
2012-02-29 08:02:10 +01:00
|
|
|
connect_path ( struct patch_record *pr )
|
|
|
|
{
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
char srcport[512];
|
|
|
|
char dstport[512];
|
|
|
|
|
|
|
|
snprintf( srcport, 512, "%s:%s", pr->src.client, pr->src.port );
|
|
|
|
snprintf( dstport, 512, "%s:%s", pr->dst.client, pr->dst.port );
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
if ( pr->active )
|
|
|
|
{
|
|
|
|
/* patch is already active, don't bother JACK with it... */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! ( find_known_port( srcport ) && find_known_port( dstport ) ) )
|
|
|
|
{
|
|
|
|
/* one of the ports doesn't exist yet... don't attempt
|
|
|
|
* connection, jack will just complain. */
|
|
|
|
printf( "Not attempting connection because one of the ports is missing.\n" );
|
2013-04-10 01:58:41 +02:00
|
|
|
return;
|
2012-03-26 00:05:17 +02:00
|
|
|
}
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
printf( "Connecting %s |> %s\n", srcport, dstport );
|
|
|
|
|
|
|
|
r = jack_connect( client, srcport, dstport );
|
|
|
|
|
|
|
|
print_patch( pr, r );
|
|
|
|
|
|
|
|
if ( r == 0 || r == EEXIST )
|
2012-03-26 00:05:17 +02:00
|
|
|
{
|
|
|
|
pr->active = 1;
|
|
|
|
return;
|
|
|
|
}
|
2012-02-29 08:02:10 +01:00
|
|
|
else
|
|
|
|
{
|
2012-03-26 00:05:17 +02:00
|
|
|
pr->active = 0;
|
|
|
|
printf( "Error is %i\n", r );
|
|
|
|
return;
|
2012-02-29 08:02:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
void
|
|
|
|
do_for_matching_patches ( const char *portname, void (*func)( struct patch_record * ) )
|
2012-02-29 08:02:10 +01:00
|
|
|
{
|
|
|
|
struct patch_record *pr;
|
|
|
|
|
|
|
|
char client[512];
|
|
|
|
char port[512];
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
sscanf( portname, "%[^:]:%[^\n]", client, port );
|
2012-02-29 08:02:10 +01:00
|
|
|
|
|
|
|
for ( pr = patch_list; pr; pr = pr->next )
|
|
|
|
{
|
|
|
|
if ( ( !strcmp( client, pr->src.client ) && !strcmp( port, pr->src.port ) ) ||
|
|
|
|
( !strcmp( client, pr->dst.client ) && !strcmp( port, pr->dst.port ) ) )
|
|
|
|
{
|
2012-03-26 00:05:17 +02:00
|
|
|
func( pr );
|
2012-02-29 08:02:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-26 00:05:17 +02:00
|
|
|
inactivate_path ( struct patch_record *pr )
|
2012-02-29 08:02:10 +01:00
|
|
|
{
|
2012-03-26 00:05:17 +02:00
|
|
|
pr->active = 0;
|
|
|
|
}
|
2012-02-29 08:02:10 +01:00
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
void
|
|
|
|
inactivate_patch ( const char *portname )
|
|
|
|
{
|
|
|
|
do_for_matching_patches( portname, inactivate_path );
|
2012-02-29 08:02:10 +01:00
|
|
|
}
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
void
|
|
|
|
activate_patch ( const char *portname )
|
2012-02-29 08:02:10 +01:00
|
|
|
{
|
2012-03-26 00:05:17 +02:00
|
|
|
do_for_matching_patches( portname, connect_path );
|
|
|
|
}
|
2012-02-29 08:02:10 +01:00
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
void remove_known_port ( const char *port )
|
|
|
|
{
|
|
|
|
/* remove it from the list of known ports */
|
2012-02-29 08:02:10 +01:00
|
|
|
{
|
2012-03-26 00:05:17 +02:00
|
|
|
struct port_record *pr;
|
|
|
|
struct port_record *lp = NULL;
|
|
|
|
|
|
|
|
for ( pr = known_ports; pr; lp = pr, pr = pr->next )
|
|
|
|
if ( !strcmp( port, pr->port ) )
|
|
|
|
{
|
|
|
|
if ( lp )
|
|
|
|
lp->next = pr->next;
|
|
|
|
else
|
|
|
|
known_ports = pr->next;
|
|
|
|
|
|
|
|
free( pr->port );
|
|
|
|
free( pr );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-02-29 08:02:10 +01:00
|
|
|
}
|
2012-03-26 00:05:17 +02:00
|
|
|
|
|
|
|
/* now mark all patches including this port as inactive */
|
|
|
|
inactivate_patch ( port );
|
2012-02-29 08:02:10 +01:00
|
|
|
}
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
/** called for every new port */
|
2012-02-29 08:02:10 +01:00
|
|
|
void
|
2012-03-26 00:05:17 +02:00
|
|
|
handle_new_port ( const char *portname )
|
2012-02-29 08:02:10 +01:00
|
|
|
{
|
2012-03-26 00:05:17 +02:00
|
|
|
enqueue_known_port( portname );
|
2012-02-29 08:02:10 +01:00
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
printf( "New endpoint '%s' registered.\n", portname );
|
|
|
|
/* this is a new port */
|
|
|
|
activate_patch( portname );
|
2012-02-29 08:02:10 +01:00
|
|
|
}
|
|
|
|
|
2013-04-11 02:23:20 +02:00
|
|
|
void
|
|
|
|
register_prexisting_ports ( void )
|
|
|
|
{
|
|
|
|
const char **port;
|
|
|
|
const char **ports = jack_get_ports( client, NULL, NULL, 0 );
|
|
|
|
|
|
|
|
if ( ! ports )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for ( port = ports; *port; port++ )
|
|
|
|
{
|
|
|
|
handle_new_port( *port );
|
|
|
|
}
|
|
|
|
|
|
|
|
free( ports );
|
|
|
|
}
|
|
|
|
|
2013-07-20 03:13:50 +02:00
|
|
|
static int stringsort ( const void *a, const void *b )
|
|
|
|
{
|
|
|
|
return strcmp(* (char * const *) a, * (char * const *) b);
|
|
|
|
}
|
2013-04-11 02:23:20 +02:00
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
void
|
|
|
|
snapshot ( const char *file )
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
const char **port;
|
|
|
|
const char **ports = jack_get_ports( client, NULL, NULL, JackPortIsOutput );
|
|
|
|
|
|
|
|
if ( ! ports )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( NULL == ( fp = fopen( file, "w" ) ) )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Error opening snapshot file for writing" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-03 07:18:44 +02:00
|
|
|
clear_all_patches();
|
|
|
|
|
2013-07-20 03:13:50 +02:00
|
|
|
const int table_increment = 16;
|
|
|
|
int table_index = 0;
|
|
|
|
size_t table_size = table_increment;
|
|
|
|
char **table = (char**)malloc( sizeof( char * ) * table_increment );
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
for ( port = ports; *port; port++ )
|
|
|
|
{
|
|
|
|
jack_port_t *p;
|
|
|
|
|
|
|
|
p = jack_port_by_name( client, *port );
|
|
|
|
|
|
|
|
const char **connections;
|
|
|
|
const char **connection;
|
|
|
|
|
|
|
|
connections = jack_port_get_all_connections( client, p );
|
|
|
|
|
|
|
|
if ( ! connections )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for ( connection = connections; *connection; connection++ )
|
|
|
|
{
|
2013-05-03 07:18:44 +02:00
|
|
|
char *s;
|
|
|
|
asprintf( &s, "%-40s |> %s\n", *port, *connection );
|
2013-07-20 03:13:50 +02:00
|
|
|
|
|
|
|
if ( table_index >= table_size )
|
|
|
|
{
|
|
|
|
table_size += table_increment;
|
|
|
|
table = (char**)realloc( table, table_size * sizeof( char *) );
|
|
|
|
}
|
|
|
|
|
|
|
|
table[table_index++] = s;
|
|
|
|
|
2013-05-03 07:18:44 +02:00
|
|
|
process_patch( s );
|
2012-02-29 08:02:10 +01:00
|
|
|
printf( "++ %s |> %s\n", *port, *connection );
|
|
|
|
}
|
|
|
|
|
|
|
|
free( connections );
|
|
|
|
}
|
|
|
|
|
|
|
|
free( ports );
|
|
|
|
|
2013-07-20 03:13:50 +02:00
|
|
|
qsort( table, table_index, sizeof(char*), stringsort );
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for ( i = 0; i < table_index; i++ )
|
|
|
|
{
|
|
|
|
fprintf( fp, "%s", table[i] );
|
|
|
|
free(table[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(table);
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
fclose( fp );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int die_now = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
signal_handler ( int x )
|
|
|
|
{
|
|
|
|
die_now = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
die ( void )
|
|
|
|
{
|
2013-04-11 02:23:29 +02:00
|
|
|
if ( client_active )
|
|
|
|
jack_deactivate( client );
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
jack_client_close( client );
|
|
|
|
client = NULL;
|
|
|
|
exit( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** set_traps
|
|
|
|
*
|
|
|
|
* Handle signals
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
set_traps ( void )
|
|
|
|
{
|
2012-03-05 04:12:32 +01:00
|
|
|
signal( SIGHUP, signal_handler );
|
2012-02-29 08:02:10 +01:00
|
|
|
signal( SIGINT, signal_handler );
|
|
|
|
// signal( SIGQUIT, signal_handler );
|
|
|
|
// signal( SIGSEGV, signal_handler );
|
|
|
|
// signal( SIGPIPE, signal_handler );
|
|
|
|
signal( SIGTERM, signal_handler );
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************/
|
|
|
|
/* OSC HANDLERS */
|
|
|
|
/****************/
|
|
|
|
|
|
|
|
int
|
|
|
|
osc_announce_error ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
|
|
|
|
{
|
2012-03-26 00:05:17 +02:00
|
|
|
if ( strcmp( types, "sis" ) )
|
|
|
|
return -1;
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
if ( strcmp( "/nsm/server/announce", &argv[0]->s ) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf( "Failed to register with NSM: %s\n", &argv[2]->s );
|
|
|
|
nsm_is_active = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
osc_announce_reply ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
|
|
|
|
{
|
|
|
|
if ( strcmp( "/nsm/server/announce", &argv[0]->s ) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf( "Successfully registered. NSM says: %s", &argv[1]->s );
|
|
|
|
|
|
|
|
nsm_is_active = 1;
|
|
|
|
nsm_addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
osc_save ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
|
|
|
|
{
|
|
|
|
snapshot( project_file );
|
|
|
|
|
|
|
|
lo_send_from( nsm_addr, losrv, LO_TT_IMMEDIATE, "/reply", "ss", path, "OK" );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-11 02:23:29 +02:00
|
|
|
void
|
|
|
|
maybe_activate_jack_client ( void )
|
|
|
|
{
|
|
|
|
if ( ! client_active )
|
|
|
|
{
|
|
|
|
jack_activate( client );
|
|
|
|
client_active = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
int
|
|
|
|
osc_open ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
|
|
|
|
{
|
|
|
|
const char *new_path = &argv[0]->s;
|
2012-03-26 00:05:17 +02:00
|
|
|
// const char *display_name = &argv[1]->s;
|
2012-02-29 08:02:10 +01:00
|
|
|
|
|
|
|
char *new_filename;
|
|
|
|
|
|
|
|
asprintf( &new_filename, "%s.jackpatch", new_path );
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if ( 0 == stat( new_filename, &st ) )
|
|
|
|
{
|
2013-04-11 02:23:20 +02:00
|
|
|
printf( "Reading patch definitions from: %s\n", new_filename );
|
2012-02-29 08:02:10 +01:00
|
|
|
if ( read_config( new_filename ) )
|
|
|
|
{
|
2012-03-26 00:05:17 +02:00
|
|
|
/* wipe_ports(); */
|
|
|
|
/* check_for_new_ports(); */
|
2013-04-11 02:23:29 +02:00
|
|
|
maybe_activate_jack_client();
|
2013-04-11 02:23:20 +02:00
|
|
|
register_prexisting_ports();
|
2012-02-29 08:02:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lo_send_from( nsm_addr, losrv, LO_TT_IMMEDIATE, "/error", "sis", path, -1, "Could not open file" );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-11 02:23:29 +02:00
|
|
|
maybe_activate_jack_client();
|
2012-02-29 08:02:10 +01:00
|
|
|
clear_all_patches();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( project_file )
|
|
|
|
free( project_file );
|
|
|
|
|
|
|
|
project_file = new_filename;
|
|
|
|
|
|
|
|
lo_send_from( nsm_addr, losrv, LO_TT_IMMEDIATE, "/reply", "ss", path, "OK" );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
announce ( const char *nsm_url, const char *client_name, const char *process_name )
|
|
|
|
{
|
|
|
|
printf( "Announcing to NSM\n" );
|
|
|
|
|
|
|
|
lo_address to = lo_address_new_from_url( nsm_url );
|
|
|
|
|
|
|
|
int pid = (int)getpid();
|
|
|
|
|
|
|
|
lo_send_from( to, losrv, LO_TT_IMMEDIATE, "/nsm/server/announce", "sssiii",
|
|
|
|
client_name,
|
|
|
|
":switch:",
|
|
|
|
process_name,
|
|
|
|
0, /* api_major_version */
|
|
|
|
8, /* api_minor_version */
|
|
|
|
pid );
|
|
|
|
|
|
|
|
lo_address_free( to );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init_osc ( const char *osc_port )
|
|
|
|
{
|
|
|
|
losrv = lo_server_new( osc_port, NULL );
|
|
|
|
//error_handler );
|
|
|
|
|
|
|
|
char *url = lo_server_get_url(losrv);
|
|
|
|
printf("OSC: %s\n",url);
|
|
|
|
free(url);
|
|
|
|
|
|
|
|
lo_server_add_method( losrv, "/nsm/client/save", "", osc_save, NULL );
|
|
|
|
lo_server_add_method( losrv, "/nsm/client/open", "sss", osc_open, NULL );
|
|
|
|
lo_server_add_method( losrv, "/error", "sis", osc_announce_error, NULL );
|
|
|
|
lo_server_add_method( losrv, "/reply", "ssss", osc_announce_reply, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
void
|
|
|
|
check_for_new_ports ( void )
|
|
|
|
{
|
|
|
|
struct port_record *p = NULL;
|
|
|
|
|
|
|
|
while ( ( p = dequeue_new_port() ) )
|
|
|
|
{
|
|
|
|
if ( p->reg )
|
|
|
|
handle_new_port( p->port );
|
|
|
|
else
|
|
|
|
remove_known_port( p->port );
|
|
|
|
|
|
|
|
free( p->port );
|
|
|
|
free( p );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
port_registration_callback( jack_port_id_t id, int reg, void *arg )
|
|
|
|
{
|
|
|
|
jack_port_t *p = jack_port_by_id( client, id );
|
|
|
|
|
|
|
|
const char *port = jack_port_name( p );
|
|
|
|
|
|
|
|
enqueue_new_port( port, reg );
|
|
|
|
}
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
/* */
|
|
|
|
|
|
|
|
int
|
|
|
|
main ( int argc, char **argv )
|
|
|
|
{
|
|
|
|
/* get_args( argc, argv ); */
|
|
|
|
|
|
|
|
jack_status_t status;
|
|
|
|
|
|
|
|
client = jack_client_open( APP_TITLE, JackNullOption, &status );
|
|
|
|
|
2012-03-26 00:05:17 +02:00
|
|
|
jack_set_port_registration_callback( client, port_registration_callback, NULL );
|
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
if ( ! client )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Could not register JACK client\n" );
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
}
|
2012-03-26 00:05:17 +02:00
|
|
|
|
|
|
|
pthread_mutex_init( &port_lock, NULL );
|
2012-02-29 08:02:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
set_traps();
|
2013-04-11 02:23:29 +02:00
|
|
|
|
2012-02-29 08:02:10 +01:00
|
|
|
if ( argc > 1 )
|
|
|
|
{
|
2013-04-11 02:23:29 +02:00
|
|
|
maybe_activate_jack_client();
|
2012-02-29 08:02:10 +01:00
|
|
|
if ( ! strcmp( argv[1], "--save" ) )
|
|
|
|
{
|
|
|
|
if ( argc > 2 )
|
|
|
|
{
|
|
|
|
printf( "Saving current graph to: %s\n", argv[2] );
|
|
|
|
snapshot( argv[2] );
|
2013-04-11 02:23:29 +02:00
|
|
|
die();
|
2012-02-29 08:02:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read_config( argv[1] );
|
|
|
|
printf( "Monitoring...\n" );
|
|
|
|
for ( ;; )
|
|
|
|
{
|
|
|
|
usleep( 50000 );
|
2012-03-26 00:05:17 +02:00
|
|
|
check_for_new_ports();
|
2012-02-29 08:02:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init_osc( NULL );
|
|
|
|
|
|
|
|
const char *nsm_url = getenv( "NSM_URL" );
|
|
|
|
|
|
|
|
if ( nsm_url )
|
|
|
|
{
|
|
|
|
announce( nsm_url, APP_TITLE, argv[0] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Could not register as NSM client.\n" );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ;; )
|
|
|
|
{
|
|
|
|
lo_server_recv_noblock( losrv, 500 );
|
|
|
|
|
2013-04-11 02:23:29 +02:00
|
|
|
if ( client_active )
|
|
|
|
check_for_new_ports();
|
2012-02-29 08:02:10 +01:00
|
|
|
|
|
|
|
if ( die_now )
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|