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