Hid/HidHandler.cs
changeset 79 cdc5f8f1b79e
parent 78 72885c950813
child 81 baabcd5cdf8c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hid/HidHandler.cs	Sun Mar 15 20:25:58 2015 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +//
     1.5 +// Copyright (C) 2014-2015 Stéphane Lenclud.
     1.6 +//
     1.7 +// This file is part of SharpLibHid.
     1.8 +//
     1.9 +// SharpDisplayManager is free software: you can redistribute it and/or modify
    1.10 +// it under the terms of the GNU General Public License as published by
    1.11 +// the Free Software Foundation, either version 3 of the License, or
    1.12 +// (at your option) any later version.
    1.13 +//
    1.14 +// SharpDisplayManager is distributed in the hope that it will be useful,
    1.15 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +// GNU General Public License for more details.
    1.18 +//
    1.19 +// You should have received a copy of the GNU General Public License
    1.20 +// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    1.21 +//
    1.22 +
    1.23 +
    1.24 +using System;
    1.25 +using System.Windows.Forms;
    1.26 +using System.Runtime.InteropServices;
    1.27 +using System.Diagnostics;
    1.28 +using System.Text;
    1.29 +using Microsoft.Win32.SafeHandles;
    1.30 +using SharpLib.Win32;
    1.31 +using System.Collections.Generic;
    1.32 +
    1.33 +
    1.34 +namespace SharpLib.Hid
    1.35 +{
    1.36 +    /// <summary>
    1.37 +    /// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return.
    1.38 +    /// </summary>
    1.39 +    public class HidHandler
    1.40 +    {
    1.41 +        public delegate void HidEventHandler(object aSender, HidEvent aHidEvent);
    1.42 +        public event HidEventHandler OnHidEvent;
    1.43 +        List<HidEvent> iHidEvents;
    1.44 +
    1.45 +
    1.46 +        public bool IsRegistered { get; private set; }
    1.47 +
    1.48 +        public HidHandler(RAWINPUTDEVICE[] aRawInputDevices)
    1.49 +        {
    1.50 +            iHidEvents=new List<HidEvent>();
    1.51 +            IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0]));
    1.52 +        }
    1.53 +
    1.54 +        /// <summary>
    1.55 +        /// Process a WM_INPUT message.
    1.56 +        /// </summary>
    1.57 +        /// <param name="aMessage"></param>
    1.58 +        public void ProcessInput(ref Message aMessage)
    1.59 +        {
    1.60 +            if (aMessage.Msg != Const.WM_INPUT)
    1.61 +            {
    1.62 +                //We only process WM_INPUT messages
    1.63 +                return;
    1.64 +            }
    1.65 +
    1.66 +            HidEvent hidEvent = new HidEvent(aMessage, OnHidEventRepeat);
    1.67 +            hidEvent.DebugWrite();
    1.68 +
    1.69 +            if (!hidEvent.IsValid || !hidEvent.IsGeneric)
    1.70 +            {
    1.71 +                Debug.WriteLine("Skipping HID message.");
    1.72 +                return;
    1.73 +            }
    1.74 +
    1.75 +            //
    1.76 +            if (hidEvent.IsButtonUp)
    1.77 +            {
    1.78 +                //This is a key up event
    1.79 +                //We need to discard any events belonging to the same page and collection
    1.80 +                for (int i = (iHidEvents.Count-1); i >= 0; i--)
    1.81 +                {
    1.82 +                    if (iHidEvents[i].UsageId == hidEvent.UsageId)
    1.83 +                    {
    1.84 +                        iHidEvents[i].Dispose();
    1.85 +                        iHidEvents.RemoveAt(i);
    1.86 +                    }
    1.87 +                }
    1.88 +            }
    1.89 +            else
    1.90 +            {
    1.91 +                //Keep that event until we get a key up message
    1.92 +                iHidEvents.Add(hidEvent);
    1.93 +            }
    1.94 +
    1.95 +            //Broadcast our events
    1.96 +            OnHidEvent(this, hidEvent);    
    1.97 +        }
    1.98 +
    1.99 +        public void OnHidEventRepeat(HidEvent aHidEvent)
   1.100 +        {
   1.101 +            //Broadcast our events
   1.102 +            OnHidEvent(this, aHidEvent);    
   1.103 +        }
   1.104 +
   1.105 +    }
   1.106 +
   1.107 +}
   1.108 \ No newline at end of file