#if !defined(_WIN32) // POSIX
#include <signal.h>
-void sigint_handler(int sig)
+void signal_handler(int sig)
{
if (!g_killed) {
- dstream << "INFO: sigint_handler(): "
- << "Ctrl-C pressed, shutting down." << std::endl;
+ if (sig == SIGINT) {
+ dstream << "INFO: signal_handler(): "
+ << "Ctrl-C pressed, shutting down." << std::endl;
+ } else if (sig == SIGTERM) {
+ dstream << "INFO: signal_handler(): "
+ << "got SIGTERM, shutting down." << std::endl;
+ }
// Comment out for less clutter when testing scripts
/*dstream << "INFO: sigint_handler(): "
g_killed = true;
} else {
- (void)signal(SIGINT, SIG_DFL);
+ (void)signal(sig, SIG_DFL);
}
}
void signal_handler_init(void)
{
- (void)signal(SIGINT, sigint_handler);
+ (void)signal(SIGINT, signal_handler);
+ (void)signal(SIGTERM, signal_handler);
}
#else // _WIN32
{
const char *ends[] = {
"bin\\Release",
+ "bin\\MinSizeRel",
+ "bin\\RelWithDebInfo",
"bin\\Debug",
"bin\\Build",
NULL
//// Linux
-#elif defined(linux) || defined(__linux) || defined(__linux__)
+#elif defined(__linux__)
bool getCurrentExecPath(char *buf, size_t len)
{
//// Linux
-#elif defined(linux) || defined(__linux)
+#elif defined(__linux__)
bool setSystemPaths()
{
#endif
for (std::list<std::string>::const_iterator
- i = trylist.begin(); i != trylist.end(); i++) {
+ i = trylist.begin(); i != trylist.end(); ++i) {
const std::string &trypath = *i;
if (!fs::PathExists(trypath) ||
!fs::PathExists(trypath + DIR_DELIM + "builtin")) {
path_share = execpath;
path_user = execpath;
}
+ path_cache = path_user + DIR_DELIM + "cache";
#else
infostream << "Using system-wide paths (NOT RUN_IN_PLACE)" << std::endl;
// Initialize path_cache
// First try $XDG_CACHE_HOME/PROJECT_NAME
const char *cache_dir = getenv("XDG_CACHE_HOME");
+ const char *home_dir = getenv("HOME");
if (cache_dir) {
path_cache = std::string(cache_dir) + DIR_DELIM + PROJECT_NAME;
- } else {
+ } else if (home_dir) {
// Then try $HOME/.cache/PROJECT_NAME
- const char *home_dir = getenv("HOME");
- if (home_dir) {
- path_cache = std::string(home_dir) + DIR_DELIM + ".cache"
- + DIR_DELIM + PROJECT_NAME;
- }
- // If neither works, leave it at $PATH_USER/cache
+ path_cache = std::string(home_dir) + DIR_DELIM + ".cache"
+ + DIR_DELIM + PROJECT_NAME;
+ } else {
+ // If neither works, use $PATH_USER/cache
+ path_cache = path_user + DIR_DELIM + "cache";
}
// Migrate cache folder to new location if possible
migrateCachePath();
infostream << "Detected user path: " << path_user << std::endl;
infostream << "Detected cache path: " << path_cache << std::endl;
+#ifdef USE_GETTEXT
bool found_localedir = false;
-#ifdef STATIC_LOCALEDIR
+# ifdef STATIC_LOCALEDIR
if (STATIC_LOCALEDIR[0] && fs::PathExists(STATIC_LOCALEDIR)) {
found_localedir = true;
path_locale = STATIC_LOCALEDIR;
<< "(RUN_IN_PLACE or CUSTOM_LOCALEDIR)." << std::endl;
}
}
-#else
+# else
path_locale = getDataPath("locale");
if (fs::PathExists(path_locale)) {
found_localedir = true;
}
-#endif
+# endif
if (!found_localedir) {
- errorstream << "Couldn't find a locale directory!" << std::endl;
+ warningstream << "Couldn't find a locale directory!" << std::endl;
}
+#endif // USE_GETTEXT
}
#endif
}
+bool setWindowIcon(IrrlichtDevice *device)
+{
+#if defined(XORG_USED)
+# if RUN_IN_PLACE
+ return setXorgWindowIconFromPath(device,
+ path_share + "/misc/" PROJECT_NAME "-xorg-icon-128.png");
+# else
+ // We have semi-support for reading in-place data if we are
+ // compiled with RUN_IN_PLACE. Don't break with this and
+ // also try the path_share location.
+ return
+ setXorgWindowIconFromPath(device,
+ ICON_DIR "/hicolor/128x128/apps/" PROJECT_NAME ".png") ||
+ setXorgWindowIconFromPath(device,
+ path_share + "/misc/" PROJECT_NAME "-xorg-icon-128.png");
+# endif
+#elif defined(_WIN32)
+ const video::SExposedVideoData exposedData = device->getVideoDriver()->getExposedVideoData();
+ HWND hWnd; // Window handle
+
+ switch (device->getVideoDriver()->getDriverType()) {
+ case video::EDT_DIRECT3D8:
+ hWnd = reinterpret_cast<HWND>(exposedData.D3D8.HWnd);
+ break;
+ case video::EDT_DIRECT3D9:
+ hWnd = reinterpret_cast<HWND>(exposedData.D3D9.HWnd);
+ break;
+ case video::EDT_OPENGL:
+ hWnd = reinterpret_cast<HWND>(exposedData.OpenGLWin32.HWnd);
+ break;
+ default:
+ return false;
+ }
+
+ // Load the ICON from resource file
+ const HICON hicon = LoadIcon(
+ GetModuleHandle(NULL),
+ MAKEINTRESOURCE(130) // The ID of the ICON defined in winresource.rc
+ );
+
+ if (hicon) {
+ SendMessage(hWnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(hicon));
+ SendMessage(hWnd, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(hicon));
+ return true;
+ }
+ return false;
+#else
+ return false;
+#endif
+}
+
+bool setXorgWindowIconFromPath(IrrlichtDevice *device,
+ const std::string &icon_file)
+{
+#ifdef XORG_USED
+
+ video::IVideoDriver *v_driver = device->getVideoDriver();
+
+ video::IImageLoader *image_loader = NULL;
+ u32 cnt = v_driver->getImageLoaderCount();
+ for (u32 i = 0; i < cnt; i++) {
+ if (v_driver->getImageLoader(i)->isALoadableFileExtension(icon_file.c_str())) {
+ image_loader = v_driver->getImageLoader(i);
+ break;
+ }
+ }
+
+ if (!image_loader) {
+ warningstream << "Could not find image loader for file '"
+ << icon_file << "'" << std::endl;
+ return false;
+ }
+
+ io::IReadFile *icon_f = device->getFileSystem()->createAndOpenFile(icon_file.c_str());
+
+ if (!icon_f) {
+ warningstream << "Could not load icon file '"
+ << icon_file << "'" << std::endl;
+ return false;
+ }
+
+ video::IImage *img = image_loader->loadImage(icon_f);
+
+ if (!img) {
+ warningstream << "Could not load icon file '"
+ << icon_file << "'" << std::endl;
+ icon_f->drop();
+ return false;
+ }
+
+ u32 height = img->getDimension().Height;
+ u32 width = img->getDimension().Width;
+
+ size_t icon_buffer_len = 2 + height * width;
+ long *icon_buffer = new long[icon_buffer_len];
+
+ icon_buffer[0] = width;
+ icon_buffer[1] = height;
+
+ for (u32 x = 0; x < width; x++) {
+ for (u32 y = 0; y < height; y++) {
+ video::SColor col = img->getPixel(x, y);
+ long pixel_val = 0;
+ pixel_val |= (u8)col.getAlpha() << 24;
+ pixel_val |= (u8)col.getRed() << 16;
+ pixel_val |= (u8)col.getGreen() << 8;
+ pixel_val |= (u8)col.getBlue();
+ icon_buffer[2 + x + y * width] = pixel_val;
+ }
+ }
+
+ img->drop();
+ icon_f->drop();
+
+ const video::SExposedVideoData &video_data = v_driver->getExposedVideoData();
+
+ Display *x11_dpl = (Display *)video_data.OpenGLLinux.X11Display;
+
+ if (x11_dpl == NULL) {
+ warningstream << "Could not find x11 display for setting its icon."
+ << std::endl;
+ delete [] icon_buffer;
+ return false;
+ }
+
+ Window x11_win = (Window)video_data.OpenGLLinux.X11Window;
+
+ Atom net_wm_icon = XInternAtom(x11_dpl, "_NET_WM_ICON", False);
+ Atom cardinal = XInternAtom(x11_dpl, "CARDINAL", False);
+ XChangeProperty(x11_dpl, x11_win,
+ net_wm_icon, cardinal, 32,
+ PropModeReplace, (const unsigned char *)icon_buffer,
+ icon_buffer_len);
+
+ delete [] icon_buffer;
+
+#endif
+ return true;
+}
////
//// Video/Display Information (Client-only)
#endif
+void attachOrCreateConsole(void)
+{
+#ifdef _WIN32
+ static bool consoleAllocated = false;
+ const bool redirected = (_fileno(stdout) == -2 || _fileno(stdout) == -1); // If output is redirected to e.g a file
+ if (!consoleAllocated && redirected && (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())) {
+ freopen("CONOUT$", "w", stdout);
+ freopen("CONOUT$", "w", stderr);
+ consoleAllocated = true;
+ }
+#endif
+}
+
+// Load performance counter frequency only once at startup
+#ifdef _WIN32
+
+inline double get_perf_freq()
+{
+ LARGE_INTEGER freq;
+ QueryPerformanceFrequency(&freq);
+ return freq.QuadPart;
+}
+
+double perf_freq = get_perf_freq();
+
+#endif
+
} //namespace porting