From 70eccd48cddc292d212cebfb0ad72811f01fc155 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Thu, 11 Nov 2021 16:14:02 +1000
Subject: Fixes for Haiku


diff --git a/CMakeLists.txt b/CMakeLists.txt
index 705b95f..416f254 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -165,6 +165,19 @@ endif()
 if (APPLE)
     set(MORROWIND_DATA_FILES "./data" CACHE PATH "location of Morrowind data files")
     set(OPENMW_RESOURCE_FILES "../Resources/resources" CACHE PATH "location of OpenMW resources files")
+elseif(HAIKU)
+    # Paths
+    SET(BINDIR "${CMAKE_INSTALL_PREFIX}" CACHE PATH "Where to install binaries")
+    SET(LIBDIR "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}" CACHE PATH "Where to install libraries")
+    SET(DATAROOTDIR "${CMAKE_INSTALL_PREFIX}" CACHE PATH "Sets the root of data directories to a non-default location")
+    SET(GLOBAL_DATA_PATH "${DATAROOTDIR}/games/" CACHE PATH "Set data path prefix")
+    SET(DATADIR "${GLOBAL_DATA_PATH}/openmw" CACHE PATH "Sets the openmw data directories to a non-default location")
+    SET(ICONDIR "${DATAROOTDIR}/pixmaps" CACHE PATH "Set icon dir")
+    SET(LICDIR "${DATAROOTDIR}/licenses/openmw" CACHE PATH "Sets the openmw license directory to a non-default location.")
+    SET(GLOBAL_CONFIG_PATH "${CMAKE_INSTALL_PREFIX}/config/" CACHE PATH "Set config dir prefix")
+    SET(SYSCONFDIR "${GLOBAL_CONFIG_PATH}/openmw" CACHE PATH "Set config dir")
+    set(MORROWIND_DATA_FILES "${CMAKE_INSTALL_PREFIX}/data" CACHE PATH "location of Morrowind data files")
+    set(OPENMW_RESOURCE_FILES "${CMAKE_INSTALL_PREFIX}/resources" CACHE PATH "location of OpenMW resources files")
 elseif(UNIX)
     # Paths
     SET(BINDIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Where to install binaries")
diff --git a/apps/launcher/CMakeLists.txt b/apps/launcher/CMakeLists.txt
index 3018237..af2aecb 100644
--- a/apps/launcher/CMakeLists.txt
+++ b/apps/launcher/CMakeLists.txt
@@ -100,6 +100,7 @@ target_link_libraries(openmw-launcher
     ${SDL2_LIBRARY_ONLY}
     ${OPENAL_LIBRARY}
     components
+    be
 )
 
 target_link_libraries(openmw-launcher Qt5::Widgets Qt5::Core)
diff --git a/apps/launcher/graphicspage.cpp b/apps/launcher/graphicspage.cpp
index ebb031e..abd4d1c 100644
--- a/apps/launcher/graphicspage.cpp
+++ b/apps/launcher/graphicspage.cpp
@@ -11,7 +11,11 @@
 #define MAC_OS_X_VERSION_MIN_REQUIRED __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
 #endif // MAC_OS_X_VERSION_MIN_REQUIRED
 
+#ifdef __HAIKU__
+#include <Screen.h>
+#else
 #include <SDL_video.h>
+#endif
 
 #include <numeric>
 
@@ -59,6 +63,9 @@ bool Launcher::GraphicsPage::setupSDL()
         return false;
     }
 
+#ifdef __HAIKU__
+	int displays = 1;
+#else
     int displays = SDL_GetNumVideoDisplays();
 
     if (displays < 0)
@@ -71,7 +78,7 @@ bool Launcher::GraphicsPage::setupSDL()
         msgBox.exec();
         return false;
     }
-
+#endif
     screenComboBox->clear();
     mResolutionsPerScreen.clear();
     for (int i = 0; i < displays; i++)
@@ -287,9 +294,30 @@ void Launcher::GraphicsPage::saveSettings()
         Settings::Manager::setString("compute scene bounds", "Shadows", cComputeSceneBounds);
 }
 
+#ifdef __HAIKU__
+static QString makeModeString(int w, int h)
+{
+	QString aspect = getAspect(w, h);
+	QString resolution = QString::number(w) + QString(" x ") + QString::number(h);
+	if (aspect == QLatin1String("16:9") || aspect == QLatin1String("16:10")) {
+		resolution.append(" (Wide " + aspect + ")");
+	} else if (aspect == QLatin1String("4:3")) {
+		resolution.append(" (Standard 4:3)");
+	}
+	return resolution;
+}
+#endif
+
 QStringList Launcher::GraphicsPage::getAvailableResolutions(int screen)
 {
     QStringList result;
+#ifdef __HAIKU__
+	BScreen mainScreen(B_MAIN_SCREEN_ID);
+	result.append(makeModeString(640, 480));
+	result.append(makeModeString(800, 600));
+	result.append(makeModeString(1024, 768));
+	result.append(makeModeString(mainScreen.Frame().Width() + 1, mainScreen.Frame().Height() + 1));
+#else
     SDL_DisplayMode mode;
     int modeIndex, modes = SDL_GetNumDisplayModes(screen);
 
@@ -329,7 +357,7 @@ QStringList Launcher::GraphicsPage::getAvailableResolutions(int screen)
 
         result.append(resolution);
     }
