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
use std::mem::{size_of, size_of_val};
use nix;
use nix::sys::ioctl::ioctl_num_type;
use libc::{c_char, c_int, c_uint, uint8_t, uint16_t, int32_t, uint32_t, clockid_t, ioctl};

pub use libc::{
    timeval,
    input_event, input_id, input_absinfo, input_keymap_entry, input_mask,
    ff_replay, ff_trigger, ff_envelope, ff_effect, ff_constant_effect,
    ff_ramp_effect, ff_condition_effect, ff_periodic_effect, ff_rumble_effect,
};

/// Protocol version.
pub const EV_VERSION: c_int = 0x010001;

pub const ID_BUS: usize = 0;
pub const ID_VENDOR: usize = 1;
pub const ID_PRODUCT: usize = 2;
pub const ID_VERSION: usize = 3;

pub const BUS_PCI: uint16_t = 0x01;
pub const BUS_ISAPNP: uint16_t = 0x02;
pub const BUS_USB: uint16_t = 0x03;
pub const BUS_HIL: uint16_t = 0x04;
pub const BUS_BLUETOOTH: uint16_t = 0x05;
pub const BUS_VIRTUAL: uint16_t = 0x06;

pub const BUS_ISA: uint16_t = 0x10;
pub const BUS_I8042: uint16_t = 0x11;
pub const BUS_XTKBD: uint16_t = 0x12;
pub const BUS_RS232: uint16_t = 0x13;
pub const BUS_GAMEPORT: uint16_t = 0x14;
pub const BUS_PARPORT: uint16_t = 0x15;
pub const BUS_AMIGA: uint16_t = 0x16;
pub const BUS_ADB: uint16_t = 0x17;
pub const BUS_I2C: uint16_t = 0x18;
pub const BUS_HOST: uint16_t = 0x19;
pub const BUS_GSC: uint16_t = 0x1A;
pub const BUS_ATARI: uint16_t = 0x1B;
pub const BUS_SPI: uint16_t = 0x1C;
pub const BUS_RMI: uint16_t = 0x1D;
pub const BUS_CEC: uint16_t = 0x1E;
pub const BUS_INTEL_ISHTP: uint16_t = 0x1F;

pub const MT_TOOL_FINGER: uint16_t = 0;
pub const MT_TOOL_PEN: uint16_t = 1;
pub const MT_TOOL_PALM: uint16_t = 2;
pub const MT_TOOL_MAX: uint16_t = 2;

pub const FF_STATUS_STOPPED: uint16_t = 0x00;
pub const FF_STATUS_PLAYING: uint16_t = 0x01;
pub const FF_STATUS_MAX: uint16_t = 0x01;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct ff_effect_union {
    #[cfg(target_pointer_width = "64")]
    pub u: [u64; 4],
    #[cfg(target_pointer_width = "32")]
    pub u: [u32; 7],
}

impl<'a> From<&'a ff_effect> for &'a ff_effect_union {
    fn from(effect: &'a ff_effect) -> Self {
        unsafe {
            let raw = &effect.u as *const _ as *const _;
            &*raw
        }
    }
}

impl<'a> From<&'a mut ff_effect> for &'a mut ff_effect_union {
    fn from(effect: &'a mut ff_effect) -> Self {
        unsafe {
            let raw = &mut effect.u as *mut _ as *mut _;
            &mut *raw
        }
    }
}

impl ff_effect_union {
    pub fn constant(&self) -> &ff_constant_effect {
        unsafe {
            let raw = &self.u as *const _ as *const _;
            &*raw
        }
    }

    pub fn constant_mut(&mut self) -> &mut ff_constant_effect {
        unsafe {
            let raw = &mut self.u as *mut _ as *mut _;
            &mut *raw
        }
    }

    pub fn ramp(&self) -> &ff_ramp_effect {
        unsafe {
            let raw = &self.u as *const _ as *const _;
            &*raw
        }
    }

    pub fn ramp_mut(&mut self) -> &mut ff_ramp_effect {
        unsafe {
            let raw = &mut self.u as *mut _ as *mut _;
            &mut *raw
        }
    }

    pub fn periodic(&self) -> &ff_periodic_effect {
        unsafe {
            let raw = &self.u as *const _ as *const _;
            &*raw
        }
    }

    pub fn periodic_mut(&mut self) -> &mut ff_periodic_effect {
        unsafe {
            let raw = &mut self.u as *mut _ as *mut _;
            &mut *raw
        }
    }

    pub fn condition(&self) -> &[ff_condition_effect; 2] {
        unsafe {
            let raw = &self.u as *const _ as *const _;
            &*raw
        }
    }

    pub fn condition_mut(&mut self) -> &mut [ff_condition_effect; 2] {
        unsafe {
            let raw = &mut self.u as *mut _ as *mut _;
            &mut *raw
        }
    }

