refactor: Devide lib.rs into modules

This commit is contained in:
Gleb Zaharov 2024-01-12 11:39:38 +03:00
parent 994ae2f392
commit c6351affe3
9 changed files with 171 additions and 162 deletions

View File

@ -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();
}

View File

@ -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<Color>,
}
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<Event> {
match unsafe { sys::wait_event() } {
1 => Some(Event::Redraw),
2 => Some(Event::KeyPress),
_ => None,
}
}
pub fn fetch_key() -> Option<u8> {
let res = unsafe { sys::pressed_key() };
if res == 1 {
None
} else {
Some(((res >> 8) & 0xff) as u8)
}
}
pub use modules::*;

4
src/modules.rs Normal file
View File

@ -0,0 +1,4 @@
pub mod graphics;
pub mod input;
pub mod threads;
pub mod windows;

53
src/modules/graphics.rs Normal file
View File

@ -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<Color>) {
// 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(),
);
}
}

10
src/modules/input.rs Normal file
View File

@ -0,0 +1,10 @@
use crate::sys;
pub fn fetch_key() -> Option<u8> {
let res = unsafe { sys::pressed_key() };
if res == 1 {
None
} else {
Some(((res >> 8) & 0xff) as u8)
}
}

19
src/modules/threads.rs Normal file
View File

@ -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<Event> {
match unsafe { sys::wait_event() } {
1 => Some(Event::Redraw),
2 => Some(Event::KeyPress),
_ => None,
}
}

50
src/modules/windows.rs Normal file
View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -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: