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