From e7f193483f35051258d23a992b2b51705873ba19 Mon Sep 17 00:00:00 2001 From: nnyyxxxx Date: Sat, 9 Nov 2024 06:13:15 -0500 Subject: [PATCH 1/5] 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 } } From 14c167a652dda46f1e1494b5691695bfcd7b98f2 Mon Sep 17 00:00:00 2001 From: nnyyxxxx Date: Sat, 9 Nov 2024 06:52:28 -0500 Subject: [PATCH 2/5] run fmt --- kernel/src/interrupts.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/src/interrupts.rs b/kernel/src/interrupts.rs index a2b3d97..b70d911 100644 --- a/kernel/src/interrupts.rs +++ b/kernel/src/interrupts.rs @@ -123,14 +123,18 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac KeyCode::ArrowLeft => { if unsafe { BUFFER_INDEX } > 0 { WRITER.lock().decrement_column_position(); - unsafe { BUFFER_INDEX -= 1; } + unsafe { + BUFFER_INDEX -= 1; + } } } KeyCode::ArrowRight => { if unsafe { BUFFER_INDEX } < BUFFER_SIZE { WRITER.lock().increment_column_position(); - unsafe { BUFFER_INDEX += 1; } + unsafe { + BUFFER_INDEX += 1; + } } } From a20603cab09b037f8f4048c2bd36907b1586be6d Mon Sep 17 00:00:00 2001 From: nnyyxxxx Date: Sat, 9 Nov 2024 07:14:09 -0500 Subject: [PATCH 3/5] fix color bleeding --- kernel/src/vga_buffer.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/kernel/src/vga_buffer.rs b/kernel/src/vga_buffer.rs index fb87c33..7761586 100644 --- a/kernel/src/vga_buffer.rs +++ b/kernel/src/vga_buffer.rs @@ -161,10 +161,7 @@ impl Writer { color_code, }); self.column_position += 1; - - if self.column_position < BUFFER_WIDTH { - self.update_cursor(); - } + self.update_cursor(); } } } @@ -193,7 +190,7 @@ impl Writer { fn clear_row(&mut self, row: usize) { let blank = ScreenChar { ascii_character: b' ', - color_code: self.color_code, + color_code: ColorCode::new(Color::White, Color::Black), }; for col in 0..BUFFER_WIDTH { self.buffer.chars[row][col].write(blank); @@ -223,12 +220,14 @@ impl Writer { pub fn decrement_column_position(&mut self) { if self.column_position > 0 { self.column_position -= 1; + self.update_cursor(); } } pub fn increment_column_position(&mut self) { if self.column_position < BUFFER_WIDTH - 1 { self.column_position += 1; + self.update_cursor(); } } From 9a7c3ac7f7cffe74e4e8e920da0f5102eae80287 Mon Sep 17 00:00:00 2001 From: nnyyxxxx Date: Sun, 10 Nov 2024 02:23:10 -0500 Subject: [PATCH 4/5] make chars move with deleted chars --- kernel/src/interrupts.rs | 60 ++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/kernel/src/interrupts.rs b/kernel/src/interrupts.rs index b70d911..150f185 100644 --- a/kernel/src/interrupts.rs +++ b/kernel/src/interrupts.rs @@ -104,39 +104,69 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac // backspace unsafe { if BUFFER_INDEX > 0 { + for i in BUFFER_INDEX..BUFFER_SIZE { + BUFFER[i - 1] = BUFFER[i]; + } + BUFFER[BUFFER_SIZE - 1] = 0 as char; BUFFER_INDEX -= 1; + WRITER.lock().decrement_column_position(); print!(" "); WRITER.lock().decrement_column_position(); + + let pos = WRITER.lock().get_column_position(); + for i in BUFFER_INDEX..BUFFER_SIZE { + if BUFFER[i] != 0 as char { + print!("{}", BUFFER[i]); + } + } + print!(" "); + + let new_pos = WRITER.lock().get_column_position(); + for _ in pos..new_pos { + WRITER.lock().decrement_column_position(); + } } } } else { unsafe { - BUFFER[BUFFER_INDEX] = character; - BUFFER_INDEX += 1; + if BUFFER_INDEX < BUFFER_SIZE { + for i in (BUFFER_INDEX..BUFFER_SIZE - 1).rev() { + BUFFER[i + 1] = BUFFER[i]; + } + BUFFER[BUFFER_INDEX] = character; + + let pos = WRITER.lock().get_column_position(); + for i in BUFFER_INDEX..BUFFER_SIZE { + if BUFFER[i] != 0 as char { + print!("{}", BUFFER[i]); + } + } + + BUFFER_INDEX += 1; + let new_pos = WRITER.lock().get_column_position(); + for _ in pos + 1..new_pos { + WRITER.lock().decrement_column_position(); + } + } } - print!("{}", character); } } DecodedKey::RawKey(key) => match key { - KeyCode::ArrowLeft => { - if unsafe { BUFFER_INDEX } > 0 { + KeyCode::ArrowLeft => unsafe { + if BUFFER_INDEX > 0 { + BUFFER_INDEX -= 1; WRITER.lock().decrement_column_position(); - unsafe { - BUFFER_INDEX -= 1; - } } - } + }, - KeyCode::ArrowRight => { - if unsafe { BUFFER_INDEX } < BUFFER_SIZE { + KeyCode::ArrowRight => unsafe { + if BUFFER_INDEX < BUFFER_SIZE && BUFFER[BUFFER_INDEX] != 0 as char { + BUFFER_INDEX += 1; WRITER.lock().increment_column_position(); - unsafe { - BUFFER_INDEX += 1; - } } - } + }, KeyCode::F1 => { if unsafe { BUFFER_INDEX } > 0 { From 85b58f2e45f689adeb4cb4520e02f32c518bb5dc Mon Sep 17 00:00:00 2001 From: nnyyxxxx Date: Sun, 10 Nov 2024 02:45:55 -0500 Subject: [PATCH 5/5] somewhat fix this bitch --- kernel/src/interrupts.rs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/kernel/src/interrupts.rs b/kernel/src/interrupts.rs index 150f185..fe1dac7 100644 --- a/kernel/src/interrupts.rs +++ b/kernel/src/interrupts.rs @@ -131,23 +131,9 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac } else { unsafe { if BUFFER_INDEX < BUFFER_SIZE { - for i in (BUFFER_INDEX..BUFFER_SIZE - 1).rev() { - BUFFER[i + 1] = BUFFER[i]; - } BUFFER[BUFFER_INDEX] = character; - - let pos = WRITER.lock().get_column_position(); - for i in BUFFER_INDEX..BUFFER_SIZE { - if BUFFER[i] != 0 as char { - print!("{}", BUFFER[i]); - } - } - + print!("{}", character); BUFFER_INDEX += 1; - let new_pos = WRITER.lock().get_column_position(); - for _ in pos + 1..new_pos { - WRITER.lock().decrement_column_position(); - } } } } @@ -173,8 +159,10 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac clear_buffer(); print!("\n"); unsafe { - BUFFER[BUFFER_INDEX] = '\n'; - BUFFER_INDEX += 1; + BUFFER_INDEX = 0; + for i in 0..BUFFER_SIZE { + BUFFER[i] = 0 as char; + } } } }