Skip to main content

cadmus_core/
context.rs

1use crate::battery::Battery;
2use crate::db::Database;
3use crate::device::CURRENT_DEVICE;
4use crate::dictionary::{load_dictionary_from_db, Dictionary};
5use crate::font::Fonts;
6use crate::framebuffer::{Display, Framebuffer};
7use crate::frontlight::Frontlight;
8use crate::geom::Rectangle;
9use crate::helpers::{load_json, Fingerprint, Fp, IsHidden};
10use crate::library::Library;
11use crate::lightsensor::LightSensor;
12use crate::rtc::{AlarmManager, Rtc};
13use crate::settings::Settings;
14use crate::view::keyboard::Layout;
15use crate::view::ViewId;
16use chrono::Local;
17use fxhash::FxHashMap;
18use globset::Glob;
19use rand_core::SeedableRng;
20use rand_xoshiro::Xoroshiro128Plus;
21use std::collections::{BTreeMap, VecDeque};
22#[cfg(test)]
23use std::env;
24use std::io;
25use std::path::Path;
26use tracing::error;
27
28use walkdir::WalkDir;
29
30const KEYBOARD_LAYOUTS_DIRNAME: &str = "keyboard-layouts";
31pub(crate) const DICTIONARIES_DIRNAME: &str = "dictionaries";
32const INPUT_HISTORY_SIZE: usize = 32;
33
34pub struct Context {
35    pub fb: Box<dyn Framebuffer>,
36    pub alarm_manager: Option<AlarmManager>,
37    pub display: Display,
38    pub settings: Settings,
39    pub library: Library,
40    pub database: Database,
41    pub fonts: Fonts,
42    pub dictionaries: BTreeMap<String, Dictionary>,
43    pub keyboard_layouts: BTreeMap<String, Layout>,
44    pub input_history: FxHashMap<ViewId, VecDeque<String>>,
45    pub frontlight: Box<dyn Frontlight>,
46    pub battery: Box<dyn Battery>,
47    pub lightsensor: Box<dyn LightSensor>,
48    pub notification_index: u8,
49    pub kb_rect: Rectangle,
50    pub rng: Xoroshiro128Plus,
51    pub plugged: bool,
52    pub covered: bool,
53    pub shared: bool,
54    pub online: bool,
55}
56
57impl Context {
58    pub fn new(
59        fb: Box<dyn Framebuffer>,
60        rtc: Option<Rtc>,
61        library: Library,
62        database: Database,
63        settings: Settings,
64        fonts: Fonts,
65        battery: Box<dyn Battery>,
66        frontlight: Box<dyn Frontlight>,
67        lightsensor: Box<dyn LightSensor>,
68    ) -> Context {
69        let dims = fb.dims();
70        let rotation = CURRENT_DEVICE.transformed_rotation(fb.rotation());
71        let rng = Xoroshiro128Plus::seed_from_u64(Local::now().timestamp_subsec_nanos() as u64);
72        let alarm_manager = rtc.map(AlarmManager::new);
73        Context {
74            fb,
75            alarm_manager,
76            display: Display { dims, rotation },
77            library,
78            database,
79            settings,
80            fonts,
81            dictionaries: BTreeMap::new(),
82            keyboard_layouts: BTreeMap::new(),
83            input_history: FxHashMap::default(),
84            battery,
85            frontlight,
86            lightsensor,
87            notification_index: 0,
88            kb_rect: Rectangle::default(),
89            rng,
90            plugged: false,
91            covered: false,
92            shared: false,
93            online: false,
94        }
95    }
96
97    pub fn load_keyboard_layouts(&mut self) {
98        let glob = Glob::new("**/*.json").unwrap().compile_matcher();
99
100        #[cfg(test)]
101        let path = Path::new(
102            &env::var("TEST_ROOT_DIR")
103                .expect("TEST_ROOT_DIR must be set for test using keyboard layouts"),
104        )
105        .join(KEYBOARD_LAYOUTS_DIRNAME);
106
107        #[cfg(not(test))]
108        let path = Path::new(KEYBOARD_LAYOUTS_DIRNAME);
109
110        for entry in WalkDir::new(path)
111            .min_depth(1)
112            .into_iter()
113            .filter_entry(|e| !e.is_hidden())
114        {
115            if entry.is_err() {
116                continue;
117            }
118            let entry = entry.unwrap();
119            let path = entry.path();
120            if !glob.is_match(path) {
121                continue;
122            }
123            if let Ok(layout) = load_json::<Layout, _>(path)
124                .map_err(|e| error!("Can't load {}: {:#?}.", path.display(), e))
125            {
126                self.keyboard_layouts.insert(layout.name.clone(), layout);
127            }
128        }
129    }
130
131    #[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
132    pub fn load_dictionaries(&mut self) {
133        self.dictionaries.clear();
134
135        let glob = Glob::new("**/*.index").unwrap().compile_matcher();
136
137        #[cfg(test)]
138        let path = Path::new(
139            env::var("TEST_ROOT_DIR")
140                .expect("Please set TEST_ROOT_DIR for tests that need dictionaries")
141                .as_str(),
142        )
143        .join(DICTIONARIES_DIRNAME);
144
145        #[cfg(not(test))]
146        let path = Path::new(DICTIONARIES_DIRNAME);
147
148        for entry in WalkDir::new(path)
149            .min_depth(1)
150            .into_iter()
151            .filter_entry(|e| !e.is_hidden())
152        {
153            if entry.is_err() {
154                continue;
155            }
156            let entry = entry.unwrap();
157            if !glob.is_match(entry.path()) {
158                continue;
159            }
160            let index_path = entry.path().to_path_buf();
161            let mut content_path = index_path.clone();
162            content_path.set_extension("dict.dz");
163            if !content_path.exists() {
164                content_path.set_extension("");
165            }
166
167            let dict_result = match fingerprint_dict_pair(&index_path) {
168                Ok(fp) => load_dictionary_from_db(&content_path, &self.database, fp),
169                Err(e) => {
170                    tracing::warn!(
171                        path = %index_path.display(),
172                        error = %e,
173                        "failed to fingerprint index file, skipping dictionary"
174                    );
175                    continue;
176                }
177            };
178
179            if let Ok(mut dict) = dict_result {
180                let name = dict.short_name().ok().unwrap_or_else(|| {
181                    index_path
182                        .file_stem()
183                        .map(|s| s.to_string_lossy().into_owned())
184                        .unwrap_or_default()
185                });
186                self.dictionaries.insert(name, dict);
187            }
188        }
189    }
190
191    pub fn record_input(&mut self, text: &str, id: ViewId) {
192        if text.is_empty() {
193            return;
194        }
195
196        let history = self.input_history.entry(id).or_insert_with(VecDeque::new);
197
198        if history.front().map(String::as_str) != Some(text) {
199            history.push_front(text.to_string());
200        }
201
202        if history.len() > INPUT_HISTORY_SIZE {
203            history.pop_back();
204        }
205    }
206
207    pub fn set_frontlight(&mut self, enable: bool) {
208        self.settings.frontlight = enable;
209
210        if enable {
211            let levels = self.settings.frontlight_levels;
212            self.frontlight.set_warmth(levels.warmth);
213            self.frontlight.set_intensity(levels.intensity);
214        } else {
215            self.settings.frontlight_levels = self.frontlight.levels();
216            self.frontlight.set_intensity(0.0);
217            self.frontlight.set_warmth(0.0);
218        }
219    }
220}
221
222/// Fingerprints a StarDict dictionary pair by hashing only the `.index` file.
223///
224/// The `.index` and `.dict` files in a StarDict pair are always installed and
225/// replaced together, so hashing the `.index` alone is sufficient to detect
226/// any change to either file.
227fn fingerprint_dict_pair(index_path: &Path) -> io::Result<Fp> {
228    index_path.fingerprint()
229}
230
231#[cfg(test)]
232pub mod test_helpers {
233    use super::*;
234    use crate::battery::FakeBattery;
235    use crate::db::Database;
236    use crate::framebuffer::Pixmap;
237    use crate::frontlight::LightLevels;
238
239    pub fn create_test_context() -> Context {
240        let database = Database::new(":memory:").expect("failed to create in-memory database");
241        database.migrate().expect("failed to run migrations");
242        Context::new(
243            Box::new(Pixmap::new(600, 800, 1)),
244            None,
245            Library::new(Path::new("/tmp"), &database, "test").unwrap(),
246            database,
247            Settings::default(),
248            Fonts::load_from(
249                Path::new(
250                    &env::var("TEST_ROOT_DIR").expect("TEST_ROOT_DIR must be set for this test."),
251                )
252                .to_path_buf(),
253            )
254            .expect("Failed to load fonts"),
255            Box::new(FakeBattery::new()),
256            Box::new(LightLevels::default()),
257            Box::new(0u16),
258        )
259    }
260}