1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Server/MainFormHid.cs Tue Mar 17 20:12:00 2015 +0100
1.3 @@ -0,0 +1,140 @@
1.4 +using System;
1.5 +using System.Collections.Generic;
1.6 +using System.Linq;
1.7 +using System.Text;
1.8 +using System.Threading.Tasks;
1.9 +using System.Diagnostics;
1.10 +using System.Runtime.InteropServices;
1.11 +using System.Windows.Forms;
1.12 +//
1.13 +using Hid = SharpLib.Hid;
1.14 +using SharpLib.Win32;
1.15 +
1.16 +namespace SharpDisplayManager
1.17 +{
1.18 + [System.ComponentModel.DesignerCategory("Code")]
1.19 + public partial class MainForm: Form
1.20 + {
1.21 + public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent);
1.22 +
1.23 + /// <summary>
1.24 + /// Use notably to handle green start key from IR remote control
1.25 + /// </summary>
1.26 + private Hid.Handler iHidHandler;
1.27 +
1.28 + void RegisterHidDevices()
1.29 + {
1.30 + // Register the input device to receive the commands from the
1.31 + // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
1.32 + // for the vendor defined usage page.
1.33 +
1.34 + RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5];
1.35 +
1.36 + int i = 0;
1.37 + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl;
1.38 + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl;
1.39 + rid[i].dwFlags = Const.RIDEV_INPUTSINK;
1.40 + rid[i].hwndTarget = Handle;
1.41 +
1.42 + i++;
1.43 + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
1.44 + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl;
1.45 + rid[i].dwFlags = Const.RIDEV_INPUTSINK;
1.46 + rid[i].hwndTarget = Handle;
1.47 +
1.48 + i++;
1.49 + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
1.50 + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection;
1.51 + rid[i].dwFlags = Const.RIDEV_INPUTSINK;
1.52 + rid[i].hwndTarget = Handle;
1.53 +
1.54 + i++;
1.55 + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
1.56 + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl;
1.57 + rid[i].dwFlags = Const.RIDEV_INPUTSINK;
1.58 + rid[i].hwndTarget = Handle;
1.59 +
1.60 + i++;
1.61 + rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
1.62 + rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad;
1.63 + rid[i].dwFlags = Const.RIDEV_INPUTSINK;
1.64 + rid[i].hwndTarget = Handle;
1.65 +
1.66 + //i++;
1.67 + //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
1.68 + //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
1.69 + //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
1.70 + //rid[i].hwndTarget = Handle;
1.71 +
1.72 + //i++;
1.73 + //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
1.74 + //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse;
1.75 + //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
1.76 + //rid[i].hwndTarget = aHWND;
1.77 +
1.78 +
1.79 + iHidHandler = new SharpLib.Hid.Handler(rid);
1.80 + if (!iHidHandler.IsRegistered)
1.81 + {
1.82 + Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
1.83 + }
1.84 + iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
1.85 + }
1.86 +
1.87 + public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
1.88 + {
1.89 + if (aHidEvent.IsStray)
1.90 + {
1.91 + //Stray event just ignore it
1.92 + return;
1.93 + }
1.94 +
1.95 + if (this.InvokeRequired)
1.96 + {
1.97 + //Not in the proper thread, invoke ourselves
1.98 + OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
1.99 + this.Invoke(d, new object[] { aSender, aHidEvent });
1.100 + }
1.101 + else
1.102 + {
1.103 + //We are in the proper thread
1.104 + //listViewEvents.Items.Insert(0, aHidEvent.ToListViewItem());
1.105 + //toolStripStatusLabelDevice.Text = aHidEvent.Device.FriendlyName;
1.106 +
1.107 + if (aHidEvent.Usages.Count > 0
1.108 + && aHidEvent.UsagePage == (ushort)Hid.UsagePage.WindowsMediaCenterRemoteControl
1.109 + && aHidEvent.Usages[0] == (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart)
1.110 + {
1.111 + //Hard coding it all for now
1.112 + // Prepare the process to run
1.113 + ProcessStartInfo start = new ProcessStartInfo();
1.114 + // Enter in the command line arguments, everything you would enter after the executable name itself
1.115 + //start.Arguments = arguments;
1.116 + // Enter the executable to run, including the complete path
1.117 + start.FileName = "C:\\Program Files (x86)\\Team MediaPortal\\MediaPortal\\MediaPortal.exe";
1.118 + // Do you want to show a console window?
1.119 + start.WindowStyle = ProcessWindowStyle.Hidden;
1.120 + start.CreateNoWindow = true;
1.121 + // Run the external process & wait for it to finish
1.122 + Process proc = Process.Start(start);
1.123 + }
1.124 + }
1.125 + }
1.126 +
1.127 + protected override void WndProc(ref Message message)
1.128 + {
1.129 + switch (message.Msg)
1.130 + {
1.131 + case Const.WM_INPUT:
1.132 + //Returning zero means we processed that message.
1.133 + message.Result = new IntPtr(0);
1.134 + iHidHandler.ProcessInput(ref message);
1.135 + break;
1.136 + }
1.137 + //Is that needed? Check the docs.
1.138 + base.WndProc(ref message);
1.139 + }
1.140 +
1.141 +
1.142 + }
1.143 +}