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