[MWB] - refactoring "Common" classes (Part 4) (#37579)

* [MouseWithoutBorders] - moving Common.Event.cs -> Core\Event.cs - #35155

* [MouseWithoutBorders] - moving Common.Service.cs -> Core\Service.cs - #35155

* [MouseWithoutBorders] - moving Common.Launch.cs -> Core\Launch.cs - #35155

* [MouseWithoutBorders] - moving Common.Helper.cs -> Core\Helper.cs - #35155

* [MouseWithoutBorders] - refactoring and fixes for logger unit test - #35155

* [MouseWithoutBorders] - cleaning up changes - #35155

* [MouseWithoutBorders] - re-[Ignore]-ing test - #35155
This commit is contained in:
Michael Clayton
2025-03-17 22:05:36 +00:00
committed by GitHub
parent 0e62e2ddd4
commit c89280cd9e
26 changed files with 1520 additions and 1496 deletions

View File

@@ -153,7 +153,7 @@ namespace MouseWithoutBorders
string filePath = stringData;
_ = Common.ImpersonateLoggedOnUserAndDoSomething(() =>
_ = Launch.ImpersonateLoggedOnUserAndDoSomething(() =>
{
if (File.Exists(filePath) || Directory.Exists(filePath))
{
@@ -579,7 +579,7 @@ namespace MouseWithoutBorders
{
if (postAct.Equals("desktop", StringComparison.OrdinalIgnoreCase))
{
_ = ImpersonateLoggedOnUserAndDoSomething(() =>
_ = Launch.ImpersonateLoggedOnUserAndDoSomething(() =>
{
savingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\MouseWithoutBorders\\";
@@ -696,7 +696,7 @@ namespace MouseWithoutBorders
Path.GetFileName(fileName),
remoteMachine);
_ = ImpersonateLoggedOnUserAndDoSomething(() =>
_ = Launch.ImpersonateLoggedOnUserAndDoSomething(() =>
{
ProcessStartInfo startInfo = new();
startInfo.UseShellExecute = true;

View File

@@ -1,277 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
// <summary>
// Keyboard/Mouse hook callback implementation.
// </summary>
// <history>
// 2008 created by Truong Do (ductdo).
// 2009-... modified by Truong Do (TruongDo).
// 2023- Included in PowerToys.
// </history>
using MouseWithoutBorders.Class;
using MouseWithoutBorders.Core;
using MouseWithoutBorders.Form;
using Thread = MouseWithoutBorders.Core.Thread;
namespace MouseWithoutBorders
{
internal partial class Common
{
private static readonly DATA KeybdPackage = new();
private static readonly DATA MousePackage = new();
#pragma warning disable SA1307 // Accessible fields should begin with upper-case names
internal static ulong inputEventCount;
internal static ulong invalidPackageCount;
#pragma warning restore SA1307
internal static int MOVE_MOUSE_RELATIVE = 100000;
internal static int XY_BY_PIXEL = 300000;
static Common()
{
}
internal static ulong InvalidPackageCount
{
get => Common.invalidPackageCount;
set => Common.invalidPackageCount = value;
}
internal static ulong InputEventCount
{
get => Common.inputEventCount;
set => Common.inputEventCount = value;
}
internal static ulong RealInputEventCount
{
get;
set;
}
private static Point actualLastPos;
private static int myLastX;
private static int myLastY;
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Dotnet port with style preservation")]
internal static void MouseEvent(MOUSEDATA e, int dx, int dy)
{
try
{
PaintCount = 0;
bool switchByMouseEnabled = IsSwitchingByMouseEnabled();
if (switchByMouseEnabled && Sk != null && (DesMachineID == MachineID || !Setting.Values.MoveMouseRelatively) && e.dwFlags == WM_MOUSEMOVE)
{
Point p = MachineStuff.MoveToMyNeighbourIfNeeded(e.X, e.Y, MachineStuff.desMachineID);
if (!p.IsEmpty)
{
HasSwitchedMachineSinceLastCopy = true;
Logger.LogDebug(string.Format(
CultureInfo.CurrentCulture,
"***** Host Machine: newDesMachineIdEx set = [{0}]. Mouse is now at ({1},{2})",
MachineStuff.newDesMachineIdEx,
e.X,
e.Y));
myLastX = e.X;
myLastY = e.Y;
PrepareToSwitchToMachine(MachineStuff.newDesMachineIdEx, p);
}
}
if (MachineStuff.desMachineID != MachineID && MachineStuff.SwitchLocation.Count <= 0)
{
MousePackage.Des = MachineStuff.desMachineID;
MousePackage.Type = PackageType.Mouse;
MousePackage.Md.dwFlags = e.dwFlags;
MousePackage.Md.WheelDelta = e.WheelDelta;
// Relative move
if (Setting.Values.MoveMouseRelatively && Math.Abs(dx) >= MOVE_MOUSE_RELATIVE && Math.Abs(dy) >= MOVE_MOUSE_RELATIVE)
{
MousePackage.Md.X = dx;
MousePackage.Md.Y = dy;
}
else
{
MousePackage.Md.X = (e.X - MachineStuff.primaryScreenBounds.Left) * 65535 / screenWidth;
MousePackage.Md.Y = (e.Y - MachineStuff.primaryScreenBounds.Top) * 65535 / screenHeight;
}
SkSend(MousePackage, null, false);
if (MousePackage.Md.dwFlags is WM_LBUTTONUP or WM_RBUTTONUP)
{
Thread.Sleep(10);
}
NativeMethods.GetCursorPos(ref actualLastPos);
if (actualLastPos != Common.LastPos)
{
Logger.LogDebug($"Mouse cursor has moved unexpectedly: Expected: {Common.LastPos}, actual: {actualLastPos}.");
Common.LastPos = actualLastPos;
}
}
#if SHOW_ON_WINLOGON_EX
if (RunOnLogonDesktop && e.dwFlags == WM_RBUTTONUP &&
desMachineID == machineID &&
e.x > 2 && e.x < 100 && e.y > 2 && e.y < 20)
{
DoSomethingInUIThread(delegate()
{
MainForm.HideMenuWhenRunOnLogonDesktop();
MainForm.MainMenu.Hide();
MainForm.MainMenu.Show(e.x - 5, e.y - 3);
});
}
#endif
}
catch (Exception ex)
{
Logger.Log(ex);
}
}
internal static bool IsSwitchingByMouseEnabled()
{
return (EasyMouseOption)Setting.Values.EasyMouse == EasyMouseOption.Enable || InputHook.EasyMouseKeyDown;
}
internal static void PrepareToSwitchToMachine(ID newDesMachineID, Point desMachineXY)
{
Logger.LogDebug($"PrepareToSwitchToMachine: newDesMachineID = {newDesMachineID}, desMachineXY = {desMachineXY}");
if (((GetTick() - MachineStuff.lastJump < 100) && (GetTick() - MachineStuff.lastJump > 0)) || MachineStuff.desMachineID == ID.ALL)
{
Logger.LogDebug("PrepareToSwitchToMachine: lastJump");
return;
}
MachineStuff.lastJump = GetTick();
string newDesMachineName = MachineStuff.NameFromID(newDesMachineID);
if (!IsConnectedTo(newDesMachineID))
{// Connection lost, cancel switching
Logger.LogDebug("No active connection found for " + newDesMachineName);
// ShowToolTip("No active connection found for [" + newDesMachineName + "]!", 500);
}
else
{
MachineStuff.newDesMachineID = newDesMachineID;
MachineStuff.SwitchLocation.X = desMachineXY.X;
MachineStuff.SwitchLocation.Y = desMachineXY.Y;
MachineStuff.SwitchLocation.ResetCount();
_ = EvSwitch.Set();
// PostMessage(mainForm.Handle, WM_SWITCH, IntPtr.Zero, IntPtr.Zero);
if (newDesMachineID != DragDrop.DragMachine)
{
if (!DragDrop.IsDragging && !DragDrop.IsDropping)
{
if (DragDrop.MouseDown && !RunOnLogonDesktop && !RunOnScrSaverDesktop)
{
DragDrop.DragDropStep02();
}
}
else if (DragDrop.DragMachine != (ID)1)
{
DragDrop.ChangeDropMachine();
}
}
else
{
DragDrop.DragDropStep11();
}
// Change des machine
if (MachineStuff.desMachineID != newDesMachineID)
{
Logger.LogDebug("MouseEvent: Switching to new machine:" + newDesMachineName);
// Ask current machine to hide the Mouse cursor
if (newDesMachineID != ID.ALL && MachineStuff.desMachineID != MachineID)
{
SendPackage(MachineStuff.desMachineID, PackageType.HideMouse);
}
DesMachineID = newDesMachineID;
if (MachineStuff.desMachineID == MachineID)
{
if (GetTick() - clipboardCopiedTime < BIG_CLIPBOARD_DATA_TIMEOUT)
{
clipboardCopiedTime = 0;
Common.GetRemoteClipboard("PrepareToSwitchToMachine");
}
}
else
{
// Ask the new active machine to get clipboard data (if the data is too big)
SendPackage(MachineStuff.desMachineID, PackageType.MachineSwitched);
}
_ = Interlocked.Increment(ref switchCount);
}
}
}
internal static void SaveSwitchCount()
{
if (SwitchCount > 0)
{
_ = Task.Run(() =>
{
Setting.Values.SwitchCount += SwitchCount;
_ = Interlocked.Exchange(ref switchCount, 0);
});
}
}
internal static void KeybdEvent(KEYBDDATA e)
{
try
{
PaintCount = 0;
if (MachineStuff.desMachineID != MachineStuff.newDesMachineID)
{
Logger.LogDebug("KeybdEvent: Switching to new machine...");
DesMachineID = MachineStuff.newDesMachineID;
}
if (MachineStuff.desMachineID != MachineID)
{
KeybdPackage.Des = MachineStuff.desMachineID;
KeybdPackage.Type = PackageType.Keyboard;
KeybdPackage.Kd = e;
KeybdPackage.DateTime = GetTick();
SkSend(KeybdPackage, null, false);
if (KeybdPackage.Kd.dwFlags is WM_KEYUP or WM_SYSKEYUP)
{
Thread.Sleep(10);
}
}
}
catch (Exception ex)
{
Logger.Log(ex);
}
}
}
}

View File

@@ -1,526 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;
// <summary>
// Some other helper methods.
// </summary>
// <history>
// 2008 created by Truong Do (ductdo).
// 2009-... modified by Truong Do (TruongDo).
// 2023- Included in PowerToys.
// </history>
using Microsoft.Win32;
using MouseWithoutBorders.Class;
using MouseWithoutBorders.Core;
using static System.Windows.Forms.Control;
namespace MouseWithoutBorders
{
internal partial class Common
{
internal const string HELPER_FORM_TEXT = "Mouse without Borders Helper";
internal const string HelperProcessName = "PowerToys.MouseWithoutBordersHelper";
private static bool signalHelperToExit;
private static bool signalWatchDogToExit;
internal static long WndProcCounter;
private static void WatchDogThread()
{
long oldCounter = WndProcCounter;
do
{
for (int i = 0; i < 10; i++)
{
Thread.Sleep(1000);
if (signalWatchDogToExit)
{
break;
}
}
while (BlockingUI)
{
Thread.Sleep(1000);
}
if (WndProcCounter == oldCounter)
{
Process p = Process.GetCurrentProcess();
string procInfo = $"{p.PrivateMemorySize64 / 1024 / 1024}MB, {p.TotalProcessorTime}, {Environment.ProcessorCount}.";
string threadStacks = $"{procInfo} {Thread.DumpThreadsStack()}";
Logger.TelemetryLogTrace(threadStacks, SeverityLevel.Error);
break;
}
oldCounter = WndProcCounter;
}
while (true);
}
private static void HelperThread()
{
// SuppressFlow fixes an issue on service mode, where the helper process can't get enough permissions to be started again.
// More details can be found on: https://github.com/microsoft/PowerToys/pull/36892
using var asyncFlowControl = System.Threading.ExecutionContext.SuppressFlow();
try
{
while (true)
{
_ = EvSwitch.WaitOne(); // Switching to another machine?
if (signalHelperToExit)
{
break;
}
if (MachineStuff.NewDesMachineID != Common.MachineID && MachineStuff.NewDesMachineID != ID.ALL)
{
HideMouseCursor(false);
Common.MainFormDotEx(true);
}
else
{
if (MachineStuff.SwitchLocation.Count > 0)
{
MachineStuff.SwitchLocation.Count--;
// When we want to move mouse by pixels, we add 300k to x and y (search for XY_BY_PIXEL for other related code).
Logger.LogDebug($"+++++ Moving mouse to {MachineStuff.SwitchLocation.X}, {MachineStuff.SwitchLocation.Y}");
// MaxXY = 65535 so 100k is safe.
if (MachineStuff.SwitchLocation.X > XY_BY_PIXEL - 100000 || MachineStuff.SwitchLocation.Y > XY_BY_PIXEL - 100000)
{
InputSimulation.MoveMouse(MachineStuff.SwitchLocation.X - XY_BY_PIXEL, MachineStuff.SwitchLocation.Y - XY_BY_PIXEL);
}
else
{
InputSimulation.MoveMouseEx(MachineStuff.SwitchLocation.X, MachineStuff.SwitchLocation.Y);
}
Common.MainFormDot();
}
}
if (MachineStuff.NewDesMachineID == Common.MachineID)
{
ReleaseAllKeys();
}
}
}
catch (Exception e)
{
Logger.Log(e);
}
signalHelperToExit = false;
Logger.LogDebug("^^^Helper Thread exiting...^^^");
}
internal static void MainFormDotEx(bool bCheckTS)
{
Logger.LogDebug("***** MainFormDotEx:");
if (!Common.RunOnLogonDesktop && !Common.RunOnScrSaverDesktop)
{
int left = MachineStuff.PrimaryScreenBounds.Left + ((MachineStuff.PrimaryScreenBounds.Right - MachineStuff.PrimaryScreenBounds.Left) / 2) - 1;
int top = Setting.Values.HideMouse ? 3 : MachineStuff.PrimaryScreenBounds.Top + ((MachineStuff.PrimaryScreenBounds.Bottom - MachineStuff.PrimaryScreenBounds.Top) / 2);
Common.MainFormVisible = true;
if (Setting.Values.HideMouse && Setting.Values.StealFocusWhenSwitchingMachine && Common.SendMessageToHelper(0x407, new IntPtr(left), new IntPtr(top), true) == 0)
{
try
{
/* When user just switches to the Logon desktop, user is actually on the "Windows Default Lock Screen" (LockApp).
* If a click is sent to this during switch, it actually triggers a desktop switch on the local machine causing a reconnection affecting the machine switch.
* We can detect and skip in this case.
* */
IntPtr foreGroundWindow = NativeMethods.GetForegroundWindow();
string foreGroundWindowText = GetText(foreGroundWindow);
bool mouseClick = true;
if (foreGroundWindowText.Equals("Windows Default Lock Screen", StringComparison.OrdinalIgnoreCase))
{
mouseClick = false;
}
// Window title may be localized, check process name:
if (mouseClick)
{
_ = NativeMethods.GetWindowThreadProcessId(foreGroundWindow, out uint pid);
if (pid > 0)
{
string foreGroundWindowProcess = Process.GetProcessById((int)pid)?.ProcessName;
if (foreGroundWindowProcess.Equals("LockApp", StringComparison.OrdinalIgnoreCase))
{
mouseClick = false;
}
}
}
if (mouseClick)
{
InputSimulation.MouseClickDotForm(left + 1, top + 1);
}
}
catch (Exception e)
{
Logger.Log(e);
}
}
}
CustomCursor.ShowFakeMouseCursor(int.MinValue, int.MinValue);
}
internal static void MainForm3Pixels()
{
Logger.LogDebug("***** MainFormDotLarge:");
DoSomethingInUIThread(
() =>
{
MainForm.Left = MachineStuff.PrimaryScreenBounds.Left + ((MachineStuff.PrimaryScreenBounds.Right - MachineStuff.PrimaryScreenBounds.Left) / 2) - 2;
MainForm.Top = Setting.Values.HideMouse ? 3 : MachineStuff.PrimaryScreenBounds.Top + ((MachineStuff.PrimaryScreenBounds.Bottom - MachineStuff.PrimaryScreenBounds.Top) / 2) - 1;
MainForm.Width = 3;
MainForm.Height = 3;
MainForm.Opacity = 0.11D;
MainForm.TopMost = true;
if (Setting.Values.HideMouse)
{
MainForm.BackColor = Color.Black;
MainForm.Show();
Common.MainFormVisible = true;
}
else
{
MainForm.BackColor = Color.White;
MainForm.Hide();
Common.MainFormVisible = false;
}
},
true);
CustomCursor.ShowFakeMouseCursor(int.MinValue, int.MinValue);
}
internal static void MainFormDot()
{
DoSomethingInUIThread(
() =>
{
_ = Common.SendMessageToHelper(0x408, IntPtr.Zero, IntPtr.Zero, false);
MainForm.Left = MachineStuff.PrimaryScreenBounds.Left + ((MachineStuff.PrimaryScreenBounds.Right - MachineStuff.PrimaryScreenBounds.Left) / 2) - 1;
MainForm.Top = Setting.Values.HideMouse ? 3 : MachineStuff.PrimaryScreenBounds.Top + ((MachineStuff.PrimaryScreenBounds.Bottom - MachineStuff.PrimaryScreenBounds.Top) / 2);
MainForm.Width = 1;
MainForm.Height = 1;
MainForm.Opacity = 0.15;
MainForm.Hide();
Common.MainFormVisible = false;
},
true);
CustomCursor.ShowFakeMouseCursor(int.MinValue, int.MinValue);
}
internal static void ToggleIcon()
{
try
{
if (toggleIconsIndex < TOGGLE_ICONS_SIZE)
{
Common.DoSomethingInUIThread(() => Common.MainForm.ChangeIcon(toggleIcons[toggleIconsIndex++]));
}
else
{
toggleIconsIndex = 0;
toggleIcons = null;
}
}
catch (Exception e)
{
Logger.Log(e);
}
}
internal static void RunDDHelper(bool cleanUp = false)
{
if (Common.RunOnLogonDesktop || Common.RunOnScrSaverDesktop)
{
return;
}
if (cleanUp)
{
try
{
Process[] ps = Process.GetProcessesByName(HelperProcessName);
foreach (Process p in ps)
{
p.KillProcess();
}
}
catch (Exception e)
{
Logger.Log(e);
_ = Common.SendMessageToHelper(SharedConst.QUIT_CMD, IntPtr.Zero, IntPtr.Zero);
}
return;
}
if (!Common.IsMyDesktopActive())
{
return;
}
if (!Common.IpcChannelCreated)
{
Logger.TelemetryLogTrace($"{nameof(Common.IpcChannelCreated)} = {Common.IpcChannelCreated}. {Logger.GetStackTrace(new StackTrace())}", SeverityLevel.Warning);
return;
}
if (!MainForm.IsDisposed)
{
MainForm.NotifyIcon.Visible = false;
MainForm.NotifyIcon.Visible = Setting.Values.ShowOriginalUI;
}
IntPtr h = (IntPtr)NativeMethods.FindWindow(null, Common.HELPER_FORM_TEXT);
if (h.ToInt32() <= 0)
{
_ = Common.CreateProcessInInputDesktopSession(
$"\"{Path.GetDirectoryName(Application.ExecutablePath)}\\{HelperProcessName}.exe\"",
string.Empty,
Common.GetInputDesktop(),
0);
HasSwitchedMachineSinceLastCopy = true;
// Common.CreateLowIntegrityProcess("\"" + Path.GetDirectoryName(Application.ExecutablePath) + "\\MouseWithoutBordersHelper.exe\"", string.Empty, 0, false, 0);
var processes = Process.GetProcessesByName(HelperProcessName);
if (processes?.Length == 0)
{
Logger.Log("Unable to start helper process.");
Common.ShowToolTip("Error starting Mouse Without Borders Helper, clipboard sharing will not work!", 5000, ToolTipIcon.Error);
}
else
{
Logger.Log("Helper process started.");
}
}
else
{
var processes = Process.GetProcessesByName(HelperProcessName);
if (processes?.Length > 0)
{
Logger.Log("Helper process found running.");
}
else
{
Logger.Log("Invalid helper process found running.");
Common.ShowToolTip("Error finding Mouse Without Borders Helper, clipboard sharing will not work!", 5000, ToolTipIcon.Error);
}
}
}
internal static int SendMessageToHelper(int msg, IntPtr wparam, IntPtr lparam, bool wait = true, bool log = true)
{
int h = NativeMethods.FindWindow(null, Common.HELPER_FORM_TEXT);
int rv = -1;
if (h > 0)
{
rv = wait
? (int)NativeMethods.SendMessage((IntPtr)h, msg, wparam, lparam)
: NativeMethods.PostMessage((IntPtr)h, msg, wparam, lparam) ? 1 : 0;
}
if (log)
{
Logger.LogDebug($"SendMessageToHelper: HelperWindow={h}, Return={rv}, msg={msg}, w={wparam.ToInt32()}, l={lparam.ToInt32()}, Post={!wait}");
}
return rv;
}
internal static bool IsWindows8AndUp()
{
return (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 2)
|| Environment.OSVersion.Version.Major > 6;
}
internal static string GetMiniLog(IEnumerable<ControlCollection> optionControls)
{
string log = string.Empty;
log += "=============================================================================================================================\r\n";
log += $"{Application.ProductName} version {Application.ProductVersion}\r\n";
log += $"{Setting.Values.Username}/{GetDebugInfo(MyKey)}\r\n";
log += $"{MachineName}/{MachineID}/{DesMachineID}\r\n";
log += $"Id: {Setting.Values.DeviceId}\r\n";
log += $"Matrix: {string.Join(",", MachineStuff.MachineMatrix)}\r\n";
log += $"McPool: {Setting.Values.MachinePoolString}\r\n";
log += "\r\nOPTIONS:\r\n";
foreach (ControlCollection controlCollection in optionControls)
{
foreach (object c in controlCollection)
{
if (c is CheckBox checkBox)
{
log += $"({(checkBox.Checked ? 1 : 0)}) {checkBox.Text}\r\n";
continue;
}
if (c is RadioButton radioButton)
{
log += $"({(radioButton.Checked ? 1 : 0)}) {radioButton.Name}.[{radioButton.Text}]\r\n";
continue;
}
if (c is ComboBox comboBox)
{
log += $"{comboBox.Name} = {comboBox.Text}\r\n";
continue;
}
}
}
log += "\r\n";
SocketStuff sk = Sk;
if (sk?.TcpSockets != null)
{
foreach (TcpSk tcp in sk.TcpSockets)
{
log += $"{Common.MachineName}{(tcp.IsClient ? "=>" : "<=")}{tcp.MachineName}({tcp.MachineId}):{tcp.Status}\r\n";
}
}
log += string.Format(CultureInfo.CurrentCulture, "Helper:{0}\r\n", SendMessageToHelper(0x400, IntPtr.Zero, IntPtr.Zero));
log += Setting.Values.LastPersonalizeLogonScr + "\r\n";
log += "Name2IP =\r\n" + Setting.Values.Name2IP + "\r\n";
log += "Last 10 trace messages:\r\n";
log += string.Join(Environment.NewLine, Logger.LogCounter.Select(item => $"({item.Value}): {item.Key}").Take(10));
log += "\r\n=============================================================================================================================";
return log;
}
internal static bool GetUserName()
{
if (string.IsNullOrEmpty(Setting.Values.Username) && !Common.RunOnLogonDesktop)
{
if (Program.User.Contains("system", StringComparison.CurrentCultureIgnoreCase))
{
_ = Common.ImpersonateLoggedOnUserAndDoSomething(() =>
{
// See: https://stackoverflow.com/questions/19487541/how-to-get-windows-user-name-from-sessionid
static string GetUsernameBySessionId(int sessionId)
{
string username = "SYSTEM";
if (NativeMethods.WTSQuerySessionInformation(IntPtr.Zero, sessionId, NativeMethods.WTSInfoClass.WTSUserName, out nint buffer, out int strLen) && strLen > 1)
{
username = Marshal.PtrToStringAnsi(buffer);
NativeMethods.WTSFreeMemory(buffer);
if (NativeMethods.WTSQuerySessionInformation(IntPtr.Zero, sessionId, NativeMethods.WTSInfoClass.WTSDomainName, out buffer, out strLen) && strLen > 1)
{
username = @$"{Marshal.PtrToStringAnsi(buffer)}\{username}";
NativeMethods.WTSFreeMemory(buffer);
}
}
return username;
}
// The most direct way to fetch the username is WindowsIdentity.GetCurrent(true).Name
// but GetUserName can run within an ExecutionContext.SuppressFlow block, which creates issues
// with WindowsIdentity.GetCurrent.
// See: https://stackoverflow.com/questions/76998988/exception-when-using-executioncontext-suppressflow-in-net-7
// So we use WTSQuerySessionInformation as a workaround.
Setting.Values.Username = GetUsernameBySessionId(Process.GetCurrentProcess().SessionId);
});
}
else
{
Setting.Values.Username = Program.User;
}
Logger.LogDebug("[Username] = " + Setting.Values.Username);
}
return !string.IsNullOrEmpty(Setting.Values.Username);
}
internal static void ShowOneWayModeMessage()
{
ToggleShowTopMostMessage(
@"
Due to Security Controls, a remote device cannot control a SAW device.
Please use the keyboard and Mouse from the SAW device.
(Press Esc to hide this message)
",
string.Empty,
10);
}
internal static void ApplyCADSetting()
{
try
{
if (Setting.Values.DisableCAD)
{
RegistryKey k = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System");
if (k != null)
{
k.SetValue("DisableCAD", 1, RegistryValueKind.DWord);
k.Close();
}
}
else
{
RegistryKey k = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System");
if (k != null)
{
k.SetValue("DisableCAD", 0, RegistryValueKind.DWord);
k.Close();
}
}
}
catch (Exception e)
{
Logger.Log(e);
}
}
}
}

View File

@@ -93,7 +93,7 @@ namespace MouseWithoutBorders
internal static void Init()
{
_ = Common.GetUserName();
_ = Helper.GetUserName();
Common.GeneratedKey = true;
try
@@ -148,7 +148,7 @@ namespace MouseWithoutBorders
private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
Common.WndProcCounter++;
Helper.WndProcCounter++;
if (e.Mode is PowerModes.Resume or PowerModes.Suspend)
{
@@ -167,21 +167,21 @@ namespace MouseWithoutBorders
watchDogThread.Start();
*/
helper = new Thread(new ThreadStart(HelperThread), "Helper Thread");
helper = new Thread(new ThreadStart(Helper.HelperThread), "Helper Thread");
helper.SetApartmentState(ApartmentState.STA);
helper.Start();
}
private static void AskHelperThreadsToExit(int waitTime)
{
signalHelperToExit = true;
signalWatchDogToExit = true;
Helper.signalHelperToExit = true;
Helper.signalWatchDogToExit = true;
_ = EvSwitch.Set();
int c = 0;
if (helper != null && c < waitTime)
{
while (signalHelperToExit)
while (Helper.signalHelperToExit)
{
Thread.Sleep(1);
}
@@ -251,7 +251,7 @@ namespace MouseWithoutBorders
private static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
Logger.LogDebug("NetworkAvailabilityEventArgs.IsAvailable: " + e.IsAvailable.ToString(CultureInfo.InvariantCulture));
Common.WndProcCounter++;
Helper.WndProcCounter++;
ScheduleReopenSocketsDueToNetworkChanges(!e.IsAvailable);
}

View File

@@ -1,314 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security.Principal;
// <summary>
// Impersonation.
// </summary>
// <history>
// 2008 created by Truong Do (ductdo).
// 2009-... modified by Truong Do (TruongDo).
// 2023- Included in PowerToys.
// </history>
using MouseWithoutBorders.Class;
using MouseWithoutBorders.Core;
namespace MouseWithoutBorders
{
internal partial class Common
{
internal static bool RunElevated()
{
return WindowsIdentity.GetCurrent().Owner.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid);
}
internal static bool ImpersonateLoggedOnUserAndDoSomething(Action targetFunc)
{
if (Common.RunWithNoAdminRight)
{
targetFunc();
return true;
}
else
{
// SuppressFlow fixes an issue on service mode, where WTSQueryUserToken runs successfully once and then fails
// on subsequent calls. The reason appears to be an unknown issue with reverting the impersonation,
// meaning that subsequent impersonation attempts run as the logged-on user and fail.
// This is a workaround.
using var asyncFlowControl = System.Threading.ExecutionContext.SuppressFlow();
uint dwSessionId;
IntPtr hUserToken = IntPtr.Zero, hUserTokenDup = IntPtr.Zero;
try
{
dwSessionId = (uint)Process.GetCurrentProcess().SessionId;
uint rv = NativeMethods.WTSQueryUserToken(dwSessionId, ref hUserToken);
var lastError = rv == 0 ? Marshal.GetLastWin32Error() : 0;
Logger.LogDebug($"{nameof(NativeMethods.WTSQueryUserToken)} returned {rv.ToString(CultureInfo.CurrentCulture)}");
if (rv == 0)
{
Logger.Log($"{nameof(NativeMethods.WTSQueryUserToken)} failed with: {lastError}.");
return false;
}
if (!NativeMethods.DuplicateToken(hUserToken, (int)NativeMethods.SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref hUserTokenDup))
{
Logger.TelemetryLogTrace($"{nameof(NativeMethods.DuplicateToken)} Failed! {Logger.GetStackTrace(new StackTrace())}", SeverityLevel.Warning);
_ = NativeMethods.CloseHandle(hUserToken);
_ = NativeMethods.CloseHandle(hUserTokenDup);
return false;
}
if (NativeMethods.ImpersonateLoggedOnUser(hUserTokenDup))
{
targetFunc();
_ = NativeMethods.RevertToSelf();
_ = NativeMethods.CloseHandle(hUserToken);
_ = NativeMethods.CloseHandle(hUserTokenDup);
return true;
}
else
{
Logger.Log("ImpersonateLoggedOnUser Failed!");
_ = NativeMethods.CloseHandle(hUserToken);
_ = NativeMethods.CloseHandle(hUserTokenDup);
return false;
}
}
catch (Exception e)
{
Logger.Log(e);
return false;
}
}
}
internal static int CreateProcessInInputDesktopSession(string commandLine, string arg, string desktop, short wShowWindow, bool lowIntegrity = false)
// As user who runs explorer.exe
{
if (!Program.User.Contains("system", StringComparison.InvariantCultureIgnoreCase))
{
ProcessStartInfo s = new(commandLine, arg);
s.WindowStyle = wShowWindow != 0 ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden;
Process p = Process.Start(s);
return p == null ? 0 : p.Id;
}
string commandLineWithArg = commandLine + " " + arg;
int lastError;
int dwSessionId;
IntPtr hUserToken = IntPtr.Zero, hUserTokenDup = IntPtr.Zero;
Logger.LogDebug("CreateProcessInInputDesktopSession called, launching " + commandLineWithArg + " on " + desktop);
try
{
dwSessionId = Process.GetCurrentProcess().SessionId;
// Get the user token used by DuplicateTokenEx
lastError = (int)NativeMethods.WTSQueryUserToken((uint)dwSessionId, ref hUserToken);
NativeMethods.STARTUPINFO si = default;
si.cb = Marshal.SizeOf(si);
si.lpDesktop = "winsta0\\" + desktop;
si.wShowWindow = wShowWindow;
NativeMethods.SECURITY_ATTRIBUTES sa = default;
sa.Length = Marshal.SizeOf(sa);
if (!NativeMethods.DuplicateTokenEx(hUserToken, NativeMethods.MAXIMUM_ALLOWED, ref sa, (int)NativeMethods.SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, (int)NativeMethods.TOKEN_TYPE.TokenPrimary, ref hUserTokenDup))
{
lastError = Marshal.GetLastWin32Error();
Logger.Log(string.Format(CultureInfo.CurrentCulture, "DuplicateTokenEx error: {0} Token does not have the privilege.", lastError));
_ = NativeMethods.CloseHandle(hUserToken);
return 0;
}
if (lowIntegrity)
{
NativeMethods.TOKEN_MANDATORY_LABEL tIL;
// Low
string sIntegritySid = "S-1-16-4096";
bool rv = NativeMethods.ConvertStringSidToSid(sIntegritySid, out IntPtr pIntegritySid);
if (!rv)
{
Logger.Log("ConvertStringSidToSid failed");
_ = NativeMethods.CloseHandle(hUserToken);
_ = NativeMethods.CloseHandle(hUserTokenDup);
return 0;
}
tIL.Label.Attributes = NativeMethods.SE_GROUP_INTEGRITY;
tIL.Label.Sid = pIntegritySid;
rv = NativeMethods.SetTokenInformation(hUserTokenDup, NativeMethods.TOKEN_INFORMATION_CLASS.TokenIntegrityLevel, ref tIL, (uint)Marshal.SizeOf(tIL) + (uint)IntPtr.Size);
if (!rv)
{
Logger.Log("SetTokenInformation failed");
_ = NativeMethods.CloseHandle(hUserToken);
_ = NativeMethods.CloseHandle(hUserTokenDup);
return 0;
}
}
uint dwCreationFlags = NativeMethods.NORMAL_PRIORITY_CLASS | NativeMethods.CREATE_NEW_CONSOLE;
IntPtr pEnv = IntPtr.Zero;
if (NativeMethods.CreateEnvironmentBlock(ref pEnv, hUserTokenDup, true))
{
dwCreationFlags |= NativeMethods.CREATE_UNICODE_ENVIRONMENT;
}
else
{
pEnv = IntPtr.Zero;
}
_ = NativeMethods.CreateProcessAsUser(
hUserTokenDup, // client's access token
null, // file to execute
commandLineWithArg, // command line
ref sa, // pointer to process SECURITY_ATTRIBUTES
ref sa, // pointer to thread SECURITY_ATTRIBUTES
false, // handles are not inheritable
(int)dwCreationFlags, // creation flags
pEnv, // pointer to new environment block
null, // name of current directory
ref si, // pointer to STARTUPINFO structure
out NativeMethods.PROCESS_INFORMATION pi); // receives information about new process
// GetLastError should be 0
int iResultOfCreateProcessAsUser = Marshal.GetLastWin32Error();
Logger.LogDebug("CreateProcessAsUser returned " + iResultOfCreateProcessAsUser.ToString(CultureInfo.CurrentCulture));
// Close handles task
_ = NativeMethods.CloseHandle(hUserToken);
_ = NativeMethods.CloseHandle(hUserTokenDup);
return (iResultOfCreateProcessAsUser == 0) ? (int)pi.dwProcessId : 0;
}
catch (Exception e)
{
Logger.Log(e);
return 0;
}
}
#if CUSTOMIZE_LOGON_SCREEN
internal static bool CreateLowIntegrityProcess(string commandLine, string args, int wait, bool killIfTimedOut, long limitedMem, short wShowWindow = 0)
{
int processId = CreateProcessInInputDesktopSession(commandLine, args, "default", wShowWindow, true);
if (processId <= 0)
{
return false;
}
if (wait > 0)
{
if (limitedMem > 0)
{
int sec = 0;
while (true)
{
Process p;
try
{
if ((p = Process.GetProcessById(processId)) == null)
{
Logger.Log("Process exited!");
break;
}
}
catch (ArgumentException)
{
Logger.Log("GetProcessById.ArgumentException");
break;
}
if ((!p.HasExited && p.PrivateMemorySize64 > limitedMem) || (++sec > (wait / 1000)))
{
Logger.Log(string.Format(CultureInfo.CurrentCulture, "Process log (mem): {0}, {1}", sec, p.PrivateMemorySize64));
return false;
}
Thread.Sleep(1000);
}
}
else
{
Process p;
if ((p = Process.GetProcessById(processId)) == null)
{
Logger.Log("Process exited!");
}
else if (NativeMethods.WaitForSingleObject(p.Handle, wait) != NativeMethods.WAIT_OBJECT_0 && killIfTimedOut)
{
Logger.Log("Process log (time).");
TerminateProcessTree(p.Handle, (uint)processId, -1);
return false;
}
}
}
return true;
}
internal static void TerminateProcessTree(IntPtr hProcess, uint processID, int exitCode)
{
if (processID > 0 && hProcess.ToInt32() > 0)
{
Process[] processes = Process.GetProcesses();
int dwSessionId = Process.GetCurrentProcess().SessionId;
foreach (Process p in processes)
{
if (p.SessionId == dwSessionId)
{
NativeMethods.PROCESS_BASIC_INFORMATION processBasicInformation = default;
try
{
if (NativeMethods.NtQueryInformationProcess(p.Handle, 0, ref processBasicInformation, (uint)Marshal.SizeOf(processBasicInformation), out uint bytesWritten) >= 0)
{// NT_SUCCESS(...)
if (processBasicInformation.InheritedFromUniqueProcessId == processID)
{
TerminateProcessTree(p.Handle, processBasicInformation.UniqueProcessId, exitCode);
}
}
}
catch (InvalidOperationException e)
{
Logger.Log(e);
continue;
}
catch (Win32Exception e)
{
Logger.Log(e);
continue;
}
}
}
_ = NativeMethods.TerminateProcess(hProcess, (IntPtr)exitCode);
}
}
#endif
}
}

View File

@@ -1,161 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.ServiceProcess;
using System.Threading.Tasks;
using System.Windows.Forms;
// <summary>
// Service control code.
// </summary>
// <history>
// 2008 created by Truong Do (ductdo).
// 2009-... modified by Truong Do (TruongDo).
// 2023- Included in PowerToys.
// </history>
using MouseWithoutBorders.Class;
using MouseWithoutBorders.Core;
[module: SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions", Scope = "member", Target = "MouseWithoutBorders.Common.#StartMouseWithoutBordersService()", Justification = "Dotnet port with style preservation")]
namespace MouseWithoutBorders
{
internal partial class Common
{
private static bool shownErrMessage;
private static DateTime lastStartServiceTime = DateTime.UtcNow;
internal static void StartMouseWithoutBordersService(string desktopToRunMouseWithoutBordersOn = null, string startTag1 = "byapp", string startTag2 = null)
{
// NOTE(@yuyoyuppe): the new flow assumes we run both mwb processes directly from the svc.
if (Common.RunWithNoAdminRight || true)
{
return;
}
Logger.Log($"{nameof(StartMouseWithoutBordersService)}: {Logger.GetStackTrace(new StackTrace())}.");
Task task = Task.Run(() =>
{
Process[] ps = Process.GetProcessesByName("MouseWithoutBordersSvc");
if (ps.Length != 0)
{
if (DateTime.UtcNow - lastStartServiceTime < TimeSpan.FromSeconds(5))
{
Logger.Log($"{nameof(StartMouseWithoutBordersService)}: Aborted.");
return;
}
foreach (Process pp in ps)
{
Logger.Log(string.Format(CultureInfo.InvariantCulture, "Killing process MouseWithoutBordersSvc {0}.", pp.Id));
pp.KillProcess();
}
}
lastStartServiceTime = DateTime.UtcNow;
ServiceController service = new("MouseWithoutBordersSvc");
try
{
Logger.Log("Starting " + service.ServiceName);
}
catch (Exception)
{
if (!shownErrMessage)
{
shownErrMessage = true;
_ = MessageBox.Show(
Application.ProductName + " is not installed yet, please run Setup.exe first!",
Application.ProductName,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
return;
}
try
{
int c = 0;
while (service.Status != ServiceControllerStatus.Stopped && c++ < 5)
{
Thread.Sleep(1000);
service = new ServiceController("MouseWithoutBordersSvc");
}
if (string.IsNullOrWhiteSpace(desktopToRunMouseWithoutBordersOn))
{
startTag2 ??= Process.GetCurrentProcess().SessionId.ToString(CultureInfo.InvariantCulture);
service.Start(new string[] { startTag1, startTag2 });
}
else
{
service.Start(new string[] { desktopToRunMouseWithoutBordersOn });
}
}
catch (Exception e)
{
Logger.Log(e);
// ERROR_SERVICE_ALREADY_RUNNING
if (!(shownErrMessage || ((e?.InnerException as Win32Exception)?.NativeErrorCode == 1056)))
{
shownErrMessage = true;
_ = MessageBox.Show(
"Cannot start service " + service.ServiceName + ": " + e.Message,
Common.BinaryName,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
return;
}
});
// Wait for the task while not blocking the UI thread.
do
{
MMSleep(1);
if (task.IsCanceled || task.IsCompleted || task.IsFaulted)
{
break;
}
}
while (true);
}
internal static void StartServiceAndSendLogoffSignal()
{
try
{
Process[] p = Process.GetProcessesByName("winlogon");
Process me = Process.GetCurrentProcess();
string myWinlogon = p?.FirstOrDefault(item => item.SessionId == me.SessionId)?.Id.ToString(CultureInfo.InvariantCulture) ?? null;
if (string.IsNullOrWhiteSpace(myWinlogon))
{
StartMouseWithoutBordersService(null, "logoff");
}
else
{
StartMouseWithoutBordersService(null, "logoff", myWinlogon);
}
}
catch (Exception e)
{
Logger.Log($"{nameof(StartServiceAndSendLogoffSignal)}: {e.Message}");
}
}
}
}

View File

@@ -269,7 +269,7 @@ namespace MouseWithoutBorders
if (!Common.RunWithNoAdminRight)
{
Logger.LogDebug("*** Starting on active Desktop: " + desktopToRunMouseWithoutBordersOn);
StartMouseWithoutBordersService(desktopToRunMouseWithoutBordersOn);
Service.StartMouseWithoutBordersService(desktopToRunMouseWithoutBordersOn);
}
}
@@ -279,7 +279,7 @@ namespace MouseWithoutBorders
{
if (!IsMyDesktopActive() || Common.CurrentProcess.SessionId != NativeMethods.WTSGetActiveConsoleSessionId())
{
Common.RunDDHelper(true);
Helper.RunDDHelper(true);
int waitCount = 20;
while (NativeMethods.WTSGetActiveConsoleSessionId() == 0xFFFFFFFF && waitCount > 0)

View File

@@ -101,8 +101,10 @@ namespace MouseWithoutBorders
private static bool runOnLogonDesktop;
private static bool runOnScrSaverDesktop;
private static int[] toggleIcons;
private static int toggleIconsIndex;
#pragma warning disable SA1307 // Accessible fields should begin with upper-case letter
internal static int[] toggleIcons;
internal static int toggleIconsIndex;
#pragma warning restore SA1307
internal const int TOGGLE_ICONS_SIZE = 4;
internal const int ICON_ONE = 0;
internal const int ICON_ALL = 1;
@@ -114,7 +116,9 @@ namespace MouseWithoutBorders
internal const int NETWORK_STREAM_BUF_SIZE = 1024 * 1024;
internal static readonly EventWaitHandle EvSwitch = new(false, EventResetMode.AutoReset);
private static Point lastPos;
private static int switchCount;
#pragma warning disable SA1307 // Accessible fields should begin with upper-case names
internal static int switchCount;
#pragma warning restore SA1307
private static long lastReconnectByHotKeyTime;
private static int tcpPort;
private static bool secondOpenSocketTry;
@@ -543,7 +547,7 @@ namespace MouseWithoutBorders
internal static void SendAwakeBeat()
{
if (!Common.RunOnLogonDesktop && !Common.RunOnScrSaverDesktop && Common.IsMyDesktopActive() &&
Setting.Values.BlockScreenSaver && lastRealInputEventCount != Common.RealInputEventCount)
Setting.Values.BlockScreenSaver && lastRealInputEventCount != Event.RealInputEventCount)
{
SendPackage(ID.ALL, PackageType.Awake);
}
@@ -552,13 +556,13 @@ namespace MouseWithoutBorders
SendHeartBeat();
}
lastInputEventCount = Common.InputEventCount;
lastRealInputEventCount = Common.RealInputEventCount;
lastInputEventCount = Event.InputEventCount;
lastRealInputEventCount = Event.RealInputEventCount;
}
internal static void HumanBeingDetected()
{
if (lastInputEventCount == Common.InputEventCount)
if (lastInputEventCount == Event.InputEventCount)
{
if (!Common.RunOnLogonDesktop && !Common.RunOnScrSaverDesktop && Common.IsMyDesktopActive())
{
@@ -566,7 +570,7 @@ namespace MouseWithoutBorders
}
}
lastInputEventCount = Common.InputEventCount;
lastInputEventCount = Event.InputEventCount;
}
private static void PokeMyself()
@@ -581,7 +585,7 @@ namespace MouseWithoutBorders
InputSimulation.MoveMouseRelative(-x, -y);
Thread.Sleep(50);
if (lastInputEventCount != Common.InputEventCount)
if (lastInputEventCount != Event.InputEventCount)
{
break;
}
@@ -590,8 +594,8 @@ namespace MouseWithoutBorders
internal static void InitLastInputEventCount()
{
lastInputEventCount = Common.InputEventCount;
lastRealInputEventCount = Common.RealInputEventCount;
lastInputEventCount = Event.InputEventCount;
lastRealInputEventCount = Event.RealInputEventCount;
}
internal static void SendHello()
@@ -665,7 +669,7 @@ namespace MouseWithoutBorders
{
Common.DoSomethingInUIThread(() =>
{
if (!DragDrop.MouseDown && Common.SendMessageToHelper(0x401, IntPtr.Zero, IntPtr.Zero) > 0)
if (!DragDrop.MouseDown && Helper.SendMessageToHelper(0x401, IntPtr.Zero, IntPtr.Zero) > 0)
{
Common.MMSleep(0.2);
InputSimulation.SendKey(new KEYBDDATA() { wVk = (int)VK.SNAPSHOT });
@@ -674,14 +678,14 @@ namespace MouseWithoutBorders
Logger.LogDebug("PrepareScreenCapture: SNAPSHOT simulated.");
_ = NativeMethods.MoveWindow(
(IntPtr)NativeMethods.FindWindow(null, Common.HELPER_FORM_TEXT),
(IntPtr)NativeMethods.FindWindow(null, Helper.HELPER_FORM_TEXT),
MachineStuff.DesktopBounds.Left,
MachineStuff.DesktopBounds.Top,
MachineStuff.DesktopBounds.Right - MachineStuff.DesktopBounds.Left,
MachineStuff.DesktopBounds.Bottom - MachineStuff.DesktopBounds.Top,
false);
_ = Common.SendMessageToHelper(0x406, IntPtr.Zero, IntPtr.Zero, false);
_ = Helper.SendMessageToHelper(0x406, IntPtr.Zero, IntPtr.Zero, false);
}
else
{
@@ -698,7 +702,7 @@ namespace MouseWithoutBorders
// {
// Process.Start("explorer", "\"" + file + "\"");
// });
_ = CreateProcessInInputDesktopSession(
_ = Launch.CreateProcessInInputDesktopSession(
"\"" + Environment.ExpandEnvironmentVariables(@"%SystemRoot%\System32\Mspaint.exe") +
"\"",
"\"" + file + "\"",
@@ -956,8 +960,8 @@ namespace MouseWithoutBorders
{
// SwitchToMachine(MachineName.Trim());
MachineStuff.NewDesMachineID = DesMachineID = MachineID;
MachineStuff.SwitchLocation.X = XY_BY_PIXEL + myLastX;
MachineStuff.SwitchLocation.Y = XY_BY_PIXEL + myLastY;
MachineStuff.SwitchLocation.X = Event.XY_BY_PIXEL + Event.myLastX;
MachineStuff.SwitchLocation.Y = Event.XY_BY_PIXEL + Event.myLastY;
MachineStuff.SwitchLocation.ResetCount();
EvSwitch.Set();
}
@@ -1314,7 +1318,7 @@ namespace MouseWithoutBorders
}
else
{
_ = ImpersonateLoggedOnUserAndDoSomething(() =>
_ = Launch.ImpersonateLoggedOnUserAndDoSomething(() =>
{
st = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + Common.BinaryName;
if (!Directory.Exists(st))

View File

@@ -206,7 +206,7 @@ namespace MouseWithoutBorders.Class
{
int rv = 1, dx = 0, dy = 0;
bool local = false;
Common.InputEventCount++;
Event.InputEventCount++;
try
{
@@ -220,14 +220,14 @@ namespace MouseWithoutBorders.Class
}
else
{
Common.RealInputEventCount++;
Event.RealInputEventCount++;
if (MachineStuff.NewDesMachineID == Common.MachineID || MachineStuff.NewDesMachineID == ID.ALL)
{
local = true;
if (Common.MainFormVisible && !DragDrop.IsDropping)
{
Common.MainFormDot();
Helper.MainFormDot();
}
}
@@ -269,10 +269,10 @@ namespace MouseWithoutBorders.Class
{
MachineStuff.SwitchLocation.Count--;
if (MachineStuff.SwitchLocation.X > Common.XY_BY_PIXEL - 100000 || MachineStuff.SwitchLocation.Y > Common.XY_BY_PIXEL - 100000)
if (MachineStuff.SwitchLocation.X > Event.XY_BY_PIXEL - 100000 || MachineStuff.SwitchLocation.Y > Event.XY_BY_PIXEL - 100000)
{
hookCallbackMouseData.X = MachineStuff.SwitchLocation.X - Common.XY_BY_PIXEL;
hookCallbackMouseData.Y = MachineStuff.SwitchLocation.Y - Common.XY_BY_PIXEL;
hookCallbackMouseData.X = MachineStuff.SwitchLocation.X - Event.XY_BY_PIXEL;
hookCallbackMouseData.Y = MachineStuff.SwitchLocation.Y - Event.XY_BY_PIXEL;
}
else
{
@@ -308,8 +308,8 @@ namespace MouseWithoutBorders.Class
hookCallbackMouseData.Y = MachineStuff.PrimaryScreenBounds.Bottom + 1;
}
dx += dx < 0 ? -Common.MOVE_MOUSE_RELATIVE : Common.MOVE_MOUSE_RELATIVE;
dy += dy < 0 ? -Common.MOVE_MOUSE_RELATIVE : Common.MOVE_MOUSE_RELATIVE;
dx += dx < 0 ? -Event.MOVE_MOUSE_RELATIVE : Event.MOVE_MOUSE_RELATIVE;
dy += dy < 0 ? -Event.MOVE_MOUSE_RELATIVE : Event.MOVE_MOUSE_RELATIVE;
}
}
@@ -336,13 +336,13 @@ namespace MouseWithoutBorders.Class
private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
{
Common.InputEventCount++;
Event.InputEventCount++;
if (!RealData)
{
return NativeMethods.CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
Common.RealInputEventCount++;
Event.RealInputEventCount++;
keyboardHookStruct = LParamToKeyboardHookStruct(lParam);
hookCallbackKeybdData.dwFlags = keyboardHookStruct.Flags;

View File

@@ -223,7 +223,7 @@ namespace MouseWithoutBorders.Class
if (Common.MainFormVisible && !DragDrop.IsDropping)
{
Common.MainFormDot();
Helper.MainFormDot();
}
return rv;

View File

@@ -36,6 +36,7 @@ using Newtonsoft.Json;
using StreamJsonRpc;
using Logger = MouseWithoutBorders.Core.Logger;
using SettingsHelper = Microsoft.PowerToys.Settings.UI.Library.Utilities.Helper;
using Thread = MouseWithoutBorders.Core.Thread;
[module: SuppressMessage("Microsoft.MSInternal", "CA904:DeclareTypesInMicrosoftOrSystemNamespace", Scope = "namespace", Target = "MouseWithoutBorders", Justification = "Dotnet port with style preservation")]
@@ -128,7 +129,7 @@ namespace MouseWithoutBorders.Class
{
if (args.Length > 2)
{
Helper.UserLocalAppDataPath = args[2].Trim();
SettingsHelper.UserLocalAppDataPath = args[2].Trim();
}
}
@@ -235,7 +236,7 @@ namespace MouseWithoutBorders.Class
Application.SetCompatibleTextRenderingDefault(false);
Common.Init();
Common.WndProcCounter++;
Core.Helper.WndProcCounter++;
var formScreen = new FrmScreen();
@@ -430,7 +431,7 @@ namespace MouseWithoutBorders.Class
Logger.Log(e);
}
Common.StartMouseWithoutBordersService();
Service.StartMouseWithoutBordersService();
}
internal static string User { get; set; }

View File

@@ -33,6 +33,7 @@ using MouseWithoutBorders.Core;
using Settings.UI.Library.Attributes;
using Lock = System.Threading.Lock;
using SettingsHelper = Microsoft.PowerToys.Settings.UI.Library.Utilities.Helper;
[module: SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Scope = "member", Target = "MouseWithoutBorders.Properties.Setting.Values.#LoadIntSetting(System.String,System.Int32)", Justification = "Dotnet port with style preservation")]
[module: SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Scope = "member", Target = "MouseWithoutBorders.Properties.Setting.Values.#SaveSetting(System.String,System.Object)", Justification = "Dotnet port with style preservation")]
@@ -193,7 +194,7 @@ namespace MouseWithoutBorders.Class
{
_settingsUtils = new SettingsUtils();
_watcher = Helper.GetFileWatcher("MouseWithoutBorders", "settings.json", () =>
_watcher = SettingsHelper.GetFileWatcher("MouseWithoutBorders", "settings.json", () =>
{
try
{

View File

@@ -1684,7 +1684,7 @@ namespace MouseWithoutBorders.Class
{
string fileName = null;
if (!Common.ImpersonateLoggedOnUserAndDoSomething(() =>
if (!Launch.ImpersonateLoggedOnUserAndDoSomething(() =>
{
if (!File.Exists(Common.LastDragDropFile))
{
@@ -1873,7 +1873,7 @@ namespace MouseWithoutBorders.Class
}
else
{
_ = Common.ImpersonateLoggedOnUserAndDoSomething(() => { r = SendFileEx(s, ecStream, fileName); });
_ = Launch.ImpersonateLoggedOnUserAndDoSomething(() => { r = SendFileEx(s, ecStream, fileName); });
}
return r;
@@ -2111,7 +2111,7 @@ namespace MouseWithoutBorders.Class
}
}
_ = Common.CreateLowIntegrityProcess(
_ = Launch.CreateLowIntegrityProcess(
"\"" + Path.GetDirectoryName(Application.ExecutablePath) + "\\MouseWithoutBordersHelper.exe\"",
"InternalError" + " \"" + msg + "\"",
0,