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