# HG changeset patch # User StephaneLenclud # Date 1426619520 -3600 # Node ID 66a68098a4d13c44a3b6d35c800fa5bea4e4a179 # Parent b3cc1093c22b7b998d59ae3666144e963191b459 Adding missing source file. Updating SharpLibHid. Now Green Start works better when app in background. diff -r b3cc1093c22b -r 66a68098a4d1 Server/MainFormHid.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Server/MainFormHid.cs Tue Mar 17 20:12:00 2015 +0100 @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Windows.Forms; +// +using Hid = SharpLib.Hid; +using SharpLib.Win32; + +namespace SharpDisplayManager +{ + [System.ComponentModel.DesignerCategory("Code")] + public partial class MainForm: Form + { + public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent); + + /// + /// Use notably to handle green start key from IR remote control + /// + private Hid.Handler iHidHandler; + + void RegisterHidDevices() + { + // Register the input device to receive the commands from the + // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp + // for the vendor defined usage page. + + RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5]; + + int i = 0; + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl; + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl; + rid[i].dwFlags = Const.RIDEV_INPUTSINK; + rid[i].hwndTarget = Handle; + + i++; + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer; + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl; + rid[i].dwFlags = Const.RIDEV_INPUTSINK; + rid[i].hwndTarget = Handle; + + i++; + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer; + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection; + rid[i].dwFlags = Const.RIDEV_INPUTSINK; + rid[i].hwndTarget = Handle; + + i++; + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl; + rid[i].dwFlags = Const.RIDEV_INPUTSINK; + rid[i].hwndTarget = Handle; + + i++; + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad; + rid[i].dwFlags = Const.RIDEV_INPUTSINK; + rid[i].hwndTarget = Handle; + + //i++; + //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls; + //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard; + //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; + //rid[i].hwndTarget = Handle; + + //i++; + //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls; + //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse; + //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK; + //rid[i].hwndTarget = aHWND; + + + iHidHandler = new SharpLib.Hid.Handler(rid); + if (!iHidHandler.IsRegistered) + { + Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); + } + iHidHandler.OnHidEvent += HandleHidEventThreadSafe; + } + + public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent) + { + if (aHidEvent.IsStray) + { + //Stray event just ignore it + return; + } + + if (this.InvokeRequired) + { + //Not in the proper thread, invoke ourselves + OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe); + this.Invoke(d, new object[] { aSender, aHidEvent }); + } + else + { + //We are in the proper thread + //listViewEvents.Items.Insert(0, aHidEvent.ToListViewItem()); + //toolStripStatusLabelDevice.Text = aHidEvent.Device.FriendlyName; + + if (aHidEvent.Usages.Count > 0 + && aHidEvent.UsagePage == (ushort)Hid.UsagePage.WindowsMediaCenterRemoteControl + && aHidEvent.Usages[0] == (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart) + { + //Hard coding it all for now + // Prepare the process to run + ProcessStartInfo start = new ProcessStartInfo(); + // Enter in the command line arguments, everything you would enter after the executable name itself + //start.Arguments = arguments; + // Enter the executable to run, including the complete path + start.FileName = "C:\\Program Files (x86)\\Team MediaPortal\\MediaPortal\\MediaPortal.exe"; + // Do you want to show a console window? + start.WindowStyle = ProcessWindowStyle.Hidden; + start.CreateNoWindow = true; + // Run the external process & wait for it to finish + Process proc = Process.Start(start); + } + } + } + + protected override void WndProc(ref Message message) + { + switch (message.Msg) + { + case Const.WM_INPUT: + //Returning zero means we processed that message. + message.Result = new IntPtr(0); + iHidHandler.ProcessInput(ref message); + break; + } + //Is that needed? Check the docs. + base.WndProc(ref message); + } + + + } +} diff -r b3cc1093c22b -r 66a68098a4d1 Server/SharpDisplayManager.csproj --- a/Server/SharpDisplayManager.csproj Sun Mar 15 22:08:30 2015 +0100 +++ b/Server/SharpDisplayManager.csproj Tue Mar 17 20:12:00 2015 +0100 @@ -32,7 +32,7 @@ index.htm false 1 - 0.3.0.%2a + 0.3.1.%2a false true true @@ -109,8 +109,9 @@ ..\packages\NAudio.1.7.2\lib\net35\NAudio.dll - - ..\packages\SharpLibHid.1.0.0\lib\net20\SharpLibHid.dll + + False + ..\packages\SharpLibHid.1.0.1\lib\net20\SharpLibHid.dll @@ -135,9 +136,7 @@ - - Form - + MainForm.cs diff -r b3cc1093c22b -r 66a68098a4d1 Server/packages.config --- a/Server/packages.config Sun Mar 15 22:08:30 2015 +0100 +++ b/Server/packages.config Tue Mar 17 20:12:00 2015 +0100 @@ -1,5 +1,5 @@  - + \ No newline at end of file diff -r b3cc1093c22b -r 66a68098a4d1 SharpDisplayManager.sln --- a/SharpDisplayManager.sln Sun Mar 15 22:08:30 2015 +0100 +++ b/SharpDisplayManager.sln Tue Mar 17 20:12:00 2015 +0100 @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +# Visual Studio 2013 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpDisplayManager", "Server\SharpDisplayManager.csproj", "{1DA8C1B3-18C5-4E74-BE4E-0B0E15FBAF49}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpDisplayClient", "Client\SharpDisplayClient.csproj", "{7EE64074-8CDB-4448-B40C-81B75D6B31CD}"