Server/FormMain.Hid.cs
author StephaneLenclud
Wed, 17 Aug 2016 16:39:36 +0200
changeset 237 1a1c2ae3a29c
parent 233 2b9541e54f7d
child 238 c92587ddabcd
permissions -rw-r--r--
Adding HID consumer control event.
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@155
    11
using System.ComponentModel;
StephaneLenclud@125
    12
//
StephaneLenclud@125
    13
using Hid = SharpLib.Hid;
StephaneLenclud@125
    14
using SharpLib.Win32;
StephaneLenclud@125
    15
StephaneLenclud@125
    16
namespace SharpDisplayManager
StephaneLenclud@125
    17
{
StephaneLenclud@138
    18
    /// <summary>
StephaneLenclud@138
    19
    /// Implement handling of HID input reports notably to be able to launch an application using the Green Start button from IR remotes.
StephaneLenclud@138
    20
    /// </summary>
StephaneLenclud@131
    21
    [System.ComponentModel.DesignerCategory("Code")]
StephaneLenclud@226
    22
    public class FormMainHid : Form
StephaneLenclud@131
    23
    {
StephaneLenclud@131
    24
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SwitchToThisWindow")]
StephaneLenclud@131
    25
        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
    26
        //
StephaneLenclud@131
    27
        public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent);
StephaneLenclud@126
    28
StephaneLenclud@131
    29
        /// <summary>
StephaneLenclud@131
    30
        /// Use notably to handle green start key from IR remote control
StephaneLenclud@131
    31
        /// </summary>
StephaneLenclud@131
    32
        private Hid.Handler iHidHandler;
StephaneLenclud@126
    33
StephaneLenclud@131
    34
        /// <summary>
StephaneLenclud@131
    35
        /// Register HID devices so that we receive corresponding WM_INPUT messages.
StephaneLenclud@131
    36
        /// </summary>
StephaneLenclud@131
    37
        protected void RegisterHidDevices()
StephaneLenclud@131
    38
        {
StephaneLenclud@131
    39
            // Register the input device to receive the commands from the
StephaneLenclud@131
    40
            // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
StephaneLenclud@131
    41
            // for the vendor defined usage page.
StephaneLenclud@128
    42
StephaneLenclud@131
    43
            RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5];
StephaneLenclud@128
    44
StephaneLenclud@131
    45
            int i = 0;
StephaneLenclud@131
    46
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl;
StephaneLenclud@131
    47
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl;
StephaneLenclud@233
    48
            rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
StephaneLenclud@131
    49
            rid[i].hwndTarget = Handle;
StephaneLenclud@128
    50
StephaneLenclud@131
    51
            i++;
StephaneLenclud@131
    52
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
StephaneLenclud@131
    53
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl;
StephaneLenclud@233
    54
            rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
StephaneLenclud@131
    55
            rid[i].hwndTarget = Handle;
StephaneLenclud@126
    56
StephaneLenclud@131
    57
            i++;
StephaneLenclud@131
    58
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
StephaneLenclud@131
    59
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection;
StephaneLenclud@233
    60
            rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
StephaneLenclud@131
    61
            rid[i].hwndTarget = Handle;
StephaneLenclud@125
    62
StephaneLenclud@131
    63
            i++;
StephaneLenclud@131
    64
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    65
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl;
StephaneLenclud@233
    66
            rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
StephaneLenclud@131
    67
            rid[i].hwndTarget = Handle;
StephaneLenclud@125
    68
StephaneLenclud@131
    69
            i++;
StephaneLenclud@131
    70
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    71
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad;
StephaneLenclud@233
    72
            rid[i].dwFlags = RawInputDeviceFlags.RIDEV_INPUTSINK;
StephaneLenclud@131
    73
            rid[i].hwndTarget = Handle;
StephaneLenclud@125
    74
StephaneLenclud@131
    75
            //i++;
StephaneLenclud@131
    76
            //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    77
            //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
StephaneLenclud@131
    78
            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
StephaneLenclud@131
    79
            //rid[i].hwndTarget = Handle;
StephaneLenclud@125
    80
StephaneLenclud@131
    81
            //i++;
StephaneLenclud@131
    82
            //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    83
            //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse;
StephaneLenclud@131
    84
            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
StephaneLenclud@131
    85
            //rid[i].hwndTarget = aHWND;
StephaneLenclud@125
    86
StephaneLenclud@125
    87
StephaneLenclud@131
    88
            iHidHandler = new SharpLib.Hid.Handler(rid);
StephaneLenclud@131
    89
            if (!iHidHandler.IsRegistered)
StephaneLenclud@131
    90
            {
StephaneLenclud@131
    91
                Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@131
    92
            }
StephaneLenclud@131
    93
            iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
StephaneLenclud@159
    94
StephaneLenclud@131
    95
        }
