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@83: public class Handler : IDisposable sl@29: { StephaneLenclud@81: public delegate void HidEventHandler(object aSender, Event aHidEvent); sl@29: public event HidEventHandler OnHidEvent; StephaneLenclud@81: List iHidEvents; StephaneLenclud@83: RAWINPUTDEVICE[] iRawInputDevices; sl@41: sl@29: sl@29: public bool IsRegistered { get; private set; } StephaneLenclud@83: public bool ManageRepeats { get; private set; } sl@29: StephaneLenclud@83: public Handler(RAWINPUTDEVICE[] aRawInputDevices, bool aManageRepeats=false) sl@29: { StephaneLenclud@83: iRawInputDevices = aRawInputDevices; StephaneLenclud@83: iHidEvents = new List(); StephaneLenclud@83: IsRegistered = Function.RegisterRawInputDevices(iRawInputDevices, (uint)iRawInputDevices.Length, (uint)Marshal.SizeOf(iRawInputDevices[0])); StephaneLenclud@83: ManageRepeats = aManageRepeats; StephaneLenclud@83: } StephaneLenclud@83: StephaneLenclud@83: /// StephaneLenclud@83: /// Will de-register devices. StephaneLenclud@83: /// StephaneLenclud@83: public void Dispose() StephaneLenclud@83: { StephaneLenclud@83: //Setup device removal StephaneLenclud@83: for (int i=0; i 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@83: Event hidEvent = new Event(aMessage, OnHidEventRepeat, ManageRepeats); 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@83: if (ManageRepeats) sl@41: { StephaneLenclud@83: if (hidEvent.IsButtonUp) sl@41: { StephaneLenclud@83: //This is a key up event StephaneLenclud@83: //We need to discard any events belonging to the same page and collection StephaneLenclud@83: for (int i = (iHidEvents.Count - 1); i >= 0; i--) sl@41: { StephaneLenclud@83: if (iHidEvents[i].UsageId == hidEvent.UsageId) StephaneLenclud@83: { StephaneLenclud@83: iHidEvents[i].Dispose(); StephaneLenclud@83: iHidEvents.RemoveAt(i); StephaneLenclud@83: } sl@41: } sl@41: } StephaneLenclud@83: else StephaneLenclud@83: { StephaneLenclud@83: //Keep that event until we get a key up message StephaneLenclud@83: iHidEvents.Add(hidEvent); StephaneLenclud@83: } 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: }