From c45cb64865022279219781c18d3306f233092577 Mon Sep 17 00:00:00 2001
From: gmurray <gmurray@gmail.com>
Date: Tue, 17 May 2016 13:46:14 -0400
Subject: [PATCH 1/3] add ability to no restart a UWP app, adjust mouse move
 simulation to work with UWP

---
 .../Handlers/NewSessionHandler.cs             |  4 ++
 src/WinAppDriver/ResetStrategy.cs             |  5 ++-
 src/WinAppDriver/UI/Mouse.cs                  | 37 ++++++++++++++++++-
 .../Wrappers/WinUserWrapper/IWinUserWrap.cs   |  3 ++
 .../Wrappers/WinUserWrapper/WinUserWrap.cs    |  8 ++++
 5 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/src/WinAppDriver/Handlers/NewSessionHandler.cs b/src/WinAppDriver/Handlers/NewSessionHandler.cs
index 1e06f24..c94833a 100755
--- a/src/WinAppDriver/Handlers/NewSessionHandler.cs
+++ b/src/WinAppDriver/Handlers/NewSessionHandler.cs
@@ -156,7 +156,11 @@ public object Handle(Dictionary<string, string> urlParams, string body, ref ISes
                 }
             }
 
+            if (caps.ResetStrategy != ResetStrategy.SkipActivate)
+            {
             app.Activate();
+            }
+
             session = this.sessionManager.CreateSession(app, caps);
 
             // TODO turn off IME, release all modifier keys
diff --git a/src/WinAppDriver/ResetStrategy.cs b/src/WinAppDriver/ResetStrategy.cs
index 81b4f72..8b632f1 100755
--- a/src/WinAppDriver/ResetStrategy.cs
+++ b/src/WinAppDriver/ResetStrategy.cs
@@ -14,6 +14,9 @@ internal enum ResetStrategy
         Full,
 
         [EnumMember(Value = "noReset")]
-        No
+        No,
+
+        [EnumMember(Value = "skipActivate")]
+        SkipActivate
     }
 }
\ No newline at end of file
diff --git a/src/WinAppDriver/UI/Mouse.cs b/src/WinAppDriver/UI/Mouse.cs
index e3eb281..dc84189 100755
--- a/src/WinAppDriver/UI/Mouse.cs
+++ b/src/WinAppDriver/UI/Mouse.cs
@@ -21,8 +21,41 @@ public Mouse(IWinUserWrap winUser)
 
         public Point Position
         {
-            get { return System.Windows.Forms.Cursor.Position; }
-            private set { System.Windows.Forms.Cursor.Position = value; }
+            get
+            {
+                return System.Windows.Forms.Cursor.Position;
+            }
+
+            private set
+            {
+                Point normalizedXY = AdjustXYToScreen(value.X, value.Y);
+
+                INPUT input = new INPUT
+                {
+                    type = (int)INPUTTYPE.MOUSE,
+                    u = new InputUnion
+                    {
+                        mi = new MOUSEINPUT
+                        {
+                            dx = normalizedXY.X,
+                            dy = normalizedXY.Y,
+                            mouseData = 0,
+                            dwFlags = (uint)(MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE | MOUSEEVENTF.VIRTUALDESK),
+                            time = 0,
+                            dwExtraInfo = new IntPtr(0),
+                        }
+                    }
+                };
+                this.winUser.SendInput(1, new INPUT[] { input }, Marshal.SizeOf(typeof(INPUT)));
+            }
+        }
+
+        public static Point AdjustXYToScreen(int x, int y)
+        {
+            Rectangle bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
+            int x = (0xffff * x) / bounds.Width;
+            int y = (0xffff * y) / bounds.Height;
+            return new Point(x, y);
         }
 
         public void Click(MouseButton button)
diff --git a/src/WinAppDriver/Wrappers/WinUserWrapper/IWinUserWrap.cs b/src/WinAppDriver/Wrappers/WinUserWrapper/IWinUserWrap.cs
index 5ab5e3b..f236c7c 100755
--- a/src/WinAppDriver/Wrappers/WinUserWrapper/IWinUserWrap.cs
+++ b/src/WinAppDriver/Wrappers/WinUserWrapper/IWinUserWrap.cs
@@ -15,6 +15,8 @@ internal interface IWinUserWrap
 
         uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
 
+        int GetSystemMetrics(int nIndex);
+
         IntPtr GetMessageExtraInfo();
 
         short VkKeyScan(char ch);
@@ -84,6 +86,7 @@ internal enum MOUSEEVENTF : uint
         MIDDLEDOWN = 0x0020,
         MIDDLEUP = 0x0040,
         ABSOLUTE = 0x8000,
+        VIRTUALDESK = 0x4000
     }
 
     [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Reviewed.")]
diff --git a/src/WinAppDriver/Wrappers/WinUserWrapper/WinUserWrap.cs b/src/WinAppDriver/Wrappers/WinUserWrapper/WinUserWrap.cs
index 3f0c7c3..8f0c13f 100755
--- a/src/WinAppDriver/Wrappers/WinUserWrapper/WinUserWrap.cs
+++ b/src/WinAppDriver/Wrappers/WinUserWrapper/WinUserWrap.cs
@@ -33,6 +33,11 @@ public short VkKeyScan(char ch)
             return WinUserExtern.VkKeyScan(ch);
         }
 
