/* 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 #if !defined(_WIN32) && !defined(__APPLE__) && \ !defined(__ANDROID__) && !defined(SERVER) #define XORG_USED #endif #ifdef XORG_USED #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\\Debug", "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(path_user); #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 \"" <res_name = (char *)name.c_str(); classhint->res_class = (char *)name.c_str(); XSetClassHint((Display *)video_data.OpenGLLinux.X11Display, video_data.OpenGLLinux.X11Window, classhint); XFree(classhint); #endif } #ifndef SERVER v2u32 getWindowSize() { return device->getVideoDriver()->getScreenSize(); } std::vector > getVideoModes() { std::vector > mlist; video::IVideoModeList *modelist = device->getVideoModeList(); u32 num_modes = modelist->getVideoModeCount(); for (u32 i = 0; i != num_modes; i++) { core::dimension2d mode_res = modelist->getVideoModeResolution(i); s32 mode_depth = modelist->getVideoModeDepth(i); mlist.push_back(core::vector3d(mode_res.Width, mode_res.Height, mode_depth)); } return mlist; } std::vector getSupportedVideoDrivers() { std::vector drivers; for (int i = 0; i != irr::video::EDT_COUNT; i++) { if (irr::IrrlichtDevice::isDriverSupported((irr::video::E_DRIVER_TYPE)i)) drivers.push_back((irr::video::E_DRIVER_TYPE)i); } return drivers; } const char *getVideoDriverName(irr::video::E_DRIVER_TYPE type) { static const char *driver_ids[] = { "null", "software", "burningsvideo", "direct3d8", "direct3d9", "opengl", "ogles1", "ogles2", }; return driver_ids[type]; } const char *getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type) { static const char *driver_names[] = { "NULL Driver", "Software Renderer", "Burning's Video", "Direct3D 8", "Direct3D 9", "OpenGL", "OpenGL ES1", "OpenGL ES2", }; return driver_names[type]; } #ifndef __ANDROID__ #ifdef XORG_USED static float calcDisplayDensity() { const char* current_display = getenv("DISPLAY"); if (current_display != NULL) { 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); XCloseDisplay(x11display); return std::max(dpi_height,dpi_width) / 96.0; } } /* return manually specified dpi */ return g_settings->getFloat("screen_dpi")/96.0; } float getDisplayDensity() { static float cached_display_density = calcDisplayDensity(); return cached_display_density; } #else float getDisplayDensity() { return g_settings->getFloat("screen_dpi")/96.0; } #endif v2u32 getDisplaySize() { IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL); core::dimension2d deskres = nulldevice->getVideoModeList()->getDesktopResolution(); nulldevice -> drop(); return deskres; } #endif #endif } //namespace porting