From a4dade48889221d8e8a719f79df5baafb7b537df Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 16 Jan 2019 22:00:42 +1000
Subject: Haiku patches


diff --git a/os-posix.c b/os-posix.c
index 4bd80e4..98565ba 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -41,6 +41,15 @@
 #include <sys/prctl.h>
 #endif
 
+#ifdef __HAIKU__
+#ifndef MCL_CURRENT
+#define MCL_CURRENT 8192
+#endif
+#ifndef MCL_FUTURE
+#define MCL_FUTURE 16384
+#endif
+#endif
+
 /*
  * Must set all three of these at once.
  * Legal combinations are              unset   by name   by uid
diff --git a/target/mips/mips-semi.c b/target/mips/mips-semi.c
index a7aefba..16c2336 100644
--- a/target/mips/mips-semi.c
+++ b/target/mips/mips-semi.c
@@ -75,6 +75,7 @@ enum UHIOpenFlags {
 
 /* Errno values taken from asm-mips/errno.h */
 static uint16_t host_to_mips_errno[] = {
+#ifndef __HAIKU__
     [ENAMETOOLONG] = 78,
 #ifdef EOVERFLOW
     [EOVERFLOW]    = 79,
@@ -82,6 +83,9 @@ static uint16_t host_to_mips_errno[] = {
 #ifdef ELOOP
     [ELOOP]        = 90,
 #endif
+#else
+NULL
+#endif
 };
 
 static int errno_mips(int err)
diff --git a/target/xtensa/xtensa-semi.c b/target/xtensa/xtensa-semi.c
index 2f76216..3540ea0 100644
--- a/target/xtensa/xtensa-semi.c
+++ b/target/xtensa/xtensa-semi.c
@@ -96,59 +96,53 @@ enum {
 
 static uint32_t errno_h2g(int host_errno)
 {
-    static const uint32_t guest_errno[] = {
-        [EPERM]         = TARGET_EPERM,
-        [ENOENT]        = TARGET_ENOENT,
-        [ESRCH]         = TARGET_ESRCH,
-        [EINTR]         = TARGET_EINTR,
-        [EIO]           = TARGET_EIO,
-        [ENXIO]         = TARGET_ENXIO,
-        [E2BIG]         = TARGET_E2BIG,
-        [ENOEXEC]       = TARGET_ENOEXEC,
-        [EBADF]         = TARGET_EBADF,
-        [ECHILD]        = TARGET_ECHILD,
-        [EAGAIN]        = TARGET_EAGAIN,
-        [ENOMEM]        = TARGET_ENOMEM,
-        [EACCES]        = TARGET_EACCES,
-        [EFAULT]        = TARGET_EFAULT,
+	switch (host_errno) {
+		case 0: return 0;
+		case EPERM: return TARGET_EPERM;
+		case ENOENT: return TARGET_ENOENT;
+		case ESRCH: return TARGET_ESRCH;
+		case EINTR: return TARGET_EINTR;
+		case EIO: return TARGET_EIO;
+		case ENXIO: return TARGET_ENXIO;
+		case E2BIG: return TARGET_E2BIG;
+		case ENOEXEC: return TARGET_ENOEXEC;
+		case EBADF: return TARGET_EBADF;
+		case ECHILD: return TARGET_ECHILD;
+		case EAGAIN: return TARGET_EAGAIN;
+		case ENOMEM: return TARGET_ENOMEM;
+		case EACCES: return TARGET_EACCES;
+		case EFAULT: return TARGET_EFAULT;
 #ifdef ENOTBLK
-        [ENOTBLK]       = TARGET_ENOTBLK,
+		case ENOTBLK: return TARGET_ENOTBLK;
 #endif
-        [EBUSY]         = TARGET_EBUSY,
-        [EEXIST]        = TARGET_EEXIST,
-        [EXDEV]         = TARGET_EXDEV,
-        [ENODEV]        = TARGET_ENODEV,
-        [ENOTDIR]       = TARGET_ENOTDIR,
-        [EISDIR]        = TARGET_EISDIR,
-        [EINVAL]        = TARGET_EINVAL,
-        [ENFILE]        = TARGET_ENFILE,
-        [EMFILE]        = TARGET_EMFILE,
-        [ENOTTY]        = TARGET_ENOTTY,
+		case EBUSY: return TARGET_EBUSY;
+		case EEXIST: return TARGET_EEXIST;
+		case EXDEV: return TARGET_EXDEV;
+		case ENODEV: return TARGET_ENODEV;
+		case ENOTDIR: return TARGET_ENOTDIR;
+		case EISDIR: return TARGET_EISDIR;
+		case EINVAL: return TARGET_EINVAL;
+		case ENFILE: return TARGET_ENFILE;
+		case EMFILE: return TARGET_EMFILE;
+		case ENOTTY: return TARGET_ENOTTY;
 #ifdef ETXTBSY
-        [ETXTBSY]       = TARGET_ETXTBSY,
+		case ETXTBSY: return TARGET_ETXTBSY;
 #endif
-        [EFBIG]         = TARGET_EFBIG,
-        [ENOSPC]        = TARGET_ENOSPC,
-        [ESPIPE]        = TARGET_ESPIPE,
-        [EROFS]         = TARGET_EROFS,
-        [EMLINK]        = TARGET_EMLINK,
-        [EPIPE]         = TARGET_EPIPE,
-        [EDOM]          = TARGET_EDOM,
-        [ERANGE]        = TARGET_ERANGE,
-        [ENOSYS]        = TARGET_ENOSYS,
+		case EFBIG: return TARGET_EFBIG;
+		case ENOSPC: return TARGET_ENOSPC;
+		case ESPIPE: return TARGET_ESPIPE;
+		case EROFS: return TARGET_EROFS;
+		case EMLINK: return TARGET_EMLINK;
+		case EPIPE: return TARGET_EPIPE;
+		case EDOM: return TARGET_EDOM;
+		case ERANGE: return TARGET_ERANGE;
+		case ENOSYS: return TARGET_ENOSYS;
 #ifdef ELOOP
-        [ELOOP]         = TARGET_ELOOP,
+		case ELOOP: return TARGET_ELOOP;
 #endif
     };
 
-    if (host_errno == 0) {
-        return 0;
-    } else if (host_errno > 0 && host_errno < ARRAY_SIZE(guest_errno) &&
-            guest_errno[host_errno]) {
-        return guest_errno[host_errno];
-    } else {
-        return TARGET_EINVAL;
-    }
+    return TARGET_EINVAL;
 }
 
 typedef struct XtensaSimConsole {
diff --git a/tests/Makefile.include b/tests/Makefile.include
index fb0b449..6780e70 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -778,7 +778,7 @@ tests/migration/initrd-stress.img: tests/migration/stress$(EXESUF)
 	rmdir $(INITRD_WORK_DIR)
 
 ifeq ($(CONFIG_POSIX),y)
-LIBS += -lutil
+LIBS += 
 endif
 
 # QTest rules
diff --git a/util/compatfd.c b/util/compatfd.c
index 980bd33..31e01d6 100644
--- a/util/compatfd.c
+++ b/util/compatfd.c
@@ -17,7 +17,9 @@
 #include "qemu-common.h"
 #include "qemu/thread.h"
 
+#ifndef __HAIKU__
 #include <sys/syscall.h>
+#endif
 
 struct sigfd_compat_info
 {
diff --git a/util/main-loop.c b/util/main-loop.c
index affe040..de89c2c 100644
--- a/util/main-loop.c
+++ b/util/main-loop.c
@@ -83,7 +83,9 @@ static int qemu_signal_init(void)
      */
     sigemptyset(&set);
     sigaddset(&set, SIG_IPI);
+#ifdef SIGIO
     sigaddset(&set, SIGIO);
+#endif
     sigaddset(&set, SIGALRM);
     sigaddset(&set, SIGBUS);
     /* SIGINT cannot be handled via signalfd, so that ^C can be used
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index c1bee2a..b1011b0 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -36,7 +36,9 @@
 #include "qapi/error.h"
 #include "qemu/sockets.h"
 #include <libgen.h>
+#ifndef __HAIKU__
 #include <sys/signal.h>
+#endif
 #include "qemu/cutils.h"
 
 #ifdef CONFIG_LINUX
diff --git a/util/qemu-openpty.c b/util/qemu-openpty.c
index 2e8b43b..adcc732 100644
--- a/util/qemu-openpty.c
+++ b/util/qemu-openpty.c
@@ -35,7 +35,9 @@
 #include "qemu/osdep.h"
 #include "qemu-common.h"
 
-#if defined(__GLIBC__)
+#if defined(__HAIKU__)
+# include <termios.h>
+#elif defined(__GLIBC__)
 # include <pty.h>
 #elif defined CONFIG_BSD
 # include <termios.h>
-- 
2.19.1


From 47e776e4f6736ac5cc283747e810ecc6a16f45aa Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 16 Jan 2019 22:01:29 +1000
Subject: Haiku build fix


diff --git a/configure b/configure
index 0a3c6a7..2013a19 100755
--- a/configure
+++ b/configure
@@ -865,8 +865,8 @@ SunOS)
 ;;
 Haiku)
   haiku="yes"
-  QEMU_CFLAGS="-DB_USE_POSITIVE_POSIX_ERRORS $QEMU_CFLAGS"
-  LIBS="-lposix_error_mapper -lnetwork $LIBS"
+  QEMU_CFLAGS="-DB_USE_POSITIVE_POSIX_ERRORS -D_BSD_SOURCE $QEMU_CFLAGS"
+  LIBS="-lposix_error_mapper -lnetwork -lbsd $LIBS"
 ;;
 Linux)
   audio_drv_list="oss"
diff --git a/include/qemu/bswap.h b/include/qemu/bswap.h
index a684c1a..09bdb7f 100644
--- a/include/qemu/bswap.h
+++ b/include/qemu/bswap.h
@@ -8,6 +8,8 @@
 # include <machine/bswap.h>
 #elif defined(__FreeBSD__)
 # include <sys/endian.h>
+#elif defined(__HAIKU__)
+# include <endian.h>
 #elif defined(CONFIG_BYTESWAP_H)
 # include <byteswap.h>
 
-- 
2.19.1


From b264891c1f3d2e9d40e0fc8f082e304e7a3295bf Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 16 Jan 2019 22:02:47 +1000
Subject: Fix keymap for Haiku


diff --git a/ui/input-keymap-haiku-to-qcode.h b/ui/input-keymap-haiku-to-qcode.h
new file mode 100644
index 0000000..c3b4fff
--- /dev/null
+++ b/ui/input-keymap-haiku-to-qcode.h
@@ -0,0 +1,105 @@
+const guint16 qemu_input_map_haiku_to_qcode[] = {
+  [0x00] = Q_KEY_CODE_UNMAPPED,
+  [0x01] = Q_KEY_CODE_ESC,
+  [0x02] = Q_KEY_CODE_F1,
+  [0x03] = Q_KEY_CODE_F2,
+  [0x04] = Q_KEY_CODE_F3,
+  [0x05] = Q_KEY_CODE_F4,
+  [0x06] = Q_KEY_CODE_F5,
+  [0x07] = Q_KEY_CODE_F6,
+  [0x08] = Q_KEY_CODE_F7,
+  [0x09] = Q_KEY_CODE_F8,
+  [0x0a] = Q_KEY_CODE_F9,
+  [0x0b] = Q_KEY_CODE_F10,
+  [0x0c] = Q_KEY_CODE_F11,
+  [0x0d] = Q_KEY_CODE_F12,
+  [0x0e] = Q_KEY_CODE_SYSRQ,
+  [0x0f] = Q_KEY_CODE_SCROLL_LOCK,
+  [0x10] = Q_KEY_CODE_UNMAPPED, // ?
+  [0x11] = Q_KEY_CODE_GRAVE_ACCENT,
+  [0x12] = Q_KEY_CODE_1,
+  [0x13] = Q_KEY_CODE_2,
+  [0x14] = Q_KEY_CODE_3,
+  [0x15] = Q_KEY_CODE_4,
+  [0x16] = Q_KEY_CODE_5,
+  [0x17] = Q_KEY_CODE_6,
+  [0x18] = Q_KEY_CODE_7,
+  [0x19] = Q_KEY_CODE_8,
+  [0x1a] = Q_KEY_CODE_9,
+  [0x1b] = Q_KEY_CODE_0,
+  [0x1c] = Q_KEY_CODE_MINUS,
+  [0x1d] = Q_KEY_CODE_EQUAL,
+  [0x1e] = Q_KEY_CODE_BACKSPACE,
+  [0x1f] = Q_KEY_CODE_INSERT,
+  [0x20] = Q_KEY_CODE_HOME,
+  [0x21] = Q_KEY_CODE_PGUP,
+  [0x22] = Q_KEY_CODE_NUM_LOCK,
+  [0x23] = Q_KEY_CODE_KP_DIVIDE,
+  [0x24] = Q_KEY_CODE_KP_MULTIPLY,
+  [0x25] = Q_KEY_CODE_KP_SUBTRACT,
+  [0x26] = Q_KEY_CODE_TAB,
+  [0x27] = Q_KEY_CODE_Q,
+  [0x28] = Q_KEY_CODE_W,
+  [0x29] = Q_KEY_CODE_E,
+  [0x2a] = Q_KEY_CODE_R,
+  [0x2b] = Q_KEY_CODE_T,
+  [0x2c] = Q_KEY_CODE_Y,
+  [0x2d] = Q_KEY_CODE_U,
+  [0x2e] = Q_KEY_CODE_I,
+  [0x2f] = Q_KEY_CODE_O,
+  [0x30] = Q_KEY_CODE_P,
+  [0x31] = Q_KEY_CODE_BRACKET_LEFT,
+  [0x32] = Q_KEY_CODE_BRACKET_RIGHT,
+  [0x33] = Q_KEY_CODE_BACKSLASH,
+  [0x34] = Q_KEY_CODE_DELETE,
+  [0x35] = Q_KEY_CODE_END,
+  [0x36] = Q_KEY_CODE_PGDN,
+  [0x37] = Q_KEY_CODE_KP_7,
+  [0x38] = Q_KEY_CODE_KP_8,
+  [0x39] = Q_KEY_CODE_KP_9,
+  [0x3a] = Q_KEY_CODE_KP_ADD,
+  [0x3b] = Q_KEY_CODE_CAPS_LOCK,
+  [0x3c] = Q_KEY_CODE_A,
+  [0x3d] = Q_KEY_CODE_S,
+  [0x3e] = Q_KEY_CODE_D,
+  [0x3f] = Q_KEY_CODE_F,
+  [0x40] = Q_KEY_CODE_G,
+  [0x41] = Q_KEY_CODE_H,
+  [0x42] = Q_KEY_CODE_J,
+  [0x43] = Q_KEY_CODE_K,
+  [0x44] = Q_KEY_CODE_L,
+  [0x45] = Q_KEY_CODE_SEMICOLON,
+  [0x46] = Q_KEY_CODE_APOSTROPHE,
+  [0x47] = Q_KEY_CODE_RET,
+  [0x48] = Q_KEY_CODE_KP_4,
+  [0x49] = Q_KEY_CODE_KP_5,
+  [0x4a] = Q_KEY_CODE_KP_6,
+  [0x4b] = Q_KEY_CODE_SHIFT,
+  [0x4c] = Q_KEY_CODE_Z,
+  [0x4d] = Q_KEY_CODE_X,
+  [0x4e] = Q_KEY_CODE_C,
+  [0x4f] = Q_KEY_CODE_V,
+  [0x50] = Q_KEY_CODE_B,
+  [0x51] = Q_KEY_CODE_N,
+  [0x52] = Q_KEY_CODE_M,
+  [0x53] = Q_KEY_CODE_COMMA,
+  [0x54] = Q_KEY_CODE_DOT,
+  [0x55] = Q_KEY_CODE_SLASH,
+  [0x56] = Q_KEY_CODE_SHIFT_R,
+  [0x57] = Q_KEY_CODE_UP,
+  [0x58] = Q_KEY_CODE_KP_1,
+  [0x59] = Q_KEY_CODE_KP_2,
+  [0x5a] = Q_KEY_CODE_KP_3,
+  [0x5b] = Q_KEY_CODE_KP_ENTER,
+  [0x5c] = Q_KEY_CODE_CTRL,
+  [0x5d] = Q_KEY_CODE_ALT,
+  [0x5e] = Q_KEY_CODE_SPC,
+  [0x5f] = Q_KEY_CODE_ALT_R,
+  [0x60] = Q_KEY_CODE_CTRL_R,
+  [0x61] = Q_KEY_CODE_LEFT,
+  [0x62] = Q_KEY_CODE_DOWN,
+  [0x63] = Q_KEY_CODE_RIGHT,
+  [0x64] = Q_KEY_CODE_KP_0,
+  [0x65] = Q_KEY_CODE_KP_DECIMAL
+};
+const guint qemu_input_map_haiku_to_qcode_len = sizeof(qemu_input_map_haiku_to_qcode)/sizeof(qemu_input_map_haiku_to_qcode[0]);
diff --git a/ui/sdl.c b/ui/sdl.c
index 190b16f..b2c0590 100644
--- a/ui/sdl.c
+++ b/ui/sdl.c
@@ -35,9 +35,12 @@
 #include "ui/console.h"
 #include "ui/input.h"
 #include "sysemu/sysemu.h"
-#ifndef WIN32
+#if !defined(WIN32) && !defined(__HAIKU__)
 #include "x_keymap.h"
 #endif
+#if defined(__HAIKU__)
+#include "input-keymap-haiku-to-qcode.h"
+#endif
 #include "sdl_zoom.h"
 
 static DisplayChangeListener *dcl;
@@ -224,6 +227,9 @@ static const guint16 *sdl_get_keymap(size_t *maplen)
 #if defined(WIN32)
     *maplen = qemu_input_map_atset1_to_qcode_len;
     return qemu_input_map_atset1_to_qcode;
+#elif defined(__HAIKU__)
+    *maplen = qemu_input_map_haiku_to_qcode_len;
+    return qemu_input_map_haiku_to_qcode;
 #else
 #if defined(SDL_VIDEO_DRIVER_X11)
     SDL_SysWMinfo info;
diff --git a/ui/x_keymap.c b/ui/x_keymap.c
index 2bc0143..6e29686 100644
--- a/ui/x_keymap.c
+++ b/ui/x_keymap.c
@@ -9,6 +9,7 @@
  * published by the Free Software Foundation.
  */
 
+#ifndef __HAIKU__
 #include "qemu/osdep.h"
 
 #include "x_keymap.h"
@@ -112,3 +113,4 @@ const guint16 *qemu_xkeymap_mapping_table(Display *dpy, size_t *maplen)
         return NULL;
     }
 }
+#endif
diff --git a/ui/x_keymap.h b/ui/x_keymap.h
index 0395e33..96f4812 100644
--- a/ui/x_keymap.h
+++ b/ui/x_keymap.h
@@ -25,8 +25,11 @@
 #ifndef QEMU_X_KEYMAP_H
 #define QEMU_X_KEYMAP_H
 
+#ifndef __HAIKU__
+
 #include <X11/Xlib.h>
 
 const guint16 *qemu_xkeymap_mapping_table(Display *dpy, size_t *maplen);
 
 #endif
+#endif
diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index 865e476..faa3d9a 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -293,7 +293,11 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
         }
     }
     if (rc < 0) {
+#ifndef __HAIKU__
         error_exit(errno, __func__);
+#else
+		return -1;
+#endif
     }
     return 0;
 #endif
-- 
2.19.1


From a4a7a33d9a708302d158f64767ce0c368c0490c7 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 16 Jan 2019 22:03:12 +1000
Subject: Fix crash on qemu-img exit


diff --git a/util/notify.c b/util/notify.c
index 06de63a..9f85c2d 100644
--- a/util/notify.c
+++ b/util/notify.c
@@ -37,6 +37,9 @@ void notifier_list_notify(NotifierList *list, void *data)
     Notifier *notifier, *next;
 
     QLIST_FOREACH_SAFE(notifier, &list->notifiers, node, next) {
+#ifdef __HAIKU__
+	if(notifier->notify == NULL)break;
+#endif
         notifier->notify(notifier, data);
     }
 }
-- 
2.19.1


From c0e278138903513f4c747572421e371436dc980a Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 16 Jan 2019 23:29:06 +1000
Subject: Use system int defs


diff --git a/disas/nanomips.h b/disas/nanomips.h
index 84cc9a6..742a867 100644
--- a/disas/nanomips.h
+++ b/disas/nanomips.h
@@ -24,10 +24,14 @@
 
 #include <string>
 
+#ifndef __HAIKU__
 typedef unsigned short uint16;
 typedef unsigned int uint32;
 typedef long long int64;
 typedef unsigned long long uint64;
+#else
+#include <SupportDefs.h>
+#endif
 
 namespace img
 {
-- 
2.19.1


From 6be2d318e0899d9e58070e9819904f6040e705b1 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 16 Jan 2019 23:29:29 +1000
Subject: Disable mlockall for Haiku


diff --git a/os-posix.c b/os-posix.c
index 98565ba..668a405 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -361,11 +361,11 @@ bool is_daemonized(void)
 int os_mlock(void)
 {
     int ret = 0;
-
+#ifndef __HAIKU__
     ret = mlockall(MCL_CURRENT | MCL_FUTURE);
     if (ret < 0) {
         error_report("mlockall: %s", strerror(errno));
     }
-
+#endif
     return ret;
 }
-- 
2.19.1

