1
0
Fork 0
forked from AbleOS/ableos

Merge branch 'master' into fix/rhai-repl

This commit is contained in:
able 2022-02-20 11:06:56 +00:00
commit 9273d74446
14 changed files with 793 additions and 796 deletions

View file

@ -2,19 +2,19 @@ use lazy_static::lazy_static;
use spin::Mutex; use spin::Mutex;
use uart_16550::SerialPort; use uart_16550::SerialPort;
lazy_static! { lazy_static! {
pub static ref SERIAL1: Mutex<SerialPort> = { pub static ref SERIAL1: Mutex<SerialPort> = {
let mut serial_port = unsafe { SerialPort::new(0x3F8) }; let mut serial_port = unsafe { SerialPort::new(0x3F8) };
serial_port.init(); serial_port.init();
Mutex::new(serial_port) Mutex::new(serial_port)
}; };
} }
#[doc(hidden)] #[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) { pub fn _print(args: ::core::fmt::Arguments) {
use core::fmt::Write; use core::fmt::Write;
SERIAL1 SERIAL1
.lock() .lock()
.write_fmt(args) .write_fmt(args)
.expect("Printing to serial failed"); .expect("Printing to serial failed");
} }
/// Prints to the host through the serial interface. /// Prints to the host through the serial interface.
#[macro_export] #[macro_export]

View file

@ -1,112 +1,112 @@
use bootloader::bootinfo::{MemoryMap, MemoryRegionType}; use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
use x86_64::{ use x86_64::{
structures::paging::{ structures::paging::{
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB, FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
}, },
PhysAddr, VirtAddr, PhysAddr, VirtAddr,
}; };
pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> { pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> {
let level_4_table = active_level_4_table(physical_memory_offset); let level_4_table = active_level_4_table(physical_memory_offset);
OffsetPageTable::new(level_4_table, physical_memory_offset) OffsetPageTable::new(level_4_table, physical_memory_offset)
} }
unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable { unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable {
use x86_64::registers::control::Cr3; use x86_64::registers::control::Cr3;
let (level_4_table_frame, _) = Cr3::read(); let (level_4_table_frame, _) = Cr3::read();
let phys = level_4_table_frame.start_address(); let phys = level_4_table_frame.start_address();
let virt = physical_memory_offset + phys.as_u64(); let virt = physical_memory_offset + phys.as_u64();
let page_table_ptr: *mut PageTable = virt.as_mut_ptr(); let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
// THIS IS UNSAFE // THIS IS UNSAFE
&mut *page_table_ptr &mut *page_table_ptr
} }
fn translate_addr_inner(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> { fn translate_addr_inner(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {
use x86_64::registers::control::Cr3; use x86_64::registers::control::Cr3;
use x86_64::structures::paging::page_table::FrameError; use x86_64::structures::paging::page_table::FrameError;
let (level_4_table_frame, _) = Cr3::read(); let (level_4_table_frame, _) = Cr3::read();
let table_indexes = [ let table_indexes = [
addr.p4_index(), addr.p4_index(),
addr.p3_index(), addr.p3_index(),
addr.p2_index(), addr.p2_index(),
addr.p1_index(), addr.p1_index(),
]; ];
let mut frame = level_4_table_frame; let mut frame = level_4_table_frame;
for &index in &table_indexes { for &index in &table_indexes {
// convert the frame into a page table reference // convert the frame into a page table reference
let virt = physical_memory_offset + frame.start_address().as_u64(); let virt = physical_memory_offset + frame.start_address().as_u64();
let table_ptr: *const PageTable = virt.as_ptr(); let table_ptr: *const PageTable = virt.as_ptr();
let table = unsafe { &*table_ptr }; let table = unsafe { &*table_ptr };
let entry = &table[index]; let entry = &table[index];
frame = match entry.frame() { frame = match entry.frame() {
Ok(frame) => frame, Ok(frame) => frame,
Err(FrameError::FrameNotPresent) => return None, Err(FrameError::FrameNotPresent) => return None,
Err(FrameError::HugeFrame) => panic!["huge pages not supported"], Err(FrameError::HugeFrame) => panic!["huge pages not supported"],
}; };
} }
Some(frame.start_address() + u64::from(addr.page_offset())) Some(frame.start_address() + u64::from(addr.page_offset()))
} }
pub unsafe fn translate_addr(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> { pub unsafe fn translate_addr(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {
translate_addr_inner(addr, physical_memory_offset) translate_addr_inner(addr, physical_memory_offset)
} }
pub fn create_example_mapping( pub fn create_example_mapping(
page: Page, page: Page,
mapper: &mut OffsetPageTable, mapper: &mut OffsetPageTable,
frame_allocator: &mut impl FrameAllocator<Size4KiB>, frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) { ) {
use x86_64::structures::paging::PageTableFlags as Flags; use x86_64::structures::paging::PageTableFlags as Flags;
let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000)); let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
let flags = Flags::PRESENT | Flags::WRITABLE; let flags = Flags::PRESENT | Flags::WRITABLE;
let map_to_result = unsafe { mapper.map_to(page, frame, flags, frame_allocator) }; let map_to_result = unsafe { mapper.map_to(page, frame, flags, frame_allocator) };
map_to_result.expect("map_to failed").flush(); map_to_result.expect("map_to failed").flush();
} }
pub struct EmptyFrameAllocator; pub struct EmptyFrameAllocator;
unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator { unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> { fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> {
None None
} }
} }
pub struct BootInfoFrameAllocator { pub struct BootInfoFrameAllocator {
memory_map: &'static MemoryMap, memory_map: &'static MemoryMap,
next: usize, next: usize,
} }
impl BootInfoFrameAllocator { impl BootInfoFrameAllocator {
pub unsafe fn init(memory_map: &'static MemoryMap) -> Self { pub unsafe fn init(memory_map: &'static MemoryMap) -> Self {
Self { Self {
memory_map, memory_map,
next: 0, next: 0,
} }
} }
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> { fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
let regions = self.memory_map.iter(); let regions = self.memory_map.iter();
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable); let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
let addr_range = usable_regions.map(|r| r.range.start_addr()..r.range.end_addr()); let addr_range = usable_regions.map(|r| r.range.start_addr()..r.range.end_addr());
let frame_address = addr_range.flat_map(|r| r.step_by(4096)); let frame_address = addr_range.flat_map(|r| r.step_by(4096));
frame_address.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr))) frame_address.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)))
} }
} }
unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator { unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> { fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> {
let frame = self.usable_frames().nth(self.next); let frame = self.usable_frames().nth(self.next);
self.next += 1; self.next += 1;
frame frame
} }
} }

View file

@ -1,34 +1,34 @@
// Can be standardized // Can be standardized
// NOTE: Move this to relib // NOTE: Move this to relib
pub struct SemanticVersion { pub struct SemanticVersion {
pub major: u8, pub major: u8,
pub minor: u8, pub minor: u8,
pub patch: u8, pub patch: u8,
} }
impl core::fmt::Display for SemanticVersion { impl core::fmt::Display for SemanticVersion {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "v{}.{}.{}", self.major, self.minor, self.patch) write!(f, "v{}.{}.{}", self.major, self.minor, self.patch)
} }
} }
// NOTE: Move to somewhere else // NOTE: Move to somewhere else
lazy_static! { lazy_static! {
pub static ref KINFO: KernelInfo = KernelInfo { pub static ref KINFO: KernelInfo = KernelInfo {
kernel_version: SemanticVersion { kernel_version: SemanticVersion {
major: 0, major: 0,
minor: 0, minor: 0,
patch: 0, patch: 0,
}, },
memory: SystemMemory { used: 0, total: 0 } memory: SystemMemory { used: 0, total: 0 }
}; };
} }
/// simple info you would want to know in a neofetch like program /// simple info you would want to know in a neofetch like program
pub struct KernelInfo { pub struct KernelInfo {
// os: String, // os: String,
// host: String, // host: String,
pub kernel_version: SemanticVersion, pub kernel_version: SemanticVersion,
// cpu: String, // cpu: String,
// gpu: String, // gpu: String,
pub memory: SystemMemory, pub memory: SystemMemory,
} }
use super::systeminfo::SystemMemory; use super::systeminfo::SystemMemory;
use lazy_static::lazy_static; use lazy_static::lazy_static;

View file

