refactor: Change to Rust's CStr

This commit is contained in:
Gleb Zaharov 2024-05-08 20:42:41 +03:00
parent 78f050e20c
commit 21c90abd44
9 changed files with 41 additions and 54 deletions

View File

@ -21,8 +21,3 @@ path = "examples/dark.rs"
[profile.release]
opt-level = "z"
lto = "thin"
[dependencies]
cstr_core = { version = "0.2.6", default-features = false, features = ["nightly"] }
[build-dependencies]

View File

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

View File

@ -1,8 +1,6 @@
#![no_std]
#![no_main]
use cstr_core::{cstr, CStr};
use kos::{
graphics::{display_message, Color, Dot, Size},
input::fetch_key,
@ -13,14 +11,14 @@ use kos::{
},
};
const HEADER: &CStr = cstr!("Dark Mode Demo");
const TEXT: [&CStr; 6] = [
cstr!("Lorem ipsum dolor sit amet,"),
cstr!("semper et rutrum placerat,"),
cstr!("Integer sed diam commodo quam varius"),
cstr!("Sed finibus urna sit amet felis"),
cstr!("vestibulum elementum. Maecenas at feugiat lacus"),
cstr!("tristique et sit amet tortor."),
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 {
@ -66,9 +64,9 @@ fn draw_window(invert: bool) {
Dot { x: 20, y: 310 },
Color::rgb(255, 255, 255),
if invert {
cstr!("Light mode")
c"Light mode"
} else {
cstr!("Dark mode")
c"Dark mode"
},
None,
);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,7 +2,8 @@ use crate::graphics::{Color, Dot, Size};
use crate::sys;
use crate::system::debug_write;
use crate::throw_new;
use cstr_core::{cstr, CStr};
use alloc::ffi::CString;
use core::ffi::CStr;
#[repr(u32)]
pub enum WindowKind {
@ -16,12 +17,12 @@ pub enum WindowKind {
pub struct WindowParams<'a> {
pub color: Color,
pub kind: WindowKind,
pub title: Option<&'a cstr_core::CStr>,
pub title: Option<&'a CStr>,
}
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;
unsafe {
@ -60,14 +61,12 @@ pub fn define_button(
crate::graphics::display_message(
Dot { x: 10, y: 200 },
Color::rgb(255, 0, 0),
CStr::from_bytes_with_nul(
format!(
"x:{:?} y:{:?} w:{:?} h:{:?}\n\0",
start.x, start.y, size.width, size.height
)
.as_bytes(),
)
.unwrap_or(cstr!("String error")),
CString::new(format!(
"x:{:?} y:{:?} w:{:?} h:{:?}\n",
start.x, start.y, size.width, size.height
))
.expect("CString error")
.as_c_str(),
None,
);
throw_new!(format!(