6e91fb1b56ea90490f1401514385d52eafb2152f
[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 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         GError *error = NULL;
131         char *config_file = NULL;
132         char *s;
133         int l;
134         int opt;
135     GList *list = NULL, *li;
136
137
138 #ifndef _WIN32
139         signal(SIGCHLD, sigchld);
140 #endif
141
142         setenv("LC_NUMERIC","C",1);
143         setlocale(LC_ALL,"");
144         setlocale(LC_NUMERIC,"C");
145         if (file_exists("navit.c") || file_exists("navit.o") || file_exists("navit.lo")) {
146                 char buffer[PATH_MAX];
147                 printf(_("Running from source directory\n"));
148                 getcwd(buffer, PATH_MAX);
149                 setenv("NAVIT_PREFIX", buffer, 0);
150                 setenv("NAVIT_LIBDIR", buffer, 0);
151                 setenv("NAVIT_SHAREDIR", buffer, 0);
152                 setenv("NAVIT_LIBPREFIX", "*/.libs/", 0);
153                 s=g_strdup_printf("%s/../locale", buffer);
154                 setenv("NAVIT_LOCALEDIR", s, 0);
155                 g_free(s);
156                 setenv("NAVIT_USER_DATADIR", "./", 0);
157         } else {
158                 if (!getenv("NAVIT_PREFIX")) {
159                         l=strlen(program);
160                         if (l > 10 && !strcmp(program+l-10,"/bin/navit")) {
161                                 s=g_strdup(program);
162                                 s[l-10]='\0';
163                                 if (strcmp(s, PREFIX))
164                                         printf(_("setting '%s' to '%s'\n"), "NAVIT_PREFIX", s);
165                                 setenv("NAVIT_PREFIX", s, 0);
166                                 g_free(s);
167                         } else
168                                 setenv("NAVIT_PREFIX", PREFIX, 0);
169                 }
170 #ifdef _WIN32
171                 s=g_strdup_printf("locale");
172 #else
173                 s=g_strdup_printf("%s/share/locale", getenv("NAVIT_PREFIX"));
174 #endif
175                 setenv("NAVIT_LOCALEDIR", s, 0);
176                 g_free(s);
177 #ifdef _WIN32
178                 s=g_strdup_printf(".");
179 #else
180                 s=g_strdup_printf("%s/share/navit", getenv("NAVIT_PREFIX"));
181 #endif
182                 setenv("NAVIT_SHAREDIR", s, 0);
183                 g_free(s);
184                 s=g_strdup_printf("%s/lib/navit", getenv("NAVIT_PREFIX"));
185                 setenv("NAVIT_LIBDIR", s, 0);
186                 g_free(s);
187                 setenv("NAVIT_USER_DATADIR", g_strjoin(NULL, get_home_directory(), "/.navit/", NULL), 0);
188         }
189         if (getenv("LC_ALL")) 
190                 dbg(0,"Warning: LC_ALL is set, this might lead to problems\n");
191         s = getenv("NAVIT_WID");
192         if (s) {
193                 setenv("SDL_WINDOWID", s, 0);
194         }
195 }
196
197 void
198 main_init_nls(void)
199 {
200         bindtextdomain(PACKAGE, getenv("NAVIT_LOCALEDIR"));
201         bind_textdomain_codeset (PACKAGE, "UTF-8");
202         textdomain(PACKAGE);
203 }