[pulseaudio-discuss] [PATCH 3/5] pactl: Split out functions for code reuse

Peter Meerwald pmeerw at pmeerw.net
Fri Feb 14 10:11:49 CET 2014


to be folded after review

Signed-off-by: Peter Meerwald <pmeerw at pmeerw.net>
---
 src/utils/pactl.c | 85 ++++++++++++++++---------------------------------------
 1 file changed, 25 insertions(+), 60 deletions(-)

diff --git a/src/utils/pactl.c b/src/utils/pactl.c
index f0f4878..8d5e197 100644
--- a/src/utils/pactl.c
+++ b/src/utils/pactl.c
@@ -874,6 +874,23 @@ static void unload_module_by_name_callback(pa_context *c, const pa_module_info *
     }
 }
 
+static void fill_volume(pa_cvolume *cv, unsigned supported) {
+    if (volume.channels == 1) {
+        pa_cvolume_set(&volume, supported, volume.values[0]);
+        volume.channels = supported;
+    } else if (volume.channels != supported) {
+        pa_log(_("Failed to set volume: You tried to set volumes for %d channels, whereas channel/s supported = %d\n"),
+            volume.channels, supported);
+        quit(1);
+        return;
+    }
+
+    if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE)
+        volume_relative_adjust(cv);
+    else
+        *cv = volume;
+}
+
 static void get_sink_volume_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
     pa_cvolume cv;
     unsigned channels_supported;
@@ -889,23 +906,10 @@ static void get_sink_volume_callback(pa_context *c, const pa_sink_info *i, int i
 
     pa_assert(i);
 
+    cv = i->volume;
     channels_supported = (&i->channel_map)->channels;
-    if ((volume.channels < channels_supported && volume.channels != 1) || (volume.channels > channels_supported)) {
-        pa_log(_("Failed to set volume: You tried to set volumes for %d channels, whereas channel/s supported = %d\n"), volume.channels, channels_supported);
-        quit(1);
-        return;
-    }
+    fill_volume(&cv, channels_supported);
 
-    if (volume.channels == 1)
-        pa_cvolume_set(&volume, channels_supported, volume.values[0]);
-    volume.channels = channels_supported;
-
-    if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
-        cv = i->volume;
-        volume_relative_adjust(&cv);
-    }
-    else
-        cv = volume;
     pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &cv, simple_callback, NULL));
 }
 
@@ -924,23 +928,10 @@ static void get_source_volume_callback(pa_context *c, const pa_source_info *i, i
 
     pa_assert(i);
 
+    cv = i->volume;
     channels_supported = (&i->channel_map)->channels;
-    if ((volume.channels < channels_supported && volume.channels != 1) || (volume.channels > channels_supported)) {
-        pa_log(_("Failed to set volume: You tried to set volumes for %d channels, whereas channel/s supported = %d\n"), volume.channels, channels_supported);
-        quit(1);
-        return;
-    }
+    fill_volume(&cv, channels_supported);
 
-    if (volume.channels == 1)
-        pa_cvolume_set(&volume, channels_supported, volume.values[0]);
-    volume.channels = channels_supported;
-
-    if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
-        cv = i->volume;
-        volume_relative_adjust(&cv);
-    }
-    else
-        cv = volume;
     pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &cv, simple_callback, NULL));
 }
 
@@ -959,23 +950,10 @@ static void get_sink_input_volume_callback(pa_context *c, const pa_sink_input_in
 
     pa_assert(i);
 
+    cv = i->volume;
     channels_supported = (&i->channel_map)->channels;
-    if ((volume.channels < channels_supported && volume.channels != 1) || (volume.channels > channels_supported)) {
-        pa_log(_("Failed to set volume: You tried to set volumes for %d channels, whereas channel/s supported = %d\n"), volume.channels, channels_supported);
-        quit(1);
-        return;
-    }
+    fill_volume(&cv, channels_supported);
 
-    if (volume.channels == 1)
-        pa_cvolume_set(&volume, channels_supported, volume.values[0]);
-    volume.channels = channels_supported;
-
-    if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
-        cv = i->volume;
-        volume_relative_adjust(&cv);
-    }
-    else
-        cv = volume;
     pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &cv, simple_callback, NULL));
 }
 
@@ -994,23 +972,10 @@ static void get_source_output_volume_callback(pa_context *c, const pa_source_out
 
     pa_assert(o);
 
+    cv = o->volume;
     channels_supported = (&o->channel_map)->channels;
-    if ((volume.channels < channels_supported && volume.channels != 1) || (volume.channels > channels_supported)) {
-        pa_log(_("Failed to set volume: You tried to set volumes for %d channels, whereas channel/s supported = %d\n"), volume.channels, channels_supported);
-        quit(1);
-        return;
-    }
+    fill_volume(&cv, channels_supported);
 
-    if (volume.channels == 1)
-        pa_cvolume_set(&volume, channels_supported, volume.values[0]);
-    volume.channels = channels_supported;
-
-    if ((volume_flags & VOL_RELATIVE) == VOL_RELATIVE) {
-        cv = o->volume;
-        volume_relative_adjust(&cv);
-    }
-    else
-        cv = volume;
     pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &cv, simple_callback, NULL));
 }
 
-- 
1.8.3.2



More information about the pulseaudio-discuss mailing list