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
// Copyright 2019 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::collections::BTreeMap;
use std::os::raw::c_uint;
use std::ptr::null;

use base::ioctl_ior_nr;
use base::ioctl_iow_nr;
use base::ioctl_with_mut_ref;
use base::ioctl_with_ptr;
use base::ioctl_with_ref;
use base::AsRawDescriptor;
use data_model::Le32;
use linux_input_sys::constants::*;

use super::virtio_input_absinfo;
use super::virtio_input_bitmap;
use super::virtio_input_device_ids;
use super::InputError;
use super::Result;

const EVDEV: c_uint = 69;

#[repr(C)]
#[derive(Copy, Clone)]
struct evdev_buffer {
    buffer: [std::os::raw::c_uchar; 128],
}

impl evdev_buffer {
    fn new() -> evdev_buffer {
        evdev_buffer { buffer: [0u8; 128] }
    }

    fn get(&self, bit: usize) -> bool {
        let idx = bit / 8;
        let inner_bit = bit % 8;
        self.buffer
            .get(idx)
            .map_or(false, |val| val & (1u8 << inner_bit) != 0)
    }
}

#[repr(C)]
#[derive(Copy, Clone)]
struct evdev_id {
    bustype: u16,
    vendor: u16,
    product: u16,
    version: u16,
}

impl evdev_id {
    fn new() -> evdev_id {
        evdev_id {
            bustype: 0,
            vendor: 0,
            product: 0,
            version: 0,
        }
    }
}

#[repr(C)]
#[derive(Copy, Clone)]
struct evdev_abs_info {
    // These should technically by signed ints, but Le32 is only compatible with u32 and we only
    // forward the bytes but don't care about its actual values.
    value: u32,
    minimum: u32,
    maximum: u32,
    fuzz: u32,
    flat: u32,
    resolution: u32,
}

impl evdev_abs_info {
    fn new() -> evdev_abs_info {
        evdev_abs_info {
            value: 0,
            minimum: 0,
            maximum: 0,
            fuzz: 0,
            flat: 0,
            resolution: 0,
        }
    }
}

impl From<evdev_abs_info> for virtio_input_absinfo {
    fn from(other: evdev_abs_info) -> Self {
        virtio_input_absinfo {
            min: Le32::from(other.minimum),
            max: Le32::from(other.maximum),
            fuzz: Le32::from(other.fuzz),
            flat: Le32::from(other.flat),
        }
    }
}

ioctl_ior_nr!(EVIOCGID, EVDEV, 0x02, evdev_id);
ioctl_ior_nr!(EVIOCGNAME, EVDEV, 0x06, evdev_buffer);
ioctl_ior_nr!(EVIOCGUNIQ, EVDEV, 0x08, evdev_buffer);
ioctl_ior_nr!(EVIOCGPROP, EVDEV, 0x09, evdev_buffer);
ioctl_ior_nr!(EVIOCGBIT, EVDEV, 0x20 + evt, evdev_buffer, evt);
ioctl_ior_nr!(EVIOCGABS, EVDEV, 0x40 + abs, evdev_abs_info, abs);
ioctl_iow_nr!(EVIOCGRAB, EVDEV, 0x90, u32);

fn errno() -> base::Error {
    base::Error::last()
}

fn string_from_bytes_with_nul(buffer: &[u8], mut len: usize) -> Result<String> {
    // Trim NUL byte.
    if len > 0 && buffer[len] == 0 {
        len -= 1;
    }
    String::from_utf8(buffer[0..len].to_vec()).map_err(InputError::InvalidString)
}

/// Gets id information from an event device (see EVIOCGID ioctl for details).
pub fn device_ids<T: AsRawDescriptor>(descriptor: &T) -> Result<virtio_input_device_ids> {
    let mut dev_id = evdev_id::new();
    let len = {
        // SAFETY:
        // Safe because the kernel won't write more than size of evdev_id and we check the return
        // value
        unsafe { ioctl_with_mut_ref(descriptor, EVIOCGID, &mut dev_id) }
    };
    if len < 0 {
        return Err(InputError::EvdevIdError(errno()));
    }
    Ok(virtio_input_device_ids::new(
        dev_id.bustype,
        dev_id.vendor,
        dev_id.product,
        dev_id.version,
    ))
}

/// Gets the name of an event device (see EVIOCGNAME ioctl for details).
pub fn name<T: AsRawDescriptor>(descriptor: &T) -> Result<String> {
    let mut name = evdev_buffer::new();
    let len = {
        // SAFETY:
        // Safe because the kernel won't write more than size of evdev_buffer and we check the
        // return value
        unsafe { ioctl_with_mut_ref(descriptor, EVIOCGNAME, &mut name) }
    };
    if len < 0 {
        return Err(InputError::EvdevNameError(errno()));
    }
    string_from_bytes_with_nul(&name.buffer, len as usize)
}

