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
use std::time::Duration;
use crate::error::{Error, Result};
use crate::link::Link;
use crate::protocol::opcode::{
OPT_IMPERFECT, OPT_MOVE_RIDE, Q_CAPS, Q_CATCH, Q_HEALTH, Q_LOCKS, Q_MOUSE_INFO, Q_RATE,
Q_STATS, Q_VERSION,
};
use crate::protocol::{Resp, parse_resp};
use crate::types::{
Action, Blanket, Button, Caps, CatchMask, CatchState, CountersSnapshot, Health,
ImperfectStatus, Input, Key, LedMode, LedTarget, LockDirection, LockTarget, Locks, MediaKey,
Motion, MouseInfo, Rate, RebootTarget, Stats, Version,
};
use super::Device;
use super::catch::EventStream;
use super::logs::LogStream;
/// An async view over a [`Device`] — the same `Link` core, with `async` queries.
#[derive(Clone, Debug)]
pub struct AsyncDevice {
link: Link,
}
impl From<Device> for AsyncDevice {
fn from(device: Device) -> Self {
AsyncDevice { link: device.link }
}
}
impl Device {
/// Convert this device into an [`AsyncDevice`] over the same core.
pub fn into_async(self) -> AsyncDevice {
AsyncDevice::from(self)
}
}
impl AsyncDevice {
fn dev(&self) -> Device {
Device {
link: self.link.clone(),
}
}
/// Consume back into the sync [`Device`].
pub fn into_inner(self) -> Device {
Device { link: self.link }
}
/// `MOVE` — relative cursor movement. Instant; see [`Device::move_rel`].
pub fn move_rel(&self, dx: i16, dy: i16) -> Result<()> {
self.dev().move_rel(dx, dy)
}
/// `WHEEL` — vertical scroll. Instant; see [`Device::wheel`].
pub fn wheel(&self, delta: i16) -> Result<()> {
self.dev().wheel(delta)
}
/// `MOVE` — field-generic relative axis (cursor or wheel). Instant; see [`Device::move_axis`].
pub fn move_axis(&self, motion: Motion) -> Result<()> {
self.dev().move_axis(motion)
}
/// `INJECT` — field-generic momentary override (button, key, or media). Instant; see [`Device::inject`].
pub fn inject(&self, input: impl Into<Input>, action: Action) -> Result<()> {
self.dev().inject(input, action)
}
/// `BUTTON` — set an injection override. Instant; see [`Device::button`].
pub fn button(&self, button: Button, action: Action) -> Result<()> {
self.dev().button(button, action)
}
/// Press (hold) a button. Instant; see [`Device::press`].
pub fn press(&self, button: Button) -> Result<()> {
self.dev().press(button)
}
/// Soft-release a button. Instant; see [`Device::soft_release`].
pub fn soft_release(&self, button: Button) -> Result<()> {
self.dev().soft_release(button)
}
/// Force-release a button. Instant; see [`Device::force_release`].
pub fn force_release(&self, button: Button) -> Result<()> {
self.dev().force_release(button)
}
/// `KEY` — set a keyboard key/modifier override. Instant; see [`Device::key`].
pub fn key(&self, key: Key, action: Action) -> Result<()> {
self.dev().key(key, action)
}
/// Press (hold) a key. Instant; see [`Device::key_down`].
pub fn key_down(&self, key: Key) -> Result<()> {
self.dev().key_down(key)
}
/// Soft-release a key. Instant; see [`Device::key_up`].
pub fn key_up(&self, key: Key) -> Result<()> {
self.dev().key_up(key)
}
/// Force-release a key. Instant; see [`Device::key_force_release`].
pub fn key_force_release(&self, key: Key) -> Result<()> {
self.dev().key_force_release(key)
}
/// `CONSUMER` — set a media-key override. Instant; see [`Device::media`].
pub fn media(&self, key: MediaKey, action: Action) -> Result<()> {
self.dev().media(key, action)
}
/// Press (hold) a media key. Instant; see [`Device::media_down`].
pub fn media_down(&self, key: MediaKey) -> Result<()> {
self.dev().media_down(key)
}
/// Soft-release a media key. Instant; see [`Device::media_up`].
pub fn media_up(&self, key: MediaKey) -> Result<()> {
self.dev().media_up(key)
}
/// Force-release a media key. Instant; see [`Device::media_force_release`].
pub fn media_force_release(&self, key: MediaKey) -> Result<()> {
self.dev().media_force_release(key)
}
/// `RESET` — return to passthrough. Instant; see [`Device::reset`].
pub fn reset(&self) -> Result<()> {
self.dev().reset()
}
/// Reboot a chip (run or ROM download per the target). Instant; see [`Device::reboot`].
pub fn reboot(&self, target: RebootTarget) -> Result<()> {
self.dev().reboot(target)
}
/// Re-assert every currently held override. Instant; see [`Device::reapply`].
pub fn reapply(&self) -> Result<()> {
self.dev().reapply()
}
/// Best-effort reconnect over the shared core; blocks the calling thread (serial rescan + reopen). See [`Device::reconnect`].
pub fn reconnect(&self) -> Result<()> {
self.dev().reconnect()
}
/// A snapshot of the always-on counters. See [`Device::counters`].
pub fn counters(&self) -> CountersSnapshot {
self.dev().counters()
}
/// A [`LogStream`] over the device `LOG` stream; it offers `recv_async`. See [`Device::logs`].
pub fn logs(&self) -> LogStream {
self.dev().logs()
}
/// `LED` — override a status LED. Instant; see [`Device::led`].
pub fn led(&self, target: LedTarget, mode: LedMode, level: u8) -> Result<()> {
self.dev().led(target, mode, level)
}
/// `LOCK` — lock an axis or button edge. Instant; see [`Device::lock`].
pub fn lock(&self, target: LockTarget, direction: LockDirection) -> Result<()> {
self.dev().lock(target, direction)
}
/// `LOCK` — release a locked axis or button edge. Instant; see [`Device::unlock`].
pub fn unlock(&self, target: LockTarget, direction: LockDirection) -> Result<()> {
self.dev().unlock(target, direction)
}
/// `LOCK` — block a keyboard key or modifier. Instant; see [`Device::lock_key`].
pub fn lock_key(&self, key: Key, direction: LockDirection) -> Result<()> {
self.dev().lock_key(key, direction)
}
/// `LOCK` — release a locked keyboard key or modifier. Instant; see [`Device::unlock_key`].
pub fn unlock_key(&self, key: Key, direction: LockDirection) -> Result<()> {
self.dev().unlock_key(key, direction)
}
/// `LOCK` — block a media usage. Instant; see [`Device::lock_media`].
pub fn lock_media(&self, key: MediaKey) -> Result<()> {
self.dev().lock_media(key)
}
/// `LOCK` — release a locked media usage. Instant; see [`Device::unlock_media`].
pub fn unlock_media(&self, key: MediaKey) -> Result<()> {
self.dev().unlock_media(key)
}
/// `LOCK` — blanket-block a whole input class. Instant; see [`Device::lock_all`].
pub fn lock_all(&self, what: Blanket) -> Result<()> {
self.dev().lock_all(what)
}
/// `LOCK` — release a blanket whole-class lock. Instant; see [`Device::unlock_all`].
pub fn unlock_all(&self, what: Blanket) -> Result<()> {
self.dev().unlock_all(what)
}
/// Subscribe to the physical-input event stream. Instant; the returned [`EventStream`] offers
/// `recv_async`. See [`Device::catch_events`].
pub fn catch_events(&self, mask: CatchMask) -> Result<EventStream> {
self.dev().catch_events(mask)
}
/// `OPTION(IMPERFECT)` — opt into cloning an over-capacity device. Instant; see [`Device::allow_imperfect_clones`].
pub fn allow_imperfect_clones(&self, allow: bool) -> Result<()> {
self.dev().allow_imperfect_clones(allow)
}
/// `OPTION(MOVE_RIDE)` — movement riding. Instant; see [`Device::set_movement_riding`].
pub fn set_movement_riding(&self, window: Option<Duration>) -> Result<()> {
self.dev().set_movement_riding(window)
}
/// Query the box version, awaiting the correlated `RESP` with the default timeout.
pub async fn query_version(&self) -> Result<Version> {
let payload = self
.link
.query_async(Q_VERSION, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::Version(v)) => Ok(v),
_ => Err(Error::NoReply),
}
}
/// Query the box health flags, awaiting the correlated `RESP` with the default timeout.
pub async fn query_health(&self) -> Result<Health> {
let payload = self
.link
.query_async(Q_HEALTH, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::Health(h)) => Ok(h),
_ => Err(Error::NoReply),
}
}
/// Query the cloned mouse's USB identity (§4.3), awaiting the correlated `RESP`.
pub async fn query_mouse_info(&self) -> Result<MouseInfo> {
let payload = self
.link
.query_async(Q_MOUSE_INFO, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::MouseInfo(m)) => Ok(m),
_ => Err(Error::NoReply),
}
}
/// Query the whole cloned device's capabilities in one request (§4.4), awaiting the correlated `RESP`.
pub async fn caps(&self) -> Result<Caps> {
let payload = self
.link
.query_async(Q_CAPS, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::Caps(c)) => Ok(c),
_ => Err(Error::NoReply),
}
}
/// Query the live native report rate (§4.5), awaiting the correlated `RESP`.
pub async fn query_rate(&self) -> Result<Rate> {
let payload = self
.link
.query_async(Q_RATE, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::Rate(r)) => Ok(r),
_ => Err(Error::NoReply),
}
}
/// Query the box's delivery/telemetry counters (§4.6), awaiting the correlated `RESP`.
pub async fn query_stats(&self) -> Result<Stats> {
let payload = self
.link
.query_async(Q_STATS, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::Stats(s)) => Ok(s),
_ => Err(Error::NoReply),
}
}
/// Query the active lock bitmask (§4.8), awaiting the correlated `RESP`.
pub async fn query_locks(&self) -> Result<Locks> {
let payload = self
.link
.query_async(Q_LOCKS, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::Locks(l)) => Ok(l),
_ => Err(Error::NoReply),
}
}
/// Query the catch subscription mask + box-side dropped count (§4.9), awaiting the correlated `RESP`.
pub async fn query_catch(&self) -> Result<CatchState> {
let payload = self
.link
.query_async(Q_CATCH, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::Catch(c)) => Ok(c),
_ => Err(Error::NoReply),
}
}
/// Query the imperfect-clone status (§4.14), awaiting the correlated `RESP`.
pub async fn query_imperfect(&self) -> Result<ImperfectStatus> {
let payload = self
.link
.query_option_async(OPT_IMPERFECT, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::Imperfect(i)) => Ok(i),
_ => Err(Error::NoReply),
}
}
/// Query the movement-riding window (§4.14), awaiting the correlated `RESP`; `None` = off.
pub async fn query_movement_riding(&self) -> Result<Option<Duration>> {
let payload = self
.link
.query_option_async(OPT_MOVE_RIDE, self.link.query_timeout_default())
.await?;
match parse_resp(&payload) {
Some(Resp::MovementRiding(w)) => Ok(w),
_ => Err(Error::NoReply),
}
}
/// Open a device at `path` and wrap it as an [`AsyncDevice`].
pub fn open(path: impl AsRef<std::path::Path>) -> Result<AsyncDevice> {
Ok(Device::open(path)?.into_async())
}
/// Discover the first medius box, open it, and wrap as an [`AsyncDevice`]; blocks (scan + handshake). See [`Device::find`].
pub fn find() -> Result<AsyncDevice> {
Ok(Device::find()?.into_async())
}
}