Module controls

Source
Expand description

Safe definitions around V4L2 extended controls.

Extended controls are represented using the [SafeExtControl] type, which is a transparent wrapper for v4l2_ext_control. It takes a generic parameter defining the actual control to use, which limits its API to the methods safe to use for that control type.

A mutable reference to a single [SafeExtControl] can be passed to ioctl methods such as crate::ioctl::g_ext_ctrls to get or set the value of that control only. Setting more than one control at the same time requires to pass a type implementing AsV4l2ControlSlice, that returns a slice if the v4l2_ext_controls to manipulate.

Since SafeExtControl is a transparent wrapper around v4l2_ext_control, an array of it can safely implement AsV4l2ControlSlice. Or, more conveniently, a #[repr(C)] type containing only SafeExtControls:

#[repr(C)]
struct Controls {
    brightness: SafeExtControl<Brightness>,
    contrast: SafeExtControl<Contrast>,
}

impl AsV4l2ControlSlice for &mut Controls {
    fn as_v4l2_control_slice(&mut self) -> &mut [v4l2_ext_control] {
        let ptr = (*self) as *mut Controls as *mut v4l2_ext_control;
        unsafe { std::slice::from_raw_parts_mut(ptr, 2) }
    }
}

let mut controls = Controls {
    brightness: SafeExtControl::<Brightness>::from_value(128),
    contrast: SafeExtControl::<Contrast>::from_value(128),
};

s_ext_ctrls(&device, CtrlWhich::Current, &mut controls).unwrap();
assert_eq!(controls.brightness.value(), 128);
assert_eq!(controls.contrast.value(), 128);

Due to the use of repr(C), the Controls type has the same layout as an array of v4l2_ext_controls and thus can be passed to s_ext_ctrls safely.

Sub-modules contain the type definitions for each control, organized by control class. Due to the large number of controls they are not all defined, so please add those you need if they are missing.

Modules§

codec
Definition of CODEC class controls.
user
Definition of USER class controls.

Structs§

SafeExtControl
Memory-safe v4l2_ext_control.

Traits§

AsV4l2ControlSlice
Trait implemented by types that can be passed to the g/s/try_ext_ctrls family of functions.
ExtControlTrait
Trait implemented by types representing a given control in order to define its properties and set of available methods.