StephaneLenclud@125
    96
StephaneLenclud@159
    97
StephaneLenclud@159
    98
StephaneLenclud@159
    99
StephaneLenclud@131
   100
        /// <summary>
StephaneLenclud@131
   101
        /// Here we receive HID events from our HID library.
StephaneLenclud@131
   102
        /// </summary>
StephaneLenclud@131
   103
        /// <param name="aSender"></param>
StephaneLenclud@131
   104
        /// <param name="aHidEvent"></param>
StephaneLenclud@131
   105
        public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
StephaneLenclud@131
   106
        {
StephaneLenclud@131
   107
            if (aHidEvent.IsStray)
StephaneLenclud@131
   108
            {
StephaneLenclud@131
   109
                //Stray event just ignore it
StephaneLenclud@131
   110
                return;
StephaneLenclud@131
   111
            }
StephaneLenclud@125
   112
StephaneLenclud@131
   113
            if (this.InvokeRequired)
StephaneLenclud@131
   114
            {
StephaneLenclud@131
   115
                //Not in the proper thread, invoke ourselves
StephaneLenclud@131
   116
                OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
StephaneLenclud@131
   117
                this.Invoke(d, new object[] { aSender, aHidEvent });
StephaneLenclud@131
   118
            }
StephaneLenclud@131
   119
            else
StephaneLenclud@131
   120
            {
StephaneLenclud@150
   121
                if (aHidEvent.Usages.Count == 0)
StephaneLenclud@150
   122
                {
StephaneLenclud@150
   123
                    //No usage, nothing to do then
StephaneLenclud@150
   124
                    return;
StephaneLenclud@150
   125
                }
StephaneLenclud@150
   126
StephaneLenclud@131
   127
                //We are in the proper thread
StephaneLenclud@150
   128
                if (aHidEvent.UsagePage == (ushort) Hid.UsagePage.WindowsMediaCenterRemoteControl)
StephaneLenclud@131
   129
                {
StephaneLenclud@150
   130
                    switch (aHidEvent.Usages[0])
StephaneLenclud@131
   131
                    {
StephaneLenclud@150
   132
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart:
StephaneLenclud@150
   133
                            HandleGreenStart();
StephaneLenclud@150
   134
                            break;
StephaneLenclud@150
   135
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Eject:
StephaneLenclud@150
   136
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Ext2:
StephaneLenclud@150
   137
                            HandleEject();
StephaneLenclud@150
   138
                            break;
StephaneLenclud@131
   139
                    }
StephaneLenclud@131
   140
                }
StephaneLenclud@237
   141
                else if (aHidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer)
StephaneLenclud@237
   142
                {
StephaneLenclud@237
   143
                    //Keep this for debug when only ThinkPad keyboard is available
StephaneLenclud@237
   144
                    //if (aHidEvent.Usages[0] == (ushort)Hid.Usage.ConsumerControl.ThinkPadFullscreenMagnifier)
StephaneLenclud@237
   145
                    //{
StephaneLenclud@237
   146
                    //    HandleEject();
StephaneLenclud@237
   147
                    //}
StephaneLenclud@152
   148
StephaneLenclud@237
   149
                    EventHidConsumerControl e = new EventHidConsumerControl {Usage = (Hid.Usage.ConsumerControl)aHidEvent.Usages[0]};
StephaneLenclud@237
   150
                    Properties.Settings.Default.EarManager.TriggerEvent(e);
StephaneLenclud@237
   151
StephaneLenclud@237
   152
StephaneLenclud@152
   153
                }
StephaneLenclud@152
   154
StephaneLenclud@131
   155
            }
StephaneLenclud@131
   156
        }
