Commit b5a7f0ec authored by minus's avatar minus

Toggle button LED on mute & speakers

parent cdce3a82
......@@ -18,7 +18,7 @@ static void xpa_on_sink_input_info(pa_context *ctx,
struct sink_input_watch *w = sink_inputs_get(info->name);
if (w) {
sink_input_watch_add_index(w, info->index);
sink_input_watch_update(w, info->volume.values[0], info->mute != 0);
sink_input_watch_update(w, info->sink, info->volume.values[0], info->mute != 0);
}
}
......
......@@ -35,7 +35,8 @@ void sink_input_watch_remove_index(struct sink_input_watch *w, uint32_t pa_index
}
}
void sink_input_watch_update(struct sink_input_watch *w, pa_volume_t volume, bool mute) {
void sink_input_watch_update(struct sink_input_watch *w, uint32_t sink_index, pa_volume_t volume, bool mute) {
w->sink = sink_index;
w->volume = volume;
w->mute = mute;
if (w->volume_changed) {
......@@ -49,6 +50,7 @@ struct sink_input_watch *sink_inputs_add_sink(const char* sink_name) {
sizeof(struct sink_input_watch) * sink_inputs.len);
struct sink_input_watch *item = &sink_inputs.items[sink_inputs.len - 1];
item->name = strdup(sink_name);
item->sink = -1;
item->volume = PA_VOLUME_MUTED;
item->mute = true;
item->indices.len = 0;
......
......@@ -3,6 +3,7 @@
struct sink_input_watch {
const char *name;
uint32_t sink;
pa_volume_t volume;
bool mute;
struct indices_list {
......@@ -15,7 +16,7 @@ struct sink_input_watch {
void sink_input_watch_add_index(struct sink_input_watch *w, uint32_t pa_index);
void sink_input_watch_remove_index(struct sink_input_watch *w, uint32_t pa_index);
void sink_input_watch_update(struct sink_input_watch *w, pa_volume_t volume, bool mute);
void sink_input_watch_update(struct sink_input_watch *w, uint32_t sink_index, pa_volume_t volume, bool mute);
struct sink_input_watch *sink_inputs_add_sink(const char* sink_name);
struct sink_input_watch *sink_inputs_get(const char *sink_name);
void sink_inputs_remove_index(uint32_t pa_index);
......@@ -37,6 +37,8 @@ gboolean on_alsa_readable(GIOChannel *source, GIOCondition condition, gpointer d
return TRUE;
}
static int port;
snd_seq_t *alsa(int source) {
// connect to ALSA seq
snd_seq_t *seq;
......@@ -45,7 +47,7 @@ snd_seq_t *alsa(int source) {
exit(1);
}
snd_seq_set_client_name(seq, CLIENT_NAME);
int port = snd_seq_create_simple_port(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);
......@@ -104,6 +106,25 @@ 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) {
snd_seq_event_t ev;
snd_seq_ev_clear(&ev);
snd_seq_ev_set_source(&ev, port);
snd_seq_ev_set_subs(&ev);
snd_seq_ev_set_direct(&ev);
snd_seq_ev_set_noteon(&ev, 0, button, state);
snd_seq_event_output_direct(seq, &ev);
}
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);
uint32_t speakers = 3;
uint32_t headphones = 4;
midi_button_led(seq, BOTTOM1, w->sink == speakers ? BUTTON_LED_ON : BUTTON_LED_OFF);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
......@@ -112,10 +133,13 @@ int main(int argc, char *argv[]) {
}
int source = atoi(argv[1]);
snd_seq_t *seq = alsa(source);
struct sink_input_watch *mpd = sink_inputs_add_sink("mpd pulse");
mpd->userdata = seq;
mpd->volume_changed = show_mute_and_speakers;
pa();
snd_seq_t *seq = alsa(source);
// create PA watch object for sink name "mpd pulse"
// with a callback function on change (info, sinks)
......@@ -127,7 +151,7 @@ int main(int argc, char *argv[]) {
// on change from MIDI side, the callback is executed and passed the
// MIDI action as well as the mapping struct with the PA watch reference
GMainLoop *main_loop = g_main_loop_new (NULL, FALSE);
GMainLoop *main_loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(main_loop);
g_main_loop_unref(main_loop);
return 0;
......
......@@ -24,6 +24,11 @@ enum button {
LAYERB = 85,
};
enum button_led_state {
BUTTON_LED_ON = 127,
BUTTON_LED_OFF = 0,
};
enum encoder {
ENC1 = 16,
ENC2 = 17,
......
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