[Goodies-commits] r6310 - xfce4-radio-plugin/branches/newincludes/panel-plugin

Stefan Ott cockroach at xfce.org
Sun Dec 14 06:36:44 CET 2008


Author: cockroach
Date: 2008-12-14 05:36:43 +0000 (Sun, 14 Dec 2008)
New Revision: 6310

Added:
   xfce4-radio-plugin/branches/newincludes/panel-plugin/radio.c
   xfce4-radio-plugin/branches/newincludes/panel-plugin/radio.h
Modified:
   xfce4-radio-plugin/branches/newincludes/panel-plugin/Makefile.am
   xfce4-radio-plugin/branches/newincludes/panel-plugin/xfce4-radio.c
   xfce4-radio-plugin/branches/newincludes/panel-plugin/xfce4-radio.h
Log:
Ported code to gnomeradio api


Modified: xfce4-radio-plugin/branches/newincludes/panel-plugin/Makefile.am
===================================================================
--- xfce4-radio-plugin/branches/newincludes/panel-plugin/Makefile.am	2008-12-14 01:38:20 UTC (rev 6309)
+++ xfce4-radio-plugin/branches/newincludes/panel-plugin/Makefile.am	2008-12-14 05:36:43 UTC (rev 6310)
@@ -2,7 +2,8 @@
 plugin_PROGRAMS = xfce4-radio-plugin
 
 xfce4_radio_plugin_SOURCES =						\
-	radio.c	radio.h
+	xfce4-radio.c xfce4-radio.h					\
+	radio.c v4l1.c v4l2.c
 
 xfce4_radio_plugin_CFLAGS =						\
 	-DPACKAGE_LOCALE_DIR=\"$(localedir)\"				\

Added: xfce4-radio-plugin/branches/newincludes/panel-plugin/radio.c
===================================================================
--- xfce4-radio-plugin/branches/newincludes/panel-plugin/radio.c	                        (rev 0)
+++ xfce4-radio-plugin/branches/newincludes/panel-plugin/radio.c	2008-12-14 05:36:43 UTC (rev 6310)
@@ -0,0 +1,145 @@
+/*
+ *  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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <math.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <assert.h>
+
+#include "radio.h"
+#include "v4l1.h"
+#include "v4l2.h"
+
+static RadioDev *dev;
+
+/*
+ * These functions handle the radio device
+ */
+ 
+int radio_init(char *device, DriverType driver)
+{
+    int rv = -1;
+	if (dev) {
+		radio_stop();
+	}
+
+	switch (driver) {
+		case DRIVER_V4L2:
+			goto try_v4l2;
+		case DRIVER_ANY:
+		case DRIVER_V4L1:
+		default:
+			goto try_v4l1;
+	}
+
+try_v4l1:
+	dev = v4l1_radio_dev_new();
+	rv = dev->init (dev, device);
+	if (rv == 0) {
+        fprintf(stderr, "Initializing v4l1 failed\n");
+		dev->finalize (dev);
+		dev = NULL;
+		if (driver != DRIVER_ANY)
+			goto failure;
+	} else {
+		goto success;
+	}
+
+try_v4l2:
+	dev = v4l2_radio_dev_new();
+	rv = dev->init (dev, device);
+	if (rv == 0) {
+        fprintf(stderr, "Initializing v4l2 failed\n");
+		dev->finalize (dev);
+		dev = NULL;
+		if (driver != DRIVER_ANY)
+			goto failure;
+	} else {
+		goto success;
+	}
+
+success:
+	radio_unmute();
+failure:
+
+	return rv;
+}
+
+int radio_is_init(void)
+{
+	if (dev) return dev->is_init (dev);
+	else return 0;
+}
+
+void radio_stop(void)
+{
+	radio_mute();
+	
+	if (dev) dev->finalize (dev);
+}
+
+void radio_set_freq(float frequency)
+{
+	if (dev) dev->set_freq (dev, frequency);
+}
+
+void radio_unmute(void)
+{
+	if (dev) dev->mute (dev, 0);
+}
+
+void radio_mute(void)
+{
+	if (dev) dev->mute (dev, 1);
+}
+
+int radio_get_stereo(void)
+{
+	if (dev) return dev->get_stereo (dev);
+	else return -1;
+}
+
+int radio_get_signal(void)
+{
+	if (dev) return dev->get_signal (dev);
+	else return -1;
+}
+
+int radio_check_station(float freq)
+{
+	static int a, b;
+	static float last;
+	int signal;
+	
+	signal = radio_get_signal();
+	
+	if (last == 0.0f)
+		last = freq;
+	
+	if ((a + b + signal > 8) && (fabsf(freq - last) > 0.25f)) {
+		a = b = 0;
+		last = freq;
+		return 1;
+	}
+	a = b;
+	b = signal;
+	return 0;
+}
+

