Translated using Weblate (Japanese)
[oweals/minetest.git] / src / porting.cpp
index 8ea61f7ccf030a46dc0a3fd39aa665acbee2037f..d0dfc801fcbd5e47957a52f163f66f0ff1cf73b2 100644 (file)
@@ -75,8 +75,7 @@ bool * signal_handler_killstatus(void)
 
 void sigint_handler(int sig)
 {
-       if(g_killed == false)
-       {
+       if(!g_killed) {
                dstream<<DTIME<<"INFO: sigint_handler(): "
                                <<"Ctrl-C pressed, shutting down."<<std::endl;
 
@@ -86,9 +85,7 @@ void sigint_handler(int sig)
                debug_stacks_print();*/
 
                g_killed = true;
-       }
-       else
-       {
+       } else {
                (void)signal(SIGINT, SIG_DFL);
        }
 }
@@ -126,136 +123,12 @@ BOOL WINAPI event_handler(DWORD sig)
 
 void signal_handler_init(void)
 {
-       SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
+       SetConsoleCtrlHandler((PHANDLER_ROUTINE)event_handler, TRUE);
 }
 
 #endif
 
 
-/*
-       Multithreading support
-*/
-int getNumberOfProcessors()
-{
-#if defined(_SC_NPROCESSORS_ONLN)
-
-       return sysconf(_SC_NPROCESSORS_ONLN);
-
-#elif defined(__FreeBSD__) || defined(__APPLE__)
-
-       unsigned int len, count;
-       len = sizeof(count);
-       return sysctlbyname("hw.ncpu", &count, &len, NULL, 0);
-
-#elif defined(_GNU_SOURCE)
-
-       return get_nprocs();
-
-#elif defined(_WIN32)
-
-       SYSTEM_INFO sysinfo;
-       GetSystemInfo(&sysinfo);
-       return sysinfo.dwNumberOfProcessors;
-
-#elif defined(PTW32_VERSION) || defined(__hpux)
-
-       return pthread_num_processors_np();
-
-#else
-
-       return 1;
-
-#endif
-}
-
-
-#ifndef __ANDROID__
-bool threadBindToProcessor(threadid_t tid, int pnumber)
-{
-#if defined(_WIN32)
-
-       HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, tid);
-       if (!hThread)
-               return false;
-
-       bool success = SetThreadAffinityMask(hThread, 1 << pnumber) != 0;
-
-       CloseHandle(hThread);
-       return success;
-
-#elif (defined(__FreeBSD__) && (__FreeBSD_version >= 702106)) \
-       || defined(__linux) || defined(linux)
-
-       cpu_set_t cpuset;
-
-       CPU_ZERO(&cpuset);
-       CPU_SET(pnumber, &cpuset);
-       return pthread_setaffinity_np(tid, sizeof(cpuset), &cpuset) == 0;
-
-#elif defined(__sun) || defined(sun)
-
-       return processor_bind(P_LWPID, MAKE_LWPID_PTHREAD(tid),
-               pnumber, NULL) == 0;
-
-#elif defined(_AIX)
-
-       return bindprocessor(BINDTHREAD, (tid_t)tid, pnumber) == 0;
-
-#elif defined(__hpux) || defined(hpux)
-
-       pthread_spu_t answer;
-
-       return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP,
-               &answer, pnumber, tid) == 0;
-
-#elif defined(__APPLE__)
-
-       struct thread_affinity_policy tapol;
-
-       thread_port_t threadport = pthread_mach_thread_np(tid);
-       tapol.affinity_tag = pnumber + 1;
-       return thread_policy_set(threadport, THREAD_AFFINITY_POLICY,
-               (thread_policy_t)&tapol, THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS;
-
-#else
-
-       return false;
-
-#endif
-}
-#endif
-
-bool threadSetPriority(threadid_t tid, int prio)
-{
-#if defined(_WIN32)
-
-       HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, tid);
-       if (!hThread)
-               return false;
-
-       bool success = SetThreadPriority(hThread, prio) != 0;
-
-       CloseHandle(hThread);
-       return success;
-
-#else
-
-       struct sched_param sparam;
-       int policy;
-
-       if (pthread_getschedparam(tid, &policy, &sparam) != 0)
-               return false;
-
-       int min = sched_get_priority_min(policy);
-       int max = sched_get_priority_max(policy);
-
-       sparam.sched_priority = min + prio * (max - min) / THREAD_PRIORITY_HIGHEST;
-       return pthread_setschedparam(tid, policy, &sparam) == 0;
-
-#endif
-}
-
-
 /*
        Path mangler
 */
@@ -263,6 +136,8 @@ bool threadSetPriority(threadid_t tid, int prio)
 // Default to RUN_IN_PLACE style relative paths
 std::string path_share = "..";
 std::string path_user = "..";
