From 34de5e855a94be54f33a0ef91628334882922eb2 Mon Sep 17 00:00:00 2001 From: NicolasConstant Date: Mon, 9 Nov 2015 00:23:11 -0500 Subject: [PATCH] Refactorisation --- ConsoleApp/Program.cs | 14 ++--- .../AudioManagement/CsCoreSoundAnalyser.cs} | 4 +- .../AudioManagement}/NAudioSoundAnalyser.cs | 5 +- .../DummyMessageDisplayer.cs | 11 ++++ .../MessageManagement}/MessageDisplayer.cs | 2 +- .../PowerRequestContextHandler.cs} | 34 ++++++++++-- .../SetThreadExecutionStateHandler.cs | 53 +++++++++++++++++++ .../Domain/SpotifySaveModeStopper.cs | 26 +-------- .../SpotifySleepModeStopper.csproj | 13 +++-- .../Tools/DummyMessageDisplayer.cs | 16 ------ SpotifySleepModeStopper/Tools/Repeat.cs | 29 ++++++++++ WindowsService/Service1.cs | 5 +- 12 files changed, 148 insertions(+), 64 deletions(-) rename SpotifySleepModeStopper/{Tools/SoundAnalyser.cs => Domain/AudioManagement/CsCoreSoundAnalyser.cs} (92%) rename SpotifySleepModeStopper/{Tools => Domain/AudioManagement}/NAudioSoundAnalyser.cs (85%) create mode 100644 SpotifySleepModeStopper/Domain/MessageManagement/DummyMessageDisplayer.cs rename SpotifySleepModeStopper/{Tools => Domain/MessageManagement}/MessageDisplayer.cs (78%) rename SpotifySleepModeStopper/{Tools/ConstantDisplayAndPowerHandler.cs => Domain/PowerManagement/PowerRequestContextHandler.cs} (73%) create mode 100644 SpotifySleepModeStopper/Domain/PowerManagement/SetThreadExecutionStateHandler.cs delete mode 100644 SpotifySleepModeStopper/Tools/DummyMessageDisplayer.cs create mode 100644 SpotifySleepModeStopper/Tools/Repeat.cs diff --git a/ConsoleApp/Program.cs b/ConsoleApp/Program.cs index 6775d40..fc4d814 100644 --- a/ConsoleApp/Program.cs +++ b/ConsoleApp/Program.cs @@ -4,6 +4,9 @@ using System.Text; using System.Threading.Tasks; using SpotifyTools.Domain; +using SpotifyTools.Domain.AudioManagement; +using SpotifyTools.Domain.MessageManagement; +using SpotifyTools.Domain.PowerManagement; using SpotifyTools.Tools; namespace PreventSleep @@ -12,14 +15,11 @@ class Program { static void Main(string[] args) { - var spotifyAnalyser = new SpotifySaveModeStopper(new MessageDisplayer(), new PreventSleepScreen(), new SoundAnalyser()); + var spotifyAnalyser = new SpotifySaveModeStopper(new MessageDisplayer(), new PowerRequestContextHandler(), new CsCoreSoundAnalyser()); spotifyAnalyser.StartListening(); - - while (true) - Task.Delay(3000).Wait(); - - - + + Console.ReadKey(); + //var preventer = new PreventSleepScreen(); //while (true) //{ diff --git a/SpotifySleepModeStopper/Tools/SoundAnalyser.cs b/SpotifySleepModeStopper/Domain/AudioManagement/CsCoreSoundAnalyser.cs similarity index 92% rename from SpotifySleepModeStopper/Tools/SoundAnalyser.cs rename to SpotifySleepModeStopper/Domain/AudioManagement/CsCoreSoundAnalyser.cs index d418bee..e599ba8 100644 --- a/SpotifySleepModeStopper/Tools/SoundAnalyser.cs +++ b/SpotifySleepModeStopper/Domain/AudioManagement/CsCoreSoundAnalyser.cs @@ -3,9 +3,9 @@ using CSCore.CoreAudioAPI; using SpotifyTools.Contracts; -namespace SpotifyTools.Tools +namespace SpotifyTools.Domain.AudioManagement { - public class SoundAnalyser : ISoundAnalyser + public class CsCoreSoundAnalyser : ISoundAnalyser { public bool IsWindowsOutputingSound() { diff --git a/SpotifySleepModeStopper/Tools/NAudioSoundAnalyser.cs b/SpotifySleepModeStopper/Domain/AudioManagement/NAudioSoundAnalyser.cs similarity index 85% rename from SpotifySleepModeStopper/Tools/NAudioSoundAnalyser.cs rename to SpotifySleepModeStopper/Domain/AudioManagement/NAudioSoundAnalyser.cs index df88d63..eff8c40 100644 --- a/SpotifySleepModeStopper/Tools/NAudioSoundAnalyser.cs +++ b/SpotifySleepModeStopper/Domain/AudioManagement/NAudioSoundAnalyser.cs @@ -1,12 +1,9 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using NAudio.CoreAudioApi; using SpotifyTools.Contracts; -namespace SpotifyTools.Tools +namespace SpotifyTools.Domain.AudioManagement { public class NAudioSoundAnalyser : ISoundAnalyser { diff --git a/SpotifySleepModeStopper/Domain/MessageManagement/DummyMessageDisplayer.cs b/SpotifySleepModeStopper/Domain/MessageManagement/DummyMessageDisplayer.cs new file mode 100644 index 0000000..0cda46c --- /dev/null +++ b/SpotifySleepModeStopper/Domain/MessageManagement/DummyMessageDisplayer.cs @@ -0,0 +1,11 @@ +using SpotifyTools.Contracts; + +namespace SpotifyTools.Domain.MessageManagement +{ + public class DummyMessageDisplayer : IMessageDisplayer + { + public void OutputMessage(string mess) + { + } + } +} diff --git a/SpotifySleepModeStopper/Tools/MessageDisplayer.cs b/SpotifySleepModeStopper/Domain/MessageManagement/MessageDisplayer.cs similarity index 78% rename from SpotifySleepModeStopper/Tools/MessageDisplayer.cs rename to SpotifySleepModeStopper/Domain/MessageManagement/MessageDisplayer.cs index 931eead..e5d1561 100644 --- a/SpotifySleepModeStopper/Tools/MessageDisplayer.cs +++ b/SpotifySleepModeStopper/Domain/MessageManagement/MessageDisplayer.cs @@ -1,7 +1,7 @@ using System; using SpotifyTools.Contracts; -namespace SpotifyTools.Tools +namespace SpotifyTools.Domain.MessageManagement { public class MessageDisplayer : IMessageDisplayer { diff --git a/SpotifySleepModeStopper/Tools/ConstantDisplayAndPowerHandler.cs b/SpotifySleepModeStopper/Domain/PowerManagement/PowerRequestContextHandler.cs similarity index 73% rename from SpotifySleepModeStopper/Tools/ConstantDisplayAndPowerHandler.cs rename to SpotifySleepModeStopper/Domain/PowerManagement/PowerRequestContextHandler.cs index fd29a37..694f5a9 100644 --- a/SpotifySleepModeStopper/Tools/ConstantDisplayAndPowerHandler.cs +++ b/SpotifySleepModeStopper/Domain/PowerManagement/PowerRequestContextHandler.cs @@ -2,9 +2,9 @@ using System.Runtime.InteropServices; using SpotifyTools.Contracts; -namespace SpotifyTools.Tools +namespace SpotifyTools.Domain.PowerManagement { - public class PreventSleepScreen : IPreventSleepScreen + public class PowerRequestContextHandler : IPreventSleepScreen { #region prevent screensaver, display dimming and automatically sleeping PowerRequestContext _powerRequestContext; @@ -74,6 +74,9 @@ public struct PowerRequestContextDetailed } #endregion + [DllImport("kernel32.dll")] + static extern uint GetLastError(); + /// /// Prevent screensaver, display dimming and power saving. This function wraps PInvokes on Win32 API. /// @@ -92,8 +95,8 @@ public void EnableConstantDisplayAndPower(bool enableConstantDisplayAndPower) _powerRequest = PowerCreateRequest(ref _powerRequestContext); // Set the request - PowerSetRequest(_powerRequest, PowerRequestType.PowerRequestSystemRequired); - PowerSetRequest(_powerRequest, PowerRequestType.PowerRequestDisplayRequired); + var s = PowerSetRequest(_powerRequest, PowerRequestType.PowerRequestSystemRequired); + var s2 = PowerSetRequest(_powerRequest, PowerRequestType.PowerRequestDisplayRequired); } else { @@ -104,5 +107,28 @@ public void EnableConstantDisplayAndPower(bool enableConstantDisplayAndPower) CloseHandle(_powerRequest); } } + + //[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)] + //internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName); + + //[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] + //internal static extern IntPtr LoadLibrary(string dllToLoad); + + //private static bool PowerAvailabilityRequestsSupported() + //{ + // var ptr = LoadLibrary("kernel32.dll"); + // var ptr2 = GetProcAddress(ptr, "PowerSetRequest"); + + // if (ptr2 == IntPtr.Zero) + // { + // // Power availability requests NOT suppoted. + // return false; + // } + // else + // { + // // Power availability requests ARE suppoted. + // return true; + // } + //} } } \ No newline at end of file diff --git a/SpotifySleepModeStopper/Domain/PowerManagement/SetThreadExecutionStateHandler.cs b/SpotifySleepModeStopper/Domain/PowerManagement/SetThreadExecutionStateHandler.cs new file mode 100644 index 0000000..61a5c88 --- /dev/null +++ b/SpotifySleepModeStopper/Domain/PowerManagement/SetThreadExecutionStateHandler.cs @@ -0,0 +1,53 @@ +using System; +using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; +using SpotifyTools.Contracts; +using SpotifyTools.Tools; + +namespace SpotifyTools.Domain.PowerManagement +{ + public class SetThreadExecutionStateHandler : IPreventSleepScreen + { + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + private static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags); + + [Flags] + private enum ExecutionState : uint + { + EsAwaymodeRequired = 0x00000040, + EsContinuous = 0x80000000, + EsDisplayRequired = 0x00000002, + EsSystemRequired = 0x00000001 + } + + public static void PreventSleep() + { + SetThreadExecutionState(ExecutionState.EsDisplayRequired | ExecutionState.EsContinuous | ExecutionState.EsSystemRequired); + } + + public static void AllowSleep() + { + SetThreadExecutionState(ExecutionState.EsContinuous); + } + + private Task _keepScreenUp; + private CancellationTokenSource _cToken; + + public void EnableConstantDisplayAndPower(bool enableConstantDisplayAndPower) + { + if (enableConstantDisplayAndPower) + { + _cToken?.Cancel(); + _cToken = new CancellationTokenSource(); + + Repeat.Interval(TimeSpan.FromSeconds(10), PreventSleep, _cToken.Token); + } + else + { + _cToken?.Cancel(); + AllowSleep(); + } + } + } +} diff --git a/SpotifySleepModeStopper/Domain/SpotifySaveModeStopper.cs b/SpotifySleepModeStopper/Domain/SpotifySaveModeStopper.cs index 5d7d187..ad70110 100644 --- a/SpotifySleepModeStopper/Domain/SpotifySaveModeStopper.cs +++ b/SpotifySleepModeStopper/Domain/SpotifySaveModeStopper.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using SpotifyTools.Contracts; +using SpotifyTools.Tools; namespace SpotifyTools.Domain { @@ -34,7 +35,7 @@ public void StartListening() _cancellationTokenSource?.Cancel(); _cancellationTokenSource = new CancellationTokenSource(); _analyst = Repeat.Interval( - TimeSpan.FromSeconds(60), + TimeSpan.FromSeconds(10), AnalyseSpotifyStatus, _cancellationTokenSource.Token); } @@ -98,29 +99,6 @@ private bool IsSoundStreaming() return isSpotifyPlaying; } } - - internal static class Repeat - { - public static Task Interval( - TimeSpan pollInterval, - Action action, - CancellationToken token) - { - // We don't use Observable.Interval: - // If we block, the values start bunching up behind each other. - return Task.Factory.StartNew( - () => - { - for (;;) - { - if (token.WaitHandle.WaitOne(pollInterval)) - break; - - action(); - } - }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default); - } - } } diff --git a/SpotifySleepModeStopper/SpotifySleepModeStopper.csproj b/SpotifySleepModeStopper/SpotifySleepModeStopper.csproj index 173a1e7..e361e64 100644 --- a/SpotifySleepModeStopper/SpotifySleepModeStopper.csproj +++ b/SpotifySleepModeStopper/SpotifySleepModeStopper.csproj @@ -51,17 +51,20 @@ + - - - - - + + + + + + +