    pub fn rumble(&self) -> &ff_rumble_effect {
        unsafe {
            let raw = &self.u as *const _ as *const _;
            &*raw
        }
    }

    pub fn rumble_mut(&mut self) -> &mut ff_rumble_effect {
        unsafe {
            let raw = &mut self.u as *mut _ as *mut _;
            &mut *raw
        }
    }
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct repeat_settings {
    pub delay: c_uint,
    pub period: c_uint,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct input_mt_request_layout<T: ?Sized = [int32_t]> {
    pub code: uint32_t,
    pub values: T,
}

ioctl! {
    /// get driver version
    read ev_get_version with b'E', 0x01; c_int
}

ioctl! {
    /// get device ID
    read ev_get_id with b'E', 0x02; input_id
}

ioctl! {
    /// get repeat settings
    read ev_get_rep with b'E', 0x03; repeat_settings
}

ioctl! {
    /// set repeat settings
    write_ptr ev_set_rep with b'E', 0x03; repeat_settings
}

ioctl! {
    /// get keycode
    read ev_get_keycode with b'E', 0x04; [c_uint; 2]
}

ioctl! {
    /// get keycode
    read ev_get_keycode_v2 with b'E', 0x04; input_keymap_entry
}

ioctl! {
    /// set keycode
    write_ptr ev_set_keycode with b'E', 0x04; [c_uint; 2]
}

ioctl! {
    /// set keycode
    write_ptr ev_set_keycode_v2 with b'E', 0x04; input_keymap_entry
}

ioctl! {
    /// get device name
    read_buf ev_get_name with b'E', 0x06; c_char
}

ioctl! {
    /// get physical location
    read_buf ev_get_phys with b'E', 0x07; c_char
}

ioctl! {
    /// get unique identifier
    read_buf ev_get_uniq with b'E', 0x08; c_char
}

ioctl! {
    /// get device properties
    read_buf ev_get_prop with b'E', 0x09; uint8_t
}

/// get MT slot values
pub unsafe fn ev_get_mtslots(fd: c_int, buf: *mut input_mt_request_layout) -> nix::Result<i32> {
    // for some reason this isn't _IORW?
    convert_ioctl_res!(ioctl(fd, ior!(b'E', 0x0a, size_of_val(&*buf)) as ioctl_num_type, buf))
}

ioctl! {
    /// get global key state
    read_buf ev_get_key with b'E', 0x18; uint8_t
}

ioctl! {
    /// get all LEDs
    read_buf ev_get_led with b'E', 0x19; uint8_t
}

ioctl! {
    /// get all sounds status
    read_buf ev_get_snd with b'E', 0x1a; uint8_t
}

ioctl! {
    /// get all switch states
    read_buf ev_get_sw with b'E', 0x1b; uint8_t
}

/// get event bits
pub unsafe fn ev_get_bit(fd: c_int, ev: u32, buf: &mut [uint8_t]) -> nix::Result<i32> {
    convert_ioctl_res!(ioctl(fd, ior!(b'E', 0x20 + ev, buf.len()) as ioctl_num_type, buf))
}

/// get abs value/limits
pub unsafe fn ev_get_abs(fd: c_int, abs: u32, buf: *mut input_absinfo) -> nix::Result<i32> {
    convert_ioctl_res!(ioctl(fd, ior!(b'E', 0x40 + abs, size_of::<input_absinfo>()) as ioctl_num_type, buf))
}

/// set abs value/limits
pub unsafe fn ev_set_abs(fd: c_int, abs: u32, buf: *const input_absinfo) -> nix::Result<i32> {
    convert_ioctl_res!(ioctl(fd, ior!(b'E', 0x40 + abs, size_of::<input_absinfo>()) as ioctl_num_type, buf))
}

/// send a force effect to a force feedback device
pub unsafe fn ev_send_ff(fd: c_int, buf: *mut ff_effect) -> nix::Result<i32> {
    // for some reason this isn't _IORW?
    convert_ioctl_res!(ioctl(fd, iow!(b'E', 0x80, size_of::<ff_effect>()) as ioctl_num_type, buf))
}

ioctl! {
    /// Erase a force effect
    write_int ev_erase_ff with b'E', 0x81
}

ioctl! {
    /// Report number of effects playable at the same time
    read ev_get_effects with b'E', 0x84; c_int
}

ioctl! {
    /// Grab/Release device
    write_int ev_grab with b'E', 0x90
}

ioctl! {
    /// Revoke device access
    write_int ev_revoke with b'E', 0x91
}

ioctl! {
    /// Retrieve current event mask
    read ev_get_mask with b'E', 0x92; input_mask
}

ioctl! {
    /// Set event mask
    write_ptr ev_set_mask with b'E', 0x93; input_mask
}

ioctl! {
    /// Set clockid to be used for timestamps
    write_ptr ev_set_clockid with b'E', 0xa0; clockid_t
}