GConf backend for settings
authorjait <jari.tenhunen@iki.fi>
Sun, 24 Aug 2008 19:05:23 +0000 (19:05 +0000)
committerjait <jari.tenhunen@iki.fi>
Sun, 24 Aug 2008 19:05:23 +0000 (19:05 +0000)
git-svn-id: file:///svnroot/tunertool/trunk@18 4bb5ff34-d565-4b58-9699-12000fa1827a

ChangeLog
configure.ac
debian/control
src/Makefile.am
src/gstpitch.c
src/settings.c [new file with mode: 0644]
src/settings.h [new file with mode: 0644]
src/tuner.c

index 8a52f7b..3bc5fd5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2008-08-24  Jari Tenhunen  <jari.tenhunen@iki.fi>
+       * src/tuner.c src/settings.h src/settings.c
+       src/Makefile.am configure.ac debian/control:
+       GConf backend for settings
+
 2008-08-20  Jari Tenhunen  <jari.tenhunen@iki.fi>
        * src/tuner64.png src/tuner26.png:
        Slightly reworked, transparent icons
index 51e0aa5..eac5712 100644 (file)
@@ -189,6 +189,21 @@ AC_SUBST(GTK_CFLAGS)
 AC_SUBST(HAVE_GTK)
 AM_CONDITIONAL(HAVE_GTK, test "x$HAVE_GTK" = "xyes")
 
+dnl If we need them, we can also use the gstreamer-controller libraries
+PKG_CHECK_MODULES(GCONF,
+                  gconf-2.0,
+                  HAVE_GCONF=yes, HAVE_GCONF=no)
+
+dnl Give a warning if we don't have gstreamer-controller
+dnl you can turn this into an error if you need them
+if test "x$HAVE_GCONF" = "xno"; then
+  AC_MSG_ERROR(no GConf libraries found)
+fi
+
+dnl make _CFLAGS and _LIBS available
+AC_SUBST(GCONF_CFLAGS)
+AC_SUBST(GCONFL_LIBS)
+
 dnl set the plugindir where plugins should be installed
 if test "x${prefix}" = "x$HOME"; then
   plugindir="$HOME/.gstreamer-$GST_MAJORMINOR/plugins"
index 59d408a..5ba94ce 100644 (file)
@@ -2,7 +2,7 @@ Source: tuner
 Section: user/tools
 Priority: optional
 Maintainer: Josep Torra <jtorra@uoc.edu>
-Build-Depends: debhelper (>= 4.0.0), libhildon1-dev | hildon-libs-dev, libgtk2.0-dev, libgstreamer0.10-dev, libgstreamer-plugins-base0.10-dev, libosso-dev (>= 1)
+Build-Depends: debhelper (>= 4.0.0), libhildon1-dev | hildon-libs-dev, libgtk2.0-dev, libgstreamer0.10-dev, libgstreamer-plugins-base0.10-dev, libosso-dev (>= 1), libgconf2-dev
 Standards-Version: 3.6.0
 
 Package: tuner
index 5ee52df..ba0cb8e 100644 (file)
@@ -23,7 +23,7 @@ libgsttonesrc_la_LIBADD = $(GSTPB_BASE_LIBS) $(GST_BASE_LIBS) \
 libgsttonesrc_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
 
 # headers we need but don't want installed
-noinst_HEADERS = gstpitch.h gsttonesrc.h kiss_fft.h _kiss_fft_guts.h
+noinst_HEADERS = gstpitch.h gsttonesrc.h kiss_fft.h _kiss_fft_guts.h settings.h
 EXTRA_DIST = tuner26.png  tuner40.png  tuner64.png
 
 if HAVE_GTK
@@ -38,9 +38,9 @@ demo_tuner_SOURCES = demo-tuner.c
 demo_tuner_CFLAGS  = $(GST_CFLAGS) $(GTK_CFLAGS) 
 demo_tuner_LDFLAGS = $(GST_LIBS) $(GTK_LIBS)
 
-tuner_SOURCES = tuner.c
+tuner_SOURCES = tuner.c settings.c
 tuner_DEPENDENCIES = $(top_builddir)/src/libgstpitch.la $(top_builddir)/src/libgsttonesrc.la
