Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

770 lines
16KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. /* jackpatch.c
  19. This program is just like ASSPatch, except that it works with Jack ports (audio and MIDI).
  20. */
  21. #define _GNU_SOURCE
  22. #pragma GCC diagnostic ignored "-Wunused-parameter"
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <signal.h>
  31. #include <getopt.h>
  32. #include <sys/types.h>
  33. #include <sys/time.h>
  34. #include <errno.h>
  35. #include <jack/jack.h>
  36. #include <lo/lo.h>
  37. #include <pthread.h>
  38. jack_client_t *client;
  39. pthread_mutex_t port_lock;
  40. lo_server losrv;
  41. lo_address nsm_addr;
  42. int nsm_is_active;
  43. char *project_file;
  44. #undef VERSION
  45. #define APP_TITLE "JACKPatch"
  46. #define VERSION "0.2"
  47. struct patch_record {
  48. struct {
  49. char *client;
  50. char *port;
  51. } src , dst;
  52. int active; /* true if patch has already been activate (by us) */
  53. struct patch_record *next;
  54. };
  55. struct port_record {
  56. char *port;
  57. int reg; /* true if registered, false if unregistered */
  58. struct port_record *next;
  59. };
  60. static struct port_record *new_ports = NULL;
  61. static struct port_record *known_ports = NULL;
  62. static struct patch_record *patch_list = NULL;
  63. /**
  64. * Pretty-print patch relationship of /pr/
  65. */
  66. void
  67. print_patch ( struct patch_record *pr, int mode )
  68. {
  69. printf( "%s from '%s:%s' to '%s:%s'\n", mode ? ">>" : "::",
  70. pr->src.client, pr->src.port, pr->dst.client, pr->dst.port );
  71. }
  72. void
  73. enqueue ( struct patch_record *p )
  74. {
  75. p->next = patch_list;
  76. patch_list = p;
  77. }
  78. void
  79. dequeue ( struct patch_record *pr )
  80. {
  81. if ( !pr )
  82. return;
  83. free( pr->src.port );
  84. free( pr->dst.port );
  85. free( pr->src.client );
  86. free( pr->dst.client );
  87. free( pr );
  88. }
  89. void
  90. enqueue_port ( struct port_record **q, const char *port, int reg )
  91. {
  92. struct port_record *p = malloc( sizeof( struct port_record ));
  93. p->port = strdup( port );
  94. p->reg = reg;
  95. p->next = *q;
  96. *q = p;
  97. }
  98. struct port_record *
  99. dequeue_port ( struct port_record **q )
  100. {
  101. if ( *q )
  102. {
  103. struct port_record *p = *q;
  104. *q = p->next;
  105. return p;
  106. }
  107. return NULL;
  108. }
  109. void enqueue_known_port ( const char *port )
  110. {
  111. enqueue_port( &known_ports, port, 1 );
  112. }
  113. const char * find_known_port ( const char *port )
  114. {
  115. struct port_record *pr;
  116. for ( pr = known_ports; pr; pr = pr->next )
  117. if ( !strcmp( port, pr->port ) )
  118. return pr->port;
  119. return NULL;
  120. }
  121. void
  122. enqueue_new_port ( const char *port, int reg )
  123. {
  124. pthread_mutex_lock( &port_lock );
  125. enqueue_port( &new_ports, port, reg );
  126. pthread_mutex_unlock( &port_lock );
  127. }
  128. struct port_record *
  129. dequeue_new_port ( void )
  130. {
  131. pthread_mutex_lock( &port_lock );
  132. struct port_record *p = dequeue_port( &new_ports );
  133. pthread_mutex_unlock( &port_lock );
  134. return p;
  135. }
  136. int
  137. process_patch ( const char *patch )
  138. {
  139. struct patch_record *pr;
  140. char *leftc, *rightc, *leftp, *rightp;
  141. char dir[3];
  142. int retval;
  143. retval = sscanf( patch, " %a[^:]:%a[^|] |%1[<>|] %a[^:]:%a[^\n]",
  144. &leftc, &leftp, dir, &rightc, &rightp );
  145. if ( retval == EOF )
  146. return -1;
  147. if ( retval != 5 )
  148. return 0;
  149. /* trim space */
  150. int j;
  151. for ( j = strlen( leftp ) - 1; j > 0; j-- )
  152. {
  153. if ( leftp[j] == ' ' || leftp[j] == '\t' )
  154. leftp[j] = 0;
  155. else
  156. break;
  157. }
  158. dir[2] = 0;
  159. pr = malloc( sizeof( struct patch_record ) );
  160. switch ( *dir )
  161. {
  162. case '<':
  163. pr->src.client = rightc;
  164. pr->src.port = rightp;
  165. pr->dst.client = leftc;
  166. pr->dst.port = leftp;
  167. enqueue( pr );
  168. break;
  169. case '>':
  170. pr->src.client = leftc;
  171. pr->src.port = leftp;
  172. pr->dst.client = rightc;
  173. pr->dst.port = rightp;
  174. enqueue( pr );
  175. break;
  176. case '|':
  177. pr->src.client = rightc;
  178. pr->src.port = rightp;
  179. pr->dst.client = leftc;
  180. pr->dst.port = leftp;
  181. enqueue( pr );
  182. pr = malloc( sizeof( struct patch_record ) );
  183. pr->src.client = strdup( leftc );
  184. pr->src.port = strdup( leftp );
  185. pr->dst.client = strdup( rightc );
  186. pr->dst.port = strdup( rightp );
  187. enqueue( pr );
  188. break;
  189. default:
  190. // fprintf( stderr, "Invalid token '|%s' at line %i of %s!", dir, i, file );
  191. free( pr );
  192. return 0;
  193. }
  194. pr->active = 0;
  195. print_patch( pr, 1 );
  196. return 1;
  197. }
  198. void
  199. clear_all_patches ( )
  200. {
  201. struct patch_record *pr;
  202. while ( patch_list )
  203. {
  204. pr = patch_list;
  205. patch_list = pr->next;
  206. dequeue( pr );
  207. }
  208. }
  209. /**
  210. * Crudely parse configuration file named by /file/ using fscanf
  211. */
  212. int
  213. read_config ( const char *file )
  214. {
  215. FILE *fp;
  216. int i = 0;
  217. if ( NULL == ( fp = fopen( file, "r" ) ) )
  218. return 0;
  219. clear_all_patches();
  220. while ( !feof( fp ) && !ferror( fp ) )
  221. {
  222. int retval;
  223. unsigned int k;
  224. char buf[BUFSIZ];
  225. i++;
  226. for ( k = 0; k < sizeof( buf ) - 1; k++ )
  227. {
  228. retval = fread( buf + k, 1, 1, fp );
  229. if ( retval != 1 )
  230. break;
  231. if ( buf[k] == '\n' )
  232. {
  233. if ( k == 0 )
  234. continue;
  235. else
  236. break;
  237. }
  238. }
  239. if ( retval == 0 )
  240. break;
  241. retval = process_patch( buf );
  242. if ( retval < 0 )
  243. break;
  244. if ( retval == 0 )
  245. {
  246. printf( "bad line %i.\n", i );
  247. continue;
  248. }
  249. }
  250. fclose( fp );
  251. return 1;
  252. }
  253. /* returns 0 if connection failed, true if succeeded. Already connected
  254. * is not considered failure */
  255. void
  256. connect_path ( struct patch_record *pr )
  257. {
  258. int r = 0;
  259. char srcport[512];
  260. char dstport[512];
  261. snprintf( srcport, 512, "%s:%s", pr->src.client, pr->src.port );
  262. snprintf( dstport, 512, "%s:%s", pr->dst.client, pr->dst.port );
  263. if ( pr->active )
  264. {
  265. /* patch is already active, don't bother JACK with it... */
  266. return;
  267. }
  268. if ( ! ( find_known_port( srcport ) && find_known_port( dstport ) ) )
  269. {
  270. /* one of the ports doesn't exist yet... don't attempt
  271. * connection, jack will just complain. */
  272. printf( "Not attempting connection because one of the ports is missing.\n" );
  273. }
  274. printf( "Connecting %s |> %s\n", srcport, dstport );
  275. r = jack_connect( client, srcport, dstport );
  276. print_patch( pr, r );
  277. if ( r == 0 || r == EEXIST )
  278. {
  279. pr->active = 1;
  280. return;
  281. }
  282. else
  283. {
  284. pr->active = 0;
  285. printf( "Error is %i\n", r );
  286. return;
  287. }
  288. }
  289. void
  290. do_for_matching_patches ( const char *portname, void (*func)( struct patch_record * ) )
  291. {
  292. struct patch_record *pr;
  293. char client[512];
  294. char port[512];
  295. sscanf( portname, "%[^:]:%[^\n]", client, port );
  296. for ( pr = patch_list; pr; pr = pr->next )
  297. {
  298. if ( ( !strcmp( client, pr->src.client ) && !strcmp( port, pr->src.port ) ) ||
  299. ( !strcmp( client, pr->dst.client ) && !strcmp( port, pr->dst.port ) ) )
  300. {
  301. func( pr );
  302. }
  303. }
  304. }
  305. void
  306. inactivate_path ( struct patch_record *pr )
  307. {
  308. pr->active = 0;
  309. }
  310. void
  311. inactivate_patch ( const char *portname )
  312. {
  313. do_for_matching_patches( portname, inactivate_path );
  314. }
  315. void
  316. activate_patch ( const char *portname )
  317. {
  318. do_for_matching_patches( portname, connect_path );
  319. }
  320. void remove_known_port ( const char *port )
  321. {
  322. /* remove it from the list of known ports */
  323. {
  324. struct port_record *pr;
  325. struct port_record *lp = NULL;
  326. for ( pr = known_ports; pr; lp = pr, pr = pr->next )
  327. if ( !strcmp( port, pr->port ) )
  328. {
  329. if ( lp )
  330. lp->next = pr->next;
  331. else
  332. known_ports = pr->next;
  333. free( pr->port );
  334. free( pr );
  335. break;
  336. }
  337. }
  338. /* now mark all patches including this port as inactive */
  339. inactivate_patch ( port );
  340. }
  341. /**
  342. * Attempt to activate all connections in patch list
  343. */
  344. void
  345. activate_all_patches ( void )
  346. {
  347. struct patch_record *pr;
  348. for ( pr = patch_list; pr; pr = pr->next )
  349. connect_path( pr );
  350. }
  351. /** called for every new port */
  352. void
  353. handle_new_port ( const char *portname )
  354. {
  355. enqueue_known_port( portname );
  356. printf( "New endpoint '%s' registered.\n", portname );
  357. /* this is a new port */
  358. activate_patch( portname );
  359. }
  360. void
  361. snapshot ( const char *file )
  362. {
  363. FILE *fp;
  364. const char **port;
  365. const char **ports = jack_get_ports( client, NULL, NULL, JackPortIsOutput );
  366. if ( ! ports )
  367. return;
  368. if ( NULL == ( fp = fopen( file, "w" ) ) )
  369. {
  370. fprintf( stderr, "Error opening snapshot file for writing" );
  371. return;
  372. }
  373. for ( port = ports; *port; port++ )
  374. {
  375. jack_port_t *p;
  376. p = jack_port_by_name( client, *port );
  377. const char **connections;
  378. const char **connection;
  379. connections = jack_port_get_all_connections( client, p );
  380. if ( ! connections )
  381. continue;
  382. for ( connection = connections; *connection; connection++ )
  383. {
  384. fprintf( fp, "%-40s |> %s\n", *port, *connection );
  385. printf( "++ %s |> %s\n", *port, *connection );
  386. }
  387. free( connections );
  388. }
  389. free( ports );
  390. fclose( fp );
  391. }
  392. static int die_now = 0;
  393. void
  394. signal_handler ( int x )
  395. {
  396. die_now = 1;
  397. }
  398. void
  399. die ( void )
  400. {
  401. jack_deactivate( client );
  402. jack_client_close( client );
  403. client = NULL;
  404. exit( 0 );
  405. }
  406. /** set_traps
  407. *
  408. * Handle signals
  409. */
  410. void
  411. set_traps ( void )
  412. {
  413. signal( SIGHUP, signal_handler );
  414. signal( SIGINT, signal_handler );
  415. // signal( SIGQUIT, signal_handler );
  416. // signal( SIGSEGV, signal_handler );
  417. // signal( SIGPIPE, signal_handler );
  418. signal( SIGTERM, signal_handler );
  419. }
  420. /****************/
  421. /* OSC HANDLERS */
  422. /****************/
  423. int
  424. osc_announce_error ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  425. {
  426. if ( strcmp( types, "sis" ) )
  427. return -1;
  428. if ( strcmp( "/nsm/server/announce", &argv[0]->s ) )
  429. return -1;
  430. printf( "Failed to register with NSM: %s\n", &argv[2]->s );
  431. nsm_is_active = 0;
  432. return 0;
  433. }
  434. int
  435. osc_announce_reply ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  436. {
  437. if ( strcmp( "/nsm/server/announce", &argv[0]->s ) )
  438. return -1;
  439. printf( "Successfully registered. NSM says: %s", &argv[1]->s );
  440. nsm_is_active = 1;
  441. nsm_addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ) );
  442. return 0;
  443. }
  444. int
  445. osc_save ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  446. {
  447. snapshot( project_file );
  448. lo_send_from( nsm_addr, losrv, LO_TT_IMMEDIATE, "/reply", "ss", path, "OK" );
  449. return 0;
  450. }
  451. int
  452. osc_open ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  453. {
  454. const char *new_path = &argv[0]->s;
  455. // const char *display_name = &argv[1]->s;
  456. char *new_filename;
  457. asprintf( &new_filename, "%s.jackpatch", new_path );
  458. struct stat st;
  459. if ( 0 == stat( new_filename, &st ) )
  460. {
  461. if ( read_config( new_filename ) )
  462. {
  463. printf( "Reading patch definitions from: %s\n", new_filename );
  464. /* wipe_ports(); */
  465. /* check_for_new_ports(); */
  466. activate_all_patches();
  467. }
  468. else
  469. {
  470. lo_send_from( nsm_addr, losrv, LO_TT_IMMEDIATE, "/error", "sis", path, -1, "Could not open file" );
  471. return 0;
  472. }
  473. }
  474. else
  475. {
  476. clear_all_patches();
  477. }
  478. if ( project_file )
  479. free( project_file );
  480. project_file = new_filename;
  481. lo_send_from( nsm_addr, losrv, LO_TT_IMMEDIATE, "/reply", "ss", path, "OK" );
  482. return 0;
  483. }
  484. void
  485. announce ( const char *nsm_url, const char *client_name, const char *process_name )
  486. {
  487. printf( "Announcing to NSM\n" );
  488. lo_address to = lo_address_new_from_url( nsm_url );
  489. int pid = (int)getpid();
  490. lo_send_from( to, losrv, LO_TT_IMMEDIATE, "/nsm/server/announce", "sssiii",
  491. client_name,
  492. ":switch:",
  493. process_name,
  494. 0, /* api_major_version */
  495. 8, /* api_minor_version */
  496. pid );
  497. lo_address_free( to );
  498. }
  499. void
  500. init_osc ( const char *osc_port )
  501. {
  502. losrv = lo_server_new( osc_port, NULL );
  503. //error_handler );
  504. char *url = lo_server_get_url(losrv);
  505. printf("OSC: %s\n",url);
  506. free(url);
  507. lo_server_add_method( losrv, "/nsm/client/save", "", osc_save, NULL );
  508. lo_server_add_method( losrv, "/nsm/client/open", "sss", osc_open, NULL );
  509. lo_server_add_method( losrv, "/error", "sis", osc_announce_error, NULL );
  510. lo_server_add_method( losrv, "/reply", "ssss", osc_announce_reply, NULL );
  511. }
  512. void
  513. check_for_new_ports ( void )
  514. {
  515. struct port_record *p = NULL;
  516. while ( ( p = dequeue_new_port() ) )
  517. {
  518. if ( p->reg )
  519. handle_new_port( p->port );
  520. else
  521. remove_known_port( p->port );
  522. free( p->port );
  523. free( p );
  524. }
  525. }
  526. void
  527. port_registration_callback( jack_port_id_t id, int reg, void *arg )
  528. {
  529. jack_port_t *p = jack_port_by_id( client, id );
  530. const char *port = jack_port_name( p );
  531. enqueue_new_port( port, reg );
  532. }
  533. /* */
  534. int
  535. main ( int argc, char **argv )
  536. {
  537. /* get_args( argc, argv ); */
  538. jack_status_t status;
  539. client = jack_client_open( APP_TITLE, JackNullOption, &status );
  540. jack_set_port_registration_callback( client, port_registration_callback, NULL );
  541. if ( ! client )
  542. {
  543. fprintf( stderr, "Could not register JACK client\n" );
  544. exit(1);
  545. }
  546. pthread_mutex_init( &port_lock, NULL );
  547. jack_activate( client );
  548. set_traps();
  549. if ( argc > 1 )
  550. {
  551. if ( ! strcmp( argv[1], "--save" ) )
  552. {
  553. if ( argc > 2 )
  554. {
  555. printf( "Saving current graph to: %s\n", argv[2] );
  556. snapshot( argv[2] );
  557. exit(0);
  558. }
  559. }
  560. else
  561. {
  562. read_config( argv[1] );
  563. printf( "Monitoring...\n" );
  564. for ( ;; )
  565. {
  566. usleep( 50000 );
  567. check_for_new_ports();
  568. }
  569. }
  570. }
  571. init_osc( NULL );
  572. const char *nsm_url = getenv( "NSM_URL" );
  573. if ( nsm_url )
  574. {
  575. announce( nsm_url, APP_TITLE, argv[0] );
  576. }
  577. else
  578. {
  579. fprintf( stderr, "Could not register as NSM client.\n" );
  580. exit(1);
  581. }
  582. for ( ;; )
  583. {
  584. lo_server_recv_noblock( losrv, 500 );
  585. check_for_new_ports();
  586. if ( die_now )
  587. die();
  588. }
  589. }