-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathmod.rs
557 lines (491 loc) · 17.8 KB
/
mod.rs
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Have some way to set if socket can be used for reciver, sender, or both?
// - restrict what devices it can use?
// For emulation:
// - create a seat for each seat
// - create a device for pointer, touch, keyboard, if the seat has that
// * send keymap for keyboard
// - direct emulated input on these to the relevant handles
// For reciever context:
// - do we need to pass the application any requests from the client?
// re-export listener source?
use calloop::{EventSource, PostAction, Readiness, Token, TokenFactory};
use once_cell::sync::Lazy;
use reis::{
calloop::EisRequestSourceEvent,
eis::{self, device::DeviceType},
request::{self, Connection, DeviceCapability, EisRequest},
};
use rustix::fd::AsFd;
use std::{collections::HashMap, ffi::CStr, io, path::PathBuf};
use xkbcommon::xkb;
use crate::{
backend::input::{self, InputBackend, InputEvent},
input::keyboard::XkbConfig,
utils::SealedFile,
};
static SERVER_INTERFACES: Lazy<HashMap<&'static str, u32>> = Lazy::new(|| {
let mut m = HashMap::new();
m.insert("ei_callback", 1);
m.insert("ei_connection", 1);
m.insert("ei_seat", 1);
m.insert("ei_device", 1);
m.insert("ei_pingpong", 1);
m.insert("ei_keyboard", 1);
m.insert("ei_pointer", 1);
m.insert("ei_pointer_absolute", 1);
m.insert("ei_button", 1);
m.insert("ei_scroll", 1);
m.insert("ei_touchscreen", 1);
m
});
struct SenderState {
name: Option<String>,
connection: eis::Connection,
seat: eis::Seat,
last_serial: u32,
}
impl SenderState {
fn new(name: Option<String>, connection: eis::Connection) -> Self {
// TODO create seat, etc.
// check protocol versions
let seat = connection.seat(1);
seat.name("default");
seat.capability(0x2, "ei_pointer");
seat.capability(0x4, "ei_pointer_absolute");
seat.capability(0x8, "ei_button");
seat.capability(0x10, "ei_scroll");
seat.capability(0x20, "ei_keyboard");
seat.capability(0x40, "ei_touchscreen");
seat.done();
Self {
name,
connection,
seat,
last_serial: 0,
}
}
}
#[derive(Debug)]
pub struct EiInput {
source: reis::calloop::EisRequestSource,
seat: Option<reis::request::Seat>,
}
impl EiInput {
pub fn new(context: eis::Context) -> Self {
Self {
source: reis::calloop::EisRequestSource::new(context, 0),
seat: None,
}
}
}
fn disconnected(
connection: &Connection,
reason: eis::connection::DisconnectReason,
explaination: &str,
) -> io::Result<calloop::PostAction> {
connection.disconnected(reason, explaination);
connection.flush();
Ok(calloop::PostAction::Remove)
}
impl InputBackend for EiInput {
type Device = request::Device;
type KeyboardKeyEvent = request::KeyboardKey;
type PointerAxisEvent = ScrollEvent;
type PointerButtonEvent = request::Button;
type PointerMotionEvent = request::PointerMotion;
type PointerMotionAbsoluteEvent = request::PointerMotionAbsolute;
type GestureSwipeBeginEvent = input::UnusedEvent;
type GestureSwipeUpdateEvent = input::UnusedEvent;
type GestureSwipeEndEvent = input::UnusedEvent;
type GesturePinchBeginEvent = input::UnusedEvent;
type GesturePinchUpdateEvent = input::UnusedEvent;
type GesturePinchEndEvent = input::UnusedEvent;
type GestureHoldBeginEvent = input::UnusedEvent;
type GestureHoldEndEvent = input::UnusedEvent;
type TouchDownEvent = request::TouchDown;
type TouchUpEvent = request::TouchUp;
type TouchMotionEvent = request::TouchMotion;
type TouchCancelEvent = input::UnusedEvent; // XXX?
type TouchFrameEvent = input::UnusedEvent; // XXX
type TabletToolAxisEvent = input::UnusedEvent;
type TabletToolProximityEvent = input::UnusedEvent;
type TabletToolTipEvent = input::UnusedEvent;
type TabletToolButtonEvent = input::UnusedEvent;
type SwitchToggleEvent = input::UnusedEvent;
type SpecialEvent = input::UnusedEvent;
}
impl input::Device for request::Device {
fn id(&self) -> String {
self.name().unwrap_or("").to_string()
}
fn name(&self) -> String {
self.name().unwrap_or("").to_string()
}
fn has_capability(&self, capability: input::DeviceCapability) -> bool {
if let Ok(capability) = DeviceCapability::try_from(capability) {
self.has_capability(capability)
} else {
false
}
}
fn usb_id(&self) -> Option<(u32, u32)> {
None
}
fn syspath(&self) -> Option<PathBuf> {
None
}
}
impl<T: request::DeviceEvent + request::EventTime> input::Event<EiInput> for T {
fn time(&self) -> u64 {
request::EventTime::time(self)
}
fn device(&self) -> request::Device {
request::DeviceEvent::device(self).clone()
}
}
impl input::KeyboardKeyEvent<EiInput> for request::KeyboardKey {
fn key_code(&self) -> input::Keycode {
input::Keycode::from(self.key + 8)
}
fn state(&self) -> input::KeyState {
match self.state {
eis::keyboard::KeyState::Released => input::KeyState::Released,
eis::keyboard::KeyState::Press => input::KeyState::Pressed,
}
}
fn count(&self) -> u32 {
1
}
}
pub enum ScrollEvent {
Delta(request::ScrollDelta),
Cancel(request::ScrollCancel),
Discrete(request::ScrollDiscrete),
Stop(request::ScrollStop),
}
impl input::Event<EiInput> for ScrollEvent {
fn time(&self) -> u64 {
match self {
Self::Delta(evt) => evt.time(),
Self::Cancel(evt) => evt.time(),
Self::Discrete(evt) => evt.time(),
Self::Stop(evt) => evt.time(),
}
}
fn device(&self) -> request::Device {
match self {
Self::Delta(evt) => evt.device(),
Self::Cancel(evt) => evt.device(),
Self::Discrete(evt) => evt.device(),
Self::Stop(evt) => evt.device(),
}
}
}
impl input::PointerAxisEvent<EiInput> for ScrollEvent {
fn amount(&self, axis: input::Axis) -> Option<f64> {
match self {
Self::Delta(evt) => match axis {
input::Axis::Horizontal if evt.dx != 0.0 => Some(evt.dx.into()),
input::Axis::Vertical if evt.dy != 0.0 => Some(evt.dy.into()),
_ => None,
},
// Same as Mutter
Self::Cancel(evt) => match axis {
input::Axis::Horizontal if evt.x => Some(0.01),
input::Axis::Vertical if evt.y => Some(0.01),
_ => None,
},
Self::Discrete(_evt) => None,
Self::Stop(evt) => match axis {
input::Axis::Horizontal if evt.x => Some(0.0),
input::Axis::Vertical if evt.y => Some(0.0),
_ => None,
},
}
}
fn amount_v120(&self, axis: input::Axis) -> Option<f64> {
match self {
Self::Discrete(evt) => match axis {
input::Axis::Horizontal if evt.discrete_dx != 0 => Some(evt.discrete_dx.into()),
input::Axis::Vertical if evt.discrete_dy != 0 => Some(evt.discrete_dy.into()),
_ => None,
},
_ => None,
}
}
fn source(&self) -> input::AxisSource {
// Mutter seems to also use wheel for all the scroll events
input::AxisSource::Wheel
}
fn relative_direction(&self, _axis: input::Axis) -> input::AxisRelativeDirection {
input::AxisRelativeDirection::Identical
}
}
impl input::PointerButtonEvent<EiInput> for request::Button {
fn button_code(&self) -> u32 {
self.button
}
fn state(&self) -> input::ButtonState {
match self.state {
eis::button::ButtonState::Press => input::ButtonState::Pressed,
eis::button::ButtonState::Released => input::ButtonState::Released,
}
}
}
impl input::PointerMotionEvent<EiInput> for request::PointerMotion {
fn delta_x(&self) -> f64 {
self.dx.into()
}
fn delta_y(&self) -> f64 {
self.dy.into()
}
fn delta_x_unaccel(&self) -> f64 {
self.dx.into()
}
fn delta_y_unaccel(&self) -> f64 {
self.dy.into()
}
}
impl input::PointerMotionAbsoluteEvent<EiInput> for request::PointerMotionAbsolute {}
impl input::AbsolutePositionEvent<EiInput> for request::PointerMotionAbsolute {
fn x(&self) -> f64 {
self.dx_absolute.into()
}
fn y(&self) -> f64 {
self.dy_absolute.into()
}
fn x_transformed(&self, _width: i32) -> f64 {
// XXX ?
self.dx_absolute.into()
}
fn y_transformed(&self, _height: i32) -> f64 {
self.dy_absolute.into()
}
}
impl input::TouchDownEvent<EiInput> for request::TouchDown {}
impl input::TouchEvent<EiInput> for request::TouchDown {
fn slot(&self) -> input::TouchSlot {
Some(self.touch_id).into()
}
}
impl input::AbsolutePositionEvent<EiInput> for request::TouchDown {
fn x(&self) -> f64 {
self.x.into()
}
fn y(&self) -> f64 {
self.y.into()
}
fn x_transformed(&self, _width: i32) -> f64 {
// XXX ?
self.x.into()
}
fn y_transformed(&self, _height: i32) -> f64 {
self.y.into()
}
}
impl input::TouchUpEvent<EiInput> for request::TouchUp {}
impl input::TouchEvent<EiInput> for request::TouchUp {
fn slot(&self) -> input::TouchSlot {
Some(self.touch_id).into()
}
}
impl input::TouchMotionEvent<EiInput> for request::TouchMotion {}
impl input::TouchEvent<EiInput> for request::TouchMotion {
fn slot(&self) -> input::TouchSlot {
Some(self.touch_id).into()
}
}
impl input::AbsolutePositionEvent<EiInput> for request::TouchMotion {
fn x(&self) -> f64 {
self.x.into()
}
fn y(&self) -> f64 {
self.y.into()
}
fn x_transformed(&self, _width: i32) -> f64 {
// XXX ?
self.x.into()
}
fn y_transformed(&self, _height: i32) -> f64 {
self.y.into()
}
}
impl EventSource for EiInput {
type Event = InputEvent<EiInput>;
type Metadata = ();
type Ret = ();
type Error = io::Error;
fn process_events<F>(
&mut self,
readiness: Readiness,
token: Token,
mut cb: F,
) -> Result<PostAction, <Self as EventSource>::Error>
where
F: FnMut(InputEvent<EiInput>, &mut ()) -> (),
{
self.source.process_events(readiness, token, |event, connection| {
match event {
Ok(EisRequestSourceEvent::Connected) => {
let seat = connection.add_seat(
Some("default"),
&[
DeviceCapability::Pointer,
DeviceCapability::PointerAbsolute,
DeviceCapability::Keyboard,
DeviceCapability::Touch,
DeviceCapability::Scroll,
DeviceCapability::Button,
],
);
self.seat = Some(seat);
}
Ok(EisRequestSourceEvent::Request(EisRequest::Disconnect)) => {
return Ok(PostAction::Remove);
}
Ok(EisRequestSourceEvent::Request(EisRequest::Bind(request))) => {
let capabilities = request.capabilities;
// TODO Handle in converter
if capabilities & 0x7e != capabilities {
return disconnected(
connection,
eis::connection::DisconnectReason::Value,
"Invalid capabilities",
);
}
let seat = self.seat.as_ref().unwrap();
if connection.has_interface("ei_keyboard")
&& capabilities & 2 << DeviceCapability::Keyboard as u64 != 0
{
// XXX use seat keymap
let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);
let keymap = XkbConfig::default().compile_keymap(&context).unwrap();
let keymap_text = keymap.get_as_string(xkb::KEYMAP_FORMAT_TEXT_V1);
let file = SealedFile::with_data(
CStr::from_bytes_with_nul(b"eis-keymap\0").unwrap(),
keymap_text.as_bytes(),
)
.unwrap();
let device = seat.add_device(
Some("keyboard"),
DeviceType::Virtual,
&[DeviceCapability::Keyboard],
|device| {
let keyboard = device.interface::<eis::Keyboard>().unwrap();
keyboard.keymap(
eis::keyboard::KeymapType::Xkb,
keymap_text.len() as _,
file.as_fd(),
);
},
);
}
// XXX button/etc should be on same object
if connection.has_interface("ei_pointer")
&& capabilities & 2 << DeviceCapability::Pointer as u64 != 0
{
seat.add_device(
Some("pointer"),
DeviceType::Virtual,
&[DeviceCapability::Pointer],
|_| {},
);
}
if connection.has_interface("ei_touchscreen")
&& capabilities & 2 << DeviceCapability::Touch as u64 != 0
{
seat.add_device(
Some("touch"),
DeviceType::Virtual,
&[DeviceCapability::Touch],
|_| {},
);
}
if connection.has_interface("ei_pointer_absolute")
&& capabilities & 2 << DeviceCapability::PointerAbsolute as u64 != 0
{
seat.add_device(
Some("pointer-abs"),
DeviceType::Virtual,
&[DeviceCapability::PointerAbsolute],
|_| {},
);
}
// TODO create devices; compare against current bitflag
}
Ok(EisRequestSourceEvent::Request(request)) => {
if let Some(input_event) = convert_request(request) {
cb(input_event, &mut ());
}
}
Ok(EisRequestSourceEvent::InvalidObject(_object_id)) => {}
Err(err) => {
tracing::error!("Libei client error: {}", err);
return Ok(PostAction::Remove);
}
}
connection.flush();
Ok(PostAction::Continue)
})
}
fn register(
&mut self,
poll: &mut calloop::Poll,
token_factory: &mut TokenFactory,
) -> Result<(), calloop::Error> {
self.source.register(poll, token_factory)
}
fn reregister(
&mut self,
poll: &mut calloop::Poll,
token_factory: &mut TokenFactory,
) -> Result<(), calloop::Error> {
self.source.reregister(poll, token_factory)
}
fn unregister(&mut self, poll: &mut calloop::Poll) -> Result<(), calloop::Error> {
self.source.unregister(poll)
}
}
fn convert_request(request: EisRequest) -> Option<InputEvent<EiInput>> {
match request {
EisRequest::KeyboardKey(event) => Some(InputEvent::Keyboard { event }),
EisRequest::PointerMotion(event) => Some(InputEvent::PointerMotion { event }),
EisRequest::PointerMotionAbsolute(event) => Some(InputEvent::PointerMotionAbsolute { event }),
EisRequest::Button(event) => Some(InputEvent::PointerButton { event }),
EisRequest::ScrollDelta(event) => Some(InputEvent::PointerAxis {
event: ScrollEvent::Delta(event),
}),
EisRequest::ScrollStop(event) => Some(InputEvent::PointerAxis {
event: ScrollEvent::Stop(event),
}),
EisRequest::ScrollCancel(event) => Some(InputEvent::PointerAxis {
event: ScrollEvent::Cancel(event),
}),
EisRequest::ScrollDiscrete(event) => Some(InputEvent::PointerAxis {
event: ScrollEvent::Discrete(event),
}),
EisRequest::TouchDown(event) => Some(InputEvent::TouchDown { event }),
EisRequest::TouchUp(event) => Some(InputEvent::TouchUp { event }),
EisRequest::TouchMotion(event) => Some(InputEvent::TouchMotion { event }),
EisRequest::Frame(_) => None, // TODO
EisRequest::Disconnect
| EisRequest::Bind(_)
| EisRequest::DeviceStartEmulating(_)
| EisRequest::DeviceStopEmulating(_) => None,
}
}
// XXX not a direct match?
impl TryFrom<input::DeviceCapability> for DeviceCapability {
type Error = ();
fn try_from(other: input::DeviceCapability) -> Result<DeviceCapability, ()> {
match other {
input::DeviceCapability::Gesture => Err(()),
input::DeviceCapability::Keyboard => Ok(DeviceCapability::Keyboard),
input::DeviceCapability::Pointer => Ok(DeviceCapability::Pointer),
input::DeviceCapability::Switch => Err(()),
input::DeviceCapability::TabletPad => Err(()),
input::DeviceCapability::TabletTool => Err(()),
input::DeviceCapability::Touch => Ok(DeviceCapability::Touch),
}
}
}