From c6351affe39c61b8441cc0cff295ccd92d28fce3 Mon Sep 17 00:00:00 2001 From: sweetbread Date: Fri, 12 Jan 2024 11:39:38 +0300 Subject: [PATCH] refactor: Devide lib.rs into modules --- example/hwa.rs | 28 ++++----- src/lib.rs | 132 +--------------------------------------- src/modules.rs | 4 ++ src/modules/graphics.rs | 53 ++++++++++++++++ src/modules/input.rs | 10 +++ src/modules/threads.rs | 19 ++++++ src/modules/windows.rs | 50 +++++++++++++++ src/sys.rs | 28 ++++++--- src/syscalls.S | 9 +-- 9 files changed, 171 insertions(+), 162 deletions(-) create mode 100644 src/modules.rs create mode 100644 src/modules/graphics.rs create mode 100644 src/modules/input.rs create mode 100644 src/modules/threads.rs create mode 100644 src/modules/windows.rs diff --git a/example/hwa.rs b/example/hwa.rs index f5fa150..dd62107 100644 --- a/example/hwa.rs +++ b/example/hwa.rs @@ -3,15 +3,18 @@ use cstr_core::{cstr, CStr}; -use kos::{Color, Dot, Event, Size, WindowKind, WindowParams, WindowTextParams}; +use kos::graphics::{display_message, Color, Dot, Size}; +use kos::input::fetch_key; +use kos::threads::{exit, fetch_event, Event}; +use kos::windows::{define_window, end_window_draw, start_window_draw, WindowKind, WindowParams}; const HEADER: &CStr = cstr!("Hey Kolibri"); -const MSG: &str = "Hello from Rust!"; +const MSG: &CStr = cstr!("Hello from Rust!"); #[inline(always)] // for some reason function removed otherwise fn draw_window() { - kos::start_window_draw(); - kos::define_window( + start_window_draw(); + define_window( Dot { x: 50, y: 50 }, Size { width: 300, @@ -23,28 +26,21 @@ fn draw_window() { title: Some(HEADER), }, ); - kos::display_message( - Dot { x: 0, y: 10 }, - WindowTextParams { - color: Color::rgb(0x66, 0x22, 0x22), - text: MSG, - bg_color: None, - }, - ); - kos::end_window_draw(); + display_message(Dot { x: 0, y: 10 }, Color::rgb(0x66, 0x22, 0x22), MSG, None); + end_window_draw(); } #[no_mangle] fn kol_main() -> ! { draw_window(); - while let Some(ev) = kos::fetch_event() { + while let Some(ev) = fetch_event() { match ev { Event::Redraw => draw_window(), - Event::KeyPress => drop(kos::fetch_key()), + Event::KeyPress => drop(fetch_key()), _ => break, } } - kos::exit(); + exit(); } diff --git a/src/lib.rs b/src/lib.rs index 7b60602..838fe2e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,135 +1,7 @@ #![no_std] +mod modules; mod nanolibc; mod sys; -pub use sys::*; - -#[derive(Clone, Copy)] -pub struct Color(u8, u8, u8); - -impl Color { - pub fn rgb(r: u8, g: u8, b: u8) -> Self { - Self(r, g, b) - } - - pub fn r(&self) -> u8 { - self.0 - } - - pub fn g(&self) -> u8 { - self.1 - } - - pub fn b(&self) -> u8 { - self.2 - } - - pub fn as_rgb_val(self) -> u32 { - (self.0 as u32) << 16 | (self.1 as u32) << 8 | (self.2 as u32) - } -} - -pub struct Dot { - pub x: u32, - pub y: u32, -} - -pub struct Size { - pub width: u32, - pub height: u32, -} - -#[repr(u32)] -pub enum WindowKind { - Fixed = 0, - NoDraw = 1, - Resizable = 2, - Themed = 3, - FixedThemed = 4, -} - -pub struct WindowParams<'a> { - pub color: Color, - pub kind: WindowKind, - pub title: Option<&'a cstr_core::CStr>, -} - -pub fn define_window(start: Dot, size: Size, params: WindowParams<'_>) { - const RELATIVE_FLAG: u32 = 0x20; - - unsafe { - sys::define_window( - start.x << 16 | size.width, - start.y << 16 | size.height, - params.color.as_rgb_val() - | (RELATIVE_FLAG | (params.title.is_some() as u32) << 4 | params.kind as u32) << 24, - 0, - params - .title - .map(|s| s.as_ptr()) - .unwrap_or_else(core::ptr::null) as u32, - ); - } -} - -pub struct WindowTextParams<'a> { - pub color: Color, - pub text: &'a str, - pub bg_color: Option, -} - -pub fn display_message(start: Dot, params: WindowTextParams<'_>) { - const UTF8_FLAG: u32 = 0b0011_0000 << 24; - const BG_FLAG: u32 = 0b0100_0000 << 24; - - unsafe { - sys::display_message( - start.x << 16 | start.y, - params.color.as_rgb_val() | BG_FLAG * params.bg_color.is_some() as u32 | UTF8_FLAG, - params.text.as_ptr() as u32, - params.text.len() as u32, - 0, - ); - } -} - -pub fn exit() -> ! { - unsafe { sys::exit() } -} - -pub fn start_window_draw() { - unsafe { sys::start_window_draw() } -} - -pub fn end_window_draw() { - unsafe { sys::end_window_draw() } -} - -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - exit(); -} - -#[non_exhaustive] -pub enum Event { - Redraw, - KeyPress, -} - -pub fn fetch_event() -> Option { - match unsafe { sys::wait_event() } { - 1 => Some(Event::Redraw), - 2 => Some(Event::KeyPress), - _ => None, - } -} - -pub fn fetch_key() -> Option { - let res = unsafe { sys::pressed_key() }; - if res == 1 { - None - } else { - Some(((res >> 8) & 0xff) as u8) - } -} +pub use modules::*; diff --git a/src/modules.rs b/src/modules.rs new file mode 100644 index 0000000..8321061 --- /dev/null +++ b/src/modules.rs @@ -0,0 +1,4 @@ +pub mod graphics; +pub mod input; +pub mod threads; +pub mod windows; diff --git a/src/modules/graphics.rs b/src/modules/graphics.rs new file mode 100644 index 0000000..1df1c47 --- /dev/null +++ b/src/modules/graphics.rs @@ -0,0 +1,53 @@ +use crate::sys; +use cstr_core::CStr; + +#[derive(Clone, Copy)] +pub struct Color(u8, u8, u8); + +impl Color { + pub fn rgb(r: u8, g: u8, b: u8) -> Self { + Self(r, g, b) + } + + pub fn r(&self) -> u8 { + self.0 + } + + pub fn g(&self) -> u8 { + self.1 + } + + pub fn b(&self) -> u8 { + self.2 + } + + pub fn as_rgb_val(self) -> u32 { + (self.0 as u32) << 16 | (self.1 as u32) << 8 | (self.2 as u32) + } +} + +pub struct Dot { + pub x: u32, + pub y: u32, +} + +pub struct Size { + pub width: u32, + pub height: u32, +} + +pub fn display_message<'a>(start: Dot, color: Color, text: &'a CStr, bg_color: Option) { + // XX=ABFFCSSS + const UTF8_FLAG: u32 = (3 << 4) << 24; // FF + const BG_FLAG: u32 = (1 << 6) << 24; // B + const ASCIIZ_FLAG: u32 = (1 << 7) << 24; // A + + unsafe { + sys::display_message( + start.x << 16 | start.y, + color.as_rgb_val() | BG_FLAG * bg_color.is_some() as u32 | UTF8_FLAG | ASCIIZ_FLAG, + text.as_ptr() as u32, + bg_color.unwrap_or(Color(0, 0, 0)).as_rgb_val(), + ); + } +} diff --git a/src/modules/input.rs b/src/modules/input.rs new file mode 100644 index 0000000..991fc3e --- /dev/null +++ b/src/modules/input.rs @@ -0,0 +1,10 @@ +use crate::sys; + +pub fn fetch_key() -> Option { + let res = unsafe { sys::pressed_key() }; + if res == 1 { + None + } else { + Some(((res >> 8) & 0xff) as u8) + } +} diff --git a/src/modules/threads.rs b/src/modules/threads.rs new file mode 100644 index 0000000..fd70a19 --- /dev/null +++ b/src/modules/threads.rs @@ -0,0 +1,19 @@ +use crate::sys; + +pub fn exit() -> ! { + unsafe { sys::exit() } +} + +#[non_exhaustive] +pub enum Event { + Redraw, + KeyPress, +} + +pub fn fetch_event() -> Option { + match unsafe { sys::wait_event() } { + 1 => Some(Event::Redraw), + 2 => Some(Event::KeyPress), + _ => None, + } +} diff --git a/src/modules/windows.rs b/src/modules/windows.rs new file mode 100644 index 0000000..e042844 --- /dev/null +++ b/src/modules/windows.rs @@ -0,0 +1,50 @@ +use crate::graphics::{Color, Dot, Size}; +use crate::sys; + +#[repr(u32)] +pub enum WindowKind { + Fixed = 0, + NoDraw = 1, + Resizable = 2, + Themed = 3, + FixedThemed = 4, +} + +pub struct WindowParams<'a> { + pub color: Color, + pub kind: WindowKind, + pub title: Option<&'a cstr_core::CStr>, +} + +pub fn define_window(start: Dot, size: Size, params: WindowParams<'_>) { + const RELATIVE_FLAG: u32 = 0x20; + + unsafe { + sys::define_window( + start.x << 16 | size.width, + start.y << 16 | size.height, + params.color.as_rgb_val() + | (RELATIVE_FLAG | (params.title.is_some() as u32) << 4 | params.kind as u32) << 24, + 0, + params + .title + .map(|s| s.as_ptr()) + .unwrap_or_else(core::ptr::null) as u32, + ); + } +} + +pub fn start_window_draw() { + unsafe { sys::start_window_draw() } +} + +pub fn end_window_draw() { + unsafe { sys::end_window_draw() } +} + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + unsafe { + sys::exit(); + } +} diff --git a/src/sys.rs b/src/sys.rs index 6abf83b..c5b4eb2 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -1,22 +1,30 @@ #[link(name = "syscalls")] extern "C" { - #[link_name = "_start_window_draw"] - pub fn start_window_draw(); - - #[link_name = "_end_window_draw"] - pub fn end_window_draw(); - + // -1 #[link_name = "_exit"] pub fn exit() -> !; + // 0 #[link_name = "_define_window"] pub fn define_window(ebx: u32, ecx: u32, edx: u32, esi: u32, edi: u32); - #[link_name = "_display_message"] - pub fn display_message(ebx: u32, ecx: u32, edx: u32, esi: u32, edi: u32); + // 2 + #[link_name = "_pressed_key"] + pub fn pressed_key() -> u32; + + // 4 + #[link_name = "_display_message"] + pub fn display_message(ebx: u32, ecx: u32, edx: u32, edi: u32); + + // 10 #[link_name = "_wait_event"] pub fn wait_event() -> u32; - #[link_name = "_pressed_key"] - pub fn pressed_key() -> u32; + // 12.1 + #[link_name = "_start_window_draw"] + pub fn start_window_draw(); + + // 12.2 + #[link_name = "_end_window_draw"] + pub fn end_window_draw(); } diff --git a/src/syscalls.S b/src/syscalls.S index c92386d..2ffe630 100644 --- a/src/syscalls.S +++ b/src/syscalls.S @@ -42,17 +42,14 @@ _define_window: ret _display_message: - push edi - push esi + push esi edi mov eax, SF_DRAW_TEXT mov ebx, dword [esp + 0x0c] mov ecx, dword [esp + 0x10] mov edx, dword [esp + 0x14] - mov esi, dword [esp + 0x18] - mov edi, dword [esp + 0x1c] + mov edi, dword [esp + 0x18] int 0x40 - pop esi - pop edi + pop edi esi ret _wait_event: