WingMan

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 9  →  ?path2? @ 10
File deleted
/trunk/WingMan/MouseKey/MouseKeyBindingsExchange.cs
File deleted
/trunk/WingMan/MouseKey/RemoteMouseKeyBindings.cs
File deleted
/trunk/WingMan/MouseKey/MouseKeyBindingExchange.cs
File deleted
/trunk/WingMan/MouseKey/MouseKeyBindings.cs
File deleted
/trunk/WingMan/MouseKey/MouseKeyBinding.cs
File deleted
/trunk/WingMan/MouseKey/RemoteMouseKeyBinding.cs
File deleted
/trunk/WingMan/MouseKey/MouseKeyBindingsSynchronizer.cs
File deleted
/trunk/WingMan/MouseKey/MouseKeyBindingsSynchronizedEventArgs.cs
/trunk/WingMan/MouseKey/ExecuteKeyBinding.cs
@@ -0,0 +1,23 @@
using System.Xml.Serialization;
 
namespace WingMan.MouseKey
{
public class ExecuteKeyBinding
{
[XmlIgnore] public static readonly XmlSerializer XmlSerializer =
new XmlSerializer(typeof(ExecuteKeyBinding));
 
public ExecuteKeyBinding()
{
}
 
public ExecuteKeyBinding(string nick, string name)
{
Nick = nick;
Name = name;
}
 
public string Nick { get; set; }
public string Name { get; set; }
}
}
/trunk/WingMan/MouseKey/KeyBinding.cs
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace WingMan.MouseKey
{
public class KeyBinding : IEquatable<KeyBinding>
{
public KeyBinding()
{
}
 
public KeyBinding(string name, List<string> keys) : this()
{
Name = name;
Keys = keys;
}
 
public string DisplayName => $"{Name} ({string.Join(" + ", Keys.ToArray())})";
 
public string Name { get; set; } = string.Empty;
 
public List<string> Keys { get; set; } = new List<string>();
 
public bool Equals(KeyBinding other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(Name, other.Name) && Keys.SequenceEqual(other.Keys);
}
 
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((KeyBinding) obj);
}
 
public override int GetHashCode()
{
unchecked
{
return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ (Keys != null ? Keys.GetHashCode() : 0);
}
}
}
}
/trunk/WingMan/MouseKey/KeyBindingExchange.cs
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Xml.Serialization;
 
namespace WingMan.MouseKey
{
public class KeyBindingExchange
{
[XmlIgnore] public static readonly XmlSerializer XmlSerializer =
new XmlSerializer(typeof(KeyBindingExchange));
 
public KeyBindingExchange()
{
}
 
public KeyBindingExchange(string nick, List<KeyBinding> keyBindings)
{
Nick = nick;
KeyBindings = keyBindings;
}
 
public string Nick { get; set; }
 
public List<KeyBinding> KeyBindings { get; set; }
}
}
/trunk/WingMan/MouseKey/KeyBindingExecutingEventArgs.cs
@@ -0,0 +1,16 @@
using System;
 
namespace WingMan.MouseKey
{
public class KeyBindingExecutingEventArgs : EventArgs
{
public KeyBindingExecutingEventArgs(string nick, string name)
{
Nick = nick;
Name = name;
}
 
public string Nick { get; set; }
public string Name { get; set; }
}
}
/trunk/WingMan/MouseKey/KeyBindingMatchedEventArgs.cs
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
 
namespace WingMan.MouseKey
{
public class KeyBindingMatchedEventArgs : EventArgs
{
public KeyBindingMatchedEventArgs(string nick, string name, List<string> keyCombo)
{
Nick = nick;
Name = name;
KeyCombo = keyCombo;
}
 
public string Nick { get; set; }
public string Name { get; set; }
public List<string> KeyCombo { get; set; }
}
}
/trunk/WingMan/MouseKey/KeyBindingsExchange.cs
@@ -0,0 +1,9 @@
using System.Collections.Generic;
 
