Server/FormMain.Hid.cs
author StephaneLenclud
Thu, 18 Aug 2016 17:43:03 +0200
changeset 240 5c4f1e2bf29a
parent 237 1a1c2ae3a29c
child 241 3b5a94f31400
permissions -rw-r--r--
Adding empty class for eject optical drive action.
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@238
   130
                    //Trigger events as needed
StephaneLenclud@238
   131
                    EventHidWindowsMediaCenter e = new EventHidWindowsMediaCenter { Usage = (Hid.Usage.WindowsMediaCenterRemoteControl)aHidEvent.Usages[0] };
StephaneLenclud@238
   132
                    Properties.Settings.Default.EarManager.TriggerEvent(e);
StephaneLenclud@238
   133
StephaneLenclud@238
   134
                    //Old legacy hard coded stuff
StephaneLenclud@238
   135
                    //TODO: remove it
StephaneLenclud@150
   136
                    switch (aHidEvent.Usages[0])
StephaneLenclud@131
   137
                    {
StephaneLenclud@150
   138
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart:
StephaneLenclud@150
   139
                            HandleGreenStart();
StephaneLenclud@150
   140
                            break;
StephaneLenclud@150
   141
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Eject:
StephaneLenclud@150
   142
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Ext2:
StephaneLenclud@150
   143
                            HandleEject();
StephaneLenclud@150
   144
                            break;
StephaneLenclud@131
   145
                    }
StephaneLenclud@131
   146
                }
StephaneLenclud@237
   147
                else if (aHidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer)
StephaneLenclud@237
   148
                {
StephaneLenclud@238
   149
                    EventHidConsumerControl e = new EventHidConsumerControl { Usage = (Hid.Usage.ConsumerControl)aHidEvent.Usages[0] };
StephaneLenclud@238
   150
                    Properties.Settings.Default.EarManager.TriggerEvent(e);
StephaneLenclud@238
   151
StephaneLenclud@237
   152
                    //Keep this for debug when only ThinkPad keyboard is available
StephaneLenclud@237
   153
                    //if (aHidEvent.Usages[0] == (ushort)Hid.Usage.ConsumerControl.ThinkPadFullscreenMagnifier)
StephaneLenclud@237
   154
                    //{
StephaneLenclud@237
   155
                    //    HandleEject();
StephaneLenclud@237
   156
                    //}
StephaneLenclud@152
   157
                }
StephaneLenclud@152
   158
StephaneLenclud@131
   159
            }
StephaneLenclud@131
   160
        }
StephaneLenclud@125
   161
StephaneLenclud@151
   162
        /// <summary>
StephaneLenclud@151
   163
        /// 
StephaneLenclud@151
   164
        /// </summary>
StephaneLenclud@155
   165
        /// <param name="aPrefix"></param>
StephaneLenclud@155
   166
        private void CheckLastError(string aPrefix)
StephaneLenclud@155
   167
        {
StephaneLenclud@155
   168
            string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
StephaneLenclud@155
   169
            Debug.WriteLine(aPrefix + Marshal.GetLastWin32Error().ToString() + ": " + errorMessage);
StephaneLenclud@155
   170
        }
StephaneLenclud@155
   171
StephaneLenclud@155
   172
        /// <summary>
StephaneLenclud@155
   173
        /// 
StephaneLenclud@155
   174
        /// </summary>
StephaneLenclud@151
   175
        /// <param name="data"></param>
StephaneLenclud@151
   176
        /// <returns></returns>
StephaneLenclud@151
   177
        private IntPtr MarshalToPointer(object data)
StephaneLenclud@151
   178
        {
StephaneLenclud@151
   179
            IntPtr buf = Marshal.AllocHGlobal(
StephaneLenclud@151
   180
                Marshal.SizeOf(data));
StephaneLenclud@151
   181
            Marshal.StructureToPtr(data,
StephaneLenclud@151
   182
                buf, false);
StephaneLenclud@151
   183
            return buf;
StephaneLenclud@151
   184
        }
StephaneLenclud@151
   185
StephaneLenclud@151
   186
        /// <summary>
StephaneLenclud@151
   187
        /// 
StephaneLenclud@151
   188
        /// </summary>
StephaneLenclud@151
   189
        /// <returns></returns>
StephaneLenclud@152
   190
        private SafeFileHandle OpenVolume(string aDriveName)
StephaneLenclud@150
   191
        {
StephaneLenclud@152
   192
            return Function.CreateFile("\\\\.\\" + aDriveName,
StephaneLenclud@150
   193
                               SharpLib.Win32.FileAccess.GENERIC_READ,
StephaneLenclud@150
   194
                               SharpLib.Win32.FileShare.FILE_SHARE_READ | SharpLib.Win32.FileShare.FILE_SHARE_WRITE,
StephaneLenclud@150
   195
                               IntPtr.Zero,
StephaneLenclud@150
   196
                               CreationDisposition.OPEN_EXISTING,
StephaneLenclud@150
   197
                               0,
StephaneLenclud@150
   198
                               IntPtr.Zero);
StephaneLenclud@150
   199
        }
