Hid/HidHandler.cs
author StephaneLenclud
Sun, 15 Mar 2015 20:25:58 +0100
changeset 79 cdc5f8f1b79e
parent 78 HidHandler.cs@72885c950813
child 81 baabcd5cdf8c
permissions -rw-r--r--
Moving files around.
StephaneLenclud@76
     1
//
StephaneLenclud@76
     2
// Copyright (C) 2014-2015 Stéphane Lenclud.
StephaneLenclud@76
     3
//
StephaneLenclud@76
     4
// This file is part of SharpLibHid.
StephaneLenclud@76
     5
//
StephaneLenclud@76
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@76
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@76
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@76
     9
// (at your option) any later version.
StephaneLenclud@76
    10
//
StephaneLenclud@76
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@76
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@76
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@76
    14
// GNU General Public License for more details.
StephaneLenclud@76
    15
//
StephaneLenclud@76
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@76
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@76
    18
//
StephaneLenclud@76
    19
StephaneLenclud@76
    20
sl@29
    21
using System;
sl@29
    22
using System.Windows.Forms;
sl@29
    23
using System.Runtime.InteropServices;
sl@29
    24
using System.Diagnostics;
sl@29
    25
using System.Text;
sl@29
    26
using Microsoft.Win32.SafeHandles;
StephaneLenclud@77
    27
using SharpLib.Win32;
sl@29
    28
using System.Collections.Generic;
sl@29
    29
sl@29
    30
StephaneLenclud@77
    31
namespace SharpLib.Hid
sl@29
    32
{
sl@29
    33
    /// <summary>
sl@29
    34
    /// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return.
sl@29
    35
    /// </summary>
sl@40
    36
    public class HidHandler
sl@29
    37
    {
sl@29
    38
        public delegate void HidEventHandler(object aSender, HidEvent aHidEvent);
sl@29
    39
        public event HidEventHandler OnHidEvent;
sl@41
    40
        List<HidEvent> iHidEvents;
sl@41
    41
sl@29
    42
sl@29
    43
        public bool IsRegistered { get; private set; }
sl@29
    44
sl@29
    45
        public HidHandler(RAWINPUTDEVICE[] aRawInputDevices)
sl@29
    46
        {
sl@41
    47
            iHidEvents=new List<HidEvent>();
sl@29
    48
            IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0]));
sl@29
    49
        }
sl@29
    50
StephaneLenclud@78
    51
        /// <summary>
StephaneLenclud@78
    52
        /// Process a WM_INPUT message.
StephaneLenclud@78
    53
        /// </summary>
StephaneLenclud@78
    54
        /// <param name="aMessage"></param>
StephaneLenclud@78
    55
        public void ProcessInput(ref Message aMessage)
sl@29
    56
        {
StephaneLenclud@78
    57
            if (aMessage.Msg != Const.WM_INPUT)
StephaneLenclud@78
    58
            {
StephaneLenclud@78
    59
                //We only process WM_INPUT messages
StephaneLenclud@78
    60
                return;
StephaneLenclud@78
    61
            }
StephaneLenclud@78
    62
StephaneLenclud@76
    63
            HidEvent hidEvent = new HidEvent(aMessage, OnHidEventRepeat);
sl@29
    64
            hidEvent.DebugWrite();
sl@29
    65
sl@29
    66
            if (!hidEvent.IsValid || !hidEvent.IsGeneric)
sl@29
    67
            {
sl@29
    68
                Debug.WriteLine("Skipping HID message.");
sl@29
    69
                return;
sl@29
    70
            }
sl@29
    71
sl@41
    72
            //
StephaneLenclud@49
    73
            if (hidEvent.IsButtonUp)
sl@41
    74
            {
sl@41
    75
                //This is a key up event
sl@41
    76
                //We need to discard any events belonging to the same page and collection
sl@41
    77
                for (int i = (iHidEvents.Count-1); i >= 0; i--)
sl@41
    78
                {
sl@41
    79
                    if (iHidEvents[i].UsageId == hidEvent.UsageId)
sl@41
    80
                    {
sl@41
    81
                        iHidEvents[i].Dispose();
sl@41
    82
                        iHidEvents.RemoveAt(i);
sl@41
    83
                    }
sl@41
    84
                }
sl@41
    85
            }
sl@41
    86
            else
sl@41
    87
            {
sl@41
    88
                //Keep that event until we get a key up message
sl@41
    89
                iHidEvents.Add(hidEvent);
sl@41
    90
            }
sl@41
    91
sl@29
    92
            //Broadcast our events
sl@29
    93
            OnHidEvent(this, hidEvent);    
sl@29
    94
        }
sl@29
    95
sl@41
    96
        public void OnHidEventRepeat(HidEvent aHidEvent)
sl@41
    97
        {
sl@41
    98
            //Broadcast our events
sl@41
    99
            OnHidEvent(this, aHidEvent);    
sl@41
   100
        }
sl@41
   101
sl@29
   102
    }
sl@29
   103
sl@29
   104
}