]> git.street.me.uk Git - andy/viking.git/blame - src/main.c
Fix maintaining a transparent background when an alpha value is applied to a pixbuf.
[andy/viking.git] / src / main.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>
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
a5daec1c
GB
22#ifdef HAVE_CONFIG
23#include "config.h"
24#endif /* HAVE_CONFIG */
25
50a14534 26#include "viking.h"
b6e6dfbd 27#include "icons/icons.h"
50a14534
EB
28#include "mapcache.h"
29#include "background.h"
ad0a8c2d 30#include "dems.h"
18ec873d 31#include "babel.h"
94a036d4 32#include "curl_download.h"
17a1f8f9 33#include "preferences.h"
a7023a1b 34#include "viklayer_defaults.h"
a58aaed4 35#include "globals.h"
55ddef4e 36#include "vikmapslayer.h"
3bd57299 37#include "vikgeoreflayer.h"
9f30939a 38#include "vikrouting.h"
5ab2942c 39#include "vikutils.h"
44871dd1 40#include "util.h"
75b7457a 41#include "toolbar.h"
50a14534 42
51539ae0 43#ifdef HAVE_STDLIB_H
fd0a7199 44#include <stdlib.h>
51539ae0
GB
45#endif
46#ifdef HAVE_STRING_H
50a14534 47#include <string.h>
51539ae0 48#endif
50a14534 49
57e87d1d 50#include <glib/gprintf.h>
a5daec1c 51#include <glib/gi18n.h>
57e87d1d 52
cdcaf41c
QT
53#include "modules.h"
54
6c6f8d24
MA
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
5cd09d57
RN
63#ifdef HAVE_X11_XLIB_H
64#include "X11/Xlib.h"
65#endif
66
14b9e3a4
GB
67#if GLIB_CHECK_VERSION (2, 32, 0)
68/* Callback to log message */
69static 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
2936913d
GB
77/* Callback to mute log message */
78static 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}
14b9e3a4 85#endif
50a14534 86
5cd09d57
RN
87#if HAVE_X11_XLIB_H
88static int myXErrorHandler(Display *display, XErrorEvent *theEvent)
89{
90 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
c12d4347
RN
101// Default values that won't actually get applied unless changed by command line parameter values
102static gdouble latitude = 0.0;
103static gdouble longitude = 0.0;
104static gint zoom_level_osm = -1;
105static gint map_id = -1;
106
53c54171 107/* Options */
fd0a7199
GB
108static GOptionEntry entries[] =
109{
2936913d
GB
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 },
c12d4347
RN
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 },
fd0a7199
GB
117 { NULL }
118};
119
50a14534
EB
120int main( int argc, char *argv[] )
121{
122 VikWindow *first_window;
260703bf 123 GdkPixbuf *main_icon;
50a14534
EB
124 gboolean dashdash_already = FALSE;
125 int i = 0;
fd0a7199
GB
126 GError *error = NULL;
127 gboolean gui_initialized;
a5daec1c
GB
128
129 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
130 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
131 textdomain (GETTEXT_PACKAGE);
50a14534 132
b832ea69 133#if ! GLIB_CHECK_VERSION (2, 32, 0)
50a14534 134 g_thread_init ( NULL );
b832ea69 135#endif
50a14534
EB
136 gdk_threads_init ();
137
fd0a7199
GB
138 gui_initialized = gtk_init_with_args (&argc, &argv, "files+", entries, NULL, &error);
139 if (!gui_initialized)
140 {
141 /* check if we have an error message */
142 if (error == NULL)
143 {
144 /* no error message, the GUI initialization failed */
145 const gchar *display_name = gdk_get_display_arg_name ();
146 g_fprintf (stderr, "Failed to open display: %s\n", (display_name != NULL) ? display_name : " ");
147 }
148 else
149 {
150 /* yep, there's an error, so print it */
151 g_fprintf (stderr, "Parsing command line options failed: %s\n", error->message);
152 g_error_free (error);
153 g_fprintf (stderr, "Run \"%s --help\" to see the list of recognized options.\n",argv[0]);
154 }
155 return EXIT_FAILURE;
156 }
53c54171 157
2936913d 158 if (vik_version)
53c54171 159 {
02468bf1 160 g_printf ("%s %s\nCopyright (c) 2003-2008 Evan Battaglia\nCopyright (c) 2008-"THEYEAR" Viking's contributors\n", PACKAGE_NAME, PACKAGE_VERSION);
53c54171
GB
161 return EXIT_SUCCESS;
162 }
50a14534 163
14b9e3a4
GB
164#if GLIB_CHECK_VERSION (2, 32, 0)
165 if (vik_debug)
166 g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, log_debug, NULL);
167#else
2936913d
GB
168 if (!vik_debug)
169 g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, mute_log, NULL);
14b9e3a4 170#endif
2936913d 171
5cd09d57
RN
172#if HAVE_X11_XLIB_H
173 XSetErrorHandler(myXErrorHandler);
174#endif
175
20015a31
RN
176 // Discover if this is the very first run
177 a_vik_very_first_run ();
178
66b23637 179 a_settings_init ();
843b99df
GB
180 a_preferences_init ();
181
e530cc9f
RN
182 /*
183 * First stage initialization
184 *
185 * Should not use a_preferences_get() yet
186 *
187 * Since the first time a_preferences_get() is called it loads any preferences values from disk,
188 * but of course for preferences not registered yet it can't actually understand them
189 * so subsequent initial attempts to get those preferences return the default value, until the values have changed
190 */
a58aaed4
GB
191 a_vik_preferences_init ();
192
a7023a1b
RN
193 a_layer_defaults_init ();
194
6693f5f9
GB
195 a_download_init();
196 curl_download_init();
197
18ec873d
GB
198 a_babel_init ();
199
cdcaf41c
QT
200 /* Init modules/plugins */
201 modules_init();
202
3bd57299 203 vik_georef_layer_init ();
55ddef4e 204 maps_layer_init ();
50a14534
EB
205 a_mapcache_init ();
206 a_background_init ();
a5c8699d 207
75b7457a 208 a_toolbar_init();
9f30939a
GB
209 vik_routing_prefs_init();
210
e530cc9f
RN
211 /*
212 * Second stage initialization
213 *
214 * Can now use a_preferences_get()
215 */
216 a_background_post_init ();
217 a_babel_post_init ();
218 modules_post_init ();
219
220 // May need to initialize the Positonal TimeZone lookup
74562734
RN
221 if ( a_vik_get_time_ref_frame() == VIK_TIME_REF_WORLD )
222 vu_setup_lat_lon_tz_lookup();
223
260703bf 224 /* Set the icon */
10f9bcb6 225 main_icon = gdk_pixbuf_from_pixdata(&viking_pixbuf, FALSE, NULL);
260703bf
GB
226 gtk_window_set_default_icon(main_icon);
227
940e85ee
RN
228 gdk_threads_enter ();
229
0d66c56c 230 // Ask for confirmation of default settings on first run
5ab2942c 231 vu_set_auto_features_on_first_run ();
0d66c56c 232
260703bf 233 /* Create the first window */
8a13dbd2 234 first_window = vik_window_new_window();
50a14534 235
5ab2942c 236 vu_check_latest_version ( GTK_WINDOW(first_window) );
91c46f90 237
50a14534
EB
238 while ( ++i < argc ) {
239 if ( strcmp(argv[i],"--") == 0 && !dashdash_already )
240 dashdash_already = TRUE; /* hack to open '-' */
d4a8b54d
RN
241 else {
242 VikWindow *newvw = first_window;
243 gboolean change_filename = (i == 1);
244
245 // Open any subsequent .vik files in their own window
246 if ( i > 1 && check_file_magic_vik ( argv[i] ) ) {
247 newvw = vik_window_new_window ();
248 change_filename = TRUE;
249 }
250
251 vik_window_open_file ( newvw, argv[i], change_filename );
252 }
50a14534
EB
253 }
254
8fa25c61
RN
255 vik_window_new_window_finish ( first_window );
256
c12d4347
RN
257 vu_command_line ( first_window, latitude, longitude, zoom_level_osm, map_id );
258
50a14534
EB
259 gtk_main ();
260 gdk_threads_leave ();
261
18ec873d 262 a_babel_uninit ();
75b7457a 263 a_toolbar_uninit ();
f5e80a61 264 a_background_uninit ();
50a14534 265 a_mapcache_uninit ();
ad0a8c2d 266 a_dems_uninit ();
a7023a1b 267 a_layer_defaults_uninit ();
17a1f8f9 268 a_preferences_uninit ();
66b23637 269 a_settings_uninit ();
50a14534 270
e3da2277
RN
271 modules_uninit();
272
3eba9bbf
RN
273 curl_download_uninit();
274
74562734
RN
275 vu_finalize_lat_lon_tz_lookup ();
276
44871dd1
RN
277 // Clean up any temporary files
278 util_remove_all_in_deletion_list ();
279
50a14534
EB
280 return 0;
281}