]> err.no Git - mapper/blob - src/mapper.c
Start to rewrite the GPS system to support location information from multiple sources.
[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  * Save state and destroy all non-UI elements created by this program in
117  * preparation for exiting.
118  */
119 static void 
120 mapper_destroy(void)
121 {
122 config_save();
123 config_save_repo();
124 gps_disconnect(_gps);
125 map_download_deinit();
126 #if defined (WITH_GST) && defined (WITH_ESPEAK)
127 speak_deinit();
128 #endif
129 osm_deinit();
130 db_close(&_db);
131 track_deinit();
132 route_deinit();
133
134 gnome_vfs_shutdown();
135 #ifdef WITH_OSSO
136 osso_deinitialize(_osso);
137 #endif
138 curl_global_cleanup();
139 }
140
141 static gint 
142 mapper_osso_init(void)
143 {
144 #ifdef WITH_OSSO
145 /* Initialize _osso. */
146 _osso = osso_initialize("org.tal.mapper", VERSION, TRUE, NULL);
147 if (!_osso) {
148         g_printerr("osso_initialize failed.\n");
149         return 1;
150 }
151 #endif
152 return 0;
153 }
154
155 static gint 
156 mapper_osso_cb_init(void)
157 {
158 gchar *filter_string;
159
160 #ifdef WITH_OSSO
161 if (OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_cb_default, NULL)) {
162         g_printerr("osso_rpc_set_default_cb_f failed.\n");
163         return 1;
164 }
165 #endif
166
167 #ifdef WITH_OSSO_IC
168 filter_string = g_strdup_printf("interface=%s", ICD_DBUS_INTERFACE);
169 /* add match */
170 dbus_bus_add_match(dbus_conn, filter_string, NULL);
171 g_free(filter_string);
172
173 /* add the callback */
174 dbus_connection_add_filter(dbus_conn, get_connection_status_signal_cb, NULL, NULL);
175 osso_iap_cb(iap_callback);
176 #endif
177
178 return 0;
179 }
180
181 static void 
182 timezone_init(void)
183 {
184 time_t time1;
185 struct tm time2;
186
187 time1 = time(NULL);
188 localtime_r(&time1, &time2);
189 _gmtoffset = time2.tm_gmtoff;
190 }
191
192 static gboolean
193 mapper_init(gpointer data)
194 {
195 GError *error = NULL;
196 gboolean ret=TRUE;
197 gfloat p=0;
198 gchar *w="Starting";
199
200 switch (mis) {
201         case MAPPER_INIT_START:
202                 curl_global_init(CURL_GLOBAL_NOTHING);
203                 gnome_vfs_init();
204                 _gps=gps_new();
205                 _gps->io.type=GPS_IO_SIMULATION;
206 #ifdef WITH_BLUEZ
207                 _gps->io.type=GPS_IO_RFCOMM;
208 #endif
209 #ifdef WITH_HILDON_DBUS_BT
210                 _gps->io.type=GPS_IO_HILDON_DBUS;
211 #endif
212                 _gps->io.conn = RCVR_OFF;
213                 timezone_init();
214                 gpx_init();
215                 variables_init();
216                 latlon_init();
217                 track_init();
218                 route_init();
219                 mis=MAPPER_INIT_CONFIG;
220                 p=0.1;
221                 w="Init";
222         break;
223         case MAPPER_INIT_CONFIG:
224                 config_init();
225                 map_download_init();
226                 mis=MAPPER_INIT_DB;
227                 p=0.2;
228                 w="Config";
229         break;
230         case MAPPER_INIT_DB:
231                 if (db_connect(&_db, _mapper_db)) {
232                         mis=MAPPER_INIT_POI;
233                 } else {
234                         mis=MAPPER_INIT_VOICE;
235                 }
236                 p=0.5;
237                 w="Database";
238         break;
239         case MAPPER_INIT_POI:
240                 poi_init(&_db);
241                 mis=MAPPER_INIT_OSM;
242                 p=0.6;
243                 w="POIs";
244         break;
245         case MAPPER_INIT_OSM:
246                 osm_init(&_db);
247                 mis=MAPPER_INIT_VOICE;
248                 p=0.8;
249                 w="OSM";
250         break;
251         case MAPPER_INIT_VOICE:
252                 #if defined (WITH_GST) && defined (WITH_ESPEAK)
253                 if (speak_init("en",_voice_speed,_voice_pitch)==FALSE) {
254                         g_printerr("Espeak init failed\n");
255                         popup_error(_window, "Speech init failed. Disabled.");
256                 }
257                 #endif
258                 mis=MAPPER_INIT_MISC;
259                 p=0.85;
260                 w="Speech";
261         break;
262         case MAPPER_INIT_MISC:
263                 /* Initialize D-Bus system connection. */
264                 if (NULL == (dbus_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error))) {
265                         g_printerr("Failed to open connection to D-Bus: %s.\n", error->message);
266                         error = NULL;
267                 }
268
269                 /* XXX: Move this */
270                 #ifdef WITH_OSSO
271                 osso_hw_set_event_cb(_osso, NULL, osso_cb_hw_state, NULL);
272                 #endif
273
274                 #ifdef WITH_HILDON_DBUS_BT
275                 if (NULL == (_rfcomm_req_proxy = dbus_g_proxy_new_for_name(dbus_conn, BTCOND_SERVICE, BTCOND_REQ_PATH, BTCOND_REQ_INTERFACE))) {
276                         g_printerr("Failed to open connection to %s.\n", BTCOND_REQ_INTERFACE);
277                         popup_error(_window, "Bluetooth connection handling failed.");
278                 }
279                 #endif
280                 mis=MAPPER_INIT_UI;
281                 p=0.9;
282                 w="Misc";
283         break;
284         case MAPPER_INIT_UI:
285                 mapper_ui_init();
286                 mis=MAPPER_INIT_GOTO;
287                 p=0.99;
288                 w="UI";
289         break;
290         case MAPPER_INIT_GOTO:
291                 if (map_goto_position(&_home)==FALSE)
292                         g_printerr("Home is not set.\n");
293                 else
294                         map_center_unit(_center.unitx, _center.unity);
295                 mis=MAPPER_INIT_DONE;
296                 p=1.0;
297                 w="Done";
298         break;
299         case MAPPER_INIT_DONE:
300                 progress_dialog_remove(init_dialog);
301                 if (_enable_gps)   
302                 gps_connect_now(_gps);
303                 return FALSE;
304         break;
305 }
306
307 gtk_progress_bar_set_fraction(GTK_PROGRESS(init_progress), p);
308 gtk_progress_bar_set_text(GTK_PROGRESS(init_progress), w);
309 return ret;
310 }
311
312 gint 
313 main(gint argc, gchar * argv[])
314 {
315 /* Initialize localization. */
316 setlocale(LC_ALL, "");
317 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
318 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
319 textdomain(GETTEXT_PACKAGE);
320
321 g_thread_init(NULL);
322 g_type_init();
323 g_set_application_name("Mapper");
324 gtk_init(&argc, &argv);
325 if (mapper_osso_init()!=0)
326         return 1;
327 if (mapper_osso_cb_init()!=0)
328         return 1;
329 #if defined (WITH_GST)
330 gst_init(&argc, &argv);
331 #endif
332
333 init_progress=gtk_progress_bar_new();
334 init_dialog=progress_dialog("Mapper is loading...",init_progress);
335 g_idle_add((GSourceFunc)mapper_init, NULL);
336 gtk_main();
337 mapper_destroy();
338
339 return 0;
340 }