From c2f37dedd9cf2da94add9a888af9b95db58be487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Tue, 29 Nov 2022 21:39:54 +0100 Subject: [PATCH] Fix using Playnite to launch Steam game where on exit Desktop was activated --- RELEASE.md | 1 + SteamController/Context.cs | 7 ++++ SteamController/Controller.cs | 3 +- SteamController/Managers/ProcessManager.cs | 20 +-------- SteamController/Managers/ProfileSwitcher.cs | 45 +++++++++++++++++++++ SteamController/Managers/SteamManager.cs | 3 -- 6 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 SteamController/Managers/ProfileSwitcher.cs diff --git a/RELEASE.md b/RELEASE.md index 4731ea7..008b673 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -21,3 +21,4 @@ It does help this project on being supported. - Fix `Process Kill` action (STEAM+B for 3s) - Go back to `Startup Profile` on `Toggle deskptop mode` - The `X360.Beep()` cycles currently connected device (fixes Playnite error) +- Fix using Playnite to launch Steam game where on exit Desktop was activated diff --git a/SteamController/Context.cs b/SteamController/Context.cs index 1723422..853b45b 100644 --- a/SteamController/Context.cs +++ b/SteamController/Context.cs @@ -20,6 +20,7 @@ namespace SteamController private int selectedProfile; public bool RequestEnable { get; set; } = true; + public bool GameProcessRunning { get; set; } = false; public bool SteamUsesX360Controller { get; set; } = false; public bool SteamUsesSteamInput { get; set; } = false; @@ -54,6 +55,7 @@ namespace SteamController Mouse = new Devices.MouseController(); ProfileChanged += (_) => X360.Beep(); + ProfileChanged += (profile) => TraceLine("Context: Selected Profile: {0}", profile.Name); } public void Dispose() @@ -139,8 +141,12 @@ namespace SteamController var current = CurrentProfile; if (current is null) return; + if (current.IsDesktop) + { + TraceLine("Context: SelectController"); SelectNext(); + } } public bool SelectNext() @@ -173,6 +179,7 @@ namespace SteamController public void BackToDefault() { + TraceLine("Context: Back To Default."); if (SelectDefault is not null) SelectDefault(); } diff --git a/SteamController/Controller.cs b/SteamController/Controller.cs index 726bb37..800e7c0 100644 --- a/SteamController/Controller.cs +++ b/SteamController/Controller.cs @@ -26,7 +26,8 @@ namespace SteamController }, Managers = { new Managers.ProcessManager(), - new Managers.SteamManager() + new Managers.SteamManager(), + new Managers.ProfileSwitcher() } }; diff --git a/SteamController/Managers/ProcessManager.cs b/SteamController/Managers/ProcessManager.cs index 42899d8..b2cfc08 100644 --- a/SteamController/Managers/ProcessManager.cs +++ b/SteamController/Managers/ProcessManager.cs @@ -9,8 +9,6 @@ namespace SteamController.Managers "Playnite.FullscreenApp" }; - private bool activated; - private Process? FindActivationProcess() { foreach (var processName in ActivationProcessNames) @@ -25,23 +23,7 @@ namespace SteamController.Managers public override void Tick(Context context) { - // React to state change - if (FindActivationProcess() is not null) - { - if (!activated) - { - activated = true; - context.SelectController(); - } - } - else - { - if (activated) - { - activated = false; - context.BackToDefault(); - } - } + context.GameProcessRunning = FindActivationProcess() is not null; } } } diff --git a/SteamController/Managers/ProfileSwitcher.cs b/SteamController/Managers/ProfileSwitcher.cs new file mode 100644 index 0000000..f4dd488 --- /dev/null +++ b/SteamController/Managers/ProfileSwitcher.cs @@ -0,0 +1,45 @@ +using System.Diagnostics; +using SteamController.Helpers; + +namespace SteamController.Managers +{ + public sealed class ProfileSwitcher : Manager + { + [Flags] + private enum ActiveMode + { + None, + SteamInput = 1, + SteamX360 = 2, + OtherGame = 4 + } + + private ActiveMode wasActive; + + public override void Tick(Context context) + { + ActiveMode active = GetActiveMode(context); + if (wasActive == active) + return; + + if (active != ActiveMode.None) + context.SelectController(); + else + context.BackToDefault(); + + wasActive = active; + } + + private ActiveMode GetActiveMode(Context context) + { + ActiveMode mode = ActiveMode.None; + if (context.SteamUsesSteamInput) + mode |= ActiveMode.SteamInput; + if (context.SteamUsesX360Controller) + mode |= ActiveMode.SteamX360; + if (context.GameProcessRunning) + mode |= ActiveMode.OtherGame; + return mode; + } + } +} diff --git a/SteamController/Managers/SteamManager.cs b/SteamController/Managers/SteamManager.cs index 55049a7..e4f649e 100644 --- a/SteamController/Managers/SteamManager.cs +++ b/SteamController/Managers/SteamManager.cs @@ -32,14 +32,11 @@ namespace SteamController.Managers Devices.Xbox360Controller.VendorID, Devices.Xbox360Controller.ProductID ) != true; - - context.SelectController(); } else { context.SteamUsesSteamInput = false; context.SteamUsesX360Controller = false; - context.BackToDefault(); } lastState = usesController;