From 20336dc6e0638fe7e5a2c68a3e0ec756796f8bcb Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Sat, 7 May 2011 17:42:09 +0200 Subject: [PATCH] Rinonimato il tutto in audio_switcher --- Makefile | 7 +++-- audio_switcher.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ robolmute.c | 88 -------------------------------------------------------- 3 files changed, 93 insertions(+), 90 deletions(-) create mode 100644 audio_switcher.c delete mode 100644 robolmute.c diff --git a/Makefile b/Makefile index 73f8d02..ca44a11 100644 --- a/Makefile +++ b/Makefile @@ -2,5 +2,8 @@ LDFLAGS=$(shell pkg-config --libs glib-2.0 gdk-2.0) CFLAGS=$(shell pkg-config --cflags glib-2.0 gdk-2.0) LDFLAGS+= -lpulse -lnotify -robolmute: robolmute.c - cc -o robolmute $(CFLAGS) $(LDFLAGS) robolmute.c +audio_switcher: audio_switcher.c + cc -o audio_switcher $(CFLAGS) $(LDFLAGS) audio_switcher.c + +clean: + rm audio_switcher diff --git a/audio_switcher.c b/audio_switcher.c new file mode 100644 index 0000000..566b7fd --- /dev/null +++ b/audio_switcher.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include + +/* Global variable to keep track of the main pulse audio loop */ +pa_mainloop* mainloop; + +void +success_callback(pa_context *c, int success, char* new_output) +{ + /* Notify to the user that the switch was successful */ + GError* error = NULL; + char body[255]; + if (strlen(new_output) < 200) + sprintf(body, "New active output: %s", new_output); + else + strncpy(body, new_output, 255); + NotifyNotification *notification = notify_notification_new ("Audio switched", + body, + "gnome-mixer"); + notify_notification_show (notification, + &error); + + /* Exit the pulseaudio mainloop */ + pa_mainloop_quit(mainloop, success); +} + + +void +toggle_mute(pa_context *c, pa_sink_info *i, void* user_data) +{ + int j; + if (i) { + /* Cycle through available active ports. This should + * rotate between headphones and/or analog output. */ + for(j = 0; j < i->n_ports; j++) { + if (i->active_port == i->ports[j]) { + pa_context_set_sink_port_by_index(c, + i->index, + i->ports[(j + 1) % i->n_ports]->name, + (pa_context_success_cb_t) success_callback, + (char*) i->ports[(j+1) % i->n_ports]->description); + } + } + } +} + + +void +context_connected_cb(pa_context *c, void* user_data) +{ + if (pa_context_get_state(c) == PA_CONTEXT_READY) { + pa_context_get_sink_info_list(c, + (pa_sink_info_cb_t) toggle_mute, + NULL); + } +} + +int +main() +{ + mainloop = pa_mainloop_new(); + pa_mainloop_api *api = pa_mainloop_get_api (mainloop); + pa_context *c; + pa_operation* operation; + pa_operation_state_t state = PA_OPERATION_RUNNING; + int retval; + pa_proplist *proplist; + + notify_init ("AudioSwitch"); + + proplist = pa_proplist_new(); + pa_proplist_sets(proplist, PA_PROP_DEVICE_API, "alsa"); + c = pa_context_new_with_proplist(api, "Audio Switcher", + proplist); + + /* Connect to the default Pulseaudio server, ask for + * information about Front so we can check if it is + * already muted or not. */ + pa_context_connect(c, NULL, PA_CONTEXT_NOFLAGS, NULL); + pa_context_set_state_callback(c, (pa_context_notify_cb_t) context_connected_cb, mainloop); + + /* Loop :) */ + pa_mainloop_run(mainloop, &retval); + return retval; +} diff --git a/robolmute.c b/robolmute.c deleted file mode 100644 index 41c4078..0000000 --- a/robolmute.c +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include -#include - -/* Global variable to keep track of the main pulse audio loop */ -pa_mainloop* mainloop; - -void -success_callback(pa_context *c, int success, char* new_output) -{ - /* Notify to the user that the switch was successful */ - GError* error = NULL; - char body[255]; - if (strlen(new_output) < 200) - sprintf(body, "New active output: %s", new_output); - else - strncpy(body, new_output, 255); - NotifyNotification *notification = notify_notification_new ("Audio switched", - body, - "gnome-mixer"); - notify_notification_show (notification, - &error); - - /* Exit the pulseaudio mainloop */ - pa_mainloop_quit(mainloop, success); -} - - -void -toggle_mute(pa_context *c, pa_sink_info *i, void* user_data) -{ - int j; - if (i) { - /* Cycle through available active ports. This should - * rotate between headphones and/or analog output. */ - for(j = 0; j < i->n_ports; j++) { - if (i->active_port == i->ports[j]) { - pa_context_set_sink_port_by_index(c, - i->index, - i->ports[(j + 1) % i->n_ports]->name, - (pa_context_success_cb_t) success_callback, - (char*) i->ports[(j+1) % i->n_ports]->description); - } - } - } -} - - -void -context_connected_cb(pa_context *c, void* user_data) -{ - if (pa_context_get_state(c) == PA_CONTEXT_READY) { - pa_context_get_sink_info_list(c, - (pa_sink_info_cb_t) toggle_mute, - NULL); - } -} - -int -main() -{ - mainloop = pa_mainloop_new(); - pa_mainloop_api *api = pa_mainloop_get_api (mainloop); - pa_context *c; - pa_operation* operation; - pa_operation_state_t state = PA_OPERATION_RUNNING; - int retval; - pa_proplist *proplist; - - notify_init ("AudioSwitch"); - - proplist = pa_proplist_new(); - pa_proplist_sets(proplist, PA_PROP_DEVICE_API, "alsa"); - c = pa_context_new_with_proplist(api, "RobolMute", - proplist); - - /* Connect to the default Pulseaudio server, ask for - * information about Front so we can check if it is - * already muted or not. */ - pa_context_connect(c, NULL, PA_CONTEXT_NOFLAGS, NULL); - pa_context_set_state_callback(c, (pa_context_notify_cb_t) context_connected_cb, mainloop); - - /* Loop :) */ - pa_mainloop_run(mainloop, &retval); - return retval; -} -- 2.1.4