Fix:core:Remove pointless '#if 1'
[profile/ivi/navit.git] / navit / navit / main.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <locale.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <signal.h>
25 #include <glib.h>
26 #include <sys/types.h>
27
28 #include "config.h"
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #ifndef _WIN32
35 #include <sys/wait.h>
36 #include <signal.h>
37 #endif
38
39 #include "file.h"
40 #include "debug.h"
41 #include "main.h"
42 #include "navit.h"
43 #include "gui.h"
44 #include "item.h"
45 #include "xmlconfig.h"
46 #include "coord.h"
47 #include "route.h"
48 #include "navigation.h"
49 #include "event.h"
50 #include "callback.h"
51 #include "navit_nls.h"
52 #include "util.h"
53 #if HAVE_API_WIN32_BASE
54 #include <windows.h>
55 #include <winbase.h>
56 #endif
57
58 #ifdef HAVE_API_WIN32_CE
59 #include "libc.h"
60 #endif
61
62 struct map_data *map_data_default;
63
64 struct callback_list *cbl;
65
66 #ifdef HAVE_API_WIN32
67 void
68 setenv(char *var, char *val, int overwrite)
69 {
70         char *str=g_strdup_printf("%s=%s",var,val);
71         if (overwrite || !getenv(var))
72                 putenv(str);
73         g_free(str);
74 }
75 #endif
76
77 /*
78  * environment_vars[][0:name,1-3:mode]
79  * ':'  replaced with NAVIT_PREFIX
80  * '::' replaced with NAVIT_PREFIX and LIBDIR
81  * '~'  replaced with HOME
82 */
83 static char *environment_vars[][5]={
84         {"NAVIT_LIBDIR",      ":",          "::/navit",      ":\\lib",      ":/lib"},
85         {"NAVIT_SHAREDIR",    ":",          ":/share/navit", ":",           ":/share"},
86         {"NAVIT_LOCALEDIR",   ":/../locale",":/share/locale",":\\locale",   ":/locale"},
87         {"NAVIT_USER_DATADIR",":",          "~/.navit",      ":\\data",     ":/home"},
88         {"NAVIT_LOGFILE",     NULL,         NULL,            ":\\navit.log",NULL},
89         {"NAVIT_LIBPREFIX",   "*/.libs/",   NULL,            NULL,          NULL},
90         {NULL,                NULL,         NULL,            NULL,          NULL},
91 };
92
93 static void
94 main_setup_environment(int mode)
95 {
96         int i=0;
97         char *var,*val,*homedir;
98         while ((var=environment_vars[i][0])) {
99                 val=environment_vars[i][mode+1];
100                 if (val) {
101                         switch (val[0]) {
102                         case ':':
103                                 if (val[1] == ':')
104                                         val=g_strdup_printf("%s/%s%s", getenv("NAVIT_PREFIX"), LIBDIR+sizeof(PREFIX), val+2);
105                                 else
106                                         val=g_strdup_printf("%s%s", getenv("NAVIT_PREFIX"), val+1);
107                                 break;
108                         case '~':
109                                 homedir=getenv("HOME");
110                                 if (!homedir)
111                                         homedir="./";
112                                 val=g_strdup_printf("%s%s", homedir, val+1);
113                                 break;
114                         default:
115                                 val=g_strdup(val);
116                                 break;
117                         }
118                         setenv(var, val, 0);
119                         g_free(val);
120                 }
121                 i++;
122         }
123 }
124
125 #ifdef HAVE_API_WIN32_BASE
126 char *nls_table[][3]={
127         // NLS Table compiled by Nick "Number6" Geoghegan
128         // Not an exhaustive list, but supports 99% of all languages in Windows
129         //{"LANGNAME", "CTRYNAME", "Language Code"},
130         {"AFK", "ZAF", "af_ZA"},        // Afrikaans (South Africa)
131         {"SQI", "ALB", "sq_AL"},        // Albanian (Albania)
132         {"AMH", "ETH", "am_ET"},        // Amharic (Ethiopia)
133         {"ARG", "DZA", "ar_DZ"},        // Arabic (Algeria)
134         {"ARH", "BHR", "ar_BH"},        // Arabic (Bahrain)
135         {"ARE", "EGY", "ar_EG"},        // Arabic (Egypt)
136         {"ARI", "IRQ", "ar_IQ"},        // Arabic (Iraq)
137         {"ARJ", "JOR", "ar_JO"},        // Arabic (Jordan)
138         {"ARK", "KWT", "ar_KW"},        // Arabic (Kuwait)
139         {"ARB", "LBN", "ar_LB"},        // Arabic (Lebanon)
140         {"ARL", "LBY", "ar_LY"},        // Arabic (Libya)
141         {"ARM", "MAR", "ar_MA"},        // Arabic (Morocco)
142         {"ARO", "OMN", "ar_OM"},        // Arabic (Oman)
143         {"ARQ", "QAT", "ar_QA"},        // Arabic (Qatar)
144         {"ARA", "SAU", "ar_SA"},        // Arabic (Saudi Arabia)
145         {"ARS", "SYR", "ar_SY"},        // Arabic (Syria)
146         {"ART", "TUN", "ar_TN"},        // Arabic (Tunisia)
147         {"ARU", "ARE", "ar_AE"},        // Arabic (U.A.E.)
148         {"ARY", "YEM", "ar_YE"},        // Arabic (Yemen)
149         {"HYE", "ARM", "hy_AM"},        // Armenian (Armenia)
150         {"ASM", "IND", "as_IN"},        // Assamese (India)
151         {"BAS", "RUS", "ba_RU"},        // Bashkir (Russia)
152         {"EUQ", "ESP", "eu_ES"},        // Basque (Basque)
153         {"BEL", "BLR", "be_BY"},        // Belarusian (Belarus)
154         {"BNG", "BDG", "bn_BD"},        // Bengali (Bangladesh)
155         {"BNG", "IND", "bn_IN"},        // Bengali (India)
156         {"BRE", "FRA", "br_FR"},        // Breton (France)
157         {"BGR", "BGR", "bg_BG"},        // Bulgarian (Bulgaria)
158         {"CAT", "ESP", "ca_ES"},        // Catalan (Catalan)
159         {"ZHH", "HKG", "zh_HK"},        // Chinese (Hong Kong S.A.R.)
160         {"ZHM", "MCO", "zh_MO"},        // Chinese (Macao S.A.R.)
161         {"CHS", "CHN", "zh_CN"},        // Chinese (People's Republic of China)
162         {"ZHI", "SGP", "zh_SG"},        // Chinese (Singapore)
163         {"CHT", "TWN", "zh_TW"},        // Chinese (Taiwan)
164         {"COS", "FRA", "co_FR"},        // Corsican (France)
165         {"HRV", "HRV", "hr_HR"},        // Croatian (Croatia)
166         {"HRB", "BIH", "hr_BA"},        // Croatian (Latin, Bosnia and Herzegovina)
167         {"CSY", "CZE", "cs_CZ"},        // Czech (Czech Republic)
168         {"DAN", "DNK", "da_DK"},        // Danish (Denmark)
169         {"NLB", "BEL", "nl_BE"},        // Dutch (Belgium)
170         {"NLD", "NLD", "nl_NL"},        // Dutch (Netherlands)
171         {"ENA", "AUS", "en_AU"},        // English (Australia)
172         {"ENL", "BLZ", "en_BZ"},        // English (Belize)
173         {"ENC", "CAN", "en_CA"},        // English (Canada)
174         {"ENB", "CAR", "en_CB"},        // English (Caribbean)
175         {"ENN", "IND", "en_IN"},        // English (India)
176         {"ENI", "IRL", "en_IE"},        // English (Ireland)
177         {"ENJ", "JAM", "en_JM"},        // English (Jamaica)
178         {"ENM", "MYS", "en_MY"},        // English (Malaysia)
179         {"ENZ", "NZL", "en_NZ"},        // English (New Zealand)
180         {"ENP", "PHL", "en_PH"},        // English (Republic of the Philippines)
181         {"ENE", "SGP", "en_SG"},        // English (Singapore)
182         {"ENS", "ZAF", "en_ZA"},        // English (South Africa)
183         {"ENT", "TTO", "en_TT"},        // English (Trinidad and Tobago)
184         {"ENG", "GBR", "en_GB"},        // English (United Kingdom)
185         {"ENU", "USA", "en_US"},        // English (United States)
186         {"ENW", "ZWE", "en_ZW"},        // English (Zimbabwe)
187         {"ETI", "EST", "et_EE"},        // Estonian (Estonia)
188         {"FOS", "FRO", "fo_FO"},        // Faroese (Faroe Islands)
189         {"FIN", "FIN", "fi_FI"},        // Finnish (Finland)
190         {"FRB", "BEL", "fr_BE"},        // French (Belgium)
191         {"FRC", "CAN", "fr_CA"},        // French (Canada)
192         {"FRA", "FRA", "fr_FR"},        // French (France)
193         {"FRL", "LUX", "fr_LU"},        // French (Luxembourg)
194         {"FRM", "MCO", "fr_MC"},        // French (Principality of Monaco)
195         {"FRS", "CHE", "fr_CH"},        // French (Switzerland)
196         {"FYN", "NLD", "fy_NL"},        // Frisian (Netherlands)
197         {"GLC", "ESP", "gl_ES"},        // Galician (Galician)
198         {"KAT", "GEO", "ka_GE"},        // Georgian (Georgia)
199         {"DEA", "AUT", "de_AT"},        // German (Austria)
200         {"DEU", "DEU", "de_DE"},        // German (Germany)
201         {"DEC", "LIE", "de_LI"},        // German (Liechtenstein)
202         {"DEL", "LUX", "de_LU"},        // German (Luxembourg)
203         {"DES", "CHE", "de_CH"},        // German (Switzerland)
204         {"ELL", "GRC", "el_GR"},        // Greek (Greece)
205         {"KAL", "GRL", "kl_GL"},        // Greenlandic (Greenland)
206         {"GUJ", "IND", "gu_IN"},        // Gujarati (India)
207         {"HEB", "ISR", "he_IL"},        // Hebrew (Israel)
208         {"HIN", "IND", "hi_IN"},        // Hindi (India)
209         {"HUN", "HUN", "hu_HU"},        // Hungarian (Hungary)
210         {"ISL", "ISL", "is_IS"},        // Icelandic (Iceland)
211         {"IBO", "NGA", "ig_NG"},        // Igbo (Nigeria)
212         {"IND", "IDN", "id_ID"},        // Indonesian (Indonesia)
213         {"IRE", "IRL", "ga_IE"},        // Irish (Ireland)
214         {"XHO", "ZAF", "xh_ZA"},        // isiXhosa (South Africa)
215         {"ZUL", "ZAF", "zu_ZA"},        // isiZulu (South Africa)
216         {"ITA", "ITA", "it_IT"},        // Italian (Italy)
217         {"ITS", "CHE", "it_CH"},        // Italian (Switzerland)
218         {"JPN", "JPN", "ja_JP"},        // Japanese (Japan)
219         {"KDI", "IND", "kn_IN"},        // Kannada (India)
220         {"KKZ", "KAZ", "kk_KZ"},        // Kazakh (Kazakhstan)
221         {"KHM", "KHM", "km_KH"},        // Khmer (Cambodia)
222         {"KIN", "RWA", "rw_RW"},        // Kinyarwanda (Rwanda)
223         {"SWK", "KEN", "sw_KE"},        // Kiswahili (Kenya)
224         {"KOR", "KOR", "ko_KR"},        // Korean (Korea)
225         {"KYR", "KGZ", "ky_KG"},        // Kyrgyz (Kyrgyzstan)
226         {"LAO", "LAO", "lo_LA"},        // Lao (Lao P.D.R.)
227         {"LVI", "LVA", "lv_LV"},        // Latvian (Latvia)
228         {"LTH", "LTU", "lt_LT"},        // Lithuanian (Lithuania)
229         {"LBX", "LUX", "lb_LU"},        // Luxembourgish (Luxembourg)
230         {"MKI", "MKD", "mk_MK"},        // Macedonian (Former Yugoslav Republic of Macedonia)
231         {"MSB", "BRN", "ms_BN"},        // Malay (Brunei Darussalam)
232         {"MSL", "MYS", "ms_MY"},        // Malay (Malaysia)
233         {"MYM", "IND", "ml_IN"},        // Malayalam (India)
234         {"MLT", "MLT", "mt_MT"},        // Maltese (Malta)
235         {"MRI", "NZL", "mi_NZ"},        // Maori (New Zealand)
236         {"MAR", "IND", "mr_IN"},        // Marathi (India)
237         {"MON", "MNG", "mn_MN"},        // Mongolian (Cyrillic, Mongolia)
238         {"NEP", "NEP", "ne_NP"},        // Nepali (Nepal)
239         {"NOR", "NOR", "nb_NO"},        // Norwegian, BokmÃ¥(Norway)
240         {"NON", "NOR", "nn_NO"},        // Norwegian, Nynorsk (Norway)
241         {"OCI", "FRA", "oc_FR"},        // Occitan (France)
242         {"ORI", "IND", "or_IN"},        // Oriya (India)
243         {"PAS", "AFG", "ps_AF"},        // Pashto (Afghanistan)
244         {"FAR", "IRN", "fa_IR"},        // Persian
245         {"PLK", "POL", "pl_PL"},        // Polish (Poland)
246         {"PTB", "BRA", "pt_BR"},        // Portuguese (Brazil)
247         {"PTG", "PRT", "pt_PT"},        // Portuguese (Portugal)
248         {"PAN", "IND", "pa_IN"},        // Punjabi (India)
249         {"ROM", "ROM", "ro_RO"},        // Romanian (Romania)
250         {"RMC", "CHE", "rm_CH"},        // Romansh (Switzerland)
251         {"RUS", "RUS", "ru_RU"},        // Russian (Russia)
252         {"SMG", "FIN", "se_FI"},        // Sami, Northern (Finland)
253         {"SME", "NOR", "se_NO"},        // Sami, Northern (Norway)
254         {"SMF", "SWE", "se_SE"},        // Sami, Northern (Sweden)
255         {"SAN", "IND", "sa_IN"},        // Sanskrit (India)
256         {"TSN", "ZAF", "tn_ZA"},        // Setswana (South Africa)
257         {"SIN", "LKA", "si_LK"},        // Sinhala (Sri Lanka)
258         {"SKY", "SVK", "sk_SK"},        // Slovak (Slovakia)
259         {"SLV", "SVN", "sl_SI"},        // Slovenian (Slovenia)
260         {"ESS", "ARG", "es_AR"},        // Spanish (Argentina)
261         {"ESB", "BOL", "es_BO"},        // Spanish (Bolivia)
262         {"ESL", "CHL", "es_CL"},        // Spanish (Chile)
263         {"ESO", "COL", "es_CO"},        // Spanish (Colombia)
264         {"ESC", "CRI", "es_CR"},        // Spanish (Costa Rica)
265         {"ESD", "DOM", "es_DO"},        // Spanish (Dominican Republic)
266         {"ESF", "ECU", "es_EC"},        // Spanish (Ecuador)
267         {"ESE", "SLV", "es_SV"},        // Spanish (El Salvador)
268         {"ESG", "GTM", "es_GT"},        // Spanish (Guatemala)
269         {"ESH", "HND", "es_HN"},        // Spanish (Honduras)
270         {"ESM", "MEX", "es_MX"},        // Spanish (Mexico)
271         {"ESI", "NIC", "es_NI"},        // Spanish (Nicaragua)
272         {"ESA", "PAN", "es_PA"},        // Spanish (Panama)
273         {"ESZ", "PRY", "es_PY"},        // Spanish (Paraguay)
274         {"ESR", "PER", "es_PE"},        // Spanish (Peru)
275         {"ESU", "PRI", "es_PR"},        // Spanish (Puerto Rico)
276         {"ESN", "ESP", "es_ES"},        // Spanish (Spain)
277         {"EST", "USA", "es_US"},        // Spanish (United States)
278         {"ESY", "URY", "es_UY"},        // Spanish (Uruguay)
279         {"ESV", "VEN", "es_VE"},        // Spanish (Venezuela)
280         {"SVF", "FIN", "sv_FI"},        // Swedish (Finland)
281         {"SVE", "SWE", "sv_SE"},        // Swedish (Sweden)
282         {"TAM", "IND", "ta_IN"},        // Tamil (India)
283         {"TTT", "RUS", "tt_RU"},        // Tatar (Russia)
284         {"TEL", "IND", "te_IN"},        // Telugu (India)
285         {"THA", "THA", "th_TH"},        // Thai (Thailand)
286         {"BOB", "CHN", "bo_CN"},        // Tibetan (PRC)
287         {"TRK", "TUR", "tr_TR"},        // Turkish (Turkey)
288         {"TUK", "TKM", "tk_TM"},        // Turkmen (Turkmenistan)
289         {"UIG", "CHN", "ug_CN"},        // Uighur (PRC)
290         {"UKR", "UKR", "uk_UA"},        // Ukrainian (Ukraine)
291         {"URD", "PAK", "ur_PK"},        // Urdu (Islamic Republic of Pakistan)
292         {"VIT", "VNM", "vi_VN"},        // Vietnamese (Vietnam)
293         {"CYM", "GBR", "cy_GB"},        // Welsh (United Kingdom)
294         {"WOL", "SEN", "wo_SN"},        // Wolof (Senegal)
295         {"III", "CHN", "ii_CN"},        // Yi (PRC)
296         {"YOR", "NGA", "yo_NG"},        // Yoruba (Nigeria)
297         {NULL,NULL,NULL},               // Default - Can't find the language / Language not listed above                
298 };
299
300 static void
301 win_set_nls(void)
302 {
303         char country[32],lang[32];
304         int i=0;
305
306 #ifdef HAVE_API_WIN32_CE 
307         wchar_t wcountry[32],wlang[32]; 
308         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
309         WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
310         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
311         WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
312 #else
313         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, lang, sizeof(lang));
314         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, country, sizeof(country));
315 #endif
316         while (nls_table[i][0]) {
317                 if (!strcmp(nls_table[i][0], lang) && !(strcmp(nls_table[i][1], country))) {
318                         dbg(1,"Setting LANG=%s for Lang %s Country %s\n",nls_table[i][2], lang, country);
319                         setenv("LANG",nls_table[i][2],0);
320                         return;
321                 }
322                 i++;
323         }
324         dbg(1,"Lang %s Country %s not found\n",lang,country);
325 }
326 #endif
327
328 void
329 main_init(const char *program)
330 {
331         char *s;
332 #ifdef _UNICODE         /* currently for wince */
333                 wchar_t wfilename[MAX_PATH + 1];
334 #endif
335
336         spawn_process_init();
337
338         cbl=callback_list_new();
339 #ifdef HAVE_API_WIN32_BASE
340         win_set_nls();
341 #endif
342         setenv("LC_NUMERIC","C",1);
343         setlocale(LC_ALL,"");
344         setlocale(LC_NUMERIC,"C");
345 #if !defined _WIN32 && !defined _WIN32_WCE
346         if (file_exists("navit.c") || file_exists("navit.o") || file_exists("navit.lo") || file_exists("version.h")) {
347                 char buffer[PATH_MAX];
348                 printf(_("Running from source directory\n"));
349                 getcwd(buffer, PATH_MAX);               /* libc of navit returns "dummy" */
350                 setenv("NAVIT_PREFIX", buffer, 0);
351                 main_setup_environment(0);
352         } else {
353                 if (!getenv("NAVIT_PREFIX")) {
354                         int l;
355                         int progpath_len;
356                         char *progpath="/bin/navit";
357                         l=strlen(program);
358                         progpath_len=strlen(progpath);
359                         if (l > progpath_len && !strcmp(program+l-progpath_len,progpath)) {
360                                 s=g_strdup(program);
361                                 s[l-progpath_len]='\0';
362                                 if (strcmp(s, PREFIX))
363                                         printf(_("setting '%s' to '%s'\n"), "NAVIT_PREFIX", s);
364                                 setenv("NAVIT_PREFIX", s, 0);
365                                 g_free(s);
366                         } else
367                                 setenv("NAVIT_PREFIX", PREFIX, 0);
368                 }
369 #ifdef HAVE_API_ANDROID
370                 main_setup_environment(3);
371 #else
372                 main_setup_environment(1);
373 #endif
374         }
375
376 #else           /* _WIN32 || _WIN32_WCE */
377         if (!getenv("NAVIT_PREFIX"))
378         {
379                 char  filename[MAX_PATH + 1],
380                      *end;
381                 int len;
382
383                 *filename = '\0';
384 #ifdef _UNICODE         /* currently for wince */
385                 if (GetModuleFileNameW(NULL, wfilename, MAX_PATH))
386                 {
387                         wcstombs(filename, wfilename, MAX_PATH);
388 #else
389                 if (GetModuleFileName(NULL, filename, MAX_PATH))
390                 {
391 #endif
392                         end = strrchr(filename, L'\\'); /* eliminate the file name which is on the right side */
393                         if(end)
394                                 *end = '\0';
395                 }
396                 len=strlen(filename);
397                 if (len > 4 && !strcmp(filename+len-4,"\\bin")) {
398                         filename[len-4]='\0';
399                 }
400                 setenv("NAVIT_PREFIX", filename, 0);
401         }
402         if (!getenv("HOME"))
403                 setenv("HOME", getenv("NAVIT_PREFIX"), 0);
404         main_setup_environment(2);
405 #endif  /* _WIN32 || _WIN32_WCE */
406
407         if (getenv("LC_ALL"))
408                 dbg(0,"Warning: LC_ALL is set, this might lead to problems (e.g. strange positions from GPS)\n");
409         s = getenv("NAVIT_WID");
410         if (s) {
411                 setenv("SDL_WINDOWID", s, 0);
412         }
413 }
414
415 void
416 main_init_nls(void)
417 {
418 #ifdef ENABLE_NLS
419 #ifdef FORCE_LOCALE
420 #define STRINGIFY2(x) #x
421 #define STRINGIFY(x) STRINGIFY2(x)
422         setlocale(LC_MESSAGES,STRINGIFY(FORCE_LOCALE));
423 #endif
424         bindtextdomain(PACKAGE, getenv("NAVIT_LOCALEDIR"));
425         bind_textdomain_codeset (PACKAGE, "UTF-8");
426         textdomain(PACKAGE);
427 #endif
428 }