Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connexion hid win32 qt5 #48

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 80 additions & 8 deletions plugins/connexion_plugin/win32/ConnexionHID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@

#include <mars/utils/Mutex.h>
#include <QApplication>
#ifdef USE_QT5
#include <QAbstractNativeEventFilter>
#endif
#include <cmath>
#include <windows.h>
#include <iostream>

namespace mars {
namespace plugins {
Expand All @@ -39,11 +43,30 @@ namespace mars {

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;
Expand Down Expand Up @@ -73,7 +96,12 @@ namespace mars {
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);
Expand Down Expand Up @@ -112,53 +140,82 @@ namespace mars {
}

void closeConnexionHID() {
#ifdef USE_QT5
qApp->removeNativeEventFilter(smef);
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";


//cout << "Message: WM_INPUT erhalten" << endl;
// 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 <short*> (&raw->data.hid.bRawData + 1);
//cout << "packet1 X: " << pnData[0] << " Y: " << pnData[1] << " Z: " << pnData[2] << endl;
// std::cout << "packet1 X: " << pnData[0] << " Y: " << pnData[1] << " Z: " << pnData[2] << "\n";
// while(is_waiting) Sleep(1);

valueMutex.lock();
Expand All @@ -167,17 +224,25 @@ namespace mars {
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) {
// Direct rotation vector (NOT Euler)
#endif
// Direct rotation vector (NOT Euler)
short *pnData = reinterpret_cast <short*> (&raw->data.hid.bRawData + 1);
//cout << "packet2 rX: " << pnData[0] << " rY: " << pnData[1] << " rZ: " << pnData[2] << endl;
// 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 <unsigned long*> (&raw->data.hid.bRawData + 1);
if (dwKeyState & 1) {
Expand All @@ -192,19 +257,26 @@ namespace mars {
idleFrameCount[2] = 0;
valueMutex.unlock();
}
//cout << "key pressed: " << dwKeyState << endl;
// std::cout << "key pressed: " << dwKeyState << "\n";
}
}
}

// std::cout << "nativeEventFilter 2:K\n";

// Do something
*result = 1;
//*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;
}
}
Expand Down