From 9e263bbfa015b600b0dfd7955631057263fcaec7 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Mon, 9 Sep 2019 00:15:38 +1000
Subject: Fix build for Haiku


diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3aef3ba..443afb8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -205,7 +205,11 @@ CHECK_INCLUDE_FILE_CXX(sys/timerfd.h HAVE_SYS_TIMERFD_H)
 CHECK_INCLUDE_FILE_CXX(sys/socket.h HAVE_SYS_SOCKET_H)
 CHECK_INCLUDE_FILE_CXX(sys/eventfd.h HAVE_SYS_EVENTFD_H)
 CHECK_INCLUDE_FILE_CXX(sys/file.h HAVE_SYS_FILE_H)
+if(NOT HAIKU)
 CHECK_INCLUDE_FILES_CXX("X11/Xlib.h;X11/extensions/Xrandr.h" HAVE_X11_EXTENSIONS_XRANDR_H)
+else()
+set(X11_LIBRARIES "")
+endif()
 CHECK_CXX_SOURCE_COMPILES("#include <getopt.h>\nint main(int argc, char * argv[]) { getopt_long(argc, argv, \"\", 0, 0); }" HAVE_GETOPT_H)
 
 ############################################################################
@@ -1053,9 +1057,12 @@ endif()
 if(HAVE_PTHREAD)
 	target_link_libraries(libmisc pthread)
 endif()
-if(UNIX AND NOT APPLE)
+if(UNIX AND NOT APPLE AND NOT HAIKU)
 	target_link_libraries(libmisc rt)
 endif()
+if(HAIKU)
+	target_link_libraries(libmisc network)
+endif()
 
 add_library(libc4script STATIC
 src/C4Include.cpp
diff --git a/config.h.cmake b/config.h.cmake
index e2cec4c..cfe20bb 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -107,7 +107,11 @@
 
 /* Path to data directory */
 #ifdef WITH_APPDIR_INSTALLATION
+#ifdef __HAIKU__
+#define OC_SYSTEM_DATA_DIR "data"
+#else
 #define OC_SYSTEM_DATA_DIR "../share/games/openclonk"
+#endif
 #else
 #define OC_SYSTEM_DATA_DIR "${CMAKE_INSTALL_PREFIX}/share/games/openclonk"
 #endif
diff --git a/src/config/C4Config.cpp b/src/config/C4Config.cpp
index ed44d42..d045723 100644
--- a/src/config/C4Config.cpp
+++ b/src/config/C4Config.cpp
@@ -110,8 +110,8 @@ void C4ConfigGraphics::CompileFunc(StdCompiler *pComp)
 {
 	pComp->Value(mkNamingAdapt(ResX,                  "ResolutionX",         -1             ,false, true));
 	pComp->Value(mkNamingAdapt(ResY,                  "ResolutionY",         -1             ,false, true));
-	pComp->Value(mkNamingAdapt(WindowX,               "WindowX",              800           ,false, true));
-	pComp->Value(mkNamingAdapt(WindowY,               "WindowY",              600           ,false, true));
+	pComp->Value(mkNamingAdapt(WindowX,               "WindowX",              640           ,false, true));
+	pComp->Value(mkNamingAdapt(WindowY,               "WindowY",              480           ,false, true));
 	pComp->Value(mkNamingAdapt(RefreshRate,           "RefreshRate",          0             ));
 	pComp->Value(mkNamingAdapt(SplitscreenDividers,   "SplitscreenDividers",  1             ));
 	pComp->Value(mkNamingAdapt(ShowStartupMessages,   "ShowStartupMessages",  1             ,false, true));
@@ -121,14 +121,14 @@ void C4ConfigGraphics::CompileFunc(StdCompiler *pComp)
 	pComp->Value(mkNamingAdapt(ShowClock,             "ShowClock",            0             ,false, true));
 	pComp->Value(mkNamingAdapt(ShowCrewNames,         "ShowCrewNames",        1             ,false, true));
 	pComp->Value(mkNamingAdapt(ShowCrewCNames,        "ShowCrewCNames",       0             ,false, true));
-	pComp->Value(mkNamingAdapt(Windowed,              "Windowed",             0             ,false, true));
+	pComp->Value(mkNamingAdapt(Windowed,              "Windowed",             1             ,false, true));
 	pComp->Value(mkNamingAdapt(PXSGfx,                "PXSGfx"  ,             1             ));
 	pComp->Value(mkNamingAdapt(Gamma,                 "Gamma"  ,              100           ));
 	pComp->Value(mkNamingAdapt(Currency,              "Currency"  ,           0             ));
 	pComp->Value(mkNamingAdapt(Monitor,               "Monitor",              0             )); // 0 = D3DADAPTER_DEFAULT
 	pComp->Value(mkNamingAdapt(MaxRefreshDelay,       "MaxRefreshDelay",      30            ));
 	pComp->Value(mkNamingAdapt(NoOffscreenBlits,      "NoOffscreenBlits",     1             ));
-	pComp->Value(mkNamingAdapt(MultiSampling,         "MultiSampling",        4             ));
+	pComp->Value(mkNamingAdapt(MultiSampling,         "MultiSampling",        0             ));
 	pComp->Value(mkNamingAdapt(AutoFrameSkip,         "AutoFrameSkip",        1          ));
 	pComp->Value(mkNamingAdapt(MouseCursorSize,       "MouseCursorSize",      50            ));
 }
@@ -209,7 +209,9 @@ void C4ConfigSecurity::CompileFunc(StdCompiler *pComp)
 	pComp->Value(mkNamingAdapt(s(KeyPath),              "KeyPath",              R"(%APPDATA%\)" C4ENGINENAME, false, true));
 #elif defined(__linux__)
 	pComp->Value(mkNamingAdapt(s(KeyPath),              "KeyPath",              "$HOME/.clonk/" C4ENGINENICK, false, true));
-#elif defined(__APPLE__)
+#elif defined(__HAIKU__)
+	pComp->Value(mkNamingAdapt(s(KeyPath),              "KeyPath",              "$HOME/config/settings/" C4ENGINENICK, false, true));
+#elif defined(__APPLE__)#elif defined(__APPLE__)
 	pComp->Value(mkNamingAdapt(s(KeyPath),              "KeyPath",              "$HOME/Library/Application Support/" C4ENGINENAME, false, true));
 #endif
 }
