+static void volume_relative_adjust(pa_cvolume *cv) {
+ pa_assert((volume_flags & VOL_RELATIVE) == VOL_RELATIVE);
+
+ /* Relative volume change is additive in case of UINT or PERCENT
+ * and multiplicative for LINEAR or DECIBEL */
+ if ((volume_flags & 0x0F) == VOL_UINT || (volume_flags & 0x0F) == VOL_PERCENT) {
+ pa_volume_t v = pa_cvolume_avg(cv);
+ v = v + volume < PA_VOLUME_NORM ? PA_VOLUME_MUTED : v + volume - PA_VOLUME_NORM;
+ pa_cvolume_set(cv, 1, v);
+ }
+ if ((volume_flags & 0x0F) == VOL_LINEAR || (volume_flags & 0x0F) == VOL_DECIBEL) {
+ pa_sw_cvolume_multiply_scalar(cv, cv, volume);
+ }
+}
+
+static void unload_module_by_name_callback(pa_context *c, const pa_module_info *i, int is_last, void *userdata) {
+ static bool unloaded = false;
+
+ if (is_last < 0) {
+ pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c)));
+ quit(1);
+ return;
+ }
+
+ if (is_last) {
+ if (unloaded == false)
+ pa_log(_("Failed to unload module: Module %s not loaded"), module_name);
+ complete_action();
+ return;
+ }
+
+ pa_assert(i);
+
+ if (pa_streq(module_name, i->name)) {
+ unloaded = true;
+ actions++;
+ pa_operation_unref(pa_context_unload_module(c, i->index, simple_callback, NULL));
+ }
+}
+
+static void get_sink_volume_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
+ pa_cvolume cv;
+
+ if (is_last < 0) {
+ pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
+ quit(1);
+ return;
+ }
+
+ if (is_last)
+ return;
+
+ pa_assert(i);
+
+ cv = i->volume;
+ volume_relative_adjust(&cv);
+ pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &cv, simple_callback, NULL));
+}
+
+static void get_source_volume_callback(pa_context *c, const pa_source_info *i, int is_last, void *userdata) {
+ pa_cvolume cv;
+
+ if (is_last < 0) {
+ pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
+ quit(1);
+ return;
+ }
+
+ if (is_last)
+ return;
+
+ pa_assert(i);
+
+ cv = i->volume;
+ volume_relative_adjust(&cv);
+ pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &cv, simple_callback, NULL));
+}
+
+static void get_sink_input_volume_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
+ pa_cvolume cv;
+
+ if (is_last < 0) {
+ pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
+ quit(1);
+ return;
+ }
+
+ if (is_last)
+ return;
+
+ pa_assert(i);
+
+ cv = i->volume;
+ volume_relative_adjust(&cv);
+ pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &cv, simple_callback, NULL));
+}
+
+static void get_source_output_volume_callback(pa_context *c, const pa_source_output_info *o, int is_last, void *userdata) {
+ pa_cvolume cv;
+
+ if (is_last < 0) {
+ pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
+ quit(1);
+ return;
+ }
+
+ if (is_last)
+ return;
+
+ pa_assert(o);
+
+ cv = o->volume;
+ volume_relative_adjust(&cv);
+ pa_operation_unref(pa_context_set_source_output_volume(c, source_output_idx, &cv, simple_callback, NULL));
+}
+
+static void sink_toggle_mute_callback(pa_context *c, const pa_sink_info *i, int is_last, void *userdata) {
+ if (is_last < 0) {
+ pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c)));
+ quit(1);
+ return;
+ }
+
+ if (is_last)
+ return;
+
+ pa_assert(i);
+
+ pa_operation_unref(pa_context_set_sink_mute_by_name(c, i->name, !i->mute, simple_callback, NULL));
+}
+
+static void source_toggle_mute_callback(pa_context *c, const pa_source_info *o, int is_last, void *userdata) {
+ if (is_last < 0) {
+ pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c)));
+ quit(1);
+ return;
+ }
+
+ if (is_last)
+ return;
+
+ pa_assert(o);
+
+ pa_operation_unref(pa_context_set_source_mute_by_name(c, o->name, !o->mute, simple_callback, NULL));
+}
+
+static void sink_input_toggle_mute_callback(pa_context *c, const pa_sink_input_info *i, int is_last, void *userdata) {
+ if (is_last < 0) {
+ pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c)));
+ quit(1);
+ return;
+ }
+
+ if (is_last)
+ return;
+
+ pa_assert(i);
+
+ pa_operation_unref(pa_context_set_sink_input_mute(c, i->index, !i->mute, simple_callback, NULL));
+}
+
+static void source_output_toggle_mute_callback(pa_context *c, const pa_source_output_info *o, int is_last, void *userdata) {
+ if (is_last < 0) {
+ pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c)));
+ quit(1);
+ return;
+ }
+
+ if (is_last)
+ return;
+
+ pa_assert(o);
+
+ pa_operation_unref(pa_context_set_source_output_mute(c, o->index, !o->mute, simple_callback, NULL));
+}
+
+/* PA_MAX_FORMATS is defined in internal.h so we just define a sane value here */
+#define MAX_FORMATS 256
+
+static void set_sink_formats(pa_context *c, uint32_t sink, const char *str) {
+ pa_format_info *f_arr[MAX_FORMATS];
+ char *format = NULL;
+ const char *state = NULL;
+ int i = 0;
+ pa_operation *o = NULL;
+
+ while ((format = pa_split(str, ";", &state))) {
+ pa_format_info *f = pa_format_info_from_string(pa_strip(format));
+
+ if (!f) {
+ pa_log(_("Failed to set format: invalid format string %s"), format);
+ goto error;
+ }
+
+ f_arr[i++] = f;
+ pa_xfree(format);
+ }
+
+ o = pa_ext_device_restore_save_formats(c, PA_DEVICE_TYPE_SINK, sink, i, f_arr, simple_callback, NULL);
+ if (o) {
+ pa_operation_unref(o);
+ actions++;
+ }
+
+done:
+ if (format)
+ pa_xfree(format);
+ while(i--)
+ pa_format_info_free(f_arr[i]);
+
+ return;
+
+error:
+ while(i--)
+ pa_format_info_free(f_arr[i]);
+ quit(1);
+ goto done;
+}
+