From ccd496c16818c010218b5bd205d0db0db1e5fb6f Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 17 Feb 2024 21:21:29 +0100 Subject: [PATCH] Initial commit --- .gitignore | 5 ++ .idea/.idea.CSMediaControl/.idea/.gitignore | 13 ++++ .../.idea.CSMediaControl/.idea/encodings.xml | 4 ++ .../.idea/indexLayout.xml | 8 +++ .idea/.idea.CSMediaControl/.idea/vcs.xml | 6 ++ CSMediaControl.sln | 16 +++++ CSMediaControl/CSMediaControl.csproj | 15 ++++ CSMediaControl/MediaController.cs | 33 +++++++++ CSMediaControl/Program.cs | 11 +++ CSMediaControl/Watchdog.cs | 72 +++++++++++++++++++ 10 files changed, 183 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.idea.CSMediaControl/.idea/.gitignore create mode 100644 .idea/.idea.CSMediaControl/.idea/encodings.xml create mode 100644 .idea/.idea.CSMediaControl/.idea/indexLayout.xml create mode 100644 .idea/.idea.CSMediaControl/.idea/vcs.xml create mode 100644 CSMediaControl.sln create mode 100644 CSMediaControl/CSMediaControl.csproj create mode 100644 CSMediaControl/MediaController.cs create mode 100644 CSMediaControl/Program.cs create mode 100644 CSMediaControl/Watchdog.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/.idea/.idea.CSMediaControl/.idea/.gitignore b/.idea/.idea.CSMediaControl/.idea/.gitignore new file mode 100644 index 0000000..dc00553 --- /dev/null +++ b/.idea/.idea.CSMediaControl/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/contentModel.xml +/.idea.CSMediaControl.iml +/projectSettingsUpdater.xml +/modules.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.CSMediaControl/.idea/encodings.xml b/.idea/.idea.CSMediaControl/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.CSMediaControl/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.CSMediaControl/.idea/indexLayout.xml b/.idea/.idea.CSMediaControl/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.CSMediaControl/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.CSMediaControl/.idea/vcs.xml b/.idea/.idea.CSMediaControl/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.CSMediaControl/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CSMediaControl.sln b/CSMediaControl.sln new file mode 100644 index 0000000..9456294 --- /dev/null +++ b/CSMediaControl.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSMediaControl", "CSMediaControl\CSMediaControl.csproj", "{96EB5D7F-F477-4AFA-BCF6-F5F4A85A88C2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {96EB5D7F-F477-4AFA-BCF6-F5F4A85A88C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {96EB5D7F-F477-4AFA-BCF6-F5F4A85A88C2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {96EB5D7F-F477-4AFA-BCF6-F5F4A85A88C2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {96EB5D7F-F477-4AFA-BCF6-F5F4A85A88C2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/CSMediaControl/CSMediaControl.csproj b/CSMediaControl/CSMediaControl.csproj new file mode 100644 index 0000000..fd7d1ce --- /dev/null +++ b/CSMediaControl/CSMediaControl.csproj @@ -0,0 +1,15 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + + diff --git a/CSMediaControl/MediaController.cs b/CSMediaControl/MediaController.cs new file mode 100644 index 0000000..36d7131 --- /dev/null +++ b/CSMediaControl/MediaController.cs @@ -0,0 +1,33 @@ +using System.Runtime.InteropServices; + +namespace CSMediaControl; + +internal static class MediaController +{ + [DllImport("user32.dll")] + // ReSharper disable once IdentifierTypo + private static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo); + private const int KeyEventFExtendedKey = 1; + //private const int KeyEventFKeyup = 0; + //https://learn.microsoft.com/de-de/windows/win32/inputdev/virtual-key-codes + + public static void PlayPause() + { + keybd_event(0xB3, 0, KeyEventFExtendedKey, IntPtr.Zero); + } + + public static void Next() + { + keybd_event(0xB0, 0, KeyEventFExtendedKey, IntPtr.Zero); + } + + public static void Previous() + { + keybd_event(0xB1, 0, KeyEventFExtendedKey, IntPtr.Zero); + } + + public static void Stop() + { + keybd_event(0xB2, 0, KeyEventFExtendedKey, IntPtr.Zero); + } +} \ No newline at end of file diff --git a/CSMediaControl/Program.cs b/CSMediaControl/Program.cs new file mode 100644 index 0000000..09292d0 --- /dev/null +++ b/CSMediaControl/Program.cs @@ -0,0 +1,11 @@ +// See https://aka.ms/new-console-template for more information + +using CSMediaControl; + +ConsoleKey key; +do +{ + Console.WriteLine("Is media currently playing? (y/n)"); + key = Console.ReadKey(true).Key; +} while (key != ConsoleKey.Y && key != ConsoleKey.N); +Watchdog _ = new (key == ConsoleKey.Y); \ No newline at end of file diff --git a/CSMediaControl/Watchdog.cs b/CSMediaControl/Watchdog.cs new file mode 100644 index 0000000..cc700bf --- /dev/null +++ b/CSMediaControl/Watchdog.cs @@ -0,0 +1,72 @@ +using CS2GSI; +using GlaxLogger; +using Microsoft.Extensions.Logging; + +namespace CSMediaControl; +using GSI = CS2GSI.CS2GSI; + +public class Watchdog : IDisposable, IAsyncDisposable +{ + private bool _keepRunning = true; + private readonly GSI _gsi; + private readonly Logger _logger; + private bool _mediaRunning; + + public Watchdog(bool mediaRunning) + { + this._mediaRunning = mediaRunning; + this._logger = new Logger(LogLevel.Information, consoleOut: Console.Out); + this._gsi = new GSI(this._logger); + + this._gsi.OnRoundStart += MusicStart; + this._gsi.OnRoundOver += MusicStop; + this._gsi.OnDeath += MusicStop; + + Console.WriteLine("Press p to pause/play media."); + while (_keepRunning) + { + if (Console.KeyAvailable && Console.ReadKey(true).Key is ConsoleKey.P) + { + if(this._mediaRunning) + MusicStop(null); + else + MusicStart(null); + }else + Thread.Sleep(10); + } + this._logger.Log(LogLevel.Information, "Bye!"); + } + + private void MusicStop(CS2EventArgs? _) + { + if (_mediaRunning) + { + this._logger.Log(LogLevel.Information, "Music Pause"); + MediaController.PlayPause(); + this._mediaRunning = false; + } + } + + private void MusicStart(CS2EventArgs? _) + { + if (!_mediaRunning) + { + this._logger.Log(LogLevel.Information, "Music Start"); + MediaController.PlayPause(); + this._mediaRunning = true; + } + } + + + public void Dispose() + { + this._keepRunning = false; + _logger.Dispose(); + } + + public async ValueTask DisposeAsync() + { + this._keepRunning = false; + await _logger.DisposeAsync(); + } +} \ No newline at end of file