fractal/contrib/qr_code_scanner/camera/
linux.rs

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// SPDX-License-Identifier: GPL-3.0-or-later
use std::time::Duration;

use ashpd::desktop::camera;
use gtk::{glib, prelude::*, subclass::prelude::*};
use tracing::error;

use super::{
    camera_paintable::{linux::LinuxCameraPaintable, CameraPaintable},
    Camera, CameraImpl,
};
use crate::{spawn_tokio, utils::timeout_future};

mod imp {
    use super::*;

    #[derive(Debug, Default)]
    pub struct LinuxCamera {
        pub paintable: glib::WeakRef<LinuxCameraPaintable>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for LinuxCamera {
        const NAME: &'static str = "LinuxCamera";
        type Type = super::LinuxCamera;
        type ParentType = Camera;
    }

    impl ObjectImpl for LinuxCamera {}

    impl CameraImpl for LinuxCamera {
        async fn has_cameras(&self) -> bool {
            let handle = spawn_tokio!(async move {
                let camera = match camera::Camera::new().await {
                    Ok(camera) => camera,
                    Err(error) => {
                        error!("Could not create instance of camera proxy: {error}");
                        return false;
                    }
                };

                match camera.is_present().await {
                    Ok(is_present) => is_present,
                    Err(error) => {
                        error!("Could not check whether system has cameras: {error}");
                        false
                    }
                }
            });
            let abort_handle = handle.abort_handle();

            match timeout_future(Duration::from_secs(1), handle).await {
                Ok(is_present) => is_present.expect("The task should not have been aborted"),
                Err(_) => {
                    abort_handle.abort();
                    error!("Could not check whether system has cameras: the request timed out");
                    false
                }
            }
        }

        async fn paintable(&self) -> Option<CameraPaintable> {
            // We need to make sure that the Paintable is taken only from the MainContext
            assert!(glib::MainContext::default().is_owner());

            if let Some(paintable) = self.paintable.upgrade() {
                return Some(paintable.upcast());
            }

            let handle = spawn_tokio!(async move { camera::request().await });
            let abort_handle = handle.abort_handle();

            match timeout_future(Duration::from_secs(1), handle).await {
                Ok(tokio_res) => match tokio_res.expect("The task should not have been aborted") {
                    Ok(Some((fd, streams))) => {
                        let paintable = LinuxCameraPaintable::new(fd, streams).await;
                        self.paintable.set(Some(&paintable));

                        Some(paintable.upcast())
                    }
                    Ok(None) => {
                        error!("Could not request access to cameras: the response is empty");
                        None
                    }
                    Err(error) => {
                        error!("Could not request access to cameras: {error}");
                        None
                    }
                },
                Err(_) => {
                    // Error because we reached the timeout.
                    abort_handle.abort();
                    error!("Could not request access to cameras: the request timed out");
                    None
                }
            }
        }
    }
}

glib::wrapper! {
    pub struct LinuxCamera(ObjectSubclass<imp::LinuxCamera>) @extends Camera;
}

impl LinuxCamera {
    /// Create a new `LinuxCamera`.
    pub fn new() -> Self {
        glib::Object::new()
    }
}

impl Default for LinuxCamera {
    fn default() -> Self {
        Self::new()
    }
}

unsafe impl Send for LinuxCamera {}
unsafe impl Sync for LinuxCamera {}