Added: xfce4-radio-plugin/branches/newincludes/panel-plugin/radio.h
===================================================================
--- xfce4-radio-plugin/branches/newincludes/panel-plugin/radio.h	                        (rev 0)
+++ xfce4-radio-plugin/branches/newincludes/panel-plugin/radio.h	2008-12-14 05:36:43 UTC (rev 6310)
@@ -0,0 +1,58 @@
+/*
+ *  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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef _RADIO_H
+#define _RADIO_H
+
+typedef struct _RadioDev RadioDev;
+struct _RadioDev
+{
+	int  (*init)       (RadioDev *dev, char *device);
+	int  (*is_init)    (RadioDev *dev);
+	void (*set_freq)   (RadioDev *dev, float freq);
+	void (*mute)       (RadioDev *dev, int   mute);
+	int  (*get_stereo) (RadioDev *dev);
+	int  (*get_signal) (RadioDev *dev);
+	void (*finalize)   (RadioDev *dev);
+};
+
+typedef enum _DriverType DriverType;
+enum _DriverType
+{
+	DRIVER_ANY,
+	DRIVER_V4L1,
+	DRIVER_V4L2
+};
+
+int radio_init(char *device, DriverType type);
+
+int radio_is_init(void);
+
+void radio_stop(void);
+
+void radio_set_freq(float freq);
+
+int radio_check_station(float freq);
+
+void radio_unmute(void);
+
+void radio_mute(void);
+
+int radio_get_stereo(void);
+
+int radio_get_signal(void);
+
+#endif

Modified: xfce4-radio-plugin/branches/newincludes/panel-plugin/xfce4-radio.c
===================================================================
--- xfce4-radio-plugin/branches/newincludes/panel-plugin/xfce4-radio.c	2008-12-14 01:38:20 UTC (rev 6309)
+++ xfce4-radio-plugin/branches/newincludes/panel-plugin/xfce4-radio.c	2008-12-14 05:36:43 UTC (rev 6310)
@@ -29,6 +29,8 @@
 #include <stdlib.h>
 
 #include "xfce4-radio.h"
+
+#include "radio.h"
 #include "v4l1.h"
 #include "v4l2.h"
 
@@ -38,7 +40,7 @@
 #include <gdk/gdkkeysyms.h>
 
 static int
-radio_get_signal (int fd)
+old_radio_get_signal (int fd)
 {
 	struct video_tuner vt;
 	int signal;
@@ -62,7 +64,7 @@
 	else
 	{
 		gtk_widget_show (data->signal_bar);
-		double signal = radio_get_signal (data->fd);
+		double signal = old_radio_get_signal (data->fd);
 
 		if (signal > data->max_signal_strength)
 			data->max_signal_strength = signal;
@@ -182,8 +184,11 @@
 			return TRUE;
 	}
 
-	if (ioctl (data->fd, VIDIOCGAUDIO, &vid_aud))
+	// TODO: This one seems to fail occasionally
+	if (ioctl (data->fd, VIDIOCGAUDIO, &vid_aud)) {
 		perror ("VIDIOCGAUDIO");
+		return TRUE; // Abort if ioctl fails
+	}
 
 	if (vid_aud.flags & VIDEO_AUDIO_MUTE)
 	{
@@ -192,7 +197,7 @@
 		// Radio is off.  Did I think it was on?
 		if (data->on)
 		{
-			// manually clean up; radio_stop would call ioctl
+			// manually clean up; xfce4_radio_stop would call ioctl
 			if (data->show_signal)
 				gtk_widget_hide (data->signal_bar);
 			data->on = FALSE;
@@ -203,7 +208,8 @@
 		// Radio is on.  Did I think it was off?
 		if (!data->on)
 		{
-			// manually clean up; radio_start would tune the radio
+			// manually clean up; xfce4_radio_start would tune the
+			// radio
 			gtk_tooltips_enable (data->tooltips);
 			data->on = TRUE;
 		}
@@ -227,9 +233,38 @@
 	return TRUE;
 }
 
+static void
+xfce4_radio_tune (radio_gui* data)
+{
+//	int freq = (data->freq * data->freqfact) / 100;
+//	ioctl (data->fd, VIDIOCSFREQ, &freq);
+
+	double freq = data->freq / 100.0;
+	radio_set_freq (freq);
+	DBG ("Tuning to %f", freq);
+
+	update_label (data);
+	update_signal_bar (data);
+
+	write_config (data, FALSE);
+}
+
 static gboolean
-radio_start (radio_gui* data)
+xfce4_radio_start (radio_gui* data)
 {
+	DBG("Starting");
+	if (!radio_init (data->device, DRIVER_V4L2))
+	{
+		GtkWindow* win = GTK_WINDOW (gtk_widget_get_toplevel(
+								data->box));
+		GtkWidget* warn = gtk_message_dialog_new (win, 0,
+			GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
+			_("Error opening radio device"));
+		gtk_dialog_run (GTK_DIALOG (warn));
+		gtk_widget_destroy (warn);
+		return FALSE;
+	}
+/*
 	struct video_tuner tuner;
 	struct video_audio vid_aud;
 
@@ -252,21 +287,23 @@
 	if (ioctl (data->fd, VIDIOCGAUDIO, &vid_aud)) perror("VIDIOCGAUDIO");
 	if (vid_aud.volume == 0) vid_aud.volume = 65535;
 	vid_aud.flags &= ~VIDEO_AUDIO_MUTE;
-	if (ioctl (data->fd, VIDIOCSAUDIO, &vid_aud)) perror("VIDIOCSAUDIO");
+	if (ioctl (data->fd, VIDIOCSAUDIO, &vid_aud)) perror("VIDIOCSAUDIO");*/
 
 	if (strcmp(data->startup_command, "") != 0)
 	{
 		xfce_exec(data->startup_command, FALSE, FALSE, NULL);
 	}
