Ejemplo n.º 1
0
        private void GetPhysicalMax(NativeMethods.RAWINPUT rawInput, IntPtr pPreparsedData)
        {
            short valueCapsLength = 1;
            Point p = new Point();

            HidNativeApi.HidP_Value_Caps[] hvc = new HidNativeApi.HidP_Value_Caps[valueCapsLength];

            HidNativeApi.HidP_GetSpecificValueCaps(HidReportType.Input, NativeMethods.GenericDesktopPage, 1, NativeMethods.XCoordinateId, hvc, ref valueCapsLength, pPreparsedData);
            p.X = hvc[0].PhysicalMax != 0 ? hvc[0].PhysicalMax : hvc[0].LogicalMax;

            HidNativeApi.HidP_GetSpecificValueCaps(HidReportType.Input, NativeMethods.GenericDesktopPage, 1, NativeMethods.YCoordinateId, hvc, ref valueCapsLength, pPreparsedData);
            p.Y = hvc[0].PhysicalMax != 0 ? hvc[0].PhysicalMax : hvc[0].LogicalMax;

            _touchScreenPhysicalMax[rawInput.header.hDevice] = p;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Processes WM_INPUT messages to retrieve information about any
        /// touch events that occur.
        /// </summary>
        /// <param name="LParam">The WM_INPUT message to process.</param>
        private void ProcessInputCommand(IntPtr LParam)
        {
            uint dwSize = 0;

            // First call to GetRawInputData sets the value of dwSize
            // dwSize can then be used to allocate the appropriate amount of memore,
            // storing the pointer in "buffer".
            NativeMethods.GetRawInputData(LParam, NativeMethods.RID_INPUT, IntPtr.Zero,
                                          ref dwSize,
                                          (uint)Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER)));

            IntPtr buffer         = Marshal.AllocHGlobal((int)dwSize);
            IntPtr pPreparsedData = IntPtr.Zero;

            try
            {
                // Check that buffer points to something, and if so,
                // call GetRawInputData again to fill the allocated memory
                // with information about the input
                if (buffer != IntPtr.Zero &&
                    NativeMethods.GetRawInputData(LParam, NativeMethods.RID_INPUT,
                                                  buffer,
                                                  ref dwSize,
                                                  (uint)Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER))) == dwSize)
                {
                    NativeMethods.RAWINPUT raw = (NativeMethods.RAWINPUT)Marshal.PtrToStructure(buffer, typeof(NativeMethods.RAWINPUT));

                    if (!_touchScreenPhysicalMax.ContainsKey(raw.header.hDevice))
                    {
                        return;
                    }
                    GetCurrentScreenOrientation();

                    uint pcbSize = 0;
                    NativeMethods.GetRawInputDeviceInfo(raw.header.hDevice, NativeMethods.RIDI_PREPARSEDDATA, IntPtr.Zero, ref pcbSize);
                    pPreparsedData = Marshal.AllocHGlobal((int)pcbSize);

                    NativeMethods.GetRawInputDeviceInfo(raw.header.hDevice, NativeMethods.RIDI_PREPARSEDDATA, pPreparsedData, ref pcbSize);

                    if (_touchScreenPhysicalMax[raw.header.hDevice].Equals(Point.Empty))
                    {
                        GetPhysicalMax(raw, pPreparsedData);
                        return;
                    }

                    int contactCount = 0;

                    IntPtr pRawData = new IntPtr(buffer.ToInt64() + (raw.header.dwSize - raw.hid.dwSizHid * raw.hid.dwCount));

                    HidNativeApi.HidP_GetUsageValue(HidReportType.Input, NativeMethods.TouchScreenUsagePage, 0, NativeMethods.ContactCountId,
                                                    ref contactCount, pPreparsedData, pRawData, raw.hid.dwSizHid);

                    int linkCount = 0;
                    HidNativeApi.HidP_GetLinkCollectionNodes(null, ref linkCount, pPreparsedData);
                    HidNativeApi.HIDP_LINK_COLLECTION_NODE[] lcn = new HidNativeApi.HIDP_LINK_COLLECTION_NODE[linkCount];
                    HidNativeApi.HidP_GetLinkCollectionNodes(lcn, ref linkCount, pPreparsedData);

                    if (contactCount != 0)
                    {
                        _requiringContactCount = contactCount;
                        _outputTouchs          = new List <RawTouchData>(contactCount);
                    }
                    if (_requiringContactCount == 0)
                    {
                        return;
                    }
                    int contactIdentifier = 0;
                    int physicalX         = 0;
                    int physicalY         = 0;
                    int screenWidth       = Screen.PrimaryScreen.Bounds.Width;
                    int screenHeight      = Screen.PrimaryScreen.Bounds.Height;
                    for (int dwIndex = 0; dwIndex < raw.hid.dwCount; dwIndex++)
                    {
                        for (short nodeIndex = 1; nodeIndex <= lcn[0].NumberOfChildren; nodeIndex++)
                        {
                            IntPtr pRawDataPacket = new IntPtr(pRawData.ToInt64() + dwIndex * raw.hid.dwSizHid);
                            HidNativeApi.HidP_GetUsageValue(HidReportType.Input, NativeMethods.TouchScreenUsagePage, nodeIndex, NativeMethods.ContactIdentifierId, ref contactIdentifier, pPreparsedData, pRawDataPacket, raw.hid.dwSizHid);
                            HidNativeApi.HidP_GetScaledUsageValue(HidReportType.Input, NativeMethods.GenericDesktopPage, nodeIndex, NativeMethods.XCoordinateId, ref physicalX, pPreparsedData, pRawDataPacket, raw.hid.dwSizHid);
                            HidNativeApi.HidP_GetScaledUsageValue(HidReportType.Input, NativeMethods.GenericDesktopPage, nodeIndex, NativeMethods.YCoordinateId, ref physicalY, pPreparsedData, pRawDataPacket, raw.hid.dwSizHid);

                            int usageLength = 0;
                            HidNativeApi.HidP_GetUsages(HidReportType.Input, NativeMethods.TouchScreenUsagePage, nodeIndex, null, ref usageLength, pPreparsedData, pRawData, raw.hid.dwSizHid);
                            HidNativeApi.HIDP_DATA[] hd = new HidNativeApi.HIDP_DATA[usageLength];
                            HidNativeApi.HidP_GetUsages(HidReportType.Input, NativeMethods.TouchScreenUsagePage, nodeIndex, hd, ref usageLength, pPreparsedData, pRawData, raw.hid.dwSizHid);
                            int x, y;
                            if (_isAxisCorresponds)
                            {
                                x = physicalX * screenWidth / _touchScreenPhysicalMax[raw.header.hDevice].X;
                                y = physicalY * screenHeight / _touchScreenPhysicalMax[raw.header.hDevice].Y;
                            }
                            else
                            {
                                x = physicalY * screenWidth / _touchScreenPhysicalMax[raw.header.hDevice].Y;
                                y = physicalX * screenHeight / _touchScreenPhysicalMax[raw.header.hDevice].X;
                            }

                            x = _xAxisDirection ? x : screenWidth - x;
                            y = _yAxisDirection ? y : screenHeight - y;
                            bool tip = hd.Length != 0 && hd[0].DataIndex == NativeMethods.TipId;
                            _outputTouchs.Add(new RawTouchData(tip, contactIdentifier, new Point(x, y)));

                            if (--_requiringContactCount == 0)
                            {
                                break;
                            }
                        }
                        if (_requiringContactCount == 0)
                        {
                            break;
                        }
                    }

                    if (_requiringContactCount == 0 && PointsIntercepted != null)
                    {
                        PointsIntercepted(this, new RawPointsDataMessageEventArgs(_outputTouchs));
                    }
                }
                else
                {
                    throw new ApplicationException("GetRawInputData does not return correct size !\n.");
                }
            }
            finally
            {
                Marshal.FreeHGlobal(pPreparsedData);
                Marshal.FreeHGlobal(buffer);
            }
        }
        bool CopyRawInputToDataByteArray(IntPtr lParam)
        {
            int dwSize             = 0;
            int rawInputHeaderSize = Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER));

            //If pData is NULL, the required size of the buffer is returned in *pcbSize.
            NativeMethods.GetRawInputData(lParam, (uint)NativeMethods.RID.INPUT, IntPtr.Zero, ref dwSize, rawInputHeaderSize);

            if (dwSize > rawInputDataBufferSize)
            {
                rawInputDataBufferSize = dwSize;
                ReleaseRawInputPtr();
            }
            if (rawInputPtr == IntPtr.Zero)
            {
                // allocate unmanaged memory, must be released by Marshal.FreeHGlobal in ReleaseRawInputDataBuffer()
                rawInputPtr = Marshal.AllocHGlobal((int)rawInputDataBufferSize);
                if (rawInputPtr == IntPtr.Zero)
                {
                    if (ApplicationCommonSettings.IsDebugging)
                    {
                        Debugger.Break();
                    }
                    return(false);
                }
            }
            try
            {
                if (NativeMethods.GetRawInputData(lParam, (uint)NativeMethods.RID.INPUT, rawInputPtr, ref dwSize, rawInputHeaderSize) != dwSize)
                {
                    if (ApplicationCommonSettings.IsDebugging)
                    {
                        Debugger.Break();
                    }
                    return(false);
                }
                rawInput = (NativeMethods.RAWINPUT)Marshal.PtrToStructure(rawInputPtr, typeof(NativeMethods.RAWINPUT));
                //Debug.WriteLine("rawInput.header.hDevice: " + rawInput.header.hDevice);

                latestRawInputKind = rawInput.header.dwType;

                int offset = 0;
                reportAsByteArrayActualLength = 0;
                switch (latestRawInputKind)
                {
                case NativeMethods.RawInputHeaderType.Hid:
                    offset = Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER)) + Marshal.SizeOf(typeof(NativeMethods.RAWHID));
                    reportAsByteArrayActualLength = rawInput.hid.dwSizeHid;
                    break;

                case NativeMethods.RawInputHeaderType.Mouse:
                    offset = Marshal.SizeOf(typeof(NativeMethods.RAWINPUTHEADER));
                    reportAsByteArrayActualLength = Marshal.SizeOf(typeof(NativeMethods.RAWMOUSE));
                    break;

                case NativeMethods.RawInputHeaderType.Keyboard:
                    return(false);

                    break;

                default:
                    return(false);

                    break;
                }
                if (reportAsByteArrayActualLength > 64)
                {
                    return(false);
                }
                Marshal.Copy(rawInputPtr, bRawData, 0, offset + reportAsByteArrayActualLength);
                Array.Copy(bRawData, offset, reportAsByteArray, 0, reportAsByteArrayActualLength);
                return(true);
            }
            catch (Exception ex)
            {
                if (ApplicationCommonSettings.IsDebugging)
                {
                    Debugger.Break();
                }
                Debug.WriteLine(ex.Message);
                return(false);
            }
        }