jack_connectd

Simple daemon for connecting and disconnecting ports upon registration for the JACK audio system
Log | Files | Refs | README | LICENSE

main.c (1808B)


      1 #include <arpa/tftp.h>
      2 #include <jack/jack.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "config.h"
      8 
      9 #define LEN(arr)	(sizeof(arr) / sizeof(arr[0]))
     10 
     11 static void
     12 registration_cb(jack_port_id_t port_id, int is_registered, void *arg)
     13 {
     14 	struct jack_connection_t conn;
     15 	jack_client_t *client;
     16 	jack_port_t *port;
     17 	const char *port_name;
     18 	int ret;
     19 
     20 	client = (jack_client_t *)arg;
     21 	port = jack_port_by_id(client, port_id);
     22 	port_name = jack_port_name(port);
     23 
     24 	if (is_registered) {
     25 		for (unsigned long i = 0; i < LEN(auto_connects); i++) {
     26 			conn = auto_connects[i];
     27 
     28 			if (strcmp(port_name, conn.in)) {
     29 				if (jack_port_flags(port) & JackPortIsInput) {
     30 					ret = jack_connect(client, port_name, conn.out);
     31 					if (ret != 0 && ret != EEXISTS) {
     32 						fprintf(stderr, "Failed to connect %s to %s.\n",
     33 						    port_name, conn.out);
     34 					}
     35 				}
     36 			} else if (strcmp(port_name, conn.out)) {
     37 				if (jack_port_flags(port) & JackPortIsOutput) {
     38 					ret = jack_connect(client, conn.in, port_name);
     39 					if (ret != 0 && ret != EEXISTS) {
     40 						fprintf(stderr, "Failed to connect %s to %s.\n",
     41 						    conn.in, port_name);
     42 					}
     43 				}
     44 			}
     45 		}
     46 
     47 		printf("%s registered\n", port_name);
     48 	} else {
     49 		printf("%s unregistered\n", port_name);
     50 	}
     51 }
     52 
     53 int
     54 main(void)
     55 {
     56 	jack_client_t *client;
     57 
     58 	client = jack_client_open("jack_connectd", JackNullOption, NULL);
     59 	if (client == NULL) {
     60 		fprintf(stderr, "Failed to connect to JACK server.\n");
     61 		return 1;
     62 	}
     63 
     64 	/* client is passed as arg so it can be used within cb */
     65 	jack_set_port_registration_callback(client, registration_cb, client);
     66 
     67 	/* start processing callbacks */
     68 	if (jack_activate(client) != 0) {
     69 		fprintf(stderr, "Failed to activate JACK client.\n");
     70 		return 1;
     71 	}
     72 
     73 	/* do nothing else while waiting */
     74 	for (;;);
     75 
     76 	return 0;
     77 }