StephaneLenclud@76: //
StephaneLenclud@76: // Copyright (C) 2014-2015 Stéphane Lenclud.
StephaneLenclud@76: //
StephaneLenclud@76: // This file is part of SharpLibHid.
StephaneLenclud@76: //
StephaneLenclud@76: // SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@76: // it under the terms of the GNU General Public License as published by
StephaneLenclud@76: // the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@76: // (at your option) any later version.
StephaneLenclud@76: //
StephaneLenclud@76: // SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@76: // but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@76: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
StephaneLenclud@76: // GNU General Public License for more details.
StephaneLenclud@76: //
StephaneLenclud@76: // You should have received a copy of the GNU General Public License
StephaneLenclud@76: // along with SharpDisplayManager. If not, see .
StephaneLenclud@76: //
StephaneLenclud@76:
StephaneLenclud@76:
sl@29: using System;
sl@29: using System.Windows.Forms;
sl@29: using System.Runtime.InteropServices;
sl@29: using System.Diagnostics;
sl@29: using System.Text;
sl@29: using Microsoft.Win32.SafeHandles;
StephaneLenclud@77: using SharpLib.Win32;
sl@29: using System.Collections.Generic;
sl@29:
sl@29:
StephaneLenclud@77: namespace SharpLib.Hid
sl@29: {
sl@29: ///
sl@29: /// Our HID handler manages raw input registrations, processes WM_INPUT messages and broadcasts HID events in return.
sl@29: ///
StephaneLenclud@81: public class Handler
sl@29: {
StephaneLenclud@81: public delegate void HidEventHandler(object aSender, Event aHidEvent);
sl@29: public event HidEventHandler OnHidEvent;
StephaneLenclud@81: List iHidEvents;
sl@41:
sl@29:
sl@29: public bool IsRegistered { get; private set; }
sl@29:
StephaneLenclud@81: public Handler(RAWINPUTDEVICE[] aRawInputDevices)
sl@29: {
StephaneLenclud@81: iHidEvents=new List();
sl@29: IsRegistered = Function.RegisterRawInputDevices(aRawInputDevices, (uint)aRawInputDevices.Length, (uint)Marshal.SizeOf(aRawInputDevices[0]));
sl@29: }
sl@29:
StephaneLenclud@78: ///
StephaneLenclud@78: /// Process a WM_INPUT message.
StephaneLenclud@78: ///
StephaneLenclud@78: ///
StephaneLenclud@78: public void ProcessInput(ref Message aMessage)
sl@29: {
StephaneLenclud@78: if (aMessage.Msg != Const.WM_INPUT)
StephaneLenclud@78: {
StephaneLenclud@78: //We only process WM_INPUT messages
StephaneLenclud@78: return;
StephaneLenclud@78: }
StephaneLenclud@78:
StephaneLenclud@81: Event hidEvent = new Event(aMessage, OnHidEventRepeat);
sl@29: hidEvent.DebugWrite();
sl@29:
sl@29: if (!hidEvent.IsValid || !hidEvent.IsGeneric)
sl@29: {
sl@29: Debug.WriteLine("Skipping HID message.");
sl@29: return;
sl@29: }
sl@29:
sl@41: //
StephaneLenclud@49: if (hidEvent.IsButtonUp)
sl@41: {
sl@41: //This is a key up event
sl@41: //We need to discard any events belonging to the same page and collection
sl@41: for (int i = (iHidEvents.Count-1); i >= 0; i--)
sl@41: {
sl@41: if (iHidEvents[i].UsageId == hidEvent.UsageId)
sl@41: {
sl@41: iHidEvents[i].Dispose();
sl@41: iHidEvents.RemoveAt(i);
sl@41: }
sl@41: }
sl@41: }
sl@41: else
sl@41: {
sl@41: //Keep that event until we get a key up message
sl@41: iHidEvents.Add(hidEvent);
sl@41: }
sl@41:
sl@29: //Broadcast our events
sl@29: OnHidEvent(this, hidEvent);
sl@29: }
sl@29:
StephaneLenclud@81: public void OnHidEventRepeat(Event aHidEvent)
sl@41: {
sl@41: //Broadcast our events
sl@41: OnHidEvent(this, aHidEvent);
sl@41: }
sl@41:
sl@29: }
sl@29:
sl@29: }