gilrs_core/platform/linux/
ioctl.rs

1// Copyright 2016-2018 Mateusz Sieczko and other GilRs Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8// Some ioctls are exported by ioctl crate only for x86_64, so we have to define them anyway.
9// Diffing linux/input.h across different architectures (i686, x86_64 and arm) didn't show any
10// difference, so it looks like conditional compilation is not needed.
11#![allow(dead_code)]
12
13use nix::{ioctl_read, ioctl_read_buf, ioctl_write_int, ioctl_write_ptr, request_code_read};
14use std::mem::MaybeUninit;
15
16#[cfg(target_env = "musl")]
17pub type IoctlRequest = libc::c_int;
18#[cfg(not(target_env = "musl"))]
19pub type IoctlRequest = libc::c_ulong;
20
21ioctl_read!(eviocgid, b'E', 0x02, /*struct*/ input_id);
22ioctl_write_int!(eviocrmff, b'E', 0x81);
23ioctl_write_ptr!(eviocsff, b'E', 0x80, ff_effect);
24ioctl_read_buf!(eviocgname, b'E', 0x06, MaybeUninit<u8>);
25ioctl_read_buf!(eviocgkey, b'E', 0x18, u8);
26
27pub unsafe fn eviocgbit(fd: libc::c_int, ev: u32, len: libc::c_int, buf: *mut u8) -> libc::c_int {
28    ::nix::libc::ioctl(
29        fd,
30        request_code_read!(b'E', 0x20 + ev, len) as IoctlRequest,
31        buf,
32    )
33}
34
35pub unsafe fn eviocgabs(fd: ::libc::c_int, abs: u32, buf: *mut input_absinfo) -> libc::c_int {
36    ::nix::libc::ioctl(
37        fd,
38        request_code_read!(b'E', 0x40 + abs, ::std::mem::size_of::<input_absinfo>())
39            as IoctlRequest,
40        buf,
41    )
42}
43
44#[derive(Copy, Clone)]
45#[repr(C)]
46pub struct input_event {
47    pub time: libc::timeval,
48    pub type_: u16,
49    pub code: u16,
50    pub value: i32,
51}
52
53impl ::std::default::Default for input_event {
54    fn default() -> Self {
55        unsafe { ::std::mem::zeroed() }
56    }
57}
58
59impl ::std::fmt::Debug for input_event {
60    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
61        write!(
62            f,
63            "input_event {{ time: {{ tv_sec: {}, tv_usec: {} }}, type_: {}, code: {}, value: {}",
64            self.time.tv_sec, self.time.tv_usec, self.type_, self.code, self.value
65        )
66    }
67}
68
69#[derive(Copy, Clone)]
70#[repr(C)]
71pub struct input_id {
72    pub bustype: u16,
73    pub vendor: u16,
74    pub product: u16,
75    pub version: u16,
76}
77
78#[derive(Copy, Clone, Default, PartialEq, Eq, Debug)]
79#[repr(C)]
80pub struct input_absinfo {
81    pub value: i32,
82    pub minimum: i32,
83    pub maximum: i32,
84    pub fuzz: i32,
85    pub flat: i32,
86    pub resolution: i32,
87}
88
89#[derive(Copy, Clone, Default)]
90#[repr(C)]
91pub struct ff_replay {
92    pub length: u16,
93    pub delay: u16,
94}
95
96#[derive(Copy, Clone, Default)]
97#[repr(C)]
98pub struct ff_trigger {
99    pub button: u16,
100    pub interval: u16,
101}
102
103#[derive(Copy, Clone)]
104#[repr(C)]
105pub struct ff_envelope {
106    pub attack_length: u16,
107    pub attack_level: u16,
108    pub fade_length: u16,
109    pub fade_level: u16,
110}
111
112#[derive(Copy, Clone)]
113#[repr(C)]
114pub struct ff_constant_effect {
115    pub level: i16,
116    pub envelope: ff_envelope,
117}
118
119#[derive(Copy, Clone)]
120#[repr(C)]
121pub struct ff_ramp_effect {
122    pub start_level: i16,
123    pub end_level: i16,
124    pub envelope: ff_envelope,
125}
126
127#[derive(Copy, Clone)]
128#[repr(C)]
129pub struct ff_condition_effect {
130    pub right_saturation: u16,
131    pub left_saturation: u16,
132
133    pub right_coeff: i16,
134    pub left_coeff: i16,
135
136    pub deadband: u16,
137    pub center: i16,
138}
139
140#[derive(Copy, Clone)]
141#[repr(C)]
142pub struct ff_periodic_effect {
143    pub waveform: u16,
144    pub period: u16,
145    pub magnitude: i16,
146    pub offset: i16,
147    pub phase: u16,
148
149    pub envelope: ff_envelope,
150
151    pub custom_len: u32,
152    pub custom_data: *mut i16,
153}
154
155#[derive(Copy, Clone)]
156#[repr(C)]
157pub struct ff_rumble_effect {
158    pub strong_magnitude: u16,
159    pub weak_magnitude: u16,
160}
161
162#[derive(Copy, Clone)]
163#[repr(C)]
164pub struct ff_effect {
165    pub type_: u16,
166    pub id: i16,
167    pub direction: u16,
168    pub trigger: ff_trigger,
169    pub replay: ff_replay,
170    // FIXME this is actually a union
171    #[cfg(target_pointer_width = "64")]
172    pub u: [u64; 4],
173    #[cfg(target_pointer_width = "32")]
174    pub u: [u32; 7],
175}