From 66a5d896959b7d3f52f7b1d4e0929f6480950e85 Mon Sep 17 00:00:00 2001
From: Sergei Reznikov <diver@gelios.net>
Date: Wed, 30 Jan 2019 22:39:57 +0300
Subject: WIP: fix build on Haiku


diff --git a/CMakeLists.txt b/CMakeLists.txt
index 553f1e6..5f79177 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -33,8 +33,9 @@ set(QT_MINIMUM_VERSION "5.7.1")
 
 find_package(Qt5Widgets "${QT_MINIMUM_VERSION}" REQUIRED)
 find_package(Qt5LinguistTools "${QT_MINIMUM_VERSION}" REQUIRED)
-find_package(Qt5X11Extras "${QT_MINIMUM_VERSION}" REQUIRED)
-
+if (UNIX AND NOT HAIKU)
+	find_package(Qt5X11Extras "${REQUIRED_QT_VERSION}" REQUIRED)
+endif()
 find_package(lxqt-build-tools "${LXQTBT_MINIMUM_VERSION}" REQUIRED)
 find_package(GLIB "${GLIB_MINIMUM_VERSION}" REQUIRED COMPONENTS gio gio-unix gobject gthread)
 find_package(MenuCache "${LIBMENUCACHE_MINIMUM_VERSION}" REQUIRED)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ec9d9c2..19ee0e9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -131,9 +131,14 @@ install(EXPORT
     COMPONENT Devel
 )
 