StephaneLenclud@125
   157
StephaneLenclud@151
   158
        /// <summary>
StephaneLenclud@151
   159
        /// 
StephaneLenclud@151
   160
        /// </summary>
StephaneLenclud@155
   161
        /// <param name="aPrefix"></param>
StephaneLenclud@155
   162
        private void CheckLastError(string aPrefix)
StephaneLenclud@155
   163
        {
StephaneLenclud@155
   164
            string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
StephaneLenclud@155
   165
            Debug.WriteLine(aPrefix + Marshal.GetLastWin32Error().ToString() + ": " + errorMessage);
StephaneLenclud@155
   166
        }
StephaneLenclud@155
   167
StephaneLenclud@155
   168
        /// <summary>
StephaneLenclud@155
   169
        /// 
StephaneLenclud@155
   170
        /// </summary>
StephaneLenclud@151
   171
        /// <param name="data"></param>
StephaneLenclud@151
   172
        /// <returns></returns>
StephaneLenclud@151
   173
        private IntPtr MarshalToPointer(object data)
StephaneLenclud@151
   174
        {
StephaneLenclud@151
   175
            IntPtr buf = Marshal.AllocHGlobal(
StephaneLenclud@151
   176
                Marshal.SizeOf(data));
StephaneLenclud@151
   177
            Marshal.StructureToPtr(data,
StephaneLenclud@151
   178
                buf, false);
StephaneLenclud@151
   179
            return buf;
StephaneLenclud@151
   180
        }
StephaneLenclud@151
   181
StephaneLenclud@151
   182
        /// <summary>
StephaneLenclud@151
   183
        /// 
StephaneLenclud@151
   184
        /// </summary>
StephaneLenclud@151
   185
        /// <returns></returns>
StephaneLenclud@152
   186
        private SafeFileHandle OpenVolume(string aDriveName)
StephaneLenclud@150
   187
        {
StephaneLenclud@152
   188
            return Function.CreateFile("\\\\.\\" + aDriveName,
StephaneLenclud@150
   189
                               SharpLib.Win32.FileAccess.GENERIC_READ,
StephaneLenclud@150
   190
                               SharpLib.Win32.FileShare.FILE_SHARE_READ | SharpLib.Win32.FileShare.FILE_SHARE_WRITE,
StephaneLenclud@150
   191
                               IntPtr.Zero,
StephaneLenclud@150
   192
                               CreationDisposition.OPEN_EXISTING,
StephaneLenclud@150
   193
                               0,
StephaneLenclud@150
   194
                               IntPtr.Zero);
StephaneLenclud@150
   195
        }
StephaneLenclud@150
   196
StephaneLenclud@150
   197
        /// <summary>
StephaneLenclud@150
   198
        /// 
StephaneLenclud@150
   199
        /// </summary>
StephaneLenclud@151
   200
        /// <param name="aVolume"></param>
StephaneLenclud@151
   201
        /// <returns></returns>
StephaneLenclud@151
   202
        private bool LockVolume(SafeFileHandle aVolume)
