WingMan – Diff between revs 25 and 27

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 25 Rev 27
1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Collections.Generic;
3 using System.Collections.ObjectModel; 3 using System.Collections.ObjectModel;
4 using System.ComponentModel; 4 using System.ComponentModel;
5 using System.Drawing; 5 using System.Drawing;
6 using System.IO; 6 using System.IO;
7 using System.Linq; 7 using System.Linq;
8 using System.Net; 8 using System.Net;
9 using System.Threading; 9 using System.Threading;
10 using System.Threading.Tasks; 10 using System.Threading.Tasks;
11 using System.Windows.Forms; 11 using System.Windows.Forms;
12 using Gma.System.MouseKeyHook; 12 using Gma.System.MouseKeyHook;
13 using MQTTnet.Extensions.ManagedClient; 13 using MQTTnet.Extensions.ManagedClient;
14 using MQTTnet.Server; 14 using MQTTnet.Server;
15 using WingMan.AutoCompletion; 15 using WingMan.AutoCompletion;
16 using WingMan.Bindings; 16 using WingMan.Bindings;
17 using WingMan.Communication; 17 using WingMan.Communication;
18 using WingMan.Lobby; 18 using WingMan.Lobby;
19 using WingMan.Properties; 19 using WingMan.Properties;
20 using WingMan.Utilities; 20 using WingMan.Utilities;
21   21  
22 namespace WingMan 22 namespace WingMan
23 { 23 {
24 public partial class WingManForm : Form 24 public partial class WingManForm : Form
25 { 25 {
26 public WingManForm() 26 public WingManForm()
27 { 27 {
28 InitializeComponent(); 28 InitializeComponent();
29   29  
30 FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); 30 FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
31 FormCancellationTokenSource = new CancellationTokenSource(); 31 FormCancellationTokenSource = new CancellationTokenSource();
32   32  
33 // Set up autocompletion. 33 // Set up autocompletion.
34 AutoCompletion = new AutoCompletion.AutoCompletion(FormTaskScheduler, FormCancellationTokenSource.Token); 34 AutoCompletion = new AutoCompletion.AutoCompletion(FormTaskScheduler, FormCancellationTokenSource.Token);
35 AutoCompletion.OnSaveFailed += AutoCompletionOnSaveFailed; 35 AutoCompletion.OnSaveFailed += AutoCompletionOnSaveFailed;
36 AutoCompletion.OnLoadFailed += AutoCompletionOnLoadFailed; 36 AutoCompletion.OnLoadFailed += AutoCompletionOnLoadFailed;
37   37  
38 Task.Run(() => AutoCompletion.Load(Address.Name, Address.AutoCompleteCustomSource)); 38 Task.Run(() => AutoCompletion.Load(Address.Name, Address.AutoCompleteCustomSource));
39 Task.Run(() => AutoCompletion.Load(Port.Name, Address.AutoCompleteCustomSource)); 39 Task.Run(() => AutoCompletion.Load(Port.Name, Address.AutoCompleteCustomSource));
40 Task.Run(() => AutoCompletion.Load(Nick.Name, Nick.AutoCompleteCustomSource)); 40 Task.Run(() => AutoCompletion.Load(Nick.Name, Nick.AutoCompleteCustomSource));
41   41  
42 MqttCommunication = new MqttCommunication(FormTaskScheduler, FormCancellationTokenSource.Token); 42 MqttCommunication = new MqttCommunication(FormTaskScheduler, FormCancellationTokenSource.Token);
43 MqttCommunication.OnClientAuthenticationFailed += OnMqttClientAuthenticationFailed; 43 MqttCommunication.OnClientAuthenticationFailed += OnMqttClientAuthenticationFailed;
44 MqttCommunication.OnClientConnectionFailed += OnMqttClientConnectionFailed; 44 MqttCommunication.OnClientConnectionFailed += OnMqttClientConnectionFailed;
45 MqttCommunication.OnClientDisconnected += OnMqttClientDisconnected; 45 MqttCommunication.OnClientDisconnected += OnMqttClientDisconnected;
46 MqttCommunication.OnClientConnected += OnMqttClientConnected; 46 MqttCommunication.OnClientConnected += OnMqttClientConnected;
47 MqttCommunication.OnServerAuthenticationFailed += OnMqttServerAuthenticationFailed; 47 MqttCommunication.OnServerAuthenticationFailed += OnMqttServerAuthenticationFailed;
48 MqttCommunication.OnServerStopped += OnMqttServerStopped; 48 MqttCommunication.OnServerStopped += OnMqttServerStopped;
49 MqttCommunication.OnServerStarted += OnMqttServerStarted; 49 MqttCommunication.OnServerStarted += OnMqttServerStarted;
50 MqttCommunication.OnServerClientConnected += OnMqttServerClientConnected; 50 MqttCommunication.OnServerClientConnected += OnMqttServerClientConnected;
51 MqttCommunication.OnServerClientDisconnected += OnMqttServerClientDisconnected; 51 MqttCommunication.OnServerClientDisconnected += OnMqttServerClientDisconnected;
52   52  
53 LocalKeyBindings = new LocalKeyBindings(new List<KeyBinding>()); 53 LocalKeyBindings = new LocalKeyBindings(new List<KeyBinding>());
54 RemoteKeyBindings = new RemoteKeyBindings(new ObservableCollection<RemoteKeyBinding>()); 54 RemoteKeyBindings = new RemoteKeyBindings(new ObservableCollection<RemoteKeyBinding>());
55   55  
56 LocalCheckedListBoxBindingSource = new BindingSource 56 LocalCheckedListBoxBindingSource = new BindingSource
57 { 57 {
58 DataSource = LocalKeyBindings.Bindings 58 DataSource = LocalKeyBindings.Bindings
59 }; 59 };
60 LocalCheckedListBoxBindingSource.ListChanged += LocalCheckedListBoxBindingSourceOnListChanged; 60 LocalCheckedListBoxBindingSource.ListChanged += LocalCheckedListBoxBindingSourceOnListChanged;
61 LocalBindingsCheckedListBox.DisplayMember = "DisplayName"; 61 LocalBindingsCheckedListBox.DisplayMember = "DisplayName";
62 LocalBindingsCheckedListBox.ValueMember = "Keys"; 62 LocalBindingsCheckedListBox.ValueMember = "Keys";
63 LocalBindingsCheckedListBox.DataSource = LocalCheckedListBoxBindingSource; 63 LocalBindingsCheckedListBox.DataSource = LocalCheckedListBoxBindingSource;
64   64  
65 KeyBindingsExchange = new KeyBindingsExchange 65 KeyBindingsExchange = new KeyBindingsExchange
66 { 66 {
67 ExchangeBindings = new List<KeyBindingExchange>() 67 ExchangeBindings = new List<KeyBindingExchange>()
68 }; 68 };
69   69  
70 RemoteBindingsComboBoxSource = new BindingSource 70 RemoteBindingsComboBoxSource = new BindingSource
71 { 71 {
72 DataSource = KeyBindingsExchange.ExchangeBindings 72 DataSource = KeyBindingsExchange.ExchangeBindings
73 }; 73 };
74 RemoteBindingsComboBox.DisplayMember = "Nick"; 74 RemoteBindingsComboBox.DisplayMember = "Nick";
75 RemoteBindingsComboBox.ValueMember = "KeyBindings"; 75 RemoteBindingsComboBox.ValueMember = "KeyBindings";
76 RemoteBindingsComboBox.DataSource = RemoteBindingsComboBoxSource; 76 RemoteBindingsComboBox.DataSource = RemoteBindingsComboBoxSource;
77   77  
78 // Start lobby message synchronizer. 78 // Start lobby message synchronizer.
79 LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MqttCommunication, FormTaskScheduler, 79 LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MqttCommunication, FormTaskScheduler,
80 FormCancellationTokenSource.Token); 80 FormCancellationTokenSource.Token);
81 LobbyMessageSynchronizer.OnLobbyMessageReceived += OnLobbyMessageReceived; 81 LobbyMessageSynchronizer.OnLobbyMessageReceived += OnLobbyMessageReceived;
82   82  
83 // Start mouse key bindings synchronizer. 83 // Start mouse key bindings synchronizer.
84 KeyBindingsSynchronizer = new KeyBindingsSynchronizer(LocalKeyBindings, MqttCommunication, 84 KeyBindingsSynchronizer = new KeyBindingsSynchronizer(LocalKeyBindings, MqttCommunication,
85 FormTaskScheduler, FormCancellationTokenSource.Token); 85 FormTaskScheduler, FormCancellationTokenSource.Token);
86 KeyBindingsSynchronizer.OnMouseKeyBindingsSynchronized += OnMouseKeyBindingsSynchronized; 86 KeyBindingsSynchronizer.OnMouseKeyBindingsSynchronized += OnMouseKeyBindingsSynchronized;
87   87  
88 // Start mouse key interceptor. 88 // Start mouse key interceptor.
89 KeyInterceptor = new KeyInterceptor(RemoteKeyBindings, MqttCommunication, FormTaskScheduler, 89 KeyInterceptor = new KeyInterceptor(RemoteKeyBindings, MqttCommunication, FormTaskScheduler,
90 FormCancellationTokenSource.Token); 90 FormCancellationTokenSource.Token);
91 KeyInterceptor.OnMouseKeyBindingMatched += OnMouseKeyBindingMatched; 91 KeyInterceptor.OnMouseKeyBindingMatched += OnMouseKeyBindingMatched;
92   92  
93 // Start mouse key simulator. 93 // Start mouse key simulator.
94 KeySimulator = new KeySimulator(LocalKeyBindings, MqttCommunication, FormTaskScheduler, 94 KeySimulator = new KeySimulator(LocalKeyBindings, MqttCommunication, FormTaskScheduler,
95 FormCancellationTokenSource.Token); 95 FormCancellationTokenSource.Token);
96 KeySimulator.OnMouseKeyBindingExecuting += OnMouseKeyBindingExecuting; 96 KeySimulator.OnMouseKeyBindingExecuting += OnMouseKeyBindingExecuting;
97 } 97 }
98   98  
99 private static AutoCompletion.AutoCompletion AutoCompletion { get; set; } 99 private static AutoCompletion.AutoCompletion AutoCompletion { get; set; }
100 private static CancellationTokenSource FormCancellationTokenSource { get; set; } 100 private static CancellationTokenSource FormCancellationTokenSource { get; set; }
101   101  
102 private static TaskScheduler FormTaskScheduler { get; set; } 102 private static TaskScheduler FormTaskScheduler { get; set; }
103   103  
104 private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; } 104 private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; }
105   105  
106 private List<string> MouseKeyCombo { get; set; } 106 private List<string> MouseKeyCombo { get; set; }
107   107  
108 private LocalKeyBindings LocalKeyBindings { get; } 108 private LocalKeyBindings LocalKeyBindings { get; }
109   109  
110 private RemoteKeyBindings RemoteKeyBindings { get; } 110 private RemoteKeyBindings RemoteKeyBindings { get; }
111   111  
112 private BindingSource LocalCheckedListBoxBindingSource { get; } 112 private BindingSource LocalCheckedListBoxBindingSource { get; }
113   113  
114 private BindingSource RemoteBindingsComboBoxSource { get; } 114 private BindingSource RemoteBindingsComboBoxSource { get; }
115   115  
116 private KeyBindingsExchange KeyBindingsExchange { get; } 116 private KeyBindingsExchange KeyBindingsExchange { get; }
117   117  
118 public MqttCommunication MqttCommunication { get; set; } 118 public MqttCommunication MqttCommunication { get; set; }
119   119  
120 public LobbyMessageSynchronizer LobbyMessageSynchronizer { get; set; } 120 public LobbyMessageSynchronizer LobbyMessageSynchronizer { get; set; }
121   121  
122 public KeyBindingsSynchronizer KeyBindingsSynchronizer { get; set; } 122 public KeyBindingsSynchronizer KeyBindingsSynchronizer { get; set; }
123   123  
124 public KeyInterceptor KeyInterceptor { get; set; } 124 public KeyInterceptor KeyInterceptor { get; set; }
125   125  
126 public KeySimulator KeySimulator { get; set; } 126 public KeySimulator KeySimulator { get; set; }
127   127  
128 private void LocalCheckedListBoxBindingSourceOnListChanged(object sender, ListChangedEventArgs e) 128 private void LocalCheckedListBoxBindingSourceOnListChanged(object sender, ListChangedEventArgs e)
129 { 129 {
130 // Check items 130 // Check items
131 LocalBindingsCheckedListBox.ItemCheck -= LocalBindingsCheckedListBoxItemCheck; 131 LocalBindingsCheckedListBox.ItemCheck -= LocalBindingsCheckedListBoxItemCheck;
132   132  
133 for (var i = 0; i < LocalKeyBindings.Bindings.Count; ++i) 133 for (var i = 0; i < LocalKeyBindings.Bindings.Count; ++i)
134 switch (LocalKeyBindings.Bindings[i].Enabled) 134 switch (LocalKeyBindings.Bindings[i].Enabled)
135 { 135 {
136 case true: 136 case true:
137 LocalBindingsCheckedListBox.SetItemCheckState(i, CheckState.Checked); 137 LocalBindingsCheckedListBox.SetItemCheckState(i, CheckState.Checked);
138 break; 138 break;
139 default: 139 default:
140 LocalBindingsCheckedListBox.SetItemCheckState(i, CheckState.Unchecked); 140 LocalBindingsCheckedListBox.SetItemCheckState(i, CheckState.Unchecked);
141 break; 141 break;
142 } 142 }
143   143  
144 LocalBindingsCheckedListBox.ItemCheck += LocalBindingsCheckedListBoxItemCheck; 144 LocalBindingsCheckedListBox.ItemCheck += LocalBindingsCheckedListBoxItemCheck;
145 } 145 }
146   146  
147 private void AutoCompletionOnLoadFailed(object sender, AutoCompletionFailedEventArgs args) 147 private void AutoCompletionOnLoadFailed(object sender, AutoCompletionFailedEventArgs args)
148 { 148 {
149 ActivityTextBox.AppendText( 149 ActivityTextBox.AppendText(
150 $"{Strings.Failed_loading_autocomplete_source} : {args.Name} : {args.Exception.Message}{Environment.NewLine}"); 150 $"{Strings.Failed_loading_autocomplete_source} : {args.Name} : {args.Exception.Message}{Environment.NewLine}");
151 } 151 }
152   152  
153 private void AutoCompletionOnSaveFailed(object sender, AutoCompletionFailedEventArgs args) 153 private void AutoCompletionOnSaveFailed(object sender, AutoCompletionFailedEventArgs args)
154 { 154 {
155 ActivityTextBox.AppendText( 155 ActivityTextBox.AppendText(
156 $"{Strings.Failed_saving_autocomplete_source} : {args.Name} : {args.Exception.Message}{Environment.NewLine}"); 156 $"{Strings.Failed_saving_autocomplete_source} : {args.Name} : {args.Exception.Message}{Environment.NewLine}");
157 } 157 }
158   158  
159 /// <inheritdoc /> 159 /// <inheritdoc />
160 /// <summary> 160 /// <summary>
161 /// Clean up any resources being used. 161 /// Clean up any resources being used.
162 /// </summary> 162 /// </summary>
163 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 163 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
164 protected override void Dispose(bool disposing) 164 protected override void Dispose(bool disposing)
165 { 165 {
166 FormCancellationTokenSource?.Dispose(); 166 FormCancellationTokenSource?.Dispose();
167 FormCancellationTokenSource = null; 167 FormCancellationTokenSource = null;
168   168  
169 if (disposing && components != null) 169 if (disposing && components != null)
170 { 170 {
171 components.Dispose(); 171 components.Dispose();
172 components = null; 172 components = null;
173 } 173 }
174   174  
175 base.Dispose(disposing); 175 base.Dispose(disposing);
176 } 176 }
177   177  
178 private void OnMouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args) 178 private void OnMouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args)
179 { 179 {
180 ActivityTextBox.AppendText( 180 ActivityTextBox.AppendText(
181 $"{Strings.Executing_binding_from_remote_client} : {args.Nick} : {args.Name}{Environment.NewLine}"); 181 $"{Strings.Executing_binding_from_remote_client} : {args.Nick} : {args.Name}{Environment.NewLine}");
182 } 182 }
183   183  
184 private void OnMouseKeyBindingMatched(object sender, KeyBindingMatchedEventArgs args) 184 private void OnMouseKeyBindingMatched(object sender, KeyBindingMatchedEventArgs args)
185 { 185 {
186 ActivityTextBox.AppendText( 186 ActivityTextBox.AppendText(
187 $"{Strings.Matched_remote_key_binding} : {args.Nick} : {args.Name} : {string.Join(" + ", args.KeyCombo)}{Environment.NewLine}"); 187 $"{Strings.Matched_remote_key_binding} : {args.Nick} : {args.Name} : {string.Join(" + ", args.KeyCombo)}{Environment.NewLine}");
188 } 188 }
189   189  
190 private void OnMqttServerClientDisconnected(object sender, MqttClientDisconnectedEventArgs e) 190 private void OnMqttServerClientDisconnected(object sender, MqttClientDisconnectedEventArgs e)
191 { 191 {
192 ActivityTextBox.AppendText( 192 ActivityTextBox.AppendText(
193 $"{Strings.Client_disconnected}{Environment.NewLine}"); 193 $"{Strings.Client_disconnected}{Environment.NewLine}");
194 } 194 }
195   195  
196 private void OnMqttServerClientConnected(object sender, MqttClientConnectedEventArgs e) 196 private void OnMqttServerClientConnected(object sender, MqttClientConnectedEventArgs e)
197 { 197 {
198 ActivityTextBox.AppendText( 198 ActivityTextBox.AppendText(
199 $"{Strings.Client_connected}{Environment.NewLine}"); 199 $"{Strings.Client_connected}{Environment.NewLine}");
200 } 200 }
201   201  
202 private void OnMqttServerStarted(object sender, EventArgs e) 202 private void OnMqttServerStarted(object sender, EventArgs e)
203 { 203 {
204 ActivityTextBox.AppendText( 204 ActivityTextBox.AppendText(
205 $"{Strings.Server_started}{Environment.NewLine}"); 205 $"{Strings.Server_started}{Environment.NewLine}");
206 } 206 }
207   207  
208 private void OnMqttServerStopped(object sender, EventArgs e) 208 private void OnMqttServerStopped(object sender, EventArgs e)
209 { 209 {
210 ActivityTextBox.AppendText( 210 ActivityTextBox.AppendText(
211 $"{Strings.Server_stopped}{Environment.NewLine}"); 211 $"{Strings.Server_stopped}{Environment.NewLine}");
212 } 212 }
213   213  
214 private void OnMqttClientConnected(object sender, MQTTnet.Client.MqttClientConnectedEventArgs e) 214 private void OnMqttClientConnected(object sender, MQTTnet.Client.MqttClientConnectedEventArgs e)
215 { 215 {
216 ActivityTextBox.AppendText( 216 ActivityTextBox.AppendText(
217 $"{Strings.Client_connected}{Environment.NewLine}"); 217 $"{Strings.Client_connected}{Environment.NewLine}");
218 } 218 }
219   219  
220 private void OnMqttClientDisconnected(object sender, MQTTnet.Client.MqttClientDisconnectedEventArgs e) 220 private void OnMqttClientDisconnected(object sender, MQTTnet.Client.MqttClientDisconnectedEventArgs e)
221 { 221 {
222 ActivityTextBox.AppendText( 222 ActivityTextBox.AppendText(
223 $"{Strings.Client_disconnected}{Environment.NewLine}"); 223 $"{Strings.Client_disconnected}{Environment.NewLine}");
224 } 224 }
225   225  
226 private void OnMqttClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e) 226 private void OnMqttClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e)
227 { 227 {
228 ActivityTextBox.AppendText( 228 ActivityTextBox.AppendText(
229 $"{Strings.Client_connection_failed}{Environment.NewLine}"); 229 $"{Strings.Client_connection_failed}{Environment.NewLine}");
230 } 230 }
231   231  
232 private void OnMqttServerAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e) 232 private void OnMqttServerAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e)
233 { 233 {
234 ActivityTextBox.AppendText( 234 ActivityTextBox.AppendText(
235 $"{Strings.Failed_to_authenticate_client} : {e.Exception}{Environment.NewLine}"); 235 $"{Strings.Failed_to_authenticate_client} : {e.Exception}{Environment.NewLine}");
236 } 236 }
237   237  
238 private void OnMqttClientAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e) 238 private void OnMqttClientAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e)
239 { 239 {
240 ActivityTextBox.AppendText( 240 ActivityTextBox.AppendText(
241 $"{Strings.Failed_to_authenticate_with_server} : {e.Exception}{Environment.NewLine}"); 241 $"{Strings.Failed_to_authenticate_with_server} : {e.Exception}{Environment.NewLine}");
242 } 242 }
243   243  
244 private void OnMouseKeyBindingsSynchronized(object sender, KeyBindingsSynchronizerEventArgs e) 244 private void OnMouseKeyBindingsSynchronized(object sender, KeyBindingsSynchronizerEventArgs e)
245 { 245 {
246 ActivityTextBox.AppendText( 246 ActivityTextBox.AppendText(
247 $"{Strings.Synchronized_bindings_with_client} : {e.Bindings.Nick} : {e.Bindings.KeyBindings.Count}{Environment.NewLine}"); 247 $"{Strings.Synchronized_bindings_with_client} : {e.Bindings.Nick} : {e.Bindings.KeyBindings.Count}{Environment.NewLine}");
248   248  
249 var exchangeBindings = KeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding => 249 var exchangeBindings = KeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding =>
250 string.Equals(exchangeBinding.Nick, e.Bindings.Nick, StringComparison.Ordinal)); 250 string.Equals(exchangeBinding.Nick, e.Bindings.Nick, StringComparison.Ordinal));
251   251  
252 // If the nick does not exist then add it. 252 // If the nick does not exist then add it.
253 if (exchangeBindings == null) 253 if (exchangeBindings == null)
254 { 254 {
255 KeyBindingsExchange.ExchangeBindings.Add(e.Bindings); 255 KeyBindingsExchange.ExchangeBindings.Add(e.Bindings);
256 RemoteBindingsComboBoxSource.ResetBindings(false); 256 RemoteBindingsComboBoxSource.ResetBindings(false);
257 UpdateRemoteItems(); 257 UpdateRemoteItems();
258 return; 258 return;
259 } 259 }
260   260  
261 // If the bindings for the nick have not changed then do not update. 261 // If the bindings for the nick have not changed then do not update.
262 if (exchangeBindings.KeyBindings.SequenceEqual(e.Bindings.KeyBindings)) 262 if (exchangeBindings.KeyBindings.SequenceEqual(e.Bindings.KeyBindings))
263 { 263 {
264 RemoteBindingsComboBoxSource.ResetBindings(false); 264 RemoteBindingsComboBoxSource.ResetBindings(false);
265 UpdateRemoteItems(); 265 UpdateRemoteItems();
266 return; 266 return;
267 } 267 }
268   268  
269 // Update the bindings. 269 // Update the bindings.
270 exchangeBindings.KeyBindings = e.Bindings.KeyBindings; 270 exchangeBindings.KeyBindings = e.Bindings.KeyBindings;
271 RemoteBindingsComboBoxSource.ResetBindings(false); 271 RemoteBindingsComboBoxSource.ResetBindings(false);
272 UpdateRemoteItems(); 272 UpdateRemoteItems();
273 } 273 }
274   274  
275 private void UpdateRemoteItems() 275 private void UpdateRemoteItems()
276 { 276 {
277 var exchangeBindings = (List<KeyBinding>) RemoteBindingsComboBox.SelectedValue; 277 var exchangeBindings = (List<KeyBinding>) RemoteBindingsComboBox.SelectedValue;
278 if (exchangeBindings == null) 278 if (exchangeBindings == null)
279 return; 279 return;
280   280  
281 var replaceMouseBindings = new ObservableCollection<RemoteKeyBinding>(); 281 var replaceMouseBindings = new ObservableCollection<RemoteKeyBinding>();
282 foreach (var remoteBinding in RemoteKeyBindings.Bindings) 282 foreach (var remoteBinding in RemoteKeyBindings.Bindings)
283 { 283 {
284 if (!exchangeBindings.Any(binding => 284 if (!exchangeBindings.Any(binding =>
285 string.Equals(binding.Name, remoteBinding.Name, StringComparison.Ordinal))) 285 string.Equals(binding.Name, remoteBinding.Name, StringComparison.Ordinal)))
286 continue; 286 continue;
287   287  
288 replaceMouseBindings.Add(remoteBinding); 288 replaceMouseBindings.Add(remoteBinding);
289 } 289 }
290   290  
291 RemoteKeyBindings.Bindings.Clear(); 291 RemoteKeyBindings.Bindings.Clear();
292 foreach (var binding in replaceMouseBindings) RemoteKeyBindings.Bindings.Add(binding); 292 foreach (var binding in replaceMouseBindings) RemoteKeyBindings.Bindings.Add(binding);
293   293  
294 RemoteBindingsListBox.Items.Clear(); 294 RemoteBindingsListBox.Items.Clear();
295 RemoteBindingsListBox.DisplayMember = "Name"; 295 RemoteBindingsListBox.DisplayMember = "Name";
296 RemoteBindingsListBox.ValueMember = "Name"; 296 RemoteBindingsListBox.ValueMember = "Name";
297 var bindings = exchangeBindings.Select(binding => (object) binding.Name).ToArray(); 297 var bindings = exchangeBindings.Select(binding => (object) binding.Name).ToArray();
298 if (bindings.Length == 0) 298 if (bindings.Length == 0)
299 return; 299 return;
300   300  
301 RemoteBindingsListBox.Items.AddRange(bindings); 301 RemoteBindingsListBox.Items.AddRange(bindings);
302 } 302 }
303   303  
304 private void OnLobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e) 304 private void OnLobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e)
305 { 305 {
306 notifyIcon1.BalloonTipTitle = Properties.Strings.Lobby_message; 306 notifyIcon1.BalloonTipTitle = Strings.Lobby_message;
307 notifyIcon1.BalloonTipText = $"{e.Nick} : {e.Message}{Environment.NewLine}"; 307 notifyIcon1.BalloonTipText = $"{e.Nick} : {e.Message}{Environment.NewLine}";
308 notifyIcon1.ShowBalloonTip(1000); 308 notifyIcon1.ShowBalloonTip(1000);
309   309  
310 LobbyTextBox.AppendText($"{e.Nick} : {e.Message}{Environment.NewLine}"); 310 LobbyTextBox.AppendText($"{e.Nick} : {e.Message}{Environment.NewLine}");
311 } 311 }
312   312  
313 private void AddressTextBoxClick(object sender, EventArgs e) 313 private void AddressTextBoxClick(object sender, EventArgs e)
314 { 314 {
315 Address.BackColor = Color.Empty; 315 Address.BackColor = Color.Empty;
316 } 316 }
317   317  
318 private void PortTextBoxClick(object sender, EventArgs e) 318 private void PortTextBoxClick(object sender, EventArgs e)
319 { 319 {
320 Port.BackColor = Color.Empty; 320 Port.BackColor = Color.Empty;
321 } 321 }
322   322  
323 private async void HostButtonClickAsync(object sender, EventArgs e) 323 private async void HostButtonClickAsync(object sender, EventArgs e)
324 { 324 {
325 // Stop the MQTT server if it is running. 325 // Stop the MQTT server if it is running.
326 if (MqttCommunication.Running) 326 if (MqttCommunication.Running)
327 { 327 {
328 await MqttCommunication.Stop(); 328 await MqttCommunication.Stop();
329 HostButton.BackColor = Color.Empty; 329 HostButton.BackColor = Color.Empty;
330   330  
331 // Enable controls. 331 // Enable controls.
332 ConnectButton.Enabled = true; 332 ConnectButton.Enabled = true;
333 Address.Enabled = true; 333 Address.Enabled = true;
334 Port.Enabled = true; 334 Port.Enabled = true;
335 Nick.Enabled = true; 335 Nick.Enabled = true;
336 Password.Enabled = true; 336 Password.Enabled = true;
337 return; 337 return;
338 } 338 }
339   339  
340 if (!ValidateConnectionParameters(out var ipAddress, out var port, out var nick, out var password)) 340 if (!ValidateConnectionParameters(out var ipAddress, out var port, out var nick, out var password))
341 return; 341 return;
342   342  
343 StoreConnectionAutocomplete(); 343 StoreConnectionAutocomplete();
344   344  
345 // Start the MQTT server. 345 // Start the MQTT server.
346 if (!await MqttCommunication 346 if (!await MqttCommunication
347 .Start(MqttCommunicationType.Server, ipAddress, port, nick, password)) 347 .Start(MqttCommunicationType.Server, ipAddress, port, nick, password))
348 { 348 {
349 ActivityTextBox.AppendText( 349 ActivityTextBox.AppendText(
350 $"{Strings.Failed_starting_server}{Environment.NewLine}"); 350 $"{Strings.Failed_starting_server}{Environment.NewLine}");
351 return; 351 return;
352 } 352 }
353   353  
354 HostButton.BackColor = Color.Aquamarine; 354 HostButton.BackColor = Color.Aquamarine;
355   355  
356 // Disable controls 356 // Disable controls
357 ConnectButton.Enabled = false; 357 ConnectButton.Enabled = false;
358 Address.Enabled = false; 358 Address.Enabled = false;
359 Port.Enabled = false; 359 Port.Enabled = false;
360 Nick.Enabled = false; 360 Nick.Enabled = false;
361 Password.Enabled = false; 361 Password.Enabled = false;
362 } 362 }
363   363  
364 private async void StoreConnectionAutocomplete() 364 private async void StoreConnectionAutocomplete()
365 { 365 {
366 Address.AutoCompleteCustomSource.Add(Address.Text); 366 Address.AutoCompleteCustomSource.Add(Address.Text);
367   367  
368 await AutoCompletion.Save(Address.Name, Address.AutoCompleteCustomSource); 368 await AutoCompletion.Save(Address.Name, Address.AutoCompleteCustomSource);
369   369  
370 Port.AutoCompleteCustomSource.Add(Port.Text); 370 Port.AutoCompleteCustomSource.Add(Port.Text);
371   371  
372 await AutoCompletion.Save(Port.Name, Port.AutoCompleteCustomSource); 372 await AutoCompletion.Save(Port.Name, Port.AutoCompleteCustomSource);
373   373  
374 Nick.AutoCompleteCustomSource.Add(Nick.Text); 374 Nick.AutoCompleteCustomSource.Add(Nick.Text);
375   375  
376 await AutoCompletion.Save(Nick.Name, Nick.AutoCompleteCustomSource); 376 await AutoCompletion.Save(Nick.Name, Nick.AutoCompleteCustomSource);
377 } 377 }
378   378  
379 private bool ValidateConnectionParameters( 379 private bool ValidateConnectionParameters(
380 out IPAddress address, 380 out IPAddress address,
381 out int port, 381 out int port,
382 out string nick, 382 out string nick,
383 out string password) 383 out string password)
384 { 384 {
385 address = IPAddress.Any; 385 address = IPAddress.Any;
386 port = 0; 386 port = 0;
387 nick = string.Empty; 387 nick = string.Empty;
388 password = string.Empty; 388 password = string.Empty;
389   389  
390 if (string.IsNullOrEmpty(Address.Text) && 390 if (string.IsNullOrEmpty(Address.Text) &&
391 string.IsNullOrEmpty(Port.Text) && 391 string.IsNullOrEmpty(Port.Text) &&
392 string.IsNullOrEmpty(Nick.Text) && 392 string.IsNullOrEmpty(Nick.Text) &&
393 string.IsNullOrEmpty(Password.Text)) 393 string.IsNullOrEmpty(Password.Text))
394 { 394 {
395 Address.BackColor = Color.LightPink; 395 Address.BackColor = Color.LightPink;
396 Port.BackColor = Color.LightPink; 396 Port.BackColor = Color.LightPink;
397 Nick.BackColor = Color.LightPink; 397 Nick.BackColor = Color.LightPink;
398 Password.BackColor = Color.LightPink; 398 Password.BackColor = Color.LightPink;
399 return false; 399 return false;
400 } 400 }
401   401  
402 if (!IPAddress.TryParse(Address.Text, out address)) 402 if (!IPAddress.TryParse(Address.Text, out address))
403 try 403 try
404 { 404 {
405 address = Dns.GetHostAddresses(Address.Text).FirstOrDefault(); 405 address = Dns.GetHostAddresses(Address.Text).FirstOrDefault();
406 } 406 }
407 catch (Exception ex) 407 catch (Exception ex)
408 { 408 {
409 ActivityTextBox.AppendText( 409 ActivityTextBox.AppendText(
410 $"{Strings.Could_not_resolve_hostname} : {ex.Message}{Environment.NewLine}"); 410 $"{Strings.Could_not_resolve_hostname} : {ex.Message}{Environment.NewLine}");
411   411  
412 Address.BackColor = Color.LightPink; 412 Address.BackColor = Color.LightPink;
413 return false; 413 return false;
414 } 414 }
415   415  
416 if (!uint.TryParse(Port.Text, out var uPort)) 416 if (!uint.TryParse(Port.Text, out var uPort))
417 { 417 {
418 Port.BackColor = Color.LightPink; 418 Port.BackColor = Color.LightPink;
419 return false; 419 return false;
420 } 420 }
421   421  
422 port = (int) uPort; 422 port = (int) uPort;
423   423  
424 if (string.IsNullOrEmpty(Nick.Text)) 424 if (string.IsNullOrEmpty(Nick.Text))
425 { 425 {
426 Nick.BackColor = Color.LightPink; 426 Nick.BackColor = Color.LightPink;
427 return false; 427 return false;
428 } 428 }
429   429  
430 nick = Nick.Text; 430 nick = Nick.Text;
431   431  
432 if (string.IsNullOrEmpty(Password.Text)) 432 if (string.IsNullOrEmpty(Password.Text))
433 { 433 {
434 Password.BackColor = Color.LightPink; 434 Password.BackColor = Color.LightPink;
435 return false; 435 return false;
436 } 436 }
437   437  
438 password = AES.ExpandKey(Password.Text); 438 password = AES.ExpandKey(Password.Text);
439   439  
440 Address.BackColor = Color.Empty; 440 Address.BackColor = Color.Empty;
441 Port.BackColor = Color.Empty; 441 Port.BackColor = Color.Empty;
442 Nick.BackColor = Color.Empty; 442 Nick.BackColor = Color.Empty;
443 Password.BackColor = Color.Empty; 443 Password.BackColor = Color.Empty;
444   444  
445 return true; 445 return true;
446 } 446 }
447   447  
448 private async void ConnectButtonClickAsync(object sender, EventArgs e) 448 private async void ConnectButtonClickAsync(object sender, EventArgs e)
449 { 449 {
450 if (MqttCommunication.Running) 450 if (MqttCommunication.Running)
451 { 451 {
452 await MqttCommunication.Stop(); 452 await MqttCommunication.Stop();
453 ConnectButton.Text = Strings.Connect; 453 ConnectButton.Text = Strings.Connect;
454 ConnectButton.BackColor = Color.Empty; 454 ConnectButton.BackColor = Color.Empty;
455   455  
456 Address.Enabled = true; 456 Address.Enabled = true;
457 Port.Enabled = true; 457 Port.Enabled = true;
458 Nick.Enabled = true; 458 Nick.Enabled = true;
459 Password.Enabled = true; 459 Password.Enabled = true;
460 HostButton.Enabled = true; 460 HostButton.Enabled = true;
461 return; 461 return;
462 } 462 }
463   463  
464 if (!ValidateConnectionParameters(out var ipAddress, out var port, out var nick, out var password)) 464 if (!ValidateConnectionParameters(out var ipAddress, out var port, out var nick, out var password))
465 return; 465 return;
466   466  
467 StoreConnectionAutocomplete(); 467 StoreConnectionAutocomplete();
468   468  
469 if (!await MqttCommunication 469 if (!await MqttCommunication
470 .Start(MqttCommunicationType.Client, ipAddress, port, nick, password)) 470 .Start(MqttCommunicationType.Client, ipAddress, port, nick, password))
471 { 471 {
472 ActivityTextBox.AppendText( 472 ActivityTextBox.AppendText(
473 $"{Strings.Failed_starting_client}{Environment.NewLine}"); 473 $"{Strings.Failed_starting_client}{Environment.NewLine}");
474 return; 474 return;
475 } 475 }
476   476  
477 ConnectButton.Text = Strings.Disconnect; 477 ConnectButton.Text = Strings.Disconnect;
478 ConnectButton.BackColor = Color.Aquamarine; 478 ConnectButton.BackColor = Color.Aquamarine;
479   479  
480 HostButton.Enabled = false; 480 HostButton.Enabled = false;
481 Address.Enabled = false; 481 Address.Enabled = false;
482 Port.Enabled = false; 482 Port.Enabled = false;
483 Nick.Enabled = false; 483 Nick.Enabled = false;
484 Password.Enabled = false; 484 Password.Enabled = false;
485 } 485 }
486   486  
487 private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e) 487 private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e)
488 { 488 {
489 // Do not send messages if the communication is not running. 489 // Do not send messages if the communication is not running.
490 if (!MqttCommunication.Running) 490 if (!MqttCommunication.Running)
491 return; 491 return;
492   492  
493 if (e.KeyCode != Keys.Enter) 493 if (e.KeyCode != Keys.Enter)
494 return; 494 return;
495   495  
496 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); 496 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text);
497   497  
498 LobbySayTextBox.Text = string.Empty; 498 LobbySayTextBox.Text = string.Empty;
499 } 499 }
500   500  
501 private void LocalAddBindingButtonClick(object sender, EventArgs e) 501 private void LocalAddBindingButtonClick(object sender, EventArgs e)
502 { 502 {
503 if (string.IsNullOrEmpty(LocalNameTextBox.Text)) 503 if (string.IsNullOrEmpty(LocalNameTextBox.Text))
504 { 504 {
505 LocalNameTextBox.BackColor = Color.LightPink; 505 LocalNameTextBox.BackColor = Color.LightPink;
506 return; 506 return;
507 } 507 }
508   508  
509 // Only unique names allowed. 509 // Only unique names allowed.
510 if (LocalKeyBindings.Bindings.Any(binding => 510 if (LocalKeyBindings.Bindings.Any(binding =>
511 string.Equals(binding.Name, LocalNameTextBox.Text, StringComparison.Ordinal))) 511 string.Equals(binding.Name, LocalNameTextBox.Text, StringComparison.Ordinal)))
512 { 512 {
513 LocalNameTextBox.BackColor = Color.LightPink; 513 LocalNameTextBox.BackColor = Color.LightPink;
514 LocalBindingsCheckedListBox.BackColor = Color.LightPink; 514 LocalBindingsCheckedListBox.BackColor = Color.LightPink;
515 return; 515 return;
516 } 516 }
517   517  
518 LocalNameTextBox.BackColor = Color.Empty; 518 LocalNameTextBox.BackColor = Color.Empty;
519 LocalBindingsCheckedListBox.BackColor = Color.Empty; 519 LocalBindingsCheckedListBox.BackColor = Color.Empty;
520   520  
521 ShowOverlayPanel(); 521 ShowOverlayPanel();
522   522  
523 MouseKeyCombo = new List<string>(); 523 MouseKeyCombo = new List<string>();
524   524  
525 MouseKeyApplicationHook = Hook.GlobalEvents(); 525 MouseKeyApplicationHook = Hook.GlobalEvents();
526 MouseKeyApplicationHook.KeyUp += LocalMouseKeyHookOnKeyUp; 526 MouseKeyApplicationHook.KeyUp += LocalMouseKeyHookOnKeyUp;
527 MouseKeyApplicationHook.KeyDown += LocalMouseKeyHookOnKeyDown; 527 MouseKeyApplicationHook.KeyDown += LocalMouseKeyHookOnKeyDown;
528 } 528 }
529   529  
530 private void ShowOverlayPanel() 530 private void ShowOverlayPanel()
531 { 531 {
532 OverlayPanel.BringToFront(); 532 OverlayPanel.BringToFront();
533 OverlayPanel.Visible = true; 533 OverlayPanel.Visible = true;
534 OverlayPanel.Invalidate(); 534 OverlayPanel.Invalidate();
535 } 535 }
536   536  
537 private async void LocalMouseKeyHookOnKeyUp(object sender, KeyEventArgs e) 537 private async void LocalMouseKeyHookOnKeyUp(object sender, KeyEventArgs e)
538 { 538 {
539 LocalKeyBindings.Bindings.Add(new KeyBinding(LocalNameTextBox.Text, MouseKeyCombo)); 539 LocalKeyBindings.Bindings.Add(new KeyBinding(LocalNameTextBox.Text, MouseKeyCombo));
540   540  
541 LocalCheckedListBoxBindingSource.ResetBindings(false); 541 LocalCheckedListBoxBindingSource.ResetBindings(false);
542   542  
543 MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown; 543 MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown;
544 MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp; 544 MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp;
545   545  
546 MouseKeyApplicationHook.Dispose(); 546 MouseKeyApplicationHook.Dispose();
547   547  
548 LocalNameTextBox.Text = string.Empty; 548 LocalNameTextBox.Text = string.Empty;
549 HideOverlayPanel(); 549 HideOverlayPanel();
550   550  
551 await SaveLocalMouseKeyBindings(); 551 await SaveLocalMouseKeyBindings();
552 } 552 }
553   553  
554 private void HideOverlayPanel() 554 private void HideOverlayPanel()
555 { 555 {
556 OverlayPanel.SendToBack(); 556 OverlayPanel.SendToBack();
557 OverlayPanel.Visible = false; 557 OverlayPanel.Visible = false;
558 OverlayPanel.Invalidate(); 558 OverlayPanel.Invalidate();
559 } 559 }
560   560  
561 private void LocalMouseKeyHookOnKeyDown(object sender, KeyEventArgs e) 561 private void LocalMouseKeyHookOnKeyDown(object sender, KeyEventArgs e)
562 { 562 {
563 e.SuppressKeyPress = true; 563 e.SuppressKeyPress = true;
564   564  
565 KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key); 565 KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key);
566   566  
567 MouseKeyCombo.Add(key); 567 MouseKeyCombo.Add(key);
568 } 568 }
569   569  
570 private void LocalNameTextBoxClick(object sender, EventArgs e) 570 private void LocalNameTextBoxClick(object sender, EventArgs e)
571 { 571 {
572 LocalNameTextBox.BackColor = Color.Empty; 572 LocalNameTextBox.BackColor = Color.Empty;
573 } 573 }
574   574  
575 private async void LocalBindingsRemoveButtonClick(object sender, EventArgs e) 575 private async void LocalBindingsRemoveButtonClick(object sender, EventArgs e)
576 { 576 {
577 var helmBinding = (KeyBinding) LocalBindingsCheckedListBox.SelectedItem; 577 var helmBinding = (KeyBinding) LocalBindingsCheckedListBox.SelectedItem;
578 if (helmBinding == null) 578 if (helmBinding == null)
579 return; 579 return;
580   580  
581 LocalKeyBindings.Bindings.Remove(helmBinding); 581 LocalKeyBindings.Bindings.Remove(helmBinding);
582 LocalCheckedListBoxBindingSource.ResetBindings(false); 582 LocalCheckedListBoxBindingSource.ResetBindings(false);
583   583  
584 await SaveLocalMouseKeyBindings(); 584 await SaveLocalMouseKeyBindings();
585 } 585 }
586   586  
587 private async void LobbySayButtonClick(object sender, EventArgs e) 587 private async void LobbySayButtonClick(object sender, EventArgs e)
588 { 588 {
589 // Do not send messages if the communication is not running. 589 // Do not send messages if the communication is not running.
590 if (!MqttCommunication.Running) 590 if (!MqttCommunication.Running)
591 return; 591 return;
592   592  
593 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); 593 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text);
594   594  
595 LobbySayTextBox.Text = string.Empty; 595 LobbySayTextBox.Text = string.Empty;
596 } 596 }
597   597  
598 private void RemoteBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e) 598 private void RemoteBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e)
599 { 599 {
600 UpdateRemoteItems(); 600 UpdateRemoteItems();
601 } 601 }
602   602  
603 private async void WingManFormOnLoad(object sender, EventArgs e) 603 private async void WingManFormOnLoad(object sender, EventArgs e)
604 { 604 {
605 await LoadLocalMouseKeyBindings(); 605 await LoadLocalMouseKeyBindings();
606   606  
607 await LoadRemoteMouseKeyBindings(); 607 await LoadRemoteMouseKeyBindings();
608 } 608 }
609   609  
610 private void RemoteBindingsBindButtonClicked(object sender, EventArgs e) 610 private void RemoteBindingsBindButtonClicked(object sender, EventArgs e)
611 { 611 {
612 if (string.IsNullOrEmpty((string) RemoteBindingsListBox.SelectedItem)) 612 if (string.IsNullOrEmpty((string) RemoteBindingsListBox.SelectedItem))
613 { 613 {
614 RemoteBindingsListBox.BackColor = Color.LightPink; 614 RemoteBindingsListBox.BackColor = Color.LightPink;
615 return; 615 return;
616 } 616 }
617   617  
618 RemoteBindingsListBox.BackColor = Color.Empty; 618 RemoteBindingsListBox.BackColor = Color.Empty;
619   619  
620 ShowOverlayPanel(); 620 ShowOverlayPanel();
621   621  
622 MouseKeyCombo = new List<string>(); 622 MouseKeyCombo = new List<string>();
623   623  
624 MouseKeyApplicationHook = Hook.GlobalEvents(); 624 MouseKeyApplicationHook = Hook.GlobalEvents();
625 MouseKeyApplicationHook.KeyUp += RemoteKeyHookOnKeyUp; 625 MouseKeyApplicationHook.KeyUp += RemoteKeyHookOnKeyUp;
626 MouseKeyApplicationHook.KeyDown += RemoteKeyHookOnKeyDown; 626 MouseKeyApplicationHook.KeyDown += RemoteKeyHookOnKeyDown;
627 } 627 }
628   628  
629 private void RemoteBindingsUnbindButtonClicked(object sender, EventArgs e) 629 private void RemoteBindingsUnbindButtonClicked(object sender, EventArgs e)
630 { 630 {
631 var item = (string) RemoteBindingsListBox.SelectedItem; 631 var item = (string) RemoteBindingsListBox.SelectedItem;
632 if (string.IsNullOrEmpty(item)) 632 if (string.IsNullOrEmpty(item))
633 { 633 {
634 RemoteBindingsListBox.BackColor = Color.LightPink; 634 RemoteBindingsListBox.BackColor = Color.LightPink;
635 return; 635 return;
636 } 636 }
637   637  
638 RemoteBindingsListBox.BackColor = Color.Empty; 638 RemoteBindingsListBox.BackColor = Color.Empty;
639   639  
640 var remoteKeyBinding = RemoteKeyBindings.Bindings.FirstOrDefault(binding => 640 var remoteKeyBinding = RemoteKeyBindings.Bindings.FirstOrDefault(binding =>
641 string.Equals(binding.Name, item, StringComparison.Ordinal)); 641 string.Equals(binding.Name, item, StringComparison.Ordinal));
642   642  
643 if (remoteKeyBinding == null) 643 if (remoteKeyBinding == null)
644 return; 644 return;
645   645  
646 RemoteKeyBindings.Bindings.Remove(remoteKeyBinding); 646 RemoteKeyBindings.Bindings.Remove(remoteKeyBinding);
647 RemoteBindingsBindToBox.Text = string.Empty; 647 RemoteBindingsBindToBox.Text = string.Empty;
648 } 648 }
649   649  
650 private void RemoteKeyHookOnKeyDown(object sender, KeyEventArgs e) 650 private void RemoteKeyHookOnKeyDown(object sender, KeyEventArgs e)
651 { 651 {
652 e.SuppressKeyPress = true; 652 e.SuppressKeyPress = true;
653   653  
654 if (!KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key)) 654 if (!KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key))
655 return; 655 return;
656   656  
657 MouseKeyCombo.Add(key); 657 MouseKeyCombo.Add(key);
658 } 658 }
659   659  
660 private async void RemoteKeyHookOnKeyUp(object sender, KeyEventArgs e) 660 private async void RemoteKeyHookOnKeyUp(object sender, KeyEventArgs e)
661 { 661 {
662 RemoteKeyBindings.Bindings.Add(new RemoteKeyBinding(RemoteBindingsComboBox.Text, 662 RemoteKeyBindings.Bindings.Add(new RemoteKeyBinding(RemoteBindingsComboBox.Text,
663 (string) RemoteBindingsListBox.SelectedItem, MouseKeyCombo)); 663 (string) RemoteBindingsListBox.SelectedItem, MouseKeyCombo));
664   664  
665 MouseKeyApplicationHook.KeyDown -= RemoteKeyHookOnKeyDown; 665 MouseKeyApplicationHook.KeyDown -= RemoteKeyHookOnKeyDown;
666 MouseKeyApplicationHook.KeyUp -= RemoteKeyHookOnKeyUp; 666 MouseKeyApplicationHook.KeyUp -= RemoteKeyHookOnKeyUp;
667   667  
668 MouseKeyApplicationHook.Dispose(); 668 MouseKeyApplicationHook.Dispose();
669   669  
670 RemoteBindingsBindToBox.Text = string.Join(" + ", MouseKeyCombo); 670 RemoteBindingsBindToBox.Text = string.Join(" + ", MouseKeyCombo);
671 HideOverlayPanel(); 671 HideOverlayPanel();
672   672  
673 await SaveRemoteMouseKeyBindings(); 673 await SaveRemoteMouseKeyBindings();
674 } 674 }
675   675  
676 private void RemoteBindingsListBoxSelectedValueChanged(object sender, EventArgs e) 676 private void RemoteBindingsListBoxSelectedValueChanged(object sender, EventArgs e)
677 { 677 {
678 RemoteBindingsBindToBox.Text = ""; 678 RemoteBindingsBindToBox.Text = "";
679   679  
680 var name = (string) RemoteBindingsListBox.SelectedItem; 680 var name = (string) RemoteBindingsListBox.SelectedItem;
681 if (string.IsNullOrEmpty(name)) 681 if (string.IsNullOrEmpty(name))
682 return; 682 return;
683   683  
684 var nick = RemoteBindingsComboBox.Text; 684 var nick = RemoteBindingsComboBox.Text;
685 if (string.IsNullOrEmpty(nick)) 685 if (string.IsNullOrEmpty(nick))
686 return; 686 return;
687   687  
688 foreach (var binding in RemoteKeyBindings.Bindings) 688 foreach (var binding in RemoteKeyBindings.Bindings)
689 { 689 {
690 if (!string.Equals(binding.Nick, nick) || !string.Equals(binding.Name, name)) 690 if (!string.Equals(binding.Nick, nick) || !string.Equals(binding.Name, name))
691 continue; 691 continue;
692   692  
693 RemoteBindingsBindToBox.Text = string.Join(" + ", binding.Keys); 693 RemoteBindingsBindToBox.Text = string.Join(" + ", binding.Keys);
694 break; 694 break;
695 } 695 }
696 } 696 }
697   697  
698 private void WingManFormResized(object sender, EventArgs e) 698 private void WingManFormResized(object sender, EventArgs e)
699 { 699 {
700 if (WindowState == FormWindowState.Minimized) 700 if (WindowState == FormWindowState.Minimized) Hide();
701 { -  
702 Hide(); -  
703 } -  
704 } 701 }
705   702  
706 private void NotifyIconDoubleClick(object sender, EventArgs e) 703 private void NotifyIconDoubleClick(object sender, EventArgs e)
707 { 704 {
708 Show(); 705 Show();
709 WindowState = FormWindowState.Normal; 706 WindowState = FormWindowState.Normal;
710 } 707 }
711   708  
712 private async void LocalBindingsCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e) 709 private async void LocalBindingsCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)
713 { 710 {
714 var helmBinding = (KeyBinding) LocalBindingsCheckedListBox.Items[e.Index]; 711 var helmBinding = (KeyBinding) LocalBindingsCheckedListBox.Items[e.Index];
715 if (helmBinding == null) 712 if (helmBinding == null)
716 return; 713 return;
717   714  
718 switch (e.NewValue) 715 switch (e.NewValue)
719 { 716 {
720 case CheckState.Checked: 717 case CheckState.Checked:
721 helmBinding.Enabled = true; 718 helmBinding.Enabled = true;
722 break; 719 break;
723 case CheckState.Unchecked: 720 case CheckState.Unchecked:
724 helmBinding.Enabled = false; 721 helmBinding.Enabled = false;
725 break; 722 break;
726 } 723 }
727   724  
728 await SaveLocalMouseKeyBindings(); 725 await SaveLocalMouseKeyBindings();
729 } 726 }
730   727  
731 #region Saving and loading 728 #region Saving and loading
732   729  
733 private async Task SaveLocalMouseKeyBindings() 730 private async Task SaveLocalMouseKeyBindings()
734 { 731 {
735 try 732 try
736 { 733 {
737 using (var memoryStream = new MemoryStream()) 734 using (var memoryStream = new MemoryStream())
738 { 735 {
739 LocalKeyBindings.XmlSerializer.Serialize(memoryStream, LocalKeyBindings); 736 LocalKeyBindings.XmlSerializer.Serialize(memoryStream, LocalKeyBindings);
740   737  
741 memoryStream.Position = 0L; 738 memoryStream.Position = 0L;
742   739  
743 using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Create)) 740 using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Create))
744 { 741 {
745 await memoryStream.CopyToAsync(fileStream); 742 await memoryStream.CopyToAsync(fileStream);
746 } 743 }
747 } 744 }
748 } 745 }
749 catch (Exception) 746 catch (Exception)
750 { 747 {
751 ActivityTextBox.AppendText( 748 ActivityTextBox.AppendText(
752 $"{Strings.Failed_saving_local_bindings}{Environment.NewLine}"); 749 $"{Strings.Failed_saving_local_bindings}{Environment.NewLine}");
753 } 750 }
754 } 751 }
755   752  
756 private async Task LoadLocalMouseKeyBindings() 753 private async Task LoadLocalMouseKeyBindings()
757 { 754 {
758 try 755 try
759 { 756 {
760 using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Open)) 757 using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Open))
761 { 758 {
762 using (var memoryStream = new MemoryStream()) 759 using (var memoryStream = new MemoryStream())
763 { 760 {
764 await fileStream.CopyToAsync(memoryStream); 761 await fileStream.CopyToAsync(memoryStream);
765   762  
766 memoryStream.Position = 0L; 763 memoryStream.Position = 0L;
767   764  
768 var loadedBindings = 765 var loadedBindings =
769 (LocalKeyBindings) LocalKeyBindings.XmlSerializer.Deserialize(memoryStream); 766 (LocalKeyBindings) LocalKeyBindings.XmlSerializer.Deserialize(memoryStream);
770   767  
771 foreach (var binding in loadedBindings.Bindings) 768 foreach (var binding in loadedBindings.Bindings)
772 LocalKeyBindings.Bindings.Add(binding); 769 LocalKeyBindings.Bindings.Add(binding);
773   770  
774   771  
775 LocalCheckedListBoxBindingSource.ResetBindings(false); 772 LocalCheckedListBoxBindingSource.ResetBindings(false);
776 } 773 }
777 } 774 }
778 } 775 }
779 catch (Exception) 776 catch (Exception)
780 { 777 {
781 ActivityTextBox.AppendText( 778 ActivityTextBox.AppendText(
782 $"{Strings.Failed_loading_local_bindings}{Environment.NewLine}"); 779 $"{Strings.Failed_loading_local_bindings}{Environment.NewLine}");
783 } 780 }
784 } 781 }
785   782  
786 private async Task SaveRemoteMouseKeyBindings() 783 private async Task SaveRemoteMouseKeyBindings()
787 { 784 {
788 try 785 try
789 { 786 {
790 using (var memoryStream = new MemoryStream()) 787 using (var memoryStream = new MemoryStream())
791 { 788 {
792 RemoteKeyBindings.XmlSerializer.Serialize(memoryStream, RemoteKeyBindings); 789 RemoteKeyBindings.XmlSerializer.Serialize(memoryStream, RemoteKeyBindings);
793   790  
794 memoryStream.Position = 0L; 791 memoryStream.Position = 0L;
795   792  
796 using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Create)) 793 using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Create))
797 { 794 {
798 await memoryStream.CopyToAsync(fileStream); 795 await memoryStream.CopyToAsync(fileStream);
799 } 796 }
800 } 797 }
801 } 798 }
802 catch (Exception) 799 catch (Exception)
803 { 800 {
804 ActivityTextBox.AppendText( 801 ActivityTextBox.AppendText(
805 $"{Strings.Failed_saving_remote_bindings}{Environment.NewLine}"); 802 $"{Strings.Failed_saving_remote_bindings}{Environment.NewLine}");
806 } 803 }
807 } 804 }
808   805  
809 private async Task LoadRemoteMouseKeyBindings() 806 private async Task LoadRemoteMouseKeyBindings()
810 { 807 {
811 try 808 try
812 { 809 {
813 using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Open)) 810 using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Open))
814 { 811 {
815 using (var memoryStream = new MemoryStream()) 812 using (var memoryStream = new MemoryStream())
816 { 813 {
817 await fileStream.CopyToAsync(memoryStream); 814 await fileStream.CopyToAsync(memoryStream);
818   815  
819 memoryStream.Position = 0L; 816 memoryStream.Position = 0L;
820   817  
821 var loadedBindings = 818 var loadedBindings =
822 (RemoteKeyBindings) RemoteKeyBindings.XmlSerializer.Deserialize(memoryStream); 819 (RemoteKeyBindings) RemoteKeyBindings.XmlSerializer.Deserialize(memoryStream);
823   820  
824 foreach (var binding in loadedBindings.Bindings) 821 foreach (var binding in loadedBindings.Bindings)
825 RemoteKeyBindings.Bindings.Add(binding); 822 RemoteKeyBindings.Bindings.Add(binding);
826 } 823 }
827 } 824 }
828 } 825 }
829 catch (Exception) 826 catch (Exception)
830 { 827 {
831 ActivityTextBox.AppendText( 828 ActivityTextBox.AppendText(
832 $"{Strings.Failed_loading_remote_bindings}{Environment.NewLine}"); 829 $"{Strings.Failed_loading_remote_bindings}{Environment.NewLine}");
833 } 830 }
834 } 831 }
835   832  
836 #endregion 833 #endregion
837 } 834 }
838 } 835 }
839   836