Hid/HidHandler.cs
author StephaneLenclud
Tue, 17 Mar 2015 16:46:28 +0100
changeset 84 e13ea80016a1
parent 83 2d5955694057
permissions -rw-r--r--
Making our repeat feature a lot safer.
DebugWrites now only compiled for debug builds.
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>
StephaneLenclud@83
    36
    public class Handler : IDisposable
sl@29
    37
    {
StephaneLenclud@81
    38
        public delegate void HidEventHandler(object aSender, Event aHidEvent);
sl@29
    39
        public event HidEventHandler OnHidEvent;
StephaneLenclud@81
    40
        List<Event> iHidEvents;
StephaneLenclud@83
    41
        RAWINPUTDEVICE[] iRawInputDevices;
sl@41
    42
sl@29
    43
sl@29
    44
        public bool IsRegistered { get; private set; }
StephaneLenclud@83
    45
        public bool ManageRepeats { get; private set; }
sl@29
    46
StephaneLenclud@83
    47
        public Handler(RAWINPUTDEVICE[] aRawInputDevices, bool aManageRepeats=false)
sl@29
    48
        {
StephaneLenclud@83
    49
            iRawInputDevices = aRawInputDevices;
StephaneLenclud@83
    50
            iHidEvents = new List<Event>();
StephaneLenclud@83
    51
            IsRegistered = Function.RegisterRawInputDevices(iRawInputDevices, (uint)iRawInputDevices.Length, (uint)Marshal.SizeOf(iRawInputDevices[0]));
StephaneLenclud@83
    52
            ManageRepeats = aManageRepeats;
StephaneLenclud@83
    53
        }
StephaneLenclud@83
    54
StephaneLenclud@83
    55
        /// <summary>
StephaneLenclud@83
    56
        /// Will de-register devices.
StephaneLenclud@83
    57
        /// </summary>
StephaneLenclud@83
    58
        public void Dispose()
StephaneLenclud@83
    59
        {
StephaneLenclud@83
    60
            //Setup device removal
StephaneLenclud@83
    61
            for (int i=0; i<iRawInputDevices.Length; i++)
StephaneLenclud@83
    62
            {
StephaneLenclud@83
    63
                iRawInputDevices[i].dwFlags = Const.RIDEV_REMOVE;
StephaneLenclud@83
    64
                iRawInputDevices[i].hwndTarget = IntPtr.Zero;
StephaneLenclud@83
    65
            }
StephaneLenclud@83
    66
StephaneLenclud@83
    67
            //De-register
StephaneLenclud@83
    68
            Function.RegisterRawInputDevices(iRawInputDevices, (uint)iRawInputDevices.Length, (uint)Marshal.SizeOf(iRawInputDevices[0]));
StephaneLenclud@83
    69
            IsRegistered = false;
sl@29
    70
        }
sl@29
    71
StephaneLenclud@78
    72
        /// <summary>
StephaneLenclud@78
    73
        /// Process a WM_INPUT message.
StephaneLenclud@78
    74
        /// </summary>
StephaneLenclud@78
    75
        /// <param name="aMessage"></param>
StephaneLenclud@78
    76
        public void ProcessInput(ref Message aMessage)
sl@29
    77
        {
StephaneLenclud@78
    78
            if (aMessage.Msg != Const.WM_INPUT)
StephaneLenclud@78
    79
            {
StephaneLenclud@78
    80
                //We only process WM_INPUT messages
StephaneLenclud@78
    81
                return;
StephaneLenclud@78
    82
            }
StephaneLenclud@78
    83
StephaneLenclud@83
    84
            Event hidEvent = new Event(aMessage, OnHidEventRepeat, ManageRepeats);
sl@29
    85
            hidEvent.DebugWrite();
sl@29
    86
sl@29
    87
            if (!hidEvent.IsValid || !hidEvent.IsGeneric)
sl@29
    88
            {
sl@29
    89
                Debug.WriteLine("Skipping HID message.");
sl@29
    90
                return;
sl@29
    91
            }
sl@29
    92
StephaneLenclud@84
    93
            //We want to repeat only a single event at a time.
StephaneLenclud@84
    94
            //Any other event will interrupt the current repeat.
StephaneLenclud@83
    95
            if (ManageRepeats)
sl@41
    96
            {
StephaneLenclud@84
    97
                //Discard all outstanding repeats, though we should only ever have only one
StephaneLenclud@84
    98
                for (int i = (iHidEvents.Count - 1); i >= 0; i--)
sl@41
    99
                {
StephaneLenclud@84
   100
                        iHidEvents[i].Dispose();
StephaneLenclud@84
   101
                        iHidEvents.RemoveAt(i);
sl@41
   102
                }
StephaneLenclud@84
   103
                //Add our newly created event in our repeat list
StephaneLenclud@84
   104
                //TODO: instead of a list we could now have a single event since we only support one repeat at a time
StephaneLenclud@84
   105
                iHidEvents.Add(hidEvent);
sl@41
   106
            }
sl@41
   107
sl@29
   108
            //Broadcast our events
sl@29
   109
            OnHidEvent(this, hidEvent);    
sl@29
   110
        }
sl@29
   111
StephaneLenclud@81
   112
        public void OnHidEventRepeat(Event aHidEvent)
sl@41
   113
        {
sl@41
   114
            //Broadcast our events
sl@41
   115
            OnHidEvent(this, aHidEvent);    
sl@41
   116
        }
sl@41
   117
sl@29
   118
    }
sl@29
   119
sl@29
   120
}