Skip to content

Commit

Permalink
init cursor moving functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nnyyxxxx committed Nov 9, 2024
1 parent e6f2c48 commit e7f1934
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 86 deletions.
161 changes: 82 additions & 79 deletions kernel/src/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
19 changes: 12 additions & 7 deletions kernel/src/vga_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit e7f1934

Please sign in to comment.