]> git.street.me.uk Git - andy/viking.git/blob - src/main.c
Threading rewrite to remove use of unsupported cross platform threading update of...
[andy/viking.git] / src / main.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG
23 #include "config.h"
24 #endif /* HAVE_CONFIG */
25
26 #include "viking.h"
27 #include "icons/icons.h"
28 #include "mapcache.h"
29 #include "background.h"
30 #include "dems.h"
31 #include "babel.h"
32 #include "curl_download.h"
33 #include "preferences.h"
34 #include "viklayer_defaults.h"
35 #include "globals.h"
36 #include "vikmapslayer.h"
37 #include "vikgeoreflayer.h"
38 #include "vikrouting.h"
39 #include "vikutils.h"
40 #include "util.h"
41 #include "toolbar.h"
42
43 #ifdef HAVE_STDLIB_H
44 #include <stdlib.h>
45 #endif
46 #ifdef HAVE_STRING_H
47 #include <string.h>
48 #endif
49
50 #include <glib/gprintf.h>
51 #include <glib/gi18n.h>
52
53 #include "modules.h"
54
55 /* FIXME LOCALEDIR must be configured by ./configure --localedir */
56 /* But something does not work actually. */
57 /* So, we need to redefine this variable on windows. */
58 #ifdef WINDOWS
59 #undef LOCALEDIR
60 #define LOCALEDIR "locale"
61 #endif
62
63 #ifdef HAVE_X11_XLIB_H
64 #include "X11/Xlib.h"
65 #endif
66
67 #if GLIB_CHECK_VERSION (2, 32, 0)
68 /* Callback to log message */
69 static void log_debug(const gchar *log_domain,
70                       GLogLevelFlags log_level,
71                       const gchar *message,
72                       gpointer user_data)
73 {
74   g_print("** (viking): DEBUG: %s\n", message);
75 }
76 #else
77 /* Callback to mute log message */
78 static void mute_log(const gchar *log_domain,
79                      GLogLevelFlags log_level,
80                      const gchar *message,
81                      gpointer user_data)
82 {
83   /* Nothing to do, we just want to mute */
84 }
85 #endif
86
87 #if HAVE_X11_XLIB_H
88 static int myXErrorHandler(Display *display, XErrorEvent *theEvent)
89 {
90   (void)g_fprintf (stderr,
91                    _("Ignoring Xlib error: error code %d request code %d\n"),
92                    theEvent->error_code,
93                    theEvent->request_code);
94   // No exit on X errors!
95   //  mainly to handle out of memory error when requesting large pixbuf from user request
96   //  see vikwindow.c::save_image_file ()
97   return 0;
98 }
99 #endif
100
101 // Default values that won't actually get applied unless changed by command line parameter values
102 static gdouble latitude = 0.0;
103 static gdouble longitude = 0.0;
104 static gint zoom_level_osm = -1;
105 static gint map_id = -1;
106
107 /* Options */
108 static GOptionEntry entries[] = 
109 {
110   { "debug", 'd', 0, G_OPTION_ARG_NONE, &vik_debug, N_("Enable debug output"), NULL },
111   { "verbose", 'V', 0, G_OPTION_ARG_NONE, &vik_verbose, N_("Enable verbose output"), NULL },
112   { "version", 'v', 0, G_OPTION_ARG_NONE, &vik_version, N_("Show version"), NULL },
113   { "latitude", 0, 0, G_OPTION_ARG_DOUBLE, &latitude, N_("Latitude in decimal degrees"), NULL },
114   { "longitude", 0, 0, G_OPTION_ARG_DOUBLE, &longitude, N_("Longitude in decimal degrees"), NULL },
115   { "zoom", 'z', 0, G_OPTION_ARG_INT, &zoom_level_osm, N_("Zoom Level (OSM). Value can be 0 - 22"), NULL },
116   { "map", 'm', 0, G_OPTION_ARG_INT, &map_id, N_("Add a map layer by id value. Use 0 for the default map."), NULL },
117   { NULL }
118 };
119
120 int main( int argc, char *argv[] )
121 {
122   VikWindow *first_window;
123   GdkPixbuf *main_icon;
124   gboolean dashdash_already = FALSE;
125   int i = 0;
126   GError *error = NULL;
127   gboolean gui_initialized;
128         
129   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);  
130   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
131   textdomain (GETTEXT_PACKAGE);
132
133 #if ! GLIB_CHECK_VERSION (2, 32, 0)
134   g_thread_init ( NULL );
135 #endif
136
137   gui_initialized = gtk_init_with_args (&argc, &argv, "files+", entries, NULL, &error);
138   if (!gui_initialized)
139   {
140     /* check if we have an error message */
141     if (error == NULL)
142     {
143       /* no error message, the GUI initialization failed */
144       const gchar *display_name = gdk_get_display_arg_name ();
145       (void)g_fprintf (stderr, "Failed to open display: %s\n", (display_name != NULL) ? display_name : " ");
146     }
147     else
148     {
149       /* yep, there's an error, so print it */
150       (void)g_fprintf (stderr, "Parsing command line options failed: %s\n", error->message);
151       g_error_free (error);
152       (void)g_fprintf (stderr, "Run \"%s --help\" to see the list of recognized options.\n",argv[0]);
153     }
154     return EXIT_FAILURE;
155   }
156    
157   if (vik_version)
158   {
159     (void)g_printf (_("%s %s\nCopyright (c) 2003-2008 Evan Battaglia\nCopyright (c) 2008-%s Viking's contributors\n"), PACKAGE_NAME, PACKAGE_VERSION, THEYEAR);
160     return EXIT_SUCCESS;
161   }
162
163 #if GLIB_CHECK_VERSION (2, 32, 0)
164   if (vik_debug)
165     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, log_debug, NULL);
166 #else
167   if (!vik_debug)
168     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, mute_log, NULL);
169 #endif
170
171 #if HAVE_X11_XLIB_H
172   XSetErrorHandler(myXErrorHandler);
173 #endif
174
175   // Ensure correct capitalization of the program name
176   g_set_application_name ("Viking");
177
178   // Discover if this is the very first run
179   a_vik_very_first_run ();
180
181   a_settings_init ();
182   a_preferences_init ();
183
184  /*
185   * First stage initialization
186   *
187   * Should not use a_preferences_get() yet
188   *
189   * Since the first time a_preferences_get() is called it loads any preferences values from disk,
190   *  but of course for preferences not registered yet it can't actually understand them
191   *  so subsequent initial attempts to get those preferences return the default value, until the values have changed
192   */
193   a_vik_preferences_init ();
194
195   a_layer_defaults_init ();
196
197   a_download_init();
198   curl_download_init();
199
200   a_babel_init ();
201
202   /* Init modules/plugins */
203   modules_init();
204
205   vik_georef_layer_init ();
206   maps_layer_init ();
207   a_mapcache_init ();
208   a_background_init ();
209
210   a_toolbar_init();
211   vik_routing_prefs_init();
212
213   /*
214    * Second stage initialization
215    *
216    * Can now use a_preferences_get()
217    */
218   a_background_post_init ();
219   a_babel_post_init ();
220   modules_post_init ();
221
222   // May need to initialize the Positonal TimeZone lookup
223   if ( a_vik_get_time_ref_frame() == VIK_TIME_REF_WORLD )
224     vu_setup_lat_lon_tz_lookup();
225
226   /* Set the icon */
227   main_icon = gdk_pixbuf_from_pixdata(&viking_pixbuf, FALSE, NULL);
228   gtk_window_set_default_icon(main_icon);
229
230   // Ask for confirmation of default settings on first run
231   vu_set_auto_features_on_first_run ();
232
233   /* Create the first window */
234   first_window = vik_window_new_window();
235
236   vu_check_latest_version ( GTK_WINDOW(first_window) );
237
238   // Load startup file first so that subsequent files are loaded on top
239   // Especially so that new tracks+waypoints will be above any maps in a startup file
240   if ( a_vik_get_startup_method () == VIK_STARTUP_METHOD_SPECIFIED_FILE ) {
241     gboolean load_startup_file = TRUE;
242     // When a viking file is to be loaded via the command line
243     //  then we'll skip loading any startup file
244     int jj = 0;
245     while ( ++jj < argc ) {
246       if ( check_file_magic_vik(argv[jj]) )
247         load_startup_file = FALSE;
248     }
249     if ( load_startup_file )
250       vik_window_open_file ( first_window, a_vik_get_startup_file(), TRUE );
251   }
252
253   while ( ++i < argc ) {
254     if ( strcmp(argv[i],"--") == 0 && !dashdash_already )
255       dashdash_already = TRUE; /* hack to open '-' */
256     else {
257       VikWindow *newvw = first_window;
258       gboolean change_filename = (i == 1);
259
260       // Open any subsequent .vik files in their own window
261       if ( i > 1 && check_file_magic_vik ( argv[i] ) ) {
262         newvw = vik_window_new_window ();
263         change_filename = TRUE;
264       }
265
266       vik_window_open_file ( newvw, argv[i], change_filename );
267     }
268   }
269
270   vik_window_new_window_finish ( first_window );
271
272   vu_command_line ( first_window, latitude, longitude, zoom_level_osm, map_id );
273
274   gtk_main ();
275
276   a_babel_uninit ();
277   a_toolbar_uninit ();
278   a_background_uninit ();
279   a_mapcache_uninit ();
280   a_dems_uninit ();
281   a_layer_defaults_uninit ();
282   a_preferences_uninit ();
283   a_settings_uninit ();
284
285   modules_uninit();
286
287   curl_download_uninit();
288
289   vu_finalize_lat_lon_tz_lookup ();
290
291   // Clean up any temporary files
292   util_remove_all_in_deletion_list ();
293
294   return 0;
295 }