-tuner_CFLAGS  = $(GST_CFLAGS) $(GTK_CFLAGS) $(MAEMO_CFLAGS) -D_GNU_SOURCE
-tuner_LDADD = $(GST_LIBS) $(GTK_LIBS) $(MAEMO_LIBS) $(top_builddir)/src/libgstpitch.la $(top_builddir)/src/libgsttonesrc.la
+tuner_CFLAGS  = $(GST_CFLAGS) $(GTK_CFLAGS) $(MAEMO_CFLAGS) $(GCONF_CFLAGS) -D_GNU_SOURCE
+tuner_LDADD = $(GST_LIBS) $(GTK_LIBS) $(MAEMO_LIBS) $(GCONF_LIBS) $(top_builddir)/src/libgstpitch.la $(top_builddir)/src/libgsttonesrc.la
 
 
index 956414e..0250a5a 100644 (file)
@@ -379,7 +379,9 @@ gst_pitch_message_new (GstPitch * filter)
       break;
   }
 
+  /*
   g_debug("freq %d[%d]\n", frequency, frequency_module);
+  */
   GST_DEBUG_OBJECT (filter, "preparing message, frequency = %d ", frequency);
 
   s = gst_structure_new ("pitch", "frequency", G_TYPE_INT, frequency, NULL);
diff --git a/src/settings.c b/src/settings.c
new file mode 100644 (file)
index 0000000..225d0b7
--- /dev/null
@@ -0,0 +1,125 @@
+/* vim: set sts=2 sw=2 et: */
+/* 
+ * Copyright (C) 2008 Jari Tenhunen <jari.tenhunen@iki.fi>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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 Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "settings.h"
+
+static GConfClient * client = NULL;
+
+static GConfClient *
+get_client ()
+{
+  if (!client)
+    client = gconf_client_get_default ();
+
+  return client;
+
+}
+
+gboolean
+settings_get_display_keepalive (gboolean default_value)
+{
+  GConfValue * value = NULL;
+  GError * err = NULL;
+  gboolean val = default_value;
+
+  value = gconf_client_get (get_client (), GCONF_KEY_DISPLAY_KEEPALIVE, &err);
+  if (err) {
+    g_error_free (err);
+  }
+
+  if (value) {
+    if (value->type == GCONF_VALUE_BOOL)
+      val = gconf_value_get_bool (value);
+
+    gconf_value_free (value);
+  }
+
+  return val;
+}
+
+gboolean
+settings_set_disp_keepalive (gboolean val)
+{
+  return gconf_client_set_bool (get_client (), GCONF_KEY_DISPLAY_KEEPALIVE, val, NULL);
+}
+
+gint
+settings_get_algorithm (gint default_value)
+{
+  GConfValue * value = NULL;
+  GError * err = NULL;
+  gint val = default_value;
+
+  value = gconf_client_get (get_client (), GCONF_KEY_ALGORITHM, &err);
+  if (err) {
+    g_error_free (err);
+  }
+
+  if (value) {
+    if (value->type == GCONF_VALUE_INT)
+      val = gconf_value_get_int (value);
+
+    gconf_value_free (value);
+  }
+
+  return val;
+}
+
+gboolean
+settings_set_algorithm (gint val)
+{
+  return gconf_client_set_int (get_client (), GCONF_KEY_ALGORITHM, val, NULL);
+}
+
+gint
+settings_get_calibration (gint default_value)
+{
+  GError * err = NULL;
+  gint val;
+
+  val = gconf_client_get_int (get_client (), GCONF_KEY_CALIBRATION, &err);
+  if (err) {
+    val = default_value;
+    g_error_free (err);
+  }
+  if (val == 0)
+    val = default_value;
+
+  return val;
+}
+
+gboolean
+settings_set_calibration (gint value)
+{
+  return gconf_client_set_int (get_client (), GCONF_KEY_CALIBRATION, value, NULL);
+}
+
+gboolean
+settings_init (GConfClientNotifyFunc func, gpointer user_data)
+{
+  gconf_client_add_dir (get_client (), GCONF_ROOT, GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
+  gconf_client_notify_add (get_client (), GCONF_ROOT, func, user_data, NULL, NULL);
+
+  return TRUE;
+}
diff --git a/src/settings.h b/src/settings.h
new file mode 100644 (file)
index 0000000..11ba480
--- /dev/null
@@ -0,0 +1,38 @@
+/* vim: set sts=2 sw=2 et: */
+/*
+ * Copyright (C) 2008 Jari Tenhunen <jari.tenhunen@iki.fi>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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 Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __SETTINGS_H__
+#define __SETTINGS_H__
+
+#include <gconf/gconf-client.h>
+
+#define GCONF_ROOT "/apps/tuner"
+#define GCONF_KEY_ALGORITHM GCONF_ROOT "/algorithm"
+#define GCONF_KEY_CALIBRATION GCONF_ROOT "/calibration"
+#define GCONF_KEY_DISPLAY_KEEPALIVE GCONF_ROOT "/display_keepalive"
+
+gint settings_get_algorithm (gint default_value);
+gint settings_get_calibration (gint default_value);
+gboolean settings_set_calibration (gint value);
+gboolean settings_get_display_keepalive (gboolean default_value);
+gboolean settings_init (GConfClientNotifyFunc func, gpointer user_data);
+
+#endif /* __SETTINGS_H__ */
+
index f8af9a6..987d2a6 100644 (file)
 #include <math.h>
 #include <gst/gst.h>
 #include <gtk/gtk.h>
