Fix:maptool:Another name for faroe islands
[navit-package] / 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 <getopt.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <glib.h>
27 #include <sys/types.h>
28
29 #ifndef _WIN32
30 #include <sys/wait.h>
31 #include <signal.h>
32 #endif
33
34 #include <unistd.h>
35 #include "config.h"
36 #include "file.h"
37 #include "debug.h"
38 #include "main.h"
39 #include "navit.h"
40 #include "gui.h"
41 #include "item.h"
42 #include "xmlconfig.h"
43 #include "coord.h"
44 #include "route.h"
45 #include "navigation.h"
46 #include "event.h"
47 #include "callback.h"
48 #include "navit_nls.h"
49 #if HAVE_API_WIN32_BASE
50 #include <windows.h>
51 #include <winbase.h>
52 #endif
53
54
55 struct map_data *map_data_default;
56
57 struct callback_list *cbl;
58
59
60 static void sigchld(int sig)
61 {
62 #if !defined(_WIN32) && !defined(__CEGCC__)
63         int status;
64         while (waitpid(-1, &status, WNOHANG) > 0);
65 #endif
66 }
67
68 #ifdef HAVE_API_WIN32
69 void
70 setenv(char *var, char *val, int overwrite)
71 {
72         char *str=g_strdup_printf("%s=%s",var,val);
73         if (overwrite || !getenv(var))
74                 putenv(str);
75         g_free(str);
76 }
77 #endif
78
79 /*
80  * environment_vars[][0:name,1-3:mode]
81  * ':'  replaced with NAVIT_PREFIX
82  * '::' replaced with NAVIT_PREFIX and LIBDIR
83  * '~'  replaced with HOME
84 */
85 static char *environment_vars[][5]={
86         {"NAVIT_LIBDIR",      ":",          "::/navit",      ":\\lib",      ":/lib"},
87         {"NAVIT_SHAREDIR",    ":",          ":/share/navit", ":",           ":/share"},
88         {"NAVIT_LOCALEDIR",   ":/../locale",":/share/locale",":\\locale",   ":/locale"},
89         {"NAVIT_USER_DATADIR",":",          "~/.navit",      ":\\data",     ":/home"},
90 #if 1
91         {"NAVIT_LOGFILE",     NULL,         NULL,            ":\\navit.log",NULL},
92 #endif
93         {"NAVIT_LIBPREFIX",   "*/.libs/",   NULL,            NULL,          NULL},
94         {NULL,                NULL,         NULL,            NULL,          NULL},
95 };
96
97 static void
98 main_setup_environment(int mode)
99 {
100         int i=0;
101         char *var,*val,*homedir;
102         while ((var=environment_vars[i][0])) {
103                 val=environment_vars[i][mode+1];
104                 if (val) {
105                         switch (val[0]) {
106                         case ':':
107                                 if (val[1] == ':')
108                                         val=g_strdup_printf("%s/%s%s", getenv("NAVIT_PREFIX"), LIBDIR+sizeof(PREFIX), val+2);
109                                 else
110                                         val=g_strdup_printf("%s%s", getenv("NAVIT_PREFIX"), val+1);
111                                 break;
112                         case '~':
113                                 homedir=getenv("HOME");
114                                 if (!homedir)
115                                         homedir="./";
116                                 val=g_strdup_printf("%s%s", homedir, val+1);
117                                 break;
118                         default:
119                                 val=g_strdup(val);
120                                 break;
121                         }
122                         setenv(var, val, 0);
123                         g_free(val);
124                 }
125                 i++;
126         }
127 }
128
129 #ifdef HAVE_API_WIN32_BASE
130 char *nls_table[][3]={
131         {"DAN","DNK","da_DK"},
132         {"DEU","DEU","de_DE"},
133         {"DEA","AUT","de_AT"},
134         {"ENU","USA","en_US"},
135         {"FRA","FRA","fr_FR"},
136         {"RUS","RUS","ru_RU"},
137         {NULL,NULL,NULL},
138 };
139
140 static void
141 win_set_nls(void)
142 {
143         wchar_t wcountry[32],wlang[32]; 
144         char country[32],lang[32];
145         int i=0;
146
147         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
148         WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
149         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
150         WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
151         while (nls_table[i][0]) {
152                 if (!strcmp(nls_table[i][0], lang) && !(strcmp(nls_table[i][1], country))) {
153                         dbg(1,"Setting LANG=%s for Lang %s Country %s\n",nls_table[i][2], lang, country);
154                         setenv("LANG",nls_table[i][2],0);
155                         return;
156                 }
157                 i++;
158         }
159         dbg(1,"Lang %s Country %s not found\n",lang,country);
160 }
161 #endif
162
163 void
164 main_init(char *program)
165 {
166         char *s;
167         int l;
168
169 #ifndef _WIN32
170         signal(SIGCHLD, sigchld);
171 #endif
172         cbl=callback_list_new();
173 #ifdef HAVE_API_WIN32_BASE
174         win_set_nls();
175 #endif
176         setenv("LC_NUMERIC","C",1);
177         setlocale(LC_ALL,"");
178         setlocale(LC_NUMERIC,"C");
179 #if !defined _WIN32 && !defined _WIN32_WCE
180         if (file_exists("navit.c") || file_exists("navit.o") || file_exists("navit.lo")) {
181                 char buffer[PATH_MAX];
182                 printf(_("Running from source directory\n"));
183                 getcwd(buffer, PATH_MAX);               /* libc of navit returns "dummy" */
184                 setenv("NAVIT_PREFIX", buffer, 0);
185                 main_setup_environment(0);
186         } else {
187                 if (!getenv("NAVIT_PREFIX")) {
188                 int progpath_len;
189                         char *progpath="/bin/navit";
190                         l=strlen(program);
191                         progpath_len=strlen(progpath);
192                         if (l > progpath_len && !strcmp(program+l-progpath_len,progpath)) {
193                                 s=g_strdup(program);
194                                 s[l-progpath_len]='\0';
195                                 if (strcmp(s, PREFIX))
196                                         printf(_("setting '%s' to '%s'\n"), "NAVIT_PREFIX", s);
197                                 setenv("NAVIT_PREFIX", s, 0);
198                                 g_free(s);
199                         } else
200                                 setenv("NAVIT_PREFIX", PREFIX, 0);
201                 }
202 #ifdef HAVE_API_ANDROID
203                 main_setup_environment(3);
204 #else
205                 main_setup_environment(1);
206 #endif
207         }
208
209 #else           /* _WIN32 || _WIN32_WCE */
210         if (!getenv("NAVIT_PREFIX"))
211         {
212                 char  filename[MAX_PATH + 1],
213                      *end;
214
215                 *filename = '\0';
216 #ifdef _UNICODE         /* currently for wince */
217                 wchar_t wfilename[MAX_PATH + 1];
218                 if (GetModuleFileNameW(NULL, wfilename, MAX_PATH))
219                 {
220                         wcstombs(filename, wfilename, MAX_PATH);
221 #else
222                 if (GetModuleFileName(NULL, filename, MAX_PATH))
223                 {
224 #endif
225                         end = strrchr(filename, L'\\'); /* eliminate the file name which is on the right side */
226                         if(end)
227                                 *end = '\0';
228                 }
229                 setenv("NAVIT_PREFIX", filename, 0);
230         }
231         if (!getenv("HOME"))
232                 setenv("HOME", getenv("NAVIT_PREFIX"), 0);
233         main_setup_environment(2);
234 #endif  /* _WIN32 || _WIN32_WCE */
235
236         if (getenv("LC_ALL"))
237                 dbg(0,"Warning: LC_ALL is set, this might lead to problems (e.g. strange positions from GPS)\n");
238         s = getenv("NAVIT_WID");
239         if (s) {
240                 setenv("SDL_WINDOWID", s, 0);
241         }
242 }
243
244 void
245 main_init_nls(void)
246 {
247 #ifdef ENABLE_NLS
248 #ifdef FORCE_LOCALE
249 #define STRINGIFY2(x) #x
250 #define STRINGIFY(x) STRINGIFY2(x)
251         setlocale(LC_MESSAGES,STRINGIFY(FORCE_LOCALE));
252 #endif
253         bindtextdomain(PACKAGE, getenv("NAVIT_LOCALEDIR"));
254         bind_textdomain_codeset (PACKAGE, "UTF-8");
255         textdomain(PACKAGE);
256 #endif
257 }