示例#1
0
bool Mouse3DInput::translateRawInputData(UINT nInputCode, PRAWINPUT pRawInput)
{
	bool bIsForeground = (nInputCode == RIM_INPUT);

	// We are not interested in keyboard or mouse data received via raw input
	if (pRawInput->header.dwType != RIM_TYPEHID)
		return false;

	RID_DEVICE_INFO sRidDeviceInfo;
	sRidDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO);
	UINT cbSize = sizeof(RID_DEVICE_INFO);

	if (::GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_DEVICEINFO, &sRidDeviceInfo, &cbSize) == cbSize)
	{
		if (sRidDeviceInfo.hid.dwVendorId == LOGITECH_VENDOR_ID)
		{
			if (pRawInput->data.hid.bRawData[0] == 0x01)
			{
				// Translation vector
				InputData& deviceData = m_device2Data[pRawInput->header.hDevice];
				deviceData.timeToLive = c_defaultTimeToLive;
				if (bIsForeground)
				{
					short* pnRawData = reinterpret_cast<short*>(&pRawInput->data.hid.bRawData[1]);
					// Cache the pan zoom data
					deviceData.axes[0] = static_cast<float>(pnRawData[0]);
					deviceData.axes[1] = static_cast<float>(pnRawData[1]);
					deviceData.axes[2] = static_cast<float>(pnRawData[2]);

					if (pRawInput->data.hid.dwSizeHid >= 13) // Highspeed package
					{
						// Cache the rotation data
						deviceData.axes[3] = static_cast<float>(pnRawData[3]);
						deviceData.axes[4] = static_cast<float>(pnRawData[4]);
						deviceData.axes[5] = static_cast<float>(pnRawData[5]);
						deviceData.isDirty = true;
						return true;
					}
				}
				else // Zero out the data if the app is not in forground
				{
					deviceData.axes.assign(6, 0.0f);
				}
			}
			else if (pRawInput->data.hid.bRawData[0] == 0x02)
			{
				// Rotation vector
				// If we are not in foreground do nothing
				// The rotation vector was zeroed out with the translation vector in the previous message
				if (bIsForeground)
				{
					InputData& deviceData = m_device2Data[pRawInput->header.hDevice];
					deviceData.timeToLive = c_defaultTimeToLive;

					short* pnRawData = reinterpret_cast<short*>(&pRawInput->data.hid.bRawData[1]);
					// Cache the rotation data
					deviceData.axes[3] = static_cast<float>(pnRawData[0]);
					deviceData.axes[4] = static_cast<float>(pnRawData[1]);
					deviceData.axes[5] = static_cast<float>(pnRawData[2]);
					deviceData.isDirty = true;

					return true;
				}
			}
			else if (pRawInput->data.hid.bRawData[0] == 0x03) // Keystate change
			{
				// this is a package that contains 3d mouse keystate information
				// bit0=key1, bit=key2 etc.
				unsigned long dwKeystate = *reinterpret_cast<unsigned long*>(&pRawInput->data.hid.bRawData[1]);

				// Log the keystate changes
				unsigned long dwOldKeystate = m_device2Keystate[pRawInput->header.hDevice];
				if (dwKeystate != 0)
					m_device2Keystate[pRawInput->header.hDevice] = dwKeystate;
				else
					m_device2Keystate.erase(pRawInput->header.hDevice);

				//  Only call the keystate change handlers if the app is in foreground
				if (bIsForeground)
				{
					unsigned long dwChange = dwKeystate ^ dwOldKeystate;

					for (int nKeycode=1; nKeycode<33; nKeycode++)
					{
						if (dwChange & 0x01)
						{
							int nVirtualKeyCode = HidToVirtualKey(sRidDeviceInfo.hid.dwProductId, nKeycode);
							if (nVirtualKeyCode)
							{
								if (dwKeystate&0x01)
									on3dmouseKeyDown(pRawInput->header.hDevice, nVirtualKeyCode);
								else
									on3dmouseKeyUp(pRawInput->header.hDevice, nVirtualKeyCode);
							}
						}
						dwChange >>= 1;
						dwKeystate >>= 1;
					}
				}
			}
		}
	}