/// Gets the unique (serial) name of an event device (see EVIOCGUNIQ ioctl for details).
pub fn serial_name<T: AsRawDescriptor>(descriptor: &T) -> Result<String> {
    let mut uniq = evdev_buffer::new();
    let len = {
        // SAFETY:
        // Safe because the kernel won't write more than size of evdev_buffer and we check the
        // return value
        unsafe { ioctl_with_mut_ref(descriptor, EVIOCGUNIQ, &mut uniq) }
    };
    if len < 0 {
        return Err(InputError::EvdevSerialError(errno()));
    }
    string_from_bytes_with_nul(&uniq.buffer, len as usize)
}

/// Gets the properties of an event device (see EVIOCGPROP ioctl for details).
pub fn properties<T: AsRawDescriptor>(descriptor: &T) -> Result<virtio_input_bitmap> {
    let mut props = evdev_buffer::new();
    let len = {
        // SAFETY:
        // Safe because the kernel won't write more than size of evdev_buffer and we check the
        // return value
        unsafe { ioctl_with_mut_ref(descriptor, EVIOCGPROP, &mut props) }
    };
    if len < 0 {
        return Err(InputError::EvdevPropertiesError(errno()));
    }
    Ok(virtio_input_bitmap::new(props.buffer))
}

/// Gets the event types supported by an event device as well as the event codes supported for each
/// type (see EVIOCGBIT ioctl for details).
pub fn supported_events<T: AsRawDescriptor>(
    descriptor: &T,
) -> Result<BTreeMap<u16, virtio_input_bitmap>> {
    let mut evts: BTreeMap<u16, virtio_input_bitmap> = BTreeMap::new();

    let mut evt_types = evdev_buffer::new();
    let len = {
        // SAFETY:
        // Safe because the kernel won't write more than size of evdev_buffer and we check the
        // return value
        unsafe { ioctl_with_mut_ref(descriptor, EVIOCGBIT(0), &mut evt_types) }
    };
    if len < 0 {
        return Err(InputError::EvdevEventTypesError(errno()));
    }

    // no need to ask for zero (EV_SYN) since it's always supported and treated as a special case
    for ev in 1..EV_MAX {
        if ev == EV_REP || !evt_types.get(ev as usize) {
            // Event type not supported, skip it.
            continue;
        }
        // Create a new zero-filled buffer every time to avoid carry-overs.
        let mut evt_codes = evdev_buffer::new();
        let len = {
            // SAFETY:
            // Safe because the kernel won't write more than size of evdev_buffer and we check the
            // return value
            unsafe { ioctl_with_mut_ref(descriptor, EVIOCGBIT(ev as c_uint), &mut evt_codes) }
        };
        if len < 0 {
            return Err(InputError::EvdevEventTypesError(errno()));
        }
        evts.insert(ev, virtio_input_bitmap::new(evt_codes.buffer));
    }
    Ok(evts)
}

/// Gets the absolute axes of an event device (see EVIOCGABS ioctl for details).
pub fn abs_info<T: AsRawDescriptor>(descriptor: &T) -> BTreeMap<u16, virtio_input_absinfo> {
    let mut map: BTreeMap<u16, virtio_input_absinfo> = BTreeMap::new();

    for abs in 0..ABS_MAX {
        // Create a new one, zero-ed out every time to avoid carry-overs.
        let mut abs_info = evdev_abs_info::new();
        let ret = {
            // SAFETY:
            // Safe because the kernel won't write more than size of evdev_buffer and we check the
            // return value
            unsafe { ioctl_with_mut_ref(descriptor, EVIOCGABS(abs as c_uint), &mut abs_info) }
        };
        if ret == 0 {
            map.insert(abs, virtio_input_absinfo::from(abs_info));
        }
    }
    map
}

/// Grabs an event device (see EVIOCGGRAB ioctl for details). After this function succeeds the given
/// descriptor has exclusive access to the device, effectively making it unusable for any other
/// process in the host.
pub fn grab_evdev<T: AsRawDescriptor>(descriptor: &mut T) -> Result<()> {
    let val: u32 = 1;
    let ret = {
        // SAFETY:
        // Safe because the kernel only read the value of the ptr and we check the return value
        unsafe { ioctl_with_ref(descriptor, EVIOCGRAB, &val) }
    };
    if ret == 0 {
        Ok(())
    } else {
        Err(InputError::EvdevGrabError(errno()))
    }
}

pub fn ungrab_evdev<T: AsRawDescriptor>(descriptor: &mut T) -> Result<()> {
    let ret = {
        // SAFETY:
        // Safe because the kernel only reads the value of the ptr (doesn't dereference) and
        // we check the return value
        unsafe { ioctl_with_ptr(descriptor, EVIOCGRAB, null::<u32>()) }
    };
    if ret == 0 {
        Ok(())
    } else {
        Err(InputError::EvdevGrabError(errno()))
    }
}