@ -1,10 +1,10 @@
pub trait Server { pub trait Server {
/// Initialize the server and return a number /// Initialize the server and return a number
fn initialize() -> u32; fn initialize() -> u32;
/// kill the server /// kill the server
fn kill() -> bool; fn kill() -> bool;
// put data in the servers outbox // put data in the servers outbox
fn send(); fn send();
// put data in the servers inbox and notify it // put data in the servers inbox and notify it
fn recieve(); fn recieve();
} }

View file

@ -1,3 +1,3 @@
//! //!
pub mod window;
pub mod compositor; pub mod compositor;
pub mod window;

View file

@ -1,16 +1,16 @@
use crate::{ use crate::{
DecodedKey, HandleControl, KeyCode, KeyboardLayout, LayoutEntry, LayoutEntryKind, Modifiers, DecodedKey, HandleControl, KeyCode, KeyboardLayout, LayoutEntry, LayoutEntryKind, Modifiers,
}; };
// Do not edit this file directly. Instead, create a `Keyboard` and modify that. // Do not edit this file directly. Instead, create a `Keyboard` and modify that.
pub struct CustomLayout { pub struct CustomLayout {
mapping: [LayoutEntry; 256], mapping: [LayoutEntry; 256],
} }
impl Default for CustomLayout { impl Default for CustomLayout {
fn default() -> Self { fn default() -> Self {
Self::new_us104key() Self::new_us104key()
} }
} }
#[rustfmt::skip] #[rustfmt::skip]
impl CustomLayout { impl CustomLayout {
@ -101,76 +101,76 @@ impl CustomLayout {
} }
} }
impl KeyboardLayout for CustomLayout { impl KeyboardLayout for CustomLayout {
fn map_keycode( fn map_keycode(
&self, &self,
keycode: KeyCode, keycode: KeyCode,
modifiers: &Modifiers, modifiers: &Modifiers,
handle_ctrl: HandleControl, handle_ctrl: HandleControl,
) -> DecodedKey { ) -> DecodedKey {
let map_to_unicode = handle_ctrl == HandleControl::MapLettersToUnicode; let map_to_unicode = handle_ctrl == HandleControl::MapLettersToUnicode;
let spot = &self.mapping[keycode as usize]; let spot = &self.mapping[keycode as usize];
if let Some(k) = if map_to_unicode && modifiers.is_ctrl() { if let Some(k) = if map_to_unicode && modifiers.is_ctrl() {
match spot.kind { match spot.kind {
LayoutEntryKind::Regular => spot.raw_unicode, LayoutEntryKind::Regular => spot.raw_unicode,
LayoutEntryKind::Numlockable => None, LayoutEntryKind::Numlockable => None,
LayoutEntryKind::Capslockable => spot.raw_unicode, LayoutEntryKind::Capslockable => spot.raw_unicode,
}
} else if modifiers.alt_gr {
match spot.kind {
LayoutEntryKind::Regular => spot.altgr,
LayoutEntryKind::Numlockable => None,
LayoutEntryKind::Capslockable => spot.altgr,
}
} else if modifiers.is_shifted() {
match spot.kind {
LayoutEntryKind::Regular => spot.shifted,
LayoutEntryKind::Numlockable => {
if modifiers.numlock {
spot.locked_shifted
} else {
spot.shifted
}
} }
LayoutEntryKind::Capslockable => { } else if modifiers.alt_gr {
if modifiers.is_caps() { match spot.kind {
spot.locked_shifted LayoutEntryKind::Regular => spot.altgr,
} else { LayoutEntryKind::Numlockable => None,
spot.shifted LayoutEntryKind::Capslockable => spot.altgr,
}
} }
} } else if modifiers.is_shifted() {
} else { match spot.kind {
match spot.kind { LayoutEntryKind::Regular => spot.shifted,
LayoutEntryKind::Regular => spot.unshifted, LayoutEntryKind::Numlockable => {
LayoutEntryKind::Numlockable => { if modifiers.numlock {
if modifiers.numlock { spot.locked_shifted
spot.locked } else {
} else { spot.shifted
spot.unshifted }
} }
LayoutEntryKind::Capslockable => {
if modifiers.is_caps() {
spot.locked_shifted
} else {
spot.shifted
}
}
} }
LayoutEntryKind::Capslockable => { } else {
if modifiers.is_caps() { match spot.kind {
spot.locked LayoutEntryKind::Regular => spot.unshifted,
} else { LayoutEntryKind::Numlockable => {
spot.unshifted if modifiers.numlock {
} spot.locked
} else {
spot.unshifted
}
}
LayoutEntryKind::Capslockable => {
if modifiers.is_caps() {
spot.locked
} else {
spot.unshifted
}
}
} }
} } {
} { k
k } else {
} else { DecodedKey::RawKey(keycode as u8)
DecodedKey::RawKey(keycode as u8) }
} }
}
} }
// Note(elfein) Not super hard to get right, but still- DO NOT TOUCH // Note(elfein) Not super hard to get right, but still- DO NOT TOUCH
impl CustomLayout { impl CustomLayout {
// See how hard this is to get right? // See how hard this is to get right?
// See the complexity of all the methods? // See the complexity of all the methods?
// Yeah- if you don't know what you're doing, ask before you touch! // Yeah- if you don't know what you're doing, ask before you touch!
pub fn set(&mut self, pos: KeyCode, entry: LayoutEntry) { pub fn set(&mut self, pos: KeyCode, entry: LayoutEntry) {
self.mapping[pos as usize] = entry; self.mapping[pos as usize] = entry;
} }
} }

View file