+        public int GetSystemMetrics(int nIndex)
+        {
+            return WinUserExtern.GetSystemMetrics(nIndex);
+        }
+
         [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Reviewed.")]
         public bool SetForegroundWindow(IntPtr hWnd)
         {
@@ -61,6 +66,9 @@ private class WinUserExtern
             [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Reviewed.")]
             [DllImport("user32.dll", SetLastError = true)]
             public static extern bool SetForegroundWindow(IntPtr hWnd);
+
+            [DllImport("user32.dll", ExactSpelling = true, EntryPoint = "GetSystemMetrics", CharSet = CharSet.Auto)]
+            public static extern int GetSystemMetrics(int nIndex);
         }
     }
 }
\ No newline at end of file

From 1a34ebac9f65c8cd0261d409719c62e117ed1cd3 Mon Sep 17 00:00:00 2001
From: gmurray <gmurray@gmail.com>
Date: Wed, 18 May 2016 15:13:47 -0400
Subject: [PATCH 2/3] fixes for mouse positioning with multiple monitors.

---
 src/WinAppDriver/UI/Mouse.cs                        | 13 +++++++------
 .../Wrappers/WinUserWrapper/IWinUserWrap.cs         |  2 --
 .../Wrappers/WinUserWrapper/WinUserWrap.cs          |  8 --------
 3 files changed, 7 insertions(+), 16 deletions(-)

diff --git a/src/WinAppDriver/UI/Mouse.cs b/src/WinAppDriver/UI/Mouse.cs
index dc84189..6708336 100755
--- a/src/WinAppDriver/UI/Mouse.cs
+++ b/src/WinAppDriver/UI/Mouse.cs
@@ -10,6 +10,8 @@
 
     internal class Mouse : IMouse
     {
+        private const int NormalizedMaximum = 0xFFFF;
+
         private static ILogger logger = Logger.GetLogger("WinAppDriver");
 
         private IWinUserWrap winUser;
@@ -28,7 +30,7 @@ public Point Position
 
             private set
             {
-                Point normalizedXY = AdjustXYToScreen(value.X, value.Y);
+                Point normalizedXY = this.NormalizeCoordinates(value.X, value.Y);
 
                 INPUT input = new INPUT
                 {
@@ -50,12 +52,11 @@ private set
             }
         }
 
-        public static Point AdjustXYToScreen(int x, int y)
+        public Point NormalizeCoordinates(int x, int y)
         {
-            Rectangle bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
-            int x = (0xffff * x) / bounds.Width;
-            int y = (0xffff * y) / bounds.Height;
-            return new Point(x, y);
+            int normalizedX = (NormalizedMaximum * x) / width;
+            int normalizedY = (NormalizedMaximum * y) / height;
+            return new Point(normalizedX, normalizedY);
         }
 
         public void Click(MouseButton button)
diff --git a/src/WinAppDriver/Wrappers/WinUserWrapper/IWinUserWrap.cs b/src/WinAppDriver/Wrappers/WinUserWrapper/IWinUserWrap.cs
index f236c7c..188c8fb 100755
--- a/src/WinAppDriver/Wrappers/WinUserWrapper/IWinUserWrap.cs
+++ b/src/WinAppDriver/Wrappers/WinUserWrapper/IWinUserWrap.cs
@@ -15,8 +15,6 @@ internal interface IWinUserWrap
 
         uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
 
-        int GetSystemMetrics(int nIndex);
-
         IntPtr GetMessageExtraInfo();
 
         short VkKeyScan(char ch);
diff --git a/src/WinAppDriver/Wrappers/WinUserWrapper/WinUserWrap.cs b/src/WinAppDriver/Wrappers/WinUserWrapper/WinUserWrap.cs
index 8f0c13f..3f0c7c3 100755
--- a/src/WinAppDriver/Wrappers/WinUserWrapper/WinUserWrap.cs
+++ b/src/WinAppDriver/Wrappers/WinUserWrapper/WinUserWrap.cs
@@ -33,11 +33,6 @@ public short VkKeyScan(char ch)
             return WinUserExtern.VkKeyScan(ch);
         }
 
-        public int GetSystemMetrics(int nIndex)
-        {
-            return WinUserExtern.GetSystemMetrics(nIndex);
-        }
-
         [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Reviewed.")]
         public bool SetForegroundWindow(IntPtr hWnd)
         {
@@ -66,9 +61,6 @@ private class WinUserExtern
             [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Reviewed.")]
             [DllImport("user32.dll", SetLastError = true)]
             public static extern bool SetForegroundWindow(IntPtr hWnd);
-
-            [DllImport("user32.dll", ExactSpelling = true, EntryPoint = "GetSystemMetrics", CharSet = CharSet.Auto)]
-            public static extern int GetSystemMetrics(int nIndex);
         }
     }
 }
\ No newline at end of file

From d84ba1de09ebde490d0157e9b67619de3b4141bd Mon Sep 17 00:00:00 2001
From: gmurray <gmurray@gmail.com>
Date: Wed, 18 May 2016 15:14:16 -0400
Subject: [PATCH 3/3] fixes for mouse positioning with multiple monitors.

---
 src/WinAppDriver/UI/Mouse.cs | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/WinAppDriver/UI/Mouse.cs b/src/WinAppDriver/UI/Mouse.cs
index 6708336..962c422 100755
--- a/src/WinAppDriver/UI/Mouse.cs
+++ b/src/WinAppDriver/UI/Mouse.cs
@@ -54,6 +54,8 @@ private set
 
         public Point NormalizeCoordinates(int x, int y)
         {
+            var width = System.Windows.Forms.SystemInformation.VirtualScreen.Width;
+            var height = System.Windows.Forms.SystemInformation.VirtualScreen.Height;
             int normalizedX = (NormalizedMaximum * x) / width;
             int normalizedY = (NormalizedMaximum * y) / height;
             return new Point(normalizedX, normalizedY);