Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement cursor moving functionality #37

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 99 additions & 74 deletions kernel/src/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,109 +92,134 @@ 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
unsafe {
if BUFFER_INDEX > 0 {
BUFFER_INDEX -= 1;
WRITER.lock().decrement_column_position();
print!(" ");
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 {
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 {
}
} else {
unsafe {
if BUFFER_INDEX < BUFFER_SIZE {
BUFFER[BUFFER_INDEX] = character;
print!("{}", character);
BUFFER_INDEX += 1;
}
print!("{}", character);
}
}
}

DecodedKey::RawKey(key) => match key {
KeyCode::F1 => {
if unsafe { BUFFER_INDEX } > 0 {
clear_buffer();
print!("\n");
unsafe {
BUFFER[BUFFER_INDEX] = '\n';
BUFFER_INDEX += 1;
DecodedKey::RawKey(key) => match key {
KeyCode::ArrowLeft => unsafe {
if BUFFER_INDEX > 0 {
BUFFER_INDEX -= 1;
WRITER.lock().decrement_column_position();
}
},

KeyCode::ArrowRight => unsafe {
if BUFFER_INDEX < BUFFER_SIZE && BUFFER[BUFFER_INDEX] != 0 as char {
BUFFER_INDEX += 1;
WRITER.lock().increment_column_position();
}
},

KeyCode::F1 => {
if unsafe { BUFFER_INDEX } > 0 {
clear_buffer();
print!("\n");
unsafe {
BUFFER_INDEX = 0;
for i in 0..BUFFER_SIZE {
BUFFER[i] = 0 as char;
}
}
}
}

KeyCode::ArrowUp => {
let mut cmd_history = CMD_HISTORY.lock();
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();
}
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();
}
}

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);
for i in cmd_history.history[cmd_history.history.len() - cmd_history.last - 1].chars() {
unsafe {
BUFFER[BUFFER_INDEX] = i;
BUFFER_INDEX += 1;
}
cmd_history.last += 1;
print!("{}", i);
}
cmd_history.last += 1;
}
}

KeyCode::ArrowDown => {
let mut cmd_history = CMD_HISTORY.lock();
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();
}
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
30 changes: 16 additions & 14 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 All @@ -165,10 +161,7 @@ impl Writer {
color_code,
});
self.column_position += 1;

if self.column_position < BUFFER_WIDTH {
self.update_cursor();
}
self.update_cursor();
}
}
}
Expand Down Expand Up @@ -197,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);
Expand Down Expand Up @@ -225,12 +218,21 @@ impl Writer {
}

pub fn decrement_column_position(&mut self) {
self.column_position -= 1;
self.update_cursor();
if self.column_position > 0 {
self.column_position -= 1;
self.update_cursor();
}
}

pub fn increment_column_position(&mut self) {
self.column_position += 1;
self.update_cursor();
if self.column_position < BUFFER_WIDTH - 1 {
self.column_position += 1;
self.update_cursor();
}
}

pub fn get_column_position(&mut self) -> usize {
self.column_position
}
}

Expand Down
Loading