Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

11 changed files with 42 additions and 173 deletions

View File

@ -14,10 +14,11 @@ path = "examples/hwa.rs"
name = "con" name = "con"
path = "examples/con.rs" path = "examples/con.rs"
[[example]]
name = "dark"
path = "examples/dark.rs"
[profile.release] [profile.release]
opt-level = "z" opt-level = "z"
lto = "thin" lto = "thin"
[dependencies]
cstr_core = { version = "0.2.6", default-features = false, features = ["nightly"] }
[build-dependencies]

View File

@ -1,15 +1,19 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
use cstr_core::cstr;
use kos::{dll::Console, threads::exit}; use kos::{dll::Console, threads::exit};
extern crate alloc; extern crate alloc;
#[no_mangle] #[no_mangle]
pub fn kol_main() { pub fn kol_main() {
let header = cstr!("Rust!");
let string = "Hi from Rust!";
let con_lib = Console::import(None).unwrap(); let con_lib = Console::import(None).unwrap();
con_lib.init(u32::MAX, u32::MAX, u32::MAX, u32::MAX, c"Rust!"); con_lib.init(u32::MAX, u32::MAX, u32::MAX, u32::MAX, header);
con_lib.write_string("Hi from Rust!"); con_lib.write_string(string);
con_lib.exit(false); con_lib.exit(false);
exit(); exit();

View File

@ -1,116 +0,0 @@
#![no_std]
#![no_main]
use kos::{
graphics::{display_message, Color, Dot, Size},
input::fetch_key,
threads::{exit, fetch_event, Event},
windows::{
define_button, define_window, end_window_draw, get_button_id, invert_pixel,
start_window_draw, WindowKind, WindowParams, CLOSE_BUTTON,
},
};
const HEADER: &'static CStr = c"Dark Mode Demo";
const TEXT: [&'static CStr; 6] = [
c"Lorem ipsum dolor sit amet,",
c"semper et rutrum placerat,",
c"Integer sed diam commodo quam varius",
c"Sed finibus urna sit amet felis",
c"vestibulum elementum. Maecenas at feugiat lacus",
c"tristique et sit amet tortor.",
];
const BTN: u32 = 42;
const WINDOW_SIZE: Size = Size {
width: 400,
height: 400,
};
extern crate alloc;
fn draw_window(invert: bool) {
start_window_draw();
define_window(
Dot { x: 50, y: 50 },
WINDOW_SIZE,
WindowParams {
color: Color::rgb(0xff, 0xff, 0xff),
kind: WindowKind::FixedThemed,
title: Some(HEADER),
},
);
display_message(Dot { x: 10, y: 10 }, Color::rgb(0, 0, 0), TEXT[0], None);
display_message(Dot { x: 10, y: 50 }, Color::rgb(0, 0, 0), TEXT[1], None);
display_message(Dot { x: 10, y: 90 }, Color::rgb(0, 0, 0), TEXT[2], None);
display_message(Dot { x: 10, y: 130 }, Color::rgb(0, 0, 0), TEXT[3], None);
display_message(Dot { x: 10, y: 170 }, Color::rgb(0, 0, 0), TEXT[4], None);
display_message(Dot { x: 10, y: 210 }, Color::rgb(0, 0, 0), TEXT[5], None);
define_button(
Dot { x: 10, y: 300 },
Size {
width: 100,
height: 30,
},
BTN,
true,
true,
Some(Color::rgb(147, 112, 219)),
);
display_message(
Dot { x: 20, y: 310 },
Color::rgb(255, 255, 255),
if invert {
c"Light mode"
} else {
c"Dark mode"
},
None,
);
if invert {
for x in 0..WINDOW_SIZE.width {
for y in 0..WINDOW_SIZE.height {
invert_pixel(Dot { x, y })
}
}
}
end_window_draw();
return;
}
fn button_handler(invert: &mut bool) {
let btn_id = get_button_id();
if btn_id.is_some() {
match btn_id.unwrap() {
CLOSE_BUTTON => exit(),
BTN => {
*invert = !*invert;
draw_window(*invert);
}
_ => {}
}
}
}
#[no_mangle]
fn kol_main() {
let mut invert = false;
while let Some(ev) =
fetch_event((Event::Redraw as u32) | (Event::KeyPress as u32) | (Event::BtnPress as u32))
{
match ev {
Event::Redraw => draw_window(invert),
Event::KeyPress => drop(fetch_key()),
Event::BtnPress => button_handler(&mut invert),
_ => break,
}
}
}

View File

@ -1,8 +1,8 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
use alloc::ffi::CString; use cstr_core::{cstr, CStr};
use core::ffi::CStr;
use kos::{ use kos::{
graphics::{display_message, Color, Dot, Size}, graphics::{display_message, Color, Dot, Size},
input::fetch_key, input::fetch_key,
@ -14,8 +14,8 @@ use kos::{
}, },
}; };
const HEADER: &CStr = c"Hey Kolibri"; const HEADER: &CStr = cstr!("Hey Kolibri");
const MSG: &CStr = c"Hello from Rust!"; const MSG: &CStr = cstr!("Hello from Rust!");
const BTN: u32 = 42; const BTN: u32 = 42;
#[macro_use] #[macro_use]
@ -45,18 +45,16 @@ fn draw_window(c: usize) {
); );
let btn_str = match get_lang() { let btn_str = match get_lang() {
Lang::German => format!("Taste gedrückt: {} mal", c), Lang::German => format!("Taste gedrückt: {} mal\0", c),
Lang::Russian => format!("Кнопка нажата: {} раз", c), Lang::Russian => format!("Кнопка нажата: {} раз\0", c),
Lang::French => format!("Button enfoncé : {} fois", c), Lang::French => format!("Button enfoncé : {} fois\0", c),
_ => format!("Button pressed: {} times", c), _ => format!("Button pressed: {} times\0", c),
}; };
display_message( display_message(
Dot { x: 10, y: 30 }, Dot { x: 10, y: 30 },
Color::rgb(0, 0, 0), Color::rgb(0, 0, 0),
CString::new(btn_str.as_bytes()) CStr::from_bytes_with_nul(btn_str.as_bytes()).unwrap_or(cstr!("String error")),
.expect("CString error")
.as_c_str(),
None, None,
); );

