From b40385d190c2748f877dd355b14f85a46e98b320 Mon Sep 17 00:00:00 2001
From: Jerome Duval <jerome.duval@gmail.com>
Date: Wed, 22 Mar 2017 22:05:39 +0100
Subject: Haiku patch


diff --git a/toonz/sources/CMakeLists.txt b/toonz/sources/CMakeLists.txt
index bddb3f9..956a612 100644
--- a/toonz/sources/CMakeLists.txt
+++ b/toonz/sources/CMakeLists.txt
@@ -94,6 +94,8 @@ elseif(UNIX)
     endif()
     if(CMAKE_SYSTEM_NAME MATCHES "Linux")
         add_definitions(-DLINUX)
+    elseif(CMAKE_SYSTEM_NAME MATCHES "Haiku")
+        add_definitions(-DHAIKU)
     else()
         message(WARNING "Support for generic Unix (Not Apple or Linux) isn't yet working!")
     endif()
diff --git a/toonz/sources/common/tapptools/tenv.cpp b/toonz/sources/common/tapptools/tenv.cpp
index d7b3268..9e43635 100644
--- a/toonz/sources/common/tapptools/tenv.cpp
+++ b/toonz/sources/common/tapptools/tenv.cpp
@@ -5,7 +5,7 @@
 #include "tconvert.h"
 #include "tfilepath_io.h"
 
-#ifdef LINUX
+#if defined(LINUX) || defined(HAIKU)
 #include <QDir>
 #endif
 #include <QSettings>
diff --git a/toonz/sources/common/tapptools/ttimer.cpp b/toonz/sources/common/tapptools/ttimer.cpp
index a955ee4..139334b 100644
--- a/toonz/sources/common/tapptools/ttimer.cpp
+++ b/toonz/sources/common/tapptools/ttimer.cpp
@@ -122,7 +122,7 @@ void CALLBACK ElapsedTimeCB(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1,
   if (imp->m_action) imp->m_action->sendCommand(imp->m_ticks);
 }
 };
-#elif LINUX
+#elif defined(LINUX) || defined(HAIKU)
 
 #include <SDL_timer.h>
 #include <SDL.h>
diff --git a/toonz/sources/common/tcore/tstopwatch.cpp b/toonz/sources/common/tcore/tstopwatch.cpp
index 5a993a8..0231839 100644
--- a/toonz/sources/common/tcore/tstopwatch.cpp
+++ b/toonz/sources/common/tcore/tstopwatch.cpp
@@ -8,7 +8,7 @@
 #include <stdlib.h>
 #else  //_WIN32
 
-#if defined(__APPLE_CC__)
+#if defined(__APPLE_CC__) || defined(HAIKU)
 #include <unistd.h>
 #else
 #include <sys/unistd.h>
diff --git a/toonz/sources/common/tgl/tgl.cpp b/toonz/sources/common/tgl/tgl.cpp
index c4bcd70..510e9ef 100644
--- a/toonz/sources/common/tgl/tgl.cpp
+++ b/toonz/sources/common/tgl/tgl.cpp
@@ -12,14 +12,14 @@
 #ifdef _WIN32
 #include <cstdlib>
 #include <GL/glut.h>
-#elif defined(LINUX)
+#elif defined(LINUX) || defined(HAIKU)
 #include <GL/glut.h>
 #else
 #include <GLUT/glut.h>
 #endif
 #endif
 
-#if defined(MACOSX) || defined(LINUX)
+#if defined(MACOSX) || defined(LINUX) || defined(HAIKU)
 #include <QGLContext>
 #endif
 
