]> git.itanic.dy.fi Git - maemo-mapper/blob - src/main.c
df73b24b2684e1871edc086ad36d8fd04a9806bb
[maemo-mapper] / src / main.c
1 /*
2  * Copyright (C) 2006, 2007 John Costigan.
3  *
4  * POI and GPS-Info code originally written by Cezary Jackiewicz.
5  *
6  * Default map data provided by http://www.openstreetmap.org/
7  *
8  * This file is part of Maemo Mapper.
9  *
10  * Maemo Mapper is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * Maemo Mapper is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Maemo Mapper.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #define _GNU_SOURCE
25
26 #ifdef HAVE_CONFIG_H
27 #    include "config.h"
28 #endif
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33 #include <dbus/dbus-glib.h>
34 #include <locale.h>
35
36 #include <gconf/gconf-client.h>
37 #include <device_symbols.h>
38 #include <conicconnection.h>
39 #include <conicconnectionevent.h>
40
41 #ifndef LEGACY
42 #    include <hildon/hildon-program.h>
43 #    include <hildon/hildon-banner.h>
44 #else
45 #    include <hildon-widgets/hildon-program.h>
46 #    include <hildon-widgets/hildon-banner.h>
47 #endif
48
49 #include "types.h"
50 #include "data.h"
51 #include "defines.h"
52
53 #include "cmenu.h"
54 #include "dbus-ifc.h"
55 #include "display.h"
56 #include "gps.h"
57 #include "gpx.h"
58 #include "input.h"
59 #include "main.h"
60 #include "maps.h"
61 #include "menu.h"
62 #include "path.h"
63 #include "poi.h"
64 #include "settings.h"
65 #include "util.h"
66
67 static void osso_cb_hw_state(osso_hw_state_t *state, gpointer data);
68
69 static HildonProgram *_program = NULL;
70
71 static ConIcConnection *_conic_conn = NULL;
72 static gboolean _conic_is_connecting = FALSE;
73 static gboolean _conic_conn_failed = FALSE;
74 static GMutex *_conic_connection_mutex = NULL;
75 static GCond *_conic_connection_cond = NULL;
76
77 /* Dynamically-sized in-memory map cache. */
78 static size_t _map_cache_size = (32*1024*1024);
79 static gboolean _map_cache_enabled = TRUE;
80
81 static void
82 conic_conn_event(ConIcConnection *connection, ConIcConnectionEvent *event)
83 {
84     ConIcConnectionStatus status;
85     printf("%s()\n", __PRETTY_FUNCTION__);
86
87     g_mutex_lock(_conic_connection_mutex);
88
89     status = con_ic_connection_event_get_status(event);
90
91     if((_conic_is_connected = (status == CON_IC_STATUS_CONNECTED)))
92     {
93         /* We're connected. */
94         _conic_conn_failed = FALSE;
95         if(_download_banner != NULL)
96             gtk_widget_show(_download_banner);
97     }
98     else
99     {
100         /* We're not connected. */
101         /* Mark as a failed connection, if we had been trying to connect. */
102         _conic_conn_failed = _conic_is_connecting;
103         if(_download_banner != NULL)
104             gtk_widget_hide(_download_banner);
105     }
106
107     _conic_is_connecting = FALSE; /* No longer trying to connect. */
108     g_cond_broadcast(_conic_connection_cond);
109     g_mutex_unlock(_conic_connection_mutex);
110
111     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
112 }
113
114 void
115 conic_recommend_connected()
116 {
117     printf("%s()\n", __PRETTY_FUNCTION__);
118
119 #ifdef __arm__
120     g_mutex_lock(_conic_connection_mutex);
121     if(!_conic_is_connecting)
122     {
123         /* Fire up a connection request. */
124         con_ic_connection_connect(_conic_conn, CON_IC_CONNECT_FLAG_NONE);
125         _conic_is_connecting = TRUE;
126     }
127     g_mutex_unlock(_conic_connection_mutex);
128 #endif
129
130     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
131 }
132
133 gboolean
134 conic_ensure_connected()
135 {
136     printf("%s()\n", __PRETTY_FUNCTION__);
137
138 #ifdef __arm__
139     while(_window && !_conic_is_connected)
140     {   
141         g_mutex_lock(_conic_connection_mutex);
142         /* If we're not connected, and if we're not connecting, and if we're
143          * not in the wake of a connection failure, then try to connect. */
144         if(!_conic_is_connected && !_conic_is_connecting &&!_conic_conn_failed)
145         {
146             /* Fire up a connection request. */
147             con_ic_connection_connect(_conic_conn, CON_IC_CONNECT_FLAG_NONE);
148             _conic_is_connecting = TRUE;
149         }
150         g_cond_wait(_conic_connection_cond, _conic_connection_mutex);
151         g_mutex_unlock(_conic_connection_mutex);
152     }
153 #else
154     _conic_is_connected = TRUE;
155 #endif
156
157     vprintf("%s(): return %d\n", __PRETTY_FUNCTION__,
158             _window && _conic_is_connected);
159     return _window && _conic_is_connected;
160 }
161
162 /**
163  * Save state and destroy all non-UI elements created by this program in
164  * preparation for exiting.
165  */
166 static void
167 maemo_mapper_destroy()
168 {
169     printf("%s()\n", __PRETTY_FUNCTION__);
170
171     /* _program and widgets have already been destroyed. */
172     _window = NULL;
173
174     gps_destroy(FALSE);
175
176     path_destroy();
177
178     settings_save();
179
180     poi_destroy();
181
182     g_mutex_lock(_mut_priority_mutex);
183     _mut_priority_tree = g_tree_new((GCompareFunc)mut_priority_comparefunc);
184     g_mutex_unlock(_mut_priority_mutex);
185
186     /* Allow remaining downloads to finish. */
187     g_mutex_lock(_conic_connection_mutex);
188     g_cond_broadcast(_conic_connection_cond);
189     g_mutex_unlock(_conic_connection_mutex);
190     g_thread_pool_free(_mut_thread_pool, TRUE, TRUE);
191
192     if(_curr_repo->db)
193     {
194 #ifdef MAPDB_SQLITE
195         g_mutex_lock(_mapdb_mutex);
196         sqlite3_close(_curr_repo->db);
197         _curr_repo->db = NULL;
198         g_mutex_unlock(_mapdb_mutex);
199 #else
200         g_mutex_lock(_mapdb_mutex);
201         gdbm_close(_curr_repo->db);
202         _curr_repo->db = NULL;
203         g_mutex_unlock(_mapdb_mutex);
204 #endif
205     }
206     map_cache_destroy();
207
208     gps_destroy(TRUE);
209
210     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
211 }
212
213 /**
214  * Initialize everything required in preparation for calling gtk_main().
215  */
216 static void
217 maemo_mapper_init(gint argc, gchar **argv)
218 {
219     GtkWidget *hbox, *label, *vbox;
220     printf("%s()\n", __PRETTY_FUNCTION__);
221
222     /* Set enum-based constants. */
223     UNITS_ENUM_TEXT[UNITS_KM] = _("km");
224     UNITS_ENUM_TEXT[UNITS_MI] = _("mi.");
225     UNITS_ENUM_TEXT[UNITS_NM] = _("n.m.");
226
227     ROTATE_DIR_ENUM_TEXT[ROTATE_DIR_UP] = _("Up");
228     ROTATE_DIR_ENUM_TEXT[ROTATE_DIR_RIGHT] = _("Right");
229     ROTATE_DIR_ENUM_TEXT[ROTATE_DIR_DOWN] = _("Down");
230     ROTATE_DIR_ENUM_TEXT[ROTATE_DIR_LEFT] = _("Left");
231
232     UNBLANK_ENUM_TEXT[UNBLANK_WITH_GPS] = _("When Receiving Any GPS Data");
233     UNBLANK_ENUM_TEXT[UNBLANK_WHEN_MOVING] = _("When Moving");
234     UNBLANK_ENUM_TEXT[UNBLANK_FULLSCREEN] = _("When Moving (Full Screen Only)");
235     UNBLANK_ENUM_TEXT[UNBLANK_WAYPOINT] = _("When Approaching a Waypoint");
236     UNBLANK_ENUM_TEXT[UNBLANK_NEVER] = _("Never");
237
238     INFO_FONT_ENUM_TEXT[INFO_FONT_XXSMALL] = "xx-small";
239     INFO_FONT_ENUM_TEXT[INFO_FONT_XSMALL] = "x-small";
240     INFO_FONT_ENUM_TEXT[INFO_FONT_SMALL] = "small";
241     INFO_FONT_ENUM_TEXT[INFO_FONT_MEDIUM] = "medium";
242     INFO_FONT_ENUM_TEXT[INFO_FONT_LARGE] = "large";
243     INFO_FONT_ENUM_TEXT[INFO_FONT_XLARGE] = "x-large";
244     INFO_FONT_ENUM_TEXT[INFO_FONT_XXLARGE] = "xx-large";
245
246     CUSTOM_KEY_ICON[CUSTOM_KEY_UP] = HWK_BUTTON_UP;
247     CUSTOM_KEY_ICON[CUSTOM_KEY_LEFT] = HWK_BUTTON_LEFT;
248     CUSTOM_KEY_ICON[CUSTOM_KEY_DOWN] = HWK_BUTTON_DOWN;
249     CUSTOM_KEY_ICON[CUSTOM_KEY_RIGHT] = HWK_BUTTON_RIGHT;
250     CUSTOM_KEY_ICON[CUSTOM_KEY_SELECT] = HWK_BUTTON_SELECT;
251     CUSTOM_KEY_ICON[CUSTOM_KEY_INCREASE] = HWK_BUTTON_INCREASE;
252     CUSTOM_KEY_ICON[CUSTOM_KEY_DECREASE] = HWK_BUTTON_DECREASE;
253     CUSTOM_KEY_ICON[CUSTOM_KEY_FULLSCREEN] = HWK_BUTTON_VIEW;
254     CUSTOM_KEY_ICON[CUSTOM_KEY_ESC] = HWK_BUTTON_CANCEL;
255
256     CUSTOM_KEY_DEFAULT[CUSTOM_KEY_UP] = CUSTOM_ACTION_RESET_VIEW_ANGLE;
257     CUSTOM_KEY_DEFAULT[CUSTOM_KEY_LEFT] =CUSTOM_ACTION_ROTATE_COUNTERCLOCKWISE;
258     CUSTOM_KEY_DEFAULT[CUSTOM_KEY_DOWN] = CUSTOM_ACTION_TOGGLE_AUTOROTATE;
259     CUSTOM_KEY_DEFAULT[CUSTOM_KEY_RIGHT] = CUSTOM_ACTION_ROTATE_CLOCKWISE;
260     CUSTOM_KEY_DEFAULT[CUSTOM_KEY_SELECT] = CUSTOM_ACTION_TOGGLE_AUTOCENTER;
261     CUSTOM_KEY_DEFAULT[CUSTOM_KEY_INCREASE] = CUSTOM_ACTION_ZOOM_IN;
262     CUSTOM_KEY_DEFAULT[CUSTOM_KEY_DECREASE] = CUSTOM_ACTION_ZOOM_OUT;
263     CUSTOM_KEY_DEFAULT[CUSTOM_KEY_FULLSCREEN]= CUSTOM_ACTION_TOGGLE_FULLSCREEN;
264     CUSTOM_KEY_DEFAULT[CUSTOM_KEY_ESC] = CUSTOM_ACTION_TOGGLE_TRACKS;
265
266     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_PAN_NORTH] = _("Pan North");
267     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_PAN_WEST] = _("Pan West");
268     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_PAN_SOUTH] = _("Pan South");
269     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_PAN_EAST] = _("Pan East");
270     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_PAN_UP] = _("Pan Up");
271     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_PAN_DOWN] = _("Pan Down");
272     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_PAN_LEFT] = _("Pan Left");
273     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_PAN_RIGHT] = _("Pan Right");
274     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_RESET_VIEW_ANGLE]
275         = _("Reset Viewing Angle");
276     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_ROTATE_CLOCKWISE]
277         = _("Rotate View Clockwise");
278     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_ROTATE_COUNTERCLOCKWISE]
279         = _("Rotate View Counter-Clockwise");
280     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TOGGLE_AUTOCENTER]
281         = _("Toggle Auto-Center");
282     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TOGGLE_AUTOROTATE]
283         = _("Toggle Auto-Rotate");
284     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TOGGLE_FULLSCREEN]
285         = _("Toggle Fullscreen");
286     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_ZOOM_IN] = _("Zoom In");
287     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_ZOOM_OUT] = _("Zoom Out");
288     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TOGGLE_TRACKS] = _("Toggle Tracks");
289     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TOGGLE_SCALE] = _("Toggle Scale");
290     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TOGGLE_POI] = _("Toggle POIs");
291     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_CHANGE_REPO]
292         = _("Select Next Repository");
293     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_ROUTE_DISTNEXT]
294         = _("Show Distance to Next Waypoint");
295     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_ROUTE_DISTLAST]
296         = _("Show Distance to End of Route");
297     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TRACK_BREAK]=_("Insert Track Break");
298     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TRACK_CLEAR] = _("Clear Track");
299     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TRACK_DISTLAST]
300         = _("Show Distance from Last Break");
301     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TRACK_DISTFIRST]
302         = _("Show Distance from Beginning");
303     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TOGGLE_GPS] = _("Toggle GPS");
304     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TOGGLE_GPSINFO]=_("Toggle GPS Info");
305     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_TOGGLE_SPEEDLIMIT]
306         = _("Toggle Speed Limit");
307     CUSTOM_ACTION_ENUM_TEXT[CUSTOM_ACTION_RESET_BLUETOOTH]
308         = _("Reset Bluetooth");
309
310     DEG_FORMAT_ENUM_TEXT[DDPDDDDD] = "-dd.ddddd°";
311     DEG_FORMAT_ENUM_TEXT[DD_MMPMMM] = "-dd°mm.mmm'";
312     DEG_FORMAT_ENUM_TEXT[DD_MM_SSPS] = "-dd°mm'ss.s\"";
313     DEG_FORMAT_ENUM_TEXT[DDPDDDDD_NSEW] = "dd.ddddd° S";
314     DEG_FORMAT_ENUM_TEXT[DD_MMPMMM_NSEW] = "dd°mm.mmm' S";
315     DEG_FORMAT_ENUM_TEXT[DD_MM_SSPS_NSEW] = "dd°mm'ss.s\" S";
316     DEG_FORMAT_ENUM_TEXT[NSEW_DDPDDDDD] = "S dd.ddddd°";
317     DEG_FORMAT_ENUM_TEXT[NSEW_DD_MMPMMM] = "S dd° mm.mmm'";
318     DEG_FORMAT_ENUM_TEXT[NSEW_DD_MM_SSPS] = "S dd° mm' ss.s\"";
319
320     SPEED_LOCATION_ENUM_TEXT[SPEED_LOCATION_TOP_LEFT] = _("Top-Left");
321     SPEED_LOCATION_ENUM_TEXT[SPEED_LOCATION_TOP_RIGHT] = _("Top-Right");
322     SPEED_LOCATION_ENUM_TEXT[SPEED_LOCATION_BOTTOM_RIGHT] = _("Bottom-Right");
323     SPEED_LOCATION_ENUM_TEXT[SPEED_LOCATION_BOTTOM_LEFT] = _("Bottom-Left");
324
325     GPS_RCVR_ENUM_TEXT[GPS_RCVR_BT] = _("Bluetooth");
326     GPS_RCVR_ENUM_TEXT[GPS_RCVR_GPSD] = _("GPSD");
327     GPS_RCVR_ENUM_TEXT[GPS_RCVR_FILE] = _("File");
328
329     /* Set up track array (must be done before config). */
330     memset(&_track, 0, sizeof(_track));
331     memset(&_route, 0, sizeof(_route));
332     MACRO_PATH_INIT(_track);
333     MACRO_PATH_INIT(_route);
334
335     _mapdb_mutex = g_mutex_new();
336     _mut_priority_mutex = g_mutex_new();
337     _mouse_mutex = g_mutex_new();
338
339     _conic_connection_mutex = g_mutex_new();
340     _conic_connection_cond = g_cond_new();
341
342     settings_init();
343     map_cache_init(_map_cache_size);
344
345     /* Initialize _program. */
346     _program = HILDON_PROGRAM(hildon_program_get_instance());
347     g_set_application_name("Maemo Mapper");
348
349     /* Initialize _window. */
350     _window = GTK_WIDGET(hildon_window_new());
351     hildon_program_add_window(_program, HILDON_WINDOW(_window));
352
353     /* Lets go fullscreen if so requested in saved config */
354     if (_fullscreen) {
355       gtk_window_fullscreen(GTK_WINDOW(_window));
356     }
357
358     /* Create and add widgets and supporting data. */
359     hbox = gtk_hbox_new(FALSE, 0);
360     gtk_container_add(GTK_CONTAINER(_window), hbox);
361
362     _gps_widget = gtk_frame_new("GPS Info");
363     gtk_container_add(GTK_CONTAINER(_gps_widget),
364             vbox = gtk_vbox_new(FALSE, 0));
365     gtk_widget_set_size_request(GTK_WIDGET(_gps_widget), 180, 0);
366     gtk_box_pack_start(GTK_BOX(hbox), _gps_widget, FALSE, TRUE, 0);
367
368     label = gtk_label_new(" ");
369     gtk_widget_set_size_request(GTK_WIDGET(label), -1, 10);
370     gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 0);
371
372     _text_lat = gtk_label_new(" --- ");
373     gtk_widget_set_size_request(GTK_WIDGET(_text_lat), -1, 30);
374     gtk_box_pack_start(GTK_BOX(vbox), _text_lat, FALSE, TRUE, 0);
375
376     _text_lon = gtk_label_new(" --- ");
377     gtk_widget_set_size_request(GTK_WIDGET(_text_lon), -1, 30);
378     gtk_box_pack_start(GTK_BOX(vbox), _text_lon, FALSE, TRUE, 0);
379
380     _text_speed = gtk_label_new(" --- ");
381     gtk_widget_set_size_request(GTK_WIDGET(_text_speed), -1, 30);
382     gtk_box_pack_start(GTK_BOX(vbox), _text_speed, FALSE, TRUE, 0);
383
384     _text_alt = gtk_label_new(" --- ");
385     gtk_widget_set_size_request(GTK_WIDGET(_text_alt), -1, 30);
386     gtk_box_pack_start(GTK_BOX(vbox), _text_alt, FALSE, TRUE, 0);
387
388     label = gtk_label_new(" ");
389     gtk_widget_set_size_request(GTK_WIDGET(label), -1, 10);
390     gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 0);
391
392     _sat_panel = gtk_drawing_area_new ();
393     gtk_widget_set_size_request (_sat_panel, -1, 100);
394     gtk_box_pack_start(GTK_BOX(vbox), _sat_panel, TRUE, TRUE, 0);
395
396     label = gtk_label_new(" ");
397     gtk_widget_set_size_request(GTK_WIDGET(label), -1, 10);
398     gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 0);
399
400     _text_time = gtk_label_new("--:--:--");
401     gtk_widget_set_size_request(GTK_WIDGET(_text_time), -1, 30);
402     gtk_box_pack_start(GTK_BOX(vbox), _text_time, FALSE, TRUE, 0);
403
404     _heading_panel = gtk_drawing_area_new ();
405     gtk_widget_set_size_request (_heading_panel, -1, 100);
406     gtk_box_pack_start(GTK_BOX(vbox), _heading_panel, TRUE, TRUE, 0);
407
408     _map_widget = gtk_drawing_area_new();
409
410     gtk_box_pack_start(GTK_BOX(hbox), _map_widget, TRUE, TRUE, 0);
411
412     gtk_widget_show_all(hbox);
413     gps_show_info(); /* hides info, if necessary. */
414
415     gtk_widget_realize(_map_widget);
416
417     /* Tweak the foreground and background colors a little bit... */
418     {
419         GdkColor color;
420         GdkGCValues values;
421         GdkColormap *colormap = gtk_widget_get_colormap(_map_widget);
422
423         gdk_gc_get_values(
424                 _map_widget->style->fg_gc[GTK_STATE_NORMAL],
425                 &values);
426         gdk_colormap_query_color(colormap, values.foreground.pixel, &color);
427         gtk_widget_modify_fg(_map_widget, GTK_STATE_ACTIVE, &color);
428
429         gdk_gc_get_values(
430                 _map_widget->style->bg_gc[GTK_STATE_NORMAL],
431                 &values);
432         gdk_colormap_query_color(colormap, values.foreground.pixel, &color);
433         gtk_widget_modify_bg(_map_widget, GTK_STATE_ACTIVE, &color);
434
435         /* Use a black background for _map_widget, since missing tiles are
436          * also drawn with a black background. */
437         color.red = 0; color.green = 0; color.blue = 0;
438         gtk_widget_modify_bg(_map_widget,
439                 GTK_STATE_NORMAL, &color);
440     }
441
442     _map_pixmap = gdk_pixmap_new(_map_widget->window, 1, 1, -1);
443     /* -1: use bit depth of widget->window. */
444
445     _map_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);
446
447     _mut_exists_table = g_hash_table_new(
448             (GHashFunc)mut_exists_hashfunc, (GEqualFunc)mut_exists_equalfunc);
449     _mut_priority_tree = g_tree_new((GCompareFunc)mut_priority_comparefunc);
450
451     _mut_thread_pool = g_thread_pool_new(
452             (GFunc)thread_proc_mut, NULL, NUM_DOWNLOAD_THREADS, FALSE, NULL);
453     _mrt_thread_pool = g_thread_pool_new(
454             (GFunc)thread_render_map, NULL, 1, FALSE, NULL);
455
456     /* Connect signals. */
457     g_signal_connect(G_OBJECT(_window), "destroy",
458             G_CALLBACK(gtk_main_quit), NULL);
459
460     memset(&_autoroute_data, 0, sizeof(_autoroute_data));
461
462     latlon2unit(_gps.lat, _gps.lon, _pos.unitx, _pos.unity);
463
464     /* Initialize our line styles. */
465     update_gcs();
466
467     menu_init();
468     cmenu_init();
469     path_init();
470     gps_init();
471     input_init();
472     poi_db_connect();
473     display_init();
474     dbus_ifc_init();
475
476     /* If present, attempt to load the file specified on the command line. */
477     if(argc > 1)
478     {
479         GnomeVFSResult vfs_result;
480         gint size;
481         gchar *buffer;
482         gchar *file_uri;
483
484         /* Get the selected filename. */
485         file_uri = gnome_vfs_make_uri_from_shell_arg(argv[1]);
486
487         if(GNOME_VFS_OK != (vfs_result = gnome_vfs_read_entire_file(
488                         file_uri, &size, &buffer)))
489         {
490             gchar buffer[BUFFER_SIZE];
491             snprintf(buffer, sizeof(buffer),
492                     "%s:\n%s", _("Failed to open file for reading"),
493                     gnome_vfs_result_to_string(vfs_result));
494             popup_error(_window, buffer);
495         }
496         else
497         {
498             if(gpx_path_parse(&_route, buffer, size, 0))
499             {
500                 path_save_route_to_db();
501                 MACRO_BANNER_SHOW_INFO(_window, _("Route Opened"));
502             }
503             else
504                 popup_error(_window, _("Error parsing GPX file."));
505             g_free(buffer);
506         }
507         g_free(file_uri);
508     }
509
510     /* If we have a route, calculate the next point. */
511     route_find_nearest_point();
512
513     _conic_conn = con_ic_connection_new();
514     g_object_set(_conic_conn, "automatic-connection-events", TRUE, NULL);
515     g_signal_connect(G_OBJECT(_conic_conn), "connection-event",
516             G_CALLBACK(conic_conn_event), NULL);
517
518     g_idle_add((GSourceFunc)window_present, NULL);
519
520
521     osso_hw_set_event_cb(_osso, NULL, osso_cb_hw_state, NULL);
522
523     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
524 }
525
526 static gboolean
527 osso_cb_hw_state_idle(osso_hw_state_t *state)
528 {
529     static gboolean _must_save_data = FALSE;
530     printf("%s(inact=%d, save=%d, shut=%d, memlow=%d, state=%d)\n",
531             __PRETTY_FUNCTION__, state->system_inactivity_ind,
532             state->save_unsaved_data_ind, state->shutdown_ind,
533             state->memory_low_ind, state->sig_device_mode_ind);
534
535     if(state->shutdown_ind)
536     {
537         maemo_mapper_destroy();
538         exit(1);
539     }
540
541     if(state->save_unsaved_data_ind)
542     {
543         settings_save();
544         _must_save_data = TRUE;
545     }
546
547     if(state->memory_low_ind)
548     {
549         // Disable the map cache and set the next max cache size to
550         // slightly less than the current cache size.
551         _map_cache_size = map_cache_resize(0) * 0.8;
552         _map_cache_enabled = FALSE;
553     }
554     else
555     {
556         if(!_map_cache_enabled)
557         {
558             // Restore the map cache.
559             map_cache_resize(_map_cache_size);
560             _map_cache_enabled = TRUE;
561         }
562     }
563
564     g_free(state);
565
566     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
567     return FALSE;
568 }
569
570 static void
571 osso_cb_hw_state(osso_hw_state_t *state, gpointer data)
572 {
573     printf("%s()\n", __PRETTY_FUNCTION__);
574     osso_hw_state_t *state_copy = g_new(osso_hw_state_t, 1);
575     memcpy(state_copy, state, sizeof(osso_hw_state_t));
576     g_idle_add((GSourceFunc)osso_cb_hw_state_idle, state_copy);
577     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
578 }
579
580 gint
581 main(gint argc, gchar *argv[])
582 {
583     printf("%s()\n", __PRETTY_FUNCTION__);
584
585     /* Initialize localization. */
586     setlocale(LC_ALL, "");
587     bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
588     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
589     textdomain(GETTEXT_PACKAGE);
590
591     g_thread_init(NULL);
592
593     /* Initialize _osso. */
594     _osso = osso_initialize("com.gnuite.maemo_mapper", VERSION, TRUE, NULL);
595     if(!_osso)
596     {
597         g_printerr("osso_initialize failed.\n");
598         return 1;
599     }
600
601     gtk_init(&argc, &argv);
602
603     /* Init gconf. */
604     g_type_init();
605     gconf_init(argc, argv, NULL);
606
607     /* Init Gnome-VFS. */
608     gnome_vfs_init();
609
610 #ifdef DEBUG
611     /* This is just some helpful DBUS testing code. */
612     if(argc >= 3)
613     {
614         /* Try to set the center to a new lat/lon. */
615         GError *error = NULL;
616         gchar *error_check;
617         gdouble lat, lon;
618         DBusGConnection *bus;
619         DBusGProxy *proxy;
620         
621         bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
622         if(!bus || error)
623         {
624             g_printerr("Error: %s\n", error->message);
625             return -1;
626         }
627
628         proxy = dbus_g_proxy_new_for_name(bus,
629                         MM_DBUS_SERVICE, MM_DBUS_PATH, MM_DBUS_INTERFACE);
630
631         lat = g_ascii_strtod((argv[1]), &error_check);
632         if(error_check == argv[1])
633         {
634             g_printerr("Failed to parse string as float: %s\n", argv[1]);
635             return 1;
636         }
637
638         lon = g_ascii_strtod((argv[2]), &error_check);
639         if(error_check == argv[2])
640         {
641             g_printerr("Failed to parse string as float: %s\n", argv[2]);
642             return 2;
643         }
644
645         error = NULL;
646         if(argc >= 4)
647         {
648             /* We are specifying a zoom. */
649             gint zoom;
650
651             zoom = g_ascii_strtod((argv[3]), &error_check);
652             if(error_check == argv[3])
653             {
654                 g_printerr("Failed to parse string as integer: %s\n", argv[3]);
655                 return 3;
656             }
657             if(!dbus_g_proxy_call(proxy, MM_DBUS_METHOD_SET_VIEW_POSITION,
658                         &error,
659                         G_TYPE_DOUBLE, lat, G_TYPE_DOUBLE, lon,
660                         G_TYPE_INT, zoom, G_TYPE_INVALID,
661                         G_TYPE_INVALID)
662                     || error)
663             {
664                 g_printerr("Error: %s\n", error->message);
665                 return 4;
666             }
667         }
668         else
669         {
670             /* Not specifying a zoom. */
671             if(!dbus_g_proxy_call(proxy, MM_DBUS_METHOD_SET_VIEW_POSITION,
672                         &error,
673                         G_TYPE_DOUBLE, lat, G_TYPE_DOUBLE, lon, G_TYPE_INVALID,
674                         G_TYPE_INVALID)
675                     || error)
676             {
677                 g_printerr("Error: %s\n", error->message);
678                 return -2;
679             }
680         }
681
682         g_object_unref(proxy);
683         dbus_g_connection_unref(bus);
684
685         return 0;
686     }
687 #endif
688
689     maemo_mapper_init(argc, argv);
690
691     gtk_main();
692
693     maemo_mapper_destroy();
694
695     osso_deinitialize(_osso);
696
697     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
698     exit(0);
699 }
700