Skip to content

Commit

Permalink
Merge pull request #7 from pulimento/feature/media_actions
Browse files Browse the repository at this point in the history
Add media actions
  • Loading branch information
pulimento committed Mar 23, 2022
2 parents 7977b8c + cacbb26 commit be4f07f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 19 deletions.
2 changes: 1 addition & 1 deletion WinRemoteControl/Actions/IAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WinRemoteControl.Actions
{
interface IAction
public interface IAction
{
public Result DoAction();
}
Expand Down
20 changes: 20 additions & 0 deletions WinRemoteControl/Actions/MediaNextSongAction.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
20 changes: 20 additions & 0 deletions WinRemoteControl/Actions/MediaPrevSongAction.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
7 changes: 7 additions & 0 deletions WinRemoteControl/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
39 changes: 22 additions & 17 deletions WinRemoteControl/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, IAction> topicsAndActions;

public MainForm()
{
SetupLog();
InitializeComponent();
SetEventListeners();
topicsAndActions = SetupTopicsAndActions();

Log.Information("Application started");
}

public Dictionary<string, IAction> SetupTopicsAndActions()
{
return new Dictionary<string, IAction>() {
{ 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");
}
}

Expand Down Expand Up @@ -129,10 +133,11 @@ public async Task<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<MqttTopicFilter> 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
{
Expand Down
2 changes: 1 addition & 1 deletion WinRemoteControl/WinRemoteControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<RepositoryUrl>https://github.com/pulimento</RepositoryUrl>
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<FileVersion>1.0.1.0</FileVersion>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Expand Down

0 comments on commit be4f07f

Please sign in to comment.