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
#![feature(generators, generator_trait)]
#![warn(clippy::all)]

mod device;
mod discovered_device;
mod driver;
mod errors;

pub use crate::{device::*, discovered_device::*, driver::*, errors::*};

pub type Result<T> = std::result::Result<T, FPrintError>;

#[derive(Debug, Clone)]
pub struct FPrint;

impl FPrint {
    /// Initialise libfprint.
    ///
    /// To enable debug output of libfprint specifically, use GLib's `G_MESSAGES_DEBUG` environment
    /// variable as explained in Running and debugging GLib Applications.
    ///
    /// The log domains used in `libfprint` are either `libfprint` or `libfprint-FP_COMPONENT` where
    /// `FP_COMPONENT` is defined in the source code for each driver, or component of the library.
    /// Starting with all and trimming down is advised.
    ///
    /// To enable debugging of `libusb`, for USB-based fingerprint reader drivers, use
    /// libusb's `LIBUSB_DEBUG` environment variable as explained in the libusb-1.0 API Reference.
    ///
    /// Example:
    /// ```bash
    /// # LIBUSB_DEBUG=4 G_MESSAGES_DEBUG=all my-libfprint-application
    /// ```
    pub fn new() -> crate::Result<FPrint> {
        let res = unsafe { fprint_sys::fp_init() } as i32;

        if res == 0 {
            Ok(FPrint)
        } else {
            Err(crate::FPrintError::InitError(res))
        }
    }

    /// Scans the system and returns a list of discovered devices. This is your entry point
    /// into finding a fingerprint reader to operate.
    pub fn discover(&self) -> crate::Result<DiscoveredDevices> {
        let devices_list = unsafe { fprint_sys::fp_discover_devs() };
        if devices_list.is_null() {
            Err(crate::FPrintError::NullPtr(
                crate::NullPtrContext::Discovering,
            ))
        } else {
            DiscoveredDevices::new(devices_list)
        }
    }
}

impl Drop for FPrint {
    fn drop(&mut self) {
        unsafe {
            fprint_sys::fp_exit();
        }
    }
}