From 88af35c0aba64783e111dfb0585d7958a9e9963e Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Sat, 14 Mar 2009 11:35:40 +0100
Subject: [PATCH] volume: moved code to mixer_all.c

---
 Makefile.am     |  2 ++
 src/mixer_all.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/mixer_all.h | 47 +++++++++++++++++++++++++++++++++++++++
 src/volume.c    | 35 +++++------------------------
 4 files changed, 113 insertions(+), 30 deletions(-)
 create mode 100644 src/mixer_all.c
 create mode 100644 src/mixer_all.h

diff --git a/Makefile.am b/Makefile.am
index bd261e372..bc5e58f54 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -79,6 +79,7 @@ mpd_headers = \
 	src/log.h \
 	src/ls.h \
 	src/main.h \
+	src/mixer_all.h \
 	src/mixer_api.h \
 	src/mixer_control.h \
 	src/event_pipe.h \
@@ -457,6 +458,7 @@ OUTPUT_SRC = \
 
 MIXER_API_SRC = \
 	src/mixer_control.c \
+	src/mixer_all.c \
 	src/mixer_api.c
 
 MIXER_SRC =
diff --git a/src/mixer_all.c b/src/mixer_all.c
new file mode 100644
index 000000000..de5856bbb
--- /dev/null
+++ b/src/mixer_all.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2003-2009 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "mixer_all.h"
+#include "mixer_control.h"
+#include "output_all.h"
+
+#include <glib.h>
+
+#undef G_LOG_DOMAIN
+#define G_LOG_DOMAIN "mixer"
+
+int
+mixer_all_get_volume(void)
+{
+	unsigned count = audio_output_count(), ok = 0;
+	int volume, total = 0;
+
+	for (unsigned i = 0; i < count; i++) {
+		if (mixer_control_getvol(i, &volume)) {
+			g_debug("device %d: volume=%d", i, volume);
+			total += volume;
+			++ok;
+		}
+	}
+
+	if (ok == 0)
+		return -1;
+
+	return total / ok;
+}
+
+bool
+mixer_all_set_volume(int volume, bool relative)
+{
+	bool success = false;
+	unsigned count = audio_output_count();
+
+	for (unsigned i = 0; i < count; i++)
+		success = mixer_control_setvol(i, volume, relative) || success;
+
+	return success;
+}
diff --git a/src/mixer_all.h b/src/mixer_all.h
new file mode 100644
index 000000000..66c4988de
--- /dev/null
+++ b/src/mixer_all.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2003-2009 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/** \file
+ *
+ * Functions which affect the mixers of all audio outputs.
+ */
+
+#ifndef MPD_MIXER_ALL_H
+#define MPD_MIXER_ALL_H
+
+#include <stdbool.h>
+
+/**
+ * Returns the average volume of all available mixers (range 0..100).
+ * Returns -1 if no mixer can be queried.
+ */
+int
+mixer_all_get_volume(void);
+
+/**
+ * Sets the volume on all available mixers.
+ *
+ * @param volume the volume (range 0..100 or -100..100 if #relative)
+ * @param relative if true, then the #volume is added to the current value
+ * @return true on success, false on failure
+ */
+bool
+mixer_all_set_volume(int volume, bool relative);
+
+#endif
diff --git a/src/volume.c b/src/volume.c
index 8e97280a4..a7bae9627 100644
--- a/src/volume.c
+++ b/src/volume.c
@@ -23,9 +23,9 @@
 #include "idle.h"
 #include "pcm_volume.h"
 #include "config.h"
-#include "mixer_control.h"
 #include "output_all.h"
 #include "mixer_api.h"
+#include "mixer_all.h"
 
 #include <glib.h>
 
@@ -166,9 +166,6 @@ void volume_init(void)
 
 static int hardware_volume_get(void)
 {
-	int device, count;
-	int volume, volume_total, volume_ok;
-
 	assert(hardware_volume_timer != NULL);
 
 	if (last_hardware_volume >= 0 &&
@@ -176,25 +173,9 @@ static int hardware_volume_get(void)
 		/* throttle access to hardware mixers */
 		return last_hardware_volume;
 
-	volume_total = 0;
-	volume_ok = 0;
-
-	count = audio_output_count();
-	for (device=0; device<count ;device++) {
-		if (mixer_control_getvol(device, &volume)) {
-			g_debug("device %d: volume: %d\n", device, volume);
-			volume_total += volume;
-			volume_ok++;
-		}
-	}
-	if (volume_ok > 0) {
-		//return average
-		last_hardware_volume = volume_total / volume_ok;
-		g_timer_start(hardware_volume_timer);
-		return last_hardware_volume;
-	} else {
-		return -1;
-	}
+	last_hardware_volume = mixer_all_get_volume();
+	g_timer_start(hardware_volume_timer);
+	return last_hardware_volume;
 }
 
 static int software_volume_get(void)
@@ -245,16 +226,10 @@ static bool software_volume_change(int change, bool rel)
 
 static bool hardware_volume_change(int change, bool rel)
 {
-	int device, count;
-
 	/* reset the cache */
 	last_hardware_volume = -1;
 
-	count = audio_output_count();
-	for (device=0; device<count ;device++) {
-		mixer_control_setvol(device, change, rel);
-	}
-	return true;
+	return mixer_all_set_volume(change, rel);
 }
 
 bool volume_level_change(int change, bool rel)