-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.rs
More file actions
122 lines (108 loc) · 2.74 KB
/
mod.rs
File metadata and controls
122 lines (108 loc) · 2.74 KB
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
120
121
122
use crate::alloc::boxed::Box;
use crate::proc::signal::SignalType;
use crate::spin::KMutex;
use crate::syscalls::signal::sys_signal;
use crate::utils::queue::Queue;
use crate::vga_buffer::WRITER;
mod input;
pub use input::{Input, Termcaps};
mod commands;
pub use commands::Command;
use crate::keyboard::SpecialKeyFlag;
use crate::vga_buffer::Screen;
pub const NB_SCREEN: u8 = 3;
pub static INPUT_BUFFER: KMutex<Option<Queue<(Input, u8)>>> = KMutex::new(None);
#[derive(Clone, Default)]
pub struct TermEmu {
screens: [Screen; NB_SCREEN as usize],
current_screen: i8
}
const SCREEN: Screen = Screen::new();
impl TermEmu {
pub const fn new() -> Self {
TermEmu {
screens: [SCREEN; NB_SCREEN as usize],
current_screen: 0
}
}
pub fn handle_event(&mut self, event: Input, spkey: u8) {
match event {
Input::Ascii(value) => {
self.new_char(value, spkey);
},
Input::Tcaps(value) => {
self.new_tcaps(value, spkey);
},
_ => {}
}
}
fn new_char(&mut self, value: char, spkey: u8) {
if spkey & (1 << SpecialKeyFlag::Ctrl as u8) != 0 {
if value.is_ascii_digit() {
self.change_screen(value.to_digit(10).unwrap() as i8 - 1);
}
} else {
self.screens[self.current_screen as usize]
.get_command()
.handle(value);
}
}
fn new_tcaps(&mut self, event: Termcaps, _spkey: u8) {
match event {
Termcaps::ArrowLEFT => {
if self.screens[self.current_screen as usize]
.get_command()
.index != 0
{
self.screens[self.current_screen as usize]
.get_command()
.index -= 1;
WRITER.lock().move_cursor(-1);
}
},
Termcaps::ArrowRIGHT => {
if self.screens[self.current_screen as usize]
.get_command()
.index != self.screens[self.current_screen as usize]
.get_command()
.len()
{
self.screens[self.current_screen as usize]
.get_command()
.index += 1;
WRITER.lock().move_cursor(1);
}
},
_ => {}
}
}
pub fn change_screen(&mut self, id: i8) {
if id >= 0 && id < NB_SCREEN as i8 {
let mut guard = WRITER.lock();
guard.save(&mut self.screens[self.current_screen as usize]);
guard.render(&mut self.screens[id as usize]);
self.current_screen = id;
}
}
}
pub static mut LOCK_CMD: bool = false;
// signal handler for unlocking cmd after init
fn unlock_cmd(_no: i32) {
unsafe { LOCK_CMD = false };
}
pub fn cli() {
let mut emulator: Box<TermEmu> = Box::default();
*INPUT_BUFFER.lock() = Some(Queue::new());
unsafe { LOCK_CMD = false };
sys_signal(SignalType::SigHup as i32, unlock_cmd);
loop {
if INPUT_BUFFER.lock().as_ref().unwrap().is_empty() {
unsafe {
crate::wrappers::hlt!();
}
} else {
let event = INPUT_BUFFER.lock().as_mut().unwrap().pop();
emulator.handle_event(event.0, event.1);
}
}
}