sane-sys 0.1.0

Raw FFI bindings to libsane, the SANE (Scanner Access Now Easy) scanner API, generated with bindgen and linked through pkg-config
Documentation
//! Raw FFI bindings to libsane, the library of
//! [SANE (Scanner Access Now Easy)](http://www.sane-project.org/).
//!
//! The bindings are generated by bindgen from the system `sane/sane.h` at
//! build time, and libsane is linked through pkg-config (`sane-backends`).
//! For a safe API, use the [`libsane`](https://docs.rs/libsane) crate.
//!
//! C enums (`SANE_Status`, `SANE_Frame`, ...) are type aliases of integers
//! with one constant per value, because backends may return values the header
//! does not list. The function-like macros of `sane.h` are provided as
//! functions: [`SANE_VERSION_CODE`], [`SANE_VERSION_MAJOR`],
//! [`SANE_VERSION_MINOR`], [`SANE_VERSION_BUILD`], [`SANE_FIX`],
//! [`SANE_UNFIX`], [`SANE_OPTION_IS_ACTIVE`] and [`SANE_OPTION_IS_SETTABLE`].
//!
//! # System requirements
//!
//! The SANE development files (`libsane-dev` on Debian and Ubuntu,
//! `sane-backends-devel` on Fedora, `sane-backends` on Homebrew), pkg-config,
//! and libclang for bindgen.

#![no_std]
#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
#![allow(clippy::all)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

/// The lamp is not ready, retry later.
///
/// Not declared by `sane.h` (it sits behind `#if 0` there), but returned by
/// some backends.
pub const SANE_STATUS_WARMING_UP: SANE_Status = 12;
/// The scanner mechanism is locked for transport.
///
/// Not declared by `sane.h` (it sits behind `#if 0` there), but returned by
/// some backends.
pub const SANE_STATUS_HW_LOCKED: SANE_Status = 13;

/// Builds a version code from its parts (`SANE_VERSION_CODE`).
#[inline]
pub const fn SANE_VERSION_CODE(major: SANE_Word, minor: SANE_Word, build: SANE_Word) -> SANE_Word {
    ((major & 0xff) << 24) | ((minor & 0xff) << 16) | (build & 0xffff)
}

/// The major version of a version code (`SANE_VERSION_MAJOR`).
#[inline]
pub const fn SANE_VERSION_MAJOR(code: SANE_Word) -> SANE_Word {
    (code >> 24) & 0xff
}

/// The minor version of a version code (`SANE_VERSION_MINOR`).
#[inline]
pub const fn SANE_VERSION_MINOR(code: SANE_Word) -> SANE_Word {
    (code >> 16) & 0xff
}

/// The build number of a version code (`SANE_VERSION_BUILD`).
#[inline]
pub const fn SANE_VERSION_BUILD(code: SANE_Word) -> SANE_Word {
    code & 0xffff
}

/// Converts a floating point number to `SANE_Fixed` (`SANE_FIX`).
///
/// Truncates toward zero like the C macro, and saturates when `value` is out
/// of range.
#[inline]
pub fn SANE_FIX(value: f64) -> SANE_Fixed {
    (value * (1 << SANE_FIXED_SCALE_SHIFT) as f64) as SANE_Fixed
}

/// Converts a `SANE_Fixed` to a floating point number (`SANE_UNFIX`).
#[inline]
pub fn SANE_UNFIX(value: SANE_Fixed) -> f64 {
    value as f64 / (1 << SANE_FIXED_SCALE_SHIFT) as f64
}

/// Whether an option with these capabilities is active
/// (`SANE_OPTION_IS_ACTIVE`).
#[inline]
pub const fn SANE_OPTION_IS_ACTIVE(cap: SANE_Int) -> bool {
    (cap & SANE_CAP_INACTIVE as SANE_Int) == 0
}

/// Whether an option with these capabilities can be set by software
/// (`SANE_OPTION_IS_SETTABLE`).
#[inline]
pub const fn SANE_OPTION_IS_SETTABLE(cap: SANE_Int) -> bool {
    (cap & SANE_CAP_SOFT_SELECT as SANE_Int) != 0
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn version_code_round_trips() {
        let code = SANE_VERSION_CODE(1, 3, 1);
        assert_eq!(SANE_VERSION_MAJOR(code), 1);
        assert_eq!(SANE_VERSION_MINOR(code), 3);
        assert_eq!(SANE_VERSION_BUILD(code), 1);
    }

    #[test]
    fn fixed_matches_the_c_macros() {
        assert_eq!(SANE_FIX(1.0), 1 << 16);
        assert_eq!(SANE_FIX(-2.5), -(5 << 15));
        // Truncation toward zero, like `(SANE_Word) (v * (1 << 16))`.
        assert_eq!(SANE_FIX(0.1), 6553);
        assert_eq!(SANE_UNFIX(1 << 15), 0.5);
    }

    #[test]
    fn capability_helpers() {
        let cap = (SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT) as SANE_Int;
        assert!(SANE_OPTION_IS_ACTIVE(cap));
        assert!(SANE_OPTION_IS_SETTABLE(cap));
        assert!(!SANE_OPTION_IS_ACTIVE(cap | SANE_CAP_INACTIVE as SANE_Int));
        assert!(!SANE_OPTION_IS_SETTABLE(SANE_CAP_HARD_SELECT as SANE_Int));
    }
}