@ -1,253 +1,255 @@
use crate::{KeyCode, ScancodeSet, DecodeState, KeyEvent, Error, KeyState, keyboard::EXTENDED_KEY_CODE}; use crate::{
keyboard::EXTENDED_KEY_CODE, DecodeState, Error, KeyCode, KeyEvent, KeyState, ScancodeSet,
};
pub struct CustomScancodeSet { pub struct CustomScancodeSet {
single_byte: [Option<KeyCode>; 256], single_byte: [Option<KeyCode>; 256],
extended: [Option<KeyCode>; 256], extended: [Option<KeyCode>; 256],
} }
impl Default for CustomScancodeSet { impl Default for CustomScancodeSet {
fn default() -> Self { fn default() -> Self {
Self::scancode_set1() Self::scancode_set1()
} }
} }
impl CustomScancodeSet { impl CustomScancodeSet {
pub fn scancode_set1() -> Self { pub fn scancode_set1() -> Self {
let mut scancode_set = Self { let mut scancode_set = Self {
single_byte: [None; 256], single_byte: [None; 256],
extended: [None; 256], extended: [None; 256],
}; };
scancode_set.single_byte[0x01] = Some(KeyCode::Escape); // 01 scancode_set.single_byte[0x01] = Some(KeyCode::Escape); // 01
scancode_set.single_byte[0x02] = Some(KeyCode::Key1); // 02 scancode_set.single_byte[0x02] = Some(KeyCode::Key1); // 02
scancode_set.single_byte[0x03] = Some(KeyCode::Key2); // 03 scancode_set.single_byte[0x03] = Some(KeyCode::Key2); // 03
scancode_set.single_byte[0x04] = Some(KeyCode::Key3); // 04 scancode_set.single_byte[0x04] = Some(KeyCode::Key3); // 04
scancode_set.single_byte[0x05] = Some(KeyCode::Key4); // 05 scancode_set.single_byte[0x05] = Some(KeyCode::Key4); // 05
scancode_set.single_byte[0x06] = Some(KeyCode::Key5); // 06 scancode_set.single_byte[0x06] = Some(KeyCode::Key5); // 06
scancode_set.single_byte[0x07] = Some(KeyCode::Key6); // 07 scancode_set.single_byte[0x07] = Some(KeyCode::Key6); // 07
scancode_set.single_byte[0x08] = Some(KeyCode::Key7); // 08 scancode_set.single_byte[0x08] = Some(KeyCode::Key7); // 08
scancode_set.single_byte[0x09] = Some(KeyCode::Key8); // 09 scancode_set.single_byte[0x09] = Some(KeyCode::Key8); // 09
scancode_set.single_byte[0x0A] = Some(KeyCode::Key9); // 0A scancode_set.single_byte[0x0A] = Some(KeyCode::Key9); // 0A
scancode_set.single_byte[0x0B] = Some(KeyCode::Key0); // 0B scancode_set.single_byte[0x0B] = Some(KeyCode::Key0); // 0B
scancode_set.single_byte[0x0C] = Some(KeyCode::Minus); // 0C scancode_set.single_byte[0x0C] = Some(KeyCode::Minus); // 0C
scancode_set.single_byte[0x0D] = Some(KeyCode::Equals); // 0D scancode_set.single_byte[0x0D] = Some(KeyCode::Equals); // 0D
scancode_set.single_byte[0x0E] = Some(KeyCode::Backspace); // 0E scancode_set.single_byte[0x0E] = Some(KeyCode::Backspace); // 0E
scancode_set.single_byte[0x0F] = Some(KeyCode::Tab); // 0F scancode_set.single_byte[0x0F] = Some(KeyCode::Tab); // 0F
scancode_set.single_byte[0x10] = Some(KeyCode::Q); // 10 scancode_set.single_byte[0x10] = Some(KeyCode::Q); // 10
scancode_set.single_byte[0x11] = Some(KeyCode::W); // 11 scancode_set.single_byte[0x11] = Some(KeyCode::W); // 11
scancode_set.single_byte[0x12] = Some(KeyCode::E); // 12 scancode_set.single_byte[0x12] = Some(KeyCode::E); // 12
scancode_set.single_byte[0x13] = Some(KeyCode::R); // 13 scancode_set.single_byte[0x13] = Some(KeyCode::R); // 13
scancode_set.single_byte[0x14] = Some(KeyCode::T); // 14 scancode_set.single_byte[0x14] = Some(KeyCode::T); // 14
scancode_set.single_byte[0x15] = Some(KeyCode::Y); // 15 scancode_set.single_byte[0x15] = Some(KeyCode::Y); // 15
scancode_set.single_byte[0x16] = Some(KeyCode::U); // 16 scancode_set.single_byte[0x16] = Some(KeyCode::U); // 16
scancode_set.single_byte[0x17] = Some(KeyCode::I); // 17 scancode_set.single_byte[0x17] = Some(KeyCode::I); // 17
scancode_set.single_byte[0x18] = Some(KeyCode::O); // 18 scancode_set.single_byte[0x18] = Some(KeyCode::O); // 18
scancode_set.single_byte[0x19] = Some(KeyCode::P); // 19 scancode_set.single_byte[0x19] = Some(KeyCode::P); // 19
scancode_set.single_byte[0x1A] = Some(KeyCode::BracketSquareLeft); // 1A scancode_set.single_byte[0x1A] = Some(KeyCode::BracketSquareLeft); // 1A
scancode_set.single_byte[0x1B] = Some(KeyCode::BracketSquareRight); // 1B scancode_set.single_byte[0x1B] = Some(KeyCode::BracketSquareRight); // 1B
scancode_set.single_byte[0x1C] = Some(KeyCode::Enter); // 1C scancode_set.single_byte[0x1C] = Some(KeyCode::Enter); // 1C
scancode_set.single_byte[0x1D] = Some(KeyCode::ControlLeft); // 1D scancode_set.single_byte[0x1D] = Some(KeyCode::ControlLeft); // 1D
scancode_set.single_byte[0x1E] = Some(KeyCode::A); // 1E scancode_set.single_byte[0x1E] = Some(KeyCode::A); // 1E
scancode_set.single_byte[0x1F] = Some(KeyCode::S); // 1F scancode_set.single_byte[0x1F] = Some(KeyCode::S); // 1F
scancode_set.single_byte[0x20] = Some(KeyCode::D); // 20 scancode_set.single_byte[0x20] = Some(KeyCode::D); // 20
scancode_set.single_byte[0x21] = Some(KeyCode::F); // 21 scancode_set.single_byte[0x21] = Some(KeyCode::F); // 21
scancode_set.single_byte[0x22] = Some(KeyCode::G); // 22 scancode_set.single_byte[0x22] = Some(KeyCode::G); // 22
scancode_set.single_byte[0x23] = Some(KeyCode::H); // 23 scancode_set.single_byte[0x23] = Some(KeyCode::H); // 23
scancode_set.single_byte[0x24] = Some(KeyCode::J); // 24 scancode_set.single_byte[0x24] = Some(KeyCode::J); // 24
scancode_set.single_byte[0x25] = Some(KeyCode::K); // 25 scancode_set.single_byte[0x25] = Some(KeyCode::K); // 25
scancode_set.single_byte[0x26] = Some(KeyCode::L); // 26 scancode_set.single_byte[0x26] = Some(KeyCode::L); // 26
scancode_set.single_byte[0x27] = Some(KeyCode::SemiColon); // 27 scancode_set.single_byte[0x27] = Some(KeyCode::SemiColon); // 27
scancode_set.single_byte[0x28] = Some(KeyCode::Quote); // 28 scancode_set.single_byte[0x28] = Some(KeyCode::Quote); // 28
scancode_set.single_byte[0x29] = Some(KeyCode::BackTick); // 29 scancode_set.single_byte[0x29] = Some(KeyCode::BackTick); // 29
scancode_set.single_byte[0x2A] = Some(KeyCode::ShiftLeft); // 2A scancode_set.single_byte[0x2A] = Some(KeyCode::ShiftLeft); // 2A
scancode_set.single_byte[0x2B] = Some(KeyCode::BackSlash); // 2B scancode_set.single_byte[0x2B] = Some(KeyCode::BackSlash); // 2B
scancode_set.single_byte[0x2C] = Some(KeyCode::Z); // 2C scancode_set.single_byte[0x2C] = Some(KeyCode::Z); // 2C
scancode_set.single_byte[0x2D] = Some(KeyCode::X); // 2D scancode_set.single_byte[0x2D] = Some(KeyCode::X); // 2D
scancode_set.single_byte[0x2E] = Some(KeyCode::C); // 2E scancode_set.single_byte[0x2E] = Some(KeyCode::C); // 2E
scancode_set.single_byte[0x2F] = Some(KeyCode::V); // 2F scancode_set.single_byte[0x2F] = Some(KeyCode::V); // 2F
scancode_set.single_byte[0x30] = Some(KeyCode::B); // 30 scancode_set.single_byte[0x30] = Some(KeyCode::B); // 30
scancode_set.single_byte[0x31] = Some(KeyCode::N); // 31 scancode_set.single_byte[0x31] = Some(KeyCode::N); // 31
scancode_set.single_byte[0x32] = Some(KeyCode::M); // 32 scancode_set.single_byte[0x32] = Some(KeyCode::M); // 32
scancode_set.single_byte[0x33] = Some(KeyCode::Comma); // 33 scancode_set.single_byte[0x33] = Some(KeyCode::Comma); // 33
scancode_set.single_byte[0x34] = Some(KeyCode::Fullstop); // 34 scancode_set.single_byte[0x34] = Some(KeyCode::Fullstop); // 34
scancode_set.single_byte[0x35] = Some(KeyCode::Slash); // 35 scancode_set.single_byte[0x35] = Some(KeyCode::Slash); // 35
scancode_set.single_byte[0x36] = Some(KeyCode::ShiftRight); // 36 scancode_set.single_byte[0x36] = Some(KeyCode::ShiftRight); // 36
scancode_set.single_byte[0x37] = Some(KeyCode::NumpadStar); // 37 scancode_set.single_byte[0x37] = Some(KeyCode::NumpadStar); // 37
scancode_set.single_byte[0x38] = Some(KeyCode::AltLeft); // 38 scancode_set.single_byte[0x38] = Some(KeyCode::AltLeft); // 38
scancode_set.single_byte[0x39] = Some(KeyCode::Spacebar); // 39 scancode_set.single_byte[0x39] = Some(KeyCode::Spacebar); // 39
scancode_set.single_byte[0x3A] = Some(KeyCode::CapsLock); // 3A scancode_set.single_byte[0x3A] = Some(KeyCode::CapsLock); // 3A
scancode_set.single_byte[0x3B] = Some(KeyCode::F1); // 3B scancode_set.single_byte[0x3B] = Some(KeyCode::F1); // 3B
scancode_set.single_byte[0x3C] = Some(KeyCode::F2); // 3C scancode_set.single_byte[0x3C] = Some(KeyCode::F2); // 3C
scancode_set.single_byte[0x3D] = Some(KeyCode::F3); // 3D scancode_set.single_byte[0x3D] = Some(KeyCode::F3); // 3D
scancode_set.single_byte[0x3E] = Some(KeyCode::F4); // 3E scancode_set.single_byte[0x3E] = Some(KeyCode::F4); // 3E
scancode_set.single_byte[0x3F] = Some(KeyCode::F5); // 3F scancode_set.single_byte[0x3F] = Some(KeyCode::F5); // 3F
scancode_set.single_byte[0x40] = Some(KeyCode::F6); // 40 scancode_set.single_byte[0x40] = Some(KeyCode::F6); // 40
scancode_set.single_byte[0x41] = Some(KeyCode::F7); // 41 scancode_set.single_byte[0x41] = Some(KeyCode::F7); // 41
scancode_set.single_byte[0x42] = Some(KeyCode::F8); // 42 scancode_set.single_byte[0x42] = Some(KeyCode::F8); // 42
scancode_set.single_byte[0x43] = Some(KeyCode::F9); // 43 scancode_set.single_byte[0x43] = Some(KeyCode::F9); // 43
scancode_set.single_byte[0x44] = Some(KeyCode::F10); // 44 scancode_set.single_byte[0x44] = Some(KeyCode::F10); // 44
scancode_set.single_byte[0x45] = Some(KeyCode::NumpadLock); // 45 scancode_set.single_byte[0x45] = Some(KeyCode::NumpadLock); // 45
scancode_set.single_byte[0x46] = Some(KeyCode::ScrollLock); // 46 scancode_set.single_byte[0x46] = Some(KeyCode::ScrollLock); // 46
scancode_set.single_byte[0x47] = Some(KeyCode::Numpad7); // 47 scancode_set.single_byte[0x47] = Some(KeyCode::Numpad7); // 47
scancode_set.single_byte[0x48] = Some(KeyCode::Numpad8); // 48 scancode_set.single_byte[0x48] = Some(KeyCode::Numpad8); // 48
scancode_set.single_byte[0x49] = Some(KeyCode::Numpad9); // 49 scancode_set.single_byte[0x49] = Some(KeyCode::Numpad9); // 49
scancode_set.single_byte[0x4A] = Some(KeyCode::NumpadMinus); // 4A scancode_set.single_byte[0x4A] = Some(KeyCode::NumpadMinus); // 4A
scancode_set.single_byte[0x4B] = Some(KeyCode::Numpad4); // 4B scancode_set.single_byte[0x4B] = Some(KeyCode::Numpad4); // 4B
scancode_set.single_byte[0x4C] = Some(KeyCode::Numpad5); // 4C scancode_set.single_byte[0x4C] = Some(KeyCode::Numpad5); // 4C
scancode_set.single_byte[0x4D] = Some(KeyCode::Numpad6); // 4D scancode_set.single_byte[0x4D] = Some(KeyCode::Numpad6); // 4D
scancode_set.single_byte[0x4E] = Some(KeyCode::NumpadPlus); // 4E scancode_set.single_byte[0x4E] = Some(KeyCode::NumpadPlus); // 4E
scancode_set.single_byte[0x4F] = Some(KeyCode::Numpad1); // 4F scancode_set.single_byte[0x4F] = Some(KeyCode::Numpad1); // 4F
scancode_set.single_byte[0x50] = Some(KeyCode::Numpad2); // 50 scancode_set.single_byte[0x50] = Some(KeyCode::Numpad2); // 50
scancode_set.single_byte[0x51] = Some(KeyCode::Numpad3); // 51 scancode_set.single_byte[0x51] = Some(KeyCode::Numpad3); // 51
scancode_set.single_byte[0x52] = Some(KeyCode::Numpad0); // 52 scancode_set.single_byte[0x52] = Some(KeyCode::Numpad0); // 52
scancode_set.single_byte[0x53] = Some(KeyCode::NumpadPeriod); // 53 scancode_set.single_byte[0x53] = Some(KeyCode::NumpadPeriod); // 53
// 0x54 // 0x54
// 0x55 // 0x55
// 0x56 // 0x56
scancode_set.single_byte[0x57] = Some(KeyCode::F11); // 57 scancode_set.single_byte[0x57] = Some(KeyCode::F11); // 57
scancode_set.single_byte[0x58] = Some(KeyCode::F12); // 58 scancode_set.single_byte[0x58] = Some(KeyCode::F12); // 58
for i in 0x81..=0xD8 { for i in 0x81..=0xD8 {
scancode_set.single_byte[i] = scancode_set.single_byte[i - 0x80]; scancode_set.single_byte[i] = scancode_set.single_byte[i - 0x80];
} }
scancode_set.extended[0x10] = Some(KeyCode::PrevTrack); // E010 scancode_set.extended[0x10] = Some(KeyCode::PrevTrack); // E010
//0x11 //0x11
//0x12 //0x12
//0x13 //0x13
//0x14 //0x14
//0x15 //0x15
//0x16 //0x16
//0x17 //0x17
//0x18 //0x18
scancode_set.extended[0x19] = Some(KeyCode::NextTrack); // E019 scancode_set.extended[0x19] = Some(KeyCode::NextTrack); // E019
//0x1A //0x1A
//0x1B //0x1B
scancode_set.extended[0x1C] = Some(KeyCode::NumpadEnter); // E01C scancode_set.extended[0x1C] = Some(KeyCode::NumpadEnter); // E01C
scancode_set.extended[0x1D] = Some(KeyCode::ControlRight); // E01D scancode_set.extended[0x1D] = Some(KeyCode::ControlRight); // E01D
//0x1E //0x1E
//0x1F //0x1F
scancode_set.extended[0x20] = Some(KeyCode::Mute); // E020 scancode_set.extended[0x20] = Some(KeyCode::Mute); // E020
scancode_set.extended[0x21] = Some(KeyCode::Calculator); // E021 scancode_set.extended[0x21] = Some(KeyCode::Calculator); // E021
scancode_set.extended[0x22] = Some(KeyCode::Play); // E022 scancode_set.extended[0x22] = Some(KeyCode::Play); // E022
//0x23 //0x23
scancode_set.extended[0x24] = Some(KeyCode::Stop); // E024 scancode_set.extended[0x24] = Some(KeyCode::Stop); // E024
//0x25 //0x25
//0x26 //0x26
//0x27 //0x27
//0x28 //0x28
//0x29 //0x29
//0x2A //0x2A
//0x2B //0x2B
//0x2C //0x2C
//0x2D //0x2D
scancode_set.extended[0x2E] = Some(KeyCode::VolumeDown); // E02E scancode_set.extended[0x2E] = Some(KeyCode::VolumeDown); // E02E
//0x2F //0x2F
scancode_set.extended[0x30] = Some(KeyCode::VolumeUp); // E030 scancode_set.extended[0x30] = Some(KeyCode::VolumeUp); // E030
//0x31 //0x31
scancode_set.extended[0x32] = Some(KeyCode::WWWHome); // E032 scancode_set.extended[0x32] = Some(KeyCode::WWWHome); // E032
//0x33 //0x33
//0x34 //0x34
scancode_set.extended[0x35] = Some(KeyCode::NumpadSlash); // E035 scancode_set.extended[0x35] = Some(KeyCode::NumpadSlash); // E035
//0x36 //0x36
//0x37 //0x37
scancode_set.extended[0x38] = Some(KeyCode::AltRight); // E038 scancode_set.extended[0x38] = Some(KeyCode::AltRight); // E038
//0x39 //0x39
//0x3A //0x3A
//0x3B //0x3B
//0x3C //0x3C
//0x3D //0x3D
//0x3E //0x3E
//0x3F //0x3F
//0x40 //0x40
//0x41 //0x41
//0x42 //0x42
//0x43 //0x43
//0x44 //0x44
//0x45 //0x45
//0x46 //0x46
scancode_set.extended[0x47] = Some(KeyCode::Home); // E047 scancode_set.extended[0x47] = Some(KeyCode::Home); // E047
scancode_set.extended[0x48] = Some(KeyCode::ArrowUp); // E048 scancode_set.extended[0x48] = Some(KeyCode::ArrowUp); // E048
scancode_set.extended[0x49] = Some(KeyCode::PageUp); // E049 scancode_set.extended[0x49] = Some(KeyCode::PageUp); // E049
//0x4A //0x4A
scancode_set.extended[0x4B] = Some(KeyCode::ArrowLeft); // E04B scancode_set.extended[0x4B] = Some(KeyCode::ArrowLeft); // E04B
//0x4C //0x4C
scancode_set.extended[0x4D] = Some(KeyCode::ArrowRight); // E04D scancode_set.extended[0x4D] = Some(KeyCode::ArrowRight); // E04D
//0x4E //0x4E
scancode_set.extended[0x4F] = Some(KeyCode::End); // E04F scancode_set.extended[0x4F] = Some(KeyCode::End); // E04F
scancode_set.extended[0x50] = Some(KeyCode::ArrowDown); // E050 scancode_set.extended[0x50] = Some(KeyCode::ArrowDown); // E050
scancode_set.extended[0x51] = Some(KeyCode::PageDown); // E051 scancode_set.extended[0x51] = Some(KeyCode::PageDown); // E051
scancode_set.extended[0x52] = Some(KeyCode::Insert); // E052 scancode_set.extended[0x52] = Some(KeyCode::Insert); // E052
scancode_set.extended[0x53] = Some(KeyCode::Delete); // E053 scancode_set.extended[0x53] = Some(KeyCode::Delete); // E053
for i in 0x90..=0xED { for i in 0x90..=0xED {
scancode_set.extended[i] = scancode_set.extended[i - 0x80]; scancode_set.extended[i] = scancode_set.extended[i - 0x80];
} }
scancode_set scancode_set
} }
pub fn scancode_set2() -> Self { pub fn scancode_set2() -> Self {
Self { Self {
single_byte: [None; 256], single_byte: [None; 256],
extended: [None; 256], extended: [None; 256],
} }
} }
} }
impl ScancodeSet for CustomScancodeSet { impl ScancodeSet for CustomScancodeSet {
fn advance_state(&self, state: &mut DecodeState, code: u8) -> Result<Option<KeyEvent>, Error> { fn advance_state(&self, state: &mut DecodeState, code: u8) -> Result<Option<KeyEvent>, Error> {
match *state { match *state {
DecodeState::Start => { DecodeState::Start => {
match code { match code {
EXTENDED_KEY_CODE => { EXTENDED_KEY_CODE => {
*state = DecodeState::Extended; *state = DecodeState::Extended;
Ok(None) Ok(None)
} }
0x80..=0xFF => { 0x80..=0xFF => {
// Release codes // Release codes
Ok(Some(KeyEvent::new( Ok(Some(KeyEvent::new(
self.map_scancode(code - 0x80)?, self.map_scancode(code - 0x80)?,
KeyState::Up, KeyState::Up,
))) )))
} }
_ => { _ => {
// Normal codes // Normal codes
Ok(Some(KeyEvent::new( Ok(Some(KeyEvent::new(
self.map_scancode(code)?, self.map_scancode(code)?,
KeyState::Down, KeyState::Down,
))) )))
} }
}
} }
} DecodeState::Extended => {
DecodeState::Extended => { *state = DecodeState::Start;
*state = DecodeState::Start; match code {
match code { 0x80..=0xFF => {
0x80..=0xFF => { // Extended Release codes
// Extended Release codes Ok(Some(KeyEvent::new(
Ok(Some(KeyEvent::new( self.map_extended_scancode(code - 0x80)?,
self.map_extended_scancode(code - 0x80)?, KeyState::Up,
KeyState::Up, )))
))) }
} _ => {
_ => { // Normal release codes
// Normal release codes Ok(Some(KeyEvent::new(
Ok(Some(KeyEvent::new( self.map_extended_scancode(code)?,
self.map_extended_scancode(code)?, KeyState::Down,
KeyState::Down, )))
))) }
} }
} }
} _ => {
_ => { // Can't get in to this state
// Can't get in to this state unimplemented!();
unimplemented!(); }
} }
} }
} fn map_scancode(&self, code: u8) -> Result<KeyCode, Error> {
fn map_scancode(&self, code: u8) -> Result<KeyCode, Error> { if let Some(kc) = self.single_byte[code as usize] {
if let Some(kc) = self.single_byte[code as usize] { Ok(kc)
Ok(kc) } else {
} else { Err(Error::UnknownKeyCode)
Err(Error::UnknownKeyCode) }
} }
} fn map_extended_scancode(&self, code: u8) -> Result<KeyCode, Error> {
fn map_extended_scancode(&self, code: u8) -> Result<KeyCode, Error> { if let Some(kc) = self.extended[code as usize] {
if let Some(kc) = self.extended[code as usize] { Ok(kc)
Ok(kc) } else {
} else { Err(Error::UnknownKeyCode)
Err(Error::UnknownKeyCode) }
} }
}
} }

