]> git.street.me.uk Git - andy/viking.git/blob - src/main.c
Add kdtree C code version 0.5.6 from https://code.google.com/p/kdtree/
[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 "vikrouting.h"
38 #include "vikutils.h"
39 #include "util.h"
40
41 #ifdef VIK_CONFIG_GEOCACHES
42 void a_datasource_gc_init();
43 #endif
44
45 #ifdef HAVE_STDLIB_H
46 #include <stdlib.h>
47 #endif
48 #ifdef HAVE_STRING_H
49 #include <string.h>
50 #endif
51
52 #include <glib/gprintf.h>
53 #include <glib/gi18n.h>
54
55 #include "modules.h"
56
57 /* FIXME LOCALEDIR must be configured by ./configure --localedir */
58 /* But something does not work actually. */
59 /* So, we need to redefine this variable on windows. */
60 #ifdef WINDOWS
61 #undef LOCALEDIR
62 #define LOCALEDIR "locale"
63 #endif
64
65 #ifdef HAVE_X11_XLIB_H
66 #include "X11/Xlib.h"
67 #endif
68
69 #if GLIB_CHECK_VERSION (2, 32, 0)
70 /* Callback to log message */
71 static void log_debug(const gchar *log_domain,
72                       GLogLevelFlags log_level,
73                       const gchar *message,
74                       gpointer user_data)
75 {
76   g_print("** (viking): DEBUG: %s\n", message);
77 }
78 #else
79 /* Callback to mute log message */
80 static void mute_log(const gchar *log_domain,
81                      GLogLevelFlags log_level,
82                      const gchar *message,
83                      gpointer user_data)
84 {
85   /* Nothing to do, we just want to mute */
86 }
87 #endif
88
89 #if HAVE_X11_XLIB_H
90 static int myXErrorHandler(Display *display, XErrorEvent *theEvent)
91 {
92   g_fprintf (stderr,
93              _("Ignoring Xlib error: error code %d request code %d\n"),
94              theEvent->error_code,
95              theEvent->request_code);
96   // No exit on X errors!
97   //  mainly to handle out of memory error when requesting large pixbuf from user request
98   //  see vikwindow.c::save_image_file ()
99   return 0;
100 }
101 #endif
102
103 /* Options */
104 static GOptionEntry entries[] = 
105 {
106   { "debug", 'd', 0, G_OPTION_ARG_NONE, &vik_debug, N_("Enable debug output"), NULL },
107   { "verbose", 'V', 0, G_OPTION_ARG_NONE, &vik_verbose, N_("Enable verbose output"), NULL },
108   { "version", 'v', 0, G_OPTION_ARG_NONE, &vik_version, N_("Show version"), NULL },
109   { NULL }
110 };
111
112 int main( int argc, char *argv[] )
113 {
114   VikWindow *first_window;
115   GdkPixbuf *main_icon;
116   gboolean dashdash_already = FALSE;
117   int i = 0;
118   GError *error = NULL;
119   gboolean gui_initialized;
120         
121   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);  
122   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
123   textdomain (GETTEXT_PACKAGE);
124
125 #if ! GLIB_CHECK_VERSION (2, 32, 0)
126   g_thread_init ( NULL );
127 #endif
128   gdk_threads_init ();
129
130   gui_initialized = gtk_init_with_args (&argc, &argv, "files+", entries, NULL, &error);
131   if (!gui_initialized)
132   {
133     /* check if we have an error message */
134     if (error == NULL)
135     {
136       /* no error message, the GUI initialization failed */
137       const gchar *display_name = gdk_get_display_arg_name ();
138       g_fprintf (stderr, "Failed to open display: %s\n", (display_name != NULL) ? display_name : " ");
139     }
140     else
141     {
142       /* yep, there's an error, so print it */
143       g_fprintf (stderr, "Parsing command line options failed: %s\n", error->message);
144       g_error_free (error);
145       g_fprintf (stderr, "Run \"%s --help\" to see the list of recognized options.\n",argv[0]);
146     }
147     return EXIT_FAILURE;
148   }
149    
150   if (vik_version)
151   {
152     g_printf ("%s %s\nCopyright (c) 2003-2008 Evan Battaglia\nCopyright (c) 2008-2012 Viking's contributors\n", PACKAGE_NAME, PACKAGE_VERSION);
153     return EXIT_SUCCESS;
154   }
155
156 #if GLIB_CHECK_VERSION (2, 32, 0)
157   if (vik_debug)
158     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, log_debug, NULL);
159 #else
160   if (!vik_debug)
161     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, mute_log, NULL);
162 #endif
163
164 #if HAVE_X11_XLIB_H
165   XSetErrorHandler(myXErrorHandler);
166 #endif
167
168   // Discover if this is the very first run
169   a_vik_very_first_run ();
170
171   a_settings_init ();
172   a_preferences_init ();
173
174   a_vik_preferences_init ();
175
176   a_layer_defaults_init ();
177
178   a_download_init();
179   curl_download_init();
180
181   a_babel_init ();
182
183   /* Init modules/plugins */
184   modules_init();
185
186   maps_layer_init ();
187   a_mapcache_init ();
188   a_background_init ();
189
190 #ifdef VIK_CONFIG_GEOCACHES
191   a_datasource_gc_init();
192 #endif
193
194   vik_routing_prefs_init();
195
196   /* Set the icon */
197   main_icon = gdk_pixbuf_from_pixdata(&viking_pixbuf, FALSE, NULL);
198   gtk_window_set_default_icon(main_icon);
199
200   gdk_threads_enter ();
201
202   // Ask for confirmation of default settings on first run
203   vu_set_auto_features_on_first_run ();
204
205   /* Create the first window */
206   first_window = vik_window_new_window();
207
208   vu_check_latest_version ( GTK_WINDOW(first_window) );
209
210   while ( ++i < argc ) {
211     if ( strcmp(argv[i],"--") == 0 && !dashdash_already )
212       dashdash_already = TRUE; /* hack to open '-' */
213     else {
214       VikWindow *newvw = first_window;
215       gboolean change_filename = (i == 1);
216
217       // Open any subsequent .vik files in their own window
218       if ( i > 1 && check_file_magic_vik ( argv[i] ) ) {
219         newvw = vik_window_new_window ();
220         change_filename = TRUE;
221       }
222
223       vik_window_open_file ( newvw, argv[i], change_filename );
224     }
225   }
226
227   vik_window_new_window_finish ( first_window );
228
229   gtk_main ();
230   gdk_threads_leave ();
231
232   a_babel_uninit ();
233
234   a_background_uninit ();
235   a_mapcache_uninit ();
236   a_dems_uninit ();
237   a_layer_defaults_uninit ();
238   a_preferences_uninit ();
239   a_settings_uninit ();
240
241   curl_download_uninit();
242
243   // Clean up any temporary files
244   util_remove_all_in_deletion_list ();
245
246   return 0;
247 }