From b6fa911090ab0511eab7a7c0583757aa08bd42bd Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sat, 4 Nov 2023 23:03:19 +0530
Subject: [PATCH] refactor the audio output toggling function

---
 rx_menu.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

diff --git a/rx_menu.c b/rx_menu.c
index 4071e89..14bdc74 100644
--- a/rx_menu.c
+++ b/rx_menu.c
@@ -178,17 +178,36 @@ static void local_output_changed_cb(GtkWidget *widget, gpointer data) {
 }
 
 void toggle_audio_output_device(void) {
-    // n_output_devices holds the number of output devices (global -
-    // aargh!!)
-    int out_index = gtk_combo_box_get_active(GTK_COMBO_BOX(output));
+    // get the currently active name of the output_device.
+    // search in the output_devices list to find its index.
+    // increment the index MOD the number of output devices.
+    // get its output name
+    // close the previous device
+    // open the new device.
+    char *cur_output_device_name = active_receiver->audio_name;
+    int current_output_index = 0;
+    for (size_t i = 0; i < n_output_devices; i++) {
+	if (strcmp(cur_output_device_name, output_devices[i].name) == 0) {
+	    current_output_index = i;
+	    break;
+	}
+    }
 
     // toggle
-    out_index = (out_index + 1) % n_output_devices;
+    int out_index = (current_output_index + 1) % n_output_devices;
 
-    // set the other device as active
-    gtk_combo_box_set_active(GTK_COMBO_BOX(output), out_index);
+    // close old output device
+    if (active_receiver->local_audio) {
+	audio_close_output(active_receiver);
+    }
 
-    local_output_changed_cb(output, NULL);
+    char *new_output_device_name = output_devices[out_index].name;
+    strcpy(active_receiver->audio_name, new_output_device_name);
+
+    // set the other device as active
+    if (audio_open_output(active_receiver) < 0) {
+	g_print("unable to open the audio output device: %s", active_receiver->audio_name);
+    }
 }
 
 static void audio_channel_cb(GtkWidget *widget, gpointer data) {
-- 
2.45.2