View File

@ -1,5 +1,5 @@
use crate::sys; use crate::sys;
use core::ffi::CStr; use cstr_core::CStr;
mod console; mod console;
pub use console::Console; pub use console::Console;

View File

@ -1,6 +1,7 @@
use crate::dll::DLL; use crate::dll::DLL;
use core::ffi::CStr;
use core::mem::transmute; use core::mem::transmute;
use cstr_core::cstr;
use cstr_core::CStr;
pub struct Console { pub struct Console {
con_init: extern "stdcall" fn(u32, u32, u32, u32, *const u8), con_init: extern "stdcall" fn(u32, u32, u32, u32, *const u8),
@ -10,14 +11,16 @@ pub struct Console {
impl Console { impl Console {
pub fn import(path: Option<&CStr>) -> Result<Self, &str> { pub fn import(path: Option<&CStr>) -> Result<Self, &str> {
let lib = DLL::load_dll(path.unwrap_or(c"/sys/lib/console.obj")); let lib = DLL::load_dll(path.unwrap_or(cstr!("/sys/lib/console.obj")));
match lib { match lib {
Err(e) => return Err(e), Err(e) => return Err(e),
Ok(dll) => unsafe { Ok(dll) => unsafe {
Ok(Console { Ok(Console {
con_init: transmute(dll.get_func(c"con_init").ok().unwrap()), con_init: transmute(dll.get_func(cstr!("con_init")).ok().unwrap()),
con_write_string: transmute(dll.get_func(c"con_write_string").ok().unwrap()), con_write_string: transmute(
con_exit: transmute(dll.get_func(c"con_exit").ok().unwrap()), dll.get_func(cstr!("con_write_string")).ok().unwrap(),
),
con_exit: transmute(dll.get_func(cstr!("con_exit")).ok().unwrap()),
}) })
}, },
} }

View File

@ -1,5 +1,5 @@
use crate::sys; use crate::sys;
use core::ffi::CStr; use cstr_core::CStr;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Color(u8, u8, u8); pub struct Color(u8, u8, u8);

View File

@ -1,7 +1,7 @@
use crate::sys; use crate::sys;
use crate::throw_new; use crate::throw_new;
use alloc::string::String; use alloc::string::String;
use core::ffi::CStr; use cstr_core::CStr;
trait Debuggable { trait Debuggable {
fn data_iter(self) -> impl Iterator<Item = u8>; fn data_iter(self) -> impl Iterator<Item = u8>;

View File

@ -2,8 +2,7 @@ use crate::graphics::{Color, Dot, Size};
use crate::sys; use crate::sys;
use crate::system::debug_write; use crate::system::debug_write;
use crate::throw_new; use crate::throw_new;
use alloc::ffi::CString; use cstr_core::{cstr, CStr};
use core::ffi::CStr;
#[repr(u32)] #[repr(u32)]
pub enum WindowKind { pub enum WindowKind {
@ -17,12 +16,12 @@ pub enum WindowKind {
pub struct WindowParams<'a> { pub struct WindowParams<'a> {
pub color: Color, pub color: Color,
pub kind: WindowKind, pub kind: WindowKind,
pub title: Option<&'a CStr>, pub title: Option<&'a cstr_core::CStr>,
} }
pub const CLOSE_BUTTON: u32 = 1; pub const CLOSE_BUTTON: u32 = 1;
pub fn define_window(start: Dot, size: Size, params: WindowParams) { pub fn define_window(start: Dot, size: Size, params: WindowParams<'_>) {
const RELATIVE_FLAG: u32 = 0x20; const RELATIVE_FLAG: u32 = 0x20;
unsafe { unsafe {
@ -40,15 +39,6 @@ pub fn define_window(start: Dot, size: Size, params: WindowParams) {
} }
} }
pub fn put_pixel(pos: Dot, color: Option<Color>) {
let color: u32 = color.unwrap_or(Color::rgb(255, 255, 255)).as_rgb_val();
unsafe { sys::put_pixel(pos.x, pos.y, color) }
}
pub fn invert_pixel(pos: Dot) {
unsafe { sys::put_pixel(pos.x, pos.y, 1 << 24) }
}
pub fn define_button( pub fn define_button(
start: Dot, start: Dot,
size: Size, size: Size,
@ -61,12 +51,14 @@ pub fn define_button(
crate::graphics::display_message( crate::graphics::display_message(
Dot { x: 10, y: 200 }, Dot { x: 10, y: 200 },
Color::rgb(255, 0, 0), Color::rgb(255, 0, 0),
CString::new(format!( CStr::from_bytes_with_nul(
"x:{:?} y:{:?} w:{:?} h:{:?}\n", format!(
start.x, start.y, size.width, size.height "x:{:?} y:{:?} w:{:?} h:{:?}\n\0",
)) start.x, start.y, size.width, size.height
.expect("CString error") )
.as_c_str(), .as_bytes(),
)
.unwrap_or(cstr!("String error")),
None, None,
); );
throw_new!(format!( throw_new!(format!(

View File

@ -8,10 +8,6 @@ extern "C" {
#[link_name = "_define_window"] #[link_name = "_define_window"]
pub fn define_window(ebx: u32, ecx: u32, edx: u32, esi: u32, edi: u32); pub fn define_window(ebx: u32, ecx: u32, edx: u32, esi: u32, edi: u32);
// 1
#[link_name = "_put_pixel"]
pub fn put_pixel(ebx: u32, ecx: u32, edx: u32);
// 2 // 2
#[link_name = "_pressed_key"] #[link_name = "_pressed_key"]
pub fn pressed_key() -> u32; pub fn pressed_key() -> u32;

View File

@ -7,7 +7,6 @@ section '.text'
public _start_window_draw public _start_window_draw
public _end_window_draw public _end_window_draw
public _define_window public _define_window
public _put_pixel
public _display_message public _display_message
public _wait_event public _wait_event
public _pressed_key public _pressed_key
@ -51,14 +50,6 @@ _define_window:
pop edi pop edi
ret ret
_put_pixel:
mov eax, SF_PUT_PIXEL
mov ebx, dword [esp + 4 * 1]
mov ecx, dword [esp + 4 * 2]
mov edx, dword [esp + 4 * 3]
int 0x40
ret
_display_message: _display_message:
push esi edi push esi edi
mov eax, SF_DRAW_TEXT mov eax, SF_DRAW_TEXT