From 35fec577c1aa3a52c481d4ae542c15498d5e2a3e Mon Sep 17 00:00:00 2001 From: Torsten Sadowski Date: Fri, 19 Oct 2018 00:19:07 +0200 Subject: [PATCH] Spacemouse platform dependent code is moved to different classes. Compiles and works for Linux --- .../GuiApplicationNativeEventAwareLinux.cpp | 49 -------- src/Gui/3Dconnexion/GuiNativeEventLinux.cpp | 81 ++++++++++++ src/Gui/3Dconnexion/GuiNativeEventLinux.h | 28 +++++ ...ventAwareMac.cpp => GuiNativeEventMac.cpp} | 55 ++++++++ src/Gui/3Dconnexion/GuiNativeEventMac.h | 50 ++++++++ ...AwareWin32.cpp => GuiNativeEventWin32.cpp} | 43 ++++++- src/Gui/3Dconnexion/GuiNativeEventWin32.h | 99 +++++++++++++++ src/Gui/CMakeLists.txt | 6 +- src/Gui/GuiApplicationNativeEventAware.cpp | 111 +---------------- src/Gui/GuiApplicationNativeEventAware.h | 117 ++---------------- 10 files changed, 370 insertions(+), 269 deletions(-) delete mode 100644 src/Gui/3Dconnexion/GuiApplicationNativeEventAwareLinux.cpp create mode 100644 src/Gui/3Dconnexion/GuiNativeEventLinux.cpp create mode 100644 src/Gui/3Dconnexion/GuiNativeEventLinux.h rename src/Gui/3Dconnexion/{GuiApplicationNativeEventAwareMac.cpp => GuiNativeEventMac.cpp} (73%) create mode 100644 src/Gui/3Dconnexion/GuiNativeEventMac.h rename src/Gui/3Dconnexion/{GuiApplicationNativeEventAwareWin32.cpp => GuiNativeEventWin32.cpp} (96%) create mode 100644 src/Gui/3Dconnexion/GuiNativeEventWin32.h diff --git a/src/Gui/3Dconnexion/GuiApplicationNativeEventAwareLinux.cpp b/src/Gui/3Dconnexion/GuiApplicationNativeEventAwareLinux.cpp deleted file mode 100644 index 044a444950..0000000000 --- a/src/Gui/3Dconnexion/GuiApplicationNativeEventAwareLinux.cpp +++ /dev/null @@ -1,49 +0,0 @@ - /* -Implementation by Torsten Sadowski 2018 - */ - -#include "GuiApplicationNativeEventAware.h" -#include "SpaceballEvent.h" -#include - -#if defined(SPNAV_FOUND) - #include -#endif - -void Gui::GUIApplicationNativeEventAware::pollSpacenav() -{ - spnav_event ev; - while(spnav_poll_event(&ev)) - { - QWidget *currentWidget = this->focusWidget(); - if (!currentWidget) - return; - if (!setOSIndependentMotionData()) return; - importSettings(); - switch (ev.type) - { - case SPNAV_EVENT_MOTION: - { - Spaceball::MotionEvent *motionEvent = new Spaceball::MotionEvent(); - motionEvent->setTranslations(ev.motion.x, ev.motion.y, ev.motion.z); - motionEvent->setRotations(ev.motion.rx, ev.motion.ry, ev.motion.rz); - this->postEvent(currentWidget, motionEvent); - break; - } - case SPNAV_EVENT_BUTTON: - { - Spaceball::ButtonEvent *buttonEvent = new Spaceball::ButtonEvent(); - buttonEvent->setButtonNumber(ev.button.bnum); - if (ev.button.press) - { - buttonEvent->setButtonStatus(Spaceball::BUTTON_PRESSED); - } - else - { - buttonEvent->setButtonStatus(Spaceball::BUTTON_RELEASED); - } - break; - } - } - } -} diff --git a/src/Gui/3Dconnexion/GuiNativeEventLinux.cpp b/src/Gui/3Dconnexion/GuiNativeEventLinux.cpp new file mode 100644 index 0000000000..0723bf0e7a --- /dev/null +++ b/src/Gui/3Dconnexion/GuiNativeEventLinux.cpp @@ -0,0 +1,81 @@ + /* +Implementation by Torsten Sadowski 2018 + */ + +#include "GuiNativeEventLinux.h" + +#include "GuiApplicationNativeEventAware.h" +#include "SpaceballEvent.h" +#include +#include +#include +#include + +#include + +Gui::GuiNativeEvent::GuiNativeEvent(Gui::GUIApplicationNativeEventAware *app) +: QObject(app) +{ + mainApp = app; +} + +Gui::GuiNativeEvent::~GuiNativeEvent() +{ + if (spnav_close()) + Base::Console().Log("Couldn't disconnect from spacenav daemon\n"); + else + Base::Console().Log("Disconnected from spacenav daemon\n"); +} + +void Gui::GuiNativeEvent::initSpaceball(QMainWindow *window) +{ + Q_UNUSED(window) + if (spnav_open() == -1) { + Base::Console().Log("Couldn't connect to spacenav daemon\n"); + } else { + Base::Console().Log("Connected to spacenav daemon\n"); + QTimer* SpacenavPollTimer = new QTimer(this); + connect(SpacenavPollTimer, &QTimer::timeout, this, &GuiNativeEvent::pollSpacenav); + SpacenavPollTimer->start(20); + mainApp->setSpaceballPresent(true); + } +} + +void Gui::GuiNativeEvent::pollSpacenav() +{ + spnav_event ev; + while(spnav_poll_event(&ev)) + { + QWidget *currentWidget = mainApp->focusWidget(); + if (!currentWidget) + return; + //if (!setOSIndependentMotionData()) return; + //importSettings(); + switch (ev.type) + { + case SPNAV_EVENT_MOTION: + { + Spaceball::MotionEvent *motionEvent = new Spaceball::MotionEvent(); + motionEvent->setTranslations(ev.motion.x, ev.motion.y, ev.motion.z); + motionEvent->setRotations(ev.motion.rx, ev.motion.ry, ev.motion.rz); + mainApp->postEvent(currentWidget, motionEvent); + break; + } + case SPNAV_EVENT_BUTTON: + { + Spaceball::ButtonEvent *buttonEvent = new Spaceball::ButtonEvent(); + buttonEvent->setButtonNumber(ev.button.bnum); + if (ev.button.press) + { + buttonEvent->setButtonStatus(Spaceball::BUTTON_PRESSED); + } + else + { + buttonEvent->setButtonStatus(Spaceball::BUTTON_RELEASED); + } + mainApp->postEvent(currentWidget, buttonEvent); + break; + } + } + } +} diff --git a/src/Gui/3Dconnexion/GuiNativeEventLinux.h b/src/Gui/3Dconnexion/GuiNativeEventLinux.h new file mode 100644 index 0000000000..5a24673b1e --- /dev/null +++ b/src/Gui/3Dconnexion/GuiNativeEventLinux.h @@ -0,0 +1,28 @@ +#ifndef GUINATIVEEVENT_H +#define GUINATIVEEVENT_H + +#include + +class QMainWindow; +class GUIApplicationNativeEventAware; + +namespace Gui +{ + class GUIApplicationNativeEventAware; + + class GuiNativeEvent : public QObject + { + public: + GuiNativeEvent(GUIApplicationNativeEventAware *app); + ~GuiNativeEvent(); + void initSpaceball(QMainWindow *window); + private: + GuiNativeEvent(); + GuiNativeEvent(GuiNativeEvent*); + void pollSpacenav(); + GUIApplicationNativeEventAware *mainApp; + }; +} + +#endif //GUINATIVEEVENT_H + diff --git a/src/Gui/3Dconnexion/GuiApplicationNativeEventAwareMac.cpp b/src/Gui/3Dconnexion/GuiNativeEventMac.cpp similarity index 73% rename from src/Gui/3Dconnexion/GuiApplicationNativeEventAwareMac.cpp rename to src/Gui/3Dconnexion/GuiNativeEventMac.cpp index 6cfb45a6ce..7f18b7c797 100644 --- a/src/Gui/3Dconnexion/GuiApplicationNativeEventAwareMac.cpp +++ b/src/Gui/3Dconnexion/GuiNativeEventMac.cpp @@ -104,6 +104,61 @@ uint32_t Gui::GUIApplicationNativeEventAware::lastButtons = 0; break; } } + +Gui::GuiNativeEvent::GuiNativeEvent(Gui::GUIApplicationNativeEventAware *app) +: QObject(app) +{ + spaceballPresent = false; + mainApp = app; +} + +Gui::GuiNativeEvent::~GuiNativeEvent() +{ + // if 3Dconnexion library was loaded at runtime + if (InstallConnexionHandlers) { + // Close our connection with the 3dx driver + if (tdxClientID) + UnregisterConnexionClient(tdxClientID); + CleanupConnexionHandlers(); + Base::Console().Log("Disconnected from 3Dconnexion driver\n"); + } +} + +void Gui::GuiNativeEvent::initSpaceball(QMainWindow *window) +{ + Q_UNUSED(window) + OSStatus err; + /* make sure the framework is installed */ + if (InstallConnexionHandlers == NULL) + { + Base::Console().Log("3Dconnexion framework not found!\n"); + return; + } + /* install 3dx message handler in order to receive driver events */ + err = InstallConnexionHandlers(tdx_drv_handler, 0L, 0L); + assert(err == 0); + if (err) + { + Base::Console().Log("Error installing 3Dconnexion handler\n"); + return; + } + /* register our app with the driver */ + // Pascal string Application name required to register driver for application + UInt8 tdxAppName[] = {7,'F','r','e','e','C','A','D'}; + // 32bit appID to register driver for application + UInt32 tdxAppID = 'FCAd'; + tdxClientID = RegisterConnexionClient( tdxAppID, tdxAppName, + kConnexionClientModeTakeOver, + kConnexionMaskAll ); + if (tdxClientID == 0) + { + Base::Console().Log("Couldn't connect to 3Dconnexion driver\n"); + return; + } + + Base::Console().Log("3Dconnexion driver initialized. Client ID: %d\n", tdxClientID); + spaceballPresent = true; +} /*! Called with the processed motion data when a 3D mouse event is received diff --git a/src/Gui/3Dconnexion/GuiNativeEventMac.h b/src/Gui/3Dconnexion/GuiNativeEventMac.h new file mode 100644 index 0000000000..2ecb833300 --- /dev/null +++ b/src/Gui/3Dconnexion/GuiNativeEventMac.h @@ -0,0 +1,50 @@ +#ifndef GUINATIVEEVENT_H +#define GUINATIVEEVENT_H + +#include + +class QMainWindow; +class GUIApplicationNativeEventAware; + +#include +#include +// Note that InstallConnexionHandlers will be replaced with +// SetConnexionHandlers "in the future". +extern OSErr InstallConnexionHandlers(ConnexionMessageHandlerProc messageHandler, + ConnexionAddedHandlerProc addedHandler, + ConnexionRemovedHandlerProc removedHandler) + __attribute__((weak_import)); +extern UInt16 RegisterConnexionClient(UInt32 signature, UInt8 *name, UInt16 mode, + UInt32 mask) __attribute__((weak_import)); +extern void UnregisterConnexionClient(UInt16 clientID) __attribute__((weak_import)); +extern void CleanupConnexionHandlers(void) __attribute__((weak_import)); + +namespace Gui +{ + class GUIApplicationNativeEventAware; + + class GuiNativeEvent : public QObject + { + public: + GuiNativeEvent(GUIApplicationNativeEventAware *app); + ~GuiNativeEvent(); + void initSpaceball(QMainWindow *window); + private: + GuiNativeEvent(); + GuiNativeEvent(GuiNativeEvent*); + bool spaceballPresent; + GUIApplicationNativeEventAware *mainApp; + + static UInt16 tdxClientID; /* ID assigned by the driver */ + static uint32_t lastButtons; + + static void tdx_drv_handler( io_connect_t connection, + natural_t messageType, + void *messageArgument ); + void Move3d(); + void Button3d(bool buttonDown, int buttonNumber); + }; +} + +#endif //GUINATIVEEVENT_H + diff --git a/src/Gui/3Dconnexion/GuiApplicationNativeEventAwareWin32.cpp b/src/Gui/3Dconnexion/GuiNativeEventWin32.cpp similarity index 96% rename from src/Gui/3Dconnexion/GuiApplicationNativeEventAwareWin32.cpp rename to src/Gui/3Dconnexion/GuiNativeEventWin32.cpp index aee218b1f7..a9a1358dd5 100644 --- a/src/Gui/3Dconnexion/GuiApplicationNativeEventAwareWin32.cpp +++ b/src/Gui/3Dconnexion/GuiNativeEventWin32.cpp @@ -16,6 +16,8 @@ http://www.3dconnexion.com/forum/viewtopic.php?f=19&t=4968&sid=72c018bdcf0e6edc9 #include "PreCompiled.h" +#include "GuiNativeEventLinux.h" + #include #include #include @@ -24,8 +26,10 @@ http://www.3dconnexion.com/forum/viewtopic.php?f=19&t=4968&sid=72c018bdcf0e6edc9 #include "GuiApplicationNativeEventAware.h" #include "SpaceballEvent.h" +Gui::GuiNativeEvent* Gui::GuiNativeEvent::gMouseInput = 0; + + // Windows dependencies, enumerators and global variables -#ifdef _USE_3DCONNEXION_SDK #include #include @@ -151,6 +155,42 @@ static const struct tag_VirtualKeys _3dmouseVirtualKeys[]= , const_cast(SpaceMouseWirelessReceiverKeys) }; +Gui::GuiNativeEvent::GuiNativeEvent(Gui::GUIApplicationNativeEventAware *app) +{ + spaceballPresent = false; + mainApp = app; +} + +Gui::GuiNativeEvent::~GuiNativeEvent() +{ + if (gMouseInput == this) { + gMouseInput = 0; + Base::Console().Log("3Dconnexion device detached.\n"); + } +} + +void Gui::GuiNativeEvent::initSpaceball(QMainWindow *window) +{ + spaceballPresent = Is3dmouseAttached(); + + if (spaceballPresent) { + fLast3dmouseInputTime = 0; + + if (InitializeRawInput((HWND)mainWindow->winId())){ + gMouseInput = this; +#if QT_VERSION >= 0x050000 + qApp->installNativeEventFilter(new Gui::RawInputEventFilter(Gui::GUIApplicationNativeEventAware::RawInputEventFilter)); +#else + qApp->setEventFilter(Gui::GUIApplicationNativeEventAware::RawInputEventFilter); +#endif + Base::Console().Log("3Dconnexion device initialized.\n"); + } else { + Base::Console().Log("3Dconnexion device is attached, but not initialized.\n"); + } + } else { + Base::Console().Log("3Dconnexion device not attached.\n"); + } +} // Methods for windows events @@ -934,4 +974,3 @@ Error: return processed; } -#endif // _USE_3DCONNEXION_SDK diff --git a/src/Gui/3Dconnexion/GuiNativeEventWin32.h b/src/Gui/3Dconnexion/GuiNativeEventWin32.h new file mode 100644 index 0000000000..8ca67ea82a --- /dev/null +++ b/src/Gui/3Dconnexion/GuiNativeEventWin32.h @@ -0,0 +1,99 @@ +#ifndef GUINATIVEEVENT_H +#define GUINATIVEEVENT_H + +#include + +#include "3Dconnexion/MouseParameters.h" + +#include +#include + +//#define _WIN32_WINNT 0x0501 //target at least windows XP +#include +#include + + +class QMainWindow; +class GUIApplicationNativeEventAware; + +namespace Gui +{ + class GUIApplicationNativeEventAware; + + class GuiNativeEvent : public QObject + { + public: + GuiNativeEvent(GUIApplicationNativeEventAware *app); + ~GuiNativeEvent(); + void initSpaceball(QMainWindow *window); + private: + GuiNativeEvent(); + GuiNativeEvent(GuiNativeEvent*); + GUIApplicationNativeEventAware *mainApp; + + class RawInputEventFilter : public QAbstractNativeEventFilter + { + public: + typedef bool (*EventFilter)(void *message, long *result); + RawInputEventFilter(EventFilter filter) : eventFilter(filter) { + } + virtual ~RawInputEventFilter() { + } + + virtual bool nativeEventFilter(const QByteArray & /*eventType*/, void *message, long *result) { + return eventFilter(message, result); + } + + private: + EventFilter eventFilter; + }; + + public: + static bool Is3dmouseAttached(); + + I3dMouseParam& MouseParams(); + const I3dMouseParam& MouseParams() const; + + virtual void Move3d(HANDLE device, std::vector& motionData); + virtual void On3dmouseKeyDown(HANDLE device, int virtualKeyCode); + virtual void On3dmouseKeyUp(HANDLE device, int virtualKeyCode); + + private: + bool InitializeRawInput(HWND hwndTarget); + static bool RawInputEventFilter(void* msg, long* result); + void OnRawInput(UINT nInputCode, HRAWINPUT hRawInput); + UINT GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader); + bool TranslateRawInputData(UINT nInputCode, PRAWINPUT pRawInput); + bool ParseRawInput(UINT nInputCode, PRAWINPUT pRawInput); + void On3dmouseInput(); + + class TInputData + { + public: + TInputData() : fAxes(6) {} + + bool IsZero() { + return (0.==fAxes[0] && 0.==fAxes[1] && 0.==fAxes[2] && + 0.==fAxes[3] && 0.==fAxes[4] && 0.==fAxes[5]); + } + + int fTimeToLive; // For telling if the device was unplugged while sending data + bool fIsDirty; + std::vector fAxes; + }; + + HWND fWindow; + + // Data cache to handle multiple rawinput devices + std::map< HANDLE, TInputData> fDevice2Data; + std::map< HANDLE, unsigned long> fDevice2Keystate; + // 3dmouse parameters + MouseParameters f3dMouseParams; // Rotate, Pan Zoom etc. + // use to calculate distance traveled since last event + DWORD fLast3dmouseInputTime; + static Gui::GUIApplicationNativeEventAware* gMouseInput; + }; +} + +#endif //GUINATIVEEVENT_H + diff --git a/src/Gui/CMakeLists.txt b/src/Gui/CMakeLists.txt index 9604f710ed..c65acf3352 100644 --- a/src/Gui/CMakeLists.txt +++ b/src/Gui/CMakeLists.txt @@ -184,21 +184,21 @@ SET(FreeCADGui_SDK_SRCS 3Dconnexion/I3dMouseParams.h 3Dconnexion/MouseParameters.cpp 3Dconnexion/MouseParameters.h - 3Dconnexion/GuiApplicationNativeEventAwareWin32.cpp + 3Dconnexion/GuiNativeEventWin32.cpp ) SOURCE_GROUP("3D connexion SDK" FILES ${FreeCADGui_SDK_SRCS}) endif(FREECAD_USE_3DCONNEXION AND MSVC) if(FREECAD_USE_3DCONNEXION AND APPLE) SET(FreeCADGui_SDK_SRCS - 3Dconnexion/GuiApplicationNativeEventAwareMac.cpp + 3Dconnexion/GuiNativeEventMac.cpp ) SOURCE_GROUP("3D connexion SDK" FILES ${FreeCADGui_SDK_SRCS}) endif(FREECAD_USE_3DCONNEXION AND APPLE) if(UNIX AND NOT APPLE) SET(FreeCADGui_SDK_SRCS - 3Dconnexion/GuiApplicationNativeEventAwareLinux.cpp + 3Dconnexion/GuiNativeEventLinux.cpp ) SOURCE_GROUP("3D connexion SDK" FILES ${FreeCADGui_SDK_SRCS}) endif(UNIX AND NOT APPLE) diff --git a/src/Gui/GuiApplicationNativeEventAware.cpp b/src/Gui/GuiApplicationNativeEventAware.cpp index 78ba28a591..7e830b5397 100644 --- a/src/Gui/GuiApplicationNativeEventAware.cpp +++ b/src/Gui/GuiApplicationNativeEventAware.cpp @@ -30,11 +30,9 @@ #include "GuiApplicationNativeEventAware.h" #include "SpaceballEvent.h" #include "Application.h" -#if defined(Q_OS_LINUX) && defined(SPNAV_FOUND) - #include +#if defined(Q_OS_LINUX) #if QT_VERSION >= 0x050000 - #include #undef Bool #undef CursorShape #undef Expose @@ -51,127 +49,28 @@ #undef Complex #endif // #if QT_VERSION >= 0x050000 -#endif // if defined(Q_OS_LINUX) && defined(SPNAV_FOUND) +#endif // if defined(Q_OS_LINUX) -#ifdef _USE_3DCONNEXION_SDK -//windows -#ifdef Q_OS_WIN -Gui::GUIApplicationNativeEventAware* Gui::GUIApplicationNativeEventAware::gMouseInput = 0; -#endif -#endif //_USE_3DCONNEXION_SDK Gui::GUIApplicationNativeEventAware::GUIApplicationNativeEventAware(int &argc, char *argv[]) : - QApplication (argc, argv), spaceballPresent(false) + QApplication (argc, argv) { - mainWindow = 0; + nativeEvent = new Gui::GuiNativeEvent(this); } Gui::GUIApplicationNativeEventAware::~GUIApplicationNativeEventAware() { -#if defined(Q_OS_LINUX) && defined(SPNAV_FOUND) - if (spnav_close()) - Base::Console().Log("Couldn't disconnect from spacenav daemon\n"); - else - Base::Console().Log("Disconnected from spacenav daemon\n"); - -#elif defined(Q_OS_WIN) && defined(_USE_3DCONNEXION_SDK) - if (gMouseInput == this) { - gMouseInput = 0; - Base::Console().Log("3Dconnexion device detached.\n"); - } - -#elif defined(Q_OS_MACX) && defined(_USE_3DCONNEXION_SDK) - // if 3Dconnexion library was loaded at runtime - if (InstallConnexionHandlers) { - // Close our connection with the 3dx driver - if (tdxClientID) - UnregisterConnexionClient(tdxClientID); - CleanupConnexionHandlers(); - Base::Console().Log("Disconnected from 3Dconnexion driver\n"); - } -#endif // Platform switch } void Gui::GUIApplicationNativeEventAware::initSpaceball(QMainWindow *window) { - mainWindow = window; - -#if defined(Q_OS_LINUX) && defined(SPNAV_FOUND) - if (spnav_open() == -1) { - Base::Console().Log("Couldn't connect to spacenav daemon\n"); - } else { - Base::Console().Log("Connected to spacenav daemon\n"); - spaceballPresent = true; - - #if QT_VERSION >= 0x050000 - QTimer* SpacenavPollTimer = new QTimer(this); - connect(SpacenavPollTimer, &QTimer::timeout, this, &GUIApplicationNativeEventAware::pollSpacenav); - SpacenavPollTimer->start(20); - #endif // #if QT_VERSION >= 0x050000 - } - -#elif defined(Q_OS_WIN) && defined(_USE_3DCONNEXION_SDK) - spaceballPresent = Is3dmouseAttached(); - - if (spaceballPresent) { - fLast3dmouseInputTime = 0; - - if (InitializeRawInput((HWND)mainWindow->winId())){ - gMouseInput = this; -#if QT_VERSION >= 0x050000 - qApp->installNativeEventFilter(new Gui::RawInputEventFilter(Gui::GUIApplicationNativeEventAware::RawInputEventFilter)); -#else - qApp->setEventFilter(Gui::GUIApplicationNativeEventAware::RawInputEventFilter); -#endif - Base::Console().Log("3Dconnexion device initialized.\n"); - } else { - Base::Console().Log("3Dconnexion device is attached, but not initialized.\n"); - } - } else { - Base::Console().Log("3Dconnexion device not attached.\n"); - } - -#elif defined(Q_OS_MACX) && defined(_USE_3DCONNEXION_SDK) - OSStatus err; - /* make sure the framework is installed */ - if (InstallConnexionHandlers == NULL) - { - Base::Console().Log("3Dconnexion framework not found!\n"); - return; - } - /* install 3dx message handler in order to receive driver events */ - err = InstallConnexionHandlers(tdx_drv_handler, 0L, 0L); - assert(err == 0); - if (err) - { - Base::Console().Log("Error installing 3Dconnexion handler\n"); - return; - } - /* register our app with the driver */ - // Pascal string Application name required to register driver for application - UInt8 tdxAppName[] = {7,'F','r','e','e','C','A','D'}; - // 32bit appID to register driver for application - UInt32 tdxAppID = 'FCAd'; - tdxClientID = RegisterConnexionClient( tdxAppID, tdxAppName, - kConnexionClientModeTakeOver, - kConnexionMaskAll ); - if (tdxClientID == 0) - { - Base::Console().Log("Couldn't connect to 3Dconnexion driver\n"); - return; - } - - Base::Console().Log("3Dconnexion driver initialized. Client ID: %d\n", tdxClientID); - spaceballPresent = true; -#endif // Platform switch - + nativeEvent->initSpaceball(window); Spaceball::MotionEvent::MotionEventType = QEvent::registerEventType(); Spaceball::ButtonEvent::ButtonEventType = QEvent::registerEventType(); } bool Gui::GUIApplicationNativeEventAware::processSpaceballEvent(QObject *object, QEvent *event) { - std::cout << "Gui::GUIApplicationNativeEventAware::processSpaceballEvent" << std::endl; if (!activeWindow()) { qDebug("No active window\n"); return true; diff --git a/src/Gui/GuiApplicationNativeEventAware.h b/src/Gui/GuiApplicationNativeEventAware.h index 3f13e04632..4dab2bf11c 100644 --- a/src/Gui/GuiApplicationNativeEventAware.h +++ b/src/Gui/GuiApplicationNativeEventAware.h @@ -29,30 +29,12 @@ class QMainWindow; -#if defined(Q_OS_WIN) && defined(_USE_3DCONNEXION_SDK) -#include "3Dconnexion/MouseParameters.h" - -#include -#include - -//#define _WIN32_WINNT 0x0501 //target at least windows XP -#include -#include - -#elif defined(Q_OS_MACX) && defined(_USE_3DCONNEXION_SDK) - -#include -#include -// Note that InstallConnexionHandlers will be replaced with -// SetConnexionHandlers "in the future". -extern OSErr InstallConnexionHandlers(ConnexionMessageHandlerProc messageHandler, - ConnexionAddedHandlerProc addedHandler, - ConnexionRemovedHandlerProc removedHandler) - __attribute__((weak_import)); -extern UInt16 RegisterConnexionClient(UInt32 signature, UInt8 *name, UInt16 mode, - UInt32 mask) __attribute__((weak_import)); -extern void UnregisterConnexionClient(UInt16 clientID) __attribute__((weak_import)); -extern void CleanupConnexionHandlers(void) __attribute__((weak_import)); +#if defined(Q_OS_LINUX) +#include "3Dconnexion/GuiNativeEventLinux.h" +#elif defined(Q_OS_WIN) +#include "3Dconnexion/GuiNativeEventWin32.h" +#elif defined(Q_OS_MACX) +#include "3Dconnexion/GuiNativeEventMac.h" #endif // Platform switch namespace Gui @@ -65,98 +47,15 @@ namespace Gui ~GUIApplicationNativeEventAware(); void initSpaceball(QMainWindow *window); bool isSpaceballPresent() const {return spaceballPresent;} + void setSpaceballPresent(bool present) {spaceballPresent = present;} bool processSpaceballEvent(QObject *object, QEvent *event); private: bool spaceballPresent; - QMainWindow *mainWindow; int motionDataArray[6]; bool setOSIndependentMotionData(); void importSettings(); float convertPrefToSensitivity(int value); - -#ifdef Q_OS_LINUX - public: - void pollSpacenav(); -#endif // Q_OS_LINUX - -#ifdef _USE_3DCONNEXION_SDK -// For Windows -#ifdef Q_OS_WIN - class RawInputEventFilter : public QAbstractNativeEventFilter - { - public: - typedef bool (*EventFilter)(void *message, long *result); - RawInputEventFilter(EventFilter filter) : eventFilter(filter) { - } - virtual ~RawInputEventFilter() { - } - - virtual bool nativeEventFilter(const QByteArray & /*eventType*/, void *message, long *result) { - return eventFilter(message, result); - } - - private: - EventFilter eventFilter; - }; - - public: - static bool Is3dmouseAttached(); - - I3dMouseParam& MouseParams(); - const I3dMouseParam& MouseParams() const; - - virtual void Move3d(HANDLE device, std::vector& motionData); - virtual void On3dmouseKeyDown(HANDLE device, int virtualKeyCode); - virtual void On3dmouseKeyUp(HANDLE device, int virtualKeyCode); - - private: - bool InitializeRawInput(HWND hwndTarget); - static bool RawInputEventFilter(void* msg, long* result); - void OnRawInput(UINT nInputCode, HRAWINPUT hRawInput); - UINT GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader); - bool TranslateRawInputData(UINT nInputCode, PRAWINPUT pRawInput); - bool ParseRawInput(UINT nInputCode, PRAWINPUT pRawInput); - void On3dmouseInput(); - - class TInputData - { - public: - TInputData() : fAxes(6) {} - - bool IsZero() { - return (0.==fAxes[0] && 0.==fAxes[1] && 0.==fAxes[2] && - 0.==fAxes[3] && 0.==fAxes[4] && 0.==fAxes[5]); - } - - int fTimeToLive; // For telling if the device was unplugged while sending data - bool fIsDirty; - std::vector fAxes; - }; - - HWND fWindow; - - // Data cache to handle multiple rawinput devices - std::map< HANDLE, TInputData> fDevice2Data; - std::map< HANDLE, unsigned long> fDevice2Keystate; - // 3dmouse parameters - MouseParameters f3dMouseParams; // Rotate, Pan Zoom etc. - // use to calculate distance traveled since last event - DWORD fLast3dmouseInputTime; - static Gui::GUIApplicationNativeEventAware* gMouseInput; -#endif // Q_OS_WIN -#ifdef Q_OS_MACX - private: - static UInt16 tdxClientID; /* ID assigned by the driver */ - static uint32_t lastButtons; - public: - static void tdx_drv_handler( io_connect_t connection, - natural_t messageType, - void *messageArgument ); - void Move3d(); - void Button3d(bool buttonDown, int buttonNumber); - -#endif// Q_OS_MACX -#endif // _USE_3DCONNEXION_SDK + GuiNativeEvent *nativeEvent; }; // end class GUIApplicationNativeEventAware } // end namespace Gui