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