]> git.street.me.uk Git - andy/viking.git/blobdiff - src/background.c
Only call gps_close() after a successful gps_open().
[andy/viking.git] / src / background.c
index f3ed15c8c085c8bf532812a7f4b06b638a0614ce..c49e517ff3463985f29785fa71133e3a9bd8fa79 100644 (file)
@@ -2,6 +2,7 @@
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (c) 2015, Rob Norris <rw_norris@hotmail.com>
  *
  * 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
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 
-#include "vikstatus.h"
 #include "background.h"
-
-static GThreadPool *thread_pool = NULL;
+#include "settings.h"
+#include "util.h"
+#include "math.h"
+#include "uibuilder.h"
+#include "globals.h"
+#include "preferences.h"
+
+static GThreadPool *thread_pool_remote = NULL;
+static GThreadPool *thread_pool_local = NULL;
+#ifdef HAVE_LIBMAPNIK
+static GThreadPool *thread_pool_local_mapnik = NULL;
+#endif
 static gboolean stop_all_threads = FALSE;
 
 static GtkWidget *bgwindow = NULL;
 static GtkWidget *bgtreeview = NULL;
 static GtkListStore *bgstore = NULL;
 
-static GSList *statusbars_to_update = NULL;
+// Still only actually updating the statusbar though
+static GSList *windows_to_update = NULL;
 
 static gint bgitemcount = 0;
 
+#define VIK_BG_NUM_ARGS 7
+
 enum
 {
   TITLE_COLUMN = 0,
@@ -44,27 +57,33 @@ enum
   N_COLUMNS,
 };
 
-void a_background_update_status ( VikStatusbar *vs, gchar *str )
+void a_background_update_status ( VikWindow *vw, gpointer data )
 {
-  gdk_threads_enter ();
-  vik_statusbar_set_message ( vs, 1, str );
-  gdk_threads_leave ();
+  static gchar buf[20];
+  g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount);
+  vik_window_statusbar_update ( vw, buf, VIK_STATUSBAR_ITEMS );
 }
 
 static void background_thread_update ()
 {
-  static gchar buf[20];
-  g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount);
-  g_slist_foreach ( statusbars_to_update, (GFunc) a_background_update_status, buf );
+  g_slist_foreach ( windows_to_update, (GFunc) a_background_update_status, NULL );
 }
 
+/**
+ * a_background_thread_progress:
+ * @callbackdata: Thread data
+ * @fraction:     The value should be between 0.0 and 1.0 indicating percentage of the task complete
+ */
 int a_background_thread_progress ( gpointer callbackdata, gdouble fraction )
 {
   gpointer *args = (gpointer *) callbackdata;
   int res = a_background_testcancel ( callbackdata );
   if (args[5] != NULL) {
+    gdouble myfraction = fabs(fraction);
+    if ( myfraction > 1.0 )
+      myfraction = 1.0;
     gdk_threads_enter();
-    gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, fraction*100, -1 );
+    gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, myfraction*100, -1 );
     gdk_threads_leave();
   }
 
@@ -74,11 +93,12 @@ int a_background_thread_progress ( gpointer callbackdata, gdouble fraction )
   return res;
 }
 
-static void thread_die ( gpointer args[6] )
+static void thread_die ( gpointer args[VIK_BG_NUM_ARGS] )
 {
   vik_thr_free_func userdata_free_func = args[3];
 
-  userdata_free_func ( args[2] );
+  if ( userdata_free_func != NULL )
+    userdata_free_func ( args[2] );
 
   if ( GPOINTER_TO_INT(args[6]) )
   {
@@ -105,7 +125,7 @@ int a_background_testcancel ( gpointer callbackdata )
   return 0;
 }
 
-static void thread_helper ( gpointer args[6], gpointer user_data )
+static void thread_helper ( gpointer args[VIK_BG_NUM_ARGS], gpointer user_data )
 {
   /* unpack args */
   vik_thr_func func = args[1];
@@ -123,10 +143,23 @@ static void thread_helper ( gpointer args[6], gpointer user_data )
   thread_die ( args );
 }
 
-void a_background_thread ( GtkWindow *parent, const gchar *message, vik_thr_func func, gpointer userdata, vik_thr_free_func userdata_free_func, vik_thr_free_func userdata_cancel_cleanup_func, gint number_items )
+/**
+ * a_background_thread:
+ * @bp:      Which pool this thread should run in
+ * @parent:
+ * @message:
+ * @func: worker function
+ * @userdata:
+ * @userdata_free_func: free function for userdata
+ * @userdata_cancel_cleanup_func:
+ * @number_items:
+ *
+ * Function to enlist new background function.
+ */
+void a_background_thread ( Background_Pool_Type bp, GtkWindow *parent, const gchar *message, vik_thr_func func, gpointer userdata, vik_thr_free_func userdata_free_func, vik_thr_free_func userdata_cancel_cleanup_func, gint number_items )
 {
   GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) );
-  gpointer *args = g_malloc ( sizeof(gpointer) * 7 );
+  gpointer *args = g_malloc ( sizeof(gpointer) * VIK_BG_NUM_ARGS );
 
   g_debug(__FUNCTION__);
 
@@ -148,9 +181,21 @@ void a_background_thread ( GtkWindow *parent, const gchar *message, vik_thr_func
                       -1 );
 
   /* run the thread in the background */
-  g_thread_pool_push( thread_pool, args, NULL );
+  if ( bp == BACKGROUND_POOL_REMOTE )
+    g_thread_pool_push( thread_pool_remote, args, NULL );
+#ifdef HAVE_LIBMAPNIK
+  else if ( bp == BACKGROUND_POOL_LOCAL_MAPNIK )
+    g_thread_pool_push( thread_pool_local_mapnik, args, NULL );
+#endif
+  else
+    g_thread_pool_push( thread_pool_local, args, NULL );
 }
 
