Skip to content

Commit

Permalink
Added project and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jul 30, 2020
1 parent f513a88 commit 48f5a22
Show file tree
Hide file tree
Showing 15 changed files with 635 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SurfaceAlwaysDTX

A super simple, open source reimplementation of Microsoft's SurfaceDTX tool found on the Surface Book 2.

The main reason this exists is to allow detaching of the Surface Book 2's screen, even if the battery is "too low" and the original tool prevents you. Use at your own risk.

I also made a writeup about the reverse engineering process of SurfaceDTX which can be found on by website: https://werwolv.net/blog/surface_ioctl
31 changes: 31 additions & 0 deletions SurfaceAlwaysDTX.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurfaceAlwaysDTX", "SurfaceAlwaysDTX\SurfaceAlwaysDTX.csproj", "{D0C6AD65-B309-4D0E-86CF-BC696ED888A0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D0C6AD65-B309-4D0E-86CF-BC696ED888A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0C6AD65-B309-4D0E-86CF-BC696ED888A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0C6AD65-B309-4D0E-86CF-BC696ED888A0}.Debug|x64.ActiveCfg = Debug|x64
{D0C6AD65-B309-4D0E-86CF-BC696ED888A0}.Debug|x64.Build.0 = Debug|x64
{D0C6AD65-B309-4D0E-86CF-BC696ED888A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0C6AD65-B309-4D0E-86CF-BC696ED888A0}.Release|Any CPU.Build.0 = Release|Any CPU
{D0C6AD65-B309-4D0E-86CF-BC696ED888A0}.Release|x64.ActiveCfg = Release|x64
{D0C6AD65-B309-4D0E-86CF-BC696ED888A0}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DAE2BD4B-EE5A-4DDF-98C1-7947877E77DD}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions SurfaceAlwaysDTX/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
88 changes: 88 additions & 0 deletions SurfaceAlwaysDTX/LatchControler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SurfaceAlwaysDTX
{
public class LatchControler
{
/// <summary>
/// Latch device driver path
/// </summary>
private static readonly string LatchDeviceDriver = "\\\\?\\ACPI#MSHW0133#2&daba3ff&1#{f49e75f6-f869-4346-9eb8-ded248275916}";

/// <summary>
/// Latch command ioctl command
/// </summary>
private static readonly UInt32 LatchCommandIoctl = 0x8000A044;

/// <summary>
/// Latch command types
/// </summary>
private enum LatchCommandType
{
Invalid,
Open,
Close_DEPRECATED,
ButtonPress,
Cancel,
MaximumValue,
}

/// <summary>
/// Structure of input data expected by the LatchCommandIoctl command
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct LatchCommandInArgs
{
public LatchCommandType LatchCommand;
public uint TimeoutMs;
}

/// <summary>
/// Unlocks the latch so it may be opened in any case, even when the battery is bellow the required treashold
/// </summary>
public static void UnlockLatch()
{
var hLatchDriver = WinAPI.CreateFileW(LatchDeviceDriver, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite, IntPtr.Zero, System.IO.FileMode.Open, System.IO.FileAttributes.Normal, IntPtr.Zero);

if (hLatchDriver == IntPtr.Zero || hLatchDriver.ToInt32() == WinAPI.INVALID_HANDLE_VALUE)
return;

var args = new LatchCommandInArgs() { LatchCommand = LatchCommandType.Open, TimeoutMs = 5000 };
var argsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(args));
Marshal.StructureToPtr(args, argsPtr, false);

uint bytesReturned = 0;

WinAPI.DeviceIoControl(hLatchDriver, LatchCommandIoctl, argsPtr, (uint)Marshal.SizeOf(args), IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);

WinAPI.CloseHandle(hLatchDriver);
}

/// <summary>
/// Opens the latch to detach the screen
/// </summary>
public static void OpenLatch()
{
var hLatchDriver = WinAPI.CreateFileW(LatchDeviceDriver, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite, IntPtr.Zero, System.IO.FileMode.Open, System.IO.FileAttributes.Normal, IntPtr.Zero);

if (hLatchDriver == IntPtr.Zero || hLatchDriver.ToInt32() == WinAPI.INVALID_HANDLE_VALUE)
return;

var args = new LatchCommandInArgs() { LatchCommand = LatchCommandType.ButtonPress, TimeoutMs = 5000 };
var argsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(args));
Marshal.StructureToPtr(args, argsPtr, false);

uint bytesReturned = 0;

WinAPI.DeviceIoControl(hLatchDriver, LatchCommandIoctl, argsPtr, (uint)Marshal.SizeOf(args), IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);

WinAPI.CloseHandle(hLatchDriver);
}
}
}
22 changes: 22 additions & 0 deletions SurfaceAlwaysDTX/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SurfaceAlwaysDTX
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TrayIconHandler());
}
}
}
36 changes: 36 additions & 0 deletions SurfaceAlwaysDTX/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Surface Always DTX")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("WerWolv")]
[assembly: AssemblyProduct("Surface Always DTX")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d0c6ad65-b309-4d0e-86cf-bc696ed888a0")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
73 changes: 73 additions & 0 deletions SurfaceAlwaysDTX/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions SurfaceAlwaysDTX/Properties/Resources.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="TrayIcon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\trayIcon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Loading

0 comments on commit 48f5a22

Please sign in to comment.