@@ -617,7 +617,7 @@ void tglMakeCurrent(TGlContext context) {
 
 void tglDoneCurrent(TGlContext) { wglMakeCurrent(NULL, NULL); }
 
-#elif defined(LINUX) || defined(__sgi) || defined(MACOSX)
+#elif defined(LINUX) || defined(__sgi) || defined(MACOSX) || defined(HAIKU)
 
 TGlContext tglGetCurrentContext() {
   return reinterpret_cast<TGlContext>(
diff --git a/toonz/sources/common/tipc/tipc.cpp b/toonz/sources/common/tipc/tipc.cpp
index e379a9d..e2ccb8e 100644
--- a/toonz/sources/common/tipc/tipc.cpp
+++ b/toonz/sources/common/tipc/tipc.cpp
@@ -18,7 +18,7 @@
 #elif defined(MACOSX)
 #include <sys/sysctl.h>
 #include <unistd.h>
-#elif defined(LINUX)
+#elif defined(LINUX) || defined(HAIKU)
 #include <sys/time.h>
 #include <unistd.h>
 #endif
diff --git a/toonz/sources/common/tsystem/tpluginmanager.cpp b/toonz/sources/common/tsystem/tpluginmanager.cpp
index 905c8b7..b1faa41 100644
--- a/toonz/sources/common/tsystem/tpluginmanager.cpp
+++ b/toonz/sources/common/tsystem/tpluginmanager.cpp
@@ -19,12 +19,16 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <dirent.h>
+#ifndef HAIKU
 #include <sys/dir.h>
+#endif
 #include <sys/param.h>  // for getfsstat
 #ifdef MACOSX
 #include <sys/ucred.h>
 #endif
+#ifndef HAIKU
 #include <sys/mount.h>
+#endif
 #include <pwd.h>
 #include <dlfcn.h>
 #endif
@@ -171,7 +175,7 @@ void TPluginManager::loadPlugin(const TFilePath &fp) {
 void TPluginManager::loadPlugins(const TFilePath &dir) {
 #if defined(_WIN32)
   const std::string extension = "dll";
-#elif defined(LINUX) || defined(__sgi)
+#elif defined(LINUX) || defined(__sgi) || defined(HAIKU)
   const std::string extension = "so";
 #elif defined(MACOSX)
   const std::string extension = "dylib";
diff --git a/toonz/sources/common/tsystem/tsystempd.cpp b/toonz/sources/common/tsystem/tsystempd.cpp
index 4edf000..3807ed5 100644
--- a/toonz/sources/common/tsystem/tsystempd.cpp
+++ b/toonz/sources/common/tsystem/tsystempd.cpp
@@ -97,6 +97,23 @@
 
 #endif
 
+#ifdef HAIKU
+#define PLATFORM HAIKU
+#include <grp.h>
+#include <utime.h>
+#include <sys/param.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <pwd.h>
+#include <dlfcn.h>
+#include <utime.h>
+#include <sys/time.h>
+
+#endif
+
 #ifndef PLATFORM
 PLATFORM_NOT_SUPPORTED
 #endif
@@ -185,6 +202,11 @@ bool TSystem::memoryShortage() {
   // to be done...
   return false;
 
+#elif defined(HAIKU)
+
+  // to be done...
+  return false;
+
 #else
 
   @ @ @ERROR : PLATFORM NOT SUPPORTED
@@ -252,6 +274,11 @@ TINT64 TSystem::getFreeMemorySize(bool onlyPhisicalMemory) {
   // to be done...
   totalFree = 512 * 1024;
 
+#elif defined(HAIKU)
+
+  // to be done...
+  totalFree = 512 * 1024;
+
 #else
   @ @ @ERROR : PLATFORM NOT SUPPORTED
 #endif
@@ -280,6 +307,9 @@ TINT64 TSystem::getDiskSize(const TFilePath &diskName) {
     return 0;
   }
 #ifndef _WIN32
+#ifdef HAIKU
+	size = 0;
+#else
   struct statfs buf;
 #ifdef __sgi
   statfs(::to_string(diskName).c_str(), &buf, sizeof(struct statfs), 0);
@@ -287,6 +317,7 @@ TINT64 TSystem::getDiskSize(const TFilePath &diskName) {
   statfs(::to_string(diskName).c_str(), &buf);
 #endif
   size = (TINT64)((buf.f_blocks * buf.f_bsize) >> 10);
+#endif
 #else
   DWORD sectorsPerCluster;     // sectors per cluster
   DWORD bytesPerSector;        // bytes per sector
@@ -317,6 +348,9 @@ TINT64 TSystem::getFreeDiskSize(const TFilePath &diskName) {
     return 0;
   }
 #ifndef _WIN32
+#ifdef HAIKU
+	size = 0;
+#else
   struct statfs buf;
 #ifdef __sgi
   statfs(diskName.getWideString().c_str(), &buf, sizeof(struct statfs), 0);
@@ -324,6 +358,7 @@ TINT64 TSystem::getFreeDiskSize(const TFilePath &diskName) {
   statfs(::to_string(diskName).c_str(), &buf);
 #endif
   size = (TINT64)(buf.f_bfree * buf.f_bsize) >> 10;
+#endif
 #else
   DWORD sectorsPerCluster;     // sectors per cluster
   DWORD bytesPerSector;        // bytes per sector
@@ -383,6 +418,11 @@ TINT64 TSystem::getMemorySize(bool onlyPhisicalMemory) {
   // to be done...
   return 512 * 1024;
 
+#elif defined(HAIKU)
+
+  // to be done...
+  return 512 * 1024;
+
 #else
   @ @ @ERROR : PLATFORM NOT SUPPORTED
 #endif
diff --git a/toonz/sources/common/tvrender/tfont_proxy.cpp b/toonz/sources/common/tvrender/tfont_proxy.cpp
index 6a3b24a..2daf9a5 100644
--- a/toonz/sources/common/tvrender/tfont_proxy.cpp
+++ b/toonz/sources/common/tvrender/tfont_proxy.cpp
@@ -1,6 +1,6 @@
 
 
-#if defined(__LP64__) && !defined(LINUX)
+#if defined(__LP64__) && !defined(LINUX) && !defined(HAIKU)
 
 // Toonz includes
 #include "tvectorimage.h"
diff --git a/toonz/sources/common/tvrender/tofflinegl.cpp b/toonz/sources/common/tvrender/tofflinegl.cpp
index 000e6df..7086c63 100644
--- a/toonz/sources/common/tvrender/tofflinegl.cpp
+++ b/toonz/sources/common/tvrender/tofflinegl.cpp
@@ -19,7 +19,7 @@
 
 #include "tthread.h"
 
-#elif defined(MACOSX)
+#elif defined(MACOSX) || defined(HAIKU)
 
 #include "qtofflinegl.h"
 
@@ -490,7 +490,7 @@ static std::shared_ptr<TOfflineGL::Imp> defaultOfflineGLGenerator(
   return std::make_shared<QtOfflineGL>(dim, shared);
 }
 
-#elif defined(MACOSX)
+#elif defined(MACOSX) || defined(HAIKU)
 
 std::shared_ptr<TOfflineGL::Imp> defaultOfflineGLGenerator(
     const TDimension &dim, std::shared_ptr<TOfflineGL::Imp> shared) {
diff --git a/toonz/sources/common/tvrender/ttessellator.cpp b/toonz/sources/common/tvrender/ttessellator.cpp
index ca049d4..bd72e62 100644
--- a/toonz/sources/common/tvrender/ttessellator.cpp
+++ b/toonz/sources/common/tvrender/ttessellator.cpp
@@ -94,7 +94,7 @@ static void CALLBACK myCombine(GLdouble coords[3], GLdouble *d[4], GLfloat w[4],
 typedef GLvoid(CALLBACK *GluCallback)(void);
 #endif
 
-#if defined(MACOSX) || defined(LINUX)
+#if defined(MACOSX) || defined(LINUX) || defined(HAIKU)
 
 typedef GLvoid (*GluCallback)();
 
diff --git a/toonz/sources/image/compatibility/tfile_io.c b/toonz/sources/image/compatibility/tfile_io.c
index 20ca6b8..5c39e52 100644
--- a/toonz/sources/image/compatibility/tfile_io.c
+++ b/toonz/sources/image/compatibility/tfile_io.c
@@ -58,7 +58,7 @@ char *convertWCHAR2CHAR(const wchar_t *wc) {
 }
 #endif
 /*-----------------------------------*/
-#if defined(MACOSX) || defined(LINUX)
+#if defined(MACOSX) || defined(LINUX) || defined(HAIKU)
 
 FILE *_wfopen(const wchar_t *fname, const wchar_t *mode) {
   char *cfname = convertWCHAR2CHAR(fname);
diff --git a/toonz/sources/image/compatibility/tfile_io.h b/toonz/sources/image/compatibility/tfile_io.h
index d7a3fdd..a333719 100644
--- a/toonz/sources/image/compatibility/tfile_io.h
+++ b/toonz/sources/image/compatibility/tfile_io.h
@@ -19,7 +19,7 @@ extern "C" {
 
 char *convertWCHAR2CHAR(const wchar_t *fname);
 
-#if defined(MACOSX) || defined(LINUX)
+#if defined(MACOSX) || defined(LINUX) || defined(HAIKU)
 
 #include <sys/types.h>
 #include <sys/stat.h>
diff --git a/toonz/sources/image/compatibility/tnz4.h b/toonz/sources/image/compatibility/tnz4.h
index 60593d6..9571725 100644
--- a/toonz/sources/image/compatibility/tnz4.h
+++ b/toonz/sources/image/compatibility/tnz4.h
@@ -36,7 +36,7 @@ TNZ_LITTLE_ENDIAN undefined !!
   unsigned char b, g, r, m;
 #elif defined(__sgi)
   unsigned char m, b, g, r;
-#elif defined(LINUX)
+#elif defined(LINUX) || defined(HAIKU)
   unsigned char r, g, b, m;
 #elif defined(MACOSX)
   unsigned char m, r, g, b;
@@ -50,7 +50,7 @@ typedef struct SPIXEL {
   unsigned short b, g, r, m;
 #elif defined(__sgi)
   unsigned short m, b, g, r;
-#elif defined(LINUX)
+#elif defined(LINUX)|| defined(HAIKU)
   unsigned short r, g, b, m;
 #elif defined(MACOSX)
   unsigned char m, r, g, b;
diff --git a/toonz/sources/image/pli/pli_io.cpp b/toonz/sources/image/pli/pli_io.cpp
index d712b4e..7f981be 100644
--- a/toonz/sources/image/pli/pli_io.cpp
+++ b/toonz/sources/image/pli/pli_io.cpp
@@ -432,7 +432,7 @@ void ParsedPli::setMaxThickness(double maxThickness) {
 };
 
 /* indirect inclusion of <math.h> causes 'abs' to return double on Linux */
-#ifdef LINUX
+#if defined(LINUX) || defined(HAIKU)
 template <typename T>
 T abs_workaround(T a) {
   return (a > 0) ? a : -a;
diff --git a/toonz/sources/image/tiio.cpp b/toonz/sources/image/tiio.cpp
index dfbd74d..367b3f9 100644
--- a/toonz/sources/image/tiio.cpp
+++ b/toonz/sources/image/tiio.cpp
@@ -53,7 +53,7 @@
 #include "./mov/tiio_movM.h"
 #include "./3gp/tiio_3gpM.h"
 
-#elif defined(LINUX)  // No more supported by the way...
+#elif defined(LINUX)  || defined(HAIKU) // No more supported by the way...
 // #include "./mov/tiio_movL.h"
 #include "./mov/tiio_mov_proxy.h"
 #include "./3gp/tiio_3gp_proxy.h"
diff --git a/toonz/sources/include/tcg/hpp/triangulate.hpp b/toonz/sources/include/tcg/hpp/triangulate.hpp
index e778824..373997b 100644
--- a/toonz/sources/include/tcg/hpp/triangulate.hpp
+++ b/toonz/sources/include/tcg/hpp/triangulate.hpp
@@ -14,7 +14,7 @@
 #include <GL/glu.h>
 #elif defined(MACOSX)
 #include <GLUT/glut.h>
-#elif defined(LINUX)
+#elif defined(LINUX) || defined(HAIKU)
 #include <GL/glut.h>
 #include <cstring>
 #endif
diff --git a/toonz/sources/include/tfont.h b/toonz/sources/include/tfont.h
index 8853424..a3dc158 100644
--- a/toonz/sources/include/tfont.h
+++ b/toonz/sources/include/tfont.h
@@ -140,7 +140,7 @@ public:
 
 // --------- TFont methods  called on curren font -----------
 
-#if !defined(__LP64__) || defined(LINUX)
+#if !defined(__LP64__) || defined(LINUX) || defined(HAIKU)
 
   TPoint drawChar(TVectorImageP &outImage, wchar_t charcode,
                   wchar_t nextCode = 0) {
diff --git a/toonz/sources/include/tgl.h b/toonz/sources/include/tgl.h
index fa2cdcb..efab600 100644
--- a/toonz/sources/include/tgl.h
+++ b/toonz/sources/include/tgl.h
@@ -21,7 +21,7 @@
 #define GLUT_NO_WARNING_DISABLE
 #endif
 
-#ifdef LINUX
+#if defined(LINUX) || defined(HAIKU)
 #include <GL/gl.h>
 #include <GL/glu.h>
 #include <GL/glut.h>
diff --git a/toonz/sources/include/tmachine.h b/toonz/sources/include/tmachine.h
index d7a8944..94f8f1e 100644
--- a/toonz/sources/include/tmachine.h
+++ b/toonz/sources/include/tmachine.h
@@ -7,7 +7,7 @@
 #define TNZ_MACHINE_CHANNEL_ORDER_BGRM 1
 #elif defined(__sgi)
 #define TNZ_MACHINE_CHANNEL_ORDER_MBGR 1
-#elif defined(LINUX)
+#elif defined(LINUX) || defined(HAIKU)
 #define TNZ_MACHINE_CHANNEL_ORDER_BGRM 1
 #elif defined(MACOSX)
 #define TNZ_MACHINE_CHANNEL_ORDER_MRGB 1
diff --git a/toonz/sources/include/tnztypes.h b/toonz/sources/include/tnztypes.h
index 66a9fc2..10b587a 100644
--- a/toonz/sources/include/tnztypes.h
+++ b/toonz/sources/include/tnztypes.h
@@ -18,7 +18,7 @@ typedef uint32_t TUINT32;
 typedef int64_t TINT64;
 typedef uint64_t TUINT64;
 
-#elif defined(__LP64__) && (!defined(LINUX))
+#elif defined(__LP64__) && (!defined(LINUX)) && (!defined(HAIKU))
 
 #define TINT32                                                                 \
   ;                                                                            \
@@ -40,6 +40,13 @@ typedef unsigned __int64_t TUINT64;
 typedef uint32_t TUINT32;
 typedef int64_t TINT64;
 typedef uint64_t TUINT64;
+
+#elif defined(HAIKU)
+#include <stdint.h>
+#define TINT32 int32_t
+typedef uint32_t TUINT32;
+typedef int64_t TINT64;
+typedef uint64_t TUINT64;
 #endif
 
 #endif
diff --git a/toonz/sources/include/toonz4.6/machine.h b/toonz/sources/include/toonz4.6/machine.h
index e9bf6f6..92ab56f 100644
--- a/toonz/sources/include/toonz4.6/machine.h
+++ b/toonz/sources/include/toonz4.6/machine.h
@@ -17,7 +17,7 @@
 #define TNZ_MACHINE_CHANNEL_ORDER_BGRM 1
 #elif defined(__sgi)
 #define TNZ_MACHINE_CHANNEL_ORDER_MBGR 1
-#elif defined(LINUX)
+#elif defined(LINUX) || defined(HAIKU)
 #define TNZ_MACHINE_CHANNEL_ORDER_BGRM 1
 #elif defined(MACOSX)
 #define TNZ_MACHINE_CHANNEL_ORDER_MRGB 1
diff --git a/toonz/sources/include/tpixel.h b/toonz/sources/include/tpixel.h
index 5dd25b0..1565fb2 100644
--- a/toonz/sources/include/tpixel.h
+++ b/toonz/sources/include/tpixel.h
@@ -177,7 +177,7 @@ undefined machine order !!!!
   TPixelRGBM64(int rr, int gg, int bb, int mm = maxChannelValue)
       : r(rr), g(gg), b(bb), m(mm){};
 #else
-#if defined(LINUX) || defined(MACOSX)
+#if defined(LINUX) || defined(MACOSX) || defined(HAIKU)
 
 #ifdef powerpc
 
diff --git a/toonz/sources/include/tsound_t.h b/toonz/sources/include/tsound_t.h
index 1735c98..4f2c9a8 100644
--- a/toonz/sources/include/tsound_t.h
+++ b/toonz/sources/include/tsound_t.h
@@ -137,7 +137,7 @@ from which it's created.It hasn't reference to the object.
 
 //! Applies a trasformation (echo, reverb, ect) to the object and returns the
 //! transformed soundtrack
-#if defined(MACOSX) || defined(LINUX)
+#if defined(MACOSX) || defined(LINUX) || defined(HAIKU)
   TSoundTrackP apply(TSoundTransform *transform) override;
 #else  // _WIN32
   TSoundTrackP apply(TSoundTransform *transform) override {
@@ -389,7 +389,7 @@ public:
 };
 
 //==============================================================================
-#if defined(MACOSX) || defined(LINUX)
+#if defined(MACOSX) || defined(LINUX) || defined(HAIKU)
 template <class T>
 DVAPI TSoundTrackP TSoundTrackT<T>::apply(TSoundTransform *transform) {
   assert(transform);
diff --git a/toonz/sources/tnzext/tlin/tlin_superlu_wrap.cpp b/toonz/sources/tnzext/tlin/tlin_superlu_wrap.cpp
index 0dcf0c5..7726ee7 100644
--- a/toonz/sources/tnzext/tlin/tlin_superlu_wrap.cpp
+++ b/toonz/sources/tnzext/tlin/tlin_superlu_wrap.cpp
@@ -309,7 +309,7 @@ void tlin::factorize(SuperMatrix *A, SuperFactors *&F, superlu_options_t *opt) {
 
   int result;
   dgstrf(opt, &AC, sp_ienv(1), sp_ienv(2), etree, NULL, 0, F->perm_c, F->perm_r,
-         F->L, F->U, &stat, &result);
+         F->L, F->U, NULL, &stat, &result);
 
   StatFree(&stat);
 
diff --git a/toonz/sources/toonz/sceneviewer.cpp b/toonz/sources/toonz/sceneviewer.cpp
index 1b521a2..d6eac5b 100644
--- a/toonz/sources/toonz/sceneviewer.cpp
+++ b/toonz/sources/toonz/sceneviewer.cpp
@@ -1,5 +1,5 @@
 
-#ifdef LINUX
+#if defined(LINUX) || defined(HAIKU)
 #define GL_GLEXT_PROTOTYPES
 #endif
 
diff --git a/toonz/sources/toonzfarm/tfarmserver/tfarmserver.cpp b/toonz/sources/toonzfarm/tfarmserver/tfarmserver.cpp
index c05d048..c5bd44a 100644
--- a/toonz/sources/toonzfarm/tfarmserver/tfarmserver.cpp
+++ b/toonz/sources/toonzfarm/tfarmserver/tfarmserver.cpp
@@ -610,7 +610,7 @@ void FarmServer::queryHwInfo(HwInfo &hwInfo) {
   sysctl(mib, 2, &physMemSize, &len, NULL, 0);
 #endif
 
-#ifdef LINUX
+#if defined(LINUX) || defined(HAIKU)
   TINT64 physMemSize =
       (TINT64)sysconf(_SC_PHYS_PAGES) * (TINT64)sysconf(_SC_PAGE_SIZE);
 #endif
diff --git a/toonz/sources/toonzlib/autopos.cpp b/toonz/sources/toonzlib/autopos.cpp
index b596d64..5928e82 100644
--- a/toonz/sources/toonzlib/autopos.cpp
+++ b/toonz/sources/toonzlib/autopos.cpp
@@ -17,7 +17,7 @@ fare resize e realloc size dello stack a 65000 unita'
 
 */
 
-#if defined(MACOSX) || defined(LINUX)
+#if defined(MACOSX) || defined(LINUX) || defined(HAIKU)
 #define TRUE 1
 #define FALSE 0
 #endif
diff --git a/toonz/sources/toonzlib/trastercentroid.cpp b/toonz/sources/toonzlib/trastercentroid.cpp
index 4f538ab..24e6114 100644
--- a/toonz/sources/toonzlib/trastercentroid.cpp
+++ b/toonz/sources/toonzlib/trastercentroid.cpp
@@ -15,7 +15,7 @@
 #include "tmsg.h"
 #include "copP.h"*/
 
-#if defined(MACOSX) || defined(LINUX)
+#if defined(MACOSX) || defined(LINUX) || defined(HAIKU)
 #define TRUE 1
 #define FALSE 0
 #endif
-- 
2.10.2