-
+#endif
     result.removeDuplicates();
     return result;
 }
diff --git a/apps/launcher/maindialog.cpp b/apps/launcher/maindialog.cpp
index d41cd52..1af391e 100644
--- a/apps/launcher/maindialog.cpp
+++ b/apps/launcher/maindialog.cpp
@@ -175,7 +175,11 @@ Launcher::FirstRunDialogResult Launcher::MainDialog::showFirstRunDialog()
 
         if (msgBox.clickedButton() == wizardButton)
         {
+#ifdef __HAIKU__
+            if (mWizardInvoker->startProcess(QLatin1String("OpenMW-Wizard"), false))
+#else
             if (mWizardInvoker->startProcess(QLatin1String("openmw-wizard"), false))
+#endif
                 return FirstRunDialogResultWizard;
         }
         else if (msgBox.clickedButton() == skipButton)
@@ -406,7 +410,11 @@ bool Launcher::MainDialog::setupGameData()
 
         if (msgBox.clickedButton() == wizardButton)
         {
+#ifdef __HAIKU__
+            if (!mWizardInvoker->startProcess(QLatin1String("OpenMW-Wizard"), false))
+#else
             if (!mWizardInvoker->startProcess(QLatin1String("openmw-wizard"), false))
+#endif
                 return false;
         }
     }
@@ -612,7 +620,11 @@ void Launcher::MainDialog::play()
 
     // Launch the game detached
 
+#ifdef __HAIKU__
+    if (mGameInvoker->startProcess(QLatin1String("OpenMW"), true))
+#else
     if (mGameInvoker->startProcess(QLatin1String("openmw"), true))
+#endif
         return qApp->quit();
 }
 
diff --git a/apps/launcher/sdlinit.cpp b/apps/launcher/sdlinit.cpp
index 4717fb2..9b74a8d 100644
--- a/apps/launcher/sdlinit.cpp
+++ b/apps/launcher/sdlinit.cpp
@@ -4,6 +4,7 @@
 
 bool initSDL()
 {
+#ifndef __HAIKU__
     SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
     SDL_SetMainReady();
     // Required for determining screen resolution and such on the Graphics tab
@@ -13,12 +14,14 @@ bool initSDL()
     }
     signal(SIGINT, SIG_DFL); // We don't want to use the SDL event loop in the launcher,
     // so reset SIGINT which SDL wants to redirect to an SDL_Quit event.
-
+#endif
     return true;
 }
 
 void quitSDL()
 {
+#ifndef __HAIKU__
     // Disconnect from SDL processes
     SDL_Quit();
+#endif
 }
diff --git a/apps/launcher/settingspage.cpp b/apps/launcher/settingspage.cpp
index ca7fd02..5664f74 100644
--- a/apps/launcher/settingspage.cpp
+++ b/apps/launcher/settingspage.cpp
@@ -96,8 +96,11 @@ Launcher::SettingsPage::~SettingsPage()
 void Launcher::SettingsPage::on_wizardButton_clicked()
 {
     mMain->writeSettings();
-
+#ifdef __HAIKU__
+    if (!mWizardInvoker->startProcess(QLatin1String("OpenMW-Wizard"), false))
+#else
     if (!mWizardInvoker->startProcess(QLatin1String("openmw-wizard"), false))
+#endif
         return;
 }
 
diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp
index a06faa4..131ab89 100644
--- a/apps/openmw/engine.cpp
+++ b/apps/openmw/engine.cpp
@@ -516,7 +516,11 @@ void OMW::Engine::createWindow(Settings::Manager& settings)
         pos_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(screen);
     }
 
+#ifdef __HAIKU__
+	Uint32 flags = SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN;
+#else
     Uint32 flags = SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE;
+#endif
     if(fullscreen)
         flags |= SDL_WINDOW_FULLSCREEN;
 
diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp
index 8f7e160..5c8c8d3 100644
--- a/apps/openmw/main.cpp
+++ b/apps/openmw/main.cpp
@@ -298,6 +298,11 @@ int runApplication(int argc, char *argv[])
     boost::filesystem::current_path(binary_path.parent_path());
     setenv("OSG_GL_TEXTURE_STORAGE", "OFF", 0);
 #endif
+#ifdef __HAIKU__
+	boost::filesystem::path binary_path = boost::filesystem::system_complete(boost::filesystem::path(argv[0]));
+	boost::filesystem::current_path(binary_path.parent_path());
+	setenv("OPENMW_DECOMPRESS_TEXTURES", "1", 1);
+#endif
 
     osg::setNotifyHandler(new OSGLogHandler());
     Files::ConfigurationManager cfgMgr;
