Server/MainForm.Hid.cs
author StephaneLenclud
Tue, 01 Sep 2015 20:57:06 +0200
changeset 150 728fe28168e2
parent 138 426cc984fd18
child 151 7f62f2c35288
permissions -rw-r--r--
Optical Drive Eject: ground work.
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@125
    11
//
StephaneLenclud@125
    12
using Hid = SharpLib.Hid;
StephaneLenclud@125
    13
using SharpLib.Win32;
StephaneLenclud@125
    14
StephaneLenclud@125
    15
namespace SharpDisplayManager
StephaneLenclud@125
    16
{
StephaneLenclud@138
    17
    /// <summary>
StephaneLenclud@138
    18
    /// Implement handling of HID input reports notably to be able to launch an application using the Green Start button from IR remotes.
StephaneLenclud@138
    19
    /// </summary>
StephaneLenclud@131
    20
    [System.ComponentModel.DesignerCategory("Code")]
StephaneLenclud@131
    21
    public class MainFormHid : Form
StephaneLenclud@131
    22
    {
StephaneLenclud@131
    23
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SwitchToThisWindow")]
StephaneLenclud@131
    24
        public static extern void SwitchToThisWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hwnd, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fUnknown);
StephaneLenclud@131
    25
        //
StephaneLenclud@131
    26
        public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent);
StephaneLenclud@126
    27
StephaneLenclud@131
    28
        /// <summary>
StephaneLenclud@131
    29
        /// Use notably to handle green start key from IR remote control
StephaneLenclud@131
    30
        /// </summary>
StephaneLenclud@131
    31
        private Hid.Handler iHidHandler;
StephaneLenclud@126
    32
StephaneLenclud@131
    33
        /// <summary>
StephaneLenclud@131
    34
        /// Register HID devices so that we receive corresponding WM_INPUT messages.
StephaneLenclud@131
    35
        /// </summary>
StephaneLenclud@131
    36
        protected void RegisterHidDevices()
StephaneLenclud@131
    37
        {
StephaneLenclud@131
    38
            // Register the input device to receive the commands from the
StephaneLenclud@131
    39
            // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
StephaneLenclud@131
    40
            // for the vendor defined usage page.
StephaneLenclud@128
    41
StephaneLenclud@131
    42
            RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5];
StephaneLenclud@128
    43
StephaneLenclud@131
    44
            int i = 0;
StephaneLenclud@131
    45
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl;
StephaneLenclud@131
    46
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl;
StephaneLenclud@131
    47
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    48
            rid[i].hwndTarget = Handle;
StephaneLenclud@128
    49
StephaneLenclud@131
    50
            i++;
StephaneLenclud@131
    51
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
StephaneLenclud@131
    52
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl;
StephaneLenclud@131
    53
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    54
            rid[i].hwndTarget = Handle;
StephaneLenclud@126
    55
StephaneLenclud@131
    56
            i++;
StephaneLenclud@131
    57
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
StephaneLenclud@131
    58
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection;
StephaneLenclud@131
    59
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    60
            rid[i].hwndTarget = Handle;
StephaneLenclud@125
    61
StephaneLenclud@131
    62
            i++;
StephaneLenclud@131
    63
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    64
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl;
StephaneLenclud@131
    65
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    66
            rid[i].hwndTarget = Handle;
StephaneLenclud@125
    67
StephaneLenclud@131
    68
            i++;
StephaneLenclud@131
    69
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    70
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad;
StephaneLenclud@131
    71
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    72
            rid[i].hwndTarget = Handle;
StephaneLenclud@125
    73
StephaneLenclud@131
    74
            //i++;
StephaneLenclud@131
    75
            //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    76
            //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
StephaneLenclud@131
    77
            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
StephaneLenclud@131
    78
            //rid[i].hwndTarget = Handle;
StephaneLenclud@125
    79
StephaneLenclud@131
    80
            //i++;
StephaneLenclud@131
    81
            //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    82
            //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse;
