Server/FormMain.Hid.cs
author StephaneLenclud
Fri, 06 Jan 2017 18:27:19 +0100
changeset 276 7cd495550d5f
parent 249 cadc4e4302c6
permissions -rw-r--r--
Published v1.4.3
Major hack into our spectrum math to make it prettier.
     1 using System;
     2 using System.IO;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 using System.Diagnostics;
     8 using System.Runtime.InteropServices;
     9 using System.Windows.Forms;
    10 using Microsoft.Win32.SafeHandles;
    11 using System.ComponentModel;
    12 //
    13 using Hid = SharpLib.Hid;
    14 using SharpLib.Win32;
    15 
    16 namespace SharpDisplayManager
    17 {
    18     /// <summary>
    19     /// Implement handling of HID input reports notably to be able to launch an application using the Green Start button from IR remotes.
    20     /// </summary>
    21     [System.ComponentModel.DesignerCategory("Code")]
    22     public class FormMainHid : Form
    23     {
    24         //
    25         public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent);
    26 
    27 
    28         /// <summary>
    29         /// Register HID devices so that we receive corresponding WM_INPUT messages.
    30         /// </summary>
    31         protected void RegisterHidDevices()
    32         {
    33             // Register the input device to receive the commands from the
    34             // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
    35             // for the vendor defined usage page.
    36 
    37             RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[6];
    38 
    39             int i = 0;
    40             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl;
    41             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl;
    42             rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
    43             rid[i].hwndTarget = Handle;
    44 
    45             i++;
    46             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
    47             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl;
    48             rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
    49             rid[i].hwndTarget = Handle;
    50 
    51             i++;
    52             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
    53             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection;
    54             rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
    55             rid[i].hwndTarget = Handle;
    56 
    57             i++;
    58             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
    59             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl;
    60             rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
    61             rid[i].hwndTarget = Handle;
    62 
    63             i++;
    64             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
    65             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad;
    66             rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
    67             rid[i].hwndTarget = Handle;
    68 
    69             i++;
    70             rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
    71             rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
    72             rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
    73             rid[i].hwndTarget = Handle;
    74 
    75             //i++;
    76             //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
    77             //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
    78             //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    79             //rid[i].hwndTarget = Handle;
    80 
    81             //i++;
    82             //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
    83             //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse;
    84             //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
    85             //rid[i].hwndTarget = aHWND;
    86 
    87 
    88             Program.HidHandler = new SharpLib.Hid.Handler(rid);
    89             if (!Program.HidHandler.IsRegistered)
    90             {
    91                 Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
    92             }
    93             Program.HidHandler.OnHidEvent += HandleHidEventThreadSafe;
    94 
    95         }
    96 
    97 
    98         /// <summary>
    99         /// Here we receive HID events from our HID library.
   100         /// </summary>
   101         /// <param name="aSender"></param>
   102         /// <param name="aHidEvent"></param>
   103         public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
   104         {
   105             if (aHidEvent.IsStray || !aHidEvent.IsValid)
   106             {
   107                 //Stray event just ignore it
   108                 return;
   109             }
   110 
   111             if (this.InvokeRequired)
   112             {
   113                 //Not in the proper thread, invoke ourselves
   114                 OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
   115                 this.Invoke(d, new object[] { aSender, aHidEvent });
   116             }
   117             else
   118             {
   119                 //Trigger corresponding EAR event if any
   120                 EventHid e = new EventHid();
   121                 e.Copy(aHidEvent);
   122                 Properties.Settings.Default.EarManager.TriggerEvents(e);
   123             }
   124         }
   125 
   126         /// <summary>
   127         /// We need to handle WM_INPUT.
   128         /// </summary>
   129         /// <param name="message"></param>
   130         protected override void WndProc(ref Message message)
   131         {
   132             switch (message.Msg)
   133             {
   134                 case Const.WM_INPUT:
   135                     //Returning zero means we processed that message.
   136                     message.Result = new IntPtr(0);
   137                     Program.HidHandler.ProcessInput(ref message);
   138                     break;
   139             }
   140 
   141             //Pass this on to base class.
   142             base.WndProc(ref message);
   143         }
   144     }
   145 }