Fix:Core:Made graphics more flexible
[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         {"DEU","DEU","de_DE"},
132         {"DEA","AUT","de_AT"},
133         {"ENU","USA","en_US"},
134         {"RUS","RUS","ru_RU"},
135         {NULL,NULL,NULL},
136 };
137
138 static void
139 win_set_nls(void)
140 {
141         wchar_t wcountry[32],wlang[32]; 
142         char country[32],lang[32];
143         int i=0;
144
145         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
146         WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
147         GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
148         WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
149         while (nls_table[i][0]) {
150                 if (!strcmp(nls_table[i][0], lang) && !(strcmp(nls_table[i][1], country))) {
151                         dbg(1,"Setting LANG=%s for Lang %s Country %s\n",nls_table[i][2], lang, country);
152                         setenv("LANG",nls_table[i][2],0);
153                         return;
154                 }
155                 i++;
156         }
157         dbg(1,"Lang %s Country %s not found\n",lang,country);
158 }
159 #endif
160
161 void
162 main_init(char *program)
163 {
164         char *s;
165         int l;
166
167 #ifndef _WIN32
168         signal(SIGCHLD, sigchld);
169 #endif
170         cbl=callback_list_new();
171 #ifdef HAVE_API_WIN32_BASE
172         win_set_nls();
173 #endif
174         setenv("LC_NUMERIC","C",1);
175         setlocale(LC_ALL,"");
176         setlocale(LC_NUMERIC,"C");
177 #if !defined _WIN32 && !defined _WIN32_WCE
178         if (file_exists("navit.c") || file_exists("navit.o") || file_exists("navit.lo")) {
179                 char buffer[PATH_MAX];
180                 printf(_("Running from source directory\n"));
181                 getcwd(buffer, PATH_MAX);               /* libc of navit returns "dummy" */
182                 setenv("NAVIT_PREFIX", buffer, 0);
183                 main_setup_environment(0);
184         } else {
185                 if (!getenv("NAVIT_PREFIX")) {
186                 int progpath_len;
187                         char *progpath="/bin/navit";
188                         l=strlen(program);
189                         progpath_len=strlen(progpath);
190                         if (l > progpath_len && !strcmp(program+l-progpath_len,progpath)) {
191                                 s=g_strdup(program);
192                                 s[l-progpath_len]='\0';
193                                 if (strcmp(s, PREFIX))
194                                         printf(_("setting '%s' to '%s'\n"), "NAVIT_PREFIX", s);
195                                 setenv("NAVIT_PREFIX", s, 0);
196                                 g_free(s);
197                         } else
198                                 setenv("NAVIT_PREFIX", PREFIX, 0);
199                 }
200 #ifdef HAVE_API_ANDROID
201                 main_setup_environment(3);
202 #else
203                 main_setup_environment(1);
204 #endif
205         }
206
207 #else           /* _WIN32 || _WIN32_WCE */
208         if (!getenv("NAVIT_PREFIX"))
209         {
210                 char  filename[MAX_PATH + 1],
211                      *end;
212
213                 *filename = '\0';
214 #ifdef _UNICODE         /* currently for wince */
215                 wchar_t wfilename[MAX_PATH + 1];
216                 if (GetModuleFileNameW(NULL, wfilename, MAX_PATH))
217                 {
218                         wcstombs(filename, wfilename, MAX_PATH);
219 #else
220                 if (GetModuleFileName(NULL, filename, MAX_PATH))
221                 {
222 #endif
223                         end = strrchr(filename, L'\\'); /* eliminate the file name which is on the right side */
224                         if(end)
225                                 *end = '\0';
226                 }
227                 setenv("NAVIT_PREFIX", filename, 0);
228         }
229         if (!getenv("HOME"))
230                 setenv("HOME", getenv("NAVIT_PREFIX"), 0);
231         main_setup_environment(2);
232 #endif  /* _WIN32 || _WIN32_WCE */
233
234         if (getenv("LC_ALL"))
235                 dbg(0,"Warning: LC_ALL is set, this might lead to problems (e.g. strange positions from GPS)\n");
236         s = getenv("NAVIT_WID");
237         if (s) {
238                 setenv("SDL_WINDOWID", s, 0);
239         }
240 }
241
242 void
243 main_init_nls(void)
244 {
245 #ifdef ENABLE_NLS
246 #ifdef FORCE_LOCALE
247 #define STRINGIFY2(x) #x
248 #define STRINGIFY(x) STRINGIFY2(x)
249         setlocale(LC_MESSAGES,STRINGIFY(FORCE_LOCALE));
250 #endif
251         bindtextdomain(PACKAGE, getenv("NAVIT_LOCALEDIR"));
252         bind_textdomain_codeset (PACKAGE, "UTF-8");
253         textdomain(PACKAGE);
254 #endif
255 }