From 3d79866f25029f05d5c9f98d9355e696b7dac36e Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Fri, 15 May 2020 22:41:51 +1000
Subject: Add Haiku suppport


diff --git a/source/mupen64plus-audio-sdl/projects/unix/Makefile b/source/mupen64plus-audio-sdl/projects/unix/Makefile
old mode 100755
new mode 100644
index 9a98687..850a066
--- a/source/mupen64plus-audio-sdl/projects/unix/Makefile
+++ b/source/mupen64plus-audio-sdl/projects/unix/Makefile
@@ -24,6 +24,11 @@
 # detect operation system
 UNAME ?= $(shell uname -s)
 OS := NONE
+ifeq ("$(UNAME)","Haiku")
+  OS = HAIKU
+  SHARED = -shared
+  SO_EXTENSION = so
+endif
 ifeq ("$(UNAME)","Linux")
   OS = LINUX
   SHARED = -shared
@@ -84,7 +89,7 @@ ifneq ("$(filter x86_64 amd64,$(HOST_CPU))","")
     PIC ?= 1
   endif
 endif
-ifneq ("$(filter pentium i%86,$(HOST_CPU))","")
+ifneq ("$(filter BePC pentium i%86,$(HOST_CPU))","")
   CPU := X86
   ARCH_DETECTED := 32BITS
   PIC ?= 0
diff --git a/source/mupen64plus-core/projects/unix/Makefile b/source/mupen64plus-core/projects/unix/Makefile
old mode 100755
new mode 100644
index c18596b..e1852d8
--- a/source/mupen64plus-core/projects/unix/Makefile
+++ b/source/mupen64plus-core/projects/unix/Makefile
@@ -25,6 +25,9 @@
 # detect operating system
 UNAME ?= $(shell uname -s)
 OS := NONE
+ifeq ("$(UNAME)","Haiku")
+  OS = HAIKU
+endif
 ifeq ("$(UNAME)","Linux")
   OS = LINUX
 endif
@@ -68,7 +71,7 @@ ifneq ("$(filter x86_64 amd64,$(HOST_CPU))","")
     PIC ?= 1
   endif
 endif
-ifneq ("$(filter pentium i%86,$(HOST_CPU))","")
+ifneq ("$(filter BePC pentium i%86,$(HOST_CPU))","")
   CPU := X86
   ARCH_DETECTED := 32BITS
   PIC ?= 0
@@ -134,7 +137,7 @@ ifeq ("$(CPU)","NONE")
 endif
 
 # base CFLAGS, LDLIBS, and LDFLAGS
-OPTFLAGS ?= -O3 -flto
+OPTFLAGS ?= -O3
 WARNFLAGS ?= -Wall
 CFLAGS += -ffast-math -fno-strict-aliasing -fvisibility=hidden -I../../src -I../../src/asm_defines -DM64P_PARALLEL
 CXXFLAGS += -fvisibility-inlines-hidden
@@ -169,6 +172,16 @@ ifeq ($(ARCH_DETECTED), 64BITS_32)
 endif
 
 # set special flags per-system
+ifeq ($(OS), HAIKU)
+  TARGET = libmupen64plus$(POSTFIX).so.2.0.0
+  SONAME = libmupen64plus$(POSTFIX).so.2
+  LDFLAGS += -Wl,-Bsymbolic -shared -Wl,-export-dynamic -Wl,-soname,$(SONAME)
+  ifeq ($(ARCH_DETECTED), 64BITS)
+    ASFLAGS = -f elf64
+  else
+    ASFLAGS = -f elf
+  endif
+endif
 ifeq ($(OS), FREEBSD)
   TARGET = libmupen64plus$(POSTFIX).so.2.0.0
   SONAME = libmupen64plus$(POSTFIX).so.2
