WingMan – Diff between revs 24 and 25

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 24 Rev 25
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;
-   307 notifyIcon1.BalloonTipText = $"{e.Nick} : {e.Message}{Environment.NewLine}";
-   308 notifyIcon1.ShowBalloonTip(1000);
-   309  
306 LobbyTextBox.AppendText($"{e.Nick} : {e.Message}{Environment.NewLine}"); 310 LobbyTextBox.AppendText($"{e.Nick} : {e.Message}{Environment.NewLine}");
307 } 311 }
308   312  
309 private void AddressTextBoxClick(object sender, EventArgs e) 313 private void AddressTextBoxClick(object sender, EventArgs e)
310 { 314 {
311 Address.BackColor = Color.Empty; 315 Address.BackColor = Color.Empty;
312 } 316 }
313   317  
314 private void PortTextBoxClick(object sender, EventArgs e) 318 private void PortTextBoxClick(object sender, EventArgs e)
315 { 319 {
316 Port.BackColor = Color.Empty; 320 Port.BackColor = Color.Empty;
317 } 321 }
318   322  
319 private async void HostButtonClickAsync(object sender, EventArgs e) 323 private async void HostButtonClickAsync(object sender, EventArgs e)
320 { 324 {
321 // Stop the MQTT server if it is running. 325 // Stop the MQTT server if it is running.
322 if (MqttCommunication.Running) 326 if (MqttCommunication.Running)
323 { 327 {
324 await MqttCommunication.Stop(); 328 await MqttCommunication.Stop();
325 HostButton.BackColor = Color.Empty; 329 HostButton.BackColor = Color.Empty;
326   330  
327 // Enable controls. 331 // Enable controls.
328 ConnectButton.Enabled = true; 332 ConnectButton.Enabled = true;
329 Address.Enabled = true; 333 Address.Enabled = true;
330 Port.Enabled = true; 334 Port.Enabled = true;
331 Nick.Enabled = true; 335 Nick.Enabled = true;
332 Password.Enabled = true; 336 Password.Enabled = true;
333 return; 337 return;
334 } 338 }
335   339  
336 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))
337 return; 341 return;
338   342  
339 StoreConnectionAutocomplete(); 343 StoreConnectionAutocomplete();
340   344  
341 // Start the MQTT server. 345 // Start the MQTT server.
342 if (!await MqttCommunication 346 if (!await MqttCommunication
343 .Start(MqttCommunicationType.Server, ipAddress, port, nick, password)) 347 .Start(MqttCommunicationType.Server, ipAddress, port, nick, password))
344 { 348 {
345 ActivityTextBox.AppendText( 349 ActivityTextBox.AppendText(
346 $"{Strings.Failed_starting_server}{Environment.NewLine}"); 350 $"{Strings.Failed_starting_server}{Environment.NewLine}");
347 return; 351 return;
348 } 352 }
349   353  
350 HostButton.BackColor = Color.Aquamarine; 354 HostButton.BackColor = Color.Aquamarine;
351   355  
352 // Disable controls 356 // Disable controls
353 ConnectButton.Enabled = false; 357 ConnectButton.Enabled = false;
354 Address.Enabled = false; 358 Address.Enabled = false;
355 Port.Enabled = false; 359 Port.Enabled = false;
356 Nick.Enabled = false; 360 Nick.Enabled = false;
357 Password.Enabled = false; 361 Password.Enabled = false;
358 } 362 }
359   363  
360 private async void StoreConnectionAutocomplete() 364 private async void StoreConnectionAutocomplete()
361 { 365 {
362 Address.AutoCompleteCustomSource.Add(Address.Text); 366 Address.AutoCompleteCustomSource.Add(Address.Text);
363   367  
364 await AutoCompletion.Save(Address.Name, Address.AutoCompleteCustomSource); 368 await AutoCompletion.Save(Address.Name, Address.AutoCompleteCustomSource);
365   369  
366 Port.AutoCompleteCustomSource.Add(Port.Text); 370 Port.AutoCompleteCustomSource.Add(Port.Text);
367   371  
368 await AutoCompletion.Save(Port.Name, Port.AutoCompleteCustomSource); 372 await AutoCompletion.Save(Port.Name, Port.AutoCompleteCustomSource);
369   373  
370 Nick.AutoCompleteCustomSource.Add(Nick.Text); 374 Nick.AutoCompleteCustomSource.Add(Nick.Text);
371   375  
372 await AutoCompletion.Save(Nick.Name, Nick.AutoCompleteCustomSource); 376 await AutoCompletion.Save(Nick.Name, Nick.AutoCompleteCustomSource);
373 } 377 }
374   378  
375 private bool ValidateConnectionParameters( 379 private bool ValidateConnectionParameters(
376 out IPAddress address, 380 out IPAddress address,
377 out int port, 381 out int port,
378 out string nick, 382 out string nick,
379 out string password) 383 out string password)
380 { 384 {
381 address = IPAddress.Any; 385 address = IPAddress.Any;
382 port = 0; 386 port = 0;
383 nick = string.Empty; 387 nick = string.Empty;
384 password = string.Empty; 388 password = string.Empty;
385   389  
386 if (string.IsNullOrEmpty(Address.Text) && 390 if (string.IsNullOrEmpty(Address.Text) &&
387 string.IsNullOrEmpty(Port.Text) && 391 string.IsNullOrEmpty(Port.Text) &&
388 string.IsNullOrEmpty(Nick.Text) && 392 string.IsNullOrEmpty(Nick.Text) &&
389 string.IsNullOrEmpty(Password.Text)) 393 string.IsNullOrEmpty(Password.Text))
390 { 394 {
391 Address.BackColor = Color.LightPink; 395 Address.BackColor = Color.LightPink;
392 Port.BackColor = Color.LightPink; 396 Port.BackColor = Color.LightPink;
393 Nick.BackColor = Color.LightPink; 397 Nick.BackColor = Color.LightPink;
394 Password.BackColor = Color.LightPink; 398 Password.BackColor = Color.LightPink;
395 return false; 399 return false;
396 } 400 }
397   401  
398 if (!IPAddress.TryParse(Address.Text, out address)) 402 if (!IPAddress.TryParse(Address.Text, out address))
399 try 403 try
400 { 404 {
401 address = Dns.GetHostAddresses(Address.Text).FirstOrDefault(); 405 address = Dns.GetHostAddresses(Address.Text).FirstOrDefault();
402 } 406 }
403 catch (Exception ex) 407 catch (Exception ex)
404 { 408 {
405 ActivityTextBox.AppendText( 409 ActivityTextBox.AppendText(
406 $"{Strings.Could_not_resolve_hostname} : {ex.Message}{Environment.NewLine}"); 410 $"{Strings.Could_not_resolve_hostname} : {ex.Message}{Environment.NewLine}");
407   411  
408 Address.BackColor = Color.LightPink; 412 Address.BackColor = Color.LightPink;
409 return false; 413 return false;
410 } 414 }
411   415  
412 if (!uint.TryParse(Port.Text, out var uPort)) 416 if (!uint.TryParse(Port.Text, out var uPort))
413 { 417 {
414 Port.BackColor = Color.LightPink; 418 Port.BackColor = Color.LightPink;
415 return false; 419 return false;
416 } 420 }
417   421  
418 port = (int) uPort; 422 port = (int) uPort;
419   423  
420 if (string.IsNullOrEmpty(Nick.Text)) 424 if (string.IsNullOrEmpty(Nick.Text))
421 { 425 {
422 Nick.BackColor = Color.LightPink; 426 Nick.BackColor = Color.LightPink;
423 return false; 427 return false;
424 } 428 }
425   429  
426 nick = Nick.Text; 430 nick = Nick.Text;
427   431  
428 if (string.IsNullOrEmpty(Password.Text)) 432 if (string.IsNullOrEmpty(Password.Text))
429 { 433 {
430 Password.BackColor = Color.LightPink; 434 Password.BackColor = Color.LightPink;
431 return false; 435 return false;
432 } 436 }
433   437  
434 password = AES.ExpandKey(Password.Text); 438 password = AES.ExpandKey(Password.Text);
435   439  
436 Address.BackColor = Color.Empty; 440 Address.BackColor = Color.Empty;
437 Port.BackColor = Color.Empty; 441 Port.BackColor = Color.Empty;
438 Nick.BackColor = Color.Empty; 442 Nick.BackColor = Color.Empty;
439 Password.BackColor = Color.Empty; 443 Password.BackColor = Color.Empty;
440   444  
441 return true; 445 return true;
442 } 446 }
443   447  
444 private async void ConnectButtonClickAsync(object sender, EventArgs e) 448 private async void ConnectButtonClickAsync(object sender, EventArgs e)
445 { 449 {
446 if (MqttCommunication.Running) 450 if (MqttCommunication.Running)
447 { 451 {
448 await MqttCommunication.Stop(); 452 await MqttCommunication.Stop();
449 ConnectButton.Text = Strings.Connect; 453 ConnectButton.Text = Strings.Connect;
450 ConnectButton.BackColor = Color.Empty; 454 ConnectButton.BackColor = Color.Empty;
451   455  
452 Address.Enabled = true; 456 Address.Enabled = true;
453 Port.Enabled = true; 457 Port.Enabled = true;
454 Nick.Enabled = true; 458 Nick.Enabled = true;
455 Password.Enabled = true; 459 Password.Enabled = true;
456 HostButton.Enabled = true; 460 HostButton.Enabled = true;
457 return; 461 return;
458 } 462 }
459   463  
460 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))
461 return; 465 return;
462   466  
463 StoreConnectionAutocomplete(); 467 StoreConnectionAutocomplete();
464   468  
465 if (!await MqttCommunication 469 if (!await MqttCommunication
466 .Start(MqttCommunicationType.Client, ipAddress, port, nick, password)) 470 .Start(MqttCommunicationType.Client, ipAddress, port, nick, password))
467 { 471 {
468 ActivityTextBox.AppendText( 472 ActivityTextBox.AppendText(
469 $"{Strings.Failed_starting_client}{Environment.NewLine}"); 473 $"{Strings.Failed_starting_client}{Environment.NewLine}");
470 return; 474 return;
471 } 475 }
472   476  
473 ConnectButton.Text = Strings.Disconnect; 477 ConnectButton.Text = Strings.Disconnect;
474 ConnectButton.BackColor = Color.Aquamarine; 478 ConnectButton.BackColor = Color.Aquamarine;
475   479  
476 HostButton.Enabled = false; 480 HostButton.Enabled = false;
477 Address.Enabled = false; 481 Address.Enabled = false;
478 Port.Enabled = false; 482 Port.Enabled = false;
479 Nick.Enabled = false; 483 Nick.Enabled = false;
480 Password.Enabled = false; 484 Password.Enabled = false;
481 } 485 }
482   486  
483 private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e) 487 private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e)
484 { 488 {
-   489 // Do not send messages if the communication is not running.
-   490 if (!MqttCommunication.Running)
-   491 return;
-   492  
485 if (e.KeyCode != Keys.Enter) 493 if (e.KeyCode != Keys.Enter)
486 return; 494 return;
487   495  
488 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); 496 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text);
489   497  
490 LobbySayTextBox.Text = string.Empty; 498 LobbySayTextBox.Text = string.Empty;
491 } 499 }
492   500  
493 private void LocalAddBindingButtonClick(object sender, EventArgs e) 501 private void LocalAddBindingButtonClick(object sender, EventArgs e)
494 { 502 {
495 if (string.IsNullOrEmpty(LocalNameTextBox.Text)) 503 if (string.IsNullOrEmpty(LocalNameTextBox.Text))
496 { 504 {
497 LocalNameTextBox.BackColor = Color.LightPink; 505 LocalNameTextBox.BackColor = Color.LightPink;
498 return; 506 return;
499 } 507 }
500   508  
501 // Only unique names allowed. 509 // Only unique names allowed.
502 if (LocalKeyBindings.Bindings.Any(binding => 510 if (LocalKeyBindings.Bindings.Any(binding =>
503 string.Equals(binding.Name, LocalNameTextBox.Text, StringComparison.Ordinal))) 511 string.Equals(binding.Name, LocalNameTextBox.Text, StringComparison.Ordinal)))
504 { 512 {
505 LocalNameTextBox.BackColor = Color.LightPink; 513 LocalNameTextBox.BackColor = Color.LightPink;
506 LocalBindingsCheckedListBox.BackColor = Color.LightPink; 514 LocalBindingsCheckedListBox.BackColor = Color.LightPink;
507 return; 515 return;
508 } 516 }
509   517  
510 LocalNameTextBox.BackColor = Color.Empty; 518 LocalNameTextBox.BackColor = Color.Empty;
511 LocalBindingsCheckedListBox.BackColor = Color.Empty; 519 LocalBindingsCheckedListBox.BackColor = Color.Empty;
512   520  
513 ShowOverlayPanel(); 521 ShowOverlayPanel();
514   522  
515 MouseKeyCombo = new List<string>(); 523 MouseKeyCombo = new List<string>();
516   524  
517 MouseKeyApplicationHook = Hook.GlobalEvents(); 525 MouseKeyApplicationHook = Hook.GlobalEvents();
518 MouseKeyApplicationHook.KeyUp += LocalMouseKeyHookOnKeyUp; 526 MouseKeyApplicationHook.KeyUp += LocalMouseKeyHookOnKeyUp;
519 MouseKeyApplicationHook.KeyDown += LocalMouseKeyHookOnKeyDown; 527 MouseKeyApplicationHook.KeyDown += LocalMouseKeyHookOnKeyDown;
520 } 528 }
521   529  
522 private void ShowOverlayPanel() 530 private void ShowOverlayPanel()
523 { 531 {
524 OverlayPanel.BringToFront(); 532 OverlayPanel.BringToFront();
525 OverlayPanel.Visible = true; 533 OverlayPanel.Visible = true;
526 OverlayPanel.Invalidate(); 534 OverlayPanel.Invalidate();
527 } 535 }
528   536  
529 private async void LocalMouseKeyHookOnKeyUp(object sender, KeyEventArgs e) 537 private async void LocalMouseKeyHookOnKeyUp(object sender, KeyEventArgs e)
530 { 538 {
531 LocalKeyBindings.Bindings.Add(new KeyBinding(LocalNameTextBox.Text, MouseKeyCombo)); 539 LocalKeyBindings.Bindings.Add(new KeyBinding(LocalNameTextBox.Text, MouseKeyCombo));
532   540  
533 LocalCheckedListBoxBindingSource.ResetBindings(false); 541 LocalCheckedListBoxBindingSource.ResetBindings(false);
534   542  
535 MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown; 543 MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown;
536 MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp; 544 MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp;
537   545  
538 MouseKeyApplicationHook.Dispose(); 546 MouseKeyApplicationHook.Dispose();
539   547  
540 LocalNameTextBox.Text = string.Empty; 548 LocalNameTextBox.Text = string.Empty;
541 HideOverlayPanel(); 549 HideOverlayPanel();
542   550  
543 await SaveLocalMouseKeyBindings(); 551 await SaveLocalMouseKeyBindings();
544 } 552 }
545   553  
546 private void HideOverlayPanel() 554 private void HideOverlayPanel()
547 { 555 {
548 OverlayPanel.SendToBack(); 556 OverlayPanel.SendToBack();
549 OverlayPanel.Visible = false; 557 OverlayPanel.Visible = false;
550 OverlayPanel.Invalidate(); 558 OverlayPanel.Invalidate();
551 } 559 }
552   560  
553 private void LocalMouseKeyHookOnKeyDown(object sender, KeyEventArgs e) 561 private void LocalMouseKeyHookOnKeyDown(object sender, KeyEventArgs e)
554 { 562 {
555 e.SuppressKeyPress = true; 563 e.SuppressKeyPress = true;
556   564  
557 KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key); 565 KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key);
558   566  
559 MouseKeyCombo.Add(key); 567 MouseKeyCombo.Add(key);
560 } 568 }
561   569  
562 private void LocalNameTextBoxClick(object sender, EventArgs e) 570 private void LocalNameTextBoxClick(object sender, EventArgs e)
563 { 571 {
564 LocalNameTextBox.BackColor = Color.Empty; 572 LocalNameTextBox.BackColor = Color.Empty;
565 } 573 }
566   574  
567 private async void LocalBindingsRemoveButtonClick(object sender, EventArgs e) 575 private async void LocalBindingsRemoveButtonClick(object sender, EventArgs e)
568 { 576 {
569 var helmBinding = (KeyBinding) LocalBindingsCheckedListBox.SelectedItem; 577 var helmBinding = (KeyBinding) LocalBindingsCheckedListBox.SelectedItem;
570 if (helmBinding == null) 578 if (helmBinding == null)
571 return; 579 return;
572   580  
573 LocalKeyBindings.Bindings.Remove(helmBinding); 581 LocalKeyBindings.Bindings.Remove(helmBinding);
574 LocalCheckedListBoxBindingSource.ResetBindings(false); 582 LocalCheckedListBoxBindingSource.ResetBindings(false);
575   583  
576 await SaveLocalMouseKeyBindings(); 584 await SaveLocalMouseKeyBindings();
577 } 585 }
578   586  
579 private async void LobbySayButtonClick(object sender, EventArgs e) 587 private async void LobbySayButtonClick(object sender, EventArgs e)
580 { 588 {
-   589 // Do not send messages if the communication is not running.
-   590 if (!MqttCommunication.Running)
-   591 return;
-   592  
581 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); 593 await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text);
582   594  
583 LobbySayTextBox.Text = string.Empty; 595 LobbySayTextBox.Text = string.Empty;
584 } 596 }
585   597  
586 private void RemoteBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e) 598 private void RemoteBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e)
587 { 599 {
588 UpdateRemoteItems(); 600 UpdateRemoteItems();
589 } 601 }
590   602  
591 private async void WingManFormOnLoad(object sender, EventArgs e) 603 private async void WingManFormOnLoad(object sender, EventArgs e)
592 { 604 {
593 await LoadLocalMouseKeyBindings(); 605 await LoadLocalMouseKeyBindings();
594   606  
595 await LoadRemoteMouseKeyBindings(); 607 await LoadRemoteMouseKeyBindings();
596 } 608 }
597   609  
598 private void RemoteBindingsBindButtonClicked(object sender, EventArgs e) 610 private void RemoteBindingsBindButtonClicked(object sender, EventArgs e)
599 { 611 {
600 if (string.IsNullOrEmpty((string) RemoteBindingsListBox.SelectedItem)) 612 if (string.IsNullOrEmpty((string) RemoteBindingsListBox.SelectedItem))
601 { 613 {
602 RemoteBindingsListBox.BackColor = Color.LightPink; 614 RemoteBindingsListBox.BackColor = Color.LightPink;
603 return; 615 return;
604 } 616 }
605   617  
606 RemoteBindingsListBox.BackColor = Color.Empty; 618 RemoteBindingsListBox.BackColor = Color.Empty;
607   619  
608 ShowOverlayPanel(); 620 ShowOverlayPanel();
609   621  
610 MouseKeyCombo = new List<string>(); 622 MouseKeyCombo = new List<string>();
611   623  
612 MouseKeyApplicationHook = Hook.GlobalEvents(); 624 MouseKeyApplicationHook = Hook.GlobalEvents();
613 MouseKeyApplicationHook.KeyUp += RemoteKeyHookOnKeyUp; 625 MouseKeyApplicationHook.KeyUp += RemoteKeyHookOnKeyUp;
614 MouseKeyApplicationHook.KeyDown += RemoteKeyHookOnKeyDown; 626 MouseKeyApplicationHook.KeyDown += RemoteKeyHookOnKeyDown;
615 } 627 }
616   628  
617 private void RemoteBindingsUnbindButtonClicked(object sender, EventArgs e) 629 private void RemoteBindingsUnbindButtonClicked(object sender, EventArgs e)
618 { 630 {
619 var item = (string) RemoteBindingsListBox.SelectedItem; 631 var item = (string) RemoteBindingsListBox.SelectedItem;
620 if (string.IsNullOrEmpty(item)) 632 if (string.IsNullOrEmpty(item))
621 { 633 {
622 RemoteBindingsListBox.BackColor = Color.LightPink; 634 RemoteBindingsListBox.BackColor = Color.LightPink;
623 return; 635 return;
624 } 636 }
625   637  
626 RemoteBindingsListBox.BackColor = Color.Empty; 638 RemoteBindingsListBox.BackColor = Color.Empty;
627   639  
628 var remoteKeyBinding = RemoteKeyBindings.Bindings.FirstOrDefault(binding => 640 var remoteKeyBinding = RemoteKeyBindings.Bindings.FirstOrDefault(binding =>
629 string.Equals(binding.Name, item, StringComparison.Ordinal)); 641 string.Equals(binding.Name, item, StringComparison.Ordinal));
630   642  
631 if (remoteKeyBinding == null) 643 if (remoteKeyBinding == null)
632 return; 644 return;
633   645  
634 RemoteKeyBindings.Bindings.Remove(remoteKeyBinding); 646 RemoteKeyBindings.Bindings.Remove(remoteKeyBinding);
635 RemoteBindingsBindToBox.Text = string.Empty; 647 RemoteBindingsBindToBox.Text = string.Empty;
636 } 648 }
637   649  
638 private void RemoteKeyHookOnKeyDown(object sender, KeyEventArgs e) 650 private void RemoteKeyHookOnKeyDown(object sender, KeyEventArgs e)
639 { 651 {
640 e.SuppressKeyPress = true; 652 e.SuppressKeyPress = true;
641   653  
642 if (!KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key)) 654 if (!KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key))
643 return; 655 return;
644   656  
645 MouseKeyCombo.Add(key); 657 MouseKeyCombo.Add(key);
646 } 658 }
647   659  
648 private async void RemoteKeyHookOnKeyUp(object sender, KeyEventArgs e) 660 private async void RemoteKeyHookOnKeyUp(object sender, KeyEventArgs e)
649 { 661 {
650 RemoteKeyBindings.Bindings.Add(new RemoteKeyBinding(RemoteBindingsComboBox.Text, 662 RemoteKeyBindings.Bindings.Add(new RemoteKeyBinding(RemoteBindingsComboBox.Text,
651 (string) RemoteBindingsListBox.SelectedItem, MouseKeyCombo)); 663 (string) RemoteBindingsListBox.SelectedItem, MouseKeyCombo));
652   664  
653 MouseKeyApplicationHook.KeyDown -= RemoteKeyHookOnKeyDown; 665 MouseKeyApplicationHook.KeyDown -= RemoteKeyHookOnKeyDown;
654 MouseKeyApplicationHook.KeyUp -= RemoteKeyHookOnKeyUp; 666 MouseKeyApplicationHook.KeyUp -= RemoteKeyHookOnKeyUp;
655   667  
656 MouseKeyApplicationHook.Dispose(); 668 MouseKeyApplicationHook.Dispose();
657   669  
658 RemoteBindingsBindToBox.Text = string.Join(" + ", MouseKeyCombo); 670 RemoteBindingsBindToBox.Text = string.Join(" + ", MouseKeyCombo);
659 HideOverlayPanel(); 671 HideOverlayPanel();
660   672  
661 await SaveRemoteMouseKeyBindings(); 673 await SaveRemoteMouseKeyBindings();
662 } 674 }
663   675  
664 private void RemoteBindingsListBoxSelectedValueChanged(object sender, EventArgs e) 676 private void RemoteBindingsListBoxSelectedValueChanged(object sender, EventArgs e)
665 { 677 {
666 RemoteBindingsBindToBox.Text = ""; 678 RemoteBindingsBindToBox.Text = "";
667   679  
668 var name = (string) RemoteBindingsListBox.SelectedItem; 680 var name = (string) RemoteBindingsListBox.SelectedItem;
669 if (string.IsNullOrEmpty(name)) 681 if (string.IsNullOrEmpty(name))
670 return; 682 return;
671   683  
672 var nick = RemoteBindingsComboBox.Text; 684 var nick = RemoteBindingsComboBox.Text;
673 if (string.IsNullOrEmpty(nick)) 685 if (string.IsNullOrEmpty(nick))
674 return; 686 return;
675   687  
676 foreach (var binding in RemoteKeyBindings.Bindings) 688 foreach (var binding in RemoteKeyBindings.Bindings)
677 { 689 {
678 if (!string.Equals(binding.Nick, nick) || !string.Equals(binding.Name, name)) 690 if (!string.Equals(binding.Nick, nick) || !string.Equals(binding.Name, name))
679 continue; 691 continue;
680   692  
681 RemoteBindingsBindToBox.Text = string.Join(" + ", binding.Keys); 693 RemoteBindingsBindToBox.Text = string.Join(" + ", binding.Keys);
682 break; 694 break;
683 } 695 }
684 } 696 }
685   697  
686 private void WingManFormResized(object sender, EventArgs e) 698 private void WingManFormResized(object sender, EventArgs e)
687 { 699 {
688 if (WindowState == FormWindowState.Minimized) 700 if (WindowState == FormWindowState.Minimized)
689 { 701 {
690 Hide(); 702 Hide();
691 notifyIcon1.Visible = true; -  
692 } 703 }
693 } 704 }
694   705  
695 private void NotifyIconDoubleClick(object sender, EventArgs e) 706 private void NotifyIconDoubleClick(object sender, EventArgs e)
696 { 707 {
697 Show(); 708 Show();
698 WindowState = FormWindowState.Normal; 709 WindowState = FormWindowState.Normal;
699 notifyIcon1.Visible = false; -  
700 } 710 }
701   711  
702 private async void LocalBindingsCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e) 712 private async void LocalBindingsCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)
703 { 713 {
704 var helmBinding = (KeyBinding) LocalBindingsCheckedListBox.Items[e.Index]; 714 var helmBinding = (KeyBinding) LocalBindingsCheckedListBox.Items[e.Index];
705 if (helmBinding == null) 715 if (helmBinding == null)
706 return; 716 return;
707   717  
708 switch (e.NewValue) 718 switch (e.NewValue)
709 { 719 {
710 case CheckState.Checked: 720 case CheckState.Checked:
711 helmBinding.Enabled = true; 721 helmBinding.Enabled = true;
712 break; 722 break;
713 case CheckState.Unchecked: 723 case CheckState.Unchecked:
714 helmBinding.Enabled = false; 724 helmBinding.Enabled = false;
715 break; 725 break;
716 } 726 }
717   727  
718 await SaveLocalMouseKeyBindings(); 728 await SaveLocalMouseKeyBindings();
719 } 729 }
720   730  
721 #region Saving and loading 731 #region Saving and loading
722   732  
723 private async Task SaveLocalMouseKeyBindings() 733 private async Task SaveLocalMouseKeyBindings()
724 { 734 {
725 try 735 try
726 { 736 {
727 using (var memoryStream = new MemoryStream()) 737 using (var memoryStream = new MemoryStream())
728 { 738 {
729 LocalKeyBindings.XmlSerializer.Serialize(memoryStream, LocalKeyBindings); 739 LocalKeyBindings.XmlSerializer.Serialize(memoryStream, LocalKeyBindings);
730   740  
731 memoryStream.Position = 0L; 741 memoryStream.Position = 0L;
732   742  
733 using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Create)) 743 using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Create))
734 { 744 {
735 await memoryStream.CopyToAsync(fileStream); 745 await memoryStream.CopyToAsync(fileStream);
736 } 746 }
737 } 747 }
738 } 748 }
739 catch (Exception) 749 catch (Exception)
740 { 750 {
741 ActivityTextBox.AppendText( 751 ActivityTextBox.AppendText(
742 $"{Strings.Failed_saving_local_bindings}{Environment.NewLine}"); 752 $"{Strings.Failed_saving_local_bindings}{Environment.NewLine}");
743 } 753 }
744 } 754 }
745   755  
746 private async Task LoadLocalMouseKeyBindings() 756 private async Task LoadLocalMouseKeyBindings()
747 { 757 {
748 try 758 try
749 { 759 {
750 using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Open)) 760 using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Open))
751 { 761 {
752 using (var memoryStream = new MemoryStream()) 762 using (var memoryStream = new MemoryStream())
753 { 763 {
754 await fileStream.CopyToAsync(memoryStream); 764 await fileStream.CopyToAsync(memoryStream);
755   765  
756 memoryStream.Position = 0L; 766 memoryStream.Position = 0L;
757   767  
758 var loadedBindings = 768 var loadedBindings =
759 (LocalKeyBindings) LocalKeyBindings.XmlSerializer.Deserialize(memoryStream); 769 (LocalKeyBindings) LocalKeyBindings.XmlSerializer.Deserialize(memoryStream);
760   770  
761 foreach (var binding in loadedBindings.Bindings) 771 foreach (var binding in loadedBindings.Bindings)
762 LocalKeyBindings.Bindings.Add(binding); 772 LocalKeyBindings.Bindings.Add(binding);
763   773  
764   774  
765 LocalCheckedListBoxBindingSource.ResetBindings(false); 775 LocalCheckedListBoxBindingSource.ResetBindings(false);
766 } 776 }
767 } 777 }
768 } 778 }
769 catch (Exception) 779 catch (Exception)
770 { 780 {
771 ActivityTextBox.AppendText( 781 ActivityTextBox.AppendText(
772 $"{Strings.Failed_loading_local_bindings}{Environment.NewLine}"); 782 $"{Strings.Failed_loading_local_bindings}{Environment.NewLine}");
773 } 783 }
774 } 784 }
775   785  
776 private async Task SaveRemoteMouseKeyBindings() 786 private async Task SaveRemoteMouseKeyBindings()
777 { 787 {
778 try 788 try
779 { 789 {
780 using (var memoryStream = new MemoryStream()) 790 using (var memoryStream = new MemoryStream())
781 { 791 {
782 RemoteKeyBindings.XmlSerializer.Serialize(memoryStream, RemoteKeyBindings); 792 RemoteKeyBindings.XmlSerializer.Serialize(memoryStream, RemoteKeyBindings);
783   793  
784 memoryStream.Position = 0L; 794 memoryStream.Position = 0L;
785   795  
786 using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Create)) 796 using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Create))
787 { 797 {
788 await memoryStream.CopyToAsync(fileStream); 798 await memoryStream.CopyToAsync(fileStream);
789 } 799 }
790 } 800 }
791 } 801 }
792 catch (Exception) 802 catch (Exception)
793 { 803 {
794 ActivityTextBox.AppendText( 804 ActivityTextBox.AppendText(
795 $"{Strings.Failed_saving_remote_bindings}{Environment.NewLine}"); 805 $"{Strings.Failed_saving_remote_bindings}{Environment.NewLine}");
796 } 806 }
797 } 807 }
798   808  
799 private async Task LoadRemoteMouseKeyBindings() 809 private async Task LoadRemoteMouseKeyBindings()
800 { 810 {
801 try 811 try
802 { 812 {
803 using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Open)) 813 using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Open))
804 { 814 {
805 using (var memoryStream = new MemoryStream()) 815 using (var memoryStream = new MemoryStream())
806 { 816 {
807 await fileStream.CopyToAsync(memoryStream); 817 await fileStream.CopyToAsync(memoryStream);
808   818  
809 memoryStream.Position = 0L; 819 memoryStream.Position = 0L;
810   820  
811 var loadedBindings = 821 var loadedBindings =
812 (RemoteKeyBindings) RemoteKeyBindings.XmlSerializer.Deserialize(memoryStream); 822 (RemoteKeyBindings) RemoteKeyBindings.XmlSerializer.Deserialize(memoryStream);
813   823  
814 foreach (var binding in loadedBindings.Bindings) 824 foreach (var binding in loadedBindings.Bindings)
815 RemoteKeyBindings.Bindings.Add(binding); 825 RemoteKeyBindings.Bindings.Add(binding);
816 } 826 }
817 } 827 }
818 } 828 }
819 catch (Exception) 829 catch (Exception)
820 { 830 {
821 ActivityTextBox.AppendText( 831 ActivityTextBox.AppendText(
822 $"{Strings.Failed_loading_remote_bindings}{Environment.NewLine}"); 832 $"{Strings.Failed_loading_remote_bindings}{Environment.NewLine}");
823 } 833 }
824 } 834 }
825   835  
826 #endregion 836 #endregion
827 } 837 }
828 } 838 }
829   839