mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 17:56:44 +02:00
feat(MouseWithoutBorders): Prevent Easy Mouse from moving to another machine when an application is running in fullscreen mode. (#39854)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request This PR adds a new feature to Easy Mouse, it is now possible to toggle a setting that will prevent Easy Mouse to switch away from the host machine when the foreground application is running in full screen mode, requiring the user to first alt tab out of the application before performing the switch, this also comes with a way to allow the switch on specific apps.  <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] **Closes:** #32197 - [x] **Communication:** I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [x] **Localization:** All end user facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [x] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: MicrosoftDocs/windows-dev-docs#5470 <!-- Provide a more detailed description of the PR, other things fixed or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments This PR changes the way Easy Mouse checks wherever it should move to another machine, after checking that the corresponding setting is enabled and that we are trying to move away from the host machine, it will run a test using native WinAPI methods to get the foreground window and check if it is running in full screen. If it is, it will then check the name of the executable against a list of ignored app configured by the user, if the executable is found in that list, the switch will be allowed despite the application running in full screen. These new settings were moved along with the original Easy Mouse toggle to a new "Easy Mouse" setting group to avoid cluttering the Keyboard shortcuts group. This feature will only work when used from the controller machine, as I didn't find a way to easily check for running application on a remote machine that didn't involved touching the sockets, I felt like such a change would be out of scope for this issue. <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed I had a hard time writing tests and didn't achieve anything meaningful enough to be included, I may require some guidance on how to properly write tests for this project. I tested my changes by running my modified version of MouseWithoutBorders on my machines, which I did for a few days now, It allowed me to catch a few bugs, but it has been running smoothly otherwise. My changes didn't seemed to have caused any automated tests to fail. It may require some additional testing for setups including more than two machines. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Kai Tao (from Dev Box) <kaitao@microsoft.com> Co-authored-by: Niels Laute <niels.laute@live.nl> Co-authored-by: Gordon Lam (SH) <yeelam@microsoft.com>
This commit is contained in:
@@ -13,6 +13,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
@@ -1560,5 +1561,98 @@ namespace MouseWithoutBorders
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool DisableEasyMouseWhenForegroundWindowIsFullscreenSetting()
|
||||
{
|
||||
return Setting.Values.DisableEasyMouseWhenForegroundWindowIsFullscreen;
|
||||
}
|
||||
|
||||
private static bool IsAppIgnoredByEasyMouseFullscreenCheck(IntPtr foregroundWindowHandle)
|
||||
{
|
||||
if (NativeMethods.GetWindowThreadProcessId(foregroundWindowHandle, out var processId) == 0)
|
||||
{
|
||||
Logger.LogDebug($"GetWindowThreadProcessId failed with error : {Marshal.GetLastWin32Error()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
var processHandle = NativeMethods.OpenProcess(0x1000, false, processId);
|
||||
if (processHandle == IntPtr.Zero)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
uint maxPath = 260;
|
||||
var nameBuffer = new char[maxPath];
|
||||
if (!NativeMethods.QueryFullProcessImageName(
|
||||
processHandle, NativeMethods.QUERY_FULL_PROCESS_NAME_FLAGS.DEFAULT, nameBuffer, ref maxPath))
|
||||
{
|
||||
Logger.LogDebug($"QueryFullProcessImageName failed with error : {Marshal.GetLastWin32Error()}");
|
||||
NativeMethods.CloseHandle(processHandle);
|
||||
return false;
|
||||
}
|
||||
|
||||
NativeMethods.CloseHandle(processHandle);
|
||||
|
||||
var name = new string(nameBuffer, 0, (int)maxPath);
|
||||
|
||||
var excludedApps = Setting.Values.EasyMouseFullscreenSwitchBlockExcludedApps;
|
||||
|
||||
return excludedApps.Contains(Path.GetFileNameWithoutExtension(name), StringComparer.OrdinalIgnoreCase)
|
||||
|| excludedApps.Contains(Path.GetFileName(name), StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
internal static bool IsEasyMouseBlockedByFullscreenWindow()
|
||||
{
|
||||
var shellHandle = NativeMethods.GetShellWindow();
|
||||
var desktopHandle = NativeMethods.GetDesktopWindow();
|
||||
var foregroundHandle = NativeMethods.GetForegroundWindow();
|
||||
|
||||
// If the foreground window is either the desktop or the Windows shell, we are not in fullscreen mode.
|
||||
if (foregroundHandle.Equals(shellHandle) || foregroundHandle.Equals(desktopHandle))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (NativeMethods.SHQueryUserNotificationState(out var userNotificationState) != 0)
|
||||
{
|
||||
Logger.LogDebug($"SHQueryUserNotificationState failed with error : {Marshal.GetLastWin32Error()}");
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (userNotificationState)
|
||||
{
|
||||
// An application running in full screen mode, check if the foreground window is
|
||||
// listed as ignored in the settings.
|
||||
case NativeMethods.USER_NOTIFICATION_STATE.BUSY:
|
||||
case NativeMethods.USER_NOTIFICATION_STATE.RUNNING_D3D_FULL_SCREEN:
|
||||
case NativeMethods.USER_NOTIFICATION_STATE.PRESENTATION_MODE:
|
||||
return !IsAppIgnoredByEasyMouseFullscreenCheck(foregroundHandle);
|
||||
|
||||
// No full screen app running.
|
||||
case NativeMethods.USER_NOTIFICATION_STATE.NOT_PRESENT:
|
||||
case NativeMethods.USER_NOTIFICATION_STATE.ACCEPTS_NOTIFICATIONS:
|
||||
case NativeMethods.USER_NOTIFICATION_STATE.QUIET_TIME:
|
||||
// Cannot determine
|
||||
case NativeMethods.USER_NOTIFICATION_STATE.APP:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a machine switch triggered by EasyMouse would be allowed to proceed due to other settings.
|
||||
/// </summary>
|
||||
/// <returns>A boolean that tells us if the switch isn't blocked by any other settings</returns>
|
||||
internal static bool IsEasyMouseSwitchAllowed()
|
||||
{
|
||||
// Never prevent a switch if we are not moving out of the host machine.
|
||||
if (!DisableEasyMouseWhenForegroundWindowIsFullscreenSetting() || DesMachineID != MachineID)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if the switch is blocked by a full-screen window running in the foreground
|
||||
return !IsEasyMouseBlockedByFullscreenWindow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,9 +122,16 @@ namespace MouseWithoutBorders.Class
|
||||
[DllImport("user32.dll", SetLastError = false)]
|
||||
internal static extern IntPtr GetDesktopWindow();
|
||||
|
||||
[LibraryImport("user32.dll")]
|
||||
internal static partial IntPtr GetShellWindow();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern IntPtr GetWindowDC(IntPtr hWnd);
|
||||
|
||||
[LibraryImport("user32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static partial bool GetWindowRect(IntPtr hWnd, out RECT rect);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
||||
internal static extern int DrawText(IntPtr hDC, string lpString, int nCount, ref RECT lpRect, uint uFormat);
|
||||
|
||||
@@ -291,6 +298,17 @@ namespace MouseWithoutBorders.Class
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
||||
|
||||
[LibraryImport("kernel32.dll",
|
||||
EntryPoint = "QueryFullProcessImageNameW",
|
||||
SetLastError = true,
|
||||
StringMarshalling = StringMarshalling.Utf16)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static partial bool QueryFullProcessImageName(
|
||||
IntPtr hProcess, QUERY_FULL_PROCESS_NAME_FLAGS dwFlags, [Out] char[] lpExeName, ref uint lpdwSize);
|
||||
|
||||
[LibraryImport("shell32.dll", SetLastError = true)]
|
||||
internal static partial int SHQueryUserNotificationState(out USER_NOTIFICATION_STATE state);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct POINT
|
||||
{
|
||||
@@ -333,11 +351,11 @@ namespace MouseWithoutBorders.Class
|
||||
|
||||
[DllImport("ntdll.dll")]
|
||||
internal static extern int NtQueryInformationProcess(
|
||||
IntPtr hProcess,
|
||||
int processInformationClass /* 0 */,
|
||||
ref PROCESS_BASIC_INFORMATION processBasicInformation,
|
||||
uint processInformationLength,
|
||||
out uint returnLength);
|
||||
IntPtr hProcess,
|
||||
int processInformationClass /* 0 */,
|
||||
ref PROCESS_BASIC_INFORMATION processBasicInformation,
|
||||
uint processInformationLength,
|
||||
out uint returnLength);
|
||||
#endif
|
||||
|
||||
#if USE_GetSecurityDescriptorSacl
|
||||
@@ -632,14 +650,14 @@ namespace MouseWithoutBorders.Class
|
||||
{
|
||||
internal int LowPart;
|
||||
internal int HighPart;
|
||||
}// end struct
|
||||
} // end struct
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct LUID_AND_ATTRIBUTES
|
||||
{
|
||||
internal LUID Luid;
|
||||
internal int Attributes;
|
||||
}// end struct
|
||||
} // end struct
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct TOKEN_PRIVILEGES
|
||||
@@ -670,23 +688,23 @@ namespace MouseWithoutBorders.Class
|
||||
internal const int TOKEN_ADJUST_SESSIONID = 0x0100;
|
||||
|
||||
internal const int TOKEN_ALL_ACCESS_P = STANDARD_RIGHTS_REQUIRED |
|
||||
TOKEN_ASSIGN_PRIMARY |
|
||||
TOKEN_DUPLICATE |
|
||||
TOKEN_IMPERSONATE |
|
||||
TOKEN_QUERY |
|
||||
TOKEN_QUERY_SOURCE |
|
||||
TOKEN_ADJUST_PRIVILEGES |
|
||||
TOKEN_ADJUST_GROUPS |
|
||||
TOKEN_ADJUST_DEFAULT;
|
||||
TOKEN_ASSIGN_PRIMARY |
|
||||
TOKEN_DUPLICATE |
|
||||
TOKEN_IMPERSONATE |
|
||||
TOKEN_QUERY |
|
||||
TOKEN_QUERY_SOURCE |
|
||||
TOKEN_ADJUST_PRIVILEGES |
|
||||
TOKEN_ADJUST_GROUPS |
|
||||
TOKEN_ADJUST_DEFAULT;
|
||||
|
||||
internal const int TOKEN_ALL_ACCESS = TOKEN_ALL_ACCESS_P | TOKEN_ADJUST_SESSIONID;
|
||||
|
||||
internal const int TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY;
|
||||
|
||||
internal const int TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
|
||||
TOKEN_ADJUST_PRIVILEGES |
|
||||
TOKEN_ADJUST_GROUPS |
|
||||
TOKEN_ADJUST_DEFAULT;
|
||||
TOKEN_ADJUST_PRIVILEGES |
|
||||
TOKEN_ADJUST_GROUPS |
|
||||
TOKEN_ADJUST_DEFAULT;
|
||||
|
||||
internal const int TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE;
|
||||
|
||||
@@ -940,6 +958,30 @@ namespace MouseWithoutBorders.Class
|
||||
NameDnsDomain = 12,
|
||||
}
|
||||
|
||||
internal enum MONITOR_FROM_WINDOW_FLAGS : uint
|
||||
{
|
||||
DEFAULT_TO_NULL = 0x00000000,
|
||||
DEFAULT_TO_PRIMARY = 0x00000001,
|
||||
DEFAULT_TO_NEAREST = 0x00000002,
|
||||
}
|
||||
|
||||
internal enum QUERY_FULL_PROCESS_NAME_FLAGS : uint
|
||||
{
|
||||
DEFAULT = 0x00000000,
|
||||
PROCESS_NAME_NATIVE = 0x00000001,
|
||||
}
|
||||
|
||||
internal enum USER_NOTIFICATION_STATE
|
||||
{
|
||||
NOT_PRESENT = 1,
|
||||
BUSY = 2,
|
||||
RUNNING_D3D_FULL_SCREEN = 3,
|
||||
PRESENTATION_MODE = 4,
|
||||
ACCEPTS_NOTIFICATIONS = 5,
|
||||
QUIET_TIME = 6,
|
||||
APP = 7,
|
||||
}
|
||||
|
||||
[DllImport("secur32.dll", CharSet = CharSet.Unicode)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
internal static extern bool GetUserNameEx(int nameFormat, StringBuilder userName, ref uint userNameSize);
|
||||
|
||||
@@ -414,6 +414,44 @@ namespace MouseWithoutBorders.Class
|
||||
}
|
||||
}
|
||||
|
||||
internal bool DisableEasyMouseWhenForegroundWindowIsFullscreen
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_loadingSettingsLock)
|
||||
{
|
||||
return _properties.DisableEasyMouseWhenForegroundWindowIsFullscreen;
|
||||
}
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
lock (_loadingSettingsLock)
|
||||
{
|
||||
_properties.DisableEasyMouseWhenForegroundWindowIsFullscreen = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal HashSet<string> EasyMouseFullscreenSwitchBlockExcludedApps
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_loadingSettingsLock)
|
||||
{
|
||||
return _properties.EasyMouseFullscreenSwitchBlockExcludedApps.Value;
|
||||
}
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
lock (_loadingSettingsLock)
|
||||
{
|
||||
_properties.EasyMouseFullscreenSwitchBlockExcludedApps.Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal string Enc(string st, bool dec, DataProtectionScope protectionScope)
|
||||
{
|
||||
if (st == null || st.Length < 1)
|
||||
|
||||
@@ -66,13 +66,17 @@ internal static class Event
|
||||
try
|
||||
{
|
||||
Common.PaintCount = 0;
|
||||
bool switchByMouseEnabled = IsSwitchingByMouseEnabled();
|
||||
|
||||
if (switchByMouseEnabled && Common.Sk != null && (Common.DesMachineID == Common.MachineID || !Setting.Values.MoveMouseRelatively) && e.dwFlags == Common.WM_MOUSEMOVE)
|
||||
// Check if easy mouse setting is enabled.
|
||||
bool isEasyMouseEnabled = IsSwitchingByMouseEnabled();
|
||||
|
||||
if (isEasyMouseEnabled && Common.Sk != null && (Common.DesMachineID == Common.MachineID || !Setting.Values.MoveMouseRelatively) && e.dwFlags == Common.WM_MOUSEMOVE)
|
||||
{
|
||||
Point p = MachineStuff.MoveToMyNeighbourIfNeeded(e.X, e.Y, MachineStuff.desMachineID);
|
||||
|
||||
if (!p.IsEmpty)
|
||||
// Check if easy mouse switches are disabled when an application is running in fullscreen mode,
|
||||
// if they are, check that there is no application running in fullscreen mode before switching.
|
||||
if (!p.IsEmpty && Common.IsEasyMouseSwitchAllowed())
|
||||
{
|
||||
Common.HasSwitchedMachineSinceLastCopy = true;
|
||||
|
||||
@@ -165,7 +169,8 @@ internal static class Event
|
||||
string newDesMachineName = MachineStuff.NameFromID(newDesMachineID);
|
||||
|
||||
if (!Common.IsConnectedTo(newDesMachineID))
|
||||
{// Connection lost, cancel switching
|
||||
{
|
||||
// Connection lost, cancel switching
|
||||
Logger.LogDebug("No active connection found for " + newDesMachineName);
|
||||
|
||||
// ShowToolTip("No active connection found for [" + newDesMachineName + "]!", 500);
|
||||
|
||||
Reference in New Issue
Block a user