From 97281e24df7cae04c542e3849c362550dc83d583 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Tue, 27 Dec 2016 10:35:39 +0100 Subject: [PATCH] Small fixes No longer taking exclusive CUE access, seems to work well with latest CUE this way Removed LUA stack trace logging, just refers to MoonSharp code anyway Artemis disables and re-enables itself after system wake for #204 --- .../DeviceProviders/Corsair/CorsairHeadset.cs | 2 +- .../Corsair/CorsairKeyboard.cs | 16 +++- .../DeviceProviders/Corsair/CorsairMouse.cs | 2 +- .../Corsair/CorsairMousemat.cs | 2 +- Artemis/Artemis/Managers/LuaManager.cs | 6 +- Artemis/Artemis/Managers/MainManager.cs | 24 ++++- .../Profiles/Lua/Modules/LuaKeyboardModule.cs | 92 +++++++++---------- 7 files changed, 86 insertions(+), 58 deletions(-) diff --git a/Artemis/Artemis/DeviceProviders/Corsair/CorsairHeadset.cs b/Artemis/Artemis/DeviceProviders/Corsair/CorsairHeadset.cs index cde46b320..908d4ae07 100644 --- a/Artemis/Artemis/DeviceProviders/Corsair/CorsairHeadset.cs +++ b/Artemis/Artemis/DeviceProviders/Corsair/CorsairHeadset.cs @@ -22,7 +22,7 @@ namespace Artemis.DeviceProviders.Corsair { CanUse = CanInitializeSdk(); if (CanUse && !CueSDK.IsInitialized) - CueSDK.Initialize(true); + CueSDK.Initialize(); Logger.Debug("Attempted to enable Corsair headset. CanUse: {0}", CanUse); diff --git a/Artemis/Artemis/DeviceProviders/Corsair/CorsairKeyboard.cs b/Artemis/Artemis/DeviceProviders/Corsair/CorsairKeyboard.cs index 3b8f30b7a..331f02531 100644 --- a/Artemis/Artemis/DeviceProviders/Corsair/CorsairKeyboard.cs +++ b/Artemis/Artemis/DeviceProviders/Corsair/CorsairKeyboard.cs @@ -10,6 +10,7 @@ using CUE.NET; using CUE.NET.Brushes; using CUE.NET.Devices.Generic; using CUE.NET.Devices.Generic.Enums; +using CUE.NET.Exceptions; using CUE.NET.Helper; using Ninject.Extensions.Logging; using Point = System.Drawing.Point; @@ -44,7 +45,7 @@ namespace Artemis.DeviceProviders.Corsair public override void Enable() { if (!CueSDK.IsInitialized) - CueSDK.Initialize(true); + CueSDK.Initialize(); CueSDK.UpdateMode = UpdateMode.Manual; _keyboard = CueSDK.KeyboardSDK; @@ -87,8 +88,17 @@ namespace Artemis.DeviceProviders.Corsair public override void Disable() { - if (CueSDK.IsInitialized) - CueSDK.Reinitialize(); + try + { + if (CueSDK.IsInitialized) + CueSDK.Reinitialize(); + } + catch (WrapperException e) + { + // This occurs when releasing the SDK after sleep, ignore it + if (e.Message != "The previously loaded Keyboard got disconnected.") + throw; + } } /// diff --git a/Artemis/Artemis/DeviceProviders/Corsair/CorsairMouse.cs b/Artemis/Artemis/DeviceProviders/Corsair/CorsairMouse.cs index e6dcd60cf..7e18ba746 100644 --- a/Artemis/Artemis/DeviceProviders/Corsair/CorsairMouse.cs +++ b/Artemis/Artemis/DeviceProviders/Corsair/CorsairMouse.cs @@ -22,7 +22,7 @@ namespace Artemis.DeviceProviders.Corsair { CanUse = CanInitializeSdk(); if (CanUse && !CueSDK.IsInitialized) - CueSDK.Initialize(true); + CueSDK.Initialize(); Logger.Debug("Attempted to enable Corsair mice. CanUse: {0}", CanUse); diff --git a/Artemis/Artemis/DeviceProviders/Corsair/CorsairMousemat.cs b/Artemis/Artemis/DeviceProviders/Corsair/CorsairMousemat.cs index e4e6cc39e..3062862d5 100644 --- a/Artemis/Artemis/DeviceProviders/Corsair/CorsairMousemat.cs +++ b/Artemis/Artemis/DeviceProviders/Corsair/CorsairMousemat.cs @@ -22,7 +22,7 @@ namespace Artemis.DeviceProviders.Corsair { CanUse = CanInitializeSdk(); if (CanUse && !CueSDK.IsInitialized) - CueSDK.Initialize(true); + CueSDK.Initialize(); Logger.Debug("Attempted to enable Corsair mousemat. CanUse: {0}", CanUse); diff --git a/Artemis/Artemis/Managers/LuaManager.cs b/Artemis/Artemis/Managers/LuaManager.cs index d75b73143..6e8fc166c 100644 --- a/Artemis/Artemis/Managers/LuaManager.cs +++ b/Artemis/Artemis/Managers/LuaManager.cs @@ -80,15 +80,15 @@ namespace Artemis.Managers } catch (InternalErrorException e) { - _logger.Error(e, "[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage); + _logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage); } catch (SyntaxErrorException e) { - _logger.Error(e, "[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage); + _logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage); } catch (ScriptRuntimeException e) { - _logger.Error(e, "[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage); + _logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage); } } diff --git a/Artemis/Artemis/Managers/MainManager.cs b/Artemis/Artemis/Managers/MainManager.cs index f9a8adb04..68a50fd42 100644 --- a/Artemis/Artemis/Managers/MainManager.cs +++ b/Artemis/Artemis/Managers/MainManager.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.Linq; using System.Reflection; using System.Threading.Tasks; @@ -10,6 +9,7 @@ using Artemis.Utilities; using Artemis.Utilities.DataReaders; using Artemis.Utilities.GameState; using Artemis.ViewModels; +using Microsoft.Win32; using Ninject; using Ninject.Extensions.Logging; @@ -51,6 +51,9 @@ namespace Artemis.Managers var updateTask = new Task(Updater.UpdateApp); updateTask.Start(); + // Listen for power mode changes + SystemEvents.PowerModeChanged += OnPowerChange; + Logger.Info("Intialized MainManager"); Logger.Info($"Artemis version {Assembly.GetExecutingAssembly().GetName().Version} is ready!"); } @@ -83,6 +86,23 @@ namespace Artemis.Managers public event EventHandler OnEnabledChangedEvent; + /// + /// Restarts the loop manager when the system resumes + /// + /// + /// + private async void OnPowerChange(object sender, PowerModeChangedEventArgs e) + { + if (e.Mode != PowerModes.Resume) + return; + + Logger.Debug("Restarting for OnPowerChange"); + DisableProgram(); + // Wait an extra while for device providers to be fully ready + await Task.Delay(2000); + EnableProgram(); + } + /// /// Loads the last active effect and starts the program /// @@ -124,13 +144,11 @@ namespace Artemis.Managers // If the currently active effect is a no longer running game, get rid of it. var activeGame = EffectManager.ActiveEffect as GameModel; if (activeGame != null) - { if (!runningProcesses.Any(p => p.ProcessName == activeGame.ProcessName && p.HasExited == false)) { Logger.Info("Disabling game: {0}", activeGame.Name); EffectManager.DisableGame(activeGame); } - } // Look for running games, stopping on the first one that's found. var newGame = EffectManager.EnabledGames diff --git a/Artemis/Artemis/Profiles/Lua/Modules/LuaKeyboardModule.cs b/Artemis/Artemis/Profiles/Lua/Modules/LuaKeyboardModule.cs index b964a8c5a..7c6905d5b 100644 --- a/Artemis/Artemis/Profiles/Lua/Modules/LuaKeyboardModule.cs +++ b/Artemis/Artemis/Profiles/Lua/Modules/LuaKeyboardModule.cs @@ -1,49 +1,49 @@ -using System; -using System.Diagnostics; -using System.Windows.Forms; -using Artemis.DeviceProviders; -using Artemis.Managers; -using MoonSharp.Interpreter; - -namespace Artemis.Profiles.Lua.Modules -{ - [MoonSharpUserData] - public class LuaKeyboardModule : LuaModule - { - private readonly KeyboardProvider _keyboardProvider; - - public LuaKeyboardModule(LuaManager luaManager) : base(luaManager) - { - _keyboardProvider = luaManager.KeyboardProvider; - - // Register the KeyMatch type for usage in GetKeyPosition - UserData.RegisterType(typeof(KeyMatch)); - } - - public override string ModuleName => "Keyboard"; - - public string Name => _keyboardProvider.Name; - public string Slug => _keyboardProvider.Slug; - public int Width => _keyboardProvider.Width; - public int Height => _keyboardProvider.Height; - - public void PressKeys(string keys) - { - SendKeys.SendWait(keys); - } - - public KeyMatch? GetKeyPosition(string key) - { - // Convert string to Keys enum, I'm not sure if built-in enums can be converted automatically - try +using System; +using System.Diagnostics; +using System.Windows.Forms; +using Artemis.DeviceProviders; +using Artemis.Managers; +using MoonSharp.Interpreter; + +namespace Artemis.Profiles.Lua.Modules +{ + [MoonSharpUserData] + public class LuaKeyboardModule : LuaModule + { + private readonly KeyboardProvider _keyboardProvider; + + public LuaKeyboardModule(LuaManager luaManager) : base(luaManager) + { + _keyboardProvider = luaManager.KeyboardProvider; + + // Register the KeyMatch type for usage in GetKeyPosition + UserData.RegisterType(typeof(KeyMatch)); + } + + public override string ModuleName => "Keyboard"; + + public string Name => _keyboardProvider.Name; + public string Slug => _keyboardProvider.Slug; + public int Width => _keyboardProvider.Width; + public int Height => _keyboardProvider.Height; + + public void PressKeys(string keys) + { + SendKeys.SendWait(keys); + } + + public KeyMatch? GetKeyPosition(string key) + { + // Convert string to Keys enum, I'm not sure if built-in enums can be converted automatically + try { var keyEnum = (Keys)Enum.Parse(typeof(Keys), key); - return _keyboardProvider.GetKeyPosition(keyEnum); - } - catch (ArgumentException) - { - throw new ScriptRuntimeException($"Key '{key}' not found"); - } - } - } + return _keyboardProvider.GetKeyPosition(keyEnum); + } + catch (ArgumentException) + { + throw new ScriptRuntimeException($"Key '{key}' not found"); + } + } + } } \ No newline at end of file