]> git.street.me.uk Git - andy/viking.git/blame - src/background.c
Add OSM GPS Tiles in default data config
[andy/viking.git] / src / background.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
c75da936 5 * Copyright (c) 2015, Rob Norris <rw_norris@hotmail.com>
50a14534
EB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <gtk/gtk.h>
4c77d5e0
GB
24#include <glib/gi18n.h>
25
50a14534 26#include "background.h"
062790f9 27#include "settings.h"
c75da936 28#include "util.h"
e38c5ff1 29#include "math.h"
a8374fcb
RN
30#include "uibuilder.h"
31#include "globals.h"
32#include "preferences.h"
50a14534 33
c75da936
RN
34static GThreadPool *thread_pool_remote = NULL;
35static GThreadPool *thread_pool_local = NULL;
a8374fcb
RN
36#ifdef HAVE_LIBMAPNIK
37static GThreadPool *thread_pool_local_mapnik = NULL;
38#endif
7711383f 39static gboolean stop_all_threads = FALSE;
634eca0a 40
d3b7baa7
RN
41// A single store of background items for all Windows
42// Must always be accessed in the main thread
50a14534
EB
43static GtkListStore *bgstore = NULL;
44
d3b7baa7 45G_LOCK_DEFINE_STATIC(window_list);
90142302
RN
46// Still only actually updating the statusbar though
47static GSList *windows_to_update = NULL;
50a14534
EB
48
49static gint bgitemcount = 0;
50
fc1318fd
RN
51#define VIK_BG_NUM_ARGS 7
52
50a14534
EB
53enum
54{
5c01c6b7 55 TITLE_COLUMN = 0,
50a14534
EB
56 PROGRESS_COLUMN,
57 DATA_COLUMN,
58 N_COLUMNS,
59};
60
90142302 61void a_background_update_status ( VikWindow *vw, gpointer data )
50a14534 62{
90142302
RN
63 static gchar buf[20];
64 g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount);
39ae014f 65 vik_window_statusbar_update ( vw, buf, VIK_STATUSBAR_ITEMS );
50a14534
EB
66}
67
d3b7baa7 68// NB can be called from any thread
50a14534
EB
69static void background_thread_update ()
70{
d3b7baa7 71 G_LOCK(window_list);
90142302 72 g_slist_foreach ( windows_to_update, (GFunc) a_background_update_status, NULL );
d3b7baa7
RN
73 G_UNLOCK(window_list);
74}
75
76typedef struct {
77 GtkTreeIter *iter;
78 gdouble percent;
79} progress_t;
80
81// In main thread
82static gboolean idle_progress_update ( gpointer user_data )
83{
84 progress_t *progress = user_data;
85 if ( bgstore )
86 gtk_list_store_set ( GTK_LIST_STORE(bgstore), progress->iter, PROGRESS_COLUMN, progress->percent, -1 );
87 g_free ( progress );
88 return FALSE;
50a14534
EB
89}
90
e38c5ff1
RN
91/**
92 * a_background_thread_progress:
93 * @callbackdata: Thread data
94 * @fraction: The value should be between 0.0 and 1.0 indicating percentage of the task complete
d3b7baa7
RN
95 *
96 * Called from other threads
97 *
98 * Returns a non zero number if the thread should be terminated
e38c5ff1 99 */
634eca0a 100int a_background_thread_progress ( gpointer callbackdata, gdouble fraction )
50a14534
EB
101{
102 gpointer *args = (gpointer *) callbackdata;
7711383f 103 int res = a_background_testcancel ( callbackdata );
a08969b3 104 if (args[5] != NULL) {
e38c5ff1
RN
105 gdouble myfraction = fabs(fraction);
106 if ( myfraction > 1.0 )
107 myfraction = 1.0;
d3b7baa7
RN
108 progress_t *progress = g_malloc0 ( sizeof(progress_t) );
109 progress->percent = myfraction * 100;
110 progress->iter = (GtkTreeIter*)args[5];
111 gdk_threads_add_idle ( idle_progress_update, progress );
a08969b3 112 }
50a14534
EB
113
114 args[6] = GINT_TO_POINTER(GPOINTER_TO_INT(args[6])-1);
115 bgitemcount--;
7711383f
JJ
116 background_thread_update();
117 return res;
50a14534
EB
118}
119
fc1318fd 120static void thread_die ( gpointer args[VIK_BG_NUM_ARGS] )
50a14534
EB
121{
122 vik_thr_free_func userdata_free_func = args[3];
123
eb6ae21e
GB
124 if ( userdata_free_func != NULL )
125 userdata_free_func ( args[2] );
50a14534
EB
126
127 if ( GPOINTER_TO_INT(args[6]) )
128 {
129 bgitemcount -= GPOINTER_TO_INT(args[6]);
130 background_thread_update ();
131 }
132
50a14534 133 g_free ( args );
50a14534
EB
134}
135
d3b7baa7
RN
136// Called from other threads
137// Returns a non zero number if the thread should be terminated
634eca0a 138int a_background_testcancel ( gpointer callbackdata )
50a14534
EB
139{
140 gpointer *args = (gpointer *) callbackdata;
7711383f
JJ
141 if ( stop_all_threads )
142 return -1;
143 if ( args && args[0] )
50a14534
EB
144 {
145 vik_thr_free_func cleanup = args[4];
146 if ( cleanup )
147 cleanup ( args[2] );
634eca0a 148 return -1;
50a14534 149 }
634eca0a 150 return 0;
50a14534 151}
818661ec 152
d3b7baa7
RN
153typedef struct {
154 GtkTreeIter *iter;
155} remove_t;
156
157// Called from the main thread
158static gboolean idle_remove ( gpointer user_data )
159{
160 remove_t *remove = user_data;
161 if ( bgstore )
162 gtk_list_store_remove ( bgstore, remove->iter );
163 g_free ( remove->iter );
164 return FALSE;
165}
166
167// Called from other threads
fc1318fd 168static void thread_helper ( gpointer args[VIK_BG_NUM_ARGS], gpointer user_data )
50a14534
EB
169{
170 /* unpack args */
171 vik_thr_func func = args[1];
172 gpointer userdata = args[2];
173
5c01c6b7
GB
174 g_debug(__FUNCTION__);
175
50a14534
EB
176 func ( userdata, args );
177
d3b7baa7
RN
178 if ( ! args[0] ) {
179 remove_t *remove = g_malloc0 ( sizeof(remove_t) );
180 remove->iter = args[5];
181 gdk_threads_add_idle ( idle_remove, remove );
182 }
50a14534
EB
183 thread_die ( args );
184}
185
43e8b799
GB
186/**
187 * a_background_thread:
c75da936 188 * @bp: Which pool this thread should run in
43e8b799
GB
189 * @parent:
190 * @message:
191 * @func: worker function
192 * @userdata:
193 * @userdata_free_func: free function for userdata
194 * @userdata_cancel_cleanup_func:
195 * @number_items:
196 *
197 * Function to enlist new background function.
198 */
c75da936 199void 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 )
50a14534
EB
200{
201 GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) );
fc1318fd 202 gpointer *args = g_malloc ( sizeof(gpointer) * VIK_BG_NUM_ARGS );
50a14534 203
5c01c6b7
GB
204 g_debug(__FUNCTION__);
205
50a14534
EB
206 args[0] = GINT_TO_POINTER(0);
207 args[1] = func;
208 args[2] = userdata;
209 args[3] = userdata_free_func;
210 args[4] = userdata_cancel_cleanup_func;
211 args[5] = piter;
212 args[6] = GINT_TO_POINTER(number_items);
213
214 bgitemcount += number_items;
215
216 gtk_list_store_append ( bgstore, piter );
5c01c6b7
GB
217 gtk_list_store_set ( bgstore, piter,
218 TITLE_COLUMN, message,
219 PROGRESS_COLUMN, 0.0,
220 DATA_COLUMN, args,
221 -1 );
50a14534
EB
222
223 /* run the thread in the background */
c75da936
RN
224 if ( bp == BACKGROUND_POOL_REMOTE )
225 g_thread_pool_push( thread_pool_remote, args, NULL );
a8374fcb
RN
226#ifdef HAVE_LIBMAPNIK
227 else if ( bp == BACKGROUND_POOL_LOCAL_MAPNIK )
228 g_thread_pool_push( thread_pool_local_mapnik, args, NULL );
229#endif
c75da936
RN
230 else
231 g_thread_pool_push( thread_pool_local, args, NULL );
50a14534
EB
232}
233
d3b7baa7 234// In main thread
50a14534
EB
235static void cancel_job_with_iter ( GtkTreeIter *piter )
236{
237 gpointer *args;
5c01c6b7 238
43e8b799 239 g_debug(__FUNCTION__);
5c01c6b7 240
50a14534
EB
241 gtk_tree_model_get( GTK_TREE_MODEL(bgstore), piter, DATA_COLUMN, &args, -1 );
242
243 /* we know args still exists because it is free _after_ the list item is destroyed */
244 /* need MUTEX ? */
245 args[0] = GINT_TO_POINTER(1); /* set killswitch */
246
50a14534 247 gtk_list_store_remove ( bgstore, piter );
d3b7baa7 248 g_free ( piter );
a08969b3 249 args[5] = NULL;
50a14534
EB
250}
251
d3b7baa7
RN
252// In main thread
253static void bgwindow_response (GtkDialog *dialog, gint response_id, GtkTreeView *bgtreeview )
50a14534 254{
d3b7baa7
RN
255 switch ( response_id ) {
256 case GTK_RESPONSE_DELETE_EVENT:
257 // Delibrate fall through
258 case GTK_RESPONSE_CLOSE:
259 gtk_widget_destroy ( GTK_WIDGET(dialog) );
260 break;
261 case 1: // Delete / Cancel selected item
439f34ec
AF
262 {
263 GtkTreeIter iter;
d3b7baa7
RN
264 if ( gtk_tree_selection_get_selected ( gtk_tree_view_get_selection(bgtreeview), NULL, &iter ) )
265 cancel_job_with_iter ( &iter );
439f34ec 266 background_thread_update();
439f34ec 267 }
d3b7baa7
RN
268 break;
269 case 2: // Clear All jobs
439f34ec
AF
270 {
271 GtkTreeIter iter;
272 while ( gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(bgstore), &iter ) )
d3b7baa7 273 cancel_job_with_iter ( &iter );
439f34ec 274 background_thread_update();
439f34ec 275 }
d3b7baa7
RN
276 default: break;
277 }
50a14534
EB
278}
279
062790f9 280#define VIK_SETTINGS_BACKGROUND_MAX_THREADS "background_max_threads"
c75da936 281#define VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL "background_max_threads_local"
062790f9 282
a8374fcb
RN
283#ifdef HAVE_LIBMAPNIK
284VikLayerParamScale params_threads[] = { {1, 64, 1, 0} }; // 64 threads should be enough for anyone...
285// implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues
286static VikLayerParam prefs_mapnik[] = {
287 { 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,
288 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 },
289};
290#endif
291
43e8b799
GB
292/**
293 * a_background_init:
294 *
e530cc9f
RN
295 * Just setup any preferences.
296 */
297void a_background_init ()
298{
299#ifdef HAVE_LIBMAPNIK
300 VikLayerParamData tmp;
301 // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues
302 tmp.u = 1; // Default to 1 thread due to potential crashing issues
303 a_preferences_register(&prefs_mapnik[0], tmp, "mapnik");
304#endif
305}
306
307/**
308 * a_background_post_init:
309 *
43e8b799
GB
310 * Initialize background feature.
311 */
e530cc9f 312void a_background_post_init()
50a14534 313{
c75da936 314 // initialize thread pools
634eca0a 315 gint max_threads = 10; /* limit maximum number of threads running at one time */
062790f9
RN
316 gint maxt;
317 if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS, &maxt ) )
318 max_threads = maxt;
319
c75da936
RN
320 thread_pool_remote = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
321
322 if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL, &maxt ) )
323 max_threads = maxt;
324 else {
325 guint cpus = util_get_number_of_cpus ();
326 max_threads = cpus > 1 ? cpus-1 : 1; // Don't use all available CPUs!
327 }
328
329 thread_pool_local = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
634eca0a 330
a8374fcb 331#ifdef HAVE_LIBMAPNIK
a8374fcb 332 // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues
a8374fcb
RN
333 guint mapnik_threads = a_preferences_get("mapnik.background_max_threads_local_mapnik")->u;
334 thread_pool_local_mapnik = g_thread_pool_new ( (GFunc) thread_helper, NULL, mapnik_threads, FALSE, NULL );
335#endif
336
d3b7baa7
RN
337 bgstore = gtk_list_store_new ( N_COLUMNS, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_POINTER );
338}
339
340/**
341 * a_background_show_window:
342 *
343 * Display the background window.
344 */
345void a_background_show_window ()
346{
347 GtkWidget *bgwindow = NULL;
348 GtkWidget *bgtreeview = NULL;
50a14534
EB
349 GtkCellRenderer *renderer;
350 GtkTreeViewColumn *column;
351 GtkWidget *scrolled_window;
352
353 /* store & treeview */
50a14534
EB
354 bgtreeview = gtk_tree_view_new_with_model ( GTK_TREE_MODEL(bgstore) );
355 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (bgtreeview), TRUE);
356 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (bgtreeview)),
357 GTK_SELECTION_SINGLE);
358
359 /* add columns */
360 renderer = gtk_cell_renderer_text_new ();
4c77d5e0 361 column = gtk_tree_view_column_new_with_attributes ( _("Job"), renderer, "text", TITLE_COLUMN, NULL );
50a14534
EB
362 gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column );
363
364 renderer = gtk_cell_renderer_progress_new ();
5c01c6b7 365 column = gtk_tree_view_column_new_with_attributes ( _("Progress"), renderer, "value", PROGRESS_COLUMN, NULL );
50a14534
EB
366 gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column );
367
368 /* setup window */
369 scrolled_window = gtk_scrolled_window_new ( NULL, NULL );
370 gtk_container_add ( GTK_CONTAINER(scrolled_window), bgtreeview );
371 gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
372
d3b7baa7 373 bgwindow = gtk_dialog_new_with_buttons ( _("Viking Background Jobs"), NULL, 0, GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, GTK_STOCK_DELETE, 1, GTK_STOCK_CLEAR, 2, NULL );
2bbbc9fb
RN
374 gtk_dialog_set_default_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
375 GtkWidget *response_w = NULL;
376#if GTK_CHECK_VERSION (2, 20, 0)
377 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT );
378#endif
9b082b39 379 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bgwindow))), scrolled_window, TRUE, TRUE, 0 );
50a14534 380 gtk_window_set_default_size ( GTK_WINDOW(bgwindow), 400, 400 );
2bbbc9fb
RN
381 if ( response_w )
382 gtk_widget_grab_focus ( response_w );
50a14534 383
d3b7baa7
RN
384 g_signal_connect ( G_OBJECT(bgwindow), "response", G_CALLBACK(bgwindow_response), GTK_TREE_VIEW(bgtreeview) );
385
386 gtk_widget_show_all ( bgwindow );
50a14534 387
d3b7baa7 388 gtk_dialog_set_default_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_CLOSE );
50a14534
EB
389}
390
43e8b799
GB
391/**
392 * a_background_uninit:
393 *
394 * Uninitialize background feature.
395 */
634eca0a
JJ
396void a_background_uninit()
397{
7711383f 398 stop_all_threads = TRUE;
d3b7baa7
RN
399 // Don't wait for these threads to complete - i.e. end now.
400 g_thread_pool_free ( thread_pool_remote, TRUE, FALSE );
c75da936 401 g_thread_pool_free ( thread_pool_local, TRUE, FALSE );
a8374fcb
RN
402#ifdef HAVE_LIBMAPNIK
403 g_thread_pool_free ( thread_pool_local_mapnik, TRUE, FALSE );
404#endif
919ed63e
RN
405 gtk_list_store_clear ( bgstore );
406 g_object_unref ( bgstore );
d3b7baa7 407 bgstore = NULL;
634eca0a
JJ
408}
409
90142302 410void a_background_add_window (VikWindow *vw)
50a14534 411{
d3b7baa7 412 G_LOCK(window_list);
90142302 413 windows_to_update = g_slist_prepend(windows_to_update,vw);
d3b7baa7 414 G_UNLOCK(window_list);
50a14534
EB
415}
416
90142302 417void a_background_remove_window (VikWindow *vw)
50a14534 418{
d3b7baa7 419 G_LOCK(window_list);
90142302 420 windows_to_update = g_slist_remove(windows_to_update,vw);
d3b7baa7 421 G_UNLOCK(window_list);
50a14534 422}