@@ -406,6 +419,9 @@ LINK.o = $(Q_LD)$(CXX) $(OPTFLAGS) $(WARNFLAGS) $(CXXFLAGS) $(LDFLAGS) $(TARGET_
 ifeq ($(OS),OSX)
   LDCONFIG ?= true  # no 'ldconfig' under OSX
 else
+  ifeq ($(OS),HAIKU)
+    LDCONFIG ?= true
+  endif
   ifeq ($(OS),LINUX)
     LDCONFIG ?= PATH="$$PATH:/sbin" ldconfig -n
   endif
diff --git a/source/mupen64plus-core/src/api/vidext.c b/source/mupen64plus-core/src/api/vidext.c
index 6ff9bd9..5062fb7 100644
--- a/source/mupen64plus-core/src/api/vidext.c
+++ b/source/mupen64plus-core/src/api/vidext.c
@@ -158,7 +158,11 @@ EXPORT m64p_error CALL VidExt_ListFullscreenModes(m64p_2d_size *SizeArray, int *
         return M64ERR_NOT_INIT;
 
     /* get a list of SDL video modes */
+#ifdef __HAIKU__
+	videoFlags = SDL_OPENGL;
+#else
     videoFlags = SDL_OPENGL | SDL_FULLSCREEN;
+#endif
 
     if ((videoInfo = SDL_GetVideoInfo()) == NULL)
     {
@@ -224,7 +228,11 @@ EXPORT m64p_error CALL VidExt_SetVideoMode(int Width, int Height, int BitsPerPix
     }
     else if (ScreenMode == M64VIDEO_FULLSCREEN)
     {
+#ifdef __HAIKU__
+        videoFlags = SDL_OPENGL;
+#else
         videoFlags = SDL_OPENGL | SDL_FULLSCREEN;
+#endif
     }
     else
     {
diff --git a/source/mupen64plus-core/src/api/vidext_sdl2_compat.h b/source/mupen64plus-core/src/api/vidext_sdl2_compat.h
index 06d778d..69beabd 100644
--- a/source/mupen64plus-core/src/api/vidext_sdl2_compat.h
+++ b/source/mupen64plus-core/src/api/vidext_sdl2_compat.h
@@ -195,6 +195,9 @@ SDL_GL_SwapBuffers(void)
 static int
 SDL_WM_ToggleFullScreen(SDL_Surface * surface)
 {
+#ifdef __HAIKU__
+	return 0;
+#else
     int window_w;
     int window_h;
 
@@ -225,6 +228,7 @@ SDL_WM_ToggleFullScreen(SDL_Surface * surface)
 
     /* We're done! */
     return 1;
+#endif
 }
 
 static int
diff --git a/source/mupen64plus-core/src/osal/files_unix.c b/source/mupen64plus-core/src/osal/files_unix.c
index 2207167..65daf08 100644
--- a/source/mupen64plus-core/src/osal/files_unix.c
+++ b/source/mupen64plus-core/src/osal/files_unix.c
@@ -35,6 +35,11 @@
 #include "api/m64p_types.h"
 #include "files.h"
 
+#ifdef __HAIKU__
+#include <FindDirectory.h>
+#include <fs_info.h>
+#endif
+
 /* definitions for system directories to search when looking for shared data files */
 #if defined(SHAREDIR)
   #define XSTR(S) STR(S) /* this wacky preprocessor thing is necessary to generate a quote-enclosed */
@@ -173,6 +178,15 @@ const char * osal_get_user_configpath(void)
 {
     static char retpath[PATH_MAX];
     int rval;
+
+#ifdef __HAIKU__
+	char path[B_PATH_NAME_LENGTH];
+	if (find_directory(B_USER_SETTINGS_DIRECTORY, 0, false, path, B_PATH_NAME_LENGTH) == B_OK) {
+		snprintf(retpath, PATH_MAX, "%s/%s", path, "Mupen64Plus/");
+		mkdir(retpath, S_IRWXU);
+		return retpath;
+	}
+#endif
     
     /* first, try the XDG_CONFIG_HOME environment variable */
     rval = get_xdg_dir(retpath, "XDG_CONFIG_HOME", "mupen64plus/");
@@ -194,7 +208,16 @@ const char * osal_get_user_datapath(void)
 {
     static char retpath[PATH_MAX];
     int rval;
-    
+
+#ifdef __HAIKU__
+	char path[B_PATH_NAME_LENGTH];
+	if (find_directory(B_USER_SETTINGS_DIRECTORY, 0, false, path, B_PATH_NAME_LENGTH) == B_OK) {
+		snprintf(retpath, PATH_MAX, "%s/%s", path, "Mupen64Plus/");
+		mkdir(retpath, S_IRWXU);
+		return retpath;
+	}
+#endif
+
     /* first, try the XDG_DATA_HOME environment variable */
     rval = get_xdg_dir(retpath, "XDG_DATA_HOME", "mupen64plus/");
     if (rval == 0)
@@ -215,7 +238,16 @@ const char * osal_get_user_cachepath(void)
 {
     static char retpath[PATH_MAX];
     int rval;
-    
+
+#ifdef __HAIKU__
+	char path[B_PATH_NAME_LENGTH];
+	if (find_directory(B_USER_CACHE_DIRECTORY, 0, false, path, B_PATH_NAME_LENGTH) == B_OK) {
+		snprintf(retpath, PATH_MAX, "%s/%s", path, "Mupen64Plus/");
+		mkdir(retpath, S_IRWXU);
+		return retpath;
+	}
+#endif
+
     /* first, try the XDG_CACHE_HOME environment variable */
     rval = get_xdg_dir(retpath, "XDG_CACHE_HOME", "mupen64plus/");
     if (rval == 0)
diff --git a/source/mupen64plus-core/subprojects/minizip/ioapi.h b/source/mupen64plus-core/subprojects/minizip/ioapi.h
index bac1325..8015acd 100644
--- a/source/mupen64plus-core/subprojects/minizip/ioapi.h
+++ b/source/mupen64plus-core/subprojects/minizip/ioapi.h
@@ -60,6 +60,11 @@
 #define ftello64 ftello
 #define fseeko64 fseeko
 #endif
+#ifdef __HAIKU__
+#define fopen64 fopen
+#define ftello64 ftello
+#define fseeko64 fseeko
+#endif
 #ifdef _MSC_VER
  #define fopen64 fopen
  #if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC)))
diff --git a/source/mupen64plus-input-sdl/projects/unix/Makefile b/source/mupen64plus-input-sdl/projects/unix/Makefile
old mode 100755
new mode 100644
index 91ba331..5e75c33
--- a/source/mupen64plus-input-sdl/projects/unix/Makefile
+++ b/source/mupen64plus-input-sdl/projects/unix/Makefile
@@ -23,6 +23,11 @@
 # detect operating system
 UNAME ?= $(shell uname -s)
 OS := NONE
+ifeq ("$(UNAME)","Haiku")
+  OS = HAIKU
+  SO_EXTENSION = so
+  SHARED = -shared
+endif
 ifeq ("$(UNAME)","Linux")
   OS = LINUX
   SO_EXTENSION = so
@@ -81,7 +86,7 @@ ifneq ("$(filter x86_64 amd64,$(HOST_CPU))","")
     PIC ?= 1
   endif
 endif
-ifneq ("$(filter pentium i%86,$(HOST_CPU))","")
+ifneq ("$(filter BePC pentium i%86,$(HOST_CPU))","")
   CPU := X86
   ARCH_DETECTED := 32BITS
   PIC ?= 0
@@ -91,7 +96,7 @@ ifeq ("$(CPU)","OTHER")
 endif
 
 # base CFLAGS, LDLIBS, and LDFLAGS
-OPTFLAGS ?= -O3 -flto
+OPTFLAGS ?= -O3
 WARNFLAGS ?= -Wall
 CFLAGS += $(OPTFLAGS) $(WARNFLAGS) -ffast-math -fno-strict-aliasing -fvisibility=hidden -I../../src -D_GNU_SOURCE=1
 LDFLAGS += $(SHARED)
diff --git a/source/mupen64plus-rsp-hle/projects/unix/Makefile b/source/mupen64plus-rsp-hle/projects/unix/Makefile
old mode 100755
new mode 100644
index 57e0580..14bc028
--- a/source/mupen64plus-rsp-hle/projects/unix/Makefile
+++ b/source/mupen64plus-rsp-hle/projects/unix/Makefile
@@ -24,6 +24,11 @@
 # detect operating system
 UNAME ?= $(shell uname -s)
 OS := NONE
+ifeq ("$(UNAME)","Haiku")
+  OS = HAIKU
+  SO_EXTENSION = so
+  SHARED = -shared
+endif
 ifeq ("$(UNAME)","Linux")
   OS = LINUX
   SO_EXTENSION = so
@@ -83,7 +88,7 @@ ifneq ("$(filter x86_64 amd64,$(HOST_CPU))","")
     PIC ?= 1
   endif
 endif
-ifneq ("$(filter pentium i%86,$(HOST_CPU))","")
+ifneq ("$(filter BePC pentium i%86,$(HOST_CPU))","")
   CPU := X86
   ARCH_DETECTED := 32BITS
   PIC ?= 0
@@ -128,7 +133,7 @@ ifeq ("$(CPU)","NONE")
 endif
 
 # base CFLAGS, LDLIBS, and LDFLAGS
-OPTFLAGS ?= -O3 -flto
+OPTFLAGS ?= -O3
 WARNFLAGS ?= -Wall
 CFLAGS += $(OPTFLAGS) $(WARNFLAGS) -ffast-math -fno-strict-aliasing -fvisibility=hidden -I../../src
 LDFLAGS += $(SHARED)
diff --git a/source/mupen64plus-ui-console/projects/unix/Makefile b/source/mupen64plus-ui-console/projects/unix/Makefile
old mode 100755
new mode 100644
index 42f11a9..5687b09
--- a/source/mupen64plus-ui-console/projects/unix/Makefile
+++ b/source/mupen64plus-ui-console/projects/unix/Makefile
@@ -23,6 +23,9 @@
 # detect operation system
 UNAME ?= $(shell uname -s)
 OS := NONE
+ifeq ("$(UNAME)","Haiku")
+  OS = HAIKU
+endif
 ifeq ("$(UNAME)","Linux")
   OS = LINUX
 endif
@@ -62,13 +65,13 @@ ifneq ("$(filter x86_64 amd64,$(HOST_CPU))","")
     ARCH_DETECTED := 64BITS
   endif
 endif
-ifneq ("$(filter pentium i%86,$(HOST_CPU))","")
+ifneq ("$(filter BePC pentium i%86,$(HOST_CPU))","")
   CPU := X86
   ARCH_DETECTED := 32BITS
 endif
 
 # base CFLAGS, LDLIBS, and LDFLAGS
-OPTFLAGS ?= -O3 -flto
+OPTFLAGS ?= -O3
 WARNFLAGS ?= -Wall
 
 CFLAGS += $(OPTFLAGS) $(WARNFLAGS) -ffast-math -fno-strict-aliasing -I../../src
@@ -76,10 +79,12 @@ ifeq ($(OS), MINGW)
   CFLAGS += -lpthread
   LDLIBS += -lpthread
 else
+  ifneq ($(OS), HAIKU)
   ifneq ($(OS), OSX)
     CFLAGS += -pthread
     LDLIBS += -pthread
   endif
+  endif
 endif
 
 # set special flags per-system
@@ -172,6 +177,7 @@ MKDIR ?= mkdir -p
 COMPILE.c = $(Q_CC)$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
 LINK.o = $(Q_LD)$(CC) $(CFLAGS) $(LDFLAGS) $(TARGET_ARCH)
 
+ifneq ($(OS), HAIKU)
 ifeq ($(PIE), 1)
   CFLAGS += -fPIE
   LDFLAGS += -pie
@@ -185,6 +191,9 @@ else
     endif
   endif
 endif
+else
+  CFLAGS += -fPIC
+endif
 
 # set installation options
 ifeq ($(PREFIX),)
diff --git a/source/mupen64plus-video-glide64mk2/projects/unix/Makefile b/source/mupen64plus-video-glide64mk2/projects/unix/Makefile
old mode 100755
new mode 100644
index 18d6fba..bee5176
--- a/source/mupen64plus-video-glide64mk2/projects/unix/Makefile
+++ b/source/mupen64plus-video-glide64mk2/projects/unix/Makefile
@@ -25,6 +25,11 @@
 # detect operating system
 UNAME ?= $(shell uname -s)
 OS := NONE
+ifeq ("$(UNAME)","Haiku")
+  OS = HAIKU
+  SO_EXTENSION = so
+  SHARED = -shared
+endif
 ifeq ("$(UNAME)","Linux")
   OS = LINUX
   SO_EXTENSION = so
@@ -86,7 +91,7 @@ ifneq ("$(filter x86_64 amd64,$(HOST_CPU))","")
     PIC ?= 1
   endif
 endif
-ifneq ("$(filter pentium i%86,$(HOST_CPU))","")
+ifneq ("$(filter BePC pentium i%86,$(HOST_CPU))","")
   CPU := X86
   ARCH_DETECTED := 32BITS
   PIC ?= 0
diff --git a/source/mupen64plus-video-glide64mk2/src/Glitch64/inc/glidesys.h b/source/mupen64plus-video-glide64mk2/src/Glitch64/inc/glidesys.h
index 12d0fcb..dd98be5 100644
--- a/source/mupen64plus-video-glide64mk2/src/Glitch64/inc/glidesys.h
+++ b/source/mupen64plus-video-glide64mk2/src/Glitch64/inc/glidesys.h
@@ -111,7 +111,7 @@ n** -----------------------------------------------------------------------
 /* Check for OS */
 #if defined(__IRIX__) || defined(__sparc__) || defined(__linux__) || \
    defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
-   defined(__FreeBSD_kernel__) || defined(__GNU__)
+   defined(__FreeBSD_kernel__) || defined(__GNU__) || defined(__HAIKU__)
 #  define GLIDE_OS        GLIDE_OS_UNIX
 #elif defined(__DOS__)
 #  define GLIDE_OS        GLIDE_OS_DOS32
diff --git a/source/mupen64plus-video-rice/projects/unix/Makefile b/source/mupen64plus-video-rice/projects/unix/Makefile
old mode 100755
new mode 100644
index 7601a80..39e081a
--- a/source/mupen64plus-video-rice/projects/unix/Makefile
+++ b/source/mupen64plus-video-rice/projects/unix/Makefile
@@ -24,6 +24,11 @@
 # detect operating system
 UNAME ?= $(shell uname -s)
 OS := NONE
+ifeq ("$(UNAME)","Haiku")
+  OS = HAIKU
+  SO_EXTENSION = so
+  SHARED = -shared
+endif
 ifeq ("$(UNAME)","Linux")
   OS = LINUX
   SO_EXTENSION = so
@@ -83,7 +88,7 @@ ifneq ("$(filter x86_64 amd64,$(HOST_CPU))","")
     PIC ?= 1
   endif
 endif
-ifneq ("$(filter pentium i%86,$(HOST_CPU))","")
+ifneq ("$(filter BePC pentium i%86,$(HOST_CPU))","")
   CPU := X86
   ARCH_DETECTED := 32BITS
   PIC ?= 0
@@ -118,7 +123,7 @@ ifeq ("$(CPU)","NONE")
 endif
 
 # base CFLAGS, LDLIBS, and LDFLAGS
-OPTFLAGS ?= -O3 -flto
+OPTFLAGS ?= -O3
 WARNFLAGS ?= -Wall
 CFLAGS += $(OPTFLAGS) $(WARNFLAGS) -ffast-math -fno-strict-aliasing -fvisibility=hidden -I../../src
 CXXFLAGS += -fvisibility-inlines-hidden
diff --git a/source/mupen64plus-video-rice/src/OGLGraphicsContext.cpp b/source/mupen64plus-video-rice/src/OGLGraphicsContext.cpp
index e4e57ba..b8a8ee3 100644
--- a/source/mupen64plus-video-rice/src/OGLGraphicsContext.cpp
+++ b/source/mupen64plus-video-rice/src/OGLGraphicsContext.cpp
@@ -100,6 +100,14 @@ bool COGLGraphicsContext::Initialize(uint32 dwWidth, uint32 dwHeight, BOOL bWind
     /* Set the video mode */
     m64p_video_mode ScreenMode = bWindowed ? M64VIDEO_WINDOWED : M64VIDEO_FULLSCREEN;
     m64p_video_flags flags = M64VIDEOFLAG_SUPPORT_RESIZING;
+	if (!bWindowed)
+	{
+		ScreenMode = M64VIDEO_WINDOWED;
+		SDL_DisplayMode desktop_mode;
+    	SDL_GetDesktopDisplayMode(0, &desktop_mode);
+		windowSetting.uDisplayWidth = desktop_mode.w;
+		windowSetting.uDisplayHeight = desktop_mode.h;
+	}
     if (CoreVideo_SetVideoMode(windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, colorBufferDepth, ScreenMode, flags) != M64ERR_SUCCESS)
     {
         DebugMessage(M64MSG_ERROR, "Failed to set %i-bit video mode: %ix%i", colorBufferDepth, (int)windowSetting.uDisplayWidth, (int)windowSetting.uDisplayHeight);
@@ -153,12 +161,12 @@ bool COGLGraphicsContext::Initialize(uint32 dwWidth, uint32 dwHeight, BOOL bWind
     InitOGLExtension();
 
     Unlock();
-
+#ifndef __HAIKU__
     Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);    // Clear buffers
     UpdateFrame();
     Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
     UpdateFrame();
-
+#endif
     m_bReady = true;
 
     return true;
@@ -213,7 +221,7 @@ bool COGLGraphicsContext::ResizeInitialize(uint32 dwWidth, uint32 dwHeight, BOOL
     UpdateFrame();
     Clear(CLEAR_COLOR_AND_DEPTH_BUFFER);
     UpdateFrame();
-    
+
     return true;
 }
 
-- 
2.26.0

