Skip to main content

sane_sys/
lib.rs

1//! Raw FFI bindings to libsane, the library of
2//! [SANE (Scanner Access Now Easy)](http://www.sane-project.org/).
3//!
4//! The bindings are generated by bindgen from the system `sane/sane.h` at
5//! build time, and libsane is linked through pkg-config (`sane-backends`).
6//! For a safe API, use the [`libsane`](https://docs.rs/libsane) crate.
7//!
8//! C enums (`SANE_Status`, `SANE_Frame`, ...) are type aliases of integers
9//! with one constant per value, because backends may return values the header
10//! does not list. The function-like macros of `sane.h` are provided as
11//! functions: [`SANE_VERSION_CODE`], [`SANE_VERSION_MAJOR`],
12//! [`SANE_VERSION_MINOR`], [`SANE_VERSION_BUILD`], [`SANE_FIX`],
13//! [`SANE_UNFIX`], [`SANE_OPTION_IS_ACTIVE`] and [`SANE_OPTION_IS_SETTABLE`].
14//!
15//! # System requirements
16//!
17//! The SANE development files (`libsane-dev` on Debian and Ubuntu,
18//! `sane-backends-devel` on Fedora, `sane-backends` on Homebrew), pkg-config,
19//! and libclang for bindgen.
20
21#![no_std]
22#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
23#![allow(clippy::all)]
24
25include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
26
27/// The lamp is not ready, retry later.
28///
29/// Not declared by `sane.h` (it sits behind `#if 0` there), but returned by
30/// some backends.
31pub const SANE_STATUS_WARMING_UP: SANE_Status = 12;
32/// The scanner mechanism is locked for transport.
33///
34/// Not declared by `sane.h` (it sits behind `#if 0` there), but returned by
35/// some backends.
36pub const SANE_STATUS_HW_LOCKED: SANE_Status = 13;
37
38/// Builds a version code from its parts (`SANE_VERSION_CODE`).
39#[inline]
40pub const fn SANE_VERSION_CODE(major: SANE_Word, minor: SANE_Word, build: SANE_Word) -> SANE_Word {
41    ((major & 0xff) << 24) | ((minor & 0xff) << 16) | (build & 0xffff)
42}
43
44/// The major version of a version code (`SANE_VERSION_MAJOR`).
45#[inline]
46pub const fn SANE_VERSION_MAJOR(code: SANE_Word) -> SANE_Word {
47    (code >> 24) & 0xff
48}
49
50/// The minor version of a version code (`SANE_VERSION_MINOR`).
51#[inline]
52pub const fn SANE_VERSION_MINOR(code: SANE_Word) -> SANE_Word {
53    (code >> 16) & 0xff
54}
55
56/// The build number of a version code (`SANE_VERSION_BUILD`).
57#[inline]
58pub const fn SANE_VERSION_BUILD(code: SANE_Word) -> SANE_Word {
59    code & 0xffff
60}
61
62/// Converts a floating point number to `SANE_Fixed` (`SANE_FIX`).
63///
64/// Truncates toward zero like the C macro, and saturates when `value` is out
65/// of range.
66#[inline]
67pub fn SANE_FIX(value: f64) -> SANE_Fixed {
68    (value * (1 << SANE_FIXED_SCALE_SHIFT) as f64) as SANE_Fixed
69}
70
71/// Converts a `SANE_Fixed` to a floating point number (`SANE_UNFIX`).
72#[inline]
73pub fn SANE_UNFIX(value: SANE_Fixed) -> f64 {
74    value as f64 / (1 << SANE_FIXED_SCALE_SHIFT) as f64
75}
76
77/// Whether an option with these capabilities is active
78/// (`SANE_OPTION_IS_ACTIVE`).
79#[inline]
80pub const fn SANE_OPTION_IS_ACTIVE(cap: SANE_Int) -> bool {
81    (cap & SANE_CAP_INACTIVE as SANE_Int) == 0
82}
83
84/// Whether an option with these capabilities can be set by software
85/// (`SANE_OPTION_IS_SETTABLE`).
86#[inline]
87pub const fn SANE_OPTION_IS_SETTABLE(cap: SANE_Int) -> bool {
88    (cap & SANE_CAP_SOFT_SELECT as SANE_Int) != 0
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn version_code_round_trips() {
97        let code = SANE_VERSION_CODE(1, 3, 1);
98        assert_eq!(SANE_VERSION_MAJOR(code), 1);
99        assert_eq!(SANE_VERSION_MINOR(code), 3);
100        assert_eq!(SANE_VERSION_BUILD(code), 1);
101    }
102
103    #[test]
104    fn fixed_matches_the_c_macros() {
105        assert_eq!(SANE_FIX(1.0), 1 << 16);
106        assert_eq!(SANE_FIX(-2.5), -(5 << 15));
107        // Truncation toward zero, like `(SANE_Word) (v * (1 << 16))`.
108        assert_eq!(SANE_FIX(0.1), 6553);
109        assert_eq!(SANE_UNFIX(1 << 15), 0.5);
110    }
111
112    #[test]
113    fn capability_helpers() {
114        let cap = (SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT) as SANE_Int;
115        assert!(SANE_OPTION_IS_ACTIVE(cap));
116        assert!(SANE_OPTION_IS_SETTABLE(cap));
117        assert!(!SANE_OPTION_IS_ACTIVE(cap | SANE_CAP_INACTIVE as SANE_Int));
118        assert!(!SANE_OPTION_IS_SETTABLE(SANE_CAP_HARD_SELECT as SANE_Int));
119    }
120}