Skip to content

Commit

Permalink
Merge pull request #55 from jonstoneman/rotation
Browse files Browse the repository at this point in the history
Get and set screen orientation
  • Loading branch information
skyline-gleb committed Nov 11, 2015
2 parents b9d0de2 + 0075992 commit d4177df
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Winium.Cruciatus/Core/DisplayOrientation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Winium.Cruciatus.Core
{
/// <summary>
/// Represents the orientation of the screen
/// </summary>
public enum DisplayOrientation
{
LANDSCAPE = 0,
LANDSCAPE_FLIPPED = 2,
PORTRAIT = 1,
PORTRAIT_FLIPPED = 3
}
}
207 changes: 207 additions & 0 deletions src/Winium.Cruciatus/Core/RotationManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@

namespace Winium.Cruciatus.Core
{
#region using

using System;
using System.Runtime.InteropServices;

#endregion

/// <summary>
/// A static class for retreiving and updating the device orientation
/// </summary>
public static class RotationManager
{
#region Constants

private const int CURRENT_SETTINGS_MODE = -1;

#endregion

#region Inner classes

[StructLayout(LayoutKind.Sequential)]
struct POINTL
{
[MarshalAs(UnmanagedType.I4)]
public int x;
[MarshalAs(UnmanagedType.I4)]
public int y;
}

[StructLayout(LayoutKind.Sequential,
CharSet = CharSet.Ansi)]
struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;

[MarshalAs(UnmanagedType.U2)]
public UInt16 dmSpecVersion;

[MarshalAs(UnmanagedType.U2)]
public UInt16 dmDriverVersion;

[MarshalAs(UnmanagedType.U2)]
public UInt16 dmSize;

[MarshalAs(UnmanagedType.U2)]
public UInt16 dmDriverExtra;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmFields;

public POINTL dmPosition;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayOrientation;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayFixedOutput;

[MarshalAs(UnmanagedType.I2)]
public Int16 dmColor;

[MarshalAs(UnmanagedType.I2)]
public Int16 dmDuplex;

[MarshalAs(UnmanagedType.I2)]
public Int16 dmYResolution;

[MarshalAs(UnmanagedType.I2)]
public Int16 dmTTOption;

[MarshalAs(UnmanagedType.I2)]
public Int16 dmCollate;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;

[MarshalAs(UnmanagedType.U2)]
public UInt16 dmLogPixels;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmBitsPerPel;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPelsWidth;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPelsHeight;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayFlags;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayFrequency;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmICMMethod;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmICMIntent;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmMediaType;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDitherType;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmReserved1;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmReserved2;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPanningWidth;

[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPanningHeight;
}

#endregion

#region Methods

[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean EnumDisplaySettings(
[param: MarshalAs(UnmanagedType.LPTStr)]
string lpszDeviceName,
[param: MarshalAs(UnmanagedType.U4)]
int iModeNum,
[In, Out]
ref DEVMODE lpDevMode);

[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int ChangeDisplaySettings(
[In, Out]
ref DEVMODE lpDevMode,
[param: MarshalAs(UnmanagedType.U4)]
uint dwflags);


/// <summary>
/// Gets the current orientation of the primary screen
/// </summary>
/// <returns>The orientation of the primary screen</returns>
public static DisplayOrientation GetCurrentOrientation()
{
var currentSettings = new DEVMODE();
currentSettings.dmSize = (ushort)Marshal.SizeOf(currentSettings);

EnumDisplaySettings(null, CURRENT_SETTINGS_MODE, ref currentSettings);

return (DisplayOrientation)currentSettings.dmDisplayOrientation;
}

/// <summary>
/// Sets the orientation of the primary screen
/// </summary>
/// <param name="orientation">The desired orientation</param>
/// <returns>
/// The result of setting the orientation:
/// 0: Success or no change required
/// 1: A device restart is required
/// -2: The device does not support this orientation
/// Any other number: Unknown error
/// </returns>
public static int SetOrientation(DisplayOrientation orientation)
{
var currentSettings = new DEVMODE();
currentSettings.dmSize = (ushort)Marshal.SizeOf(currentSettings);

EnumDisplaySettings(null, CURRENT_SETTINGS_MODE, ref currentSettings);

if (currentSettings.dmDisplayOrientation == (int)orientation)
{
return 0;
}

var newSettings = currentSettings;

newSettings.dmDisplayOrientation = (uint)orientation;

var bigDimension = Math.Max(newSettings.dmPelsHeight, newSettings.dmPelsWidth);
var smallDimension = Math.Min(newSettings.dmPelsHeight, newSettings.dmPelsWidth);

if (orientation == DisplayOrientation.LANDSCAPE)
{
newSettings.dmPelsHeight = smallDimension;
newSettings.dmPelsWidth = bigDimension;
}
else
{
newSettings.dmPelsHeight = bigDimension;
newSettings.dmPelsWidth = smallDimension;
}

return ChangeDisplaySettings(ref newSettings, 0);
}

#endregion
}
}
4 changes: 3 additions & 1 deletion src/Winium.Cruciatus/Winium.Cruciatus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<Compile Include="Application.cs" />
<Compile Include="Core\ByProperty.cs" />
<Compile Include="Core\ByXPath.cs" />
<Compile Include="Core\DisplayOrientation.cs" />
<Compile Include="Core\RotationManager.cs" />
<Compile Include="Helpers\AutomationElementHelper.cs" />
<Compile Include="Core\By.cs" />
<Compile Include="Core\ClickStrategies.cs" />
Expand Down Expand Up @@ -127,4 +129,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

0 comments on commit d4177df

Please sign in to comment.