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