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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use crate::{calls, types::*, utils, Internal, Result};
use core::mem::MaybeUninit;
use getset::CopyGetters;
use std::os::unix::io::RawFd;

impl Internal<QueryCtrl> {
    pub fn query(fd: RawFd, id: u32) -> Result<Self> {
        let ctrl = MaybeUninit::<QueryCtrl>::zeroed();

        let ctrl = unsafe_call!({
            let mut ctrl = ctrl.assume_init();
            ctrl.id = id;
            calls::query_ctrl(fd, &mut ctrl).map(|_| ctrl)
        })?;

        utils::check_str(&ctrl.name)?;

        Ok(ctrl.into())
    }

    pub fn query_next(fd: RawFd, prev_id: u32) -> Result<Option<Self>> {
        Self::query(
            fd,
            prev_id | (CtrlEnumFlag::NextCtrl | CtrlEnumFlag::NextCompound).bits(),
        )
        .map(Some)
        .or_else(|error| {
            if error.kind() == std::io::ErrorKind::InvalidInput {
                Ok(None)
            } else {
                Err(error)
            }
        })
    }
}

trivial_impls! {
    QueryCtrl {
        /// Control name
        getstr name: &str,
    }
}

impl QueryCtrl {
    pub fn is_menu(&self) -> bool {
        self.type_.is_menu()
    }

    pub fn has_payload(&self) -> bool {
        self.flags.contains(CtrlFlag::HasPayload)
    }
}

impl core::fmt::Display for QueryCtrl {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match CtrlId::try_from(self.id()) {
            Ok(id) => id.fmt(f),
            Err(id) => id.fmt(f),
        }?;
        f.write_str(": ")?;
        self.type_().fmt(f)?;
        f.write_str(" '")?;
        self.name().fmt(f)?;
        f.write_str("' ")?;
        if !self.flags().is_none() {
            self.flags().fmt(f)?;
            f.write_str(" ")?;
        }
        f.write_str("(")?;
        self.min().fmt(f)?;
        f.write_str("..=")?;
        self.max().fmt(f)?;
        f.write_str(" step:")?;
        self.step().fmt(f)?;
        f.write_str(" def:")?;
        self.default().fmt(f)?;
        f.write_str(")")?;
        Ok(())
    }
}

impl From<QueryCtrl> for QueryExtCtrl {
    fn from(ctrl: QueryCtrl) -> Self {
        Self {
            id: ctrl.id,
            type_: ctrl.type_,
            name: ctrl.name,
            min: ctrl.min as _,
            max: if matches!(ctrl.type_, CtrlType::BitMask) {
                ctrl.max as u32 as _
            } else {
                ctrl.max as _
            },
            default: if matches!(ctrl.type_, CtrlType::BitMask) {
                ctrl.default as u32 as _
            } else {
                ctrl.default as _
            },
            step: ctrl.step as _,
            flags: ctrl.flags
                | if matches!(ctrl.type_, CtrlType::String) {
                    CtrlFlag::HasPayload
                } else {
                    CtrlFlag::none()
                },
            elems: 1,
            nr_of_dims: 0,
            elem_size: match ctrl.type_ {
                CtrlType::Integer64 => core::mem::size_of::<i64>() as _,
                CtrlType::String => (ctrl.max + 1) as _,
                _ => core::mem::size_of::<i32>() as _,
            },
            dims: [0; CTRL_MAX_DIMS],
            reserved: [0; 32],
        }
    }
}

impl Internal<QueryExtCtrl> {
    pub fn query(fd: RawFd, id: u32) -> Result<Self> {
        let ctrl = MaybeUninit::<QueryExtCtrl>::zeroed();

        let ctrl = unsafe_call!({
            let mut ctrl = ctrl.assume_init();
            ctrl.id = id;
            calls::query_ext_ctrl(fd, &mut ctrl).map(|_| ctrl)
        })?;

        utils::check_str(&ctrl.name)?;

        Ok(ctrl.into())
    }

    pub fn query_next(fd: RawFd, prev_id: u32) -> Result<Option<Self>> {
        Self::query(
            fd,
            prev_id | (CtrlEnumFlag::NextCtrl | CtrlEnumFlag::NextCompound).bits(),
        )
        .map(Some)
        .or_else(|error| {
            if error.kind() == std::io::ErrorKind::InvalidInput {
                Ok(None)
            } else {
                Err(error)
            }
        })
    }