+IF(UNIX AND NOT HAIKU)
+target_link_libraries(${LIBFM_QT_LIBRARY_NAME}
+	Qt5::X11Extras
+)
+endif()
+
 target_link_libraries(${LIBFM_QT_LIBRARY_NAME}
     Qt5::Widgets
-    Qt5::X11Extras
     ${GLIB_LIBRARIES}
     ${GLIB_GIO_LIBRARIES}
     ${GLIB_GOBJECT_LIBRARIES}
diff --git a/src/applaunchcontext.cpp b/src/applaunchcontext.cpp
index e599485..ea07648 100644
--- a/src/applaunchcontext.cpp
+++ b/src/applaunchcontext.cpp
@@ -19,8 +19,11 @@
 
 
 #include "applaunchcontext.h"
-#include <QX11Info>
-#include <X11/Xlib.h>
+
+#ifndef Q_OS_HAIKU
+ #include <QX11Info>
+ #include <X11/Xlib.h>
+#endif
 
 typedef struct _FmAppLaunchContext {
   GAppLaunchContext parent;
@@ -29,11 +32,13 @@ typedef struct _FmAppLaunchContext {
 G_DEFINE_TYPE(FmAppLaunchContext, fm_app_launch_context, G_TYPE_APP_LAUNCH_CONTEXT)
 
 static char* fm_app_launch_context_get_display(GAppLaunchContext * /*context*/, GAppInfo * /*info*/, GList * /*files*/) {
+#ifndef Q_OS_HAIKU
   Display* dpy = QX11Info::display();
   if(dpy) {
     char* xstr = DisplayString(dpy);
     return g_strdup(xstr);
   }
+#endif
   return nullptr;
 }
 
diff --git a/src/core/thumbnailjob.cpp b/src/core/thumbnailjob.cpp
index bddd7de..ec2940d 100644
--- a/src/core/thumbnailjob.cpp
+++ b/src/core/thumbnailjob.cpp
@@ -9,6 +9,15 @@
 
 #include "core/legacy/fm-config.h"
 
+#ifdef Q_OS_HAIKU
+static inline void *
+mempcpy(void *to, const void *from, size_t size)
+{
+  memcpy(to, from, size);
+  return (unsigned char *)to + size;
+}
+#endif
+
 namespace Fm {
 
 QThreadPool* ThumbnailJob::threadPool_ = nullptr;
diff --git a/src/folderview.cpp b/src/folderview.cpp
index d1930e8..0bae5fe 100644
--- a/src/folderview.cpp
+++ b/src/folderview.cpp
@@ -42,7 +42,9 @@
 #include <QMessageBox>
 #include <QLineEdit>
 #include <QTextEdit>
-#include <QX11Info> // for XDS support
+#ifndef Q_OS_HAIKU
+ #include <QX11Info> // for XDS support
+#endif
 #include <xcb/xcb.h> // for XDS support
 #include "xdndworkaround.h" // for XDS support
 #include "folderview_p.h"
@@ -1092,6 +1094,7 @@ void FolderView::childDropEvent(QDropEvent* e) {
     // NOTE: in theory, it's not possible to implement XDS with pure Qt.
     // We achieved this with some dirty XCB/XDND workarounds.
     // Please refer to XdndWorkaround::clientMessage() in xdndworkaround.cpp for details.
+#ifndef Q_OS_HAIKU
     if(QX11Info::isPlatformX11() && e->mimeData()->hasFormat("XdndDirectSave0")) {
         e->setDropAction(Qt::CopyAction);
         const QWidget* targetWidget = childView()->viewport();
@@ -1130,7 +1133,7 @@ void FolderView::childDropEvent(QDropEvent* e) {
         e->accept(); // yeah! we've done with XDS so stop Qt from further event propagation.
         return;
     }
-
+#endif
     if(e->keyboardModifiers() == Qt::NoModifier) {
         // if no key modifiers are used, popup a menu
         // to ask the user for the action he/she wants to perform.
diff --git a/src/libfmqt.cpp b/src/libfmqt.cpp
index 83905ee..c7c060b 100644
--- a/src/libfmqt.cpp
+++ b/src/libfmqt.cpp
@@ -32,7 +32,9 @@ struct LibFmQtData {
     ~LibFmQtData();
 
     QTranslator translator;
+#ifndef Q_OS_HAIKU
     XdndWorkaround workaround;
+#endif
     int refCount;
     Q_DISABLE_COPY(LibFmQtData)
 };
diff --git a/src/xdndworkaround.cpp b/src/xdndworkaround.cpp
index f0a35d2..65de54a 100644
--- a/src/xdndworkaround.cpp
+++ b/src/xdndworkaround.cpp
@@ -21,7 +21,9 @@
 #include "xdndworkaround.h"
 #include <QApplication>
 #include <QDebug>
-#include <QX11Info>
+#ifndef Q_OS_HAIKU
+ #include <QX11Info>
+#endif
 #include <QMimeData>
 #include <QCursor>
 #include <QWidget>
@@ -44,72 +46,24 @@
 #endif // (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
 
 XdndWorkaround::XdndWorkaround() {
-    if(!QX11Info::isPlatformX11()) {
+//    if(!QX11Info::isPlatformX11()) {
         return;
     }
 
     // we need to filter all X11 events
-    qApp->installNativeEventFilter(this);
+//    qApp->installNativeEventFilter(this);
 
-// This part is for Qt >= 5.4 only
-#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
-    lastDrag_ = nullptr;
 
-    // initialize xinput2 since newer versions of Qt5 uses it.
-    static char xi_name[] = "XInputExtension";
-    xcb_connection_t* conn = QX11Info::connection();
-    xcb_query_extension_cookie_t cookie = xcb_query_extension(conn, strlen(xi_name), xi_name);
-    xcb_generic_error_t* err = nullptr;
-    xcb_query_extension_reply_t* reply = xcb_query_extension_reply(conn, cookie, &err);
-    if(err == nullptr) {
-        xinput2Enabled_ = true;
-        xinputOpCode_ = reply->major_opcode;
-        xinputEventBase_ = reply->first_event;
-        xinputErrorBase_ = reply->first_error;
-        // qDebug() << "xinput: " << m_xi2Enabled << m_xiOpCode << m_xiEventBase;
-    }
-    else {
-        xinput2Enabled_ = false;
-        free(err);
-    }
-    free(reply);
-#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
-}
 
 XdndWorkaround::~XdndWorkaround() {
-    if(!QX11Info::isPlatformX11()) {
+//    if(!QX11Info::isPlatformX11()) {
         return;
-    }
-    qApp->removeNativeEventFilter(this);
+//    }
+//    qApp->removeNativeEventFilter(this);
 }
 
-bool XdndWorkaround::nativeEventFilter(const QByteArray& eventType, void* message, long* /*result*/) {
-    if(Q_LIKELY(eventType == "xcb_generic_event_t")) {
-        xcb_generic_event_t* event = static_cast<xcb_generic_event_t*>(message);
-        switch(event->response_type & ~0x80) {
-        case XCB_CLIENT_MESSAGE:
-            return clientMessage(reinterpret_cast<xcb_client_message_event_t*>(event));
-        case XCB_SELECTION_NOTIFY:
-            return selectionNotify(reinterpret_cast<xcb_selection_notify_event_t*>(event));
-// This part is for Qt >= 5.4 only
-#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
-        case XCB_SELECTION_REQUEST:
-            return selectionRequest(reinterpret_cast<xcb_selection_request_event_t*>(event));
-        case XCB_GE_GENERIC:
-            // newer versions of Qt5 supports xinput2, which sends its mouse events via XGE.
-            return genericEvent(reinterpret_cast<xcb_ge_generic_event_t*>(event));
-        case XCB_BUTTON_RELEASE:
-            // older versions of Qt5 receive mouse events via old XCB events.
-            buttonRelease();
-            break;
-#endif // Qt >= 5.4
-        default:
-            break;
-        }
-    }
-    return false;
-}
 
+/*
 // static
 QByteArray XdndWorkaround::atomName(xcb_atom_t atom) {
     QByteArray name;
@@ -290,3 +244,4 @@ void XdndWorkaround::buttonRelease() {
 }
 
 #endif // QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
+*/
diff --git a/src/xdndworkaround.h b/src/xdndworkaround.h
index 9d40c11..bab0368 100644
--- a/src/xdndworkaround.h
+++ b/src/xdndworkaround.h
@@ -60,7 +60,7 @@ class XdndWorkaround : public QAbstractNativeEventFilter {
 public:
     explicit XdndWorkaround();
     ~XdndWorkaround();
-    bool nativeEventFilter(const QByteArray& eventType, void* message, long* result) override;
+//    bool nativeEventFilter(const QByteArray& eventType, void* message, long* result) override;
     static QByteArray atomName(xcb_atom_t atom);
     static xcb_atom_t internAtom(const char* name, int len = -1);
     static QByteArray windowProperty(xcb_window_t window, xcb_atom_t propAtom, xcb_atom_t typeAtom, int len);
-- 
2.19.1

