Merge branch 'upstream-svn' into upstream
[navit-package] / navit / main.c
index 7881a3b..0bc92a0 100644 (file)
@@ -65,130 +65,40 @@ static void sigchld(int sig)
 #endif
 }
 
-#if 0
-static gchar *get_home_directory(void)
-{
-       static gchar *homedir = NULL;
-
-       if (homedir) return homedir;
-       homedir = getenv("HOME");
-       if (!homedir)
-       {
-               dbg(0,"Could not find home directory. Using current directory as home directory.\n");
-               homedir =g_strdup(".");
-       } else {
-               homedir=g_strdup(homedir);
-       }
-       return homedir;
-}
-#endif
-
-static GList *navit;
-
-struct iter {
-       GList *list;
-};
-
-struct iter *
-main_iter_new(void)
-{
-       struct iter *ret=g_new0(struct iter, 1);
-       ret->list=navit;
-       return ret;
-}
-
-void
-main_iter_destroy(struct iter *iter)
-{
-       g_free(iter);
-}
-
-struct navit *
-main_get_navit(struct iter *iter)
-{
-       GList *list;
-       struct navit *ret=NULL;
-       if (iter)
-               list=iter->list;
-       else
-               list=navit;
-       if (list) {
-               ret=(struct navit *)(list->data);
-               if (iter)
-                       iter->list=g_list_next(iter->list);
-       }
-       return ret;
-       
-}
-void
-main_add_navit(struct navit *nav)
-{
-       navit=g_list_prepend(navit, nav);
-       callback_list_call_2(cbl, nav, 1);
-}
-
-void
-main_remove_navit(struct navit *nav)
-{
-       navit=g_list_remove(navit, nav);
-       callback_list_call_2(cbl, nav, 0);
-       if (! navit) 
-               event_main_loop_quit();
-}
-
-int
-main_add_attr(struct attr *attr)
-{
-       switch (attr->type)
-       {
-       case attr_callback:
-               callback_list_add(cbl, attr->u.callback);
-               return 1;
-       default:
-               return 0;
-       }
-}
-
-int
-main_remove_attr(struct attr *attr)
-{
-       switch (attr->type)
-       {
-       case attr_callback:
-               callback_list_remove(cbl, attr->u.callback);
-               return 1;
-       default:
-               return 0;
-       }
-}
-
-
 #ifdef HAVE_API_WIN32
 void
 setenv(char *var, char *val, int overwrite)
 {
        char *str=g_strdup_printf("%s=%s",var,val);
-       if (overwrite || !getenv(var)) 
+       if (overwrite || !getenv(var))
                putenv(str);
        g_free(str);
 }
 #endif
 
