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