Commit 982e8f97 authored by minus's avatar minus

Move ALSA code to separate file

parent b5a7f0ec
#include <glib.h>
#include "panelcontrol.h"
#include "alsa.h"
static gboolean on_alsa_readable(GIOChannel *source, GIOCondition condition, gpointer data) {
fprintf(stdout, "source=%p cond=%i data=%p\n", (void *)source, condition, data);
snd_seq_t *seq = (snd_seq_t *)data;
snd_seq_event_t *ev;
while (1) {
int remaining = snd_seq_event_input(seq, &ev);
if (remaining == -EAGAIN) {
return TRUE;
} else if (remaining < 0) {
perror("Error reading sequencer input");
}
switch (ev->type) {
case SND_SEQ_EVENT_NOTEON:
fprintf(stdout, "button %i: %i\n", ev->data.note.note, ev->data.note.velocity > 0);
snd_seq_ev_set_subs(ev);
snd_seq_ev_set_direct(ev);
snd_seq_event_output_direct(seq, ev);
break;
case SND_SEQ_EVENT_CONTROLLER:
fprintf(stdout, "knob %i: %i\n", ev->data.control.param, ev->data.control.value);
break;
}
snd_seq_free_event(ev);
}
return TRUE;
}
static int port;
void alsa(int source, snd_seq_t **seq_ret, int *port_ret) {
// connect to ALSA seq
snd_seq_t *seq;
if (snd_seq_open(&seq, "hw", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK) < 0) {
fprintf(stderr, "Error opening ALSA sequencer.\n");
exit(1);
}
if (seq_ret) *seq_ret = seq;
snd_seq_set_client_name(seq, CLIENT_NAME);
int port = snd_seq_create_simple_port(seq, CLIENT_NAME,
SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE
|SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
SND_SEQ_PORT_TYPE_APPLICATION);
if (port < 0) {
fprintf(stderr, "Error creating sequencer port.\n");
exit(1);
}
if (port_ret) *port_ret = port;
// read from and write to panel device
snd_seq_connect_from(seq, port, source, 0);
snd_seq_connect_to(seq, port, source, 0);
// event loop
int npfd;
struct pollfd *pfd;
npfd = snd_seq_poll_descriptors_count(seq, POLLIN);
pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd));
snd_seq_poll_descriptors(seq, pfd, npfd, POLLIN);
for (int i = 0; i < npfd; ++i) {
GIOChannel *ch = g_io_channel_unix_new(pfd[i].fd);
guint evsrcid = g_io_add_watch(ch, pfd[i].events, &on_alsa_readable, seq);
}
}
#include <alsa/asoundlib.h>
void alsa(int source, snd_seq_t **seq_ret, int *port_ret);
......@@ -9,7 +9,7 @@ alsa = dependency('alsa')
glib = dependency('glib-2.0')
executable('panelcontrol',
['panelcontrol.c', 'pa.c', 'pa_sink_input_watch_list.c'],
['panelcontrol.c', 'alsa.c', 'pa.c', 'pa_sink_input_watch_list.c'],
dependencies : [pa, alsa, glib],
install : true)
......
......@@ -4,78 +4,10 @@
#include <glib.h>
#include "panelcontrol.h"
#include "alsa.h"
#include "pa.h"
#include "pa_sink_input_watch_list.h"
gboolean on_alsa_readable(GIOChannel *source, GIOCondition condition, gpointer data) {
fprintf(stdout, "source=%p cond=%i data=%p\n", (void *)source, condition, data);
snd_seq_t *seq = (snd_seq_t *)data;
snd_seq_event_t *ev;
while (1) {
int remaining = snd_seq_event_input(seq, &ev);
if (remaining == -EAGAIN) {
return TRUE;
} else if (remaining < 0) {
perror("Error reading sequencer input");
}
switch (ev->type) {
case SND_SEQ_EVENT_NOTEON:
fprintf(stdout, "button %i: %i\n", ev->data.note.note, ev->data.note.velocity > 0);
snd_seq_ev_set_subs(ev);
snd_seq_ev_set_direct(ev);
snd_seq_event_output_direct(seq, ev);
break;
case SND_SEQ_EVENT_CONTROLLER:
fprintf(stdout, "knob %i: %i\n", ev->data.control.param, ev->data.control.value);
break;
}
snd_seq_free_event(ev);
}
return TRUE;
}
static int port;
snd_seq_t *alsa(int source) {
// connect to ALSA seq
snd_seq_t *seq;
if (snd_seq_open(&seq, "hw", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK) < 0) {
fprintf(stderr, "Error opening ALSA sequencer.\n");
exit(1);
}
snd_seq_set_client_name(seq, CLIENT_NAME);
port = snd_seq_create_simple_port(seq, CLIENT_NAME,
SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE
|SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
SND_SEQ_PORT_TYPE_APPLICATION);
if (port < 0) {
fprintf(stderr, "Error creating sequencer port.\n");
exit(1);
}
// read from and write to panel device
snd_seq_connect_from(seq, port, source, 0);
snd_seq_connect_to(seq, port, source, 0);
// event loop
int npfd;
struct pollfd *pfd;
npfd = snd_seq_poll_descriptors_count(seq, POLLIN);
pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd));
snd_seq_poll_descriptors(seq, pfd, npfd, POLLIN);
for (int i = 0; i < npfd; ++i) {
GIOChannel *ch = g_io_channel_unix_new(pfd[i].fd);
guint evsrcid = g_io_add_watch(ch, pfd[i].events, &on_alsa_readable, seq);
}
return seq;
}
//void toggle_mute(enum button button, void *userdata) {
// const char *name = (const char *)userdata;
......@@ -106,7 +38,14 @@ snd_seq_t *alsa(int source) {
//static const struct encoder_map_entry encoder_map[] = {
// {.encoder = ENC1, .sink_input = NULL, .action = &change_volume},
//};
void midi_button_led(snd_seq_t *seq, enum button button, enum button_led_state state) {
struct seq_port {
snd_seq_t *seq;
int port;
};
void midi_button_led(snd_seq_t *seq, int port, enum button button, enum button_led_state state) {
snd_seq_event_t ev;
snd_seq_ev_clear(&ev);
snd_seq_ev_set_source(&ev, port);
......@@ -117,13 +56,13 @@ void midi_button_led(snd_seq_t *seq, enum button button, enum button_led_state s
}
void show_mute_and_speakers(struct sink_input_watch *w) {
snd_seq_t *seq = w->userdata;
midi_button_led(seq, TOP1, w->mute ? BUTTON_LED_ON : BUTTON_LED_OFF);
struct seq_port *sp = w->userdata;
midi_button_led(sp->seq, sp->port, TOP1, w->mute ? BUTTON_LED_ON : BUTTON_LED_OFF);
uint32_t speakers = 3;
uint32_t headphones = 4;
midi_button_led(seq, BOTTOM1, w->sink == speakers ? BUTTON_LED_ON : BUTTON_LED_OFF);
midi_button_led(sp->seq, sp->port, BOTTOM1, w->sink == speakers ? BUTTON_LED_ON : BUTTON_LED_OFF);
}
int main(int argc, char *argv[]) {
......@@ -133,10 +72,11 @@ int main(int argc, char *argv[]) {
}
int source = atoi(argv[1]);
snd_seq_t *seq = alsa(source);
struct seq_port sp;
alsa(source, &sp.seq, &sp.port);
struct sink_input_watch *mpd = sink_inputs_add_sink("mpd pulse");
mpd->userdata = seq;
mpd->userdata = &sp;
mpd->volume_changed = show_mute_and_speakers;
pa();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment