]>
Commit | Line | Data |
---|---|---|
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 |
34 | static GThreadPool *thread_pool_remote = NULL; |
35 | static GThreadPool *thread_pool_local = NULL; | |
a8374fcb RN |
36 | #ifdef HAVE_LIBMAPNIK |
37 | static GThreadPool *thread_pool_local_mapnik = NULL; | |
38 | #endif | |
7711383f | 39 | static gboolean stop_all_threads = FALSE; |
634eca0a | 40 | |
50a14534 EB |
41 | static GtkWidget *bgwindow = NULL; |
42 | static GtkWidget *bgtreeview = NULL; | |
43 | static GtkListStore *bgstore = NULL; | |
44 | ||
90142302 RN |
45 | // Still only actually updating the statusbar though |
46 | static GSList *windows_to_update = NULL; | |
50a14534 EB |
47 | |
48 | static gint bgitemcount = 0; | |
49 | ||
fc1318fd RN |
50 | #define VIK_BG_NUM_ARGS 7 |
51 | ||
50a14534 EB |
52 | enum |
53 | { | |
5c01c6b7 | 54 | TITLE_COLUMN = 0, |
50a14534 EB |
55 | PROGRESS_COLUMN, |
56 | DATA_COLUMN, | |
57 | N_COLUMNS, | |
58 | }; | |
59 | ||
90142302 | 60 | void a_background_update_status ( VikWindow *vw, gpointer data ) |
50a14534 | 61 | { |
90142302 RN |
62 | static gchar buf[20]; |
63 | g_snprintf(buf, sizeof(buf), _("%d items"), bgitemcount); | |
39ae014f | 64 | vik_window_statusbar_update ( vw, buf, VIK_STATUSBAR_ITEMS ); |
50a14534 EB |
65 | } |
66 | ||
67 | static void background_thread_update () | |
68 | { | |
90142302 | 69 | g_slist_foreach ( windows_to_update, (GFunc) a_background_update_status, NULL ); |
50a14534 EB |
70 | } |
71 | ||
e38c5ff1 RN |
72 | /** |
73 | * a_background_thread_progress: | |
74 | * @callbackdata: Thread data | |
75 | * @fraction: The value should be between 0.0 and 1.0 indicating percentage of the task complete | |
76 | */ | |
634eca0a | 77 | int a_background_thread_progress ( gpointer callbackdata, gdouble fraction ) |
50a14534 EB |
78 | { |
79 | gpointer *args = (gpointer *) callbackdata; | |
7711383f | 80 | int res = a_background_testcancel ( callbackdata ); |
a08969b3 | 81 | if (args[5] != NULL) { |
e38c5ff1 RN |
82 | gdouble myfraction = fabs(fraction); |
83 | if ( myfraction > 1.0 ) | |
84 | myfraction = 1.0; | |
a08969b3 | 85 | gdk_threads_enter(); |
e38c5ff1 | 86 | gtk_list_store_set( GTK_LIST_STORE(bgstore), (GtkTreeIter *) args[5], PROGRESS_COLUMN, myfraction*100, -1 ); |
a08969b3 JN |
87 | gdk_threads_leave(); |
88 | } | |
50a14534 EB |
89 | |
90 | args[6] = GINT_TO_POINTER(GPOINTER_TO_INT(args[6])-1); | |
91 | bgitemcount--; | |
7711383f JJ |
92 | background_thread_update(); |
93 | return res; | |
50a14534 EB |
94 | } |
95 | ||
fc1318fd | 96 | static void thread_die ( gpointer args[VIK_BG_NUM_ARGS] ) |
50a14534 EB |
97 | { |
98 | vik_thr_free_func userdata_free_func = args[3]; | |
99 | ||
eb6ae21e GB |
100 | if ( userdata_free_func != NULL ) |
101 | userdata_free_func ( args[2] ); | |
50a14534 EB |
102 | |
103 | if ( GPOINTER_TO_INT(args[6]) ) | |
104 | { | |
105 | bgitemcount -= GPOINTER_TO_INT(args[6]); | |
106 | background_thread_update (); | |
107 | } | |
108 | ||
109 | g_free ( args[5] ); /* free iter */ | |
110 | g_free ( args ); | |
50a14534 EB |
111 | } |
112 | ||
634eca0a | 113 | int a_background_testcancel ( gpointer callbackdata ) |
50a14534 EB |
114 | { |
115 | gpointer *args = (gpointer *) callbackdata; | |
7711383f JJ |
116 | if ( stop_all_threads ) |
117 | return -1; | |
118 | if ( args && args[0] ) | |
50a14534 EB |
119 | { |
120 | vik_thr_free_func cleanup = args[4]; | |
121 | if ( cleanup ) | |
122 | cleanup ( args[2] ); | |
634eca0a | 123 | return -1; |
50a14534 | 124 | } |
634eca0a | 125 | return 0; |
50a14534 | 126 | } |
818661ec | 127 | |
fc1318fd | 128 | static void thread_helper ( gpointer args[VIK_BG_NUM_ARGS], gpointer user_data ) |
50a14534 EB |
129 | { |
130 | /* unpack args */ | |
131 | vik_thr_func func = args[1]; | |
132 | gpointer userdata = args[2]; | |
133 | ||
5c01c6b7 GB |
134 | g_debug(__FUNCTION__); |
135 | ||
50a14534 EB |
136 | func ( userdata, args ); |
137 | ||
50a14534 EB |
138 | gdk_threads_enter(); |
139 | if ( ! args[0] ) | |
140 | gtk_list_store_remove ( bgstore, (GtkTreeIter *) args[5] ); | |
141 | gdk_threads_leave(); | |
142 | ||
143 | thread_die ( args ); | |
144 | } | |
145 | ||
43e8b799 GB |
146 | /** |
147 | * a_background_thread: | |
c75da936 | 148 | * @bp: Which pool this thread should run in |
43e8b799 GB |
149 | * @parent: |
150 | * @message: | |
151 | * @func: worker function | |
152 | * @userdata: | |
153 | * @userdata_free_func: free function for userdata | |
154 | * @userdata_cancel_cleanup_func: | |
155 | * @number_items: | |
156 | * | |
157 | * Function to enlist new background function. | |
158 | */ | |
c75da936 | 159 | 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 ) |
50a14534 EB |
160 | { |
161 | GtkTreeIter *piter = g_malloc ( sizeof ( GtkTreeIter ) ); | |
fc1318fd | 162 | gpointer *args = g_malloc ( sizeof(gpointer) * VIK_BG_NUM_ARGS ); |
50a14534 | 163 | |
5c01c6b7 GB |
164 | g_debug(__FUNCTION__); |
165 | ||
50a14534 EB |
166 | args[0] = GINT_TO_POINTER(0); |
167 | args[1] = func; | |
168 | args[2] = userdata; | |
169 | args[3] = userdata_free_func; | |
170 | args[4] = userdata_cancel_cleanup_func; | |
171 | args[5] = piter; | |
172 | args[6] = GINT_TO_POINTER(number_items); | |
173 | ||
174 | bgitemcount += number_items; | |
175 | ||
176 | gtk_list_store_append ( bgstore, piter ); | |
5c01c6b7 GB |
177 | gtk_list_store_set ( bgstore, piter, |
178 | TITLE_COLUMN, message, | |
179 | PROGRESS_COLUMN, 0.0, | |
180 | DATA_COLUMN, args, | |
181 | -1 ); | |
50a14534 EB |
182 | |
183 | /* run the thread in the background */ | |
c75da936 RN |
184 | if ( bp == BACKGROUND_POOL_REMOTE ) |
185 | g_thread_pool_push( thread_pool_remote, args, NULL ); | |
a8374fcb RN |
186 | #ifdef HAVE_LIBMAPNIK |
187 | else if ( bp == BACKGROUND_POOL_LOCAL_MAPNIK ) | |
188 | g_thread_pool_push( thread_pool_local_mapnik, args, NULL ); | |
189 | #endif | |
c75da936 RN |
190 | else |
191 | g_thread_pool_push( thread_pool_local, args, NULL ); | |
50a14534 EB |
192 | } |
193 | ||
43e8b799 GB |
194 | /** |
195 | * a_background_show_window: | |
196 | * | |
197 | * Display the background window. | |
198 | */ | |
50a14534 EB |
199 | void a_background_show_window () |
200 | { | |
201 | gtk_widget_show_all ( bgwindow ); | |
202 | } | |
203 | ||
204 | static void cancel_job_with_iter ( GtkTreeIter *piter ) | |
205 | { | |
206 | gpointer *args; | |
5c01c6b7 | 207 | |
43e8b799 | 208 | g_debug(__FUNCTION__); |
5c01c6b7 | 209 | |
50a14534 EB |
210 | gtk_tree_model_get( GTK_TREE_MODEL(bgstore), piter, DATA_COLUMN, &args, -1 ); |
211 | ||
212 | /* we know args still exists because it is free _after_ the list item is destroyed */ | |
213 | /* need MUTEX ? */ | |
214 | args[0] = GINT_TO_POINTER(1); /* set killswitch */ | |
215 | ||
50a14534 | 216 | gtk_list_store_remove ( bgstore, piter ); |
a08969b3 | 217 | args[5] = NULL; |
50a14534 EB |
218 | } |
219 | ||
220 | static void bgwindow_response (GtkDialog *dialog, gint arg1 ) | |
221 | { | |
439f34ec AF |
222 | /* note this function is a signal handler called back from the GTK main loop, |
223 | * so GDK is already locked. We need to release the lock before calling | |
224 | * thread-safe routines | |
225 | */ | |
226 | if ( arg1 == 1 ) /* cancel */ | |
227 | { | |
228 | GtkTreeIter iter; | |
229 | if ( gtk_tree_selection_get_selected ( gtk_tree_view_get_selection ( GTK_TREE_VIEW(bgtreeview) ), NULL, &iter ) ) | |
230 | cancel_job_with_iter ( &iter ); | |
231 | gdk_threads_leave(); | |
232 | background_thread_update(); | |
233 | gdk_threads_enter(); | |
234 | } | |
50a14534 | 235 | else if ( arg1 == 2 ) /* clear */ |
439f34ec AF |
236 | { |
237 | GtkTreeIter iter; | |
238 | while ( gtk_tree_model_get_iter_first ( GTK_TREE_MODEL(bgstore), &iter ) ) | |
239 | cancel_job_with_iter ( &iter ); | |
439f34ec AF |
240 | gdk_threads_leave(); |
241 | background_thread_update(); | |
242 | gdk_threads_enter(); | |
243 | } | |
50a14534 EB |
244 | else /* OK */ |
245 | gtk_widget_hide ( bgwindow ); | |
246 | } | |
247 | ||
062790f9 | 248 | #define VIK_SETTINGS_BACKGROUND_MAX_THREADS "background_max_threads" |
c75da936 | 249 | #define VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL "background_max_threads_local" |
062790f9 | 250 | |
a8374fcb RN |
251 | #ifdef HAVE_LIBMAPNIK |
252 | VikLayerParamScale params_threads[] = { {1, 64, 1, 0} }; // 64 threads should be enough for anyone... | |
253 | // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues | |
254 | static VikLayerParam prefs_mapnik[] = { | |
255 | { 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, | |
256 | 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 }, | |
257 | }; | |
258 | #endif | |
259 | ||
43e8b799 GB |
260 | /** |
261 | * a_background_init: | |
262 | * | |
e530cc9f RN |
263 | * Just setup any preferences. |
264 | */ | |
265 | void a_background_init () | |
266 | { | |
267 | #ifdef HAVE_LIBMAPNIK | |
268 | VikLayerParamData tmp; | |
269 | // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues | |
270 | tmp.u = 1; // Default to 1 thread due to potential crashing issues | |
271 | a_preferences_register(&prefs_mapnik[0], tmp, "mapnik"); | |
272 | #endif | |
273 | } | |
274 | ||
275 | /** | |
276 | * a_background_post_init: | |
277 | * | |
43e8b799 GB |
278 | * Initialize background feature. |
279 | */ | |
e530cc9f | 280 | void a_background_post_init() |
50a14534 | 281 | { |
c75da936 | 282 | // initialize thread pools |
634eca0a | 283 | gint max_threads = 10; /* limit maximum number of threads running at one time */ |
062790f9 RN |
284 | gint maxt; |
285 | if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS, &maxt ) ) | |
286 | max_threads = maxt; | |
287 | ||
c75da936 RN |
288 | thread_pool_remote = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL ); |
289 | ||
290 | if ( a_settings_get_integer ( VIK_SETTINGS_BACKGROUND_MAX_THREADS_LOCAL, &maxt ) ) | |
291 | max_threads = maxt; | |
292 | else { | |
293 | guint cpus = util_get_number_of_cpus (); | |
294 | max_threads = cpus > 1 ? cpus-1 : 1; // Don't use all available CPUs! | |
295 | } | |
296 | ||
297 | thread_pool_local = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL ); | |
634eca0a | 298 | |
a8374fcb | 299 | #ifdef HAVE_LIBMAPNIK |
a8374fcb | 300 | // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues |
a8374fcb RN |
301 | guint mapnik_threads = a_preferences_get("mapnik.background_max_threads_local_mapnik")->u; |
302 | thread_pool_local_mapnik = g_thread_pool_new ( (GFunc) thread_helper, NULL, mapnik_threads, FALSE, NULL ); | |
303 | #endif | |
304 | ||
50a14534 EB |
305 | GtkCellRenderer *renderer; |
306 | GtkTreeViewColumn *column; | |
307 | GtkWidget *scrolled_window; | |
308 | ||
5c01c6b7 GB |
309 | g_debug(__FUNCTION__); |
310 | ||
50a14534 EB |
311 | /* store & treeview */ |
312 | bgstore = gtk_list_store_new ( N_COLUMNS, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_POINTER ); | |
313 | bgtreeview = gtk_tree_view_new_with_model ( GTK_TREE_MODEL(bgstore) ); | |
314 | gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (bgtreeview), TRUE); | |
315 | gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (bgtreeview)), | |
316 | GTK_SELECTION_SINGLE); | |
317 | ||
318 | /* add columns */ | |
319 | renderer = gtk_cell_renderer_text_new (); | |
4c77d5e0 | 320 | column = gtk_tree_view_column_new_with_attributes ( _("Job"), renderer, "text", TITLE_COLUMN, NULL ); |
50a14534 EB |
321 | gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column ); |
322 | ||
323 | renderer = gtk_cell_renderer_progress_new (); | |
5c01c6b7 | 324 | column = gtk_tree_view_column_new_with_attributes ( _("Progress"), renderer, "value", PROGRESS_COLUMN, NULL ); |
50a14534 EB |
325 | gtk_tree_view_append_column ( GTK_TREE_VIEW(bgtreeview), column ); |
326 | ||
327 | /* setup window */ | |
328 | scrolled_window = gtk_scrolled_window_new ( NULL, NULL ); | |
329 | gtk_container_add ( GTK_CONTAINER(scrolled_window), bgtreeview ); | |
330 | gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
331 | ||
332 | bgwindow = gtk_dialog_new_with_buttons ( "", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_DELETE, 1, GTK_STOCK_CLEAR, 2, NULL ); | |
2bbbc9fb RN |
333 | gtk_dialog_set_default_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT ); |
334 | GtkWidget *response_w = NULL; | |
335 | #if GTK_CHECK_VERSION (2, 20, 0) | |
336 | response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(bgwindow), GTK_RESPONSE_ACCEPT ); | |
337 | #endif | |
9b082b39 | 338 | gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bgwindow))), scrolled_window, TRUE, TRUE, 0 ); |
50a14534 | 339 | gtk_window_set_default_size ( GTK_WINDOW(bgwindow), 400, 400 ); |
4c77d5e0 | 340 | gtk_window_set_title ( GTK_WINDOW(bgwindow), _("Viking Background Jobs") ); |
2bbbc9fb RN |
341 | if ( response_w ) |
342 | gtk_widget_grab_focus ( response_w ); | |
50a14534 EB |
343 | /* don't destroy win */ |
344 | g_signal_connect ( G_OBJECT(bgwindow), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL ); | |
345 | ||
346 | g_signal_connect ( G_OBJECT(bgwindow), "response", G_CALLBACK(bgwindow_response), 0 ); | |
347 | ||
348 | } | |
349 | ||
43e8b799 GB |
350 | /** |
351 | * a_background_uninit: | |
352 | * | |
353 | * Uninitialize background feature. | |
354 | */ | |
634eca0a JJ |
355 | void a_background_uninit() |
356 | { | |
7711383f | 357 | stop_all_threads = TRUE; |
c75da936 RN |
358 | // wait until these threads stop |
359 | g_thread_pool_free ( thread_pool_remote, TRUE, TRUE ); | |
360 | // Don't wait for these | |
361 | g_thread_pool_free ( thread_pool_local, TRUE, FALSE ); | |
a8374fcb RN |
362 | #ifdef HAVE_LIBMAPNIK |
363 | g_thread_pool_free ( thread_pool_local_mapnik, TRUE, FALSE ); | |
364 | #endif | |
919ed63e RN |
365 | |
366 | gtk_list_store_clear ( bgstore ); | |
367 | g_object_unref ( bgstore ); | |
368 | ||
369 | gtk_widget_destroy ( bgwindow ); | |
634eca0a JJ |
370 | } |
371 | ||
90142302 | 372 | void a_background_add_window (VikWindow *vw) |
50a14534 | 373 | { |
90142302 | 374 | windows_to_update = g_slist_prepend(windows_to_update,vw); |
50a14534 EB |
375 | } |
376 | ||
90142302 | 377 | void a_background_remove_window (VikWindow *vw) |
50a14534 | 378 | { |
90142302 | 379 | windows_to_update = g_slist_remove(windows_to_update,vw); |
50a14534 | 380 | } |