WingMan

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 10  →  ?path2? @ 9
File deleted
/trunk/WingMan/MouseKey/KeyBindingExecutingEventArgs.cs
File deleted
/trunk/WingMan/MouseKey/KeyBindingsSynchronizerEventArgs.cs
File deleted
/trunk/WingMan/MouseKey/LocalKeyBindings.cs
File deleted
/trunk/WingMan/MouseKey/KeyBindingsSynchronizer.cs
File deleted
/trunk/WingMan/MouseKey/KeyBindingExchange.cs
File deleted
/trunk/WingMan/MouseKey/KeySimulator.cs
File deleted
/trunk/WingMan/MouseKey/KeyBinding.cs
File deleted
/trunk/WingMan/MouseKey/KeyBindingsExchange.cs
File deleted
/trunk/WingMan/MouseKey/KeyInterceptor.cs
File deleted
/trunk/WingMan/MouseKey/KeyBindingMatchedEventArgs.cs
File deleted
/trunk/WingMan/MouseKey/RemoteKeyBinding.cs
File deleted
/trunk/WingMan/MouseKey/RemoteKeyBindings.cs
File deleted
/trunk/WingMan/MouseKey/ExecuteKeyBinding.cs
/trunk/WingMan/MouseKey/MouseKeyBinding.cs
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace WingMan.MouseKey
{
public class MouseKeyBinding : IEquatable<MouseKeyBinding>
{
public MouseKeyBinding()
{
}
 
public MouseKeyBinding(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(MouseKeyBinding 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((MouseKeyBinding) obj);
}
 
public override int GetHashCode()
{
unchecked
{
return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ (Keys != null ? Keys.GetHashCode() : 0);
}
}
}
}
/trunk/WingMan/MouseKey/MouseKeyBindingExchange.cs
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Xml.Serialization;
 
namespace WingMan.MouseKey
{
public class MouseKeyBindingExchange
{
[XmlIgnore] public static readonly XmlSerializer XmlSerializer =
new XmlSerializer(typeof(MouseKeyBindingExchange));
 
public MouseKeyBindingExchange()
{
}
 
public MouseKeyBindingExchange(string nick, List<MouseKeyBinding> mouseKeyBindings)
{
Nick = nick;
MouseKeyBindings = mouseKeyBindings;
}
 
public string Nick { get; set; }
 
public List<MouseKeyBinding> MouseKeyBindings { get; set; }
}
}
/trunk/WingMan/MouseKey/MouseKeyBindings.cs
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Xml.Serialization;
 
namespace WingMan.MouseKey
{
public class MouseKeyBindings
{
[XmlIgnore] public static readonly XmlSerializer XmlSerializer = new XmlSerializer(typeof(MouseKeyBindings));
 
public MouseKeyBindings()
{
}
 
public MouseKeyBindings(List<MouseKeyBinding> bindings) : this()
{
Bindings = bindings;
}
 
public List<MouseKeyBinding> Bindings { get; set; }
}
}
/trunk/WingMan/MouseKey/MouseKeyBindingsExchange.cs
@@ -0,0 +1,9 @@
using System.Collections.Generic;
 
namespace WingMan.MouseKey
{
public class MouseKeyBindingsExchange
{
public List<MouseKeyBindingExchange> ExchangeBindings { get; set; }
}
}
/trunk/WingMan/MouseKey/MouseKeyBindingsSynchronizedEventArgs.cs
@@ -0,0 +1,14 @@
using System;
 
namespace WingMan.MouseKey
{
public class MouseKeyBindingsSynchronizedEventArgs : EventArgs
{
public MouseKeyBindingsSynchronizedEventArgs(MouseKeyBindingExchange mouseKeyExchangeBindings)
{
Bindings = mouseKeyExchangeBindings;
}
 
public MouseKeyBindingExchange Bindings { get; set; }
}
}
/trunk/WingMan/MouseKey/MouseKeyBindingsSynchronizer.cs
@@ -0,0 +1,92 @@
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 MouseKeyBindingsSynchronizer
{
public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e);
 
public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MqttCommunication mqttCommunication,
TaskScheduler taskScheduler, CancellationToken cancellationToken)
{
MouseKeyBindings = mouseKeyBindings;
MqttCommunication = mqttCommunication;
CancellationToken = cancellationToken;
TaskScheduler = taskScheduler;
 
SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBinding>>();
 
mqttCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived;
 
Task.Run(Synchronize, CancellationToken);
}
 
private MouseKeyBindings MouseKeyBindings { get; }
 
private ConcurrentDictionary<string, List<MouseKeyBinding>> SynchronizedMouseKeyBindings { get; }
 
private MqttCommunication MqttCommunication { get; }
 
private CancellationToken CancellationToken { get; }
private TaskScheduler TaskScheduler { get; }
public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized;
 
private async void MqttCommunicationOnOnMessageReceived(object sender,
MqttApplicationMessageReceivedEventArgs e)
{
if (e.ApplicationMessage.Topic != "exchange")
return;
 
using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload))
{
memoryStream.Position = 0L;
 
var mouseKeyBindingsExchange =
(MouseKeyBindingExchange) MouseKeyBindingExchange.XmlSerializer.Deserialize(memoryStream);
 
if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) &&
mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.MouseKeyBindings))
return;
 
// Nick does not exist so the bindings will be added.
SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick,
mouseKeyBindingsExchange.MouseKeyBindings, (s, list) => mouseKeyBindingsExchange.MouseKeyBindings);
 
await Task.Delay(0)
.ContinueWith(
_ => OnMouseKeyBindingsSynchronized?.Invoke(sender,
new MouseKeyBindingsSynchronizedEventArgs(
mouseKeyBindingsExchange)),
CancellationToken, TaskContinuationOptions.None, TaskScheduler);
}
}
 
private async Task Synchronize()
{
do
{
await Task.Delay(1000, CancellationToken).ConfigureAwait(false);
 
if (!MqttCommunication.Running)
continue;
 
using (var memoryStream = new MemoryStream())
{
MouseKeyBindingExchange.XmlSerializer.Serialize(memoryStream,
new MouseKeyBindingExchange(MqttCommunication.Nick, MouseKeyBindings.Bindings));
 
memoryStream.Position = 0L;
 
await MqttCommunication.Broadcast("exchange", memoryStream.ToArray()).ConfigureAwait(false);
}
} while (!CancellationToken.IsCancellationRequested);
}
}
}
/trunk/WingMan/MouseKey/RemoteMouseKeyBinding.cs
@@ -0,0 +1,24 @@
using System.Collections.Generic;
 
namespace WingMan.MouseKey
{
public class RemoteMouseKeyBinding
{
public RemoteMouseKeyBinding()
{
}
 
public RemoteMouseKeyBinding(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/RemoteMouseKeyBindings.cs
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Xml.Serialization;
 
namespace WingMan.MouseKey
{
public class RemoteMouseKeyBindings
{
[XmlIgnore] public static readonly XmlSerializer XmlSerializer =
new XmlSerializer(typeof(RemoteMouseKeyBindings));
 
public RemoteMouseKeyBindings()
{
}
 
public RemoteMouseKeyBindings(List<RemoteMouseKeyBinding> bindings)
{
Bindings = bindings;
}
 
public List<RemoteMouseKeyBinding> Bindings { get; set; }
}
}