bool ConnexionClient::TranslateRawInputData(UINT nInputCode, PRAWINPUT pRawInput) {
    bool bIsForeground = (nInputCode == RIM_INPUT);

    // We are not interested in keyboard or mouse data received via raw input
    if (pRawInput->header.dwType != RIM_TYPEHID) {
        return false;
    }

    if (TRACE_RIDI_DEVICENAME == 1) {
        UINT dwSize = 0;
        if (::GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_DEVICENAME, NULL, &dwSize) == 0) {
            std::vector<wchar_t> szDeviceName(dwSize + 1);
            if (::GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_DEVICENAME, &szDeviceName[0], &dwSize) > 0) {
                qDebug("Device Name = %s\nDevice handle = 0x%x\n", &szDeviceName[0], pRawInput->header.hDevice);
            }
        }
    }

    RID_DEVICE_INFO sRidDeviceInfo;
    sRidDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO);
    UINT cbSize = sizeof(RID_DEVICE_INFO);

    if (::GetRawInputDeviceInfo(pRawInput->header.hDevice, RIDI_DEVICEINFO, &sRidDeviceInfo, &cbSize) == cbSize) {
        if (TRACE_RIDI_DEVICEINFO == 1) {
            switch (sRidDeviceInfo.dwType) {
                case RIM_TYPEMOUSE:
                    qDebug("\tsRidDeviceInfo.dwType=RIM_TYPEMOUSE\n");
                    break;
                case RIM_TYPEKEYBOARD:
                    qDebug("\tsRidDeviceInfo.dwType=RIM_TYPEKEYBOARD\n");
                    break;
                case RIM_TYPEHID:
                    qDebug("\tsRidDeviceInfo.dwType=RIM_TYPEHID\n");
                    qDebug("\tVendor=0x%x\n\tProduct=0x%x\n\tUsagePage=0x%x\n\tUsage=0x%x\n",
                           sRidDeviceInfo.hid.dwVendorId,
                           sRidDeviceInfo.hid.dwProductId,
                           sRidDeviceInfo.hid.usUsagePage,
                           sRidDeviceInfo.hid.usUsage);
                    break;
            }
        }

        if (sRidDeviceInfo.hid.dwVendorId == LOGITECH_VENDOR_ID) {
            if (pRawInput->data.hid.bRawData[0] == 0x01) { // Translation vector
                TInputData& deviceData = fDevice2Data[pRawInput->header.hDevice];
                deviceData.fTimeToLive = kTimeToLive;
                if (bIsForeground) {
                    short* pnRawData = reinterpret_cast<short*>(&pRawInput->data.hid.bRawData[1]);
                    // Cache the pan zoom data
                    deviceData.fAxes[0] = static_cast<float>(pnRawData[0]);
                    deviceData.fAxes[1] = static_cast<float>(pnRawData[1]);
                    deviceData.fAxes[2] = static_cast<float>(pnRawData[2]);

                    //qDebug("Pan/Zoom RI Data =\t0x%x,\t0x%x,\t0x%x\n", pnRawData[0], pnRawData[1], pnRawData[2]);

                    if (pRawInput->data.hid.dwSizeHid >= 13) { // Highspeed package
                        // Cache the rotation data
                        deviceData.fAxes[3] = static_cast<float>(pnRawData[3]);
                        deviceData.fAxes[4] = static_cast<float>(pnRawData[4]);
                        deviceData.fAxes[5] = static_cast<float>(pnRawData[5]);
                        deviceData.fIsDirty = true;

                        //qDebug("Rotation RI Data =\t0x%x,\t0x%x,\t0x%x\n", pnRawData[3], pnRawData[4], pnRawData[5]);
                        return true;
                    }
                } else { // Zero out the data if the app is not in forground
                    deviceData.fAxes.assign(6, 0.f);
                }
            } else if (pRawInput->data.hid.bRawData[0] == 0x02) { // Rotation vector
                // If we are not in foreground do nothing
                // The rotation vector was zeroed out with the translation vector in the previous message
                if (bIsForeground) {
                    TInputData& deviceData = fDevice2Data[pRawInput->header.hDevice];
                    deviceData.fTimeToLive = kTimeToLive;

                    short* pnRawData = reinterpret_cast<short*>(&pRawInput->data.hid.bRawData[1]);
                    // Cache the rotation data
                    deviceData.fAxes[3] = static_cast<float>(pnRawData[0]);
                    deviceData.fAxes[4] = static_cast<float>(pnRawData[1]);
                    deviceData.fAxes[5] = static_cast<float>(pnRawData[2]);
                    deviceData.fIsDirty = true;

                    //qDebug("Rotation RI Data =\t0x%x,\t0x%x,\t0x%x\n", pnRawData[0], pnRawData[1], pnRawData[2]);

                    return true;
                }
            } else if (pRawInput->data.hid.bRawData[0] == 0x03) { // Keystate change
                // this is a package that contains 3d mouse keystate information
                // bit0=key1, bit=key2 etc.

                unsigned long dwKeystate = *reinterpret_cast<unsigned long*>(&pRawInput->data.hid.bRawData[1]);

                //qDebug("ButtonData =0x%x\n", dwKeystate);

                // Log the keystate changes
                unsigned long dwOldKeystate = fDevice2Keystate[pRawInput->header.hDevice];
                if (dwKeystate != 0) {
                    fDevice2Keystate[pRawInput->header.hDevice] = dwKeystate;
                } else {
                    fDevice2Keystate.erase(pRawInput->header.hDevice);
                }

                //  Only call the keystate change handlers if the app is in foreground
                if (bIsForeground) {
                    unsigned long dwChange = dwKeystate ^ dwOldKeystate;

                    for (int nKeycode = 1; nKeycode<33; nKeycode++) {
                        if (dwChange & 0x01) {
                            int nVirtualKeyCode = HidToVirtualKey(sRidDeviceInfo.hid.dwProductId, nKeycode);
                            if (nVirtualKeyCode) {
                                if (dwKeystate & 0x01) {
                                    On3dmouseKeyDown(pRawInput->header.hDevice, nVirtualKeyCode);
                                } else {
                                    On3dmouseKeyUp(pRawInput->header.hDevice, nVirtualKeyCode);
                                }
                            }
                        }
                        dwChange >>= 1;
                        dwKeystate >>= 1;
                    }
                }
            }
        }
    }