]> err.no Git - mapper/blob - src/mapper.c
Remodelling of GPS handling for hildon. Work in progress still.
[mapper] / src / mapper.c
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2007 Kaj-Michael Lang
5  * Copyright (C) 2006-2007 John Costigan.
6  *
7  * POI and GPS-Info code originally written by Cezary Jackiewicz.
8  *
9  * Default map data provided by http://www.openstreetmap.org/
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <config.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <stddef.h>
32 #include <math.h>
33 #include <errno.h>
34 #include <sys/wait.h>
35 #include <glib/gstdio.h>
36 #include <glib/gi18n.h>
37 #ifdef WITH_GST
38 #include <gst/gst.h>
39 #endif
40 #include <gtk/gtk.h>
41 #include <fcntl.h>
42 #include <gdk/gdkkeysyms.h>
43 #include <dbus/dbus.h>
44 #include <dbus/dbus-glib.h>
45 #include <libgnomevfs/gnome-vfs.h>
46 #include <curl/multi.h>
47 #include <gconf/gconf-client.h>
48 #include <libintl.h>
49 #include <locale.h>
50
51 #ifdef WITH_HILDON_DBUS_BT
52 #include <bt-dbus.h>
53 #endif
54
55 #include "mapper.h"
56 #include "hildon-mapper.h"
57 #include "utils.h"
58 #include "mapper-types.h"
59 #include "settings.h"
60 #include "gps.h"
61 #include "map.h"
62 #include "route.h"
63 #include "track.h"
64 #include "ui-common.h"
65 #include "db.h"
66 #include "osm-db.h"
67 #include "poi.h"
68 #include "cb.h"
69 #include "speak.h"
70 #include "gpx.h"
71 #include "config-gconf.h"
72
73 gfloat UNITS_CONVERT[] = {1.85200,1.15077945,1.f,};
74
75 GdkColor COLORABLE_DEFAULT[COLORABLE_ENUM_COUNT] = {
76         {0, 0x0000, 0x0000, 0xc000}
77         ,                       /* COLORABLE_MARK */
78         {0, 0x6000, 0x6000, 0xf800}
79         ,                       /* COLORABLE_MARK_VELOCITY */
80         {0, 0x8000, 0x8000, 0x8000}
81         ,                       /* COLORABLE_MARK_OLD */
82         {0, 0xe000, 0x0000, 0x0000}
83         ,                       /* COLORABLE_TRACK */
84         {0, 0xa000, 0x0000, 0x0000}
85         ,                       /* COLORABLE_TRACK_MARK */
86         {0, 0x7000, 0x0000, 0x0000}
87         ,                       /* COLORABLE_TRACK_BREAK */
88         {0, 0x0000, 0xa000, 0x0000}
89         ,                       /* COLORABLE_ROUTE */
90         {0, 0x0000, 0x8000, 0x0000}
91         ,                       /* COLORABLE_ROUTE_WAY */
92         {0, 0x0000, 0x6000, 0x0000}
93         ,                       /* COLORABLE_ROUTE_BREAK */
94         {0, 0xa000, 0x0000, 0xa000}     /* COLORABLE_POI */
95 };
96
97 enum {
98         MAPPER_INIT_START=0,
99         MAPPER_INIT_MISC,
100         MAPPER_INIT_CONFIG,
101         MAPPER_INIT_DB,
102         MAPPER_INIT_POI,
103         MAPPER_INIT_OSM,
104         MAPPER_INIT_VOICE,
105         MAPPER_INIT_UI,
106         MAPPER_INIT_GOTO,
107         MAPPER_INIT_DONE
108 } mapper_init_state;
109
110 guint mis=MAPPER_INIT_START;
111
112 static GtkWidget *init_progress;
113 static GtkWidget *init_dialog;
114
115 /**
116  * Initialize arrays
117  */
118 static void 
119 variables_init(void)
120 {
121 UNITS_TEXT[UNITS_KM] = _("km");
122 UNITS_TEXT[UNITS_MI] = _("mi.");
123 UNITS_TEXT[UNITS_NM] = _("n.m.");
124
125 INFO_FONT_TEXT[INFO_FONT_XXSMALL] = "xx-small";
126 INFO_FONT_TEXT[INFO_FONT_XSMALL] = "x-small";
127 INFO_FONT_TEXT[INFO_FONT_SMALL] = "small";
128 INFO_FONT_TEXT[INFO_FONT_MEDIUM] = "medium";
129 INFO_FONT_TEXT[INFO_FONT_LARGE] = "large";
130 INFO_FONT_TEXT[INFO_FONT_XLARGE] = "x-large";
131 INFO_FONT_TEXT[INFO_FONT_XXLARGE] = "xx-large";
132
133 CUSTOM_KEY_GCONF[CUSTOM_KEY_UP] = GCONF_KEY_PREFIX "/key_up";
134 CUSTOM_KEY_GCONF[CUSTOM_KEY_DOWN] = GCONF_KEY_PREFIX "/key_down";
135 CUSTOM_KEY_GCONF[CUSTOM_KEY_LEFT] = GCONF_KEY_PREFIX "/key_left";
136 CUSTOM_KEY_GCONF[CUSTOM_KEY_RIGHT] = GCONF_KEY_PREFIX "/key_right";
137 CUSTOM_KEY_GCONF[CUSTOM_KEY_SELECT] = GCONF_KEY_PREFIX "/key_select";
138 CUSTOM_KEY_GCONF[CUSTOM_KEY_INCREASE] = GCONF_KEY_PREFIX "/key_increase";
139 CUSTOM_KEY_GCONF[CUSTOM_KEY_DECREASE] = GCONF_KEY_PREFIX "/key_decrease";
140 CUSTOM_KEY_GCONF[CUSTOM_KEY_FULLSCREEN] = GCONF_KEY_PREFIX "/key_fullscreen";
141 CUSTOM_KEY_GCONF[CUSTOM_KEY_ESC] = GCONF_KEY_PREFIX "/key_esc";
142
143 CUSTOM_KEY_ICON[CUSTOM_KEY_UP] = HWK_BUTTON_UP;
144 CUSTOM_KEY_ICON[CUSTOM_KEY_LEFT] = HWK_BUTTON_LEFT;
145 CUSTOM_KEY_ICON[CUSTOM_KEY_DOWN] = HWK_BUTTON_DOWN;
146 CUSTOM_KEY_ICON[CUSTOM_KEY_RIGHT] = HWK_BUTTON_RIGHT;
147 CUSTOM_KEY_ICON[CUSTOM_KEY_SELECT] = HWK_BUTTON_SELECT;
148 CUSTOM_KEY_ICON[CUSTOM_KEY_INCREASE] = HWK_BUTTON_INCREASE;
149 CUSTOM_KEY_ICON[CUSTOM_KEY_DECREASE] = HWK_BUTTON_DECREASE;
150 CUSTOM_KEY_ICON[CUSTOM_KEY_FULLSCREEN] = HWK_BUTTON_VIEW;
151 CUSTOM_KEY_ICON[CUSTOM_KEY_ESC] = HWK_BUTTON_CANCEL;
152
153 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_UP] = CUSTOM_ACTION_PAN_NORTH;
154 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_LEFT] = CUSTOM_ACTION_PAN_WEST;
155 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_DOWN] = CUSTOM_ACTION_PAN_SOUTH;
156 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_RIGHT] = CUSTOM_ACTION_PAN_EAST;
157 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_SELECT] = CUSTOM_ACTION_TOGGLE_AUTOCENTER;
158 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_INCREASE] = CUSTOM_ACTION_ZOOM_IN;
159 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_DECREASE] = CUSTOM_ACTION_ZOOM_OUT;
160 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_FULLSCREEN] = CUSTOM_ACTION_TOGGLE_FULLSCREEN;
161 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_ESC] = CUSTOM_ACTION_TOGGLE_TRACKS;
162
163 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_NORTH] = _("Pan North");
164 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_WEST] = _("Pan West");
165 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_SOUTH] = _("Pan South");
166 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_EAST] = _("Pan East");
167 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_AUTOCENTER] = _("Toggle Auto-Center");
168 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_FULLSCREEN] = _("Toggle Fullscreen");
169 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ZOOM_IN] = _("Zoom In");
170 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ZOOM_OUT] = _("Zoom Out");
171 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_TRACKS] = _("Toggle Tracks");
172 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_SCALE] = _("Toggle Scale");
173 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_POI] = _("Toggle POIs");
174 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_CHANGE_REPO] = _("Select Next Repository");
175 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ROUTE_DISTNEXT] = _("Show Distance to Next Waypoint");
176 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ROUTE_DISTLAST] = _("Show Distance to End of Route");
177 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TRACK_BREAK] = _("Insert Track Break");
178 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TRACK_DISTLAST] = _("Show Distance from Last Break");
179 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TRACK_DISTFIRST] = _("Show Distance from Beginning");
180 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_GPS] = _("Toggle GPS");
181 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_GPSINFO] = _("Toggle GPS Info");
182 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_SPEEDLIMIT] = _("Toggle Speed Limit");
183
184 COLORABLE_GCONF[COLORABLE_MARK] = GCONF_KEY_PREFIX "/color_mark";
185 COLORABLE_GCONF[COLORABLE_MARK_VELOCITY] = GCONF_KEY_PREFIX "/color_mark_velocity";
186 COLORABLE_GCONF[COLORABLE_MARK_OLD] = GCONF_KEY_PREFIX "/color_mark_old";
187 COLORABLE_GCONF[COLORABLE_TRACK] = GCONF_KEY_PREFIX "/color_track";
188 COLORABLE_GCONF[COLORABLE_TRACK_MARK] = GCONF_KEY_PREFIX "/color_track_mark";
189 COLORABLE_GCONF[COLORABLE_TRACK_BREAK] = GCONF_KEY_PREFIX "/color_track_break";
190 COLORABLE_GCONF[COLORABLE_ROUTE] = GCONF_KEY_PREFIX "/color_route";
191 COLORABLE_GCONF[COLORABLE_ROUTE_WAY] = GCONF_KEY_PREFIX "/color_route_way";
192 COLORABLE_GCONF[COLORABLE_ROUTE_BREAK] = GCONF_KEY_PREFIX "/color_route_break";
193 COLORABLE_GCONF[COLORABLE_POI] = GCONF_KEY_PREFIX "/color_poi";
194 }
195
196 /**
197  * Save state and destroy all non-UI elements created by this program in
198  * preparation for exiting.
199  */
200 static void 
201 mapper_destroy(void)
202 {
203 config_save();
204 config_save_repo();
205 map_download_deinit();
206 map_poi_deinit();
207 #if defined (WITH_GST) && defined (WITH_ESPEAK)
208 speak_deinit();
209 #endif
210 gps_disconnect(_gps);
211 gps_free(_gps);
212 osm_deinit();
213 db_close(&_db);
214 track_deinit();
215 route_deinit();
216
217 gnome_vfs_shutdown();
218 #ifdef WITH_OSSO
219 osso_deinitialize(_osso);
220 #endif
221 curl_global_cleanup();
222 }
223
224 static gint 
225 mapper_osso_init(void)
226 {
227 #ifdef WITH_OSSO
228 /* Initialize _osso. */
229 _osso = osso_initialize("org.tal.mapper", VERSION, TRUE, NULL);
230 if (!_osso) {
231         g_printerr("osso_initialize failed.\n");
232         return 1;
233 }
234 #endif
235 return 0;
236 }
237
238 static gint 
239 mapper_osso_cb_init(void)
240 {
241 gchar *filter_string;
242
243 #ifdef WITH_OSSO
244 if (OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_cb_default, NULL)) {
245         g_printerr("osso_rpc_set_default_cb_f failed.\n");
246         return 1;
247 }
248 #endif
249
250 #ifdef WITH_OSSO_IC
251 filter_string = g_strdup_printf("interface=%s", ICD_DBUS_INTERFACE);
252 /* add match */
253 dbus_bus_add_match(dbus_conn, filter_string, NULL);
254 g_free(filter_string);
255
256 /* add the callback */
257 dbus_connection_add_filter(dbus_conn, get_connection_status_signal_cb, NULL, NULL);
258 osso_iap_cb(iap_callback);
259 #endif
260
261 return 0;
262 }
263
264 static void 
265 timezone_init(void)
266 {
267 time_t time1;
268 struct tm time2;
269
270 time1 = time(NULL);
271 localtime_r(&time1, &time2);
272 _gmtoffset = time2.tm_gmtoff;
273 }
274
275 static gboolean
276 mapper_init(gpointer data)
277 {
278 GError *error = NULL;
279 gboolean ret=TRUE;
280 gfloat p=0;
281 gchar *w="Starting";
282
283 switch (mis) {
284         case MAPPER_INIT_START:
285                 curl_global_init(CURL_GLOBAL_NOTHING);
286                 gnome_vfs_init();
287                 /* XXX: Load GPS configuration, then create gps */
288                 _gps=gps_new(GPS_IO_SIMULATION);
289                 _gps->connection_retry=gps_retry_connection;
290                 timezone_init();
291                 gpx_init();
292                 variables_init();
293                 latlon_init();
294                 track_init();
295                 route_init();
296                 mis=MAPPER_INIT_CONFIG;
297                 p=0.1;
298                 w="Init";
299         break;
300         case MAPPER_INIT_CONFIG:
301                 config_init();
302                 map_download_init();
303                 mis=MAPPER_INIT_DB;
304                 p=0.2;
305                 w="Config";
306         break;
307         case MAPPER_INIT_DB:
308                 if (db_connect(&_db, _mapper_db)) {
309                         mis=MAPPER_INIT_POI;
310                 } else {
311                         mis=MAPPER_INIT_VOICE;
312                 }
313                 p=0.5;
314                 w="Database";
315         break;
316         case MAPPER_INIT_POI:
317                 poi_init(&_db);
318                 mis=MAPPER_INIT_OSM;
319                 p=0.6;
320                 w="POIs";
321         break;
322         case MAPPER_INIT_OSM:
323                 osm_init(&_db);
324                 mis=MAPPER_INIT_VOICE;
325                 p=0.8;
326                 w="OSM";
327         break;
328         case MAPPER_INIT_VOICE:
329                 #if defined (WITH_GST) && defined (WITH_ESPEAK)
330                 if (speak_init("en",_voice_speed,_voice_pitch)==FALSE) {
331                         g_printerr("Espeak init failed\n");
332                         popup_error(_window, "Speech init failed. Disabled.");
333                 }
334                 #endif
335                 mis=MAPPER_INIT_MISC;
336                 p=0.85;
337                 w="Speech";
338         break;
339         case MAPPER_INIT_MISC:
340                 /* Initialize D-Bus system connection. */
341                 if (NULL == (dbus_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error))) {
342                         g_printerr("Failed to open connection to D-Bus: %s.\n", error->message);
343                         popup_error(_window, "Failed to connect to D-Bus.");
344                         error = NULL;
345                 }
346
347                 /* XXX: Move this */
348                 #ifdef WITH_OSSO
349                 osso_hw_set_event_cb(_osso, NULL, osso_cb_hw_state, NULL);
350                 #endif
351
352                 mis=MAPPER_INIT_UI;
353                 p=0.9;
354                 w="Misc";
355         break;
356         case MAPPER_INIT_UI:
357                 mapper_ui_init();
358                 mis=MAPPER_INIT_GOTO;
359                 p=0.99;
360                 w="UI";
361         break;
362         case MAPPER_INIT_GOTO:
363                 if (map_goto_position(&_home)==FALSE)
364                         g_printerr("Home is not set.\n");
365                 else
366                         map_center_unit(_center.unitx, _center.unity);
367                 mis=MAPPER_INIT_DONE;
368                 p=1.0;
369                 w="Done";
370         break;
371         case MAPPER_INIT_DONE:
372                 progress_dialog_remove(init_dialog);
373                 if (_enable_gps)   
374                         gps_connect_now(_gps);
375                 return FALSE;
376         break;
377 }
378
379 gtk_progress_bar_set_fraction(GTK_PROGRESS(init_progress), p);
380 gtk_progress_bar_set_text(GTK_PROGRESS(init_progress), w);
381 return ret;
382 }
383
384 gint 
385 main(gint argc, gchar * argv[])
386 {
387 /* Initialize localization. */
388 setlocale(LC_ALL, "");
389 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
390 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
391 textdomain(GETTEXT_PACKAGE);
392
393 g_thread_init(NULL);
394 g_type_init();
395 g_set_application_name("Mapper");
396 gtk_init(&argc, &argv);
397 if (mapper_osso_init()!=0)
398         return 1;
399 if (mapper_osso_cb_init()!=0)
400         return 1;
401 #if defined (WITH_GST)
402 gst_init(&argc, &argv);
403 #endif
404
405 init_progress=gtk_progress_bar_new();
406 init_dialog=progress_dialog("Mapper is loading...",init_progress);
407 g_idle_add((GSourceFunc)mapper_init, NULL);
408 gtk_main();
409 mapper_destroy();
410
411 return 0;
412 }