StephaneLenclud@131
    83
            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
StephaneLenclud@131
    84
            //rid[i].hwndTarget = aHWND;
StephaneLenclud@125
    85
StephaneLenclud@125
    86
StephaneLenclud@131
    87
            iHidHandler = new SharpLib.Hid.Handler(rid);
StephaneLenclud@131
    88
            if (!iHidHandler.IsRegistered)
StephaneLenclud@131
    89
            {
StephaneLenclud@131
    90
                Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@131
    91
            }
StephaneLenclud@131
    92
            iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
StephaneLenclud@131
    93
        }
StephaneLenclud@125
    94
StephaneLenclud@131
    95
        /// <summary>
StephaneLenclud@131
    96
        /// Here we receive HID events from our HID library.
StephaneLenclud@131
    97
        /// </summary>
StephaneLenclud@131
    98
        /// <param name="aSender"></param>
StephaneLenclud@131
    99
        /// <param name="aHidEvent"></param>
StephaneLenclud@131
   100
        public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
StephaneLenclud@131
   101
        {
StephaneLenclud@131
   102
            if (aHidEvent.IsStray)
StephaneLenclud@131
   103
            {
StephaneLenclud@131
   104
                //Stray event just ignore it
StephaneLenclud@131
   105
                return;
StephaneLenclud@131
   106
            }
StephaneLenclud@125
   107
StephaneLenclud@131
   108
            if (this.InvokeRequired)
StephaneLenclud@131
   109
            {
StephaneLenclud@131
   110
                //Not in the proper thread, invoke ourselves
StephaneLenclud@131
   111
                OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
StephaneLenclud@131
   112
                this.Invoke(d, new object[] { aSender, aHidEvent });
StephaneLenclud@131
   113
            }
StephaneLenclud@131
   114
            else
StephaneLenclud@131
   115
            {
StephaneLenclud@150
   116
                if (aHidEvent.Usages.Count == 0)
StephaneLenclud@150
   117
                {
StephaneLenclud@150
   118
                    //No usage, nothing to do then
StephaneLenclud@150
   119
                    return;
StephaneLenclud@150
   120
                }
StephaneLenclud@150
   121
StephaneLenclud@131
   122
                //We are in the proper thread
StephaneLenclud@150
   123
                if (aHidEvent.UsagePage == (ushort) Hid.UsagePage.WindowsMediaCenterRemoteControl)
StephaneLenclud@131
   124
                {
StephaneLenclud@150
   125
                    switch (aHidEvent.Usages[0])
StephaneLenclud@131
   126
                    {
StephaneLenclud@150
   127
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart:
StephaneLenclud@150
   128
                            HandleGreenStart();
StephaneLenclud@150
   129
                            break;
StephaneLenclud@150
   130
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Eject:
StephaneLenclud@150
   131
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Ext2:
StephaneLenclud@150
   132
                            HandleEject();
StephaneLenclud@150
   133
                            break;
StephaneLenclud@131
   134
                    }
StephaneLenclud@131
   135
                }
StephaneLenclud@131
   136
            }
StephaneLenclud@131
   137
        }
StephaneLenclud@125
   138
StephaneLenclud@150
   139
        private SafeFileHandle OpenVolume()
StephaneLenclud@150
   140
        {
StephaneLenclud@150
   141
            return Function.CreateFile(  "E:",
StephaneLenclud@150
   142
                               SharpLib.Win32.FileAccess.GENERIC_READ,
StephaneLenclud@150
   143
                               SharpLib.Win32.FileShare.FILE_SHARE_READ | SharpLib.Win32.FileShare.FILE_SHARE_WRITE,
StephaneLenclud@150
   144
                               IntPtr.Zero,
StephaneLenclud@150
   145
                               CreationDisposition.OPEN_EXISTING,
StephaneLenclud@150
   146
                               0,
StephaneLenclud@150
   147
                               IntPtr.Zero);
StephaneLenclud@150
   148
        }