    pub fn query_fallback(fd: RawFd, id: u32) -> Result<Self> {
        Self::query(fd, id).or_else(|error| {
            if error.kind() == std::io::ErrorKind::BrokenPipe {
                Internal::<QueryCtrl>::query(fd, id).map(|fb_ctrl| fb_ctrl.map(From::from))
            } else {
                Err(error)
            }
        })
    }

    pub fn query_next_fallback(fd: RawFd, prev_id: u32) -> Result<Option<Self>> {
        Self::query_fallback(
            fd,
            prev_id | (CtrlEnumFlag::NextCtrl | CtrlEnumFlag::NextCompound).bits(),
        )
        .map(Some)
        .or_else(|error| {
            if error.kind() == std::io::ErrorKind::InvalidInput {
                Ok(None)
            } else {
                Err(error)
            }
        })
    }
}

trivial_impls! {
    QueryExtCtrl {
        /// Control name
        getstr name: &str,
    }
}

impl QueryExtCtrl {
    pub fn is_menu(&self) -> bool {
        self.type_.is_menu()
    }

    pub fn has_payload(&self) -> bool {
        self.flags.contains(CtrlFlag::HasPayload)
    }

    /// The size of each dimension
    pub fn dims(&self) -> &[u32] {
        &self.dims[..=self.nr_of_dims as usize]
    }

    /// Size of value in bytes
    pub fn size(&self) -> u32 {
        self.elem_size * self.elems
    }
}

impl core::fmt::Display for QueryExtCtrl {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match CtrlId::try_from(self.id()) {
            Ok(id) => id.fmt(f),
            Err(id) => id.fmt(f),
        }?;
        f.write_str(": ")?;
        self.type_().fmt(f)?;
        f.write_str(" '")?;
        self.name().fmt(f)?;
        f.write_str("' ")?;
        if !self.flags().is_none() {
            self.flags().fmt(f)?;
            f.write_str(" ")?;
        }
        f.write_str("(")?;
        self.min().fmt(f)?;
        f.write_str("..=")?;
        self.max().fmt(f)?;
        f.write_str(" step:")?;
        self.step().fmt(f)?;
        f.write_str(" def:")?;
        self.default().fmt(f)?;
        f.write_str(")")?;
        Ok(())
    }
}

impl QueryMenu {
    pub fn id(&self) -> u32 {
        self.id
    }

    pub fn index(&self) -> u32 {
        self.index
    }
}

impl Internal<QueryMenu> {
    pub fn query(fd: RawFd, id: u32, index: u32) -> Result<Option<Self>> {
        let mut menu = unsafe {
            QueryMenu {
                id,
                index,
                ..core::mem::zeroed()
            }
        };

        unsafe_call!(calls::query_menu(fd, &mut menu as *mut _))
            .map(|_| Some(Internal(menu)))
            .or_else(|error| {
                if error.kind() == std::io::ErrorKind::InvalidInput {
                    Ok(None)
                } else {
                    Err(error)
                }
            })
    }

    pub fn into_item(self, type_: CtrlType, index: u32) -> MenuItem {
        MenuItem {
            item: self,
            type_,
            index,
        }
    }
}

/// Helper type to represent menu item
#[derive(Clone, Copy, CopyGetters)]
pub struct MenuItem {
    /// Control type
    #[getset(get_copy = "pub")]
    type_: CtrlType,

    /// Item index
    #[getset(get_copy = "pub")]
    index: u32,

    item: Internal<QueryMenu>,
}

impl Internal<MenuItem> {
    pub fn query(
        fd: RawFd,
        ctrl_type: CtrlType,
        ctrl_id: u32,
        index: u32,
    ) -> Result<Option<Internal<MenuItem>>> {
        Internal::<QueryMenu>::query(fd, ctrl_id, index)
            .map(|ok| ok.map(|item| item.into_item(ctrl_type, index).into()))
    }
}

impl MenuItem {
    pub fn name(&self) -> Option<&str> {
        if matches!(self.type_, CtrlType::Menu) {
            utils::get_str(unsafe { &self.item.union_.name }).ok()
        } else {
            None
        }
    }

    pub fn value(&self) -> Option<i64> {
        if matches!(self.type_, CtrlType::IntegerMenu) {
            Some(unsafe { self.item.union_.value })
        } else {
            None
        }
    }
}

impl core::fmt::Display for MenuItem {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.index().fmt(f)?;
        f.write_str(": ")?;
        if let Some(name) = self.name() {
            f.write_str("'")?;
            name.fmt(f)?;
            f.write_str("'")?;
        } else if let Some(value) = self.value() {
            value.fmt(f)?;
        }
        Ok(())
    }
}