@@ -301,6 +303,8 @@ void C4Config::GetConfigFileName(StdStrBuf &filename, const char *szConfigFile)
 		filename.Copy(home);
 #ifdef __APPLE__
 		filename += "Library/Preferences/" C4ENGINEID ".config";
+#elif defined(__HAIKU__)
+		filename += "config/settings/" C4ENGINENICK "/config";
 #else
 		filename += ".clonk/" C4ENGINENICK "/config";
 #endif
@@ -340,6 +344,15 @@ bool C4Config::Load(const char *szConfigFile)
 					filename += ".clonk/" C4ENGINENICK;
 					CreatePath(filename.getData());
 				}
+#endif
+#ifdef __HAIKU__
+				if (!szConfigFile)
+				{
+					StdStrBuf filename(getenv("HOME"));
+					if (filename) { filename += "/"; }
+					filename += "config/settings/" C4ENGINENICK;
+					CreatePath(filename.getData());
+				}
 #endif
 				// Buggy StdCompiler crashes when compiling a Null-StdStrBuf
 				buf.Ref(" ");
@@ -500,6 +513,8 @@ void C4ConfigGeneral::DeterminePaths()
 		SCopy(R"(%APPDATA%\)" C4ENGINENAME, UserDataPath);
 #elif defined(__APPLE__)
 		SCopy("$HOME/Library/Application Support/" C4ENGINENAME, UserDataPath);
+#elif defined(__HAIKU__)
+		SCopy("$HOME/config/settings/" C4ENGINENICK, UserDataPath);
 #else
 		SCopy("$HOME/.clonk/" C4ENGINENICK, UserDataPath);
 #endif
diff --git a/src/game/ClonkMain.cpp b/src/game/ClonkMain.cpp
index a7c69dd..746cee6 100644
--- a/src/game/ClonkMain.cpp
+++ b/src/game/ClonkMain.cpp
@@ -206,11 +206,25 @@ static void restart(char * argv[])
 
 int main (int argc, char * argv[])
 {
+#ifdef __HAIKU__
+	char *binpath = realpath(argv[0], NULL);
+	if (binpath != NULL) {
+		char *appdir = strrchr(binpath, '/');
+		*appdir = '\0';
+		chdir(binpath);
+		free(binpath);
+	}
+
+	setenv("MESA_GL_VERSION_OVERRIDE", "3.3", 1);
+	setenv("MESA_GLSL_VERSION_OVERRIDE", "330", 1);
+#endif
+#ifndef __HAIKU__
 	if (!geteuid())
 	{
 		printf("Do not run %s as root!\n", argc ? argv[0] : "this program");
 		return C4XRV_Failure;
 	}
+#endif
 #ifdef HAVE_BACKWARD
 	backward::SignalHandling sh;
 #elif defined(HAVE_SIGNAL_H)
diff --git a/src/network/C4NetIO.cpp b/src/network/C4NetIO.cpp
index 7ff84a8..ddf82ee 100644
--- a/src/network/C4NetIO.cpp
+++ b/src/network/C4NetIO.cpp
@@ -696,7 +696,7 @@ std::vector<C4NetIO::HostAddress> C4NetIO::GetLocalAddresses()
 		fclose(f);
 	}
 #endif
-
+#ifndef __HAIKU__
 	struct ifaddrs* addrs;
 	if (getifaddrs(&addrs) < 0)
 	    return result;
@@ -711,6 +711,7 @@ std::vector<C4NetIO::HostAddress> C4NetIO::GetLocalAddresses()
 		}
 	}
 	freeifaddrs(addrs);
+#endif
 #endif
 
 	return result;
-- 
2.23.0

