From d8e3c7d8e934c6841585d6dd30237bac5a473451 Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Thu, 16 Sep 2021 18:08:58 -0500 Subject: [PATCH] Move cursor with arrow keys --- src/interrupts.rs | 32 ++++++++++++++++++++++++++++++-- src/vga_buffer.rs | 6 ++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/interrupts.rs b/src/interrupts.rs index f41f014..2df9c36 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -44,7 +44,7 @@ extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFr extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) { use x86_64::instructions::port::Port; - use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1}; + use pc_keyboard::{layouts, DecodedKey, KeyCode, HandleControl, Keyboard, ScancodeSet1}; lazy_static! { static ref KEYBOARD: Mutex> = { @@ -65,8 +65,36 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac } else { print!("{}", character) } + + let writer = crate::vga_buffer::WRITER.lock(); + let col = writer.column_position; + let row = crate::vga_buffer::BUFFER_HEIGHT - 1; + + crate::vga_buffer::move_cursor(col as u16, row as u16); }, - DecodedKey::RawKey(key) => print!("{:#?}", key), } + DecodedKey::RawKey(key) => { + match key { + // TODO + KeyCode::ArrowLeft => { + let mut writer = crate::vga_buffer::WRITER.lock(); + let col = writer.column_position; + let row = crate::vga_buffer::BUFFER_HEIGHT - 1; + + crate::vga_buffer::move_cursor((col as u16) - 1, row as u16); + writer.column_position -= 1; + }, + KeyCode::ArrowRight => { + let mut writer = crate::vga_buffer::WRITER.lock(); + let col = writer.column_position; + let row = crate::vga_buffer::BUFFER_HEIGHT - 1; + + crate::vga_buffer::move_cursor((col as u16) + 1, row as u16); + writer.column_position += 1; + }, + _ => {} + } + }, + } } } diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index e1e920c..68d4092 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -43,7 +43,7 @@ struct ScreenChar { color_code: ColorCode } -const BUFFER_HEIGHT: usize = 25; +pub(crate) const BUFFER_HEIGHT: usize = 25; const BUFFER_WIDTH: usize = 80; #[repr(transparent)] @@ -52,7 +52,7 @@ struct Buffer { } pub struct Writer { - column_position: usize, + pub column_position: usize, color_code: ColorCode, buffer: &'static mut Buffer } @@ -168,7 +168,6 @@ pub fn backspace() { pub struct Cursor { port_low: Port, port_high: Port, - cursor_pos: u16 } impl Cursor { @@ -176,7 +175,6 @@ impl Cursor { Cursor { port_low: Port::new(0x3D4), port_high: Port::new(0x3D5), - cursor_pos: 0 } }