Allow using ALSA client name to select output in Linux/ALSA version

This commit is contained in:
M-HT committed 2023-11-28 21:29:39 +01:00
1 parent 7cb4c429c5
commit 5305f059d0
1 file changed
+21 -19
+21 -19
View File
@@ -28,17 +28,27 @@ typedef enum {
static snd_seq_t *seq = NULL;
static int src_port = -1, dst_port = -1, dst_client = -1;
static char *alsa_client = NULL;
static char *dev_optstr = "p:";
int mididev_init(void) {
if(dst_port == -1 || dst_client == -1) {
fprintf(stderr, "ERROR: You must specify ALSA client and port using -p [CLIENT]:[PORT]\n");
if(snd_seq_open(&seq, "default", SND_SEQ_OPEN_OUTPUT, 0) < 0) {
fprintf(stderr, "ERROR: Could not open ALSA sequencer.\n");
return -1;
}
if(snd_seq_open(&seq, "default", SND_SEQ_OPEN_OUTPUT, 0) < 0) {
fprintf(stderr, "ERROR: Could not open ALSA sequencer.\n");
if(alsa_client != NULL && *alsa_client != 0) {
snd_seq_addr_t addr;
if(snd_seq_parse_address(seq, &addr, alsa_client) >= 0) {
dst_client = addr.client;
dst_port = addr.port;
}
}
if(dst_port == -1 || dst_client == -1) {
fprintf(stderr, "ERROR: You must specify ALSA client and port using -p [CLIENT]:[PORT]\n");
snd_seq_close(seq);
return -1;
}
@@ -150,25 +160,17 @@ int mididev_parse_arg(int c, const char *optarg) {
fprintf(stderr, "ERROR: Unknown option \'%c\'.\n", c);
return -1;
}
if(alsa_client != NULL) {
free(alsa_client);
alsa_client = NULL;
}
size_t len = strlen(optarg);
char *temparg = (char*) malloc(len+1);
if(temparg == NULL) {
alsa_client = (char*) malloc(len+1);
if(alsa_client == NULL) {
fprintf(stderr, "Error allocating memory.\n");
return -1;
}
memcpy(temparg, optarg, len+1);
char *firstarg = strtok(temparg, ":");
if(firstarg == NULL) {
return -1;
}
char *secondarg = firstarg + strlen(firstarg) + 1;
if(secondarg - temparg >= len) {
return -1;
}
dst_client = (int) strtol(firstarg, NULL, 10);
dst_port = (int) strtol(secondarg, NULL, 10);
free(temparg);
memcpy(alsa_client, optarg, len+1);
return 0;
}