+/**
+ * a_background_show_window:
+ *
+ * Display the background window.
+ */
 void a_background_show_window ()
 {
   gtk_widget_show_all ( bgwindow );
@@ -160,7 +205,7 @@ static void cancel_job_with_iter ( GtkTreeIter *piter )
 {
     gpointer *args;
 
-  g_debug(__FUNCTION__);
+    g_debug(__FUNCTION__);
 
     gtk_tree_model_get( GTK_TREE_MODEL(bgstore), piter, DATA_COLUMN, &args, -1 );
 
@@ -200,12 +245,62 @@ static void bgwindow_response (GtkDialog *dialog, gint arg1 )
     gtk_widget_hide ( bgwindow );
 }
 
-void a_background_init()
+#define VIK_SETTINGS_BACKGROUND_MAX_THREADS "background_max_threads"
+#define VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL "background_max_threads_local"
+
+#ifdef HAVE_LIBMAPNIK
+VikLayerParamScale params_threads[] = { {1, 64, 1, 0} }; // 64 threads should be enough for anyone...
+// implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues
+static VikLayerParam prefs_mapnik[] = {
+  { VIK_LAYER_NUM_TYPES, "mapnik.background_max_threads_local_mapnik", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Threads:"), VIK_LAYER_WIDGET_SPINBUTTON, params_threads, NULL,
+    N_("Number of threads to use for Mapnik tasks. You need to restart Viking for a change to this value to be used"), NULL, NULL, NULL },
+};
+#endif
+
+/**
+ * a_background_init:
+ *
+ * Just setup any preferences.
+ */
+void a_background_init ()
+{
+#ifdef HAVE_LIBMAPNIK
+  VikLayerParamData tmp;
+  // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues
+  tmp.u = 1; // Default to 1 thread due to potential crashing issues
+  a_preferences_register(&prefs_mapnik[0], tmp, "mapnik");
+#endif
+}
+
+/**
+ * a_background_post_init:
+ *
+ * Initialize background feature.
+ */
+void a_background_post_init()
 {
-  /* initialize thread pool */
-  /* TODO parametrize this via preference and/or command line arg */
+  // initialize thread pools
   gint max_threads = 10;  /* limit maximum number of threads running at one time */
-  thread_pool = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
+  gint maxt;
+  if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS, &maxt ) )
+    max_threads = maxt;
+
+  thread_pool_remote = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
+
+  if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL, &maxt ) )
+    max_threads = maxt;
+  else {
+    guint cpus = util_get_number_of_cpus ();
+    max_threads = cpus > 1 ? cpus-1 : 1; // Don't use all available CPUs!
+  }
+
+  thread_pool_local = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
+
+#ifdef HAVE_LIBMAPNIK
+  // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues
+  guint mapnik_threads = a_preferences_get("mapnik.background_max_threads_local_mapnik")->u;
+  thread_pool_local_mapnik = g_thread_pool_new ( (GFunc) thread_helper, NULL, mapnik_threads, FALSE, NULL );
+#endif
 
   GtkCellRenderer *renderer;
   GtkTreeViewColumn *column;
@@ -235,9 +330,16 @@ void a_background_init()
   gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
 
   bgwindow = gtk_dialog_new_with_buttons ( "", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_DELETE, 1, GTK_STOCK_CLEAR, 2, NULL );
-  gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(bgwindow)->vbox), scrolled_window, TRUE, TRUE, 0 );
+  gtk_dialog_set_default_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
+  GtkWidget *response_w = NULL;
+#if GTK_CHECK_VERSION (2, 20, 0)
+  response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
+#endif
+  gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bgwindow))), scrolled_window, TRUE, TRUE, 0 );
   gtk_window_set_default_size ( GTK_WINDOW(bgwindow), 400, 400 );
   gtk_window_set_title ( GTK_WINDOW(bgwindow), _("Viking Background Jobs") );
+  if ( response_w )
+    gtk_widget_grab_focus ( response_w );
   /* don't destroy win */
   g_signal_connect ( G_OBJECT(bgwindow), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL );
 
@@ -245,20 +347,34 @@ void a_background_init()
 
 }
 
+/**
+ * a_background_uninit:
+ *
+ * Uninitialize background feature.
+ */
 void a_background_uninit()
 {
-  /* wait until all running threads stop */
   stop_all_threads = TRUE;
-  g_thread_pool_free ( thread_pool, TRUE, TRUE );
+  // wait until these threads stop
+  g_thread_pool_free ( thread_pool_remote, TRUE, TRUE );
+  // Don't wait for these
+  g_thread_pool_free ( thread_pool_local, TRUE, FALSE );
+#ifdef HAVE_LIBMAPNIK
+  g_thread_pool_free ( thread_pool_local_mapnik, TRUE, FALSE );
+#endif
+
+  gtk_list_store_clear ( bgstore );
+  g_object_unref ( bgstore );
+
+  gtk_widget_destroy ( bgwindow );
 }
 
-void a_background_add_status(VikStatusbar *vs)
+void a_background_add_window (VikWindow *vw)
 {
-  statusbars_to_update = g_slist_prepend(statusbars_to_update,vs);
+  windows_to_update = g_slist_prepend(windows_to_update,vw);
 }
 
-void a_background_remove_status(VikStatusbar *vs)
+void a_background_remove_window (VikWindow *vw)
 {
-  statusbars_to_update = g_slist_remove(statusbars_to_update,vs);
+  windows_to_update = g_slist_remove(windows_to_update,vw);
 }
-