+std::string path_locale = path_share + DIR_DELIM + "locale";
+
 
 std::string getDataPath(const char *subpath)
 {
@@ -306,14 +181,14 @@ std::string get_sysinfo()
 
        oss << "Windows/" << osvi.dwMajorVersion << "."
                << osvi.dwMinorVersion;
-       if(osvi.szCSDVersion[0])
+       if (osvi.szCSDVersion[0])
                oss << "-" << tmp;
        oss << " ";
        #ifdef _WIN64
        oss << "x86_64";
        #else
        BOOL is64 = FALSE;
-       if(IsWow64Process(GetCurrentProcess(), &is64) && is64)
+       if (IsWow64Process(GetCurrentProcess(), &is64) && is64)
                oss << "x86_64"; // 32-bit app on 64-bit OS
        else
                oss << "x86";
@@ -478,11 +353,11 @@ bool setSystemPaths()
        // Use ".\bin\.."
        path_share = std::string(buf) + "\\..";
 
-       // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME_LOWER>"
+       // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
        DWORD len = GetEnvironmentVariable("APPDATA", buf, sizeof(buf));
        FATAL_ERROR_IF(len == 0 || len > sizeof(buf), "Failed to get APPDATA");
 
-       path_user = std::string(buf) + DIR_DELIM PROJECT_NAME_LOWER;
+       path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME;
        return true;
 }
 
@@ -514,7 +389,7 @@ bool setSystemPaths()
                trylist.push_back(static_sharedir);
 
        trylist.push_back(bindir + DIR_DELIM ".." DIR_DELIM "share"
-               DIR_DELIM PROJECT_NAME_LOWER);
+               DIR_DELIM + PROJECT_NAME);
        trylist.push_back(bindir + DIR_DELIM "..");
 
 #ifdef __ANDROID__
@@ -543,7 +418,7 @@ bool setSystemPaths()
 
 #ifndef __ANDROID__
        path_user = std::string(getenv("HOME")) + DIR_DELIM "."
-               PROJECT_NAME_LOWER;
+               + PROJECT_NAME;
 #endif
 
        return true;
@@ -566,8 +441,9 @@ bool setSystemPaths()
        }
        CFRelease(resources_url);
 
-       path_user = std::string(getenv("HOME")) +
-               "/Library/Application Support/" PROJECT_NAME_LOWER;
+       path_user = std::string(getenv("HOME"))
+               + "/Library/Application Support/"
+               + PROJECT_NAME;
        return true;
 }
 
@@ -578,7 +454,7 @@ bool setSystemPaths()
 {
        path_share = STATIC_SHAREDIR;
        path_user  = std::string(getenv("HOME")) + DIR_DELIM "."
-               PROJECT_NAME_LOWER;
+               + lowercase(PROJECT_NAME);
        return true;
 }
 
@@ -598,7 +474,7 @@ void initializePaths()
                getExecPathFromProcfs(buf, sizeof(buf));
 
        if (success) {
-               pathRemoveFile(buf, '/');
+               pathRemoveFile(buf, DIR_DELIM_CHAR);
                std::string execpath(buf);
 
                path_share = execpath + DIR_DELIM "..";
@@ -629,7 +505,6 @@ void initializePaths()
                path_share = execpath;
                path_user  = execpath;
        }
-
 #else
        infostream << "Using system-wide paths (NOT RUN_IN_PLACE)" << std::endl;
 
@@ -640,6 +515,32 @@ void initializePaths()
 
        infostream << "Detected share path: " << path_share << std::endl;
        infostream << "Detected user path: " << path_user << std::endl;
+
+       bool found_localedir = false;
+#ifdef STATIC_LOCALEDIR
+       if (STATIC_LOCALEDIR[0] && fs::PathExists(STATIC_LOCALEDIR)) {
+               found_localedir = true;
+               path_locale = STATIC_LOCALEDIR;
+               infostream << "Using locale directory " << STATIC_LOCALEDIR << std::endl;
+       } else {
+               path_locale = getDataPath("locale");
+               if (fs::PathExists(path_locale)) {
+                       found_localedir = true;
+                       infostream << "Using in-place locale directory " << path_locale
+                               << " even though a static one was provided "
+                               << "(RUN_IN_PLACE or CUSTOM_LOCALEDIR)." << std::endl;
+               }
+       }
+#else
+       path_locale = getDataPath("locale");
+       if (fs::PathExists(path_locale)) {
+               found_localedir = true;
+       }
+#endif
+       if (!found_localedir) {
+               errorstream << "Couldn't find a locale directory!" << std::endl;
+       }
+
 }
 
 
@@ -751,25 +652,23 @@ const char *getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type)
 
 static float calcDisplayDensity()
 {
-       const charcurrent_display = getenv("DISPLAY");
+       const char *current_display = getenv("DISPLAY");
 
        if (current_display != NULL) {
-                       Display * x11display = XOpenDisplay(current_display);
+               Display *x11display = XOpenDisplay(current_display);
 
-                       if (x11display != NULL) {
-                               /* try x direct */
-                               float dpi_height =
-                                               floor(DisplayHeight(x11display, 0) /
-                                                               (DisplayHeightMM(x11display, 0) * 0.039370) + 0.5);
-                               float dpi_width =
-                                               floor(DisplayWidth(x11display, 0) /
-                                                               (DisplayWidthMM(x11display, 0) * 0.039370) +0.5);
+               if (x11display != NULL) {
+                       /* try x direct */
+                       float dpi_height = floor(DisplayHeight(x11display, 0) /
+                                                       (DisplayHeightMM(x11display, 0) * 0.039370) + 0.5);
+                       float dpi_width = floor(DisplayWidth(x11display, 0) /
+                                                       (DisplayWidthMM(x11display, 0) * 0.039370) + 0.5);
 
-                               XCloseDisplay(x11display);
+                       XCloseDisplay(x11display);
 
-                               return std::max(dpi_height,dpi_width) / 96.0;
-                       }
+                       return std::max(dpi_height,dpi_width) / 96.0;
                }
+       }
 
        /* return manually specified dpi */
        return g_settings->getFloat("screen_dpi")/96.0;