Repository navigation
Expand file tree
/
Copy pathhosted_input.rs
More file actions
448 lines (423 loc) · 13.7 KB
/
Copy pathhosted_input.rs
File metadata and controls
448 lines (423 loc) · 13.7 KB
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
//! Input from a host's own event tap.
//!
//! Scrawl's app reads the mouse, keys and modifiers itself (a key tap, a global mouse monitor and
//! an 8 ms poller). A host that runs the engine with `SharedResources::none()`, such as Hex,
//! reports what its own tap sees instead, and these pure rules turn that into the
//! [`InputEvent`]s Scrawl's taps and poller would have sent: `⇧` alone draws ink (a `⇧`-click is
//! part of the drawing), and only `⌘`/`⌃` chords are logged, never ordinary typing.
//! Ink thresholds and clicks on the host's own windows are then decided by
//! [`SharedCaptureTelemetry::handle_input`](crate::capture::SharedCaptureTelemetry::handle_input)
//! with [`is_real_ink`](crate::capture::is_real_ink)
//! and [`on_own_window`](crate::capture::on_own_window).
use crate::capture::{InputEvent, pure_shift_state};
/// The modifiers held with an event.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Modifiers {
pub command: bool,
pub control: bool,
pub option: bool,
pub shift: bool,
}
impl Modifiers {
/// `⇧` without `⌘`, `⌃` or `⌥`.
pub const fn shift_only(self) -> bool {
self.shift && !self.others()
}
const fn others(self) -> bool {
self.command || self.control || self.option
}
}
/// One event from the host's tap, in global top-left points.
#[derive(Clone, Debug, PartialEq)]
pub enum HostInput {
Button {
down: bool,
right: bool,
x: f64,
y: f64,
modifiers: Modifiers,
},
/// The pointer moved, with or without a button held.
Moved {
x: f64,
y: f64,
modifiers: Modifiers,
},
Scroll {
x: f64,
y: f64,
dx: f64,
dy: f64,
},
/// The modifiers changed with the pointer at `x`, `y`.
ModifiersChanged {
x: f64,
y: f64,
modifiers: Modifiers,
},
/// A fresh key press, never an autorepeat: its name on the user's layout ([`key_name`]).
Key {
name: Option<String>,
modifiers: Modifiers,
},
}
/// Turns one host's events into engine events, remembering between them whether `⇧` is left
/// over from a chord (such as the capture shortcut `⌥⇧S`), which must not start ink.
#[derive(Debug, Default)]
pub struct HostedInput {
await_shift_release: bool,
}
impl HostedInput {
pub fn new() -> Self {
Self::default()
}
/// For a capture that starts with `⇧` already held, as when a chord such as `⌥⇧S` started
/// it: that `⇧` must come up once before it draws ink. A host passes the live session state,
/// so a capture started without `⇧` held inks from its first stroke.
pub fn with_shift_held(held: bool) -> Self {
Self {
await_shift_release: held,
}
}
/// The engine event for `input` at `t_ms` on the capture's clock, if it makes one.
pub fn translate(&mut self, t_ms: u64, input: HostInput) -> Option<InputEvent> {
match input {
HostInput::Button {
down,
right,
x,
y,
modifiers,
} => {
let shift_only = pure_shift_state(
modifiers.shift,
modifiers.others(),
&mut self.await_shift_release,
);
Some(if down {
InputEvent::ButtonDown {
t_ms,
x,
y,
right,
shift_only,
}
} else {
InputEvent::ButtonUp {
t_ms,
x,
y,
right,
shift_only,
}
})
}
HostInput::Moved { x, y, modifiers }
| HostInput::ModifiersChanged { x, y, modifiers } => Some(InputEvent::Move {
t_ms,
x,
y,
shift_only: pure_shift_state(
modifiers.shift,
modifiers.others(),
&mut self.await_shift_release,
),
}),
HostInput::Scroll { x, y, dx, dy } => {
(dx != 0.0 || dy != 0.0).then_some(InputEvent::Scroll { t_ms, x, y, dx, dy })
}
HostInput::Key { name, modifiers } => key_input(t_ms, name.as_deref(), modifiers),
}
}
}
/// `⌘`/`⌃` chords are logged by name (`⌃⌥⇧⌘` order, as Scrawl
/// writes them). Keys without `⌘` or `⌃` are typing and never leave the host.
pub fn key_input(t_ms: u64, name: Option<&str>, modifiers: Modifiers) -> Option<InputEvent> {
let Modifiers {
command,
control,
option,
shift,
} = modifiers;
if !command && !control {
return None;
}
let mut shortcut = String::new();
for (held, symbol) in [(control, '⌃'), (option, '⌥'), (shift, '⇧'), (command, '⌘')] {
if held {
shortcut.push(symbol);
}
}
shortcut.push_str(name?);
Some(InputEvent::KeyChord { t_ms, shortcut })
}
/// The name of a macOS virtual key as the user's layout labels it: keys that type no character
/// are named the same on every layout; the rest come from `layout_char` (what `⌘` + the key
/// types), else from the ANSI QWERTY positions.
pub fn key_name(keycode: u16, layout_char: impl Fn(u16) -> Option<char>) -> Option<String> {
let fixed = match keycode {
36 | 76 => "↩",
48 => "⇥",
49 => "Space",
51 => "⌫",
53 => "Esc",
117 => "⌦",
123 => "←",
124 => "→",
125 => "↓",
126 => "↑",
115 => "Home",
119 => "End",
116 => "PgUp",
121 => "PgDn",
122 => "F1",
120 => "F2",
99 => "F3",
118 => "F4",
96 => "F5",
97 => "F6",
98 => "F7",
100 => "F8",
101 => "F9",
109 => "F10",
103 => "F11",
111 => "F12",
_ => "",
};
if !fixed.is_empty() {
return Some(fixed.to_string());
}
layout_char(keycode)
.or_else(|| ansi_key_char(keycode))
.map(|c| c.to_uppercase().collect())
}
fn ansi_key_char(keycode: u16) -> Option<char> {
const ANSI: &str = "asdfhgzxcv\0bqweryt123465=97-80]ou[ip\0lj'k;\\,/nm.";
ANSI.chars()
.nth(usize::from(keycode))
.filter(|&c| c != '\0')
}
#[cfg(test)]
mod tests {
use super::*;
const SHIFT: Modifiers = Modifiers {
command: false,
control: false,
option: false,
shift: true,
};
const OPTION_SHIFT: Modifiers = Modifiers {
command: false,
control: false,
option: true,
shift: true,
};
const COMMAND: Modifiers = Modifiers {
command: true,
control: false,
option: false,
shift: false,
};
fn moved(input: &mut HostedInput, modifiers: Modifiers) -> bool {
match input.translate(
1,
HostInput::Moved {
x: 0.0,
y: 0.0,
modifiers,
},
) {
Some(InputEvent::Move { shift_only, .. }) => shift_only,
other => panic!("not a move: {other:?}"),
}
}
#[test]
fn shift_alone_inks_but_shift_left_from_a_chord_does_not() {
let mut input = HostedInput::new();
assert!(moved(&mut input, SHIFT));
assert!(!moved(&mut input, Modifiers::default()));
// ⌥⇧S, then ⌥ comes up first: the ⇧ still held is the chord's, not ink.
assert!(!moved(&mut input, OPTION_SHIFT));
assert!(!moved(&mut input, SHIFT));
assert!(!moved(&mut input, Modifiers::default()));
assert!(moved(&mut input, SHIFT));
}
#[test]
fn a_capture_started_by_its_shortcut_waits_for_shift_to_come_up() {
// The first event a host sees may already be ⌥ coming up with ⇧ still held.
let mut input = HostedInput::with_shift_held(true);
assert!(!moved(&mut input, SHIFT));
assert!(!moved(&mut input, Modifiers::default()));
assert!(moved(&mut input, SHIFT));
}
#[test]
fn a_capture_started_without_shift_held_inks_from_the_first_stroke() {
let mut input = HostedInput::with_shift_held(false);
assert!(moved(&mut input, SHIFT));
}
#[test]
fn a_click_with_the_chords_shift_still_held_is_no_pin() {
let mut input = HostedInput::with_shift_held(true);
let click = |input: &mut HostedInput, down, modifiers| match input.translate(
1,
HostInput::Button {
down,
right: false,
x: 0.0,
y: 0.0,
modifiers,
},
) {
Some(
InputEvent::ButtonDown { shift_only, .. } | InputEvent::ButtonUp { shift_only, .. },
) => shift_only,
other => panic!("{other:?}"),
};
assert!(!click(&mut input, true, SHIFT));
assert!(!click(&mut input, false, SHIFT));
// ⇧ comes up and goes down again: now it draws.
assert!(!click(&mut input, true, Modifiers::default()));
assert!(click(&mut input, true, SHIFT));
}
#[test]
fn modifier_changes_move_the_ink_like_pointer_moves() {
let mut input = HostedInput::new();
let event = input.translate(
7,
HostInput::ModifiersChanged {
x: 3.0,
y: 4.0,
modifiers: SHIFT,
},
);
assert_eq!(
event,
Some(InputEvent::Move {
t_ms: 7,
x: 3.0,
y: 4.0,
shift_only: true
})
);
}
#[test]
fn buttons_carry_whether_shift_was_held_alone() {
let mut input = HostedInput::new();
let down = input.translate(
5,
HostInput::Button {
down: true,
right: false,
x: 1.0,
y: 2.0,
modifiers: SHIFT,
},
);
assert_eq!(
down,
Some(InputEvent::ButtonDown {
t_ms: 5,
x: 1.0,
y: 2.0,
right: false,
shift_only: true
})
);
let up = input.translate(
6,
HostInput::Button {
down: false,
right: true,
x: 1.0,
y: 2.0,
modifiers: OPTION_SHIFT,
},
);
assert_eq!(
up,
Some(InputEvent::ButtonUp {
t_ms: 6,
x: 1.0,
y: 2.0,
right: true,
shift_only: false
})
);
}
#[test]
fn empty_scrolls_are_dropped() {
let mut input = HostedInput::new();
let scroll = |dx, dy| HostInput::Scroll {
x: 0.0,
y: 0.0,
dx,
dy,
};
assert_eq!(input.translate(1, scroll(0.0, 0.0)), None);
assert!(input.translate(1, scroll(0.0, -3.0)).is_some());
}
#[test]
fn only_modifier_chords_are_logged() {
assert_eq!(
key_input(9, Some("Z"), COMMAND),
Some(InputEvent::KeyChord {
t_ms: 9,
shortcut: "⌘Z".into()
})
);
assert_eq!(
key_input(9, Some("B"), COMMAND),
Some(InputEvent::KeyChord {
t_ms: 9,
shortcut: "⌘B".into()
})
);
let all = Modifiers {
command: true,
control: true,
option: true,
shift: true,
};
assert_eq!(
key_input(9, Some("Z"), all),
Some(InputEvent::KeyChord {
t_ms: 9,
shortcut: "⌃⌥⇧⌘Z".into()
})
);
// Typing, with or without ⇧ or ⌥, never leaves the host.
assert_eq!(key_input(9, Some("A"), Modifiers::default()), None);
assert_eq!(key_input(9, Some("A"), OPTION_SHIFT), None);
assert_eq!(key_input(9, None, COMMAND), None);
}
#[test]
fn shortcut_keys_are_named_by_the_layout_not_their_qwerty_position() {
// Dvorak puts B where QWERTY has N (45) and T where QWERTY has K (40).
let dvorak = |code: u16| match code {
45 => Some('b'),
40 => Some('t'),
13 => Some(','),
_ => None,
};
assert_eq!(key_name(45, dvorak).as_deref(), Some("B"));
assert_eq!(key_name(40, dvorak).as_deref(), Some("T"));
assert_eq!(key_name(13, dvorak).as_deref(), Some(","));
// Keys that type nothing keep fixed names whatever the layout says.
let odd = |_| Some('x');
assert_eq!(key_name(36, odd).as_deref(), Some("↩"));
assert_eq!(key_name(123, odd).as_deref(), Some("←"));
assert_eq!(key_name(122, odd).as_deref(), Some("F1"));
assert_eq!(key_name(53, odd).as_deref(), Some("Esc"));
}
#[test]
fn shortcut_keys_fall_back_to_ansi_positions_without_a_layout() {
let none = |_| None;
assert_eq!(key_name(45, none).as_deref(), Some("N"));
assert_eq!(key_name(17, none).as_deref(), Some("T"));
assert_eq!(key_name(0, none).as_deref(), Some("A"));
assert_eq!(key_name(47, none).as_deref(), Some("."));
assert_eq!(key_name(29, none).as_deref(), Some("0"));
assert_eq!(key_name(10, none), None);
assert_eq!(key_name(200, none), None);
}
}