WingMan – Diff between revs 32 and 35

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 32 Rev 35
1 using System; 1 using System;
2 using System.IO; 2 using System.IO;
3 using System.Threading; 3 using System.Threading;
4 using System.Threading.Tasks; 4 using System.Threading.Tasks;
5 using WindowsInput; 5 using WindowsInput;
6 using WindowsInput.Native; 6 using WindowsInput.Native;
7 using MQTTnet; -  
8 using WingMan.Communication; 7 using WingMan.Communication;
9 using WingMan.Utilities; 8 using WingMan.Utilities;
10   9  
11 namespace WingMan.Bindings 10 namespace WingMan.Bindings
12 { 11 {
13 public class KeySimulator : IDisposable 12 public class KeySimulator : IDisposable
14 { 13 {
15 public delegate void MouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args); 14 public delegate void MouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args);
16   15  
17 public KeySimulator(LocalKeyBindings localLocalKeyBindings, MqttCommunication mqttCommunication, 16 public KeySimulator(LocalKeyBindings localLocalKeyBindings, MqttCommunication mqttCommunication,
18 TaskScheduler formTaskScheduler, CancellationToken cancellationToken) 17 TaskScheduler formTaskScheduler, CancellationToken cancellationToken)
19 { 18 {
20 LocalLocalKeyBindings = localLocalKeyBindings; 19 LocalLocalKeyBindings = localLocalKeyBindings;
21 MqttCommunication = mqttCommunication; 20 MqttCommunication = mqttCommunication;
22 TaskScheduler = formTaskScheduler; 21 TaskScheduler = formTaskScheduler;
23 CancellationToken = cancellationToken; 22 CancellationToken = cancellationToken;
24   23  
25 MqttCommunication.OnMessageReceived += OnMqttMessageReceived; 24 MqttCommunication.OnMessageReceived += OnMqttMessageReceived;
26   25  
27 InputSimulator = new InputSimulator(); 26 InputSimulator = new InputSimulator();
28 } 27 }
29   28  
30 private InputSimulator InputSimulator { get; } 29 private InputSimulator InputSimulator { get; }
31   30  
32 private MqttCommunication MqttCommunication { get; } 31 private MqttCommunication MqttCommunication { get; }
33 private TaskScheduler TaskScheduler { get; } 32 private TaskScheduler TaskScheduler { get; }
34 private CancellationToken CancellationToken { get; } 33 private CancellationToken CancellationToken { get; }
35 private LocalKeyBindings LocalLocalKeyBindings { get; } 34 private LocalKeyBindings LocalLocalKeyBindings { get; }
36   35  
37 public void Dispose() 36 public void Dispose()
38 { 37 {
39 MqttCommunication.OnMessageReceived -= OnMqttMessageReceived; 38 MqttCommunication.OnMessageReceived -= OnMqttMessageReceived;
40 } 39 }
41   40  
42 public event MouseKeyBindingExecuting OnMouseKeyBindingExecuting; 41 public event MouseKeyBindingExecuting OnMouseKeyBindingExecuting;
43   42  
44 private async void OnMqttMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) 43 private async void OnMqttMessageReceived(object sender, MqttCommunicationMessageReceivedEventArgs e)
45 { 44 {
46 if (e.ApplicationMessage.Topic != "execute") 45 if (e.Topic != "execute")
47 return; 46 return;
48   47  
-   48 using (var memoryStream = new MemoryStream())
-   49 {
-   50 await e.PayloadStream.CopyToAsync(memoryStream);
-   51  
49 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) 52 memoryStream.Position = 0L;
50 { 53  
51 var executeMouseKeyBinding = 54 var executeMouseKeyBinding =
52 (ExecuteKeyBinding) ExecuteKeyBinding.XmlSerializer.Deserialize(memoryStream); 55 (ExecuteKeyBinding) ExecuteKeyBinding.XmlSerializer.Deserialize(memoryStream);
53   56  
54 // Do not process own mouse key bindings. 57 // Do not process own mouse key bindings.
55 if (!string.Equals(executeMouseKeyBinding.Nick, MqttCommunication.Nick, StringComparison.Ordinal)) 58 if (!string.Equals(executeMouseKeyBinding.Nick, MqttCommunication.Nick, StringComparison.Ordinal))
56 return; 59 return;
57   60  
58 await Task.Delay(0, CancellationToken) 61 await Task.Delay(0, CancellationToken)
59 .ContinueWith( 62 .ContinueWith(
60 _ => OnMouseKeyBindingExecuting?.Invoke(sender, 63 _ => OnMouseKeyBindingExecuting?.Invoke(sender,
61 new KeyBindingExecutingEventArgs(executeMouseKeyBinding.Nick, 64 new KeyBindingExecutingEventArgs(executeMouseKeyBinding.Nick,
62 executeMouseKeyBinding.Name)), 65 executeMouseKeyBinding.Name)),
63 CancellationToken, 66 CancellationToken,
64 TaskContinuationOptions.None, TaskScheduler); 67 TaskContinuationOptions.None, TaskScheduler);
65   68  
66 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 69 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
67 Task.Run(() => Simulate(executeMouseKeyBinding), CancellationToken); 70 Task.Run(() => Simulate(executeMouseKeyBinding), CancellationToken);
68 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 71 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
69 } 72 }
70 } 73 }
71   74  
72 private void Simulate(ExecuteKeyBinding executeBinding) 75 private void Simulate(ExecuteKeyBinding executeBinding)
73 { 76 {
74 foreach (var localBinding in LocalLocalKeyBindings.Bindings) 77 foreach (var localBinding in LocalLocalKeyBindings.Bindings)
75 { 78 {
76 if (!string.Equals(localBinding.Name, executeBinding.Name, StringComparison.Ordinal)) 79 if (!string.Equals(localBinding.Name, executeBinding.Name, StringComparison.Ordinal))
77 continue; 80 continue;
78   81  
79 // Skip any key bindings that are not enabled. 82 // Skip any key bindings that are not enabled.
80 if (!localBinding.Enabled) 83 if (!localBinding.Enabled)
81 continue; 84 continue;
82   85  
83 // Key down 86 // Key down
84 foreach (var key in localBinding.Keys) 87 foreach (var key in localBinding.Keys)
85 { 88 {
86 if (!KeyConversion.StringToKeys.TryGetValue(key, out var press)) 89 if (!KeyConversion.StringToKeys.TryGetValue(key, out var press))
87 continue; 90 continue;
88   91  
89 InputSimulator.Keyboard.KeyDown((VirtualKeyCode) press); 92 InputSimulator.Keyboard.KeyDown((VirtualKeyCode) press);
90 } 93 }
91   94  
92 // Key up 95 // Key up
93 foreach (var key in localBinding.Keys) 96 foreach (var key in localBinding.Keys)
94 { 97 {
95 if (!KeyConversion.StringToKeys.TryGetValue(key, out var press)) 98 if (!KeyConversion.StringToKeys.TryGetValue(key, out var press))
96 continue; 99 continue;
97   100  
98 InputSimulator.Keyboard.KeyUp((VirtualKeyCode) press); 101 InputSimulator.Keyboard.KeyUp((VirtualKeyCode) press);
99 } 102 }
100 } 103 }
101 } 104 }
102 } 105 }
103 } -  
104   106 }
-   107  
105
Generated by GNU Enscript 1.6.5.90.
-  
106   -  
107   -  
108   -