+#include <gconf/gconf-client.h>
+
+#include "gstpitch.h"
+#include "settings.h"
 
 #define between(x,a,b) (((x)>=(a)) && ((x)<=(b)))
 
@@ -81,7 +85,10 @@ struct app_data
   GstElement *bin1;
   GstElement *bin2;
   GstElement *tonesrc;
+  GstElement *pitch;
   guint stop_timer_id;
+
+  gboolean display_keepalive;
 #ifdef MAEMO
   osso_context_t *osso_context;
   gpointer app;
@@ -245,6 +252,7 @@ calibration_changed (GObject * object, GParamSpec * pspec, gpointer user_data)
 
   if (value >= CALIB_MIN && value <= CALIB_MAX) {
     recalculate_scale (value);
+    settings_set_calibration (value);
   }
 }
 
@@ -561,6 +569,15 @@ display_keepalive (gpointer user_data)
   return FALSE;
 }
 
+static void
+display_keepalive_stop (AppData * appdata)
+{
+  if (appdata->display_timer_id) {
+    g_source_remove (appdata->display_timer_id);
+    appdata->display_timer_id = 0;
+  }
+}
+
 static gboolean
 topmost_notify (GObject * object, GParamSpec * pspec, gpointer user_data)
 {
@@ -576,7 +593,7 @@ topmost_notify (GObject * object, GParamSpec * pspec, gpointer user_data)
     set_pipeline_states (appdata, GST_STATE_PLAYING);
 
     /* keep display on */
-    if (appdata->display_timer_id == 0)
+    if (appdata->display_keepalive && appdata->display_timer_id == 0)
       display_keepalive (user_data);
   }
   else {
@@ -585,16 +602,45 @@ topmost_notify (GObject * object, GParamSpec * pspec, gpointer user_data)
     /* stop pipelines fully if the app stays in the background for 30 seconds */
     appdata->stop_timer_id = g_timeout_add (30000, (GSourceFunc) stop_pipelines, user_data);
     /* let display dim and switch off */
-    if (appdata->display_timer_id) {
-      g_source_remove (appdata->display_timer_id);
-      appdata->display_timer_id = 0;
-    }
+    display_keepalive_stop (appdata);
   }
 
   return FALSE;
 }
 #endif
 
