From 065faeb7d353e16aea165701e1e36f75f50d1bce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zolt=C3=A1n=20Mizsei?= <zmizsei@extrowerk.com>
Date: Sun, 6 Aug 2017 14:19:55 +0200
Subject: FS-UAE: Tidied up Haiku patchset


diff --git a/Makefile.am b/Makefile.am
index d00bbb9..7c97a4d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -751,6 +751,7 @@ libfsemu_a_SOURCES = \
 	libfsemu/src/ml/input.c \
 	libfsemu/src/ml/keyboard.c \
 	libfsemu/src/ml/macosx.c \
+	libfsemu/src/ml/haiku.c \
 	libfsemu/src/ml/ml.c \
 	libfsemu/src/ml/ml_internal.h \
 	libfsemu/src/ml/mouse.c \
diff --git a/configure.ac b/configure.ac
index 168533b..c8c4a80 100644
--- a/configure.ac
+++ b/configure.ac
@@ -270,6 +270,7 @@ AC_DEFINE([USE_SDL_INPUT], [1], [Define to 1 to use SDL for input])
 AS_CASE([$host_os],
 [mingw*], [],
 [darwin*], [],
+[haiku*], [],
 [
     X11_LIBS="-lX11"
     AC_DEFINE([USE_X11], [1], [Define to 1 to use X11])
@@ -352,6 +353,13 @@ AS_CASE([$host_os],
     OS_CPPFLAGS="-DDRAGONFLYBSD"
     LOADABLE_MODULE_EXT=".so"
 ],
+[haiku*], [
+    OS_NAME="haiku"
+    OS_NAME_2="Haiku"
+    OS_NAME_3="Haiku"
+    OS_CPPFLAGS="-DHAIKU"
+    LOADABLE_MODULE_EXT=".so"
+],
 [
     OS_NAME="unknown"
     OS_NAME_2="Unknown OS"
diff --git a/libfsemu/include/fs/endian.h b/libfsemu/include/fs/endian.h
index 01ee0f9..a276e10 100644
--- a/libfsemu/include/fs/endian.h
+++ b/libfsemu/include/fs/endian.h
@@ -7,6 +7,10 @@
 
 #include <stdint.h>
 
+#ifdef __HAIKU__
+#include <endian.h>
+#endif
+
 #ifdef _WIN32
 
 /* Use custom conversion routines for Windows */
diff --git a/libfsemu/src/ml/haiku.c b/libfsemu/src/ml/haiku.c
new file mode 100644
index 0000000..f839d2a
--- /dev/null
+++ b/libfsemu/src/ml/haiku.c
@@ -0,0 +1,127 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef HAIKU
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+
+#include <fs/log.h>
+#include <fs/ml.h>
+#include "ml_internal.h"
+
+void fs_ml_configure_window(void)
+{
+	// TODO
+}
+
+
+void fs_ml_usleep(int usec) {
+    usleep(usec);
+}
+
+void fs_ml_prevent_power_saving(void)
+{
+	// TODO
+}
+
+void fs_ml_activate_window_switcher_impl(void)
+{
+    fs_log("FIXME: Active window switcher not implemented\n");
+}
+
+#define MAX_SCANCODES 200
+static int g_key_map[MAX_SCANCODES] = {};
+
+void fs_ml_initialize_keymap() {
+    for (int i = 0; i < MAX_SCANCODES; i++) {
+        g_key_map[i] = -1;
+    }
+
+    g_key_map[53] = FS_ML_KEY_ESCAPE;
+    g_key_map[122] = FS_ML_KEY_F1;
+    g_key_map[120] = FS_ML_KEY_F2;
+    g_key_map[99] = FS_ML_KEY_F3;
+    g_key_map[118] = FS_ML_KEY_F4;
+    g_key_map[96] = FS_ML_KEY_F5;
+    g_key_map[97] = FS_ML_KEY_F6;
+    g_key_map[98] = FS_ML_KEY_F7;
+    g_key_map[100] = FS_ML_KEY_F8;
+    g_key_map[101] = FS_ML_KEY_F9;
+    g_key_map[109] = FS_ML_KEY_F10;
+    g_key_map[103] = FS_ML_KEY_F11;
+    g_key_map[111] = FS_ML_KEY_F12;
+
+    g_key_map[10] = FS_ML_KEY_BACKQUOTE;
+    g_key_map[18] = FS_ML_KEY_1;
+    g_key_map[19] = FS_ML_KEY_2;
+    g_key_map[20] = FS_ML_KEY_3;
+    g_key_map[21] = FS_ML_KEY_4;
+    g_key_map[23] = FS_ML_KEY_5;
+    g_key_map[22] = FS_ML_KEY_6;
+    g_key_map[26] = FS_ML_KEY_7;
+    g_key_map[28] = FS_ML_KEY_8;
+    g_key_map[25] = FS_ML_KEY_9;
+    g_key_map[29] = FS_ML_KEY_0;
+    g_key_map[27] = FS_ML_KEY_MINUS;
+    g_key_map[24] = FS_ML_KEY_EQUALS;
+    g_key_map[15] = FS_ML_KEY_BACKSPACE;
+
+    g_key_map[48] = FS_ML_KEY_TAB;
+    g_key_map[12] = FS_ML_KEY_Q;
+    g_key_map[13] = FS_ML_KEY_W;
+    g_key_map[14] = FS_ML_KEY_E;
+    g_key_map[15] = FS_ML_KEY_R;
+    g_key_map[17] = FS_ML_KEY_T;
+    g_key_map[16] = FS_ML_KEY_Y;
+    g_key_map[32] = FS_ML_KEY_U;
+    g_key_map[34] = FS_ML_KEY_I;
+    g_key_map[31] = FS_ML_KEY_O;
+    g_key_map[35] = FS_ML_KEY_P;
+    g_key_map[33] = FS_ML_KEY_LEFTBRACKET;
+    g_key_map[30] = FS_ML_KEY_RIGHTBRACKET;
+    g_key_map[36] = FS_ML_KEY_RETURN;
+
+    g_key_map[0] = FS_ML_KEY_A;
+    g_key_map[1] = FS_ML_KEY_S;
+    g_key_map[2] = FS_ML_KEY_D;
+    g_key_map[3] = FS_ML_KEY_F;
+    g_key_map[5] = FS_ML_KEY_G;
+    g_key_map[4] = FS_ML_KEY_H;
+    g_key_map[38] = FS_ML_KEY_J;
+    g_key_map[40] = FS_ML_KEY_K;
+    g_key_map[37] = FS_ML_KEY_L;
+    g_key_map[41] = FS_ML_KEY_SEMICOLON;
+    g_key_map[39] = FS_ML_KEY_QUOTE;
+    g_key_map[42] = FS_ML_KEY_BACKSLASH;
+
+    g_key_map[50] = FS_ML_KEY_LESS;
+    g_key_map[6] = FS_ML_KEY_Z;
+    g_key_map[7] = FS_ML_KEY_X;
+    g_key_map[8] = FS_ML_KEY_C;
+    g_key_map[9] = FS_ML_KEY_V;
+    g_key_map[11] = FS_ML_KEY_B;
+    g_key_map[45] = FS_ML_KEY_N;
+    g_key_map[46] = FS_ML_KEY_M;
+    g_key_map[43] = FS_ML_KEY_COMMA;
+    g_key_map[47] = FS_ML_KEY_PERIOD;
+    g_key_map[44] = FS_ML_KEY_SLASH;
+    g_key_map[49] = FS_ML_KEY_SPACE;
+    g_key_map[110] = FS_ML_KEY_MENU;
+
+    g_key_map[105] = FS_ML_KEY_PRINT;
+    g_key_map[107] = FS_ML_KEY_SCROLLOCK;
+    g_key_map[113] = FS_ML_KEY_PAUSE;
+}
+
+int fs_ml_scancode_to_key(int scancode) {
+    if (scancode < 0 || scancode >= MAX_SCANCODES) {
+        return -1;
+    }
+    return g_key_map[scancode];
+}
+
+#endif
diff --git a/manymouse/x11_xinput2.c b/manymouse/x11_xinput2.c
index bdbda8c..21db919 100644
--- a/manymouse/x11_xinput2.c
+++ b/manymouse/x11_xinput2.c
@@ -10,7 +10,7 @@
 #include "manymouse.h"
 /* Try to use this on everything but Windows and Mac OS by default... */
 #ifndef SUPPORT_XINPUT2
-#if ( (defined(_WIN32) || defined(__CYGWIN__)) )
+#if ( (defined(_WIN32) || defined(__CYGWIN__) || defined(__HAIKU__)) )
 #define SUPPORT_XINPUT2 0
 #elif ( (defined(__MACH__)) && (defined(__APPLE__)) )
 #define SUPPORT_XINPUT2 0
diff --git a/src/fs-uae/main.c b/src/fs-uae/main.c
index f3f0370..6aab284 100644
--- a/src/fs-uae/main.c
+++ b/src/fs-uae/main.c
@@ -40,6 +40,10 @@ static int fs_uae_argc;
 static char **fs_uae_argv;
 static int g_warn_about_missing_config_file;
 
+#ifdef __HAIKU__
+int log_bsd = 0;
+#endif
+
 #define LOG_LINE "---------------------------------------------------------" \
         "-------------------\n"
 
diff --git a/src/scp.cpp b/src/scp.cpp
index 0d7a6a5..5f35864 100644
--- a/src/scp.cpp
+++ b/src/scp.cpp
@@ -21,6 +21,10 @@
 
 #define MAX_REVS 5
 
+#ifdef __HAIKU__
+#include <endian.h>
+#endif
+
 enum pll_mode {
     PLL_fixed_clock, /* Fixed clock, snap phase to flux transitions. */
     PLL_variable_clock, /* Variable clock, snap phase to flux transitions. */
-- 
2.13.1


From b33930a42cf51826937c2290468857dbf25a1f1d Mon Sep 17 00:00:00 2001
From: Chris Moore <chris@mooreonline.org>
Date: Mon, 7 Aug 2017 19:13:30 +0100
Subject: Further fixes


diff --git a/src/ethernet.cpp b/src/ethernet.cpp
index 115aa5a..d2f2963 100644
--- a/src/ethernet.cpp
+++ b/src/ethernet.cpp
@@ -14,7 +14,7 @@
 #include "uae/slirp.h"
 
 #ifndef HAVE_INET_ATON
-static int inet_aton(const char *cp, struct in_addr *ia)
+int inet_aton(const char *cp, struct in_addr *ia)
 {
 	uint32_t addr = inet_addr(cp);
 	if (addr == 0xffffffff)
diff --git a/src/jit/exception_handler.cpp b/src/jit/exception_handler.cpp
index 8cf96f8..2e8b873 100644
--- a/src/jit/exception_handler.cpp
+++ b/src/jit/exception_handler.cpp
@@ -9,8 +9,10 @@
 #ifndef __USE_GNU
 #define __USE_GNU
 #endif
+#ifndef __HAIKU__
 #include <sys/ucontext.h>
 #endif
+#endif
 #include <signal.h>
 
 #define SIG_READ 1
-- 
2.13.1


From b3076a6b080f81acea707d4c74557b109f8e3da8 Mon Sep 17 00:00:00 2001
From: Chris Moore <chris@mooreonline.org>
Date: Tue, 8 Aug 2017 19:00:45 +0100
Subject: Fix Haiku fs-uae.dat file location


diff --git a/libfsemu/src/data.c b/libfsemu/src/data.c
index 2d597ad..a5239e7 100644
--- a/libfsemu/src/data.c
+++ b/libfsemu/src/data.c
@@ -12,6 +12,10 @@
 #include <stdint.h>
 #include <glib.h>
 
+#ifdef __HAIKU__
+#include <FindDirectory.h>
+#endif
+
 #ifdef USE_ZLIB
 #include "zlib.h"
 #endif
@@ -339,6 +343,20 @@ int fs_data_init(const char *app_name, const char *dat_name)
     }
 #endif
 
+#ifdef __HAIKU__
+    char haiku_data_path[PATH_MAX + 1];
+    status_t status = find_path(fs_data_init, B_FIND_PATH_DATA_DIRECTORY,
+        "fs-uae", haiku_data_path, PATH_MAX);
+    if (status == B_OK) {
+        if (g_dat_file == NULL) {
+            char *dat_path = g_build_filename(haiku_data_path, dat_name, NULL);
+            fs_log("checking dat file: %s\n", dat_path);
+            g_dat_file = g_fopen(dat_path, "rb");
+            free(dat_path);
+        }
+    }
+#endif
+
     if (g_dat_file == NULL) {
         error = 10;
     } else {
-- 
2.13.1


From 17456f069bc3c8b6f8f0faad51ece2528830349f Mon Sep 17 00:00:00 2001
From: Chris Moore <chris@mooreonline.org>
Date: Sun, 10 Sep 2017 09:40:21 +0100
Subject: Fix conflicting typedefs for uint32


diff --git a/src/include/gfxfilter.h b/src/include/gfxfilter.h
index 914b517..578d6b5 100644
--- a/src/include/gfxfilter.h
+++ b/src/include/gfxfilter.h
@@ -22,7 +22,11 @@ extern void PAL_1x1_16 (uae_u16 *src, int pitchs, uae_u16 *trg, int pitcht, int
 
 typedef unsigned char uint8;
 typedef unsigned short uint16;
+#ifndef __HAIKU__
 typedef unsigned long uint32;
+#else
+#include <SupportDefs.h>
+#endif
 #ifndef __cplusplus
 typedef int bool;
 #endif
-- 
2.13.1