-static char *environment_vars[][4]={
-       {"NAVIT_LIBDIR",        ":",            "::/navit",             ":/lib"},
-       {"NAVIT_SHAREDIR",      ":",            ":/share/navit",        ":"},
-       {"NAVIT_LOCALEDIR",     ":/../locale",  ":/share/locale",       ":/locale"},
-       {"NAVIT_USER_DATADIR",  ":",            "~/.navit",             ":/data"},
-       {"NAVIT_LOGFILE",       NULL,           NULL,                   ":/navit.log"},
-       {"NAVIT_LIBPREFIX",     "*/.libs/",     NULL,                   NULL},
-       {NULL,                  NULL,           NULL,                   NULL},
+/*
+ * environment_vars[][0:name,1-3:mode]
+ * ':'  replaced with NAVIT_PREFIX
+ * '::' replaced with NAVIT_PREFIX and LIBDIR
+ * '~'  replaced with HOME
+*/
+static char *environment_vars[][5]={
+       {"NAVIT_LIBDIR",      ":",          "::/navit",      ":\\lib",      ":/lib"},
+       {"NAVIT_SHAREDIR",    ":",          ":/share/navit", ":",           ":/share"},
+       {"NAVIT_LOCALEDIR",   ":/../locale",":/share/locale",":\\locale",   ":/locale"},
+       {"NAVIT_USER_DATADIR",":",          "~/.navit",      ":\\data",     ":/home"},
+#if 1
+       {"NAVIT_LOGFILE",     NULL,         NULL,            ":\\navit.log",NULL},
+#endif
+       {"NAVIT_LIBPREFIX",   "*/.libs/",   NULL,            NULL,          NULL},
+       {NULL,                NULL,         NULL,            NULL,          NULL},
 };
 
 static void
 main_setup_environment(int mode)
 {
        int i=0;
-       char *var,*val;
+       char *var,*val,*homedir;
        while ((var=environment_vars[i][0])) {
                val=environment_vars[i][mode+1];
                if (val) {
@@ -200,7 +110,10 @@ main_setup_environment(int mode)
                                        val=g_strdup_printf("%s%s", getenv("NAVIT_PREFIX"), val+1);
                                break;
                        case '~':
-                               val=g_strdup_printf("%s%s", getenv("HOME"), val+1);
+                               homedir=getenv("HOME");
+                               if (!homedir)
+                                       homedir="./";
+                               val=g_strdup_printf("%s%s", homedir, val+1);
                                break;
                        default:
                                val=g_strdup(val);
@@ -213,6 +126,40 @@ main_setup_environment(int mode)
        }
 }
 
+#ifdef HAVE_API_WIN32_BASE
+char *nls_table[][3]={
+       {"DAN","DNK","da_DK"},
+       {"DEU","DEU","de_DE"},
+       {"DEA","AUT","de_AT"},
+       {"ENU","USA","en_US"},
+       {"FRA","FRA","fr_FR"},
+       {"RUS","RUS","ru_RU"},
+       {NULL,NULL,NULL},
+};
+
+static void
+win_set_nls(void)
+{
+       wchar_t wcountry[32],wlang[32]; 
+       char country[32],lang[32];
+       int i=0;
+
+       GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME, wlang, sizeof(wlang));
+       WideCharToMultiByte(CP_ACP,0,wlang,-1,lang,sizeof(lang),NULL,NULL);
+       GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, wcountry, sizeof(wcountry));
+       WideCharToMultiByte(CP_ACP,0,wcountry,-1,country,sizeof(country),NULL,NULL);
+       while (nls_table[i][0]) {
+               if (!strcmp(nls_table[i][0], lang) && !(strcmp(nls_table[i][1], country))) {
+                       dbg(1,"Setting LANG=%s for Lang %s Country %s\n",nls_table[i][2], lang, country);
+                       setenv("LANG",nls_table[i][2],0);
+                       return;
+               }
+               i++;
+       }
+       dbg(1,"Lang %s Country %s not found\n",lang,country);
+}
+#endif
+
 void
 main_init(char *program)
 {
@@ -223,37 +170,23 @@ main_init(char *program)
        signal(SIGCHLD, sigchld);
 #endif
        cbl=callback_list_new();
+#ifdef HAVE_API_WIN32_BASE
+       win_set_nls();
+#endif
        setenv("LC_NUMERIC","C",1);
        setlocale(LC_ALL,"");
        setlocale(LC_NUMERIC,"C");
+#if !defined _WIN32 && !defined _WIN32_WCE
        if (file_exists("navit.c") || file_exists("navit.o") || file_exists("navit.lo")) {
                char buffer[PATH_MAX];
                printf(_("Running from source directory\n"));
-               getcwd(buffer, PATH_MAX);
+               getcwd(buffer, PATH_MAX);               /* libc of navit returns "dummy" */
                setenv("NAVIT_PREFIX", buffer, 0);
                main_setup_environment(0);
        } else {
                if (!getenv("NAVIT_PREFIX")) {
-                       int progpath_len;
-#ifdef HAVE_API_WIN32_BASE
-                       wchar_t filename[MAX_PATH + 1];
-                       char program[MAX_PATH + 1];
-                       char *cp;
-                       int sz;
-#ifdef HAVE_API_WIN32
-                       char *progpath="\\bin\\navit.exe";
-                       sz = GetModuleFileNameW(NULL, filename, MAX_PATH);
-#else
-                       char *progpath="\\navit.exe";
-                       sz = GetModuleFileName(NULL, filename, MAX_PATH);
-#endif
-                       if (sz > 0) 
-                               wcstombs(program, filename, sz + 1);
-                       else 
-                               strcpy(program, PREFIX);
-#else
+               int progpath_len;
                        char *progpath="/bin/navit";
-#endif
                        l=strlen(program);
                        progpath_len=strlen(progpath);
                        if (l > progpath_len && !strcmp(program+l-progpath_len,progpath)) {
@@ -265,18 +198,42 @@ main_init(char *program)
                                g_free(s);
                        } else
                                setenv("NAVIT_PREFIX", PREFIX, 0);
-               } 
-#ifdef HAVE_API_WIN32_BASE
-               setenv("HOME", getenv("NAVIT_PREFIX"), 0);
+               }
+#ifdef HAVE_API_ANDROID
+               main_setup_environment(3);
+#else
+               main_setup_environment(1);
 #endif
+       }
+
+#else          /* _WIN32 || _WIN32_WCE */
+       if (!getenv("NAVIT_PREFIX"))
+       {
+               char  filename[MAX_PATH + 1],
+                    *end;
 
-#ifdef HAVE_API_WIN32_CE
-               main_setup_environment(2);
+               *filename = '\0';
+#ifdef _UNICODE                /* currently for wince */
+               wchar_t wfilename[MAX_PATH + 1];
+               if (GetModuleFileNameW(NULL, wfilename, MAX_PATH))
+               {
+                       wcstombs(filename, wfilename, MAX_PATH);
 #else
-               main_setup_environment(1);
+               if (GetModuleFileName(NULL, filename, MAX_PATH))
+               {
 #endif
+                       end = strrchr(filename, L'\\'); /* eliminate the file name which is on the right side */
+                       if(end)
+                               *end = '\0';
+               }
+               setenv("NAVIT_PREFIX", filename, 0);
        }
-       if (getenv("LC_ALL")) 
+       if (!getenv("HOME"))
+               setenv("HOME", getenv("NAVIT_PREFIX"), 0);
+       main_setup_environment(2);
+#endif /* _WIN32 || _WIN32_WCE */
+
+       if (getenv("LC_ALL"))
                dbg(0,"Warning: LC_ALL is set, this might lead to problems (e.g. strange positions from GPS)\n");
        s = getenv("NAVIT_WID");
        if (s) {