+static void
+settings_notify (GConfClient * client, guint cnxn_id, GConfEntry * entry, gpointer user_data)
+{
+  AppData * appdata = (AppData *) user_data;
+
+  g_debug ("%s changed", gconf_entry_get_key (entry));
+
+  if (strcmp (gconf_entry_get_key (entry), GCONF_KEY_ALGORITHM) == 0) {
+    if (gconf_entry_get_value (entry) != NULL && gconf_entry_get_value (entry)->type == GCONF_VALUE_INT) {
+      g_object_set (G_OBJECT (appdata->pitch), 
+          "algorithm", gconf_value_get_int (gconf_entry_get_value (entry)),
+          NULL);
+    }
+  }
+  else if (strcmp (gconf_entry_get_key (entry), GCONF_KEY_CALIBRATION) == 0) {
+    /* TODO */
+  }
+  else if (strcmp (gconf_entry_get_key (entry), GCONF_KEY_DISPLAY_KEEPALIVE) == 0) {
+    if (gconf_entry_get_value (entry) != NULL && gconf_entry_get_value (entry)->type == GCONF_VALUE_BOOL) {
+      appdata->display_keepalive = gconf_value_get_bool (gconf_entry_get_value (entry));
+
+      if (appdata->display_keepalive && appdata->display_timer_id == 0)
+        display_keepalive (user_data);
+      else
+        display_keepalive_stop (appdata);
+    }
+  }
+  else {
+    g_warning ("unknown GConf key `%s'", gconf_entry_get_key (entry));
+  }
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -609,8 +655,9 @@ main (int argc, char *argv[])
 #endif
   osso_hw_state_t hw_state_mask = { TRUE, FALSE, FALSE, TRUE, 0 };
 #endif
+  gint calib;
 
-  GstElement *src1, *src2, *pitch, *sink1;
+  GstElement *src1, *src2, *sink1;
   GstElement *sink2;
   GstBus *bus;
 
@@ -636,7 +683,6 @@ main (int argc, char *argv[])
   plugin_pitch_init (NULL);
   plugin_tonesrc_init (NULL);
 
-  recalculate_scale (CALIB_DEFAULT);
 
   /* Init the gtk - must be called before any hildon stuff */
   gtk_init (&argc, &argv);
@@ -667,6 +713,11 @@ main (int argc, char *argv[])
   if (osso_hw_set_event_cb (appdata->osso_context, &hw_state_mask, osso_hw_state_cb, appdata) != OSSO_OK)
     g_warning ("setting osso_hw_state_cb failed!");
 
+  settings_init (&settings_notify, appdata);
+
+  calib = settings_get_calibration (CALIB_DEFAULT);
+  recalculate_scale (calib);
+
   mainBox = gtk_vbox_new (FALSE, 0);
   gtk_container_set_border_width (GTK_CONTAINER (mainBox), 0);
 #if defined(MAEMO1)
@@ -694,15 +745,18 @@ main (int argc, char *argv[])
   appdata->bin1 = gst_pipeline_new ("bin1");
 
   src1 = gst_element_factory_make (DEFAULT_AUDIOSRC, "src1");
-  pitch = gst_element_factory_make ("pitch", "pitch");
-  g_object_set (G_OBJECT (pitch), "message", TRUE, "minfreq", 10,
-      "maxfreq", 4000, NULL);
+  appdata->pitch = gst_element_factory_make ("pitch", "pitch");
+
+  g_object_set (G_OBJECT (appdata->pitch), "message", TRUE, "minfreq", 10,
+      "maxfreq", 4000, 
+      "algorithm", settings_get_algorithm (GST_PITCH_ALGORITHM_FFT),
+      NULL);
 
   sink1 = gst_element_factory_make ("fakesink", "sink1");
   g_object_set (G_OBJECT (sink1), "silent", 1, NULL);
 
-  gst_bin_add_many (GST_BIN (appdata->bin1), src1, pitch, sink1, NULL);
-  if (!gst_element_link_many (src1, pitch, sink1, NULL)) {
+  gst_bin_add_many (GST_BIN (appdata->bin1), src1, appdata->pitch, sink1, NULL);
+  if (!gst_element_link_many (src1, appdata->pitch, sink1, NULL)) {
     fprintf (stderr, "cant link elements\n");
     exit (1);
   }
@@ -755,7 +809,7 @@ main (int argc, char *argv[])
 #ifdef HILDON
   calibrate = hildon_number_editor_new (CALIB_MIN, CALIB_MAX);
   hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (calibrate),
-      CALIB_DEFAULT);
+      calib);
   /* we don't want that ugly cursor there */
   gtk_container_forall (GTK_CONTAINER (calibrate),
       (GtkCallback) fix_hildon_number_editor, NULL);
@@ -763,7 +817,7 @@ main (int argc, char *argv[])
       G_CALLBACK (calibration_changed), NULL);
 #else
   calibrate = gtk_spin_button_new_with_range (CALIB_MIN, CALIB_MAX, 1);
-  gtk_spin_button_set_value (GTK_SPIN_BUTTON (calibrate), CALIB_DEFAULT);
+  gtk_spin_button_set_value (GTK_SPIN_BUTTON (calibrate), calib);
   g_signal_connect (G_OBJECT (calibrate), "value_changed",
       G_CALLBACK (calibration_changed), NULL);
 #endif
@@ -816,8 +870,15 @@ main (int argc, char *argv[])
   gtk_widget_show_all (GTK_WIDGET (mainWin));
 #endif
 
+#if HILDON == 1
+  appdata->display_keepalive = settings_get_display_keepalive (TRUE);
+
+  if (appdata->display_keepalive)
+    display_keepalive (appdata);
+#endif
+
   set_pipeline_states (appdata, GST_STATE_PLAYING);
-  display_keepalive (appdata);
+
   gtk_main ();
   set_pipeline_states (appdata, GST_STATE_NULL);