Add:win32:Make all directories relative to the navit.exe path
[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 "xmlconfig.h"
42 #include "item.h"
43 #include "coord.h"
44 #include "route.h"
45 #include "navigation.h"
46 #include "event.h"
47 #include "navit_nls.h"
48
49 struct map_data *map_data_default;
50
51 static void sigchld(int sig)
52 {
53 #if !defined(_WIN32) && !defined(__CEGCC__)
54         int status;
55         while (waitpid(-1, &status, WNOHANG) > 0);
56 #endif
57 }
58
59
60 static gchar *get_home_directory(void)
61 {
62         static gchar *homedir = NULL;
63
64         if (homedir) return homedir;
65         homedir = getenv("HOME");
66         if (!homedir)
67         {
68                 dbg(0,"Could not find home directory. Using current directory as home directory.");
69                 homedir = ".";
70         } else {
71                 homedir=g_strdup(homedir);
72         }
73         return homedir;
74 }
75
76 static GList *navit;
77
78 struct iter {
79         GList *list;
80 };
81
82 struct iter *
83 main_iter_new(void)
84 {
85         struct iter *ret=g_new0(struct iter, 1);
86         ret->list=navit;
87         return ret;
88 }
89
90 void
91 main_iter_destroy(struct iter *iter)
92 {
93         g_free(iter);
94 }
95
96 struct navit *
97 main_get_navit(struct iter *iter)
98 {
99         GList *list;
100         struct navit *ret=NULL;
101         if (iter)
102                 list=iter->list;
103         else
104                 list=navit;
105         if (list) {
106                 ret=(struct navit *)(list->data);
107                 if (iter)
108                         iter->list=g_list_next(iter->list);
109         }
110         return ret;
111         
112 }
113 void
114 main_add_navit(struct navit *nav)
115 {
116         navit=g_list_prepend(navit, nav);
117 }
118
119 void
120 main_remove_navit(struct navit *nav)
121 {
122         navit=g_list_remove(navit, nav);
123         if (! navit) 
124                 event_main_loop_quit();
125 }
126
127 void
128 main_init(char *program)
129 {
130         char *s;
131         int l;
132
133 #ifndef _WIN32
134         signal(SIGCHLD, sigchld);
135 #endif
136
137         setenv("LC_NUMERIC","C",1);
138         setlocale(LC_ALL,"");
139         setlocale(LC_NUMERIC,"C");
140         if (file_exists("navit.c") || file_exists("navit.o") || file_exists("navit.lo")) {
141                 char buffer[PATH_MAX];
142                 printf(_("Running from source directory\n"));
143                 getcwd(buffer, PATH_MAX);
144                 setenv("NAVIT_PREFIX", buffer, 0);
145                 setenv("NAVIT_LIBDIR", buffer, 0);
146                 setenv("NAVIT_SHAREDIR", buffer, 0);
147                 setenv("NAVIT_LIBPREFIX", "*/.libs/", 0);
148                 s=g_strdup_printf("%s/../locale", buffer);
149                 setenv("NAVIT_LOCALEDIR", s, 0);
150                 g_free(s);
151                 setenv("NAVIT_USER_DATADIR", "./", 0);
152         } else {
153                 if (!getenv("NAVIT_PREFIX")) {
154                         l=strlen(program);
155                         if (l > 10 && !strcmp(program+l-10,"/bin/navit")) {
156                                 s=g_strdup(program);
157                                 s[l-10]='\0';
158                                 if (strcmp(s, PREFIX))
159                                         printf(_("setting '%s' to '%s'\n"), "NAVIT_PREFIX", s);
160                                 setenv("NAVIT_PREFIX", s, 0);
161                                 g_free(s);
162                         } else
163                                 setenv("NAVIT_PREFIX", PREFIX, 0);
164                 }
165 #ifdef _WIN32
166                 s=g_strdup_printf("locale");
167 #else
168                 s=g_strdup_printf("%s/share/locale", getenv("NAVIT_PREFIX"));
169 #endif
170                 setenv("NAVIT_LOCALEDIR", s, 0);
171                 g_free(s);
172 #ifdef _WIN32
173                 s=g_strdup_printf(".");
174 #else
175                 s=g_strdup_printf("%s/share/navit", getenv("NAVIT_PREFIX"));
176 #endif
177                 setenv("NAVIT_SHAREDIR", s, 0);
178                 g_free(s);
179                 s=g_strdup_printf("%s/lib/navit", getenv("NAVIT_PREFIX"));
180                 setenv("NAVIT_LIBDIR", s, 0);
181                 g_free(s);
182                 setenv("NAVIT_USER_DATADIR", g_strjoin(NULL, get_home_directory(), "/.navit/", NULL), 0);
183         }
184         if (getenv("LC_ALL")) 
185                 dbg(0,"Warning: LC_ALL is set, this might lead to problems\n");
186         s = getenv("NAVIT_WID");
187         if (s) {
188                 setenv("SDL_WINDOWID", s, 0);
189         }
190 }
191
192 void
193 main_init_nls(void)
194 {
195 #ifdef ENABLE_NLS
196         bindtextdomain(PACKAGE, getenv("NAVIT_LOCALEDIR"));
197         bind_textdomain_codeset (PACKAGE, "UTF-8");
198         textdomain(PACKAGE);
199 #endif
200 }