StephaneLenclud@150
   149
StephaneLenclud@150
   150
        /// <summary>
StephaneLenclud@150
   151
        /// 
StephaneLenclud@150
   152
        /// </summary>
StephaneLenclud@150
   153
        private void HandleEject()
StephaneLenclud@150
   154
        {
StephaneLenclud@150
   155
            SafeFileHandle handle = OpenVolume();
StephaneLenclud@150
   156
        }
StephaneLenclud@150
   157
StephaneLenclud@150
   158
        /// <summary>
StephaneLenclud@150
   159
        /// 
StephaneLenclud@150
   160
        /// </summary>
StephaneLenclud@150
   161
        private void HandleGreenStart()
StephaneLenclud@150
   162
        {
StephaneLenclud@150
   163
            //First check if the process we want to launch already exists
StephaneLenclud@150
   164
            string procName = Path.GetFileNameWithoutExtension(Properties.Settings.Default.StartFileName);
StephaneLenclud@150
   165
            Process[] existingProcesses = Process.GetProcessesByName(procName);
StephaneLenclud@150
   166
            if (existingProcesses == null || existingProcesses.Length == 0)
StephaneLenclud@150
   167
            {
StephaneLenclud@150
   168
                // Process do not exists just try to launch it
StephaneLenclud@150
   169
                ProcessStartInfo start = new ProcessStartInfo();
StephaneLenclud@150
   170
                // Enter in the command line arguments, everything you would enter after the executable name itself
StephaneLenclud@150
   171
                //start.Arguments = arguments; 
StephaneLenclud@150
   172
                // Enter the executable to run, including the complete path
StephaneLenclud@150
   173
                start.FileName = Properties.Settings.Default.StartFileName;
StephaneLenclud@150
   174
                start.WindowStyle = ProcessWindowStyle.Normal;
StephaneLenclud@150
   175
                start.CreateNoWindow = true;
StephaneLenclud@150
   176
                start.UseShellExecute = true;
StephaneLenclud@150
   177
                // Run the external process & wait for it to finish
StephaneLenclud@150
   178
                Process proc = Process.Start(start);
StephaneLenclud@150
   179
StephaneLenclud@150
   180
                //SL: We could have used that too
StephaneLenclud@150
   181
                //Shell32.Shell shell = new Shell32.Shell();
StephaneLenclud@150
   182
                //shell.ShellExecute(Properties.Settings.Default.StartFileName);
StephaneLenclud@150
   183
            }
StephaneLenclud@150
   184
            else
StephaneLenclud@150
   185
            {
StephaneLenclud@150
   186
                //This won't work properly until we have a manifest that enables uiAccess.
StephaneLenclud@150
   187
                //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
StephaneLenclud@150
   188
                SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);
StephaneLenclud@150
   189
            }            
StephaneLenclud@150
   190
        }
StephaneLenclud@131
   191
        /// <summary>
StephaneLenclud@131
   192
        /// We need to handle WM_INPUT.
StephaneLenclud@131
   193
        /// </summary>
StephaneLenclud@131
   194
        /// <param name="message"></param>
StephaneLenclud@131
   195
        protected override void WndProc(ref Message message)
StephaneLenclud@131
   196
        {
StephaneLenclud@131
   197
            switch (message.Msg)
StephaneLenclud@131
   198
            {
StephaneLenclud@131
   199
                case Const.WM_INPUT:
StephaneLenclud@131
   200
                    //Returning zero means we processed that message.
StephaneLenclud@131
   201
                    message.Result = new IntPtr(0);
StephaneLenclud@131
   202
                    iHidHandler.ProcessInput(ref message);
StephaneLenclud@131
   203
                    break;
StephaneLenclud@131
   204
            }
StephaneLenclud@131
   205
            //Is that needed? Check the docs.
StephaneLenclud@131
   206
            base.WndProc(ref message);
StephaneLenclud@131
   207
        }
StephaneLenclud@131
   208
    }
StephaneLenclud@125
   209
}