-
-	radio_tune (data);
+	xfce4_radio_tune (data);
 	gtk_tooltips_enable (data->tooltips);
 	return TRUE;
 }
 
 static void
-radio_stop (radio_gui* data)
+xfce4_radio_stop (radio_gui* data)
 {
+	radio_mute ();
+	gtk_tooltips_disable (data->tooltips);
+	/*
 	struct video_audio vid_aud;
 	gtk_tooltips_disable (data->tooltips);
 
@@ -274,7 +311,7 @@
 	vid_aud.flags |= VIDEO_AUDIO_MUTE;
 	if (ioctl (data->fd, VIDIOCSAUDIO, &vid_aud)) perror("VIDIOCSAUDIO");
 
-	close (data->fd);
+	close (data->fd);*/
 
 	if (data->show_signal) gtk_widget_hide (data->signal_bar);
 
@@ -326,7 +363,7 @@
 	if (freq_int >= FREQ_MIN && freq_int <= FREQ_MAX)
 	{
 		data->freq = freq_int;
-		radio_tune (data);
+		xfce4_radio_tune (data);
 		return TRUE;
 	}
 	return FALSE;
@@ -403,7 +440,7 @@
 	if (find_preset_by_name (presets, text, &iter))
 	{
 		gtk_tree_model_get (presets, &iter, 1, &data->freq, -1);
-		radio_tune (data);
+		xfce4_radio_tune (data);
 	}
 }
 
