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
use std::io;
use sys;

pub const STRING_BUFFER_LENGTH: usize = 0x200;

pub fn convert_error(e: sys::Error) -> io::Error {
    match e {
        sys::Error::Sys(errno) => errno.into(),
        _ => sys::Errno::EINVAL.into(),
    }
}

macro_rules! impl_iterable {
    (@impliter $name: ident($start:expr, $count:expr)) => {
        impl ::bitmask::BitmaskTrait for $name {
            type Array = [u8; (Self::COUNT + 7) / 8];
            type Index = $name;

            fn array_default() -> Self::Array { unsafe { ::std::mem::zeroed() } }
            fn array_slice(array: &Self::Array) -> &[u8] { array }
            fn array_slice_mut(array: &mut Self::Array) -> &mut [u8] { array }
            fn index(index: Self::Index) -> usize { index as usize }
        }

        impl ::enum_iterator::IterableEnum for $name {
            fn iter_next(v: usize) -> Option<(usize, Self)> {
                if v < Self::COUNT {
                    unsafe {
                        Some((v + 1, ::std::mem::transmute(v as u16)))
                    }
                } else {
                    None
                }
            }

            fn iter() -> ::enum_iterator::EnumIterator<Self> {
                ::enum_iterator::EnumIterator::new($start)
            }
        }

        impl $name {
            pub const COUNT: usize = $count as usize;

            pub fn iter() -> ::enum_iterator::EnumIterator<Self> {
                ::enum_iterator::IterableEnum::iter()
            }

            pub fn bitmask() -> ::bitmask::Bitmask<Self> {
                Default::default()
            }
        }

        impl From<$name> for u16 {
            fn from(v: $name) -> Self {
                v as _
            }
        }
    };
    (@implcode $name: ident($start:expr, $count:expr)) => {
        impl $name {
            pub fn from_code(code: u16) -> Result<Self, ::kinds::RangeError> {
                use std::mem;

                if code < Self::COUNT as u16 {
                    Ok(unsafe { mem::transmute(code) })
                } else {
                    Err(Default::default())
                }
            }
        }

        #[cfg(feature = "unstable")]
        impl TryFrom<u16> for $name {
            type Error = kinds::RangeError;

            fn try_from(code: u16) -> Result<Self, Self::Error> {
                Self::from_code(code)
            }
        }
    };
    ($name: ident($start:expr, $count:expr)) => {
        impl_iterable! { @impliter $name($start, $count) }
        impl_iterable! { @implcode $name($start, $count) }
    };
    (@nofromcode $name: ident($start:expr, $count:expr)) => {
        impl_iterable! { @impliter $name($start, $count) }
    };
}

macro_rules! ioctl_impl {
    ($(#[$attr:meta])* @get $f:ident = $ev:ident -> $ret:ty) => {
        $(#[$attr])*
        pub fn $f(&self) -> io::Result<$ret> {
            unsafe {
                let mut v = ::std::mem::uninitialized();
                sys::$ev(self.0, &mut v)
                    .map(|_| v.into())
                    .map_err(::macros::convert_error)
            }
        }
    };
    ($(#[$attr:meta])* @call $f:ident = $ev:ident) => {
        $(#[$attr])*
        pub fn $f(&self) -> io::Result<()> {
            unsafe {
                sys::$ev(self.0)
                    .map(drop)
                    .map_err(::macros::convert_error)
            }
        }
    };
    ($(#[$attr:meta])* @get_buf $f:ident($ty:ty) = $ev:ident) => {
        $(#[$attr])*
        pub fn $f(&self, buffer: &mut [$ty]) -> io::Result<usize> {
            unsafe {
                sys::$ev(self.0, &mut *(buffer as *mut [$ty] as *mut [_]))
                    .map(|len| len as _)
                    .map_err(::macros::convert_error)
            }
        }
    };
    ($(#[$attr:meta])* @get_str $f:ident, $fbuf:ident = $ev:ident) => {
        ioctl_impl! {
            $(#[$attr])*
            @get_buf $fbuf(u8) = $ev
        }

        $(#[$attr])*
        pub fn $f(&self) -> io::Result<Vec<u8>> {
            let mut buf = vec![0; ::macros::STRING_BUFFER_LENGTH];
            self.$fbuf(&mut buf[..]).map(move |len| {
                buf.truncate(len as _);
                buf
            })
        }
    };
    ($(#[$attr:meta])* @set_str $f:ident = $ev:ident) => {
        $(#[$attr])*
        pub fn $f(&self, value: &::std::ffi::CStr) -> io::Result<()> {
            unsafe {
                sys::$ev(self.0, value.as_ptr())
                    .map(drop)
                    .map_err(::macros::convert_error)
            }
        }
    };
    ($(#[$attr:meta])* @set $f:ident($in:ty) = $ev:ident) => {
        $(#[$attr])*
        pub fn $f(&self, value: $in) -> io::Result<()> {
            unsafe {
                sys::$ev(self.0, value as _)
                    .map(drop)
                    .map_err(::macros::convert_error)
            }
        }
    };
    ($({ $($tt:tt)* })*) => {
        $(
            ioctl_impl! {$($tt)*}
        )*
    };
}

macro_rules! impl_bitmasks {
    { $kind:path, $event: expr, $name_mask:ident, $name_mask_set:ident, $name_bits:ident } => {
        /// `EVIOCGMASK`
        pub fn $name_mask(&self) -> io::Result<::bitmask::Bitmask<$kind>> {
            let mut bitmask = ::bitmask::Bitmask::default();
            self.event_mask_raw($event, &mut bitmask).map(|_| bitmask)
        }

        /// `EVIOCSMASK`
        pub fn $name_mask_set(&self, bitmask: &::bitmask::Bitmask<$kind>) -> io::Result<()> {
            self.set_event_mask_raw($event, bitmask)
        }

        /// `EVIOCGBIT`
        pub fn $name_bits(&self) -> io::Result<::bitmask::Bitmask<$kind>> {
            let mut bitmask = ::bitmask::Bitmask::default();
            self.event_bits_raw($event, &mut bitmask).map(|_| bitmask)
        }
    };
}