[Xfce4-commits] <xfce4-radio-plugin:master> The radio tuner can now be selected from a combobox

Stefan Ott noreply at xfce.org
Sun May 22 04:50:04 CEST 2011


Updating branch refs/heads/master
         to 39136804e6692961d4b8a9d0a1d96a5882fe9589 (commit)
       from 31f20d2e107844dc3aeacb8ffa76bfe7b124228e (commit)

commit 39136804e6692961d4b8a9d0a1d96a5882fe9589
Author: Stefan Ott <stefan at ott.net>
Date:   Sat May 21 08:06:26 2011 +0200

    The radio tuner can now be selected from a combobox

 panel-plugin/xfce4-radio.c |  111 ++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 98 insertions(+), 13 deletions(-)

diff --git a/panel-plugin/xfce4-radio.c b/panel-plugin/xfce4-radio.c
index ff85e73..84f248e 100644
--- a/panel-plugin/xfce4-radio.c
+++ b/panel-plugin/xfce4-radio.c
@@ -50,6 +50,9 @@
 #include <libxfcegui4/libxfcegui4.h>
 #include <gdk/gdkkeysyms.h>
 
+#include <linux/videodev2.h>
+#include <dirent.h>
+
 #define SIGNAL_WIDTH 15
 #define SIGNAL_HEIGHT 12
 
@@ -709,6 +712,48 @@ radio_free(XfcePanelPlugin *plugin, radio_gui *data)
 		g_source_remove(data->radio_timeout_id);
 }
 
+static GList *
+radio_find_tuners(GList *tuners)
+{
+	DIR *dir;
+	struct dirent *item;
+
+	dir = opendir("/dev");
+	if (dir == NULL)
+	{
+		perror("Could not open the directory");
+		return tuners;
+	}
+	while ((item = readdir(dir)))
+	{
+		if (!strncmp(item->d_name, "radio", 5))
+		{
+			gchar *dev = g_strdup_printf("/dev/%s", item->d_name);
+			tuners = g_list_append(tuners, dev);
+		}
+	}
+	closedir(dir);
+	return tuners;
+}
+
+static gchar *
+radio_tuner_name(const gchar *devname)
+{
+	int fd = open(devname, O_RDWR);
+
+	struct v4l2_capability vcap;
+
+	if (ioctl(fd, VIDIOC_QUERYCAP, &vcap) < 0)
+	{
+		perror("VIDIOC_QUERYCAP");
+		return g_strdup_printf("Invalid tuner (%s)", devname);
+	}
+	else
+	{
+		return g_strdup_printf("%s (%s)", vcap.card, devname);
+	}
+}
+
 static radio_gui *
 plugin_control_new(XfcePanelPlugin *plugin)
 {
@@ -764,10 +809,21 @@ radio_shutdown_command_changed(GtkEditable* editable, void *pointer)
 }
 
 static void
-radio_device_changed(GtkEditable* editable, void *pointer)
+radio_device_changed(GtkEditable* box, void *pointer)
 {
 	radio_gui* data = (radio_gui*) pointer;
-	const char* device = gtk_entry_get_text(GTK_ENTRY(editable));
+	GtkComboBox *combo = GTK_COMBO_BOX(box);
+	GtkTreeModel *model = gtk_combo_box_get_model(combo);
+
+	GtkTreeIter iter;
+	gtk_combo_box_get_active_iter(combo, &iter);
+
+	GValue value={0,};
+
+	gtk_tree_model_get_value(model, &iter, 0, &value);
+	const gchar *device = g_value_get_string(&value);
+	printf("dev=%s\n", device);
+
 	strncpy(data->device, device, MAX_DEVICE_NAME_LENGTH);
 }
 
@@ -1033,7 +1089,7 @@ radio_plugin_create_options(XfcePanelPlugin *plugin, radio_gui *data)
 	GtkWidget *sync_state;			// sync state with the card
 	GtkWidget *startup_command;		// post-down command
 	GtkWidget *shutdown_command;		// post-down command
-	GtkWidget *device_entry;		// v4l device
+	GtkWidget *radio_dev;			// v4l device
 	GtkWidget *scrolling;			// mouse-scrolling action
 	GtkWidget *preset_box;
 	GtkWidget *button_box;
@@ -1276,21 +1332,50 @@ radio_plugin_create_options(XfcePanelPlugin *plugin, radio_gui *data)
 
 	// - Radio device
 	hbox = gtk_hbox_new(FALSE, 0);
-	label = gtk_label_new(_("Radio device:"));
-	device_entry = gtk_entry_new_with_max_length(MAX_DEVICE_NAME_LENGTH);
-	gtk_entry_set_text(GTK_ENTRY(device_entry), data->device);
-
-	gtk_widget_show(label);
-	gtk_widget_show(device_entry);
+	gtk_box_pack_start(GTK_BOX(current_page), hbox, FALSE, FALSE, 9);
 	gtk_widget_show(hbox);
 
-	gtk_box_pack_start(GTK_BOX(current_page), hbox, FALSE, FALSE, 9);
+	label = gtk_label_new(_("Radio device:"));
 	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
-	gtk_box_pack_start(GTK_BOX(hbox), device_entry, FALSE, FALSE, 5);
+	gtk_widget_show(label);
+
+	model = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
+
+	radio_dev = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
+	gtk_box_pack_start(GTK_BOX(hbox), radio_dev, FALSE, FALSE, 5);
+	gtk_widget_show(radio_dev);
+
+	renderer = gtk_cell_renderer_text_new();
+	gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(radio_dev), renderer, TRUE);
+	gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(radio_dev), renderer,
+							"text", 1, NULL);
+
+	GList *tuners = NULL, *tuner;
+	tuners = radio_find_tuners(tuners);
+	gchar *devname;
+
+	for (tuner = tuners; tuner; tuner = tuner->next)
+	{
+		devname = tuner->data;
+		gtk_list_store_append(model, &iter);
+		gtk_list_store_set(model, &iter,
+				0, devname,
+				1, radio_tuner_name(devname),
+		-1);
+
+		if (!g_strcmp0(devname, data->device))
+		{
+			gtk_combo_box_set_active_iter(GTK_COMBO_BOX(radio_dev),
+					&iter);
+		}
+
+		g_free(tuner->data);
+	}
+	g_list_free(tuners);
 
 	// - Synchronize state
 	sync_state = gtk_check_button_new_with_label
-		(_("Synchronize state with the card"));
+					(_("Synchronize state with the card"));
 
 	gtk_widget_show(sync_state);
 
@@ -1408,7 +1493,7 @@ radio_plugin_create_options(XfcePanelPlugin *plugin, radio_gui *data)
 			G_CALLBACK(radio_startup_command_changed), data);
 	g_signal_connect((gpointer) shutdown_command, "changed",
 			G_CALLBACK(radio_shutdown_command_changed), data);
-	g_signal_connect((gpointer) device_entry, "changed",
+	g_signal_connect((gpointer) radio_dev, "changed",
 			G_CALLBACK(radio_device_changed), data);
 	g_signal_connect(G_OBJECT(show_signal), "toggled",
 			G_CALLBACK(radio_show_signal_changed), data);



More information about the Xfce4-commits mailing list