@@ -413,11 +450,11 @@
 	if (event->button == 1)
 	{
 		if (!data->on)
-			data->on = radio_start (data);
+			data->on = xfce4_radio_start (data);
 		else
 		{
 			data->on = FALSE;
-			radio_stop (data);
+			xfce4_radio_stop (data);
 		}
 	}
 	else if (event->button == 2 && data->on)
@@ -470,18 +507,6 @@
 }
 
 static void
-radio_tune (radio_gui* data)
-{
-	int freq = (data->freq * data->freqfact) / 100;
-	ioctl (data->fd, VIDIOCSFREQ, &freq);
-
-	update_label (data);
-	update_signal_bar (data);
-
-	write_config (data, FALSE);
-}
-
-static void
 mouse_scroll(GtkWidget* src, GdkEventScroll *event, radio_gui* data)
 {
 	GtkTreePath *path;
@@ -496,7 +521,7 @@
 		data->freq += direction * FREQ_STEP;
 		if (data->freq > FREQ_MAX) data->freq = FREQ_MIN;
 		if (data->freq < FREQ_MIN) data->freq = FREQ_MAX;
-		radio_tune (data);
+		xfce4_radio_tune (data);
 	}
 	else if (data->scroll == CHANGE_PRESET)
 	{
@@ -509,7 +534,7 @@
 
 			gtk_tree_model_get (presets, &iter,
 					1, &data->freq, -1);
-			radio_tune (data);
+			xfce4_radio_tune (data);
 			return;
 		}
 		// preset found
@@ -518,7 +543,7 @@
 			if (!gtk_tree_model_iter_next (presets, &iter))
 				return;
 			gtk_tree_model_get (presets, &iter, 1, &data->freq, -1);
-			radio_tune (data);
+			xfce4_radio_tune (data);
 		}
 		else
 		{
@@ -531,7 +556,7 @@
 			gtk_tree_path_free (path);
 			gtk_tree_model_get (presets, &iter1,
 					1, &data->freq, -1);
-			radio_tune (data);
+			xfce4_radio_tune (data);
 		}
 	}
 }
@@ -580,7 +605,7 @@
 static void
 radio_free (XfcePanelPlugin *plugin, radio_gui *data)
 {
-	if (data->on) radio_stop(data);
+	if (data->on) xfce4_radio_stop (data);
 	free_presets (data);
 	g_free (data);
 

Modified: xfce4-radio-plugin/branches/newincludes/panel-plugin/xfce4-radio.h
===================================================================
--- xfce4-radio-plugin/branches/newincludes/panel-plugin/xfce4-radio.h	2008-12-14 01:38:20 UTC (rev 6309)
+++ xfce4-radio-plugin/branches/newincludes/panel-plugin/xfce4-radio.h	2008-12-14 05:36:43 UTC (rev 6310)
@@ -18,8 +18,8 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
  */
 
-#ifndef _RADIO_H
-#define _RADIO_H
+#ifndef _XFCE4_RADIO_H
+#define _XFCE4_RADIO_H
 
 #include <gtk/gtk.h>
 #include <glib.h>
@@ -93,10 +93,6 @@
 	XfcePanelPlugin		*plugin;
 } radio_gui;
 
-static void radio_tune (radio_gui*);
-//static radio_preset* find_preset_by_freq (int, radio_gui*);
 static void read_config (XfcePanelPlugin *, radio_gui *);
-//static void write_config (XfcePanelPlugin *, radio_gui *);
 static void write_config (radio_gui *i, gboolean);
-
 #endif




More information about the Goodies-commits mailing list