/* Minetest Copyright (C) 2013 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /* Random portability stuff See comments in porting.h */ #include "porting.h" #if defined(__FreeBSD__) #include #include #elif defined(_WIN32) #include #endif #if !defined(_WIN32) #include #include #endif #include "config.h" #include "debug.h" #include "filesys.h" #include "log.h" #include "util/string.h" #include "main.h" #include "settings.h" #include namespace porting { /* Signal handler (grabs Ctrl-C on POSIX systems) */ bool g_killed = false; bool * signal_handler_killstatus(void) { return &g_killed; } #if !defined(_WIN32) // POSIX #include void sigint_handler(int sig) { if(g_killed == false) { dstream< BOOL WINAPI event_handler(DWORD sig) { switch(sig) { case CTRL_C_EVENT: case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT: case CTRL_SHUTDOWN_EVENT: if(g_killed == false) { dstream<= 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 */ // Default to RUN_IN_PLACE style relative paths std::string path_share = ".."; std::string path_user = ".."; std::string getDataPath(const char *subpath) { return path_share + DIR_DELIM + subpath; } void pathRemoveFile(char *path, char delim) { // Remove filename and path delimiter int i; for(i = strlen(path)-1; i>=0; i--) { if(path[i] == delim) break; } path[i] = 0; } bool detectMSVCBuildDir(char *c_path) { std::string path(c_path); const char *ends[] = {"bin\\Release", "bin\\Build", NULL}; return (removeStringEnd(path, ends) != ""); } std::string get_sysinfo() { #ifdef _WIN32 OSVERSIONINFO osvi; std::ostringstream oss; std::string tmp; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); tmp = osvi.szCSDVersion; std::replace(tmp.begin(), tmp.end(), ' ', '_'); oss << "Windows/" << osvi.dwMajorVersion << "." << osvi.dwMinorVersion; if(osvi.szCSDVersion[0]) oss << "-" << tmp; oss << " "; #ifdef _WIN64 oss << "x86_64"; #else BOOL is64 = FALSE; if(IsWow64Process(GetCurrentProcess(), &is64) && is64) oss << "x86_64"; // 32-bit app on 64-bit OS else oss << "x86"; #endif return oss.str(); #else struct utsname osinfo; uname(&osinfo); return std::string(osinfo.sysname) + "/" + osinfo.release + " " + osinfo.machine; #endif } void initializePaths() { #if RUN_IN_PLACE /* Use relative paths if RUN_IN_PLACE */ infostream<<"Using relative paths (RUN_IN_PLACE)"<" len = GetEnvironmentVariable("APPDATA", buf, buflen); assert(len < buflen); path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME; /* Linux */ #elif defined(linux) // Get path to executable std::string bindir = ""; { char buf[BUFSIZ]; memset(buf, 0, BUFSIZ); if (readlink("/proc/self/exe", buf, BUFSIZ-1) == -1) { errorstream << "Unable to read bindir "<< std::endl; #ifndef __ANDROID__ assert("Unable to read bindir" == 0); #endif } else { pathRemoveFile(buf, '/'); bindir = buf; } } // Find share directory from these. // It is identified by containing the subdirectory "builtin". std::list trylist; std::string static_sharedir = STATIC_SHAREDIR; if(static_sharedir != "" && static_sharedir != ".") trylist.push_back(static_sharedir); trylist.push_back( bindir + DIR_DELIM + ".." + DIR_DELIM + "share" + DIR_DELIM + PROJECT_NAME); trylist.push_back(bindir + DIR_DELIM + ".."); #ifdef __ANDROID__ trylist.push_back(DIR_DELIM "sdcard" DIR_DELIM PROJECT_NAME); #endif for(std::list::const_iterator i = trylist.begin(); i != trylist.end(); i++) { const std::string &trypath = *i; if(!fs::PathExists(trypath) || !fs::PathExists(trypath + DIR_DELIM + "builtin")){ dstream<<"WARNING: system-wide share not found at \"" <getVideoDriver()->getScreenSize(); } #ifndef __ANDROID__ float getDisplayDensity() { float gui_scaling = g_settings->getFloat("gui_scaling"); // using Y here feels like a bug, this needs to be discussed later! if (getWindowSize().Y <= 800) { return (2.0/3.0) * gui_scaling; } if (getWindowSize().Y <= 1280) { return 1.0 * gui_scaling; } return (4.0/3.0) * gui_scaling; } v2u32 getDisplaySize() { IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL); core::dimension2d deskres = nulldevice->getVideoModeList()->getDesktopResolution(); nulldevice -> drop(); return deskres; } #endif #endif } //namespace porting