StephaneLenclud@151
   203
        {
StephaneLenclud@151
   204
            //Hope that's doing what I think it does
StephaneLenclud@151
   205
            IntPtr dwBytesReturned=new IntPtr();
StephaneLenclud@151
   206
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   207
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@151
   208
StephaneLenclud@151
   209
            int tries = 0;
StephaneLenclud@151
   210
            const int KMaxTries = 100;
StephaneLenclud@151
   211
            const int KSleepTime = 10;
StephaneLenclud@151
   212
            bool success = false;
StephaneLenclud@151
   213
StephaneLenclud@151
   214
            while (!success && tries < KMaxTries)
StephaneLenclud@151
   215
            {
StephaneLenclud@151
   216
                success = Function.DeviceIoControl(aVolume, Const.FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@151
   217
                System.Threading.Thread.Sleep(KSleepTime);
StephaneLenclud@151
   218
                tries++;
StephaneLenclud@151
   219
            }
StephaneLenclud@151
   220
StephaneLenclud@155
   221
            CheckLastError("Lock volume: ");
StephaneLenclud@155
   222
StephaneLenclud@151
   223
            return success;
StephaneLenclud@151
   224
        }
StephaneLenclud@151
   225
StephaneLenclud@151
   226
        /// <summary>
StephaneLenclud@151
   227
        /// 
StephaneLenclud@151
   228
        /// </summary>
StephaneLenclud@151
   229
        /// <param name="aVolume"></param>
StephaneLenclud@151
   230
        /// <returns></returns>
StephaneLenclud@151
   231
        private bool DismountVolume(SafeFileHandle aVolume)
StephaneLenclud@151
   232
        {
StephaneLenclud@151
   233
            //Hope that's doing what I think it does
StephaneLenclud@151
   234
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@151
   235
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   236
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@151
   237
StephaneLenclud@155
   238
            bool res = Function.DeviceIoControl(aVolume, Const.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   239
            CheckLastError("Dismount volume: ");
StephaneLenclud@155
   240
            return res;
StephaneLenclud@151
   241
        }
StephaneLenclud@151
   242
StephaneLenclud@151
   243
StephaneLenclud@151
   244
StephaneLenclud@151
   245
        /// <summary>
StephaneLenclud@151
   246
        /// 
StephaneLenclud@151
   247
        /// </summary>
StephaneLenclud@151
   248
        /// <param name="aVolume"></param>
StephaneLenclud@151
   249
        /// <param name="aPreventRemoval"></param>
StephaneLenclud@151
   250
        /// <returns></returns>
StephaneLenclud@151
   251
        private bool PreventRemovalOfVolume(SafeFileHandle aVolume, bool aPreventRemoval)
StephaneLenclud@151
   252
        {
StephaneLenclud@151
   253
            //Hope that's doing what I think it does
StephaneLenclud@151
   254
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@151
   255
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   256
            OVERLAPPED overlapped = new OVERLAPPED();
StephaneLenclud@151
   257
            //
StephaneLenclud@151
   258
            PREVENT_MEDIA_REMOVAL preventMediaRemoval = new PREVENT_MEDIA_REMOVAL();
StephaneLenclud@151
   259
            preventMediaRemoval.PreventMediaRemoval = Convert.ToByte(aPreventRemoval);
StephaneLenclud@151
   260
            IntPtr preventMediaRemovalParam = MarshalToPointer(preventMediaRemoval);
StephaneLenclud@151
   261
StephaneLenclud@151
   262
            bool result = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_MEDIA_REMOVAL, preventMediaRemovalParam, Convert.ToUInt32(Marshal.SizeOf(preventMediaRemoval)), IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   263
            CheckLastError("Media removal: ");
StephaneLenclud@151
   264
            Marshal.FreeHGlobal(preventMediaRemovalParam);
StephaneLenclud@151
   265
StephaneLenclud@151
   266
            return result;
StephaneLenclud@151
   267
        }
StephaneLenclud@151
   268
StephaneLenclud@151
   269
        /// <summary>
StephaneLenclud@154
   270
        /// Eject optical drive media opening the tray if any.
StephaneLenclud@151
   271
        /// </summary>
StephaneLenclud@151
   272
        /// <param name="aVolume"></param>
StephaneLenclud@151
   273
        /// <returns></returns>
StephaneLenclud@154
   274
        private bool MediaEject(SafeFileHandle aVolume)
StephaneLenclud@151
   275
        {
StephaneLenclud@151
   276
            //Hope that's doing what I think it does
StephaneLenclud@151
   277
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@151
   278
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   279
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@151
   280
StephaneLenclud@155
   281
            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   282
            CheckLastError("Media eject: ");
StephaneLenclud@155
   283
            return res;
StephaneLenclud@151
   284
        }
StephaneLenclud@151
   285
StephaneLenclud@152
   286
        /// <summary>
StephaneLenclud@154
   287
        /// Close an optical drive tray.
StephaneLenclud@152
   288
        /// </summary>
StephaneLenclud@152
   289
        /// <param name="aVolume"></param>
StephaneLenclud@152
   290
        /// <returns></returns>
StephaneLenclud@154
   291
        private bool MediaLoad(SafeFileHandle aVolume)
StephaneLenclud@152
   292
        {
StephaneLenclud@152
   293
            //Hope that's doing what I think it does
StephaneLenclud@152
   294
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@152
   295
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@152
   296
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@152
   297
StephaneLenclud@155
   298
            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_LOAD_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   299
            CheckLastError("Media load: ");
StephaneLenclud@155
   300
            return res;
StephaneLenclud@152
   301
        }
StephaneLenclud@152
   302
StephaneLenclud@154
   303
        /// <summary>
StephaneLenclud@154
   304
        /// 
StephaneLenclud@154
   305
        /// </summary>
StephaneLenclud@154
   306
        /// <param name="aVolume"></param>
StephaneLenclud@154
   307
        /// <returns></returns>
StephaneLenclud@154
   308
        private bool StorageCheckVerify(SafeFileHandle aVolume)
StephaneLenclud@154
   309
        {
StephaneLenclud@154
   310
            //Hope that's doing what I think it does
StephaneLenclud@154
   311
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@154
   312
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@154
   313
            OVERLAPPED overlapped = new OVERLAPPED();
StephaneLenclud@154
   314
StephaneLenclud@154
   315
            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_CHECK_VERIFY2, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@154
   316
StephaneLenclud@155
   317
            CheckLastError("Check verify: ");
StephaneLenclud@154
   318
StephaneLenclud@154
   319
            return res;
StephaneLenclud@154
   320
        }        
StephaneLenclud@151
   321
        
StephaneLenclud@151
   322
StephaneLenclud@151
   323
StephaneLenclud@151
   324
        /// <summary>
StephaneLenclud@151
   325
        /// Perform media ejection.
StephaneLenclud@151
   326
        /// </summary>
StephaneLenclud@150
   327
        private void HandleEject()
StephaneLenclud@150
   328
        {
StephaneLenclud@226
   329
            string drive = ((FormMain)this).OpticalDriveToEject();
StephaneLenclud@153
   330
            if (drive.Length!=2)
StephaneLenclud@153
   331
            {
StephaneLenclud@153
   332
                //Not a proper drive spec.
StephaneLenclud@153
   333
                //Probably 'None' selected.
StephaneLenclud@153
   334
                return;
StephaneLenclud@153
   335
            }
StephaneLenclud@153
   336
StephaneLenclud@153
   337
            SafeFileHandle handle = OpenVolume(drive);
StephaneLenclud@151
   338
            if (handle.IsInvalid)
StephaneLenclud@151
   339
            {
StephaneLenclud@155
   340
                CheckLastError("ERROR: Failed to open volume: ");
StephaneLenclud@151
   341
                return;
StephaneLenclud@151
   342
            }
StephaneLenclud@151
   343
StephaneLenclud@151
   344
            if (LockVolume(handle) && DismountVolume(handle))
StephaneLenclud@151
   345
            {
StephaneLenclud@154
   346
                Debug.WriteLine("Volume was dismounted.");
StephaneLenclud@151
   347
StephaneLenclud@152
   348
                if (PreventRemovalOfVolume(handle,false))
StephaneLenclud@151
   349
                {
StephaneLenclud@156
   350
                    //StorageCheckVerify(handle);
StephaneLenclud@154
   351
StephaneLenclud@156
   352
                    DateTime before;
StephaneLenclud@156
   353
                    before = DateTime.Now;
StephaneLenclud@156
   354
                    bool ejectSuccess = MediaEject(handle);
StephaneLenclud@156
   355
                    double ms = (DateTime.Now - before).TotalMilliseconds;
StephaneLenclud@156
   356
StephaneLenclud@156
   357
                    //We assume that if it take more than a certain time to for eject to execute it means we actually ejected.
StephaneLenclud@156
   358
                    //If our eject completes too rapidly we assume the tray is already open and we will try to close it. 
StephaneLenclud@156
   359
                    if (ejectSuccess && ms > 100)
StephaneLenclud@152
   360
                    {
StephaneLenclud@154
   361
                        Debug.WriteLine("Media was ejected");
StephaneLenclud@152
   362
                    }
StephaneLenclud@154
   363
                    else if (MediaLoad(handle))
StephaneLenclud@154
   364
                    {
StephaneLenclud@154
   365
                        Debug.WriteLine("Media was loaded");
StephaneLenclud@154
   366
                    }                    
StephaneLenclud@151
   367
                }
StephaneLenclud@151
   368
            }
StephaneLenclud@154
   369
            else
StephaneLenclud@154
   370
            {
StephaneLenclud@154
   371
                Debug.WriteLine("Volume lock or dismount failed.");
StephaneLenclud@154
   372
            }
StephaneLenclud@154
   373
StephaneLenclud@154
   374
            //This is needed to make sure we can open the volume next time around
StephaneLenclud@154
   375
            handle.Dispose();
StephaneLenclud@150
   376
        }
StephaneLenclud@150
   377
StephaneLenclud@150
   378
        /// <summary>
StephaneLenclud@150
   379
        /// 
StephaneLenclud@150
   380
        /// </summary>
StephaneLenclud@150
   381
        private void HandleGreenStart()
StephaneLenclud@150
   382
        {
StephaneLenclud@150
   383
            //First check if the process we want to launch already exists
StephaneLenclud@150
   384
            string procName = Path.GetFileNameWithoutExtension(Properties.Settings.Default.StartFileName);
StephaneLenclud@150
   385
            Process[] existingProcesses = Process.GetProcessesByName(procName);
StephaneLenclud@150
   386
            if (existingProcesses == null || existingProcesses.Length == 0)
StephaneLenclud@150
   387
            {
StephaneLenclud@150
   388
                // Process do not exists just try to launch it
StephaneLenclud@150
   389
                ProcessStartInfo start = new ProcessStartInfo();
StephaneLenclud@150
   390
                // Enter in the command line arguments, everything you would enter after the executable name itself
StephaneLenclud@150
   391
                //start.Arguments = arguments; 
StephaneLenclud@150
   392
                // Enter the executable to run, including the complete path
StephaneLenclud@150
   393
                start.FileName = Properties.Settings.Default.StartFileName;
StephaneLenclud@150
   394
                start.WindowStyle = ProcessWindowStyle.Normal;
StephaneLenclud@150
   395
                start.CreateNoWindow = true;
StephaneLenclud@150
   396
                start.UseShellExecute = true;
StephaneLenclud@150
   397
                // Run the external process & wait for it to finish
StephaneLenclud@150
   398
                Process proc = Process.Start(start);
StephaneLenclud@150
   399
StephaneLenclud@150
   400
                //SL: We could have used that too
StephaneLenclud@150
   401
                //Shell32.Shell shell = new Shell32.Shell();
StephaneLenclud@150
   402
                //shell.ShellExecute(Properties.Settings.Default.StartFileName);
StephaneLenclud@150
   403
            }
StephaneLenclud@150
   404
            else
StephaneLenclud@150
   405
            {
StephaneLenclud@150
   406
                //This won't work properly until we have a manifest that enables uiAccess.
StephaneLenclud@150
   407
                //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
StephaneLenclud@150
   408
                SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);
StephaneLenclud@150
   409
            }            
StephaneLenclud@150
   410
        }
StephaneLenclud@167
   411
StephaneLenclud@167
   412
StephaneLenclud@131
   413
        /// <summary>
StephaneLenclud@131
   414
        /// We need to handle WM_INPUT.
StephaneLenclud@131
   415
        /// </summary>
StephaneLenclud@131
   416
        /// <param name="message"></param>
StephaneLenclud@131
   417
        protected override void WndProc(ref Message message)
StephaneLenclud@131
   418
        {
StephaneLenclud@131
   419
            switch (message.Msg)
StephaneLenclud@131
   420
            {
StephaneLenclud@131
   421
                case Const.WM_INPUT:
StephaneLenclud@131
   422
                    //Returning zero means we processed that message.
StephaneLenclud@131
   423
                    message.Result = new IntPtr(0);
StephaneLenclud@131
   424
                    iHidHandler.ProcessInput(ref message);
StephaneLenclud@131
   425
                    break;
StephaneLenclud@131
   426
            }
StephaneLenclud@159
   427
StephaneLenclud@167
   428
            //Pass this on to base class.
StephaneLenclud@131
   429
            base.WndProc(ref message);
StephaneLenclud@131
   430
        }
StephaneLenclud@131
   431
    }
StephaneLenclud@125
   432
}