Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pop sound on volume change with libcanberra #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ PKG_CHECK_MODULES([LIBPULSE], [libpulse])
PKG_CHECK_MODULES([LIBPULSE_GLIB], [libpulse-mainloop-glib])
PKG_CHECK_MODULES([LIBNOTIFY], [libnotify])
PKG_CHECK_MODULES([XLIB], [x11])
PKG_CHECK_MODULES([LIBCANBERRA], [libcanberra])

AC_CONFIG_FILES([Makefile man/Makefile src/Makefile])
AC_OUTPUT
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pa_applet_SOURCES = \
main.c \
notifications.h \
notifications.c \
popsound.h \
popsound.c \
popup_menu.c \
popup_menu.h \
pulse_glue.c \
Expand All @@ -24,6 +26,7 @@ pa_applet_CPPFLAGS = \
$(LIBPULSE_CFLAGS) \
$(LIBPULSE_GLIB_CFLAGS) \
$(LIBNOTIFY_CFLAGS) \
$(LIBCANBERRA_CFLAGS) \
$(XLIB_CFLAGS)

pa_applet_LDADD = \
Expand All @@ -32,4 +35,5 @@ pa_applet_LDADD = \
$(LIBPULSE_LIBS) \
$(LIBPULSE_GLIB_LIBS) \
$(LIBNOTIFY_LIBS) \
$(LIBCANBERRA_LIBS) \
$(XLIB_LIBS)
7 changes: 7 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "audio_status.h"
#include "key_grabber.h"
#include "notifications.h"
#include "popsound.h"
#include "pulse_glue.h"
#include "tray_icon.h"

Expand All @@ -25,20 +26,23 @@ static void volume_raise_key_pressed(void)
audio_status_raise_volume();
pulse_glue_sync_volume();
notifications_flash();
popsound_play();
}

static void volume_lower_key_pressed(void)
{
audio_status_lower_volume();
pulse_glue_sync_volume();
notifications_flash();
popsound_play();
}

static void volume_mute_key_pressed(void)
{
audio_status_toggle_muted();
pulse_glue_sync_muted();
notifications_flash();
popsound_play();
}

static void print_usage(FILE *out)
Expand Down Expand Up @@ -93,6 +97,8 @@ int main(int argc, char **argv)
if (notifications_enabled)
notifications_init();

popsound_init();

// Grab the keys if we're configured to grab them
if (key_grabbing_enabled) {
key_grabber_register_volume_raise_callback(volume_raise_key_pressed);
Expand All @@ -112,6 +118,7 @@ int main(int argc, char **argv)
key_grabber_ungrab_keys();
if (notifications_enabled)
notifications_destroy();
popsound_destroy();
destroy_tray_icon();
pulse_glue_destroy();
audio_status_destroy();
Expand Down
48 changes: 48 additions & 0 deletions src/popsound.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of pa-applet.
*
* Refer to the LICENSE file for licensing information.
*
*/

/*
Plays a sound with libcanberra when volume is changed with keypress
Event sounds can be configured / disabled in gtk config
See also:
http://0pointer.de/lennart/projects/libcanberra/
*/

#include "audio_status.h"

#include <canberra.h>

ca_context * cacontext;

void popsound_play(void)
{
audio_status *as = shared_audio_status();

// play the sound if not muted, and canberra initialized correctly
if ( (! as->muted) && (cacontext != NULL) ) {
ca_context_play (cacontext, 1,
CA_PROP_EVENT_ID, "audio-volume-change",
CA_PROP_EVENT_DESCRIPTION, "volume changed through key press",
CA_PROP_CANBERRA_CACHE_CONTROL, "permanent",
NULL);
}
}

void popsound_init(void)
{
if (ca_context_create (&cacontext) == 0) {
ca_context_set_driver (cacontext, "pulse");
} else {
cacontext = NULL;
g_printerr("Failed to create libcanbera context\n");
}
}

void popsound_destroy(void)
{
ca_context_destroy(cacontext);
}
15 changes: 15 additions & 0 deletions src/popsound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* This file is part of pa-applet.
*
* Refer to the LICENSE file for licensing information.
*
*/

#ifndef POPSOUND_H
#define POPSOUND_H

void popsound_init(void);
void popsound_destroy(void);
void popsound_play(void);

#endif
14 changes: 13 additions & 1 deletion src/volume_scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,22 @@ static void do_show_volume_scale(GdkRectangle *rect_or_null)

// Position the window
if (rect_or_null) {
// Get monitor coordinates
GdkRectangle rectmonitor;
GdkScreen *screen = gtk_widget_get_screen(window);
gdk_screen_get_monitor_geometry(screen, gdk_screen_get_monitor_at_point(screen, rect_or_null->x, rect_or_null->y), &rectmonitor);

gint size_x, size_y;
gtk_window_get_size(GTK_WINDOW(window), &size_x, &size_y);
gint x = rect_or_null->x + (rect_or_null->width - size_x) / 2;
gint y = rect_or_null->y > size_y ? rect_or_null->y - size_y : rect_or_null->y + rect_or_null->height;

// set y assuming window above tray icon (tray icon bottom of monitor)
gint y = rect_or_null->y - size_y;
// if y not in the monitor: wrong assumption --> window bellow tray icon
if (! (rectmonitor.y < y && y < rectmonitor.y+rectmonitor.height) ) {
y = rect_or_null->y + rect_or_null->height;
}

gtk_window_move(GTK_WINDOW(window), x, y);
}

Expand Down