From e7f193483f35051258d23a992b2b51705873ba19 Mon Sep 17 00:00:00 2001 From: nnyyxxxx Date: Sat, 9 Nov 2024 06:13:15 -0500 Subject: [PATCH] init cursor moving functionality --- kernel/src/interrupts.rs | 161 ++++++++++++++++++++------------------- kernel/src/vga_buffer.rs | 19 +++-- 2 files changed, 94 insertions(+), 86 deletions(-) diff --git a/kernel/src/interrupts.rs b/kernel/src/interrupts.rs index f13705d..a2b3d97 100644 --- a/kernel/src/interrupts.rs +++ b/kernel/src/interrupts.rs @@ -92,109 +92,112 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac )); } - if unsafe { BUFFER_INDEX } < BUFFER_SIZE { - let mut keyboard = KEYBOARD.lock(); - let mut port = Port::new(0x60); - - let scancode: u8 = unsafe { port.read() }; - if let Ok(Some(key_event)) = keyboard.add_byte(scancode) { - if let Some(key) = keyboard.process_keyevent(key_event) { - match key { - DecodedKey::Unicode(character) => { - if character == '\u{8}' { - // backspace + let mut keyboard = KEYBOARD.lock(); + let mut port = Port::new(0x60); + + let scancode: u8 = unsafe { port.read() }; + if let Ok(Some(key_event)) = keyboard.add_byte(scancode) { + if let Some(key) = keyboard.process_keyevent(key_event) { + match key { + DecodedKey::Unicode(character) => { + if character == '\u{8}' { + // backspace + unsafe { + if BUFFER_INDEX > 0 { + BUFFER_INDEX -= 1; + WRITER.lock().decrement_column_position(); + print!(" "); + WRITER.lock().decrement_column_position(); + } + } + } else { + unsafe { + BUFFER[BUFFER_INDEX] = character; + BUFFER_INDEX += 1; + } + print!("{}", character); + } + } + + DecodedKey::RawKey(key) => match key { + KeyCode::ArrowLeft => { + if unsafe { BUFFER_INDEX } > 0 { + WRITER.lock().decrement_column_position(); + unsafe { BUFFER_INDEX -= 1; } + } + } + + KeyCode::ArrowRight => { + if unsafe { BUFFER_INDEX } < BUFFER_SIZE { + WRITER.lock().increment_column_position(); + unsafe { BUFFER_INDEX += 1; } + } + } + + KeyCode::F1 => { + if unsafe { BUFFER_INDEX } > 0 { + clear_buffer(); + print!("\n"); unsafe { - if BUFFER_INDEX > 0 { + BUFFER[BUFFER_INDEX] = '\n'; + BUFFER_INDEX += 1; + } + } + } + + KeyCode::ArrowUp => { + let mut cmd_history = CMD_HISTORY.lock(); + + if cmd_history.history.len() > cmd_history.last { + while unsafe { BUFFER_INDEX } > 0 { + unsafe { BUFFER_INDEX -= 1; WRITER.lock().decrement_column_position(); print!(" "); WRITER.lock().decrement_column_position(); } } - } else { - unsafe { - BUFFER[BUFFER_INDEX] = character; - BUFFER_INDEX += 1; - } - print!("{}", character); - } - } - DecodedKey::RawKey(key) => match key { - KeyCode::F1 => { - if unsafe { BUFFER_INDEX } > 0 { - clear_buffer(); - print!("\n"); + for i in cmd_history.history[cmd_history.history.len() - cmd_history.last - 1].chars() { unsafe { - BUFFER[BUFFER_INDEX] = '\n'; + BUFFER[BUFFER_INDEX] = i; BUFFER_INDEX += 1; } + print!("{}", i); } + cmd_history.last += 1; } + } - KeyCode::ArrowUp => { - let mut cmd_history = CMD_HISTORY.lock(); - - if cmd_history.history.len() > cmd_history.last { - while unsafe { BUFFER_INDEX } > 0 { - unsafe { - BUFFER_INDEX -= 1; - WRITER.lock().decrement_column_position(); - print!(" "); - WRITER.lock().decrement_column_position(); - } - } + KeyCode::ArrowDown => { + let mut cmd_history = CMD_HISTORY.lock(); - for i in cmd_history.history[cmd_history.history.len() - cmd_history.last - 1].chars() { - unsafe { - BUFFER[BUFFER_INDEX] = i; - BUFFER_INDEX += 1; - } - print!("{}", i); + if cmd_history.last > 1 { + while unsafe { BUFFER_INDEX } > 0 { + unsafe { + BUFFER_INDEX -= 1; + WRITER.lock().decrement_column_position(); + print!(" "); + WRITER.lock().decrement_column_position(); } - cmd_history.last += 1; } - } - KeyCode::ArrowDown => { - let mut cmd_history = CMD_HISTORY.lock(); - - if cmd_history.last > 1 { - while unsafe { BUFFER_INDEX } > 0 { - unsafe { - BUFFER_INDEX -= 1; - WRITER.lock().decrement_column_position(); - print!(" "); - WRITER.lock().decrement_column_position(); - } - } + cmd_history.last -= 1; - cmd_history.last -= 1; - - for i in cmd_history.history[cmd_history.history.len() - cmd_history.last].chars() { - unsafe { - BUFFER[BUFFER_INDEX] = i; - BUFFER_INDEX += 1; - } - print!("{}", i); + for i in cmd_history.history[cmd_history.history.len() - cmd_history.last].chars() { + unsafe { + BUFFER[BUFFER_INDEX] = i; + BUFFER_INDEX += 1; } + print!("{}", i); } } + } - KeyCode::ArrowLeft => { - #[cfg(debug_assertions)] - WRITER.lock().decrement_column_position(); - } - - KeyCode::ArrowRight => { - #[cfg(debug_assertions)] - WRITER.lock().increment_column_position(); - } - - _ => {} - }, - } + _ => {} + }, } + WRITER.lock().update_cursor(); } } diff --git a/kernel/src/vga_buffer.rs b/kernel/src/vga_buffer.rs index 1539eec..fb87c33 100644 --- a/kernel/src/vga_buffer.rs +++ b/kernel/src/vga_buffer.rs @@ -132,7 +132,7 @@ pub struct Writer { } impl Writer { - fn update_cursor(&mut self) { + pub fn update_cursor(&mut self) { let pos = (BUFFER_HEIGHT - 1) * BUFFER_WIDTH + self.column_position; unsafe { let mut port = x86_64::instructions::port::Port::new(0x3D4); @@ -142,10 +142,6 @@ impl Writer { port.write(0x0E_u8); data_port.write(((pos >> 8) & 0xFF) as u8); } - self.buffer.chars[BUFFER_HEIGHT - 1][self.column_position].write(ScreenChar { - ascii_character: b' ', - color_code: self.color_code, - }); } pub fn write_byte(&mut self, byte: u8) { @@ -225,10 +221,19 @@ impl Writer { } pub fn decrement_column_position(&mut self) { - self.column_position -= 1; + if self.column_position > 0 { + self.column_position -= 1; + } } + pub fn increment_column_position(&mut self) { - self.column_position += 1; + if self.column_position < BUFFER_WIDTH - 1 { + self.column_position += 1; + } + } + + pub fn get_column_position(&mut self) -> usize { + self.column_position } }