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
use bitflags::bitflags;
use std::convert::{TryFrom, TryInto};
use std::{fmt, mem, str};

use crate::v4l_sys::*;

/// Control data type
#[allow(clippy::unreadable_literal)]
#[rustfmt::skip]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Type {
    Integer         = 1,
    Boolean         = 2,
    Menu            = 3,
    Button          = 4,
    Integer64       = 5,
    CtrlClass       = 6,
    String          = 7,
    Bitmask         = 8,
    IntegerMenu     = 9,

    /* Compound types are >= 0x0100 */
    CompoundTypes   = 0x0100,
    U8              = 0x1000,
    U16             = 0x0101,
    U32             = 0x0102,
    Area            = 0x0106,
}

impl TryFrom<u32> for Type {
    type Error = ();

    fn try_from(repr: u32) -> Result<Self, Self::Error> {
        match repr {
            1 => Ok(Type::Integer),
            2 => Ok(Type::Boolean),
            3 => Ok(Type::Menu),
            4 => Ok(Type::Button),
            5 => Ok(Type::Integer64),
            6 => Ok(Type::CtrlClass),
            7 => Ok(Type::String),
            8 => Ok(Type::Bitmask),
            9 => Ok(Type::IntegerMenu),

            0x0100 => Ok(Type::U8),
            0x0101 => Ok(Type::U16),
            0x0102 => Ok(Type::U32),
            0x0106 => Ok(Type::Area),
            _ => Err(()),
        }
    }
}

impl Into<u32> for Type {
    fn into(self) -> u32 {
        self as u32
    }
}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

bitflags! {
    #[allow(clippy::unreadable_literal)]
    pub struct Flags: u32 {
        const DISABLED              = 0x0001;
        const GRABBED               = 0x0002;
        const READ_ONLY             = 0x0004;
        const UPDATE                = 0x0008;
        const INACTIVE              = 0x0010;
        const SLIDER                = 0x0020;
        const WRITE_ONLY            = 0x0040;
        const VOLATILE              = 0x0080;
        const HAS_PAYLOAD           = 0x0100;
        const EXECUTE_ON_WRITE      = 0x0200;
        const MODIFY_LAYOUT         = 0x0400;

        const NEXT_CTRL             = 0x80000000;
        const NEXT_COMPOUND         = 0x40000000;
    }
}

impl From<u32> for Flags {
    fn from(flags: u32) -> Self {
        Flags::from_bits_truncate(flags)
    }
}

impl Into<u32> for Flags {
    fn into(self) -> u32 {
        self.bits()
    }
}

impl fmt::Display for Flags {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[derive(Debug)]
/// Device control menu item
pub enum MenuItem {
    Name(String),
    Value(i64),
}

impl fmt::Display for MenuItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MenuItem::Name(name) => {
                write!(f, "{}", name)?;
            }
            MenuItem::Value(value) => {
                write!(f, "{}", value)?;
            }
        }
        Ok(())
    }
}

impl TryFrom<(Type, v4l2_querymenu)> for MenuItem {
    type Error = ();

    fn try_from(item: (Type, v4l2_querymenu)) -> Result<Self, Self::Error> {
        unsafe {
            match item.0 {
                Type::Menu => Ok(MenuItem::Name(
                    str::from_utf8(&item.1.__bindgen_anon_1.name)
                        .unwrap()
                        .trim_matches(char::from(0))
                        .to_string(),
                )),
                Type::IntegerMenu => Ok(MenuItem::Value(item.1.__bindgen_anon_1.value)),
                _ => Err(()),
            }
        }
    }
}

#[derive(Debug)]
/// Device control description
pub struct Description {
    /// Control identifier, set by the the application
    pub id: u32,
    /// Type of control
    pub typ: Type,
    /// Name of the control, intended for the user
    pub name: String,
    /// Minimum value, inclusive
    pub minimum: i32,
    /// Maximum value, inclusive
    pub maximum: i32,
    /// Step size, always positive
    pub step: i32,
    /// Default value
    pub default: i32,
    /// Control flags
    pub flags: Flags,

    /// Items for menu controls (only valid if typ is a menu type)
    pub items: Option<Vec<(u32, MenuItem)>>,
}

impl From<v4l2_queryctrl> for Description {
    fn from(ctrl: v4l2_queryctrl) -> Self {
        Description {
            id: ctrl.id,
            typ: Type::try_from(ctrl.type_).unwrap(),
            name: str::from_utf8(&ctrl.name)
                .unwrap()
                .trim_matches(char::from(0))
                .to_string(),
            minimum: ctrl.minimum,
            maximum: ctrl.maximum,
            step: ctrl.step,
            default: ctrl.default_value,
            flags: Flags::from(ctrl.flags),
            items: None,
        }
    }
}

impl fmt::Display for Description {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "ID         : {}", self.id)?;
        writeln!(f, "Type       : {}", self.typ)?;
        writeln!(f, "Name       : {}", self.name)?;
        writeln!(f, "Minimum    : {}", self.minimum)?;
        writeln!(f, "Maximum    : {}", self.maximum)?;
        writeln!(f, "Step       : {}", self.step)?;
        writeln!(f, "Default    : {}", self.default)?;
        writeln!(f, "Flags      : {}", self.flags)?;
        if let Some(items) = &self.items {
            writeln!(f, "Menu ==>")?;
            for item in items {
                writeln!(f, " * {}", item.1)?;
            }
        }
        Ok(())
    }
}

#[derive(Debug)]
/// Device control value
pub enum Control {
    /* single values */
    Value(i32),
    Value64(i64),
    String(String),
}

impl From<v4l2_control> for Control {
    fn from(ctrl: v4l2_control) -> Self {
        Control::Value(ctrl.value)
    }
}

impl TryInto<v4l2_control> for Control {
    type Error = ();

    fn try_into(self) -> Result<v4l2_control, Self::Error> {
        unsafe {
            let mut ctrl: v4l2_control = mem::zeroed();
            match self {
                Control::Value(val) => {
                    ctrl.value = val;
                    Ok(ctrl)
                }
                _ => Err(()),
            }
        }
    }
}