namespace WingMan.MouseKey
{
public class KeyBindingsExchange
{
public List<KeyBindingExchange> ExchangeBindings { get; set; }
}
}
/trunk/WingMan/MouseKey/KeyBindingsSynchronizer.cs
@@ -0,0 +1,103 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet;
using WingMan.Communication;
 
namespace WingMan.MouseKey
{
public class KeyBindingsSynchronizer : IDisposable
{
public delegate void MouseKeyBindingsSynchronized(object sender, KeyBindingsSynchronizerEventArgs e);
 
public KeyBindingsSynchronizer(LocalKeyBindings localKeyBindings, MqttCommunication mqttCommunication,
TaskScheduler taskScheduler, CancellationToken cancellationToken)
{
LocalKeyBindings = localKeyBindings;
MqttCommunication = mqttCommunication;
CancellationToken = cancellationToken;
TaskScheduler = taskScheduler;
 
SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<KeyBinding>>();
 
MqttCommunication.OnMessageReceived += MqttCommunicationOnMessageReceived;
 
Task.Run(PeriodicSynchronize, CancellationToken);
}
 
private LocalKeyBindings LocalKeyBindings { get; }
 
private ConcurrentDictionary<string, List<KeyBinding>> SynchronizedMouseKeyBindings { get; }
 
private MqttCommunication MqttCommunication { get; }
 
private CancellationToken CancellationToken { get; }
private TaskScheduler TaskScheduler { get; }
 
public void Dispose()
{
MqttCommunication.OnMessageReceived -= MqttCommunicationOnMessageReceived;
}
 
public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized;
 
private async void MqttCommunicationOnMessageReceived(object sender,
MqttApplicationMessageReceivedEventArgs e)
{
if (e.ApplicationMessage.Topic != "exchange")
return;
 
using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload))
{
memoryStream.Position = 0L;
 
var mouseKeyBindingsExchange =
(KeyBindingExchange) KeyBindingExchange.XmlSerializer.Deserialize(memoryStream);
 
// Do not add own bindings.
if (string.Equals(mouseKeyBindingsExchange.Nick, MqttCommunication.Nick))
return;
 
if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) &&
mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.KeyBindings))
return;
 
await Task.Delay(0)
.ContinueWith(
_ => OnMouseKeyBindingsSynchronized?.Invoke(sender,
new KeyBindingsSynchronizerEventArgs(
mouseKeyBindingsExchange)),
CancellationToken, TaskContinuationOptions.None, TaskScheduler);
 
// Nick does not exist so the bindings will be added.
SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick,
mouseKeyBindingsExchange.KeyBindings, (s, list) => mouseKeyBindingsExchange.KeyBindings);
}
}
 
private async Task PeriodicSynchronize()
{
do
{
await Task.Delay(1000, CancellationToken);
 
if (!MqttCommunication.Running)
continue;
 
using (var memoryStream = new MemoryStream())
{
KeyBindingExchange.XmlSerializer.Serialize(memoryStream,
new KeyBindingExchange(MqttCommunication.Nick, LocalKeyBindings.Bindings));
 
memoryStream.Position = 0L;
 
await MqttCommunication.Broadcast("exchange", memoryStream.ToArray());
}
} while (!CancellationToken.IsCancellationRequested);
}
}
}
/trunk/WingMan/MouseKey/KeyBindingsSynchronizerEventArgs.cs
@@ -0,0 +1,14 @@
using System;
 
namespace WingMan.MouseKey
{
public class KeyBindingsSynchronizerEventArgs : EventArgs
{
public KeyBindingsSynchronizerEventArgs(KeyBindingExchange keyExchangeBindings)
{
Bindings = keyExchangeBindings;
}
 
public KeyBindingExchange Bindings { get; set; }
}
}
/trunk/WingMan/MouseKey/KeyInterceptor.cs
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using WingMan.Communication;
 
