Sync computers
This commit is contained in:
67
src/drivers/keyboard.rs
Normal file
67
src/drivers/keyboard.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use crate::{
|
||||
keymap::{KeyType, ModifierType, map_keycode},
|
||||
tty::TTY0,
|
||||
virtio::{
|
||||
Virtqueue,
|
||||
input::{EventCodeValue, VirtioInputEvent, VirtioPciDriver},
|
||||
},
|
||||
virtual_fs::{FILE_SYSTEM, VirtualFileSystem},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct KeyboardState {
|
||||
// ctrl_modifier: bool,
|
||||
pub shift_modifier: bool,
|
||||
pub alt_gr_modifier: bool,
|
||||
}
|
||||
|
||||
impl KeyboardState {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
// ctrl_modifier: false,
|
||||
shift_modifier: false,
|
||||
alt_gr_modifier: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static mut KBD_QUEUE: Virtqueue = unsafe { core::mem::zeroed() };
|
||||
pub static mut KBD_DRIVER: VirtioPciDriver<KeyboardState> = unsafe {
|
||||
VirtioPciDriver::new(
|
||||
|state, event| {
|
||||
let mut kbd_buffer = FILE_SYSTEM.open("/dev/input/keyboard".as_ref()).unwrap();
|
||||
kbd_buffer
|
||||
.write(core::mem::transmute::<
|
||||
&VirtioInputEvent,
|
||||
&[u8; size_of::<VirtioInputEvent>()],
|
||||
>(event))
|
||||
.unwrap();
|
||||
|
||||
if event.is_key() {
|
||||
let event = event.as_key_event();
|
||||
#[allow(clippy::single_match)]
|
||||
match map_keycode(event.code, state) {
|
||||
KeyType::Ascii(c) if event.value == EventCodeValue::Pressed => {
|
||||
let mut buf = [0; 4];
|
||||
let to_send = c.encode_utf8(&mut buf);
|
||||
for c in to_send.as_bytes() {
|
||||
TTY0.buffer.borrow_mut().push(*c);
|
||||
}
|
||||
}
|
||||
KeyType::Modifier(ModifierType::Shift) => {
|
||||
state.shift_modifier = !state.shift_modifier;
|
||||
}
|
||||
KeyType::Modifier(ModifierType::AltGr) => {
|
||||
state.alt_gr_modifier = !state.alt_gr_modifier;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
// println!("event: {:#?}", event);
|
||||
} else {
|
||||
// println!("key pressed, {:#?}", event);
|
||||
}
|
||||
},
|
||||
KeyboardState::new(),
|
||||
&mut KBD_QUEUE,
|
||||
)
|
||||
};
|
||||
38
src/drivers/mouse.rs
Normal file
38
src/drivers/mouse.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use crate::{
|
||||
cursor::{clear_cursor, draw_cursor},
|
||||
vga::Vga,
|
||||
virtio::{self, Virtqueue, input::VirtioPciDriver},
|
||||
};
|
||||
|
||||
pub static mut MOUSE_POSITION: (u16, u16) = (0, 0);
|
||||
|
||||
static mut MOUSE_QUEUE: Virtqueue = unsafe { core::mem::zeroed() };
|
||||
pub static mut MOUSE_DRIVER: VirtioPciDriver<()> = unsafe {
|
||||
VirtioPciDriver::new(
|
||||
|_, event| {
|
||||
if event.is_relative() {
|
||||
let event = event.as_relative_event();
|
||||
|
||||
clear_cursor(&mut Vga, MOUSE_POSITION.0, MOUSE_POSITION.1);
|
||||
|
||||
match event.code {
|
||||
virtio::input::EventCodeRelative::X => {
|
||||
MOUSE_POSITION.0 = (MOUSE_POSITION.0 as i32 + event.value) as u16
|
||||
}
|
||||
virtio::input::EventCodeRelative::Y => {
|
||||
MOUSE_POSITION.1 = (MOUSE_POSITION.1 as i32 + event.value) as u16
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
draw_cursor(&mut Vga, MOUSE_POSITION.0, MOUSE_POSITION.1);
|
||||
|
||||
// println!("mouse moved relatively, {:#?}", event);
|
||||
} else {
|
||||
// println!("mouse moved, {:#?}", event);
|
||||
}
|
||||
},
|
||||
(),
|
||||
&mut MOUSE_QUEUE,
|
||||
)
|
||||
};
|
||||
Reference in New Issue
Block a user