Optical Drive Eject: ground work.
authorStephaneLenclud
Tue, 01 Sep 2015 20:57:06 +0200
changeset 150728fe28168e2
parent 149 8d169afc8bf5
child 151 7f62f2c35288
Optical Drive Eject: ground work.
Server/MainForm.Hid.cs
Server/SharpDisplayManager.csproj
Server/packages.config
     1.1 --- a/Server/MainForm.Hid.cs	Wed Aug 12 21:02:43 2015 +0200
     1.2 +++ b/Server/MainForm.Hid.cs	Tue Sep 01 20:57:06 2015 +0200
     1.3 @@ -7,6 +7,7 @@
     1.4  using System.Diagnostics;
     1.5  using System.Runtime.InteropServices;
     1.6  using System.Windows.Forms;
     1.7 +using Microsoft.Win32.SafeHandles;
     1.8  //
     1.9  using Hid = SharpLib.Hid;
    1.10  using SharpLib.Win32;
    1.11 @@ -112,44 +113,81 @@
    1.12              }
    1.13              else
    1.14              {
    1.15 +                if (aHidEvent.Usages.Count == 0)
    1.16 +                {
    1.17 +                    //No usage, nothing to do then
    1.18 +                    return;
    1.19 +                }
    1.20 +
    1.21                  //We are in the proper thread
    1.22 -                if (aHidEvent.Usages.Count > 0
    1.23 -                    && aHidEvent.UsagePage == (ushort)Hid.UsagePage.WindowsMediaCenterRemoteControl
    1.24 -                    && aHidEvent.Usages[0] == (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart)
    1.25 -                //&& aHidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer
    1.26 -                //&& aHidEvent.Usages[0] == (ushort)Hid.Usage.ConsumerControl.ThinkPadFullscreenMagnifier)
    1.27 +                if (aHidEvent.UsagePage == (ushort) Hid.UsagePage.WindowsMediaCenterRemoteControl)
    1.28                  {
    1.29 -                    //First check if the process we want to launch already exists
    1.30 -                    string procName = Path.GetFileNameWithoutExtension(Properties.Settings.Default.StartFileName);
    1.31 -                    Process[] existingProcesses = Process.GetProcessesByName(procName);
    1.32 -                    if (existingProcesses == null || existingProcesses.Length == 0)
    1.33 +                    switch (aHidEvent.Usages[0])
    1.34                      {
    1.35 -                        // Process do not exists just try to launch it
    1.36 -                        ProcessStartInfo start = new ProcessStartInfo();
    1.37 -                        // Enter in the command line arguments, everything you would enter after the executable name itself
    1.38 -                        //start.Arguments = arguments; 
    1.39 -                        // Enter the executable to run, including the complete path
    1.40 -                        start.FileName = Properties.Settings.Default.StartFileName;
    1.41 -                        start.WindowStyle = ProcessWindowStyle.Normal;
    1.42 -                        start.CreateNoWindow = true;
    1.43 -                        start.UseShellExecute = true;
    1.44 -                        // Run the external process & wait for it to finish
    1.45 -                        Process proc = Process.Start(start);
    1.46 -
    1.47 -                        //SL: We could have used that too
    1.48 -                        //Shell32.Shell shell = new Shell32.Shell();
    1.49 -                        //shell.ShellExecute(Properties.Settings.Default.StartFileName);
    1.50 -                    }
    1.51 -                    else
    1.52 -                    {
    1.53 -                        //This won't work properly until we have a manifest that enables uiAccess.
    1.54 -                        //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
    1.55 -                        SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);
    1.56 +                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart:
    1.57 +                            HandleGreenStart();
    1.58 +                            break;
    1.59 +                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Eject:
    1.60 +                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Ext2:
    1.61 +                            HandleEject();
    1.62 +                            break;
    1.63                      }
    1.64                  }
    1.65              }
    1.66          }
    1.67  
    1.68 +        private SafeFileHandle OpenVolume()
    1.69 +        {
    1.70 +            return Function.CreateFile(  "E:",
    1.71 +                               SharpLib.Win32.FileAccess.GENERIC_READ,
    1.72 +                               SharpLib.Win32.FileShare.FILE_SHARE_READ | SharpLib.Win32.FileShare.FILE_SHARE_WRITE,
    1.73 +                               IntPtr.Zero,
    1.74 +                               CreationDisposition.OPEN_EXISTING,
    1.75 +                               0,
    1.76 +                               IntPtr.Zero);
    1.77 +        }
    1.78 +
    1.79 +        /// <summary>
    1.80 +        /// 
    1.81 +        /// </summary>
    1.82 +        private void HandleEject()
    1.83 +        {
    1.84 +            SafeFileHandle handle = OpenVolume();
    1.85 +        }
    1.86 +
    1.87 +        /// <summary>
    1.88 +        /// 
    1.89 +        /// </summary>
    1.90 +        private void HandleGreenStart()
    1.91 +        {
    1.92 +            //First check if the process we want to launch already exists
    1.93 +            string procName = Path.GetFileNameWithoutExtension(Properties.Settings.Default.StartFileName);
    1.94 +            Process[] existingProcesses = Process.GetProcessesByName(procName);
    1.95 +            if (existingProcesses == null || existingProcesses.Length == 0)
    1.96 +            {
    1.97 +                // Process do not exists just try to launch it
    1.98 +                ProcessStartInfo start = new ProcessStartInfo();
    1.99 +                // Enter in the command line arguments, everything you would enter after the executable name itself
   1.100 +                //start.Arguments = arguments; 
   1.101 +                // Enter the executable to run, including the complete path
   1.102 +                start.FileName = Properties.Settings.Default.StartFileName;
   1.103 +                start.WindowStyle = ProcessWindowStyle.Normal;
   1.104 +                start.CreateNoWindow = true;
   1.105 +                start.UseShellExecute = true;
   1.106 +                // Run the external process & wait for it to finish
   1.107 +                Process proc = Process.Start(start);
   1.108 +
   1.109 +                //SL: We could have used that too
   1.110 +                //Shell32.Shell shell = new Shell32.Shell();
   1.111 +                //shell.ShellExecute(Properties.Settings.Default.StartFileName);
   1.112 +            }
   1.113 +            else
   1.114 +            {
   1.115 +                //This won't work properly until we have a manifest that enables uiAccess.
   1.116 +                //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
   1.117 +                SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);
   1.118 +            }            
   1.119 +        }
   1.120          /// <summary>
   1.121          /// We need to handle WM_INPUT.
   1.122          /// </summary>
     2.1 --- a/Server/SharpDisplayManager.csproj	Wed Aug 12 21:02:43 2015 +0200
     2.2 +++ b/Server/SharpDisplayManager.csproj	Tue Sep 01 20:57:06 2015 +0200
     2.3 @@ -115,12 +115,13 @@
     2.4        <SpecificVersion>False</SpecificVersion>
     2.5        <HintPath>..\packages\NAudio.1.7.3\lib\net35\NAudio.dll</HintPath>
     2.6      </Reference>
     2.7 -    <Reference Include="SharpLibHid, Version=1.0.0.0, Culture=neutral, processorArchitecture=x86">
     2.8 +    <Reference Include="SharpLibHid">
     2.9 +      <HintPath>..\packages\SharpLibHid.1.2.1\lib\net20\SharpLibHid.dll</HintPath>
    2.10 +      <Private>True</Private>
    2.11 +    </Reference>
    2.12 +    <Reference Include="SharpLibWin32, Version=1.0.0.0, Culture=neutral, processorArchitecture=x86">
    2.13        <SpecificVersion>False</SpecificVersion>
    2.14 -      <HintPath>..\packages\SharpLibHid.1.2.0\lib\net20\SharpLibHid.dll</HintPath>
    2.15 -    </Reference>
    2.16 -    <Reference Include="SharpLibWin32">
    2.17 -      <HintPath>..\packages\SharpLibWin32.0.0.3\lib\net20\SharpLibWin32.dll</HintPath>
    2.18 +      <HintPath>..\packages\SharpLibWin32.0.0.4\lib\net20\SharpLibWin32.dll</HintPath>
    2.19      </Reference>
    2.20      <Reference Include="System" />
    2.21      <Reference Include="System.Configuration" />
     3.1 --- a/Server/packages.config	Wed Aug 12 21:02:43 2015 +0200
     3.2 +++ b/Server/packages.config	Tue Sep 01 20:57:06 2015 +0200
     3.3 @@ -2,6 +2,6 @@
     3.4  <packages>
     3.5    <package id="LibMiniDisplay" version="1.1.4" targetFramework="net45" />
     3.6    <package id="NAudio" version="1.7.3" targetFramework="net45" />
     3.7 -  <package id="SharpLibHid" version="1.2.0" targetFramework="net45" />
     3.8 -  <package id="SharpLibWin32" version="0.0.3" targetFramework="net45" />
     3.9 +  <package id="SharpLibHid" version="1.2.1" targetFramework="net45" />
    3.10 +  <package id="SharpLibWin32" version="0.0.4" targetFramework="net45" />
    3.11  </packages>
    3.12 \ No newline at end of file