From d003878ffecf0ff183769b303ee6f3c02ba86850 Mon Sep 17 00:00:00 2001 From: "Jan Paul (RH1, Windows)" Date: Thu, 27 Aug 2015 18:10:54 +0200 Subject: [PATCH 1/5] works with Qt5, MyXcbEventFilter still needs to be nicely deleted on shutdown --- .../connexion_plugin/win32/ConnexionHID.cpp | 473 ++++++++++-------- 1 file changed, 259 insertions(+), 214 deletions(-) diff --git a/plugins/connexion_plugin/win32/ConnexionHID.cpp b/plugins/connexion_plugin/win32/ConnexionHID.cpp index ccf497655..a56eefdfb 100644 --- a/plugins/connexion_plugin/win32/ConnexionHID.cpp +++ b/plugins/connexion_plugin/win32/ConnexionHID.cpp @@ -1,214 +1,259 @@ -/* - * Copyright 2011, 2012, DFKI GmbH Robotics Innovation Center - * - * This file is part of the MARS simulation framework. - * - * MARS is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation, either version 3 - * of the License, or (at your option) any later version. - * - * MARS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with MARS. If not, see . - * - */ - -#include "../ConnexionHID.h" - -#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501) - #undef _WIN32_WINNT - #define _WIN32_WINNT 0x0601 -#endif - -#include -#include -#include -#include - -namespace mars { - namespace plugins { - namespace connexion_plugin { - - static int idleFrameCount[3] = {0,0,0}; - static int idleThreshold = 4; - - bool myEventFilter(void *message, long *result); - - // let's have some global variables. great! - static mars::utils::Mutex valueMutex; - static connexionValues tmpValues; - - - int registerRawDevices(HWND hwndMessagesWindow) { - RAWINPUTDEVICE sRawInputDevices[1]; - sRawInputDevices[0].usUsagePage = 0x01; - sRawInputDevices[0].usUsage = 0x08; - sRawInputDevices[0].dwFlags = 0x00; - sRawInputDevices[0].hwndTarget = 0x00; - //RIM_TYPEHID; - - unsigned int uiNumDevices = sizeof(sRawInputDevices)/sizeof(sRawInputDevices[0]); - unsigned int cbSize = sizeof(sRawInputDevices[0]); - - unsigned int i; - for (i = 0; i < uiNumDevices; ++i) { - sRawInputDevices[i].hwndTarget = hwndMessagesWindow; - } - return RegisterRawInputDevices(sRawInputDevices, uiNumDevices, cbSize); - } - - - int initConnexionHID(void* windowID) { - tmpValues.tx = 0.0; - tmpValues.ty = 0.0; - tmpValues.tz = 0.0; - tmpValues.rx = 0.0; - tmpValues.ry = 0.0; - tmpValues.rz = 0.0; - tmpValues.button1 = 0; - tmpValues.button2 = 0; - - qApp->setEventFilter(myEventFilter); - - HWND handleForMessages = (HWND)windowID; - return registerRawDevices(handleForMessages); - } - - void getValue(mars::interfaces::sReal *coordinates, - struct connexionValues *rawValues) { - valueMutex.lock(); - for(int i = 0; i < 3; ++i) { - ++idleFrameCount[i]; - } - *rawValues = tmpValues; - - coordinates[0] = tmpValues.tx * fabs(tmpValues.tx * 0.001); - coordinates[1] = -tmpValues.tz * fabs(tmpValues.tz * 0.001); - coordinates[2] = -tmpValues.ty * fabs(tmpValues.ty * 0.001); - coordinates[3] = tmpValues.rx * fabs(tmpValues.rx * 0.0008); - coordinates[4] = -tmpValues.rz * fabs(tmpValues.rz * 0.0008); - coordinates[5] = -tmpValues.ry * fabs(tmpValues.ry * 0.0008); - - if(idleFrameCount[0] > idleThreshold) { - tmpValues.tx = 0.0; - tmpValues.ty = 0.0; - tmpValues.tz = 0.0; - } - if(idleFrameCount[1] > idleThreshold) { - tmpValues.rx = 0.0; - tmpValues.ry = 0.0; - tmpValues.rz = 0.0; - } - if(idleFrameCount[2] > idleThreshold) { - tmpValues.button1 = 0; - tmpValues.button2 = 0; - } - valueMutex.unlock(); - } - - void closeConnexionHID() { - } - - - bool myEventFilter(void *message, long *result) { - - MSG *msgStruct = (MSG*) message; - - if (msgStruct->message == WM_INPUT) { - - - //cout << "Message: WM_INPUT erhalten" << endl; - - unsigned int dwSize = 0; - - GetRawInputData((HRAWINPUT)msgStruct->lParam, RID_INPUT, NULL, - &dwSize, sizeof(RAWINPUTHEADER)); - - LPBYTE lpb = new BYTE[dwSize]; - - if (GetRawInputData((HRAWINPUT)msgStruct->lParam, RID_INPUT, - lpb, &dwSize, sizeof(RAWINPUTHEADER)) - != dwSize) { - delete[] lpb; - return false; - } - - RAWINPUT* raw = (RAWINPUT*) lpb; - - if (raw->header.dwType != RIM_TYPEHID) { - delete[] lpb; - return false; - } - - RID_DEVICE_INFO sRidDeviceInfo; - sRidDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO); - dwSize = sizeof(RID_DEVICE_INFO); - - if (GetRawInputDeviceInfo(raw->header.hDevice, - RIDI_DEVICEINFO, - &sRidDeviceInfo, - &dwSize) == dwSize) { - if (sRidDeviceInfo.hid.dwVendorId == LOGITECH_VENDOR_ID) { - if (raw->data.hid.bRawData == 0x01) { - // Translation vector - - short *pnData = reinterpret_cast (&raw->data.hid.bRawData + 1); - //cout << "packet1 X: " << pnData[0] << " Y: " << pnData[1] << " Z: " << pnData[2] << endl; - // while(is_waiting) Sleep(1); - - valueMutex.lock(); - tmpValues.tx = pnData[0]; - tmpValues.ty = pnData[1]; - tmpValues.tz = pnData[2]; - idleFrameCount[0] = 0; - valueMutex.unlock(); - } else if (raw->data.hid.bRawData == 0x02) { - // Direct rotation vector (NOT Euler) - short *pnData = reinterpret_cast (&raw->data.hid.bRawData + 1); - //cout << "packet2 rX: " << pnData[0] << " rY: " << pnData[1] << " rZ: " << pnData[2] << endl; - valueMutex.lock(); - tmpValues.rx = pnData[0]; - tmpValues.ry = pnData[1]; - tmpValues.rz = pnData[2]; - idleFrameCount[1] = 0; - valueMutex.unlock(); - } else if (raw->data.hid.bRawData == 0x03) { - // State of the keys - unsigned long dwKeyState = *reinterpret_cast (&raw->data.hid.bRawData + 1); - if (dwKeyState & 1) { - valueMutex.lock(); - tmpValues.button1 = 1; - idleFrameCount[2] = 0; - valueMutex.unlock(); - } - if (dwKeyState & 2) { - valueMutex.lock(); - tmpValues.button2 = 1; - idleFrameCount[2] = 0; - valueMutex.unlock(); - } - //cout << "key pressed: " << dwKeyState << endl; - } - } - } - - // Do something - *result = 1; - delete[] lpb; - return true; - } - else { - // We do not want to handle this message so pass back to Windows - // to handle it in a default way - return false; - } - } - - } // end of namespace connexion_plugin - } // end of namespace plugins -} // end of namespace mars +/* + * Copyright 2011, 2012, DFKI GmbH Robotics Innovation Center + * + * This file is part of the MARS simulation framework. + * + * MARS is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * MARS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with MARS. If not, see . + * + */ + +#include "../ConnexionHID.h" + +#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501) + #undef _WIN32_WINNT + #define _WIN32_WINNT 0x0601 +#endif + +#include +#include +#include +#include +#include +#include + +namespace mars { + namespace plugins { + namespace connexion_plugin { + + static int idleFrameCount[3] = {0,0,0}; + static int idleThreshold = 4; + + bool myEventFilter(void *message, long *result); + + class MyXcbEventFilter : public QAbstractNativeEventFilter + { + public: + virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE + { + // std::cout << "nativeEventFilter 1\n"; + // std::cout << "event: <" << eventType.constData() << ">\n"; + if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { + // std::cout << "nativeEventFilter 2\n"; + return mars::plugins::connexion_plugin::myEventFilter(message, result); + } + // std::cout << "nativeEventFilter 3\n"; + return false; + } + }; + + // let's have some global variables. great! + static mars::utils::Mutex valueMutex; + static connexionValues tmpValues; + + + int registerRawDevices(HWND hwndMessagesWindow) { + RAWINPUTDEVICE sRawInputDevices[1]; + sRawInputDevices[0].usUsagePage = 0x01; + sRawInputDevices[0].usUsage = 0x08; + sRawInputDevices[0].dwFlags = 0x00; + sRawInputDevices[0].hwndTarget = 0x00; + //RIM_TYPEHID; + + unsigned int uiNumDevices = sizeof(sRawInputDevices)/sizeof(sRawInputDevices[0]); + unsigned int cbSize = sizeof(sRawInputDevices[0]); + + unsigned int i; + for (i = 0; i < uiNumDevices; ++i) { + sRawInputDevices[i].hwndTarget = hwndMessagesWindow; + } + return RegisterRawInputDevices(sRawInputDevices, uiNumDevices, cbSize); + } + + + int initConnexionHID(void* windowID) { + tmpValues.tx = 0.0; + tmpValues.ty = 0.0; + tmpValues.tz = 0.0; + tmpValues.rx = 0.0; + tmpValues.ry = 0.0; + tmpValues.rz = 0.0; + tmpValues.button1 = 0; + tmpValues.button2 = 0; + + qApp->installNativeEventFilter(new MyXcbEventFilter); + + HWND handleForMessages = (HWND)windowID; + return registerRawDevices(handleForMessages); + } + + void getValue(mars::interfaces::sReal *coordinates, + struct connexionValues *rawValues) { + valueMutex.lock(); + for(int i = 0; i < 3; ++i) { + ++idleFrameCount[i]; + } + *rawValues = tmpValues; + + coordinates[0] = tmpValues.tx * fabs(tmpValues.tx * 0.001); + coordinates[1] = -tmpValues.tz * fabs(tmpValues.tz * 0.001); + coordinates[2] = -tmpValues.ty * fabs(tmpValues.ty * 0.001); + coordinates[3] = tmpValues.rx * fabs(tmpValues.rx * 0.0008); + coordinates[4] = -tmpValues.rz * fabs(tmpValues.rz * 0.0008); + coordinates[5] = -tmpValues.ry * fabs(tmpValues.ry * 0.0008); + + if(idleFrameCount[0] > idleThreshold) { + tmpValues.tx = 0.0; + tmpValues.ty = 0.0; + tmpValues.tz = 0.0; + } + if(idleFrameCount[1] > idleThreshold) { + tmpValues.rx = 0.0; + tmpValues.ry = 0.0; + tmpValues.rz = 0.0; + } + if(idleFrameCount[2] > idleThreshold) { + tmpValues.button1 = 0; + tmpValues.button2 = 0; + } + valueMutex.unlock(); + } + + void closeConnexionHID() { + } + + + bool myEventFilter(void *message, long *result) { + + // std::cout << "nativeEventFilter 2:A\n"; + + MSG *msgStruct = (MSG*) message; + // std::cout << "nativeEventFilter 2:B\n"; + + if (msgStruct->message == WM_INPUT) { + // std::cout << "nativeEventFilter 2:C\n"; + + // std::cout << "Message: WM_INPUT erhalten" << "\n"; + + unsigned int dwSize = 0; + // std::cout << "nativeEventFilter 2:D\n"; + + GetRawInputData((HRAWINPUT)msgStruct->lParam, RID_INPUT, NULL, + &dwSize, sizeof(RAWINPUTHEADER)); + // std::cout << "nativeEventFilter 2:E\n"; + + LPBYTE lpb = new BYTE[dwSize]; + // std::cout << "nativeEventFilter 2:F\n"; + + if (GetRawInputData((HRAWINPUT)msgStruct->lParam, RID_INPUT, + lpb, &dwSize, sizeof(RAWINPUTHEADER)) + != dwSize) { + // std::cout << "nativeEventFilter 2:G\n"; + delete[] lpb; + return false; + } + + // std::cout << "nativeEventFilter 2:H\n"; + + RAWINPUT* raw = (RAWINPUT*) lpb; + + // std::cout << "nativeEventFilter 2:I\n"; + + if (raw->header.dwType != RIM_TYPEHID) { + delete[] lpb; + return false; + } + + // std::cout << "nativeEventFilter 2:J\n"; + + RID_DEVICE_INFO sRidDeviceInfo; + // std::cout << "nativeEventFilter 2:J:2\n"; + sRidDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO); + // std::cout << "nativeEventFilter 2:J:3\n"; + dwSize = sizeof(RID_DEVICE_INFO); + // std::cout << "nativeEventFilter 2:J:4\n"; + + if (GetRawInputDeviceInfo(raw->header.hDevice, + RIDI_DEVICEINFO, + &sRidDeviceInfo, + &dwSize) == dwSize) { + // std::cout << "nativeEventFilter 2:J:5\n"; + if (sRidDeviceInfo.hid.dwVendorId == LOGITECH_VENDOR_ID) { + // std::cout << "nativeEventFilter 2:J:6\n"; + // std::cout << "raw->data.hid.bRawData=" << (raw->data.hid.bRawData) << "\n"; + if (*(raw->data.hid.bRawData) == 0x01) { + // Translation vector + // std::cout << "nativeEventFilter 2:J:7\n"; + + short *pnData = reinterpret_cast (&raw->data.hid.bRawData + 1); + // std::cout << "packet1 X: " << pnData[0] << " Y: " << pnData[1] << " Z: " << pnData[2] << "\n"; + // while(is_waiting) Sleep(1); + + valueMutex.lock(); + tmpValues.tx = pnData[0]; + tmpValues.ty = pnData[1]; + tmpValues.tz = pnData[2]; + idleFrameCount[0] = 0; + valueMutex.unlock(); + } else if (*(raw->data.hid.bRawData) == 0x02) { + // Direct rotation vector (NOT Euler) + short *pnData = reinterpret_cast (&raw->data.hid.bRawData + 1); + // std::cout << "packet2 rX: " << pnData[0] << " rY: " << pnData[1] << " rZ: " << pnData[2] << "\n"; + valueMutex.lock(); + tmpValues.rx = pnData[0]; + tmpValues.ry = pnData[1]; + tmpValues.rz = pnData[2]; + idleFrameCount[1] = 0; + valueMutex.unlock(); + } else if (*(raw->data.hid.bRawData) == 0x03) { + // State of the keys + unsigned long dwKeyState = *reinterpret_cast (&raw->data.hid.bRawData + 1); + if (dwKeyState & 1) { + valueMutex.lock(); + tmpValues.button1 = 1; + idleFrameCount[2] = 0; + valueMutex.unlock(); + } + if (dwKeyState & 2) { + valueMutex.lock(); + tmpValues.button2 = 1; + idleFrameCount[2] = 0; + valueMutex.unlock(); + } + // std::cout << "key pressed: " << dwKeyState << "\n"; + } + } + } + + // std::cout << "nativeEventFilter 2:K\n"; + + // Do something + //*result = 1l; //Jan Paul: I do not know why this causes a crash + // std::cout << "nativeEventFilter 2:K:2\n"; + delete[] lpb; + + // std::cout << "nativeEventFilter 2:L\n"; + + return true; + } + else { + // We do not want to handle this message so pass back to Windows + // to handle it in a default way + // std::cout << "nativeEventFilter 2:M\n"; + return false; + } + } + + } // end of namespace connexion_plugin + } // end of namespace plugins +} // end of namespace mars From 08c202ce20989c6f8ce4d5e3ea97a1250d7e4939 Mon Sep 17 00:00:00 2001 From: "Jan Paul (RH1, Windows)" Date: Fri, 28 Aug 2015 11:09:12 +0200 Subject: [PATCH 2/5] #ifs for Qt4 and Qt5 --- .../connexion_plugin/win32/ConnexionHID.cpp | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/plugins/connexion_plugin/win32/ConnexionHID.cpp b/plugins/connexion_plugin/win32/ConnexionHID.cpp index a56eefdfb..9824fb2b6 100644 --- a/plugins/connexion_plugin/win32/ConnexionHID.cpp +++ b/plugins/connexion_plugin/win32/ConnexionHID.cpp @@ -27,7 +27,9 @@ #include #include +#ifdef USE_QT5 #include +#endif #include #include #include @@ -41,27 +43,30 @@ namespace mars { bool myEventFilter(void *message, long *result); - class MyXcbEventFilter : public QAbstractNativeEventFilter +#ifdef USE_QT5 + class SpaceMouseEventFilter : public QAbstractNativeEventFilter { public: - virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE - { - // std::cout << "nativeEventFilter 1\n"; - // std::cout << "event: <" << eventType.constData() << ">\n"; - if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { - // std::cout << "nativeEventFilter 2\n"; - return mars::plugins::connexion_plugin::myEventFilter(message, result); - } - // std::cout << "nativeEventFilter 3\n"; - return false; - } + virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE + { + // std::cout << "nativeEventFilter 1\n"; + // std::cout << "event: <" << eventType.constData() << ">\n"; + if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { + // std::cout << "nativeEventFilter 2\n"; + return mars::plugins::connexion_plugin::myEventFilter(message, result); + } + // std::cout << "nativeEventFilter 3\n"; + return false; + } }; + SpaceMouseEventFilter* smef = NULL; +#endif + // let's have some global variables. great! static mars::utils::Mutex valueMutex; static connexionValues tmpValues; - int registerRawDevices(HWND hwndMessagesWindow) { RAWINPUTDEVICE sRawInputDevices[1]; sRawInputDevices[0].usUsagePage = 0x01; @@ -91,7 +96,12 @@ namespace mars { tmpValues.button1 = 0; tmpValues.button2 = 0; - qApp->installNativeEventFilter(new MyXcbEventFilter); +#ifdef USE_QT5 + smef=new SpaceMouseEventFilter(); + qApp->installNativeEventFilter(new SpaceMouseEventFilter); +#else + qApp->setEventFilter(myEventFilter); +#endif HWND handleForMessages = (HWND)windowID; return registerRawDevices(handleForMessages); @@ -130,6 +140,10 @@ namespace mars { } void closeConnexionHID() { +#ifdef USE_QT5 + delete smef; + smef=NULL; +#endif } @@ -191,7 +205,11 @@ namespace mars { if (sRidDeviceInfo.hid.dwVendorId == LOGITECH_VENDOR_ID) { // std::cout << "nativeEventFilter 2:J:6\n"; // std::cout << "raw->data.hid.bRawData=" << (raw->data.hid.bRawData) << "\n"; +#ifdef USE_QT5 if (*(raw->data.hid.bRawData) == 0x01) { +#else + if (raw->data.hid.bRawData == 0x01) { +#endif // Translation vector // std::cout << "nativeEventFilter 2:J:7\n"; @@ -205,8 +223,12 @@ namespace mars { tmpValues.tz = pnData[2]; idleFrameCount[0] = 0; valueMutex.unlock(); +#ifdef USE_QT5 } else if (*(raw->data.hid.bRawData) == 0x02) { - // Direct rotation vector (NOT Euler) +#else + } else if (raw->data.hid.bRawData == 0x02) { +#endif + // Direct rotation vector (NOT Euler) short *pnData = reinterpret_cast (&raw->data.hid.bRawData + 1); // std::cout << "packet2 rX: " << pnData[0] << " rY: " << pnData[1] << " rZ: " << pnData[2] << "\n"; valueMutex.lock(); @@ -215,7 +237,11 @@ namespace mars { tmpValues.rz = pnData[2]; idleFrameCount[1] = 0; valueMutex.unlock(); +#ifdef USE_QT5 } else if (*(raw->data.hid.bRawData) == 0x03) { +#else + } else if (raw->data.hid.bRawData == 0x03) { +#endif // State of the keys unsigned long dwKeyState = *reinterpret_cast (&raw->data.hid.bRawData + 1); if (dwKeyState & 1) { From f0ad825bdeed819a8f4f1a0f835cf68f5ba2df48 Mon Sep 17 00:00:00 2001 From: "Jan Paul (RH1, Windows)" Date: Fri, 28 Aug 2015 11:23:08 +0200 Subject: [PATCH 3/5] converted back to Unix file format --- .../connexion_plugin/win32/ConnexionHID.cpp | 570 +++++++++--------- 1 file changed, 285 insertions(+), 285 deletions(-) diff --git a/plugins/connexion_plugin/win32/ConnexionHID.cpp b/plugins/connexion_plugin/win32/ConnexionHID.cpp index 9824fb2b6..b9d8847c2 100644 --- a/plugins/connexion_plugin/win32/ConnexionHID.cpp +++ b/plugins/connexion_plugin/win32/ConnexionHID.cpp @@ -1,285 +1,285 @@ -/* - * Copyright 2011, 2012, DFKI GmbH Robotics Innovation Center - * - * This file is part of the MARS simulation framework. - * - * MARS is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation, either version 3 - * of the License, or (at your option) any later version. - * - * MARS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with MARS. If not, see . - * - */ - -#include "../ConnexionHID.h" - -#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501) - #undef _WIN32_WINNT - #define _WIN32_WINNT 0x0601 -#endif - -#include -#include -#ifdef USE_QT5 -#include -#endif -#include -#include -#include - -namespace mars { - namespace plugins { - namespace connexion_plugin { - - static int idleFrameCount[3] = {0,0,0}; - static int idleThreshold = 4; - - bool myEventFilter(void *message, long *result); - -#ifdef USE_QT5 - class SpaceMouseEventFilter : public QAbstractNativeEventFilter - { - public: - virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE - { - // std::cout << "nativeEventFilter 1\n"; - // std::cout << "event: <" << eventType.constData() << ">\n"; - if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { - // std::cout << "nativeEventFilter 2\n"; - return mars::plugins::connexion_plugin::myEventFilter(message, result); - } - // std::cout << "nativeEventFilter 3\n"; - return false; - } - }; - - SpaceMouseEventFilter* smef = NULL; -#endif - - // let's have some global variables. great! - static mars::utils::Mutex valueMutex; - static connexionValues tmpValues; - - int registerRawDevices(HWND hwndMessagesWindow) { - RAWINPUTDEVICE sRawInputDevices[1]; - sRawInputDevices[0].usUsagePage = 0x01; - sRawInputDevices[0].usUsage = 0x08; - sRawInputDevices[0].dwFlags = 0x00; - sRawInputDevices[0].hwndTarget = 0x00; - //RIM_TYPEHID; - - unsigned int uiNumDevices = sizeof(sRawInputDevices)/sizeof(sRawInputDevices[0]); - unsigned int cbSize = sizeof(sRawInputDevices[0]); - - unsigned int i; - for (i = 0; i < uiNumDevices; ++i) { - sRawInputDevices[i].hwndTarget = hwndMessagesWindow; - } - return RegisterRawInputDevices(sRawInputDevices, uiNumDevices, cbSize); - } - - - int initConnexionHID(void* windowID) { - tmpValues.tx = 0.0; - tmpValues.ty = 0.0; - tmpValues.tz = 0.0; - tmpValues.rx = 0.0; - tmpValues.ry = 0.0; - tmpValues.rz = 0.0; - tmpValues.button1 = 0; - tmpValues.button2 = 0; - -#ifdef USE_QT5 - smef=new SpaceMouseEventFilter(); - qApp->installNativeEventFilter(new SpaceMouseEventFilter); -#else - qApp->setEventFilter(myEventFilter); -#endif - - HWND handleForMessages = (HWND)windowID; - return registerRawDevices(handleForMessages); - } - - void getValue(mars::interfaces::sReal *coordinates, - struct connexionValues *rawValues) { - valueMutex.lock(); - for(int i = 0; i < 3; ++i) { - ++idleFrameCount[i]; - } - *rawValues = tmpValues; - - coordinates[0] = tmpValues.tx * fabs(tmpValues.tx * 0.001); - coordinates[1] = -tmpValues.tz * fabs(tmpValues.tz * 0.001); - coordinates[2] = -tmpValues.ty * fabs(tmpValues.ty * 0.001); - coordinates[3] = tmpValues.rx * fabs(tmpValues.rx * 0.0008); - coordinates[4] = -tmpValues.rz * fabs(tmpValues.rz * 0.0008); - coordinates[5] = -tmpValues.ry * fabs(tmpValues.ry * 0.0008); - - if(idleFrameCount[0] > idleThreshold) { - tmpValues.tx = 0.0; - tmpValues.ty = 0.0; - tmpValues.tz = 0.0; - } - if(idleFrameCount[1] > idleThreshold) { - tmpValues.rx = 0.0; - tmpValues.ry = 0.0; - tmpValues.rz = 0.0; - } - if(idleFrameCount[2] > idleThreshold) { - tmpValues.button1 = 0; - tmpValues.button2 = 0; - } - valueMutex.unlock(); - } - - void closeConnexionHID() { -#ifdef USE_QT5 - delete smef; - smef=NULL; -#endif - } - - - bool myEventFilter(void *message, long *result) { - - // std::cout << "nativeEventFilter 2:A\n"; - - MSG *msgStruct = (MSG*) message; - // std::cout << "nativeEventFilter 2:B\n"; - - if (msgStruct->message == WM_INPUT) { - // std::cout << "nativeEventFilter 2:C\n"; - - // std::cout << "Message: WM_INPUT erhalten" << "\n"; - - unsigned int dwSize = 0; - // std::cout << "nativeEventFilter 2:D\n"; - - GetRawInputData((HRAWINPUT)msgStruct->lParam, RID_INPUT, NULL, - &dwSize, sizeof(RAWINPUTHEADER)); - // std::cout << "nativeEventFilter 2:E\n"; - - LPBYTE lpb = new BYTE[dwSize]; - // std::cout << "nativeEventFilter 2:F\n"; - - if (GetRawInputData((HRAWINPUT)msgStruct->lParam, RID_INPUT, - lpb, &dwSize, sizeof(RAWINPUTHEADER)) - != dwSize) { - // std::cout << "nativeEventFilter 2:G\n"; - delete[] lpb; - return false; - } - - // std::cout << "nativeEventFilter 2:H\n"; - - RAWINPUT* raw = (RAWINPUT*) lpb; - - // std::cout << "nativeEventFilter 2:I\n"; - - if (raw->header.dwType != RIM_TYPEHID) { - delete[] lpb; - return false; - } - - // std::cout << "nativeEventFilter 2:J\n"; - - RID_DEVICE_INFO sRidDeviceInfo; - // std::cout << "nativeEventFilter 2:J:2\n"; - sRidDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO); - // std::cout << "nativeEventFilter 2:J:3\n"; - dwSize = sizeof(RID_DEVICE_INFO); - // std::cout << "nativeEventFilter 2:J:4\n"; - - if (GetRawInputDeviceInfo(raw->header.hDevice, - RIDI_DEVICEINFO, - &sRidDeviceInfo, - &dwSize) == dwSize) { - // std::cout << "nativeEventFilter 2:J:5\n"; - if (sRidDeviceInfo.hid.dwVendorId == LOGITECH_VENDOR_ID) { - // std::cout << "nativeEventFilter 2:J:6\n"; - // std::cout << "raw->data.hid.bRawData=" << (raw->data.hid.bRawData) << "\n"; -#ifdef USE_QT5 - if (*(raw->data.hid.bRawData) == 0x01) { -#else - if (raw->data.hid.bRawData == 0x01) { -#endif - // Translation vector - // std::cout << "nativeEventFilter 2:J:7\n"; - - short *pnData = reinterpret_cast (&raw->data.hid.bRawData + 1); - // std::cout << "packet1 X: " << pnData[0] << " Y: " << pnData[1] << " Z: " << pnData[2] << "\n"; - // while(is_waiting) Sleep(1); - - valueMutex.lock(); - tmpValues.tx = pnData[0]; - tmpValues.ty = pnData[1]; - tmpValues.tz = pnData[2]; - idleFrameCount[0] = 0; - valueMutex.unlock(); -#ifdef USE_QT5 - } else if (*(raw->data.hid.bRawData) == 0x02) { -#else - } else if (raw->data.hid.bRawData == 0x02) { -#endif - // Direct rotation vector (NOT Euler) - short *pnData = reinterpret_cast (&raw->data.hid.bRawData + 1); - // std::cout << "packet2 rX: " << pnData[0] << " rY: " << pnData[1] << " rZ: " << pnData[2] << "\n"; - valueMutex.lock(); - tmpValues.rx = pnData[0]; - tmpValues.ry = pnData[1]; - tmpValues.rz = pnData[2]; - idleFrameCount[1] = 0; - valueMutex.unlock(); -#ifdef USE_QT5 - } else if (*(raw->data.hid.bRawData) == 0x03) { -#else - } else if (raw->data.hid.bRawData == 0x03) { -#endif - // State of the keys - unsigned long dwKeyState = *reinterpret_cast (&raw->data.hid.bRawData + 1); - if (dwKeyState & 1) { - valueMutex.lock(); - tmpValues.button1 = 1; - idleFrameCount[2] = 0; - valueMutex.unlock(); - } - if (dwKeyState & 2) { - valueMutex.lock(); - tmpValues.button2 = 1; - idleFrameCount[2] = 0; - valueMutex.unlock(); - } - // std::cout << "key pressed: " << dwKeyState << "\n"; - } - } - } - - // std::cout << "nativeEventFilter 2:K\n"; - - // Do something - //*result = 1l; //Jan Paul: I do not know why this causes a crash - // std::cout << "nativeEventFilter 2:K:2\n"; - delete[] lpb; - - // std::cout << "nativeEventFilter 2:L\n"; - - return true; - } - else { - // We do not want to handle this message so pass back to Windows - // to handle it in a default way - // std::cout << "nativeEventFilter 2:M\n"; - return false; - } - } - - } // end of namespace connexion_plugin - } // end of namespace plugins -} // end of namespace mars +/* + * Copyright 2011, 2012, DFKI GmbH Robotics Innovation Center + * + * This file is part of the MARS simulation framework. + * + * MARS is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * MARS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with MARS. If not, see . + * + */ + +#include "../ConnexionHID.h" + +#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501) + #undef _WIN32_WINNT + #define _WIN32_WINNT 0x0601 +#endif + +#include +#include +#ifdef USE_QT5 +#include +#endif +#include +#include +#include + +namespace mars { + namespace plugins { + namespace connexion_plugin { + + static int idleFrameCount[3] = {0,0,0}; + static int idleThreshold = 4; + + bool myEventFilter(void *message, long *result); + +#ifdef USE_QT5 + class SpaceMouseEventFilter : public QAbstractNativeEventFilter + { + public: + virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE + { + // std::cout << "nativeEventFilter 1\n"; + // std::cout << "event: <" << eventType.constData() << ">\n"; + if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG") { + // std::cout << "nativeEventFilter 2\n"; + return mars::plugins::connexion_plugin::myEventFilter(message, result); + } + // std::cout << "nativeEventFilter 3\n"; + return false; + } + }; + + SpaceMouseEventFilter* smef = NULL; +#endif + + // let's have some global variables. great! + static mars::utils::Mutex valueMutex; + static connexionValues tmpValues; + + int registerRawDevices(HWND hwndMessagesWindow) { + RAWINPUTDEVICE sRawInputDevices[1]; + sRawInputDevices[0].usUsagePage = 0x01; + sRawInputDevices[0].usUsage = 0x08; + sRawInputDevices[0].dwFlags = 0x00; + sRawInputDevices[0].hwndTarget = 0x00; + //RIM_TYPEHID; + + unsigned int uiNumDevices = sizeof(sRawInputDevices)/sizeof(sRawInputDevices[0]); + unsigned int cbSize = sizeof(sRawInputDevices[0]); + + unsigned int i; + for (i = 0; i < uiNumDevices; ++i) { + sRawInputDevices[i].hwndTarget = hwndMessagesWindow; + } + return RegisterRawInputDevices(sRawInputDevices, uiNumDevices, cbSize); + } + + + int initConnexionHID(void* windowID) { + tmpValues.tx = 0.0; + tmpValues.ty = 0.0; + tmpValues.tz = 0.0; + tmpValues.rx = 0.0; + tmpValues.ry = 0.0; + tmpValues.rz = 0.0; + tmpValues.button1 = 0; + tmpValues.button2 = 0; + +#ifdef USE_QT5 + smef=new SpaceMouseEventFilter(); + qApp->installNativeEventFilter(new SpaceMouseEventFilter); +#else + qApp->setEventFilter(myEventFilter); +#endif + + HWND handleForMessages = (HWND)windowID; + return registerRawDevices(handleForMessages); + } + + void getValue(mars::interfaces::sReal *coordinates, + struct connexionValues *rawValues) { + valueMutex.lock(); + for(int i = 0; i < 3; ++i) { + ++idleFrameCount[i]; + } + *rawValues = tmpValues; + + coordinates[0] = tmpValues.tx * fabs(tmpValues.tx * 0.001); + coordinates[1] = -tmpValues.tz * fabs(tmpValues.tz * 0.001); + coordinates[2] = -tmpValues.ty * fabs(tmpValues.ty * 0.001); + coordinates[3] = tmpValues.rx * fabs(tmpValues.rx * 0.0008); + coordinates[4] = -tmpValues.rz * fabs(tmpValues.rz * 0.0008); + coordinates[5] = -tmpValues.ry * fabs(tmpValues.ry * 0.0008); + + if(idleFrameCount[0] > idleThreshold) { + tmpValues.tx = 0.0; + tmpValues.ty = 0.0; + tmpValues.tz = 0.0; + } + if(idleFrameCount[1] > idleThreshold) { + tmpValues.rx = 0.0; + tmpValues.ry = 0.0; + tmpValues.rz = 0.0; + } + if(idleFrameCount[2] > idleThreshold) { + tmpValues.button1 = 0; + tmpValues.button2 = 0; + } + valueMutex.unlock(); + } + + void closeConnexionHID() { +#ifdef USE_QT5 + delete smef; + smef=NULL; +#endif + } + + + bool myEventFilter(void *message, long *result) { + + // std::cout << "nativeEventFilter 2:A\n"; + + MSG *msgStruct = (MSG*) message; + // std::cout << "nativeEventFilter 2:B\n"; + + if (msgStruct->message == WM_INPUT) { + // std::cout << "nativeEventFilter 2:C\n"; + + // std::cout << "Message: WM_INPUT erhalten" << "\n"; + + unsigned int dwSize = 0; + // std::cout << "nativeEventFilter 2:D\n"; + + GetRawInputData((HRAWINPUT)msgStruct->lParam, RID_INPUT, NULL, + &dwSize, sizeof(RAWINPUTHEADER)); + // std::cout << "nativeEventFilter 2:E\n"; + + LPBYTE lpb = new BYTE[dwSize]; + // std::cout << "nativeEventFilter 2:F\n"; + + if (GetRawInputData((HRAWINPUT)msgStruct->lParam, RID_INPUT, + lpb, &dwSize, sizeof(RAWINPUTHEADER)) + != dwSize) { + // std::cout << "nativeEventFilter 2:G\n"; + delete[] lpb; + return false; + } + + // std::cout << "nativeEventFilter 2:H\n"; + + RAWINPUT* raw = (RAWINPUT*) lpb; + + // std::cout << "nativeEventFilter 2:I\n"; + + if (raw->header.dwType != RIM_TYPEHID) { + delete[] lpb; + return false; + } + + // std::cout << "nativeEventFilter 2:J\n"; + + RID_DEVICE_INFO sRidDeviceInfo; + // std::cout << "nativeEventFilter 2:J:2\n"; + sRidDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO); + // std::cout << "nativeEventFilter 2:J:3\n"; + dwSize = sizeof(RID_DEVICE_INFO); + // std::cout << "nativeEventFilter 2:J:4\n"; + + if (GetRawInputDeviceInfo(raw->header.hDevice, + RIDI_DEVICEINFO, + &sRidDeviceInfo, + &dwSize) == dwSize) { + // std::cout << "nativeEventFilter 2:J:5\n"; + if (sRidDeviceInfo.hid.dwVendorId == LOGITECH_VENDOR_ID) { + // std::cout << "nativeEventFilter 2:J:6\n"; + // std::cout << "raw->data.hid.bRawData=" << (raw->data.hid.bRawData) << "\n"; +#ifdef USE_QT5 + if (*(raw->data.hid.bRawData) == 0x01) { +#else + if (raw->data.hid.bRawData == 0x01) { +#endif + // Translation vector + // std::cout << "nativeEventFilter 2:J:7\n"; + + short *pnData = reinterpret_cast (&raw->data.hid.bRawData + 1); + // std::cout << "packet1 X: " << pnData[0] << " Y: " << pnData[1] << " Z: " << pnData[2] << "\n"; + // while(is_waiting) Sleep(1); + + valueMutex.lock(); + tmpValues.tx = pnData[0]; + tmpValues.ty = pnData[1]; + tmpValues.tz = pnData[2]; + idleFrameCount[0] = 0; + valueMutex.unlock(); +#ifdef USE_QT5 + } else if (*(raw->data.hid.bRawData) == 0x02) { +#else + } else if (raw->data.hid.bRawData == 0x02) { +#endif + // Direct rotation vector (NOT Euler) + short *pnData = reinterpret_cast (&raw->data.hid.bRawData + 1); + // std::cout << "packet2 rX: " << pnData[0] << " rY: " << pnData[1] << " rZ: " << pnData[2] << "\n"; + valueMutex.lock(); + tmpValues.rx = pnData[0]; + tmpValues.ry = pnData[1]; + tmpValues.rz = pnData[2]; + idleFrameCount[1] = 0; + valueMutex.unlock(); +#ifdef USE_QT5 + } else if (*(raw->data.hid.bRawData) == 0x03) { +#else + } else if (raw->data.hid.bRawData == 0x03) { +#endif + // State of the keys + unsigned long dwKeyState = *reinterpret_cast (&raw->data.hid.bRawData + 1); + if (dwKeyState & 1) { + valueMutex.lock(); + tmpValues.button1 = 1; + idleFrameCount[2] = 0; + valueMutex.unlock(); + } + if (dwKeyState & 2) { + valueMutex.lock(); + tmpValues.button2 = 1; + idleFrameCount[2] = 0; + valueMutex.unlock(); + } + // std::cout << "key pressed: " << dwKeyState << "\n"; + } + } + } + + // std::cout << "nativeEventFilter 2:K\n"; + + // Do something + //*result = 1l; //Jan Paul: I do not know why this causes a crash + // std::cout << "nativeEventFilter 2:K:2\n"; + delete[] lpb; + + // std::cout << "nativeEventFilter 2:L\n"; + + return true; + } + else { + // We do not want to handle this message so pass back to Windows + // to handle it in a default way + // std::cout << "nativeEventFilter 2:M\n"; + return false; + } + } + + } // end of namespace connexion_plugin + } // end of namespace plugins +} // end of namespace mars From 7a2e4aa5793757e47301bccfbb2987874122c06a Mon Sep 17 00:00:00 2001 From: "Jan Paul (RH1, Windows)" Date: Fri, 28 Aug 2015 11:54:40 +0200 Subject: [PATCH 4/5] nicely removeNativeEventFilter before deleting it --- plugins/connexion_plugin/win32/ConnexionHID.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/connexion_plugin/win32/ConnexionHID.cpp b/plugins/connexion_plugin/win32/ConnexionHID.cpp index b9d8847c2..3c56d44a2 100644 --- a/plugins/connexion_plugin/win32/ConnexionHID.cpp +++ b/plugins/connexion_plugin/win32/ConnexionHID.cpp @@ -141,6 +141,7 @@ namespace mars { void closeConnexionHID() { #ifdef USE_QT5 + qApp->removeNativeEventFilter(smef); delete smef; smef=NULL; #endif From 0bd3cd57070562678c9b592f62ddaa82f476947b Mon Sep 17 00:00:00 2001 From: "Jan Paul (RH1, Windows)" Date: Mon, 14 Sep 2015 13:40:16 +0200 Subject: [PATCH 5/5] small formatting improvement --- plugins/connexion_plugin/win32/ConnexionHID.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/connexion_plugin/win32/ConnexionHID.cpp b/plugins/connexion_plugin/win32/ConnexionHID.cpp index 3c56d44a2..9703a24a9 100644 --- a/plugins/connexion_plugin/win32/ConnexionHID.cpp +++ b/plugins/connexion_plugin/win32/ConnexionHID.cpp @@ -141,9 +141,9 @@ namespace mars { void closeConnexionHID() { #ifdef USE_QT5 - qApp->removeNativeEventFilter(smef); - delete smef; - smef=NULL; + qApp->removeNativeEventFilter(smef); + delete smef; + smef=NULL; #endif }