diff --git a/components/crashcatcher/crashcatcher.cpp b/components/crashcatcher/crashcatcher.cpp
index 86571e1..7073151 100644
--- a/components/crashcatcher/crashcatcher.cpp
+++ b/components/crashcatcher/crashcatcher.cpp
@@ -12,7 +12,9 @@
 
 #include <pthread.h>
 #include <stdbool.h>
+#ifndef __HAIKU__
 #include <sys/ptrace.h>
+#endif
 
 #include <components/debug/debuglog.hpp>
 
@@ -561,6 +563,7 @@ static bool is_debugger_present()
 #endif
 }
 
+#if USE_CRASH_CATCHER
 void crashCatcherInstall(int argc, char **argv, const std::string &crashLogPath)
 {
     if (const auto env = std::getenv("OPENMW_DISABLE_CRASH_CATCHER"))
@@ -577,3 +580,4 @@ void crashCatcherInstall(int argc, char **argv, const std::string &crashLogPath)
             Log(Debug::Info) << "Crash handler installed";
     }
 }
+#endif
diff --git a/components/crashcatcher/crashcatcher.hpp b/components/crashcatcher/crashcatcher.hpp
index b693cca..8d15ef1 100644
--- a/components/crashcatcher/crashcatcher.hpp
+++ b/components/crashcatcher/crashcatcher.hpp
@@ -3,7 +3,7 @@
 
 #include <string>
 
-#if (defined(__APPLE__) || (defined(__linux)  &&  !defined(ANDROID)) || (defined(__unix) &&  !defined(ANDROID)) || defined(__posix))
+#if (defined(__APPLE__) || (defined(__linux)  &&  !defined(ANDROID) && !defined(__HAIKU__)) || (defined(__unix) &&  !defined(ANDROID) && !defined(__HAIKU__)) || defined(__posix))
     #define USE_CRASH_CATCHER 1
 #else
     #define USE_CRASH_CATCHER 0
diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp
index 0e37294..e7733c8 100644
--- a/components/debug/debugging.cpp
+++ b/components/debug/debugging.cpp
@@ -145,6 +145,9 @@ std::ostream& getRawStdout()
 
 int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[], const std::string& appName)
 {
+#ifdef __HAIKU
+	return innerApplication(argc, argv);
+#else
 #if defined _WIN32
     (void)Debug::attachParentConsole();
 #endif
@@ -222,4 +225,5 @@ int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, c
     Debug::CurrentDebugLevel = Debug::NoLevel;
 
     return ret;
+#endif
 }
diff --git a/components/files/fixedpath.hpp b/components/files/fixedpath.hpp
index 2e72b81..97c43f0 100644
--- a/components/files/fixedpath.hpp
+++ b/components/files/fixedpath.hpp
@@ -4,7 +4,7 @@
 #include <string>
 #include <boost/filesystem.hpp>
 
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
+#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__HAIKU__)
 #ifndef ANDROID
     #include <components/files/linuxpath.hpp>
     namespace Files { typedef LinuxPath TargetPathType; }
diff --git a/components/files/linuxpath.cpp b/components/files/linuxpath.cpp
index c3dead2..dc6d2b0 100644
--- a/components/files/linuxpath.cpp
+++ b/components/files/linuxpath.cpp
@@ -1,6 +1,6 @@
 #include "linuxpath.hpp"
 
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
+#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__HAIKU__)
 
 #include <pwd.h>
 #include <unistd.h>
@@ -58,17 +58,32 @@ LinuxPath::LinuxPath(const std::string& application_name)
 
 boost::filesystem::path LinuxPath::getUserConfigPath() const
 {
+#ifdef __HAIKU__
+	boost::filesystem::path path("/boot/home/config/settings");
+	return  path / mName;
+#else
     return getEnv("XDG_CONFIG_HOME", getUserHome() / ".config") / mName;
+#endif
 }
 
 boost::filesystem::path LinuxPath::getUserDataPath() const
 {
+#ifdef __HAIKU__
+	boost::filesystem::path path("/boot/home/config/settings");
+	return  path / mName;
+#else
     return getEnv("XDG_DATA_HOME", getUserHome() / ".local/share") / mName;
+#endif
 }
 
 boost::filesystem::path LinuxPath::getCachePath() const
 {
+#ifdef __HAIKU__
+       boost::filesystem::path path("/boot/home/config/cache");
+       return  path / mName;
+#else
     return getEnv("XDG_CACHE_HOME", getUserHome() / ".cache") / mName;
+#endif
 }
 
 boost::filesystem::path LinuxPath::getGlobalConfigPath() const
diff --git a/components/files/linuxpath.hpp b/components/files/linuxpath.hpp
index 7950157..32ff3d8 100644
--- a/components/files/linuxpath.hpp
+++ b/components/files/linuxpath.hpp
@@ -1,7 +1,7 @@
 #ifndef COMPONENTS_FILES_LINUXPATH_H
 #define COMPONENTS_FILES_LINUXPATH_H
 
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
+#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__HAIKU__)
 
 #include <boost/filesystem.hpp>
 
-- 
2.30.2