View file

@ -2,104 +2,102 @@ use super::DecodedKey;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum LayoutEntryKind { pub enum LayoutEntryKind {
Regular, Regular,
Numlockable, Numlockable,
Capslockable, Capslockable,
} }
impl Default for LayoutEntryKind { impl Default for LayoutEntryKind {
fn default() -> Self { fn default() -> Self {
Self::Regular Self::Regular
} }
} }
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub struct LayoutEntry { pub struct LayoutEntry {
pub kind: LayoutEntryKind, pub kind: LayoutEntryKind,
pub unshifted: Option<DecodedKey>, pub unshifted: Option<DecodedKey>,
pub shifted: Option<DecodedKey>, pub shifted: Option<DecodedKey>,
pub locked: Option<DecodedKey>, pub locked: Option<DecodedKey>,
pub locked_shifted: Option<DecodedKey>, pub locked_shifted: Option<DecodedKey>,
pub altgr: Option<DecodedKey>, pub altgr: Option<DecodedKey>,
pub raw_unicode: Option<DecodedKey>, pub raw_unicode: Option<DecodedKey>,
} }
impl LayoutEntry { impl LayoutEntry {
#[must_use] #[must_use]
pub fn regular() -> Self { pub fn regular() -> Self {
Self { Self {
kind: LayoutEntryKind::Regular, kind: LayoutEntryKind::Regular,
..Default::default() ..Default::default()
} }
} }
#[must_use] #[must_use]
pub fn numpad() -> Self { pub fn numpad() -> Self {
Self { Self {
kind: LayoutEntryKind::Numlockable, kind: LayoutEntryKind::Numlockable,
..Default::default() ..Default::default()
} }
} }
#[must_use] #[must_use]
pub fn alphabet() -> Self { pub fn alphabet() -> Self {
Self { Self {
kind: LayoutEntryKind::Capslockable, kind: LayoutEntryKind::Capslockable,
..Default::default() ..Default::default()
} }
} }
#[must_use] #[must_use]
pub fn unshifted(mut self, c: impl Into<DecodedKey>) -> Self { pub fn unshifted(mut self, c: impl Into<DecodedKey>) -> Self {
self.unshifted = Some(c.into()); self.unshifted = Some(c.into());
self self
} }
#[must_use] #[must_use]
pub fn shifted(mut self, c: impl Into<DecodedKey>) -> Self { pub fn shifted(mut self, c: impl Into<DecodedKey>) -> Self {
self.shifted = Some(c.into()); self.shifted = Some(c.into());
self self
} }
#[must_use] #[must_use]
pub fn altgr(mut self, c: impl Into<DecodedKey>) -> Self { pub fn altgr(mut self, c: impl Into<DecodedKey>) -> Self {
self.altgr = Some(c.into()); self.altgr = Some(c.into());
self self
} }
#[must_use] #[must_use]
pub fn raw_unicode(mut self, c: impl Into<DecodedKey>) -> Self { pub fn raw_unicode(mut self, c: impl Into<DecodedKey>) -> Self {
self.raw_unicode = Some(c.into()); self.raw_unicode = Some(c.into());
self self
} }
#[must_use] #[must_use]
pub fn locked(mut self, c: impl Into<DecodedKey>) -> Self { pub fn locked(mut self, c: impl Into<DecodedKey>) -> Self {
self.locked = Some(c.into()); self.locked = Some(c.into());
self self
} }
#[must_use] #[must_use]
pub fn locked_shifted(mut self, c: impl Into<DecodedKey>) -> Self { pub fn locked_shifted(mut self, c: impl Into<DecodedKey>) -> Self {
self.locked_shifted = Some(c.into()); self.locked_shifted = Some(c.into());
self self
} }
#[must_use] #[must_use]
pub fn common(self, c: impl Into<DecodedKey> + Clone) -> Self { pub fn common(self, c: impl Into<DecodedKey> + Clone) -> Self {
self self.unshifted(c.clone())
.unshifted(c.clone()) .shifted(c.clone())
.shifted(c.clone()) .locked(c.clone())
.locked(c.clone()) .locked_shifted(c)
.locked_shifted(c) }
} #[must_use]
#[must_use] pub fn low(self, c: impl Into<DecodedKey> + Clone) -> Self {
pub fn low(self, c: impl Into<DecodedKey> + Clone) -> Self { self.unshifted(c.clone()).locked_shifted(c)
self.unshifted(c.clone()).locked_shifted(c) }
} #[must_use]
#[must_use] pub fn high(self, c: impl Into<DecodedKey> + Clone) -> Self {
pub fn high(self, c: impl Into<DecodedKey> + Clone) -> Self { self.shifted(c.clone()).locked(c)
self.shifted(c.clone()).locked(c) }
} #[must_use]
#[must_use] pub fn all(self, c: impl Into<DecodedKey> + Clone) -> Self {
pub fn all(self, c: impl Into<DecodedKey> + Clone) -> Self { self.unshifted(c.clone())
self .shifted(c.clone())
.unshifted(c.clone()) .locked(c.clone())
.shifted(c.clone()) .locked_shifted(c.clone())
.locked(c.clone()) .altgr(c.clone())
.locked_shifted(c.clone()) .raw_unicode(c)
.altgr(c.clone()) }
.raw_unicode(c)
}
} }

