From 7db356cb43ee7bec475d040b2dcf3307e1ed0a96 Mon Sep 17 00:00:00 2001 From: Stanley Date: Sun, 25 Sep 2022 10:49:49 -0400 Subject: [PATCH] Added RSG GTN750 refocus override --- ArduinoAgent/ArduinoAgent.csproj | 6 +- Orchestration/Orchestration.csproj | 6 +- .../PanelConfigurationOrchestrator.cs | 94 ++++++++++++++++++- Shared/Shared.csproj | 6 +- SimconnectAgent/SimconnectAgent.csproj | 6 +- TouchPanelAgent/TouchPanelAgent.csproj | 6 +- UserDataAgent/AppSetting.cs | 3 + UserDataAgent/Profile.cs | 3 + UserDataAgent/UserDataAgent.csproj | 6 +- WebServer/WebServer.csproj | 6 +- WindowsAgent/PInvoke.cs | 3 + WindowsAgent/TouchEventManager.cs | 55 ++++++----- WindowsAgent/WindowsAgent.csproj | 6 +- WpfApp/PreferencesDialog.xaml | 25 +++-- WpfApp/UserControlPanelSelection.xaml | 3 + WpfApp/WpfApp.csproj | 6 +- 16 files changed, 178 insertions(+), 62 deletions(-) diff --git a/ArduinoAgent/ArduinoAgent.csproj b/ArduinoAgent/ArduinoAgent.csproj index b60e669..2bb3723 100644 --- a/ArduinoAgent/ArduinoAgent.csproj +++ b/ArduinoAgent/ArduinoAgent.csproj @@ -10,9 +10,9 @@ https://github.com/hawkeye-stan/msfs-popout-panel-manager MSFSPopoutPanelManager.ArduinoAgent x64 - 3.4.3.0911 - 3.4.3.0911 - 3.4.3.0911 + 3.4.4.0925 + 3.4.4.0925 + 3.4.4.0925 win-x64 Embedded Debug;Release;DebugTouchPanel;ReleaseTouchPanel diff --git a/Orchestration/Orchestration.csproj b/Orchestration/Orchestration.csproj index f5bb39d..930bc35 100644 --- a/Orchestration/Orchestration.csproj +++ b/Orchestration/Orchestration.csproj @@ -10,9 +10,9 @@ https://github.com/hawkeye-stan/msfs-popout-panel-manager MSFSPopoutPanelManager.Orchestration x64 - 3.4.3.0911 - 3.4.3.0911 - 3.4.3.0911 + 3.4.4.0925 + 3.4.4.0925 + 3.4.4.0925 win-x64 Embedded Debug;Release;DebugTouchPanel;ReleaseTouchPanel diff --git a/Orchestration/PanelConfigurationOrchestrator.cs b/Orchestration/PanelConfigurationOrchestrator.cs index 2f405f5..33445aa 100644 --- a/Orchestration/PanelConfigurationOrchestrator.cs +++ b/Orchestration/PanelConfigurationOrchestrator.cs @@ -4,6 +4,8 @@ using MSFSPopoutPanelManager.WindowsAgent; using System; using System.Drawing; using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace MSFSPopoutPanelManager.Orchestration { @@ -14,6 +16,11 @@ namespace MSFSPopoutPanelManager.Orchestration private Rectangle _lastWindowRectangle; private IntPtr _panelHandleDisableRefresh = IntPtr.Zero; + private uint _prevWinEvent = PInvokeConstant.EVENT_SYSTEM_CAPTUREEND; + private int _winEventClickLock = 0; + private object _hookLock = new object(); + private bool _isHookMouseDown = false; + public PanelConfigurationOrchestrator() { _winEvent = new PInvoke.WinEventProc(EventCallback); @@ -199,7 +206,10 @@ namespace MSFSPopoutPanelManager.Orchestration return; // Setup panel config event hooks - _winEventHook = PInvoke.SetWinEventHook(PInvokeConstant.EVENT_SYSTEM_MOVESIZEEND, PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, _winEvent, 0, 0, PInvokeConstant.WINEVENT_OUTOFCONTEXT); + if (!ActiveProfile.RealSimGearGTN750Gen1Override) + _winEventHook = PInvoke.SetWinEventHook(PInvokeConstant.EVENT_SYSTEM_MOVESIZEEND, PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, _winEvent, 0, 0, PInvokeConstant.WINEVENT_OUTOFCONTEXT); + else + _winEventHook = PInvoke.SetWinEventHook(PInvokeConstant.EVENT_SYSTEM_CAPTURESTART, PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, _winEvent, 0, 0, PInvokeConstant.WINEVENT_OUTOFCONTEXT); } private void UnhookWinEvent() @@ -214,6 +224,8 @@ namespace MSFSPopoutPanelManager.Orchestration { case PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE: case PInvokeConstant.EVENT_SYSTEM_MOVESIZEEND: + case PInvokeConstant.EVENT_SYSTEM_CAPTURESTART: + case PInvokeConstant.EVENT_SYSTEM_CAPTUREEND: // check by priority to speed up comparing of escaping constraints if (hwnd == IntPtr.Zero || idObject != 0 || hWinEventHook != _winEventHook || !AllowEdit) return; @@ -253,6 +265,17 @@ namespace MSFSPopoutPanelManager.Orchestration PInvoke.ShowWindow(hwnd, PInvokeConstant.SW_RESTORE); } break; + case PInvokeConstant.EVENT_SYSTEM_CAPTURESTART: + if (!panelConfig.HasTouchableEvent || _prevWinEvent == PInvokeConstant.EVENT_SYSTEM_CAPTURESTART) + break; + + HandleTouchDownEvent(panelConfig); + break; + case PInvokeConstant.EVENT_SYSTEM_CAPTUREEND: + if (!panelConfig.TouchEnabled || _prevWinEvent == PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE) + break; + HandleTouchUpEvent(panelConfig); + break; } } else @@ -301,6 +324,75 @@ namespace MSFSPopoutPanelManager.Orchestration case PInvokeConstant.EVENT_SYSTEM_MOVESIZEEND: ProfileData.WriteProfiles(); break; + case PInvokeConstant.EVENT_SYSTEM_CAPTURESTART: + if (!panelConfig.HasTouchableEvent || _prevWinEvent == PInvokeConstant.EVENT_SYSTEM_CAPTURESTART) + break; + + HandleTouchDownEvent(panelConfig); + break; + case PInvokeConstant.EVENT_SYSTEM_CAPTUREEND: + if (!panelConfig.TouchEnabled || _prevWinEvent == PInvokeConstant.EVENT_OBJECT_LOCATIONCHANGE) + break; + HandleTouchUpEvent(panelConfig); + break; + } + } + } + + private void HandleTouchDownEvent(PanelConfig panelConfig) + { + if (!_isHookMouseDown) + { + lock (_hookLock) + { + Point point; + PInvoke.GetCursorPos(out point); + + // Disable left clicking if user is touching the title bar area + if (point.Y - panelConfig.Top > (panelConfig.HideTitlebar ? 5 : 31)) + _isHookMouseDown = true; + } + } + } + + + private void HandleTouchUpEvent(PanelConfig panelConfig) + { + if (_isHookMouseDown) + { + Thread.Sleep(AppSettingData.AppSetting.TouchScreenSettings.TouchDownUpDelay); + + lock (_hookLock) + { + _isHookMouseDown = false; + + Point point; + PInvoke.GetCursorPos(out point); + + // Disable left clicking if user is touching the title bar area + if (point.Y - panelConfig.Top > (panelConfig.HideTitlebar ? 5 : 31)) + { + var prevWinEventClickLock = ++_winEventClickLock; + + if (prevWinEventClickLock == _winEventClickLock && AppSettingData.AppSetting.TouchScreenSettings.RefocusGameWindow) + { + Task.Run(() => RefocusMsfs(panelConfig.PanelHandle, prevWinEventClickLock)); + } + } + } + } + } + + private void RefocusMsfs(IntPtr panelConfigHandle, int prevWinEventClickLock) + { + Thread.Sleep(AppSettingData.AppSetting.TouchScreenSettings.RefocusGameWindowDelay); + + if (prevWinEventClickLock == _winEventClickLock) + { + if (!_isHookMouseDown) + { + var rectangle = WindowActionManager.GetWindowRect(panelConfigHandle); + PInvoke.SetCursorPos(rectangle.X - 5, rectangle.Y + 5); } } } diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj index fd5dae5..14973d7 100644 --- a/Shared/Shared.csproj +++ b/Shared/Shared.csproj @@ -11,9 +11,9 @@ https://github.com/hawkeye-stan/msfs-popout-panel-manager MSFSPopoutPanelManager.Shared x64 - 3.4.3.0911 - 3.4.3.0911 - 3.4.3.0911 + 3.4.4.0925 + 3.4.4.0925 + 3.4.4.0925 win-x64 Embedded Debug;Release;DebugTouchPanel;ReleaseTouchPanel diff --git a/SimconnectAgent/SimconnectAgent.csproj b/SimconnectAgent/SimconnectAgent.csproj index 37e5933..bc3b447 100644 --- a/SimconnectAgent/SimconnectAgent.csproj +++ b/SimconnectAgent/SimconnectAgent.csproj @@ -11,9 +11,9 @@ https://github.com/hawkeye-stan/msfs-popout-panel-manager MSFSPopoutPanelManager.SimConnectAgent x64 - 3.4.3.0911 - 3.4.3.0911 - 3.4.3.0911 + 3.4.4.0925 + 3.4.4.0925 + 3.4.4.0925 win-x64 Embedded Debug;Release;DebugTouchPanel;ReleaseTouchPanel diff --git a/TouchPanelAgent/TouchPanelAgent.csproj b/TouchPanelAgent/TouchPanelAgent.csproj index 9ab27bf..a4d8841 100644 --- a/TouchPanelAgent/TouchPanelAgent.csproj +++ b/TouchPanelAgent/TouchPanelAgent.csproj @@ -11,9 +11,9 @@ https://github.com/hawkeye-stan/msfs-popout-panel-manager MSFSPopoutPanelManager.TouchPanelAgent x64 - 3.4.3.0911 - 3.4.3.0911 - 3.4.3.0911 + 3.4.4.0925 + 3.4.4.0925 + 3.4.4.0925 win-x64 Embedded Debug;Release;DebugTouchPanel;ReleaseTouchPanel diff --git a/UserDataAgent/AppSetting.cs b/UserDataAgent/AppSetting.cs index b4e4f6c..5ee52d4 100644 --- a/UserDataAgent/AppSetting.cs +++ b/UserDataAgent/AppSetting.cs @@ -140,6 +140,7 @@ namespace MSFSPopoutPanelManager.UserDataAgent TouchDownUpDelay = 0; RefocusGameWindow = true; RefocusGameWindowDelay = 500; + RealSimGearGTN750Gen1Override = false; } public int TouchDownUpDelay { get; set; } @@ -147,6 +148,8 @@ namespace MSFSPopoutPanelManager.UserDataAgent public bool RefocusGameWindow { get; set; } public int RefocusGameWindowDelay { get; set; } + + public bool RealSimGearGTN750Gen1Override { get; set; } } public class TouchPanelSettings : ObservableObject diff --git a/UserDataAgent/Profile.cs b/UserDataAgent/Profile.cs index e8ba3a3..158984e 100644 --- a/UserDataAgent/Profile.cs +++ b/UserDataAgent/Profile.cs @@ -16,6 +16,7 @@ namespace MSFSPopoutPanelManager.UserDataAgent IsLocked = false; PowerOnRequiredForColdStart = false; IncludeInGamePanels = false; + RealSimGearGTN750Gen1Override = false; MsfsGameWindowConfig = new MsfsGameWindowConfig(); @@ -46,6 +47,8 @@ namespace MSFSPopoutPanelManager.UserDataAgent public bool IncludeInGamePanels { get; set; } + public bool RealSimGearGTN750Gen1Override { get; set; } + public MsfsGameWindowConfig MsfsGameWindowConfig { get; set; } [JsonIgnore] diff --git a/UserDataAgent/UserDataAgent.csproj b/UserDataAgent/UserDataAgent.csproj index d856481..9509917 100644 --- a/UserDataAgent/UserDataAgent.csproj +++ b/UserDataAgent/UserDataAgent.csproj @@ -10,9 +10,9 @@ https://github.com/hawkeye-stan/msfs-popout-panel-manager MSFSPopoutPanelManager.UserDataAgent x64 - 3.4.3.0911 - 3.4.3.0911 - 3.4.3.0911 + 3.4.4.0925 + 3.4.4.0925 + 3.4.4.0925 win-x64 Embedded Debug;Release;DebugTouchPanel;ReleaseTouchPanel diff --git a/WebServer/WebServer.csproj b/WebServer/WebServer.csproj index a5d31c9..11ce67e 100644 --- a/WebServer/WebServer.csproj +++ b/WebServer/WebServer.csproj @@ -11,9 +11,9 @@ https://github.com/hawkeye-stan/msfs-popout-panel-manager MSFSPopoutPanelManager.WebServer x64 - 3.4.3.0911 - 3.4.3.0911 - 3.4.3.0911 + 3.4.4.0925 + 3.4.4.0925 + 3.4.4.0925 win-x64 Embedded Debug;Release;DebugTouchPanel;ReleaseTouchPanel diff --git a/WindowsAgent/PInvoke.cs b/WindowsAgent/PInvoke.cs index f4a471e..ef65c6e 100644 --- a/WindowsAgent/PInvoke.cs +++ b/WindowsAgent/PInvoke.cs @@ -16,6 +16,9 @@ namespace MSFSPopoutPanelManager.WindowsAgent public const int SW_MINIMIZE = 6; public const int SW_RESTORE = 9; + public const uint EVENT_SYSTEM_CAPTURESTART = 0x0008; + public const uint EVENT_SYSTEM_CAPTUREEND = 0x0009; + public const uint EVENT_SYSTEM_MOVESIZESTART = 0x000A; public const uint EVENT_SYSTEM_MOVESIZEEND = 0x000B; public const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B; diff --git a/WindowsAgent/TouchEventManager.cs b/WindowsAgent/TouchEventManager.cs index 8f97e78..172ea08 100644 --- a/WindowsAgent/TouchEventManager.cs +++ b/WindowsAgent/TouchEventManager.cs @@ -15,10 +15,9 @@ namespace MSFSPopoutPanelManager.WindowsAgent private static PInvoke.WindowsHookExProc callbackDelegate = HookCallBack; private static bool _isTouchDown; private static int _mouseMoveCount; - private static bool _isMouseMoveBlock = false; private static object _mouseTouchLock = new object(); private static bool _isDragged = false; - private static bool _refocused = true; + private static int _refocusedTaskIndex = 0; private const int PANEL_MENUBAR_HEIGHT = 31; private const uint TOUCH_FLAG = 0xFF515700; @@ -37,18 +36,30 @@ namespace MSFSPopoutPanelManager.WindowsAgent public static void Hook() { + // If using RSG GT750 Gen 1, disable touch support + if (ActiveProfile != null && ActiveProfile.RealSimGearGTN750Gen1Override) + return; + _simulatorProcess = WindowProcessManager.GetSimulatorProcess(); Process curProcess = Process.GetCurrentProcess(); ProcessModule curModule = curProcess.MainModule; var hookWindowPtr = PInvoke.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); + _hHook = PInvoke.SetWindowsHookEx(HookType.WH_MOUSE_LL, callbackDelegate, hookWindowPtr, 0); } public static void UnHook() { - PInvoke.UnhookWindowsHookEx(_hHook); - _hHook = IntPtr.Zero; + // If using RSG GT750 Gen 1, disable touch support + if (ActiveProfile != null && ActiveProfile.RealSimGearGTN750Gen1Override) + return; + + if (_hHook != IntPtr.Zero) + { + PInvoke.UnhookWindowsHookEx(_hHook); + _hHook = IntPtr.Zero; + } } public static bool IsHooked { get { return _hHook != IntPtr.Zero; } } @@ -58,7 +69,6 @@ namespace MSFSPopoutPanelManager.WindowsAgent if (code != 0) return PInvoke.CallNextHookEx(_hHook, code, wParam, lParam); - var info = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); var extraInfo = (uint)info.dwExtraInfo; var isTouched = (extraInfo & TOUCH_FLAG) == TOUCH_FLAG; @@ -104,9 +114,7 @@ namespace MSFSPopoutPanelManager.WindowsAgent case WM_LBUTTONDOWN: if (!_isTouchDown) { - _refocused = false; _isTouchDown = true; - _isMouseMoveBlock = true; lock (_mouseTouchLock) { @@ -121,7 +129,6 @@ namespace MSFSPopoutPanelManager.WindowsAgent Thread.Sleep(AppSetting.TouchScreenSettings.TouchDownUpDelay + 25); PInvoke.mouse_event(MOUSEEVENTF_LEFTDOWN, info.pt.X, info.pt.Y, 0, 0); Thread.Sleep(AppSetting.TouchScreenSettings.TouchDownUpDelay + 50); - _isMouseMoveBlock = false; _isTouchDown = false; }); } @@ -142,11 +149,25 @@ namespace MSFSPopoutPanelManager.WindowsAgent _isDragged = false; } _mouseMoveCount = 0; - RefocusMsfsGameWindow(panelConfig); + + // Refocus game window + if (AppSetting.TouchScreenSettings.RefocusGameWindow && !panelConfig.DisableGameRefocus) + { + _refocusedTaskIndex++; + var currentRefocusIndex = _refocusedTaskIndex; + + Thread.Sleep(AppSetting.TouchScreenSettings.RefocusGameWindowDelay); + + if (currentRefocusIndex == _refocusedTaskIndex) + { + var rectangle = WindowActionManager.GetWindowRect(panelConfig.PanelHandle); + PInvoke.SetCursorPos(rectangle.X - 5, rectangle.Y + 5); + } + } }); return 1; case WM_MOUSEMOVE: - if (_isMouseMoveBlock) + if (_isTouchDown) return 1; _mouseMoveCount++; @@ -156,19 +177,5 @@ namespace MSFSPopoutPanelManager.WindowsAgent return PInvoke.CallNextHookEx(_hHook, code, wParam, lParam); } - - private static void RefocusMsfsGameWindow(PanelConfig panelConfig) - { - Thread.Sleep(AppSetting.TouchScreenSettings.RefocusGameWindowDelay); - - if (!_refocused && !_isTouchDown && AppSetting.TouchScreenSettings.RefocusGameWindow && !panelConfig.DisableGameRefocus) - { - _refocused = true; - var rectangle = WindowActionManager.GetWindowRect(_simulatorProcess.Handle); - var clientRectangle = WindowActionManager.GetClientRect(_simulatorProcess.Handle); - - PInvoke.SetCursorPos(rectangle.X + clientRectangle.Width / 2, rectangle.Y + clientRectangle.Height / 2); - } - } } } diff --git a/WindowsAgent/WindowsAgent.csproj b/WindowsAgent/WindowsAgent.csproj index b7e2765..6e9b07a 100644 --- a/WindowsAgent/WindowsAgent.csproj +++ b/WindowsAgent/WindowsAgent.csproj @@ -11,9 +11,9 @@ https://github.com/hawkeye-stan/msfs-popout-panel-manager MSFSPopoutPanelManager.WindowsAgent x64 - 3.4.3.0911 - 3.4.3.0911 - 3.4.3.0911 + 3.4.4.0925 + 3.4.4.0925 + 3.4.4.0925 win-x64 Embedded Debug;Release;DebugTouchPanel;ReleaseTouchPanel diff --git a/WpfApp/PreferencesDialog.xaml b/WpfApp/PreferencesDialog.xaml index d5d091e..5d0ca6d 100644 --- a/WpfApp/PreferencesDialog.xaml +++ b/WpfApp/PreferencesDialog.xaml @@ -262,17 +262,22 @@ - - Refocus Game Window - - - Automactically set focus back to game window after a period of inactivity on touch enabled panel. This will give your flight control back when using pop out panel. - - - - Amount of time in milliseconds to wait for touch inactivity before input focus goes back to game window. + + Refocus Game Window + + + Automactically set focus back to game window after a period of inactivity on touch enabled panel. This will give your flight control back when using pop out panel. + + + + Amount of time in milliseconds to wait for touch inactivity before input focus goes back to game window. + + + + Use custom refocus logic for RealSimGear GTN750 Gen1. + + - Touch Down Touch Up Delay diff --git a/WpfApp/UserControlPanelSelection.xaml b/WpfApp/UserControlPanelSelection.xaml index 9d8cb38..6103cad 100644 --- a/WpfApp/UserControlPanelSelection.xaml +++ b/WpfApp/UserControlPanelSelection.xaml @@ -78,6 +78,9 @@ + + +