Skip to content

Commit

Permalink
Try workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
wenxuanjun committed Oct 21, 2024
1 parent 59463d2 commit 4d43cef
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ build-std-features = ["compiler-builtins-mem"]
build-std = ["core", "compiler_builtins", "alloc"]

[build]
target = "i686-unknown-none.json"
target = ["i686-unknown-none.json", "x86_64-unknown-none"]
32 changes: 32 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Release

permissions:
contents: write

on:
release:
types: [published]

jobs:
create-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: taiki-e/create-gh-release-action@v1
with:
title: Nightly build
token: ${{ secrets.GITHUB_TOKEN }}

upload-assets:
needs: create-release
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: taiki-e/upload-rust-binary-action@v1
with:
bin: libos_terminal
target: ${{ matrix.target }}
tar: false
zip: false
features: ${{ matrix.features || '' }}
token: ${{ secrets.GITHUB_TOKEN }}
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ codegen-units = 1
spin = "0.9.8"

[dependencies.os-terminal]
version = "0.4.4"
version = "0.4.6"
features = ["truetype"]
default-features = false

[features]
default = []
embedded-font = []
Binary file not shown.
92 changes: 89 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
extern crate alloc;

use alloc::boxed::Box;
use alloc::ffi::CString;
use core::alloc::{GlobalAlloc, Layout};
use core::ffi::{c_char, c_uchar, c_uint, c_void};
use core::fmt;
use core::fmt::Write;
use core::panic::PanicInfo;
use core::slice::from_raw_parts_mut;
use os_terminal::font::TrueTypeFont;
use os_terminal::{DrawTarget, Rgb888, Terminal};
use os_terminal::{DrawTarget, Palette, Rgb888, Terminal};
use spin::Mutex;

#[panic_handler]
Expand Down Expand Up @@ -118,8 +119,35 @@ impl DrawTarget for Display {
}
}

#[repr(C)]
pub struct TerminalPalette {
foreground: u32,
background: u32,
ansi_colors: [u32; 16],
}

impl From<TerminalPalette> for Palette {
fn from(palette: TerminalPalette) -> Self {
let u32_to_rgb888 = |u32: u32| {
((u32 >> 16) as u8, (u32 >> 8) as u8, u32 as u8)
};

let mut ansi_colors = [Rgb888::default(); 16];
for (i, &color) in palette.ansi_colors.iter().enumerate() {
ansi_colors[i] = u32_to_rgb888(color);
}

Self {
foreground: u32_to_rgb888(palette.foreground),
background: u32_to_rgb888(palette.background),
ansi_colors,
}
}
}

#[no_mangle]
#[allow(static_mut_refs)]
#[cfg(feature = "embedded-font")]
pub unsafe extern "C" fn terminal_init(
width: c_uint,
height: c_uint,
Expand All @@ -136,14 +164,15 @@ pub unsafe extern "C" fn terminal_init(
if malloc.is_none() || free.is_none() {
return false;
}

MALLOC = malloc;
FREE = free;
SERIAL_PRINT = serial_print;

let display = Display::new(width, height, buffer);
let mut terminal = Terminal::new(display);

let font_buffer = include_bytes!("../fonts/SourceHanMonoSC-Min3500.otf");
let font_buffer = include_bytes!("../fonts/SourceHanMonoSC-Min3500.ttf");
terminal.set_font_manager(Box::new(TrueTypeFont::new(10.0, font_buffer)));

if serial_print.is_some() {
Expand All @@ -156,6 +185,49 @@ pub unsafe extern "C" fn terminal_init(
true
}

#[no_mangle]
#[allow(static_mut_refs)]
#[cfg(not(feature = "embedded-font"))]
pub unsafe extern "C" fn terminal_init(
width: c_uint,
height: c_uint,
screen: *mut u32,
malloc: Option<extern "C" fn(usize) -> *mut c_void>,
free: Option<extern "C" fn(*mut c_void)>,
serial_print: Option<extern "C" fn(*const c_char)>,
font_buffer: *const u8,
font_buffer_size: c_uint,
font_size: c_uint,
) -> bool {
let width = width as usize;
let height = height as usize;
let buffer = from_raw_parts_mut(screen, width * height);

// serial_print can be null
if malloc.is_none() || free.is_none() || font_buffer == core::ptr::null() {
return false;
}

MALLOC = malloc;
FREE = free;
SERIAL_PRINT = serial_print;

let display = Display::new(width, height, buffer);
let mut terminal = Terminal::new(display);

let font_buffer = core::slice::from_raw_parts(font_buffer, font_buffer_size as usize);
terminal.set_font_manager(Box::new(TrueTypeFont::new(font_size as f32, font_buffer)));

if serial_print.is_some() {
println!("Terminal: serial print is set!");
terminal.set_logger(Some(|args| println!("Terminal: {:?}", args)));
}

TERMINAL.lock().replace(terminal);

true
}

#[no_mangle]
pub extern "C" fn terminal_destroy() {
TERMINAL.lock().take();
Expand Down Expand Up @@ -195,8 +267,22 @@ pub extern "C" fn terminal_advance_state_single(c: c_char) {
pub extern "C" fn terminal_handle_keyboard(scancode: c_uchar) -> *const c_char {
if let Some(terminal) = TERMINAL.lock().as_mut() {
if let Some(s) = terminal.handle_keyboard(scancode) {
return s.as_ptr() as *const c_char;
return CString::new(s).unwrap().into_raw();
}
}
core::ptr::null()
}

#[no_mangle]
pub extern "C" fn terminal_set_color_scheme(palette_index: c_uint) {
if let Some(terminal) = TERMINAL.lock().as_mut() {
terminal.set_color_scheme(palette_index as usize);
}
}

#[no_mangle]
pub extern "C" fn terminal_set_custom_color_scheme(palette: TerminalPalette) {
if let Some(terminal) = TERMINAL.lock().as_mut() {
terminal.set_custom_color_scheme(palette.into());
}
}

0 comments on commit 4d43cef

Please sign in to comment.