diff --git a/WinRemoteControl/Actions/IAction.cs b/WinRemoteControl/Actions/IAction.cs index 3464f85..6a61dc7 100644 --- a/WinRemoteControl/Actions/IAction.cs +++ b/WinRemoteControl/Actions/IAction.cs @@ -2,7 +2,7 @@ namespace WinRemoteControl.Actions { - interface IAction + public interface IAction { public Result DoAction(); } diff --git a/WinRemoteControl/Actions/MediaNextSongAction.cs b/WinRemoteControl/Actions/MediaNextSongAction.cs new file mode 100644 index 0000000..0a41e1f --- /dev/null +++ b/WinRemoteControl/Actions/MediaNextSongAction.cs @@ -0,0 +1,20 @@ +using FluentResults; +using System; +using System.Runtime.InteropServices; +using Serilog; + +namespace WinRemoteControl.Actions +{ + class MediaNextSongAction : IAction + { + [DllImport("user32.dll")] + public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo); + + public Result DoAction() + { + Log.Information("Skipping to next song"); + keybd_event(Constants.VK_MEDIA_NEXT_TRACK, 0, Constants.KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); // Next Track + return Result.Ok(); + } + } +} diff --git a/WinRemoteControl/Actions/MediaPrevSongAction.cs b/WinRemoteControl/Actions/MediaPrevSongAction.cs new file mode 100644 index 0000000..d599eb4 --- /dev/null +++ b/WinRemoteControl/Actions/MediaPrevSongAction.cs @@ -0,0 +1,20 @@ +using FluentResults; +using System; +using System.Runtime.InteropServices; +using Serilog; + +namespace WinRemoteControl.Actions +{ + class MediaPrevSongAction : IAction + { + [DllImport("user32.dll")] + public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo); + + public Result DoAction() + { + Log.Information("Skipping to previous song"); + keybd_event(Constants.VK_MEDIA_PREV_TRACK, 0, Constants.KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); // Next Track + return Result.Ok(); + } + } +} diff --git a/WinRemoteControl/Constants.cs b/WinRemoteControl/Constants.cs index ebed17f..f178232 100644 --- a/WinRemoteControl/Constants.cs +++ b/WinRemoteControl/Constants.cs @@ -6,10 +6,17 @@ static class Constants public const int APPCOMMAND_VOLUME_UP = 0xA0000; public const int APPCOMMAND_VOLUME_DOWN = 0x90000; public const int WM_APPCOMMAND = 0x319; + public const int VK_MEDIA_NEXT_TRACK = 0xB0; + public const int VK_MEDIA_PLAY_PAUSE = 0xB3; + public const int VK_MEDIA_PREV_TRACK = 0xB1; + public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag + public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag // Topic public const string TOPIC_TOGGLE_TEAMS_MUTE = "control/toggle_teams_mute"; public const string TOPIC_VOLUME_UP = "control/volume_up"; public const string TOPIC_VOLUME_DOWN = "control/volume_down"; + public const string TOPIC_MEDIA_NEXT_SONG = "control/media_next_song"; + public const string TOPIC_MEDIA_PREV_SONG = "control/media_prev_song"; } } diff --git a/WinRemoteControl/MainForm.cs b/WinRemoteControl/MainForm.cs index 39720fb..01e25ff 100644 --- a/WinRemoteControl/MainForm.cs +++ b/WinRemoteControl/MainForm.cs @@ -17,35 +17,39 @@ namespace WinRemoteControl public partial class MainForm : Form { private IManagedMqttClient? mqttClient; - - private MqttTopicFilter[] topicsToSubscribe = { - new MqttTopicFilter { Topic = Constants.TOPIC_TOGGLE_TEAMS_MUTE }, - new MqttTopicFilter { Topic = Constants.TOPIC_VOLUME_UP }, - new MqttTopicFilter { Topic = Constants.TOPIC_VOLUME_DOWN } - }; + private readonly Dictionary topicsAndActions; public MainForm() { SetupLog(); InitializeComponent(); SetEventListeners(); + topicsAndActions = SetupTopicsAndActions(); Log.Information("Application started"); } + public Dictionary SetupTopicsAndActions() + { + return new Dictionary() { + { Constants.TOPIC_TOGGLE_TEAMS_MUTE , new ToggleMuteTeamsAction() }, + { Constants.TOPIC_VOLUME_UP , new VolumeDownAction(this) }, + { Constants.TOPIC_VOLUME_DOWN , new VolumeDownAction(this) }, + { Constants.TOPIC_MEDIA_NEXT_SONG , new MediaNextSongAction() }, + { Constants.TOPIC_MEDIA_PREV_SONG , new MediaPrevSongAction() }, + }; + } + private void DoActionForTopic(string topic, string payload) { - if (topic == Constants.TOPIC_TOGGLE_TEAMS_MUTE) + IAction action = topicsAndActions[topic]; + if (action != null) { - new ToggleMuteTeamsAction().DoAction(); - } - else if (topic == Constants.TOPIC_VOLUME_UP) - { - new VolumeUpAction(this).DoAction(); - } - else if (topic == Constants.TOPIC_VOLUME_DOWN) + action.DoAction(); + } + else { - new VolumeDownAction(this).DoAction(); + Log.Warning("Error doing action for topic, topic received is not a known one"); } } @@ -129,10 +133,11 @@ public async Task HandleConnectedAsync(MqttClientConnectedEventArgs x) Log.Information($"MQTT client connected - {item}"); // Subscribe to topics - Log.Information($"About to subscribe to topics: [{string.Join(",", topicsToSubscribe.Select(t => t.Topic))}]"); + Log.Information($"About to subscribe to topics: [{string.Join(",", topicsAndActions.Keys)}]"); + List filtersToSubscribe = topicsAndActions.Keys.Select(topic => new MqttTopicFilter { Topic = topic }).ToList(); if (this.mqttClient != null) { - await this.mqttClient.SubscribeAsync(topicsToSubscribe); + await this.mqttClient.SubscribeAsync(filtersToSubscribe); } else { diff --git a/WinRemoteControl/WinRemoteControl.csproj b/WinRemoteControl/WinRemoteControl.csproj index 6656b41..586da2d 100644 --- a/WinRemoteControl/WinRemoteControl.csproj +++ b/WinRemoteControl/WinRemoteControl.csproj @@ -17,7 +17,7 @@ https://github.com/pulimento 1.0.1.0 1.0.1.0 - 1.0.1 + 1.0.2 GPL-3.0-only true