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.

651 lines
14KB

  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. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <signal.h>
  30. #include <getopt.h>
  31. #include <sys/types.h>
  32. #include <sys/time.h>
  33. #include <errno.h>
  34. #include <jack/jack.h>
  35. #include <lo/lo.h>
  36. static const char **all_ports = NULL;
  37. jack_client_t *client;
  38. lo_server losrv;
  39. lo_address nsm_addr;
  40. int nsm_is_active;
  41. char *project_file;
  42. #define APP_TITLE "JACKPatch"
  43. #define VERSION "0.2"
  44. struct patch_record {
  45. struct {
  46. char *client;
  47. char *port;
  48. } src , dst;
  49. struct patch_record *next;
  50. };
  51. static struct patch_record *patch_list = NULL;
  52. /**
  53. * Pretty-print patch relationship of /pr/
  54. */
  55. void
  56. print_patch ( struct patch_record *pr, int mode )
  57. {
  58. printf( "%s from '%s:%s' to '%s:%s'\n", mode ? ">>" : "::",
  59. pr->src.client, pr->src.port, pr->dst.client, pr->dst.port );
  60. }
  61. void
  62. enqueue ( struct patch_record *p )
  63. {
  64. p->next = patch_list;
  65. patch_list = p;
  66. }
  67. void
  68. dequeue ( struct patch_record *pr )
  69. {
  70. if ( !pr )
  71. return;
  72. free( pr->src.port );
  73. free( pr->dst.port );
  74. free( pr->src.client );
  75. free( pr->dst.client );
  76. free( pr );
  77. }
  78. int
  79. process_patch ( const char *patch )
  80. {
  81. struct patch_record *pr;
  82. char *leftc, *rightc, *leftp, *rightp;
  83. char dir[3];
  84. int retval;
  85. retval = sscanf( patch, " %a[^:]:%a[^|] |%1[<>|] %a[^:]:%a[^\n]",
  86. &leftc, &leftp, dir, &rightc, &rightp );
  87. if ( retval == EOF )
  88. return -1;
  89. if ( retval != 5 )
  90. return 0;
  91. /* trim space */
  92. int j;
  93. for ( j = strlen( leftp ) - 1; j > 0; j-- )
  94. {
  95. if ( leftp[j] == ' ' || leftp[j] == '\t' )
  96. leftp[j] = 0;
  97. else
  98. break;
  99. }
  100. dir[2] = 0;
  101. pr = malloc( sizeof( struct patch_record ) );
  102. switch ( *dir )
  103. {
  104. case '<':
  105. pr->src.client = rightc;
  106. pr->src.port = rightp;
  107. pr->dst.client = leftc;
  108. pr->dst.port = leftp;
  109. enqueue( pr );
  110. break;
  111. case '>':
  112. pr->src.client = leftc;
  113. pr->src.port = leftp;
  114. pr->dst.client = rightc;
  115. pr->dst.port = rightp;
  116. enqueue( pr );
  117. break;
  118. case '|':
  119. pr->src.client = rightc;
  120. pr->src.port = rightp;
  121. pr->dst.client = leftc;
  122. pr->dst.port = leftp;
  123. enqueue( pr );
  124. pr = malloc( sizeof( struct patch_record ) );
  125. pr->src.client = strdup( leftc );
  126. pr->src.port = strdup( leftp );
  127. pr->dst.client = strdup( rightc );
  128. pr->dst.port = strdup( rightp );
  129. enqueue( pr );
  130. break;
  131. default:
  132. // fprintf( stderr, "Invalid token '|%s' at line %i of %s!", dir, i, file );
  133. free( pr );
  134. return 0;
  135. }
  136. print_patch( pr, 1 );
  137. return 1;
  138. }
  139. void
  140. clear_all_patches ( )
  141. {
  142. struct patch_record *pr;
  143. while ( patch_list )
  144. {
  145. pr = patch_list;
  146. patch_list = pr->next;
  147. dequeue( pr );
  148. }
  149. }
  150. /**
  151. * Crudely parse configuration file named by /file/ using fscanf
  152. */
  153. int
  154. read_config ( const char *file )
  155. {
  156. FILE *fp;
  157. int i = 0;
  158. struct patch_record *pr;
  159. if ( NULL == ( fp = fopen( file, "r" ) ) )
  160. return 0;
  161. clear_all_patches();
  162. while ( !feof( fp ) && !ferror( fp ) )
  163. {
  164. int retval;
  165. int k;
  166. char buf[BUFSIZ];
  167. i++;
  168. for ( k = 0; k < sizeof( buf ) - 1; k++ )
  169. {
  170. retval = fread( buf + k, 1, 1, fp );
  171. if ( retval != 1 )
  172. break;
  173. if ( buf[k] == '\n' )
  174. {
  175. if ( k == 0 )
  176. continue;
  177. else
  178. break;
  179. }
  180. }
  181. if ( retval == 0 )
  182. break;
  183. retval = process_patch( buf );
  184. if ( retval < 0 )
  185. break;
  186. if ( retval == 0 )
  187. {
  188. printf( "bad line %i.\n", i );
  189. continue;
  190. }
  191. }
  192. fclose( fp );
  193. return 1;
  194. }
  195. /* returns 0 if connection failed, true if succeeded. Already connected
  196. * is not considered failure */
  197. int
  198. connect_path ( struct patch_record *pr )
  199. {
  200. int r = 0;
  201. char srcport[512];
  202. char dstport[512];
  203. snprintf( srcport, 512, "%s:%s", pr->src.client, pr->src.port );
  204. snprintf( dstport, 512, "%s:%s", pr->dst.client, pr->dst.port );
  205. printf( "Connecting %s |> %s\n", srcport, dstport );
  206. r = jack_connect( client, srcport, dstport );
  207. print_patch( pr, r );
  208. if ( r == 0 || r == EEXIST )
  209. return 1;
  210. else
  211. {
  212. printf( "Error is %i", r );
  213. return 0;
  214. }
  215. }
  216. int
  217. activate_patch ( const char *portname )
  218. {
  219. struct patch_record *pr;
  220. int r = 0;
  221. char client[512];
  222. char port[512];
  223. sscanf( portname, "%[^:]:%s", client, port );
  224. for ( pr = patch_list; pr; pr = pr->next )
  225. {
  226. // printf( "checking %s:%s agains %s:%s\n", pr->src.client, pr->src.port, client, port );
  227. if ( ( !strcmp( client, pr->src.client ) && !strcmp( port, pr->src.port ) ) ||
  228. ( !strcmp( client, pr->dst.client ) && !strcmp( port, pr->dst.port ) ) )
  229. {
  230. return connect_path( pr );
  231. }
  232. }
  233. return 0;
  234. }
  235. /**
  236. * Attempt to activate all connections in patch list
  237. */
  238. void
  239. activate_all_patches ( void )
  240. {
  241. struct patch_record *pr;
  242. for ( pr = patch_list; pr; pr = pr->next )
  243. connect_path( pr );
  244. }
  245. int
  246. find_port ( const char *portname )
  247. {
  248. if ( ! all_ports || ! portname )
  249. return 0;
  250. const char **port;
  251. for ( port = all_ports; *port; port++ )
  252. {
  253. if ( 0 == strcmp( *port, portname ) )
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. /** called for every new port */
  259. int
  260. handle_new_port ( const char *portname )
  261. {
  262. printf( "New endpoint '%s' registered.\n", portname );
  263. /* this is a new port */
  264. return activate_patch( portname );
  265. }
  266. void
  267. wipe_ports ( void )
  268. {
  269. if ( all_ports )
  270. free( all_ports );
  271. all_ports = NULL;
  272. }
  273. void
  274. check_for_new_ports ( void )
  275. {
  276. const char **port;
  277. const char **ports = jack_get_ports( client, NULL, NULL, 0 );
  278. if ( ! ports )
  279. {
  280. printf( "error, no ports" );
  281. return;
  282. }
  283. for ( port = ports; *port; port++ )
  284. if ( ! find_port( *port ) )
  285. if ( ! handle_new_port( *port ) )
  286. {
  287. /* failed to connect. Probably because client is inactive. Mark it as invalid and try again later. */
  288. // *port = "RETRY";
  289. }
  290. if ( all_ports )
  291. free( all_ports );
  292. all_ports = ports;
  293. }
  294. void
  295. snapshot ( const char *file )
  296. {
  297. FILE *fp;
  298. const char **port;
  299. const char **ports = jack_get_ports( client, NULL, NULL, JackPortIsOutput );
  300. if ( ! ports )
  301. return;
  302. if ( NULL == ( fp = fopen( file, "w" ) ) )
  303. {
  304. fprintf( stderr, "Error opening snapshot file for writing" );
  305. return;
  306. }
  307. for ( port = ports; *port; port++ )
  308. {
  309. jack_port_t *p;
  310. p = jack_port_by_name( client, *port );
  311. const char **connections;
  312. const char **connection;
  313. connections = jack_port_get_all_connections( client, p );
  314. if ( ! connections )
  315. continue;
  316. for ( connection = connections; *connection; connection++ )
  317. {
  318. fprintf( fp, "%-40s |> %s\n", *port, *connection );
  319. printf( "++ %s |> %s\n", *port, *connection );
  320. }
  321. free( connections );
  322. }
  323. free( ports );
  324. fclose( fp );
  325. }
  326. static int die_now = 0;
  327. void
  328. signal_handler ( int x )
  329. {
  330. die_now = 1;
  331. }
  332. void
  333. die ( void )
  334. {
  335. jack_deactivate( client );
  336. jack_client_close( client );
  337. client = NULL;
  338. exit( 0 );
  339. }
  340. /** set_traps
  341. *
  342. * Handle signals
  343. */
  344. void
  345. set_traps ( void )
  346. {
  347. // signal( SIGHUP, signal_handler );
  348. signal( SIGINT, signal_handler );
  349. // signal( SIGQUIT, signal_handler );
  350. // signal( SIGSEGV, signal_handler );
  351. // signal( SIGPIPE, signal_handler );
  352. signal( SIGTERM, signal_handler );
  353. }
  354. /****************/
  355. /* OSC HANDLERS */
  356. /****************/
  357. int
  358. osc_announce_error ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  359. {
  360. if ( strcmp( "/nsm/server/announce", &argv[0]->s ) )
  361. return -1;
  362. printf( "Failed to register with NSM: %s\n", &argv[2]->s );
  363. nsm_is_active = 0;
  364. return 0;
  365. }
  366. int
  367. osc_announce_reply ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  368. {
  369. if ( strcmp( "/nsm/server/announce", &argv[0]->s ) )
  370. return -1;
  371. printf( "Successfully registered. NSM says: %s", &argv[1]->s );
  372. nsm_is_active = 1;
  373. nsm_addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ) );
  374. return 0;
  375. }
  376. int
  377. osc_save ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  378. {
  379. snapshot( project_file );
  380. lo_send_from( nsm_addr, losrv, LO_TT_IMMEDIATE, "/reply", "ss", path, "OK" );
  381. return 0;
  382. }
  383. int
  384. osc_open ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  385. {
  386. const char *new_path = &argv[0]->s;
  387. const char *display_name = &argv[1]->s;
  388. char *new_filename;
  389. asprintf( &new_filename, "%s.jackpatch", new_path );
  390. struct stat st;
  391. if ( 0 == stat( new_filename, &st ) )
  392. {
  393. if ( read_config( new_filename ) )
  394. {
  395. printf( "Reading patch definitions from: %s\n", new_filename );
  396. wipe_ports();
  397. check_for_new_ports();
  398. // activate_all_patches();
  399. }
  400. else
  401. {
  402. lo_send_from( nsm_addr, losrv, LO_TT_IMMEDIATE, "/error", "sis", path, -1, "Could not open file" );
  403. return 0;
  404. }
  405. }
  406. else
  407. {
  408. clear_all_patches();
  409. }
  410. if ( project_file )
  411. free( project_file );
  412. project_file = new_filename;
  413. lo_send_from( nsm_addr, losrv, LO_TT_IMMEDIATE, "/reply", "ss", path, "OK" );
  414. return 0;
  415. }
  416. void
  417. announce ( const char *nsm_url, const char *client_name, const char *process_name )
  418. {
  419. printf( "Announcing to NSM\n" );
  420. lo_address to = lo_address_new_from_url( nsm_url );
  421. int pid = (int)getpid();
  422. lo_send_from( to, losrv, LO_TT_IMMEDIATE, "/nsm/server/announce", "sssiii",
  423. client_name,
  424. ":switch:",
  425. process_name,
  426. 0, /* api_major_version */
  427. 8, /* api_minor_version */
  428. pid );
  429. lo_address_free( to );
  430. }
  431. void
  432. init_osc ( const char *osc_port )
  433. {
  434. losrv = lo_server_new( osc_port, NULL );
  435. //error_handler );
  436. char *url = lo_server_get_url(losrv);
  437. printf("OSC: %s\n",url);
  438. free(url);
  439. lo_server_add_method( losrv, "/nsm/client/save", "", osc_save, NULL );
  440. lo_server_add_method( losrv, "/nsm/client/open", "sss", osc_open, NULL );
  441. lo_server_add_method( losrv, "/error", "sis", osc_announce_error, NULL );
  442. lo_server_add_method( losrv, "/reply", "ssss", osc_announce_reply, NULL );
  443. }
  444. /* */
  445. int
  446. main ( int argc, char **argv )
  447. {
  448. /* get_args( argc, argv ); */
  449. jack_status_t status;
  450. client = jack_client_open( APP_TITLE, JackNullOption, &status );
  451. if ( ! client )
  452. {
  453. fprintf( stderr, "Could not register JACK client\n" );
  454. exit(1);
  455. }
  456. jack_activate( client );
  457. // activate_all_patches();
  458. set_traps();
  459. if ( argc > 1 )
  460. {
  461. if ( ! strcmp( argv[1], "--save" ) )
  462. {
  463. if ( argc > 2 )
  464. {
  465. printf( "Saving current graph to: %s\n", argv[2] );
  466. snapshot( argv[2] );
  467. exit(0);
  468. }
  469. }
  470. else
  471. {
  472. read_config( argv[1] );
  473. printf( "Monitoring...\n" );
  474. for ( ;; )
  475. {
  476. check_for_new_ports();
  477. usleep( 50000 );
  478. }
  479. }
  480. }
  481. init_osc( NULL );
  482. const char *nsm_url = getenv( "NSM_URL" );
  483. if ( nsm_url )
  484. {
  485. announce( nsm_url, APP_TITLE, argv[0] );
  486. }
  487. else
  488. {
  489. fprintf( stderr, "Could not register as NSM client.\n" );
  490. exit(1);
  491. }
  492. for ( ;; )
  493. {
  494. lo_server_recv_noblock( losrv, 500 );
  495. check_for_new_ports();
  496. if ( die_now )
  497. die();
  498. }
  499. }