Skip to main content

thedes_tui_core/runtime/device/
native.rs

1use crossterm::terminal;
2
3use crate::{
4    audio::{self, device::AudioDevice},
5    input::{self, device::InputDevice},
6    panic,
7    screen::{self, device::ScreenDevice},
8};
9
10use super::{Error, PanicRestoreGuard, RuntimeDevice};
11
12pub fn open() -> Box<dyn RuntimeDevice> {
13    Box::new(NativeRuntimeDevice::new())
14}
15
16#[derive(Debug)]
17struct NativeRuntimeDevice {
18    initialized: bool,
19}
20
21impl NativeRuntimeDevice {
22    pub fn new() -> Self {
23        Self { initialized: false }
24    }
25}
26
27impl RuntimeDevice for NativeRuntimeDevice {
28    fn blocking_init(&mut self) -> Result<(), Error> {
29        if self.initialized {
30            Err(Error::AlreadyInit)?
31        }
32        terminal::enable_raw_mode()?;
33        self.initialized = true;
34        Ok(())
35    }
36
37    fn blocking_shutdown(&mut self) -> Result<(), Error> {
38        if !self.initialized {
39            Err(Error::NotInit)?
40        }
41        terminal::disable_raw_mode()?;
42        self.initialized = false;
43        Ok(())
44    }
45
46    fn open_input_device(&mut self) -> Box<dyn InputDevice> {
47        input::device::native::open()
48    }
49
50    fn open_screen_device(&mut self) -> Box<dyn ScreenDevice> {
51        screen::device::native::open()
52    }
53
54    fn open_audio_device(&mut self) -> Box<dyn AudioDevice> {
55        audio::device::native::open()
56    }
57
58    fn open_panic_restore_guard(&mut self) -> Box<dyn PanicRestoreGuard> {
59        panic::restore::native::open()
60    }
61}