View file

@ -1,111 +1,111 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#[derive(Debug)] #[derive(Debug)]
pub struct Modifiers { pub struct Modifiers {
pub lshift: bool, pub lshift: bool,
pub rshift: bool, pub rshift: bool,
pub lctrl: bool, pub lctrl: bool,
pub rctrl: bool, pub rctrl: bool,
pub numlock: bool, pub numlock: bool,
pub capslock: bool, pub capslock: bool,
pub alt_gr: bool, pub alt_gr: bool,
} }
impl Modifiers { impl Modifiers {
pub fn is_shifted(&self) -> bool { pub fn is_shifted(&self) -> bool {
self.lshift | self.rshift self.lshift | self.rshift
} }
pub fn is_ctrl(&self) -> bool { pub fn is_ctrl(&self) -> bool {
self.lctrl | self.rctrl self.lctrl | self.rctrl
} }
pub fn is_caps(&self) -> bool { pub fn is_caps(&self) -> bool {
self.capslock self.capslock
} }
} }
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum KeyState { pub enum KeyState {
Up, Up,
Down, Down,
} }
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub struct KeyEvent { pub struct KeyEvent {
pub code: KeyCode, pub code: KeyCode,
pub state: KeyState, pub state: KeyState,
} }
impl KeyEvent { impl KeyEvent {
pub fn new(code: KeyCode, state: KeyState) -> KeyEvent { pub fn new(code: KeyCode, state: KeyState) -> KeyEvent {
KeyEvent { code, state } KeyEvent { code, state }
} }
} }
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum HandleControl { pub enum HandleControl {
/// If either Ctrl key is held down, convert the letters A through Z into /// If either Ctrl key is held down, convert the letters A through Z into
/// Unicode chars U+0001 through U+001A. If the Ctrl keys are not held /// Unicode chars U+0001 through U+001A. If the Ctrl keys are not held
/// down, letters go through normally. /// down, letters go through normally.
MapLettersToUnicode, MapLettersToUnicode,
/// Don't do anything special - send through the Ctrl key up/down events, /// Don't do anything special - send through the Ctrl key up/down events,
/// and leave the letters as letters. /// and leave the letters as letters.
Ignore, Ignore,
} }
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum DecodeState { pub enum DecodeState {
Start, Start,
Extended, Extended,
Release, Release,
ExtendedRelease, ExtendedRelease,
} }
/// Indicates different error conditions. /// Indicates different error conditions.
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Error { pub enum Error {
BadStartBit, BadStartBit,
BadStopBit, BadStopBit,
ParityError, ParityError,
UnknownKeyCode, UnknownKeyCode,
InvalidState, InvalidState,
} }
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[repr(u8)] #[repr(u8)]
pub enum DecodedKeyKind { pub enum DecodedKeyKind {
RawKey = 0, RawKey = 0,
Unicode = 1, Unicode = 1,
} }
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[repr(C)] #[repr(C)]
pub struct DecodedKey { pub struct DecodedKey {
pub kind: DecodedKeyKind, pub kind: DecodedKeyKind,
pub value: u32, pub value: u32,
} }
impl From<char> for DecodedKey { impl From<char> for DecodedKey {
fn from(ch: char) -> Self { fn from(ch: char) -> Self {
Self { Self {
kind: DecodedKeyKind::Unicode, kind: DecodedKeyKind::Unicode,
value: ch as u32, value: ch as u32,
} }
} }
} }
impl From<KeyCode> for DecodedKey { impl From<KeyCode> for DecodedKey {
fn from(kc: KeyCode) -> Self { fn from(kc: KeyCode) -> Self {
Self { Self {
kind: DecodedKeyKind::RawKey, kind: DecodedKeyKind::RawKey,
value: kc as u32, value: kc as u32,
} }
} }
} }
impl DecodedKey { impl DecodedKey {
pub const ZERO: Self = Self { pub const ZERO: Self = Self {
kind: DecodedKeyKind::Unicode, kind: DecodedKeyKind::Unicode,
value: 0, value: 0,
}; };
pub fn Unicode(ch: char) -> Self { pub fn Unicode(ch: char) -> Self {
Self { Self {
kind: DecodedKeyKind::Unicode, kind: DecodedKeyKind::Unicode,
value: ch.into(), value: ch.into(),
} }
} }
pub fn RawKey(byte: u8) -> Self { pub fn RawKey(byte: u8) -> Self {
Self { Self {
kind: DecodedKeyKind::RawKey, kind: DecodedKeyKind::RawKey,
value: byte.into(), value: byte.into(),
} }
} }
} }
macro_rules! keycode_enum { macro_rules! keycode_enum {
(@get_last $Variant:ident) => { (@get_last $Variant:ident) => {

View file

@ -1,5 +1,4 @@
use alloc::{boxed::Box}; use alloc::boxed::Box;
pub struct BinCodeWriter { pub struct BinCodeWriter {
pub stream: Box<u8>, pub stream: Box<u8>,

View file

@ -1,20 +1,20 @@
pub struct Time { pub struct Time {
pub year: u16, pub year: u16,
pub month: u16, pub month: u16,
pub day: u16, pub day: u16,
pub hour: u16, pub hour: u16,
pub minutes: u16, pub minutes: u16,
pub seconds: u16, pub seconds: u16,
pub microseconds: u32, pub microseconds: u32,
} }
impl fmt::Display for Time { impl fmt::Display for Time {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!( write!(
f, f,
"{:?}/{:?}/{:?} {:02}:{:02}:{:02}", "{:?}/{:?}/{:?} {:02}:{:02}:{:02}",
self.year, self.month, self.day, self.hour, self.minutes, self.seconds self.year, self.month, self.day, self.hour, self.minutes, self.seconds
) )
} }
} }
pub mod kilotime; pub mod kilotime;
use core::fmt; use core::fmt;

View file

@ -3,11 +3,11 @@ use shadeable::pixel_format::Rgba64;
use crate::SCREEN_BUFFER; use crate::SCREEN_BUFFER;
use { use {
ab_glyph::{Font, FontRef, Glyph}, ab_glyph::{Font, FontRef, Glyph},
vga::{ vga::{
colors::Color16, colors::Color16,
writers::{Graphics640x480x16, GraphicsWriter}, writers::{Graphics640x480x16, GraphicsWriter},
}, },
}; };
lazy_static::lazy_static! { lazy_static::lazy_static! {
@ -20,28 +20,27 @@ lazy_static::lazy_static! {
pub static ref VGAE_BUFF_OFFSET_Y: spin::Mutex<u8> = spin::Mutex::new(0); pub static ref VGAE_BUFF_OFFSET_Y: spin::Mutex<u8> = spin::Mutex::new(0);
} }
/// Converts a number to ... i forgor 💀 /// Converts a number to ... i forgor 💀
pub fn num_to_vga16(num: u8) -> Color16 { pub fn num_to_vga16(num: u8) -> Color16 {
use Color16::*; use Color16::*;
match num { match num {
0 => Black, 0 => Black,
1 => Blue, 1 => Blue,
2 => Green, 2 => Green,
3 => Cyan, 3 => Cyan,
4 => Red, 4 => Red,
5 => Magenta, 5 => Magenta,
6 => Brown, 6 => Brown,
7 => LightGrey, 7 => LightGrey,
8 => DarkGrey, 8 => DarkGrey,
9 => LightBlue, 9 => LightBlue,
10 => LightGreen, 10 => LightGreen,
11 => LightCyan, 11 => LightCyan,
12 => LightRed, 12 => LightRed,
13 => Pink, 13 => Pink,
14 => Yellow, 14 => Yellow,
15 => White, 15 => White,
// NOTE: Leasve the in // NOTE: Leasve the in
_ => Color16::Pink, _ => Color16::Pink,
} }
} }

View file

@ -4,46 +4,46 @@ use clap::Parser;
#[clap(version = clap::crate_version!(), author = clap::crate_authors!("\n"))] #[clap(version = clap::crate_version!(), author = clap::crate_authors!("\n"))]
/// Hello Remember this is a feature /// Hello Remember this is a feature
enum Command { enum Command {
Run { Run {
#[clap(long, short)] #[clap(long, short)]
debug: bool, debug: bool,
#[clap(long, short, arg_enum)] #[clap(long, short, arg_enum)]
machine: Option<MachineType>, machine: Option<MachineType>,
}, },
Doc { Doc {
#[clap(long, short, arg_enum)] #[clap(long, short, arg_enum)]
machine: Option<MachineType>, machine: Option<MachineType>,
}, },
} }
#[derive(clap::ArgEnum, Debug, Clone)] #[derive(clap::ArgEnum, Debug, Clone)]
enum MachineType { enum MachineType {
X86, X86,
RISCV, RISCV,
ARM, ARM,
} }
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let args = Command::parse(); let args = Command::parse();
match args { match args {
Command::Run { debug, machine } => { Command::Run { debug, machine } => {
let _dir = xshell::pushd("./ableos"); let _dir = xshell::pushd("./ableos");
let _debug_log: &[&str] = match debug { let _debug_log: &[&str] = match debug {
true => &["-D", "debug.log"], true => &["-D", "debug.log"],
false => &[], false => &[],
}; };
match machine.unwrap_or(MachineType::X86) { match machine.unwrap_or(MachineType::X86) {
MachineType::X86 => { MachineType::X86 => {
xshell::cmd!("cargo run --release").run()?; xshell::cmd!("cargo run --release").run()?;
} }
MachineType::ARM => { MachineType::ARM => {
xshell::cmd!("cargo build --release --target=json_targets/aarch64-ableos.json") xshell::cmd!("cargo build --release --target=json_targets/aarch64-ableos.json")
.run()?; .run()?;
#[rustfmt::skip] #[rustfmt::skip]
xshell::cmd!( xshell::cmd!(
"qemu-system-aarch64 "qemu-system-aarch64
-machine virt -machine virt
@ -53,10 +53,11 @@ fn main() -> anyhow::Result<()> {
-device virtio-keyboard -device virtio-keyboard
" "
).run()?; ).run()?;
} }
MachineType::RISCV => { MachineType::RISCV => {
xshell::cmd!("cargo build --release --target=riscv64gc-unknown-none-elf").run()?; xshell::cmd!("cargo build --release --target=riscv64gc-unknown-none-elf")
#[rustfmt::skip] .run()?;
#[rustfmt::skip]
xshell::cmd!( xshell::cmd!(
"qemu-system-riscv64 "qemu-system-riscv64
-machine virt -machine virt
@ -66,26 +67,27 @@ fn main() -> anyhow::Result<()> {
-bios src/arch/riscv/firmwear/opensbi-riscv64-generic-fw_jump.bin -bios src/arch/riscv/firmwear/opensbi-riscv64-generic-fw_jump.bin
-kernel target/riscv64gc-unknown-none-elf/release/ableos" -kernel target/riscv64gc-unknown-none-elf/release/ableos"
).run()?; ).run()?;
}
} }
} }
}
Command::Doc { machine } => { Command::Doc { machine } => {
let _dir = xshell::pushd("./ableos"); let _dir = xshell::pushd("./ableos");
match machine.unwrap_or(MachineType::X86) { match machine.unwrap_or(MachineType::X86) {
MachineType::X86 => { MachineType::X86 => {
xshell::cmd!("cargo doc --open").run()?; xshell::cmd!("cargo doc --open").run()?;
}
MachineType::ARM => {
xshell::cmd!("cargo doc --open --target=json_targets/aarch64-ableos.json")
.run()?;
}
MachineType::RISCV => {
xshell::cmd!("cargo doc --open --target=riscv64gc-unknown-none-elf").run()?;
}
} }
MachineType::ARM => { }
xshell::cmd!("cargo doc --open --target=json_targets/aarch64-ableos.json").run()?; }
}
MachineType::RISCV => {
xshell::cmd!("cargo doc --open --target=riscv64gc-unknown-none-elf").run()?;
}
}
}
}
Ok(()) Ok(())
} }

View file

@ -4,167 +4,164 @@ use vga::colors::Color16;
pub type Rgba64 = u64; pub type Rgba64 = u64;
pub fn get_r(rgba: Rgba64) -> u8 { pub fn get_r(rgba: Rgba64) -> u8 {
rgba.bitand(0xff_00_00_00).shr(0o30) as u8 rgba.bitand(0xff_00_00_00).shr(0o30) as u8
} }
pub fn get_g(rgba: Rgba64) -> u8 { pub fn get_g(rgba: Rgba64) -> u8 {
rgba.bitand(0xff_00_00).shr(0o20) as u8 rgba.bitand(0xff_00_00).shr(0o20) as u8
} }
pub fn get_b(rgba: Rgba64) -> u8 { pub fn get_b(rgba: Rgba64) -> u8 {
rgba.bitand(0xff_00).shr(0o10) as u8 rgba.bitand(0xff_00).shr(0o10) as u8
} }
pub fn get_a(rgba: Rgba64) -> u8 { pub fn get_a(rgba: Rgba64) -> u8 {
(rgba & 0xff) as u8 (rgba & 0xff) as u8
} }
pub fn set_r(rgba: Rgba64, r: u8) -> Rgba64 { pub fn set_r(rgba: Rgba64, r: u8) -> Rgba64 {
rgba rgba.bitand(0xffffffff_00_ff_ff_ff)
.bitand(0xffffffff_00_ff_ff_ff) .bitor((r as Rgba64).shr(0o30))
.bitor((r as Rgba64).shr(0o30))
} }
pub fn set_g(rgba: Rgba64, g: u8) -> Rgba64 { pub fn set_g(rgba: Rgba64, g: u8) -> Rgba64 {
rgba rgba.bitand(0xffffffff_ff_00_ff_ff)
.bitand(0xffffffff_ff_00_ff_ff) .bitor((g as Rgba64).shr(0o20))
.bitor((g as Rgba64).shr(0o20))
} }
pub fn set_b(rgba: Rgba64, b: u8) -> Rgba64 { pub fn set_b(rgba: Rgba64, b: u8) -> Rgba64 {
rgba rgba.bitand(0xffffffff_ff_ff_00_ff)
.bitand(0xffffffff_ff_ff_00_ff) .bitor((b as Rgba64).shr(0o10))
.bitor((b as Rgba64).shr(0o10))
} }
pub fn set_a(rgba: Rgba64, a: u8) -> Rgba64 { pub fn set_a(rgba: Rgba64, a: u8) -> Rgba64 {
rgba.bitand(0xffffffff_ff_ff_ff_00).bitor(a as Rgba64) rgba.bitand(0xffffffff_ff_ff_ff_00).bitor(a as Rgba64)
} }
pub fn rgba_div(a: Rgba64, b: Rgba64) -> Rgba64 { pub fn rgba_div(a: Rgba64, b: Rgba64) -> Rgba64 {
set_r(0, get_r(a) / get_r(b)) set_r(0, get_r(a) / get_r(b))
| set_g(0, get_g(a) / get_g(b)) | set_g(0, get_g(a) / get_g(b))
| set_g(0, get_b(a) / get_b(b)) | set_g(0, get_b(a) / get_b(b))
| set_g(0, get_a(a) / get_a(b)) | set_g(0, get_a(a) / get_a(b))
} }
pub fn new_rgba64(r: u8, g: u8, b: u8, a: u8) -> Rgba64 { pub fn new_rgba64(r: u8, g: u8, b: u8, a: u8) -> Rgba64 {
set_r(0, r) | set_g(0, g) | set_b(0, b) | set_a(0, a) set_r(0, r) | set_g(0, g) | set_b(0, b) | set_a(0, a)
} }
enum ChannelValue { enum ChannelValue {
Dark, Dark,
Low, Low,
Mid, Mid,
High, High,
} }
impl From<u8> for ChannelValue { impl From<u8> for ChannelValue {
fn from(b: u8) -> Self { fn from(b: u8) -> Self {
use ChannelValue::*; use ChannelValue::*;
match b { match b {
0x00..=0x3f => Dark, 0x00..=0x3f => Dark,
0x40..=0x7f => Low, 0x40..=0x7f => Low,
0x80..=0xbf => Mid, 0x80..=0xbf => Mid,
0xc0..=0xff => High, 0xc0..=0xff => High,
} }
} }
} }
pub fn into_vga_16(rgba_64: Rgba64) -> Color16 { pub fn into_vga_16(rgba_64: Rgba64) -> Color16 {
use ChannelValue::*; use ChannelValue::*;
use Color16::*; use Color16::*;
match ( match (
get_r(rgba_64).into(), get_r(rgba_64).into(),
get_g(rgba_64).into(), get_g(rgba_64).into(),
get_b(rgba_64).into(), get_b(rgba_64).into(),
) { ) {
(Dark, Dark, Dark) => Black, (Dark, Dark, Dark) => Black,
(Dark, Dark, Low) => Black, (Dark, Dark, Low) => Black,
(Dark, Dark, Mid) => Blue, (Dark, Dark, Mid) => Blue,
(Dark, Dark, High) => Blue, (Dark, Dark, High) => Blue,
(Dark, Low, Dark) => Black, (Dark, Low, Dark) => Black,
(Dark, Low, Low) => Black, (Dark, Low, Low) => Black,
(Dark, Low, Mid) => Blue, (Dark, Low, Mid) => Blue,
(Dark, Low, High) => Blue, (Dark, Low, High) => Blue,
(Dark, Mid, Dark) => Green, (Dark, Mid, Dark) => Green,
(Dark, Mid, Low) => Green, (Dark, Mid, Low) => Green,
(Dark, Mid, Mid) => Cyan, (Dark, Mid, Mid) => Cyan,
(Dark, Mid, High) => Cyan, (Dark, Mid, High) => Cyan,
(Dark, High, Dark) => Green, (Dark, High, Dark) => Green,
(Dark, High, Low) => Green, (Dark, High, Low) => Green,
(Dark, High, Mid) => Green, (Dark, High, Mid) => Green,
(Dark, High, High) => Cyan, (Dark, High, High) => Cyan,
(Low, Dark, Dark) => Black, (Low, Dark, Dark) => Black,
(Low, Dark, Low) => Black, (Low, Dark, Low) => Black,
(Low, Dark, Mid) => Blue, (Low, Dark, Mid) => Blue,
(Low, Dark, High) => Blue, (Low, Dark, High) => Blue,
(Low, Low, Dark) => Black, (Low, Low, Dark) => Black,
(Low, Low, Low) => DarkGrey, (Low, Low, Low) => DarkGrey,
(Low, Low, Mid) => LightGrey, (Low, Low, Mid) => LightGrey,
(Low, Low, High) => Blue, (Low, Low, High) => Blue,
(Low, Mid, Dark) => DarkGrey, (Low, Mid, Dark) => DarkGrey,
(Low, Mid, Low) => LightGrey, (Low, Mid, Low) => LightGrey,
(Low, Mid, Mid) => Cyan, (Low, Mid, Mid) => Cyan,
(Low, Mid, High) => Cyan, (Low, Mid, High) => Cyan,
(Low, High, Dark) => Green, (Low, High, Dark) => Green,
(Low, High, Low) => Green, (Low, High, Low) => Green,
(Low, High, Mid) => Cyan, (Low, High, Mid) => Cyan,
(Low, High, High) => Cyan, (Low, High, High) => Cyan,
(Mid, Dark, Dark) => Red, (Mid, Dark, Dark) => Red,
(Mid, Dark, Low) => Red, (Mid, Dark, Low) => Red,
(Mid, Dark, Mid) => Magenta, (Mid, Dark, Mid) => Magenta,
(Mid, Dark, High) => Magenta, (Mid, Dark, High) => Magenta,
(Mid, Low, Dark) => Brown, (Mid, Low, Dark) => Brown,
(Mid, Low, Low) => Red, (Mid, Low, Low) => Red,
(Mid, Low, Mid) => DarkGrey, (Mid, Low, Mid) => DarkGrey,
(Mid, Low, High) => LightBlue, (Mid, Low, High) => LightBlue,
(Mid, Mid, Dark) => Brown, (Mid, Mid, Dark) => Brown,
(Mid, Mid, Low) => Brown, (Mid, Mid, Low) => Brown,
(Mid, Mid, Mid) => LightGrey, (Mid, Mid, Mid) => LightGrey,
(Mid, Mid, High) => LightBlue, (Mid, Mid, High) => LightBlue,
(Mid, High, Dark) => Green, (Mid, High, Dark) => Green,
(Mid, High, Low) => Green, (Mid, High, Low) => Green,
(Mid, High, Mid) => LightGreen, (Mid, High, Mid) => LightGreen,
(Mid, High, High) => LightCyan, (Mid, High, High) => LightCyan,
(High, Dark, Dark) => Red, (High, Dark, Dark) => Red,
(High, Dark, _) => Magenta, (High, Dark, _) => Magenta,
(High, Low, Dark) => Red, (High, Low, Dark) => Red,
(High, Low, Low) => LightRed, (High, Low, Low) => LightRed,
(High, Low, Mid) => Pink, (High, Low, Mid) => Pink,
(High, Low, High) => Magenta, (High, Low, High) => Magenta,
(High, Mid, Dark) => Yellow, (High, Mid, Dark) => Yellow,
(High, Mid, Low) => Yellow, (High, Mid, Low) => Yellow,
(High, Mid, Mid) => LightRed, (High, Mid, Mid) => LightRed,
(High, Mid, High) => Pink, (High, Mid, High) => Pink,
(High, High, Dark) => Yellow, (High, High, Dark) => Yellow,
(High, High, Low) => White, (High, High, Low) => White,
(High, High, Mid) => White, (High, High, Mid) => White,
(High, High, High) => White, (High, High, High) => White,
} }
} }
pub fn from_vga_16(color: Color16) -> Rgba64 { pub fn from_vga_16(color: Color16) -> Rgba64 {
use Color16::*; use Color16::*;
match color { match color {
Black => new_rgba64(0, 0, 0, 0), Black => new_rgba64(0, 0, 0, 0),
DarkGrey => new_rgba64(105, 105, 105, 0), DarkGrey => new_rgba64(105, 105, 105, 0),
LightGrey => new_rgba64(211, 211, 211, 0), LightGrey => new_rgba64(211, 211, 211, 0),
// //
Blue => new_rgba64(0, 0, 0xff, 0), Blue => new_rgba64(0, 0, 0xff, 0),
Green => new_rgba64(0, 0xff, 0, 0), Green => new_rgba64(0, 0xff, 0, 0),
Red => new_rgba64(0xff, 0, 0, 0), Red => new_rgba64(0xff, 0, 0, 0),
// //
Yellow => new_rgba64(0xff, 0xff, 0, 0), Yellow => new_rgba64(0xff, 0xff, 0, 0),
Cyan => new_rgba64(0, 0xff, 0xff, 0), Cyan => new_rgba64(0, 0xff, 0xff, 0),
Magenta => new_rgba64(0xff, 0, 0xff, 0), Magenta => new_rgba64(0xff, 0, 0xff, 0),
Brown => new_rgba64(165, 42, 42, 0), Brown => new_rgba64(165, 42, 42, 0),
Pink => new_rgba64(0xff, 105, 180, 0), Pink => new_rgba64(0xff, 105, 180, 0),
White => new_rgba64(0xff, 0xff, 0xff, 0), White => new_rgba64(0xff, 0xff, 0xff, 0),
LightBlue => new_rgba64(173, 216, 230, 0), LightBlue => new_rgba64(173, 216, 230, 0),
LightGreen => new_rgba64(144, 238, 144, 0), LightGreen => new_rgba64(144, 238, 144, 0),
LightCyan => new_rgba64(88, 100, 100, 0), LightCyan => new_rgba64(88, 100, 100, 0),
LightRed => new_rgba64(0xff, 204, 203, 0), LightRed => new_rgba64(0xff, 204, 203, 0),
} }
} }