-
-
Notifications
You must be signed in to change notification settings - Fork 505
/
InputService.subscriptions.cs
537 lines (445 loc) · 25.3 KB
/
InputService.subscriptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright (c) 2022 OPTIKEY LTD (UK company number 11854839) - All Rights Reserved
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using JuliusSweetland.OptiKey.Enums;
using JuliusSweetland.OptiKey.Extensions;
using JuliusSweetland.OptiKey.Models;
using JuliusSweetland.OptiKey.Observables.TriggerSources;
using JuliusSweetland.OptiKey.Properties;
namespace JuliusSweetland.OptiKey.Services
{
public partial class InputService
{
#region Fields
private IDisposable pointsPerSecondSubscription;
private IDisposable currentPositionSubscription;
private IDisposable eyeGestureTriggerSubscription;
private IDisposable multiKeySelectionSubscription;
private CancellationTokenSource mapToDictionaryMatchesCancellationTokenSource;
private List<IDisposable> activeSelectionTriggerSubscriptions;
private List<IDisposable> activeSelectionProgressSubscriptions;
private TriggerSignal? startMultiKeySelectionTriggerSignal;
private TriggerSignal? stopMultiKeySelectionTriggerSignal;
#endregion
#region Create Points Per Second Subscription
private void CreatePointsPerSecondSubscription()
{
Log.Debug("Creating subscription to PointSource for points per second.");
pointsPerSecondSubscription = pointSource.Sequence
.Where(tp => tp.Value != null) //Filter out stale indicators - we only want 'live'/useful points in our count
.Buffer(new TimeSpan(0, 0, 0, 1))
.Select(points => points.Count)
.ObserveOnDispatcher() //Subscribe on UI thread
.Subscribe(PublishPointsPerSecond);
}
#endregion
#region Create Current Position Subscription
private void CreateCurrentPositionSubscription()
{
Log.Debug("Creating subscription to PointSource for current position.");
currentPositionSubscription = pointSource.Sequence
.Where(tp => tp.Value != null)
.Select(tp => new Tuple<Point, KeyValue>(
tp.Value.Point, tp.Value.KeyValue))
.ObserveOnDispatcher() //Subscribe on UI thread
.Subscribe(PublishCurrentPosition);
}
#endregion
#region Create Selection Progress Subscription
private void DisposeAllAndClear(List<IDisposable> list)
{
foreach (IDisposable disp in list)
{
disp.Dispose();
}
list.RemoveAll(s => true);
}
private void CreateSelectionProgressSubscription(SelectionModes mode)
{
Log.DebugFormat("Creating subscription to {0} SelectionTriggerSource for progress info.", SelectionMode);
if (eyeGestureTriggerSource != null && eyeGestureTriggerSubscription == null)
{
eyeGestureTriggerSubscription = eyeGestureTriggerSource.Sequence
.ObserveOnDispatcher()
.Subscribe(ProcessGestureTrigger);
}
DisposeAllAndClear(activeSelectionProgressSubscriptions);
activeSelectionProgressSubscriptions.Add(keySelectionTriggerSource.Sequence
.Where(ts => ts.Progress != null)
.DistinctUntilChanged()
.ObserveOnDispatcher()
.Subscribe(ts =>
{
PublishSelectionProgress(new Tuple<TriggerTypes, PointAndKeyValue, double>(
TriggerTypes.Key, ts.PointAndKeyValue, ts.Progress.Value));
}));
if (pointSelectionTriggerSource is IFixationTriggerSource)
{
IFixationTriggerSource fixationTriggerSource = pointSelectionTriggerSource as IFixationTriggerSource;
fixationTriggerSource.AllowPointsOverKeys = selectionMode == SelectionModes.SinglePoint;
}
// Point selection active if any mouse actions underway
if (mode == SelectionModes.SinglePoint || mode == SelectionModes.ContinuousPoints)
{
activeSelectionProgressSubscriptions.Add(pointSelectionTriggerSource.Sequence
.Where(ts => ts.Progress != null)
.DistinctUntilChanged()
.ObserveOnDispatcher()
.Subscribe(ts =>
{
PublishSelectionProgress(new Tuple<TriggerTypes, PointAndKeyValue, double>(
TriggerTypes.Point, ts.PointAndKeyValue, ts.Progress.Value));
}));
}
}
#endregion
#region Create Selection Subscriptions
private void CreateSelectionSubscriptions(SelectionModes mode)
{
Log.DebugFormat("Creating subscription to {0} SelectionTriggerSource for selections & results.", SelectionMode);
if (eyeGestureTriggerSource != null && eyeGestureTriggerSubscription == null)
{
eyeGestureTriggerSubscription = eyeGestureTriggerSource.Sequence
.ObserveOnDispatcher()
.Subscribe(ProcessGestureTrigger);
}
DisposeAllAndClear(activeSelectionTriggerSubscriptions);
// Key selection is always enabled, but in the case of single point selection we will only allow
// the current "mouse action" key to be selected, to allow for locking.
activeSelectionTriggerSubscriptions.Add(keySelectionTriggerSource.Sequence
.Where(ts => ts.Signal != null)
.ObserveOnDispatcher()
.Subscribe(ProcessKeySelectionTrigger));
// Point selection active if any mouse actions underway
if (mode == SelectionModes.SinglePoint || mode == SelectionModes.ContinuousPoints)
activeSelectionTriggerSubscriptions.Add(pointSelectionTriggerSource.Sequence
.Where(ts => ts.Signal != null)
.ObserveOnDispatcher()
.Subscribe(ProcessPointSelectionTrigger));
}
private async void ProcessKeySelectionTrigger(TriggerSignal triggerSignal)
{
if (triggerSignal.Signal >= 1
&& !CapturingMultiKeySelection)
{
//We are not currently capturing a multikey selection and have received a high (start) trigger signal
if (triggerSignal.PointAndKeyValue != null)
{
Log.Debug("Key selection trigger signal (with relevant PointAndKeyValue) detected.");
if (triggerSignal.PointAndKeyValue.KeyValue != null
&& (keyStateService.KeyEnabledStates == null || keyStateService.KeyEnabledStates[triggerSignal.PointAndKeyValue.KeyValue]))
{
Log.Debug("Selection mode is KEY and the key on which the trigger occurred is enabled.");
if (MultiKeySelectionSupported
&& keyStateService.KeyEnabledStates[KeyValues.MultiKeySelectionIsOnKey] //It is possible for MultiKeySelectionIsOnKey to be down/locked down even though it is disabled - check for this
&& keyStateService.KeyDownStates[KeyValues.MultiKeySelectionIsOnKey].Value.IsDownOrLockedDown()
&& triggerSignal.PointAndKeyValue.KeyValue != null
&& KeyValues.MultiKeySelectionKeys.Contains(triggerSignal.PointAndKeyValue.KeyValue)
&& !KeyValues.CombiningKeys.Any(key => keyStateService.KeyDownStates[key].Value.IsDownOrLockedDown())) //Do not start if any combining ("dead") keys are down
{
Log.Debug("Multi-key selection is currently enabled and the key on which the trigger occurred is a letter. Publishing the selection and beginning a new multi-key selection capture.");
//Multi-key selection is allowed and the trigger occurred on a letter - start a capture
startMultiKeySelectionTriggerSignal = triggerSignal;
stopMultiKeySelectionTriggerSignal = null;
CapturingMultiKeySelection = true;
PublishSelection(TriggerTypes.Key, triggerSignal.PointAndKeyValue);
//Set the key's IsHighlighted property in order to show the green border
keyStateService.KeyHighlightStates[triggerSignal.PointAndKeyValue.KeyValue].Value = true;
multiKeySelectionSubscription =
CreateMultiKeySelectionSubscription()
.ObserveOnDispatcher()
.Subscribe(
async pointsAndKeyValues => await ProcessMultiKeySelectionResult(pointsAndKeyValues, triggerSignal),
(exception =>
{
PublishError(this, exception);
stopMultiKeySelectionTriggerSignal = null;
CapturingMultiKeySelection = false;
}),
() =>
{
Log.Debug("Multi-key selection capture has completed.");
stopMultiKeySelectionTriggerSignal = null;
CapturingMultiKeySelection = false;
//Set the key's IsHighlighted to false in order to remove the green border
keyStateService.KeyHighlightStates[triggerSignal.PointAndKeyValue.KeyValue].Value = false;
});
}
else
{
PublishSelection(TriggerTypes.Key, triggerSignal.PointAndKeyValue);
await Task.Delay(20); //Add a short delay to give time for the selection animation
PublishSelectionResult(new Tuple<TriggerTypes, List<Point>, KeyValue, List<string>>(
TriggerTypes.Key,
new List<Point> { triggerSignal.PointAndKeyValue.Point },
triggerSignal.PointAndKeyValue.KeyValue,
null));
}
}
else
{
Log.Debug("Selection mode is KEY, but the trigger occurred away from a key or over a disabled key.");
}
}
else
{
Log.Error("TriggerSignal.Signal==1, but TriggerSignal.PointAndKeyValue is null. "
+ "Discarding trigger as point source is down, or producing stale points. "
+ "Publishing error instead.");
if (!Settings.Default.SuppressTriggerWithoutPositionError)
{
PublishError(this, new ApplicationException(Resources.TRIGGER_WITHOUT_POSITION_ERROR));
}
}
}
else if (CapturingMultiKeySelection)
{
//We are capturing and may have received the stop capturing signal
if ((triggerSignal.Signal >= 1 && Settings.Default.MultiKeySelectionTriggerStopSignal == TriggerStopSignals.NextHigh)
|| (triggerSignal.Signal <= -1 && Settings.Default.MultiKeySelectionTriggerStopSignal == TriggerStopSignals.NextLow))
{
//If we are using a fixation trigger source then the stop signal must occur on a letter
if (!(activeSelectionTriggerSubscriptions.Any((s) => s is KeyFixationSource)) // equivalent to "if the key selection source is a fixation one"
|| (triggerSignal.PointAndKeyValue != null && triggerSignal.PointAndKeyValue.StringIsLetter))
{
Log.Debug("Trigger signal to stop the current multi-key selection capture detected.");
stopMultiKeySelectionTriggerSignal = triggerSignal;
}
}
}
}
private void ProcessPointSelectionTrigger(TriggerSignal triggerSignal)
{
//FIXME: Is it okay to permit a point selection when a multikey selection is underway? We need to test if this results
// in undefined behaviour or poor UX
// In vanilla OK keyboards it's unlikely to happen as keys get disabled while multikey selection in progress
if (triggerSignal.Signal >= 1)
{
if (triggerSignal.PointAndKeyValue != null)
{
Log.Debug("Point selection trigger signal (with relevant PointAndKeyValue) detected.");
PublishSelection(TriggerTypes.Point, triggerSignal.PointAndKeyValue);
PublishSelectionResult(new Tuple<TriggerTypes, List<Point>, KeyValue, List<string>>(
TriggerTypes.Point,
new List<Point> { triggerSignal.PointAndKeyValue.Point }, null, null));
}
else
{
Log.Error("TriggerSignal.Signal==1, but TriggerSignal.PointAndKeyValue is null. "
+ "Discarding trigger as point source is down, or producing stale points. "
+ "Publishing error instead.");
if (!Settings.Default.SuppressTriggerWithoutPositionError)
{
PublishError(this, new ApplicationException(Resources.TRIGGER_WITHOUT_POSITION_ERROR));
}
}
}
}
private async void ProcessGestureTrigger(TriggerSignal triggerSignal)
{
// TODO: Consider what states we might want gestures to *not* trigger in
// e.g. should it be allowed in "Point selection" mode? I don't think it would have been processed previously,
// but if we are allowing continuous / repeat mouse actions then it is probably important gestures can come through too.
// Do we want to allow a gesture to trigger a disabled key?
if (CapturingMultiKeySelection)
return;
if (SelectionMode != SelectionModes.Keys)
{
Log.InfoFormat("Ignoring gesture during point selection");
return;
}
if (triggerSignal.Signal >= 1)
{
if (triggerSignal.PointAndKeyValue != null)
{
Log.Debug("Gesture trigger signal (with relevant PointAndKeyValue) detected.");
{
if (triggerSignal.PointAndKeyValue.KeyValue != null
&& (keyStateService.KeyEnabledStates == null || keyStateService.KeyEnabledStates[triggerSignal.PointAndKeyValue.KeyValue]))
{
PublishSelection(TriggerTypes.Gesture, triggerSignal.PointAndKeyValue);
await Task.Delay(20); //Add a short delay to give time for the selection animation
PublishSelectionResult(new Tuple<TriggerTypes, List<Point>, KeyValue, List<string>>(
TriggerTypes.Gesture,
new List<Point> { triggerSignal.PointAndKeyValue.Point },
triggerSignal.PointAndKeyValue.KeyValue,
null));
}
else
{
Log.Debug("Selection mode is KEY, but the trigger occurred away from a key or over a disabled key.");
}
}
}
else
{
Log.Error("TriggerSignal.Signal==1, but TriggerSignal.PointAndKeyValue is null. "
+ "Discarding trigger as point source is down, or producing stale points. "
+ "Publishing error instead.");
if (!Settings.Default.SuppressTriggerWithoutPositionError)
{
PublishError(this, new ApplicationException(Resources.TRIGGER_WITHOUT_POSITION_ERROR));
}
}
}
}
private IObservable<IList<Timestamped<PointAndKeyValue>>> CreateMultiKeySelectionSubscription()
{
return Observable.Create<IList<Timestamped<PointAndKeyValue>>>(observer =>
{
bool disposed = false;
Action disposeAllSubscriptions = null;
var intervalSubscription =
Observable.Interval(Settings.Default.MultiKeySelectionMaxDuration)
.ObserveOnDispatcher()
.Where(_ => disposed == false)
.Subscribe(i =>
{
keyStateService.ClearKeyHighlightStates();
observer.OnError(new TimeoutException("Multi-key capture has exceeded the maximum duration"));
});
var pointAndKeyValueSubscription = pointSource.Sequence
.Where(tp => tp.Value != null) //Filter out stale indicators
.Select(tp => new Timestamped<PointAndKeyValue>(tp.Value, tp.Timestamp))
.TakeWhile(tp => stopMultiKeySelectionTriggerSignal == null)
.ToList()
.Subscribe(points =>
{
observer.OnNext(points);
observer.OnCompleted();
});
disposeAllSubscriptions = () =>
{
disposed = true;
if (intervalSubscription != null)
{
intervalSubscription.Dispose();
intervalSubscription = null;
}
if (pointAndKeyValueSubscription != null)
{
pointAndKeyValueSubscription.Dispose();
pointAndKeyValueSubscription = null;
}
};
return disposeAllSubscriptions;
});
}
private async Task ProcessMultiKeySelectionResult(
IList<Timestamped<PointAndKeyValue>> pointsAndKeyValues,
TriggerSignal startSelectionTriggerSignal)
{
Log.DebugFormat("Multi-key selection captured a set of '{0}' PointAndKeyValues.", pointsAndKeyValues.Count);
RequestSuspend(); //Pause everything (i.e. processing new points) while we perform the (CPU bound) word matching
try
{
if (pointsAndKeyValues.Any())
{
var timeSpan = pointsAndKeyValues.Last().Timestamp.Subtract(pointsAndKeyValues.First().Timestamp);
var sequenceThreshold = (int)Math.Round(
((double)pointsAndKeyValues.Count / (double)timeSpan.TotalMilliseconds)
* Settings.Default.MultiKeySelectionFixationMinDwellTime.TotalMilliseconds);
Log.DebugFormat(
"Multi-key selection capture lasted {0}ms. Minimum dwell time is {1}ms, or {2} points.",
timeSpan.TotalMilliseconds,
Settings.Default.MultiKeySelectionFixationMinDwellTime.TotalMilliseconds,
sequenceThreshold);
//Always assume the start trigger is reliable if it occurs on a letter
string reliableFirstLetter =
startMultiKeySelectionTriggerSignal != null
&& startMultiKeySelectionTriggerSignal.Value.PointAndKeyValue != null
&& startMultiKeySelectionTriggerSignal.Value.PointAndKeyValue.StringIsLetter
? startMultiKeySelectionTriggerSignal.Value.PointAndKeyValue.String
: null;
Log.DebugFormat(
"First letter ('{0}') of multi-key selection capture {1} reliable.",
reliableFirstLetter,
reliableFirstLetter != null ? "IS" : "IS NOT");
//If we are using a fixation trigger and the stop trigger has occurred on a letter then it is reliable - use it
string reliableLastLetter = keySelectionTriggerSource is KeyFixationSource
&& stopMultiKeySelectionTriggerSignal != null
&& stopMultiKeySelectionTriggerSignal.Value.PointAndKeyValue != null
&& stopMultiKeySelectionTriggerSignal.Value.PointAndKeyValue.StringIsLetter
? stopMultiKeySelectionTriggerSignal.Value.PointAndKeyValue.String
: null;
Log.DebugFormat(
"Last letter ('{0}') of multi-key selection capture {1} reliable.",
reliableLastLetter,
reliableLastLetter != null ? "IS" : "IS NOT");
if (reliableLastLetter != null)
{
Log.Debug("Publishing selection event on last letter of multi-key selection capture.");
PublishSelection(TriggerTypes.Key, stopMultiKeySelectionTriggerSignal.Value.PointAndKeyValue);
}
//Why am I wrapping this call in a Task.Run? Internally the MapCaptureToEntries method uses PLINQ which also blocks the UI thread - this frees it up.
//This cannot be done inside the MapCaptureToEntries method as the method takes a ref param, which cannot be used inside an anonymous delegate or lambda.
//The method cannot be made awaitable as async/await also does not support ref params.
Tuple<List<Point>, KeyValue, List<string>> result = null;
await Task.Run(() =>
{
result = dictionaryService.MapCaptureToEntries(
pointsAndKeyValues.ToList(), sequenceThreshold,
reliableFirstLetter, reliableLastLetter,
ref mapToDictionaryMatchesCancellationTokenSource,
exception => PublishError(this, exception));
});
if (result != null)
{
if (result.Item2 == null &&
(result.Item3 == null || !result.Item3.Any()))
{
//Nothing useful in the result - play error message. Publish anyway as the points can be rendered in debugging mode.
audioService.PlaySound(Settings.Default.ErrorSoundFile, Settings.Default.ErrorSoundVolume);
}
PublishSelectionResult(new Tuple<TriggerTypes, List<Point>, KeyValue, List<string>>(
TriggerTypes.Key, result.Item1, result.Item2, result.Item3));
}
}
}
finally
{
RequestResume();
}
}
#endregion
#region Dispose Selection Subscriptions
private void DisposeSelectionSubscriptions()
{
Log.Debug("Disposing of subscriptions to SelectionTriggerSource for selections & results.");
if (eyeGestureTriggerSubscription != null)
{
eyeGestureTriggerSubscription.Dispose();
eyeGestureTriggerSubscription = null;
}
DisposeAllAndClear(activeSelectionTriggerSubscriptions);
DisposeAllAndClear(activeSelectionProgressSubscriptions);
if (multiKeySelectionSubscription != null)
{
multiKeySelectionSubscription.Dispose();
multiKeySelectionSubscription = null;
}
}
#endregion
#region Dispose Multi Key Selection
private void DisposeMultiKeySelection()
{
Log.Debug("DisposeMultiKeySelection called.");
if (multiKeySelectionSubscription != null)
{
multiKeySelectionSubscription.Dispose();
}
if (mapToDictionaryMatchesCancellationTokenSource != null)
{
mapToDictionaryMatchesCancellationTokenSource.Cancel();
}
CapturingMultiKeySelection = false;
}
#endregion
}
}