namespace WingMan.MouseKey
{
public class KeyInterceptor : IDisposable
{
public delegate void MouseKeyBindingMatched(object sender, KeyBindingMatchedEventArgs args);
 
public KeyInterceptor(RemoteKeyBindings remoteKeyBindings, MqttCommunication mqttCommunication,
TaskScheduler taskScheduler, CancellationToken cancellationToken)
{
RemoteKeyBindings = remoteKeyBindings;
MqttCommunication = mqttCommunication;
TaskScheduler = taskScheduler;
CancellationToken = cancellationToken;
 
KeyCombo = new List<string>();
 
MouseKeyGloalHook = Hook.GlobalEvents();
MouseKeyGloalHook.KeyUp += MouseKeyGloalHookOnKeyUp;
MouseKeyGloalHook.KeyDown += MouseKeyGloalHookOnKeyDown;
}
 
private RemoteKeyBindings RemoteKeyBindings { get; }
private MqttCommunication MqttCommunication { get; }
private TaskScheduler TaskScheduler { get; }
private CancellationToken CancellationToken { get; }
 
private IKeyboardMouseEvents MouseKeyGloalHook { get; }
 
public List<string> KeyCombo { get; set; }
 
public void Dispose()
{
MouseKeyGloalHook.KeyUp -= MouseKeyGloalHookOnKeyUp;
MouseKeyGloalHook.KeyDown -= MouseKeyGloalHookOnKeyDown;
 
MouseKeyGloalHook.Dispose();
}
 
public event MouseKeyBindingMatched OnMouseKeyBindingMatched;
 
private void MouseKeyGloalHookOnKeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = false;
 
if (KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key)) KeyCombo.Add(key);
}
 
private void MouseKeyGloalHookOnKeyUp(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = false;
 
var combo = new List<string>(KeyCombo);
 
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Run(() => SimulateMouseKey(new List<string>(combo)), CancellationToken);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
 
KeyCombo.Clear();
}
 
private async Task SimulateMouseKey(List<string> mouseKeyCombo)
{
foreach (var binding in RemoteKeyBindings.Bindings)
{
if (!binding.Keys.SequenceEqual(mouseKeyCombo))
continue;
 
// Raise the match event.
await Task.Delay(0, CancellationToken)
.ContinueWith(
_ => OnMouseKeyBindingMatched?.Invoke(this,
new KeyBindingMatchedEventArgs(binding.Nick, binding.Name, binding.Keys)),
CancellationToken,
TaskContinuationOptions.None, TaskScheduler);
 
using (var memoryStream = new MemoryStream())
{
ExecuteKeyBinding.XmlSerializer.Serialize(memoryStream,
new ExecuteKeyBinding(binding.Nick, binding.Name));
 
memoryStream.Position = 0L;
 
await MqttCommunication.Broadcast("execute", memoryStream.ToArray());
}
}
}
}
}
/trunk/WingMan/MouseKey/KeySimulator.cs
@@ -0,0 +1,122 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MQTTnet;
using SimWinInput;
using WingMan.Communication;
 
namespace WingMan.MouseKey
{
public class KeySimulator : IDisposable
{
public delegate void MouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args);
 
public KeySimulator(LocalKeyBindings localLocalKeyBindings, MqttCommunication mqttCommunication,
TaskScheduler formTaskScheduler, CancellationToken cancellationToken)
{
LocalLocalKeyBindings = localLocalKeyBindings;
MqttCommunication = mqttCommunication;
TaskScheduler = formTaskScheduler;
CancellationToken = cancellationToken;
 
MqttCommunication.OnMessageReceived += OnMqttMessageReceived;
}
 
private MqttCommunication MqttCommunication { get; }
private TaskScheduler TaskScheduler { get; }
private CancellationToken CancellationToken { get; }
private LocalKeyBindings LocalLocalKeyBindings { get; }
 
public void Dispose()
{
MqttCommunication.OnMessageReceived -= OnMqttMessageReceived;
}
 
public event MouseKeyBindingExecuting OnMouseKeyBindingExecuting;
 
private async void OnMqttMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
{
if (e.ApplicationMessage.Topic != "execute")
return;
 
using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload))
{
var executeMouseKeyBinding =
(ExecuteKeyBinding) ExecuteKeyBinding.XmlSerializer.Deserialize(memoryStream);
 
// Do not process own mouse key bindings.
if (!string.Equals(executeMouseKeyBinding.Nick, MqttCommunication.Nick, StringComparison.Ordinal))
return;
 
await Task.Delay(0, CancellationToken)
.ContinueWith(
_ => OnMouseKeyBindingExecuting?.Invoke(sender,
new KeyBindingExecutingEventArgs(executeMouseKeyBinding.Nick,
executeMouseKeyBinding.Name)),
CancellationToken,
TaskContinuationOptions.None, TaskScheduler);
 
Simulate(executeMouseKeyBinding);
}
}
 
