From 0ec8963a62d95322a0848ce75965b7481d05bda2 Mon Sep 17 00:00:00 2001 From: vin Date: Fri, 30 May 2025 23:24:50 -0400 Subject: write the callback --- config.h | 8 ++++++++ main.c | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 config.h diff --git a/config.h b/config.h new file mode 100644 index 0000000..f989fef --- /dev/null +++ b/config.h @@ -0,0 +1,8 @@ +struct jack_connection_t { + const char *in; + const char *out; +}; + +struct jack_connection_t auto_connects[] = { + {"system:capture_1", "system:playback_1"}, +}; diff --git a/main.c b/main.c index a821ef3..a6066c2 100644 --- a/main.c +++ b/main.c @@ -1,15 +1,49 @@ +#include #include #include #include +#include + +#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) { - jack_client_t *client = (jack_client_t *)arg; - jack_port_t *port = jack_port_by_id(client, port_id); - const char *port_name = jack_port_name(port); + 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); -- cgit v1.2.3