StephaneLenclud@150
   200
StephaneLenclud@150
   201
        /// <summary>
StephaneLenclud@150
   202
        /// 
StephaneLenclud@150
   203
        /// </summary>
StephaneLenclud@151
   204
        /// <param name="aVolume"></param>
StephaneLenclud@151
   205
        /// <returns></returns>
StephaneLenclud@151
   206
        private bool LockVolume(SafeFileHandle aVolume)
StephaneLenclud@151
   207
        {
StephaneLenclud@151
   208
            //Hope that's doing what I think it does
StephaneLenclud@151
   209
            IntPtr dwBytesReturned=new IntPtr();
StephaneLenclud@151
   210
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   211
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@151
   212
StephaneLenclud@151
   213
            int tries = 0;
StephaneLenclud@151
   214
            const int KMaxTries = 100;
StephaneLenclud@151
   215
            const int KSleepTime = 10;
StephaneLenclud@151
   216
            bool success = false;
StephaneLenclud@151
   217
StephaneLenclud@151
   218
            while (!success && tries < KMaxTries)
StephaneLenclud@151
   219
            {
StephaneLenclud@151
   220
                success = Function.DeviceIoControl(aVolume, Const.FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@151
   221
                System.Threading.Thread.Sleep(KSleepTime);
StephaneLenclud@151
   222
                tries++;
StephaneLenclud@151
   223
            }
StephaneLenclud@151
   224
StephaneLenclud@155
   225
            CheckLastError("Lock volume: ");
StephaneLenclud@155
   226
StephaneLenclud@151
   227
            return success;
StephaneLenclud@151
   228
        }
StephaneLenclud@151
   229
StephaneLenclud@151
   230
        /// <summary>
StephaneLenclud@151
   231
        /// 
StephaneLenclud@151
   232
        /// </summary>
StephaneLenclud@151
   233
        /// <param name="aVolume"></param>
StephaneLenclud@151
   234
        /// <returns></returns>
StephaneLenclud@151
   235
        private bool DismountVolume(SafeFileHandle aVolume)
StephaneLenclud@151
   236
        {
StephaneLenclud@151
   237
            //Hope that's doing what I think it does
StephaneLenclud@151
   238
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@151
   239
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   240
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@151
   241
StephaneLenclud@155
   242
            bool res = Function.DeviceIoControl(aVolume, Const.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   243
            CheckLastError("Dismount volume: ");
StephaneLenclud@155
   244
            return res;
StephaneLenclud@151
   245
        }
StephaneLenclud@151
   246
StephaneLenclud@151
   247
StephaneLenclud@151
   248
StephaneLenclud@151
   249
        /// <summary>
StephaneLenclud@151
   250
        /// 
StephaneLenclud@151
   251
        /// </summary>
StephaneLenclud@151
   252
        /// <param name="aVolume"></param>
StephaneLenclud@151
   253
        /// <param name="aPreventRemoval"></param>
StephaneLenclud@151
   254
        /// <returns></returns>
StephaneLenclud@151
   255
        private bool PreventRemovalOfVolume(SafeFileHandle aVolume, bool aPreventRemoval)
StephaneLenclud@151
   256
        {
StephaneLenclud@151
   257
            //Hope that's doing what I think it does
StephaneLenclud@151
   258
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@151
   259
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   260
            OVERLAPPED overlapped = new OVERLAPPED();
StephaneLenclud@151
   261
            //
StephaneLenclud@151
   262
            PREVENT_MEDIA_REMOVAL preventMediaRemoval = new PREVENT_MEDIA_REMOVAL();
StephaneLenclud@151
   263
            preventMediaRemoval.PreventMediaRemoval = Convert.ToByte(aPreventRemoval);
StephaneLenclud@151
   264
            IntPtr preventMediaRemovalParam = MarshalToPointer(preventMediaRemoval);
StephaneLenclud@151
   265
StephaneLenclud@151
   266
            bool result = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_MEDIA_REMOVAL, preventMediaRemovalParam, Convert.ToUInt32(Marshal.SizeOf(preventMediaRemoval)), IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   267
            CheckLastError("Media removal: ");
StephaneLenclud@151
   268
            Marshal.FreeHGlobal(preventMediaRemovalParam);
StephaneLenclud@151
   269
StephaneLenclud@151
   270
            return result;
StephaneLenclud@151
   271
        }
StephaneLenclud@151
   272
StephaneLenclud@151
   273
        /// <summary>
StephaneLenclud@154
   274
        /// Eject optical drive media opening the tray if any.
StephaneLenclud@151
   275
        /// </summary>
StephaneLenclud@151
   276
        /// <param name="aVolume"></param>
StephaneLenclud@151
   277
        /// <returns></returns>
StephaneLenclud@154
   278
        private bool MediaEject(SafeFileHandle aVolume)
StephaneLenclud@151
   279
        {
StephaneLenclud@151
   280
            //Hope that's doing what I think it does
StephaneLenclud@151
   281
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@151
   282
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   283
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@151
   284
StephaneLenclud@155
   285
            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   286
            CheckLastError("Media eject: ");
StephaneLenclud@155
   287
            return res;
StephaneLenclud@151
   288
        }
StephaneLenclud@151
   289
StephaneLenclud@152
   290
        /// <summary>
StephaneLenclud@154
   291
        /// Close an optical drive tray.
StephaneLenclud@152
   292
        /// </summary>
StephaneLenclud@152
   293
        /// <param name="aVolume"></param>
StephaneLenclud@152
   294
        /// <returns></returns>
StephaneLenclud@154
   295
        private bool MediaLoad(SafeFileHandle aVolume)
StephaneLenclud@152
   296
        {
StephaneLenclud@152
   297
            //Hope that's doing what I think it does
StephaneLenclud@152
   298
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@152
   299
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@152
   300
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@152
   301
StephaneLenclud@155
   302
            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_LOAD_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   303
            CheckLastError("Media load: ");
StephaneLenclud@155
   304
            return res;
StephaneLenclud@152
   305
        }
StephaneLenclud@152
   306
StephaneLenclud@154
   307
        /// <summary>
StephaneLenclud@154
   308
        /// 
StephaneLenclud@154
   309
        /// </summary>
StephaneLenclud@154
   310
        /// <param name="aVolume"></param>
StephaneLenclud@154
   311
        /// <returns></returns>
StephaneLenclud@154
   312
        private bool StorageCheckVerify(SafeFileHandle aVolume)
StephaneLenclud@154
   313
        {
StephaneLenclud@154
   314
            //Hope that's doing what I think it does
StephaneLenclud@154
   315
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@154
   316
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@154
   317
            OVERLAPPED overlapped = new OVERLAPPED();
StephaneLenclud@154
   318
StephaneLenclud@154
   319
            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_CHECK_VERIFY2, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@154
   320
StephaneLenclud@155
   321
            CheckLastError("Check verify: ");
StephaneLenclud@154
   322
StephaneLenclud@154
   323
            return res;
StephaneLenclud@154
   324
        }        
StephaneLenclud@151
   325
        
StephaneLenclud@151
   326
StephaneLenclud@151
   327
StephaneLenclud@151
   328
        /// <summary>
StephaneLenclud@151
   329
        /// Perform media ejection.
StephaneLenclud@151
   330
        /// </summary>
StephaneLenclud@150
   331
        private void HandleEject()
StephaneLenclud@150
   332
        {
StephaneLenclud@226
   333
            string drive = ((FormMain)this).OpticalDriveToEject();
StephaneLenclud@153
   334
            if (drive.Length!=2)
StephaneLenclud@153
   335
            {
StephaneLenclud@153
   336
                //Not a proper drive spec.
StephaneLenclud@153
   337
                //Probably 'None' selected.
StephaneLenclud@153
   338
                return;
StephaneLenclud@153
   339
            }
StephaneLenclud@153
   340
StephaneLenclud@153
   341
            SafeFileHandle handle = OpenVolume(drive);
StephaneLenclud@151
   342
            if (handle.IsInvalid)
StephaneLenclud@151
   343
            {
StephaneLenclud@155
   344
                CheckLastError("ERROR: Failed to open volume: ");
StephaneLenclud@151
   345
                return;
StephaneLenclud@151
   346
            }
StephaneLenclud@151
   347
StephaneLenclud@151
   348
            if (LockVolume(handle) && DismountVolume(handle))
StephaneLenclud@151
   349
            {
StephaneLenclud@154
   350
                Debug.WriteLine("Volume was dismounted.");
StephaneLenclud@151
   351
StephaneLenclud@152
   352
                if (PreventRemovalOfVolume(handle,false))
StephaneLenclud@151
   353
                {
StephaneLenclud@156
   354
                    //StorageCheckVerify(handle);
StephaneLenclud@154
   355
StephaneLenclud@156
   356
                    DateTime before;
StephaneLenclud@156
   357
                    before = DateTime.Now;
StephaneLenclud@156
   358
                    bool ejectSuccess = MediaEject(handle);
StephaneLenclud@156
   359
                    double ms = (DateTime.Now - before).TotalMilliseconds;
StephaneLenclud@156
   360
StephaneLenclud@156
   361
                    //We assume that if it take more than a certain time to for eject to execute it means we actually ejected.
StephaneLenclud@156
   362
                    //If our eject completes too rapidly we assume the tray is already open and we will try to close it. 
StephaneLenclud@156
   363
                    if (ejectSuccess && ms > 100)
StephaneLenclud@152
   364
                    {
StephaneLenclud@154
   365
                        Debug.WriteLine("Media was ejected");
StephaneLenclud@152
   366
                    }
StephaneLenclud@154
   367
                    else if (MediaLoad(handle))
StephaneLenclud@154
   368
                    {
StephaneLenclud@154
   369
                        Debug.WriteLine("Media was loaded");
StephaneLenclud@154
   370
                    }                    
StephaneLenclud@151
   371
                }
StephaneLenclud@151
   372
            }
StephaneLenclud@154
   373
            else
StephaneLenclud@154
   374
            {
StephaneLenclud@154
   375
                Debug.WriteLine("Volume lock or dismount failed.");
StephaneLenclud@154
   376
            }
StephaneLenclud@154
   377
StephaneLenclud@154
   378
            //This is needed to make sure we can open the volume next time around
StephaneLenclud@154
   379
            handle.Dispose();
StephaneLenclud@150
   380
        }
StephaneLenclud@150
   381
StephaneLenclud@150
   382
        /// <summary>
StephaneLenclud@150
   383
        /// 
StephaneLenclud@150
   384
        /// </summary>
StephaneLenclud@150
   385
        private void HandleGreenStart()
StephaneLenclud@150
   386
        {
StephaneLenclud@150
   387
            //First check if the process we want to launch already exists
StephaneLenclud@150
   388
            string procName = Path.GetFileNameWithoutExtension(Properties.Settings.Default.StartFileName);
StephaneLenclud@150
   389
            Process[] existingProcesses = Process.GetProcessesByName(procName);
StephaneLenclud@150
   390
            if (existingProcesses == null || existingProcesses.Length == 0)
StephaneLenclud@150
   391
            {
StephaneLenclud@150
   392
                // Process do not exists just try to launch it
StephaneLenclud@150
   393
                ProcessStartInfo start = new ProcessStartInfo();
StephaneLenclud@150
   394
                // Enter in the command line arguments, everything you would enter after the executable name itself
StephaneLenclud@150
   395
                //start.Arguments = arguments; 
StephaneLenclud@150
   396
                // Enter the executable to run, including the complete path
StephaneLenclud@150
   397
                start.FileName = Properties.Settings.Default.StartFileName;
StephaneLenclud@150
   398
                start.WindowStyle = ProcessWindowStyle.Normal;
StephaneLenclud@150
   399
                start.CreateNoWindow = true;
StephaneLenclud@150
   400
                start.UseShellExecute = true;
StephaneLenclud@150
   401
                // Run the external process & wait for it to finish
StephaneLenclud@150
   402
                Process proc = Process.Start(start);
StephaneLenclud@150
   403
StephaneLenclud@150
   404
                //SL: We could have used that too
StephaneLenclud@150
   405
                //Shell32.Shell shell = new Shell32.Shell();
StephaneLenclud@150
   406
                //shell.ShellExecute(Properties.Settings.Default.StartFileName);
StephaneLenclud@150
   407
            }
StephaneLenclud@150
   408
            else
StephaneLenclud@150
   409
            {
StephaneLenclud@150
   410
                //This won't work properly until we have a manifest that enables uiAccess.
StephaneLenclud@150
   411
                //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
StephaneLenclud@150
   412
                SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);
StephaneLenclud@150
   413
            }            
StephaneLenclud@150
   414
        }
StephaneLenclud@167
   415
StephaneLenclud@167
   416
StephaneLenclud@131
   417
        /// <summary>
StephaneLenclud@131
   418
        /// We need to handle WM_INPUT.
StephaneLenclud@131
   419
        /// </summary>
StephaneLenclud@131
   420
        /// <param name="message"></param>
StephaneLenclud@131
   421
        protected override void WndProc(ref Message message)
StephaneLenclud@131
   422
        {
StephaneLenclud@131
   423
            switch (message.Msg)
StephaneLenclud@131
   424
            {
StephaneLenclud@131
   425
                case Const.WM_INPUT:
StephaneLenclud@131
   426
                    //Returning zero means we processed that message.
StephaneLenclud@131
   427
                    message.Result = new IntPtr(0);
StephaneLenclud@131
   428
                    iHidHandler.ProcessInput(ref message);
StephaneLenclud@131
   429
                    break;
StephaneLenclud@131
   430
            }
StephaneLenclud@159
   431
StephaneLenclud@167
   432
            //Pass this on to base class.
StephaneLenclud@131
   433
            base.WndProc(ref message);
StephaneLenclud@131
   434
        }
StephaneLenclud@131
   435
    }
StephaneLenclud@125
   436
}