Skip to content

Commit

Permalink
Keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
svelterust committed Jan 27, 2024
1 parent 9d26fd5 commit 75753e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ uart_16550 = "0.2.0"
bootloader = "0.9.23"
lazy_static = { version = "1.0", features = ["spin_no_std"] }
pic8259 = "0.10.4"
pc-keyboard = "0.7.0"

[package.metadata.bootimage]
test-success-exit-code = 33
Expand Down
41 changes: 38 additions & 3 deletions src/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub static PICS: spin::Mutex<ChainedPics> =
#[repr(u8)]
pub enum InterruptIndex {
Timer = PIC_1_OFFSET,
Keyboard,
}

impl InterruptIndex {
Expand All @@ -30,10 +31,12 @@ lazy_static! {
let mut idt = InterruptDescriptorTable::new();
idt.breakpoint.set_handler_fn(breakpoint_handler);
idt[InterruptIndex::Timer.as_usize()]
.set_handler_fn(timer_interrupt_handler); // new
.set_handler_fn(timer_interrupt_handler);
idt[InterruptIndex::Keyboard.as_usize()]
.set_handler_fn(keyboard_interrupt_handler);
unsafe {
idt.double_fault.set_handler_fn(double_fault_handler)
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX); // new
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
}
idt
};
Expand All @@ -60,7 +63,6 @@ extern "x86-interrupt" fn double_fault_handler(
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
}


extern "x86-interrupt" fn timer_interrupt_handler(
_stack_frame: InterruptStackFrame)
{
Expand All @@ -69,3 +71,36 @@ extern "x86-interrupt" fn timer_interrupt_handler(
PICS.lock().notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
}
}

extern "x86-interrupt" fn keyboard_interrupt_handler(
_stack_frame: InterruptStackFrame)
{
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
use spin::Mutex;
use x86_64::instructions::port::Port;

lazy_static! {
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> =
Mutex::new(Keyboard::new(ScancodeSet1::new(), layouts::Us104Key,
HandleControl::Ignore)
);
}

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) => print!("{}", character),
DecodedKey::RawKey(key) => print!("{:?}", key),
}
}
}

unsafe {
PICS.lock()
.notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8());
}
}

0 comments on commit 75753e9

Please sign in to comment.