]> err.no Git - mapper/blob - src/mapper.c
Move some variables around
[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 "gps-conn.h"
62 #include "map.h"
63 #include "route.h"
64 #include "track.h"
65 #include "ui-common.h"
66 #include "db.h"
67 #include "osm-db.h"
68 #include "poi.h"
69 #include "cb.h"
70 #include "speak.h"
71 #include "gpx.h"
72 #include "config-gconf.h"
73 #include "dialogs.h"
74
75 gfloat UNITS_CONVERT[] = {1.85200,1.15077945,1.f,};
76
77 GdkColor COLORABLE_DEFAULT[COLORABLE_ENUM_COUNT] = {
78         {0, 0x0000, 0x0000, 0xc000}
79         ,                       /* COLORABLE_MARK */
80         {0, 0x6000, 0x6000, 0xf800}
81         ,                       /* COLORABLE_MARK_VELOCITY */
82         {0, 0x8000, 0x8000, 0x8000}
83         ,                       /* COLORABLE_MARK_OLD */
84         {0, 0xe000, 0x0000, 0x0000}
85         ,                       /* COLORABLE_TRACK */
86         {0, 0xa000, 0x0000, 0x0000}
87         ,                       /* COLORABLE_TRACK_MARK */
88         {0, 0x7000, 0x0000, 0x0000}
89         ,                       /* COLORABLE_TRACK_BREAK */
90         {0, 0x0000, 0xa000, 0x0000}
91         ,                       /* COLORABLE_ROUTE */
92         {0, 0x0000, 0x8000, 0x0000}
93         ,                       /* COLORABLE_ROUTE_WAY */
94         {0, 0x0000, 0x6000, 0x0000}
95         ,                       /* COLORABLE_ROUTE_BREAK */
96         {0, 0xa000, 0x0000, 0xa000}     /* COLORABLE_POI */
97 };
98
99 enum {
100         MAPPER_INIT_START=0,
101         MAPPER_INIT_MISC,
102         MAPPER_INIT_CONFIG,
103         MAPPER_INIT_DB,
104         MAPPER_INIT_POI,
105         MAPPER_INIT_OSM,
106         MAPPER_INIT_VOICE,
107         MAPPER_INIT_UI,
108         MAPPER_INIT_GOTO,
109         MAPPER_INIT_DONE
110 } mapper_init_state;
111
112 guint mis=MAPPER_INIT_START;
113
114 static GtkWidget *init_progress;
115 static GtkWidget *init_dialog;
116
117 /**
118  * Initialize arrays
119  */
120 static void 
121 variables_init(void)
122 {
123 INFO_FONT_TEXT[INFO_FONT_XXSMALL] = "xx-small";
124 INFO_FONT_TEXT[INFO_FONT_XSMALL] = "x-small";
125 INFO_FONT_TEXT[INFO_FONT_SMALL] = "small";
126 INFO_FONT_TEXT[INFO_FONT_MEDIUM] = "medium";
127 INFO_FONT_TEXT[INFO_FONT_LARGE] = "large";
128 INFO_FONT_TEXT[INFO_FONT_XLARGE] = "x-large";
129 INFO_FONT_TEXT[INFO_FONT_XXLARGE] = "xx-large";
130
131 CUSTOM_KEY_GCONF[CUSTOM_KEY_UP] = GCONF_KEY_PREFIX "/key_up";
132 CUSTOM_KEY_GCONF[CUSTOM_KEY_DOWN] = GCONF_KEY_PREFIX "/key_down";
133 CUSTOM_KEY_GCONF[CUSTOM_KEY_LEFT] = GCONF_KEY_PREFIX "/key_left";
134 CUSTOM_KEY_GCONF[CUSTOM_KEY_RIGHT] = GCONF_KEY_PREFIX "/key_right";
135 CUSTOM_KEY_GCONF[CUSTOM_KEY_SELECT] = GCONF_KEY_PREFIX "/key_select";
136 CUSTOM_KEY_GCONF[CUSTOM_KEY_INCREASE] = GCONF_KEY_PREFIX "/key_increase";
137 CUSTOM_KEY_GCONF[CUSTOM_KEY_DECREASE] = GCONF_KEY_PREFIX "/key_decrease";
138 CUSTOM_KEY_GCONF[CUSTOM_KEY_FULLSCREEN] = GCONF_KEY_PREFIX "/key_fullscreen";
139 CUSTOM_KEY_GCONF[CUSTOM_KEY_ESC] = GCONF_KEY_PREFIX "/key_esc";
140
141 CUSTOM_KEY_ICON[CUSTOM_KEY_UP] = HWK_BUTTON_UP;
142 CUSTOM_KEY_ICON[CUSTOM_KEY_LEFT] = HWK_BUTTON_LEFT;
143 CUSTOM_KEY_ICON[CUSTOM_KEY_DOWN] = HWK_BUTTON_DOWN;
144 CUSTOM_KEY_ICON[CUSTOM_KEY_RIGHT] = HWK_BUTTON_RIGHT;
145 CUSTOM_KEY_ICON[CUSTOM_KEY_SELECT] = HWK_BUTTON_SELECT;
146 CUSTOM_KEY_ICON[CUSTOM_KEY_INCREASE] = HWK_BUTTON_INCREASE;
147 CUSTOM_KEY_ICON[CUSTOM_KEY_DECREASE] = HWK_BUTTON_DECREASE;
148 CUSTOM_KEY_ICON[CUSTOM_KEY_FULLSCREEN] = HWK_BUTTON_VIEW;
149 CUSTOM_KEY_ICON[CUSTOM_KEY_ESC] = HWK_BUTTON_CANCEL;
150
151 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_UP] = CUSTOM_ACTION_PAN_NORTH;
152 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_LEFT] = CUSTOM_ACTION_PAN_WEST;
153 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_DOWN] = CUSTOM_ACTION_PAN_SOUTH;
154 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_RIGHT] = CUSTOM_ACTION_PAN_EAST;
155 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_SELECT] = CUSTOM_ACTION_TOGGLE_AUTOCENTER;
156 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_INCREASE] = CUSTOM_ACTION_ZOOM_IN;
157 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_DECREASE] = CUSTOM_ACTION_ZOOM_OUT;
158 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_FULLSCREEN] = CUSTOM_ACTION_TOGGLE_FULLSCREEN;
159 CUSTOM_KEY_DEFAULT[CUSTOM_KEY_ESC] = CUSTOM_ACTION_TOGGLE_TRACKS;
160
161 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_NORTH] = _("Pan North");
162 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_WEST] = _("Pan West");
163 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_SOUTH] = _("Pan South");
164 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_PAN_EAST] = _("Pan East");
165 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_AUTOCENTER] = _("Toggle Auto-Center");
166 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_FULLSCREEN] = _("Toggle Fullscreen");
167 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ZOOM_IN] = _("Zoom In");
168 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ZOOM_OUT] = _("Zoom Out");
169 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_TRACKS] = _("Toggle Tracks");
170 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_SCALE] = _("Toggle Scale");
171 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_POI] = _("Toggle POIs");
172 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_CHANGE_REPO] = _("Select Next Repository");
173 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ROUTE_DISTNEXT] = _("Show Distance to Next Waypoint");
174 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_ROUTE_DISTLAST] = _("Show Distance to End of Route");
175 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TRACK_BREAK] = _("Insert Track Break");
176 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TRACK_DISTLAST] = _("Show Distance from Last Break");
177 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TRACK_DISTFIRST] = _("Show Distance from Beginning");
178 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_GPS] = _("Toggle GPS");
179 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_GPSINFO] = _("Toggle GPS Info");
180 CUSTOM_ACTION_TEXT[CUSTOM_ACTION_TOGGLE_SPEEDLIMIT] = _("Toggle Speed Limit");
181
182 COLORABLE_GCONF[COLORABLE_MARK] = GCONF_KEY_PREFIX "/color_mark";
183 COLORABLE_GCONF[COLORABLE_MARK_VELOCITY] = GCONF_KEY_PREFIX "/color_mark_velocity";
184 COLORABLE_GCONF[COLORABLE_MARK_OLD] = GCONF_KEY_PREFIX "/color_mark_old";
185 COLORABLE_GCONF[COLORABLE_TRACK] = GCONF_KEY_PREFIX "/color_track";
186 COLORABLE_GCONF[COLORABLE_TRACK_MARK] = GCONF_KEY_PREFIX "/color_track_mark";
187 COLORABLE_GCONF[COLORABLE_TRACK_BREAK] = GCONF_KEY_PREFIX "/color_track_break";
188 COLORABLE_GCONF[COLORABLE_ROUTE] = GCONF_KEY_PREFIX "/color_route";
189 COLORABLE_GCONF[COLORABLE_ROUTE_WAY] = GCONF_KEY_PREFIX "/color_route_way";
190 COLORABLE_GCONF[COLORABLE_ROUTE_BREAK] = GCONF_KEY_PREFIX "/color_route_break";
191 COLORABLE_GCONF[COLORABLE_POI] = GCONF_KEY_PREFIX "/color_poi";
192 }
193
194 /**
195  * Save state and destroy all non-UI elements created by this program in
196  * preparation for exiting.
197  */
198 static void 
199 mapper_destroy(void)
200 {
201 config_save();
202 config_save_repo();
203 map_download_deinit();
204 map_poi_deinit();
205 #if defined (WITH_GST) && defined (WITH_ESPEAK)
206 speak_deinit();
207 #endif
208 gps_disconnect(_gps);
209 gps_free(_gps);
210 osm_deinit();
211 db_close(&_db);
212 track_deinit();
213 route_deinit();
214
215 gnome_vfs_shutdown();
216 #ifdef WITH_OSSO
217 osso_deinitialize(_osso);
218 #endif
219 curl_global_cleanup();
220 }
221
222 static gint 
223 mapper_osso_init(void)
224 {
225 #ifdef WITH_OSSO
226 /* Initialize _osso. */
227 _osso = osso_initialize("org.tal.mapper", VERSION, TRUE, NULL);
228 if (!_osso) {
229         g_printerr("osso_initialize failed.\n");
230         return 1;
231 }
232 #endif
233 return 0;
234 }
235
236 static gint 
237 mapper_osso_cb_init(void)
238 {
239 gchar *filter_string;
240
241 #ifdef WITH_OSSO
242 if (OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_cb_default, NULL)) {
243         g_printerr("osso_rpc_set_default_cb_f failed.\n");
244         return 1;
245 }
246 #endif
247
248 #ifdef WITH_OSSO_IC
249 filter_string = g_strdup_printf("interface=%s", ICD_DBUS_INTERFACE);
250 /* add match */
251 dbus_bus_add_match(dbus_conn, filter_string, NULL);
252 g_free(filter_string);
253
254 /* add the callback */
255 dbus_connection_add_filter(dbus_conn, get_connection_status_signal_cb, NULL, NULL);
256 osso_iap_cb(iap_callback);
257 #endif
258
259 return 0;
260 }
261
262 static void 
263 timezone_init(void)
264 {
265 time_t time1;
266 struct tm time2;
267
268 time1 = time(NULL);
269 localtime_r(&time1, &time2);
270 _gmtoffset = time2.tm_gmtoff;
271 }
272
273 static gboolean
274 mapper_init(gpointer data)
275 {
276 GError *error = NULL;
277 gboolean ret=TRUE;
278 gfloat p=0;
279 gchar *w="Starting";
280
281 switch (mis) {
282         case MAPPER_INIT_START:
283                 curl_global_init(CURL_GLOBAL_NOTHING);
284                 gnome_vfs_init();
285                 /* XXX: Load GPS configuration, then create gps */
286                 _gps=gps_new(GPS_IO_SIMULATION);
287                 _gps->connection_retry=gps_retry_connection;
288                 _gps->connection_error=NULL;
289                 _gps->connection_progress=gps_conn_set_progress;
290                 _gps->update_location=gps_location_update;
291                 _gps->update_info=gps_info_update;
292
293                 timezone_init();
294                 gpx_init();
295                 variables_init();
296                 latlon_init();
297                 track_init();
298                 route_init();
299                 mis=MAPPER_INIT_CONFIG;
300                 p=0.1;
301                 w="Init";
302         break;
303         case MAPPER_INIT_CONFIG:
304                 config_init();
305                 map_download_init();
306                 mis=MAPPER_INIT_DB;
307                 p=0.2;
308                 w="Config";
309         break;
310         case MAPPER_INIT_DB:
311                 if (db_connect(&_db, _mapper_db)) {
312                         mis=MAPPER_INIT_POI;
313                 } else {
314                         mis=MAPPER_INIT_VOICE;
315                 }
316                 p=0.5;
317                 w="Database";
318         break;
319         case MAPPER_INIT_POI:
320                 poi_init(&_db);
321                 mis=MAPPER_INIT_OSM;
322                 p=0.6;
323                 w="POIs";
324         break;
325         case MAPPER_INIT_OSM:
326                 osm_init(&_db);
327                 mis=MAPPER_INIT_VOICE;
328                 p=0.8;
329                 w="OSM";
330         break;
331         case MAPPER_INIT_VOICE:
332                 #if defined (WITH_GST) && defined (WITH_ESPEAK)
333                 if (speak_init("en",_voice_speed,_voice_pitch)==FALSE) {
334                         g_printerr("Espeak init failed\n");
335                         popup_error(_window, "Speech init failed. Disabled.");
336                 }
337                 #endif
338                 mis=MAPPER_INIT_MISC;
339                 p=0.85;
340                 w="Speech";
341         break;
342         case MAPPER_INIT_MISC:
343                 /* Initialize D-Bus system connection. */
344                 if (NULL == (dbus_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error))) {
345                         g_printerr("Failed to open connection to D-Bus: %s.\n", error->message);
346                         popup_error(_window, "Failed to connect to D-Bus.");
347                         error = NULL;
348                 }
349
350                 /* XXX: Move this */
351                 #ifdef WITH_OSSO
352                 osso_hw_set_event_cb(_osso, NULL, osso_cb_hw_state, NULL);
353                 #endif
354
355                 mis=MAPPER_INIT_UI;
356                 p=0.9;
357                 w="Misc";
358         break;
359         case MAPPER_INIT_UI:
360                 mapper_ui_init();
361                 mis=MAPPER_INIT_GOTO;
362                 p=0.99;
363                 w="UI";
364         break;
365         case MAPPER_INIT_GOTO:
366                 if (map_goto_position(&_home)==FALSE)
367                         g_printerr("Home is not set.\n");
368                 else
369                         map_center_unit(_center.unitx, _center.unity);
370                 mis=MAPPER_INIT_DONE;
371                 p=1.0;
372                 w="Done";
373         break;
374         case MAPPER_INIT_DONE:
375                 progress_dialog_remove(init_dialog);
376                 if (_enable_gps)   
377                         gps_connect_now(_gps);
378                 return FALSE;
379         break;
380 }
381
382 gtk_progress_bar_set_fraction(GTK_PROGRESS(init_progress), p);
383 gtk_progress_bar_set_text(GTK_PROGRESS(init_progress), w);
384 return ret;
385 }
386
387 gint 
388 main(gint argc, gchar * argv[])
389 {
390 /* Initialize localization. */
391 setlocale(LC_ALL, "");
392 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
393 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
394 textdomain(GETTEXT_PACKAGE);
395
396 g_thread_init(NULL);
397 g_type_init();
398 g_set_application_name("Mapper");
399 #if defined (WITH_GST)
400 gst_init(&argc, &argv);
401 #endif
402 gtk_init(&argc, &argv);
403 if (mapper_osso_init()!=0)
404         return 1;
405 if (mapper_osso_cb_init()!=0)
406         return 1;
407
408 init_progress=gtk_progress_bar_new();
409 init_dialog=progress_dialog(NULL, "Mapper is loading...",init_progress);
410 g_idle_add((GSourceFunc)mapper_init, NULL);
411 gtk_main();
412 mapper_destroy();
413
414 return 0;
415 }