fprint_backend_libfprint/backend.rs
1// SPDX-FileCopyrightText: 2026 fprintd (pure-Rust) contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5//! [`LibfprintBackend`]: an `fprint-core` [`Backend`] over a libfprint `FpContext`.
6//!
7//! The context is the discovery root (libfprint's `FpContext`). Like the `FpContext` it wraps,
8//! this backend is `!Send`.
9
10use fprint_core::{Backend, DeviceId, Error, Result};
11
12use crate::convert;
13use crate::device::LibfprintDevice;
14use crate::ffi::FpContext;
15
16/// Entry point to the C-libfprint-backed devices on this system.
17pub struct LibfprintBackend {
18 ctx: FpContext,
19}
20
21impl core::fmt::Debug for LibfprintBackend {
22 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23 f.debug_struct("LibfprintBackend").finish_non_exhaustive()
24 }
25}
26
27impl LibfprintBackend {
28 /// Create a backend with a fresh libfprint `FpContext`.
29 #[must_use]
30 pub fn new() -> Self {
31 LibfprintBackend {
32 ctx: FpContext::new(),
33 }
34 }
35}
36
37impl Default for LibfprintBackend {
38 fn default() -> Self {
39 Self::new()
40 }
41}
42
43impl Backend for LibfprintBackend {
44 type Device = LibfprintDevice;
45
46 async fn enumerate(&self) -> Result<Vec<LibfprintDevice>> {
47 // Read each device's static getters on this thread, hand the resulting `DeviceInfo` to a
48 // worker (which re-finds the device in its own context), and drop the caller-thread
49 // `FpDevice`s as this Vec goes out of scope — the sensor is owned only by its worker.
50 self.ctx
51 .devices()
52 .into_iter()
53 .map(|dev| LibfprintDevice::spawn(convert::device_info(&dev)))
54 .collect::<Result<Vec<_>>>()
55 }
56
57 /// Locate a reader by id (convenience over `enumerate` + find); the returned device is not
58 /// yet opened — call [`fprint_core::Device::open`] on it.
59 ///
60 /// Matches on libfprint's `device_id`, falling back to the driver id when `device_id` is
61 /// empty, as it is for the virtual debug devices (mirroring how `DeviceInfo` is built).
62 async fn open(&self, id: &DeviceId) -> Result<LibfprintDevice> {
63 for dev in self.ctx.devices() {
64 let device_id = dev.device_id();
65 let hit = if device_id.is_empty() {
66 dev.driver() == id.as_str()
67 } else {
68 device_id == id.as_str()
69 };
70 if hit {
71 return LibfprintDevice::spawn(convert::device_info(&dev));
72 }
73 }
74 Err(Error::NotFound)
75 }
76}