summaryrefslogtreecommitdiff
path: root/main.c
blob: a6066c22f2e66ca549ee98b1b5a91780c3ece213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <arpa/tftp.h>
#include <jack/jack.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"

#define LEN(arr)	(sizeof(arr) / sizeof(arr[0]))

static void
registration_cb(jack_port_id_t port_id, int is_registered, void *arg)
{
	struct jack_connection_t conn;
	jack_client_t *client;
	jack_port_t *port;
	const char *port_name;
	int ret;

	client = (jack_client_t *)arg;
	port = jack_port_by_id(client, port_id);
	port_name = jack_port_name(port);

	if (is_registered) {
		for (unsigned long i = 0; i < LEN(auto_connects); i++) {
			conn = auto_connects[i];

			if (strcmp(port_name, conn.in)) {
				if (jack_port_flags(port) & JackPortIsInput) {
					ret = jack_connect(client, port_name, conn.out);
					if (ret != 0 && ret != EEXISTS) {
						fprintf(stderr, "Failed to connect %s to %s.\n",
						    port_name, conn.out);
					}
				}
			} else if (strcmp(port_name, conn.out)) {
				if (jack_port_flags(port) & JackPortIsOutput) {
					ret = jack_connect(client, conn.in, port_name);
					if (ret != 0 && ret != EEXISTS) {
						fprintf(stderr, "Failed to connect %s to %s.\n",
						    conn.in, port_name);
					}
				}
			}
		}

		printf("%s registered\n", port_name);
	} else {
		printf("%s unregistered\n", port_name);
	}
}

int
main(void)
{
	jack_client_t *client;

	client = jack_client_open("jack_connectd", JackNullOption, NULL);
	if (client == NULL) {
		fprintf(stderr, "Failed to connect to JACK server.\n");
		return 1;
	}

	/* client is passed as arg so it can be used within cb */
	jack_set_port_registration_callback(client, registration_cb, client);

	/* start processing callbacks */
	if (jack_activate(client) != 0) {
		fprintf(stderr, "Failed to activate JACK client.\n");
		return 1;
	}

	/* do nothing else while waiting */
	for (;;);

	return 0;
}