• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use base::{AsRawDescriptor, RawDescriptor};
6 use data_model::DataInit;
7 use linux_input_sys::{virtio_input_event, InputEventDecoder};
8 use std::collections::VecDeque;
9 use std::io::{self, Error, ErrorKind, Read, Write};
10 use std::iter::ExactSizeIterator;
11 use std::os::unix::net::UnixStream;
12 
13 const EVENT_SIZE: usize = virtio_input_event::SIZE;
14 const EVENT_BUFFER_LEN_MAX: usize = 16 * EVENT_SIZE;
15 
16 // /// Half-way build `EventDevice` with only the `event_socket` defined. Finish building the
17 // /// `EventDevice` by using `status_socket`.
18 // pub struct PartialEventDevice(UnixStream);
19 
20 // impl PartialEventDevice {
21 //     /// Finish build `EventDevice` by providing the `status_socket`.
22 //     pub fn status_socket(self, status_socket: UnixStream) -> EventDevice {
23 //         EventDevice {
24 //             event_socket: self.0,
25 //             status_socket,
26 //         }
27 //     }
28 // }
29 
30 #[derive(Copy, Clone, PartialEq, Eq)]
31 pub enum EventDeviceKind {
32     /// Produces relative mouse motions, wheel, and button clicks while the real mouse is captured.
33     Mouse,
34     /// Produces absolute motion and touch events from the display window's events.
35     Touchscreen,
36     /// Produces key events while the display window has focus.
37     Keyboard,
38 }
39 
40 /// Encapsulates a virtual event device, such as a mouse or keyboard
41 pub struct EventDevice {
42     kind: EventDeviceKind,
43     event_buffer: VecDeque<u8>,
44     event_socket: UnixStream,
45 }
46 
47 impl EventDevice {
new(kind: EventDeviceKind, event_socket: UnixStream) -> EventDevice48     pub fn new(kind: EventDeviceKind, event_socket: UnixStream) -> EventDevice {
49         let _ = event_socket.set_nonblocking(true);
50         EventDevice {
51             kind,
52             event_buffer: Default::default(),
53             event_socket,
54         }
55     }
56 
57     #[inline]
mouse(event_socket: UnixStream) -> EventDevice58     pub fn mouse(event_socket: UnixStream) -> EventDevice {
59         Self::new(EventDeviceKind::Mouse, event_socket)
60     }
61 
62     #[inline]
touchscreen(event_socket: UnixStream) -> EventDevice63     pub fn touchscreen(event_socket: UnixStream) -> EventDevice {
64         Self::new(EventDeviceKind::Touchscreen, event_socket)
65     }
66 
67     #[inline]
keyboard(event_socket: UnixStream) -> EventDevice68     pub fn keyboard(event_socket: UnixStream) -> EventDevice {
69         Self::new(EventDeviceKind::Keyboard, event_socket)
70     }
71 
72     #[inline]
kind(&self) -> EventDeviceKind73     pub fn kind(&self) -> EventDeviceKind {
74         self.kind
75     }
76 
77     /// Flushes the buffered events that did not fit into the underlying transport, if any.
78     ///
79     /// Returns `Ok(true)` if, after this function returns, there all the buffer of events is
80     /// empty.
flush_buffered_events(&mut self) -> io::Result<bool>81     pub fn flush_buffered_events(&mut self) -> io::Result<bool> {
82         while !self.event_buffer.is_empty() {
83             let written = self.event_socket.write(&self.event_buffer.as_slices().0)?;
84             if written == 0 {
85                 return Ok(false);
86             }
87             self.event_buffer.drain(..written);
88         }
89         Ok(true)
90     }
91 
is_buffered_events_empty(&self) -> bool92     pub fn is_buffered_events_empty(&self) -> bool {
93         self.event_buffer.is_empty()
94     }
95 
send_report<E: IntoIterator<Item = virtio_input_event>>( &mut self, events: E, ) -> io::Result<bool> where E::IntoIter: ExactSizeIterator,96     pub fn send_report<E: IntoIterator<Item = virtio_input_event>>(
97         &mut self,
98         events: E,
99     ) -> io::Result<bool>
100     where
101         E::IntoIter: ExactSizeIterator,
102     {
103         let it = events.into_iter();
104         if self.event_buffer.len() > (EVENT_BUFFER_LEN_MAX - EVENT_SIZE * (it.len() + 1)) {
105             return Ok(false);
106         }
107 
108         for event in it {
109             let bytes = event.as_slice();
110             self.event_buffer.extend(bytes.iter());
111         }
112 
113         self.event_buffer
114             .extend(virtio_input_event::syn().as_slice().iter());
115 
116         self.flush_buffered_events()
117     }
118 
119     /// Sends the given `event`, returning `Ok(true)` if, after this function returns, there are no
120     /// buffered events remaining.
send_event_encoded(&mut self, event: virtio_input_event) -> io::Result<bool>121     pub fn send_event_encoded(&mut self, event: virtio_input_event) -> io::Result<bool> {
122         if !self.flush_buffered_events()? {
123             return Ok(false);
124         }
125 
126         let bytes = event.as_slice();
127         let written = self.event_socket.write(&bytes)?;
128 
129         if written == bytes.len() {
130             return Ok(true);
131         }
132 
133         if self.event_buffer.len() <= (EVENT_BUFFER_LEN_MAX - EVENT_SIZE) {
134             self.event_buffer.extend(bytes[written..].iter());
135         }
136 
137         Ok(false)
138     }
139 
recv_event_encoded(&self) -> io::Result<virtio_input_event>140     pub fn recv_event_encoded(&self) -> io::Result<virtio_input_event> {
141         let mut event_bytes = [0u8; 24];
142         (&self.event_socket).read_exact(&mut event_bytes)?;
143         match virtio_input_event::from_slice(&event_bytes) {
144             Some(event) => Ok(*event),
145             None => Err(Error::new(
146                 ErrorKind::InvalidInput,
147                 "failed to read virtio_input_event",
148             )),
149         }
150     }
151 }
152 
153 impl AsRawDescriptor for EventDevice {
as_raw_descriptor(&self) -> RawDescriptor154     fn as_raw_descriptor(&self) -> RawDescriptor {
155         self.event_socket.as_raw_descriptor()
156     }
157 }
158