private void Simulate(ExecuteKeyBinding executeBinding)
{
foreach (var localBinding in LocalLocalKeyBindings.Bindings)
{
if (!string.Equals(localBinding.Name, executeBinding.Name, StringComparison.Ordinal))
continue;
 
// Press
foreach (var key in localBinding.Keys)
{
if (KeyConversion.StringToKeys.TryGetValue(key, out var pressKey))
{
SimKeyboard.KeyDown(pressKey);
continue;
}
 
if (KeyConversion.StringToMouseButtons.TryGetValue(key, out var pressMouse))
switch (pressMouse)
{
case MouseButtons.Left:
SimMouse.Act(SimMouse.Action.LeftButtonDown, Cursor.Position.X, Cursor.Position.Y);
break;
case MouseButtons.Middle:
SimMouse.Act(SimMouse.Action.MiddleButtonDown, Cursor.Position.X, Cursor.Position.Y);
break;
case MouseButtons.Right:
SimMouse.Act(SimMouse.Action.RightButtonDown, Cursor.Position.X, Cursor.Position.Y);
break;
}
}
 
// Depress
foreach (var key in localBinding.Keys)
{
if (KeyConversion.StringToKeys.TryGetValue(key, out var pressKey))
{
SimKeyboard.KeyUp(pressKey);
continue;
}
 
if (KeyConversion.StringToMouseButtons.TryGetValue(key, out var pressMouse))
switch (pressMouse)
{
case MouseButtons.Left:
SimMouse.Act(SimMouse.Action.LeftButtonUp, Cursor.Position.X, Cursor.Position.Y);
break;
case MouseButtons.Middle:
SimMouse.Act(SimMouse.Action.MiddleButtonUp, Cursor.Position.X, Cursor.Position.Y);
break;
case MouseButtons.Right:
SimMouse.Act(SimMouse.Action.RightButtonUp, Cursor.Position.X, Cursor.Position.Y);
break;
}
}
}
}
}
}
/trunk/WingMan/MouseKey/LocalKeyBindings.cs
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Xml.Serialization;
 
namespace WingMan.MouseKey
{
public class LocalKeyBindings
{
[XmlIgnore] public static readonly XmlSerializer XmlSerializer = new XmlSerializer(typeof(LocalKeyBindings));
 
public LocalKeyBindings()
{
}
 
public LocalKeyBindings(List<KeyBinding> bindings) : this()
{
Bindings = bindings;
}
 
public List<KeyBinding> Bindings { get; set; }
}
}
/trunk/WingMan/MouseKey/RemoteKeyBinding.cs
@@ -0,0 +1,24 @@
using System.Collections.Generic;
 
namespace WingMan.MouseKey
{
public class RemoteKeyBinding
{
public RemoteKeyBinding()
{
}
 
public RemoteKeyBinding(string nick, string name, List<string> mouseKeyCombo)
{
Nick = nick;
Name = name;
Keys = mouseKeyCombo;
}
 
public string Nick { get; set; }
 
public string Name { get; set; }
 
public List<string> Keys { get; set; }
}
}
/trunk/WingMan/MouseKey/RemoteKeyBindings.cs
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Xml.Serialization;
 
namespace WingMan.MouseKey
{
public class RemoteKeyBindings
{
[XmlIgnore] public static readonly XmlSerializer XmlSerializer =
new XmlSerializer(typeof(RemoteKeyBindings));
 
public RemoteKeyBindings()
{
}
 
public RemoteKeyBindings(List<RemoteKeyBinding> bindings)
{
Bindings = bindings;
}
 
public List<RemoteKeyBinding> Bindings { get; set; }
}
}