Switch from NAudio to CSCore.
3 using System.Collections.Generic;
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;
13 using Hid = SharpLib.Hid;
16 namespace SharpDisplayManager
19 /// Implement handling of HID input reports notably to be able to launch an application using the Green Start button from IR remotes.
21 [System.ComponentModel.DesignerCategory("Code")]
22 public class FormMainHid : Form
25 public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent);
29 /// Register HID devices so that we receive corresponding WM_INPUT messages.
31 protected void RegisterHidDevices()
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.
37 RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[6];
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;
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;
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;
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;
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;
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;
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;
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;
88 Program.HidHandler = new SharpLib.Hid.Handler(rid);
89 if (!Program.HidHandler.IsRegistered)
91 Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
93 Program.HidHandler.OnHidEvent += HandleHidEventThreadSafe;
99 /// Here we receive HID events from our HID library.
101 /// <param name="aSender"></param>
102 /// <param name="aHidEvent"></param>
103 public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
105 if (aHidEvent.IsStray || !aHidEvent.IsValid)
107 //Stray event just ignore it
111 if (this.InvokeRequired)
113 //Not in the proper thread, invoke ourselves
114 OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
115 this.Invoke(d, new object[] { aSender, aHidEvent });
119 //Trigger corresponding EAR event if any
120 EventHid e = new EventHid();
122 Properties.Settings.Default.EarManager.TriggerEvents(e);
127 /// We need to handle WM_INPUT.
129 /// <param name="message"></param>
130 protected override void WndProc(ref Message message)
135 //Returning zero means we processed that message.
136 message.Result = new IntPtr(0);
137 Program.HidHandler.ProcessInput(ref message);
141 //Pass this on to base class.
142 base.WndProc(ref message);