Skip to content

Commit

Permalink
add color support
Browse files Browse the repository at this point in the history
  • Loading branch information
catornot committed Oct 2, 2024
1 parent 49e6381 commit e183295
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/bindings/plugin_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum LogLevel {

#[repr(i32)]
#[derive(Debug, Copy, Clone)]
#[non_exhaustive]
pub enum PluginString {
Name, // the name of the plugin
LogName, // the name used for logging
Expand All @@ -36,8 +37,10 @@ pub enum PluginString {

#[repr(u32)]
#[derive(Debug, Copy, Clone)]
#[non_exhaustive]
pub enum PluginField {
Context,
Color,
}

bitflags! {
Expand All @@ -47,3 +50,11 @@ bitflags! {
const CLIENT = 0x2;
}
}

#[repr(packed)]
#[derive(Debug, Copy, Clone)]
pub struct PluginColor {
pub red: u8,
pub green: u8,
pub blue: u8,
}
38 changes: 33 additions & 5 deletions src/high/northstar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::ffi::CStr;

use crate::bindings::plugin_abi::PluginContext;
use crate::bindings::plugin_abi::{PluginColor, PluginContext};

/// information about the plugin that the plugins system requests
#[derive(Debug)]
Expand All @@ -11,6 +11,7 @@ pub struct PluginInfo {
log_name: &'static CStr,
dependency_name: &'static CStr,
context: PluginContext,
color: PluginColor,
}

impl PluginInfo {
Expand Down Expand Up @@ -38,28 +39,55 @@ impl PluginInfo {
"consider actually having a dependency_name"
);
assert!(log_name.to_bytes().len() == 9, "log name is used for logging and ideally should be 9 chars long and all upercase to look like every other log str");
// assert!(dependency_name.to_bytes() ); // TODO a check valid constants
Self {
name,
log_name,
dependency_name,
context,
color: PluginColor {
red: 244,
green: 106,
blue: 14,
},
}
}

/// Returns a reference to the get name of the plugin.
/// Creates a new [`PluginInfo`] but with a color for logs.
///
/// # Panics
/// refer to `new`
pub const fn new_with_color(
name: &'static CStr,
log_name: &'static CStr,
dependency_name: &'static CStr,
context: PluginContext,
color: PluginColor,
) -> Self {
Self {
color,
..Self::new(name, log_name, dependency_name, context)
}
}

/// Returns a reference to the name of the plugin.
pub const fn get_name(&self) -> &'static CStr {
self.name
}
/// Returns a reference to the get log name of the plugin.
/// Returns a reference to the log name of the plugin.
pub const fn get_log_name(&self) -> &'static CStr {
self.log_name
}
/// Returns a reference to the get dependency name of the plugin.
/// Returns a reference to the dependency name of the plugin.
pub const fn get_dependency_name(&self) -> &'static CStr {
self.dependency_name
}
/// Returns the get context of the plugin.
/// Returns the context of the plugin.
pub const fn get_context(&self) -> PluginContext {
self.context
}
/// Returns the color of the plugin.
pub const fn get_color(&self) -> PluginColor {
self.color
}
}
2 changes: 1 addition & 1 deletion src/interfaces/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ unsafe extern "C" fn create_interface(
///
/// # Safety
///
/// marked as unsafe for now since I don't know if the current Interface api is invariant enough
/// marked as unsafe for now since I don't know if the current Interface api safe enough and wouldn't be exploited from safe rust
///
/// interfaces must be sync and send have to deal with the Interface struct instead of there struct which all handled by the [`crate::as_interface`] macro
pub unsafe fn register_interface<T: Send + Sync + 'static + AsInterface>(
Expand Down
28 changes: 23 additions & 5 deletions src/macros/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ macro_rules! entry {
Self
}

const fn GetString(
&self,
prop: plugin_abi::PluginString,
) -> *const std::ffi::c_char {
fn GetString(&self, prop: plugin_abi::PluginString) -> *const std::ffi::c_char {
match prop {
plugin_abi::PluginString::Name => {
$plugin::PLUGIN_INFO.get_name().as_ptr() as *const i8
Expand All @@ -75,14 +72,35 @@ macro_rules! entry {
plugin_abi::PluginString::DependencyName => {
$plugin::PLUGIN_INFO.get_dependency_name().as_ptr() as *const i8
}
#[allow(unreachable_patterns)]
// for some reason this warning appears even tho the pattern is non exhaustive
_ => {
log::warn!("invalid plugin string requested!");
c"err".as_ptr()
}
}
}

const fn GetField(&self, prop: plugin_abi::PluginField) -> i64 {
fn GetField(&self, prop: plugin_abi::PluginField) -> i64 {
match prop {
plugin_abi::PluginField::Context => {
$plugin::PLUGIN_INFO.get_context().bits() as i64
}
plugin_abi::PluginField::Color => {
let mut packed = 0;
let color = $plugin::PLUGIN_INFO.get_color();

packed += color.red as i64;
packed += (color.green as i64) << 8;
packed += (color.blue as i64) << 16;

packed
}
#[allow(unreachable_patterns)]
_ => {
log::warn!("invalid plugin field requested!");
0
}
}
}
}
Expand Down

0 comments on commit e183295

Please sign in to comment.