Skip to content

Commit

Permalink
*_rustabi: Misc cleanup and style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
caseif committed Jul 14, 2024
1 parent c237e65 commit 05b2a2a
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 68 deletions.
25 changes: 21 additions & 4 deletions engine/auxiliary/core_rustabi/src/argus/core/downstream_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

use std::ptr::null;

use lowlevel_rustabi::argus::lowlevel::Vector2i;
use lowlevel_rustabi::argus::lowlevel::Vector2u;
use lowlevel_rustabi::util::*;
Expand Down Expand Up @@ -55,7 +56,11 @@ pub fn set_scripting_parameters(params: &ScriptingParameters) {
unsafe {
argus_set_scripting_parameters(&argus_scripting_parameters_t {
has_main: params.main.is_some(),
main: params.main.as_ref().map(|s| string_to_cstring(&s).as_ptr()).unwrap_or(null()),
main: params
.main
.as_ref()
.map(|s| string_to_cstring(&s).as_ptr())
.unwrap_or(null()),
});
}
}
Expand Down Expand Up @@ -108,11 +113,23 @@ pub fn set_initial_window_parameters(params: &InitialWindowParameters) {
unsafe {
argus_set_initial_window_parameters(&argus_initial_window_parameters_t {
has_id: params.id.is_some(),
id: params.id.as_ref().map(|s| string_to_cstring(&s).as_ptr()).unwrap_or(null()),
id: params
.id
.as_ref()
.map(|s| string_to_cstring(&s).as_ptr())
.unwrap_or(null()),
has_title: params.title.is_some(),
title: params.title.as_ref().map(|s| string_to_cstring(&s).as_ptr()).unwrap_or(null()),
title: params
.title
.as_ref()
.map(|s| string_to_cstring(&s).as_ptr())
.unwrap_or(null()),
has_mode: params.mode.is_some(),
mode: params.mode.as_ref().map(|s| string_to_cstring(&s).as_ptr()).unwrap_or(null()),
mode: params
.mode
.as_ref()
.map(|s| string_to_cstring(&s).as_ptr())
.unwrap_or(null()),
has_vsync: params.vsync.is_some(),
vsync: params.vsync.unwrap_or_default(),
has_mouse_visible: params.mouse_visible.is_some(),
Expand Down
17 changes: 13 additions & 4 deletions engine/auxiliary/core_rustabi/src/argus/core/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use std::convert::TryFrom;

use num_enum::{IntoPrimitive, TryFromPrimitive};

use lowlevel_rustabi::util::str_to_cstring;

use crate::core_cabi;
use crate::core_cabi::*;

Expand All @@ -44,7 +46,7 @@ pub fn initialize_engine() {

pub fn crash(format: &str) -> ! {
unsafe {
argus_crash(string_to_cstring(format));
argus_crash(str_to_cstring(format).as_ptr());
}
}

Expand All @@ -56,13 +58,17 @@ pub fn start_engine(callback: DeltaCallback) -> ! {

pub fn get_current_lifecycle_stage() -> LifecycleStage {
unsafe {
return LifecycleStage::try_from(argus_get_current_lifecycle_stage()).unwrap();
return LifecycleStage::try_from(argus_get_current_lifecycle_stage())
.expect("Invalid LifecycleStage ordinal");
}
}

pub fn register_update_callback(update_callback: DeltaCallback, ordering: Ordering) -> Index {
unsafe {
return argus_register_render_callback(Some(update_callback), ordering as bindings::Ordering);
return argus_register_render_callback(
Some(update_callback),
ordering as bindings::Ordering,
);
}
}

Expand All @@ -74,7 +80,10 @@ pub fn unregister_update_callback(id: Index) {

pub fn register_render_callback(render_callback: DeltaCallback, ordering: Ordering) -> Index {
unsafe {
return argus_register_render_callback(Some(render_callback), ordering as bindings::Ordering);
return argus_register_render_callback(
Some(render_callback),
ordering as bindings::Ordering,
);
}
}

Expand Down
5 changes: 3 additions & 2 deletions engine/auxiliary/core_rustabi/src/argus/core/engine_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

use std::convert::TryFrom;
use std::ffi::{CString, c_char};
use std::ffi::{c_char, CString};
use std::ptr::null_mut;

use lowlevel_rustabi::util::*;
Expand Down Expand Up @@ -83,7 +83,8 @@ pub fn set_render_backend(name: &str) {

pub fn get_screen_space_scale_mode() -> ScreenSpaceScaleMode {
unsafe {
return ScreenSpaceScaleMode::try_from(core_cabi::get_screen_space_scale_mode()).unwrap();
return ScreenSpaceScaleMode::try_from(core_cabi::get_screen_space_scale_mode())
.expect("Invalid ScreenSpaceScaleMode ordinal");
}
}

Expand Down
17 changes: 12 additions & 5 deletions engine/auxiliary/core_rustabi/src/argus/core/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use crate::argus::core::Index;
use crate::argus::core::Ordering;
use crate::core_cabi;

pub use crate::core_cabi::argus_event_t;
pub use crate::core_cabi::argus_event_const_t;
pub use crate::core_cabi::argus_event_t;

#[repr(u32)]
pub enum TargetThread {
Expand All @@ -35,7 +35,9 @@ pub enum TargetThread {
}

pub trait ArgusEvent {
fn of(handle: argus_event_t) -> Self where Self : Sized;
fn of(handle: argus_event_t) -> Self
where
Self: Sized;

fn get_type_id(&self) -> String;

Expand All @@ -56,8 +58,13 @@ extern "C" fn clean_up_event_handler(_: Index, ctx: *mut c_void) {
let _: Box<Box<dyn FnMut(argus_event_const_t)>> = unsafe { Box::from_raw(mem::transmute(ctx)) };
}

pub fn register_event_handler<E: ArgusEvent, D>(type_id: &str, handler: &EventHandler<E, D>,
target_thread: TargetThread, data: *mut D, ordering: Ordering) -> Index {
pub fn register_event_handler<E: ArgusEvent, D>(
type_id: &str,
handler: &EventHandler<E, D>,
target_thread: TargetThread,
data: *mut D,
ordering: Ordering,
) -> Index {
unsafe {
let closure = |handle: argus_event_const_t| {
handler(&E::of(handle as argus_event_t), data);
Expand All @@ -71,7 +78,7 @@ pub fn register_event_handler<E: ArgusEvent, D>(type_id: &str, handler: &EventHa
target_thread as core_cabi::TargetThread,
Box::into_raw(ctx) as *mut c_void,
ordering as core_cabi::Ordering,
Some(clean_up_event_handler)
Some(clean_up_event_handler),
);
}
}
Expand Down
29 changes: 19 additions & 10 deletions engine/auxiliary/core_rustabi/src/argus/core/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ pub fn lifecycle_stage_to_str(stage: LifecycleStage) -> String {
}
}

pub fn register_dynamic_module(id: &str, lifecycle_callback: LifecycleUpdateCallback, dependencies: Vec<String>) {
pub fn register_dynamic_module(
id: &str,
lifecycle_callback: LifecycleUpdateCallback,
dependencies: Vec<String>,
) {
unsafe {
let (names, count) = string_vec_to_cstr_arr(dependencies).into();
argus_register_dynamic_module(str_to_cstring(id).as_ptr(), Some(lifecycle_callback), count, names);
argus_register_dynamic_module(
str_to_cstring(id).as_ptr(),
Some(lifecycle_callback),
count,
names,
);
}
}

Expand All @@ -58,11 +67,11 @@ pub fn enable_dynamic_module(module_id: &str) -> bool {
}
}

pub fn get_present_dynamic_modules() -> Vec<String> {
unsafe {
let arr = argus_get_present_dynamic_modules();
let vec = argus::lowlevel::string_array_to_vec(arr);
argus::lowlevel::free_string_array(arr);
return vec;
}
}
pub fn get_present_dynamic_modules() -> Vec<String> {
unsafe {
let arr = argus_get_present_dynamic_modules();
let vec = argus::lowlevel::string_array_to_vec(arr);
argus::lowlevel::free_string_array(arr);
return vec;
}
}
2 changes: 1 addition & 1 deletion engine/auxiliary/core_rustabi/src/core_cabi/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ extern "C" {
pub fn argus_get_present_dynamic_modules() -> StringArray;
pub fn argus_initialize_engine();
pub fn argus_start_engine(callback: delta_callback_t) -> !;
pub fn argus_crash(format: *const ::std::os::raw::c_char) -> !;
pub fn argus_crash(msg: *const ::std::os::raw::c_char) -> !;
pub fn argus_get_current_lifecycle_stage() -> LifecycleStage;
pub fn argus_register_update_callback(
update_callback: delta_callback_t,
Expand Down
2 changes: 2 additions & 0 deletions engine/auxiliary/core_rustabi/src/core_cabi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#[allow(unused_imports)]
pub mod bindings;

#[allow(unused_imports)]
use lowlevel_rustabi::lowlevel_cabi::*;

pub use self::bindings::*;
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use std::ffi::CStr;

use crate::lowlevel_cabi::*;

pub use crate::lowlevel_cabi::StringArray as StringArray;
pub use crate::lowlevel_cabi::StringArrayConst as StringArrayConst;
use crate::lowlevel_cabi;
pub use crate::lowlevel_cabi::StringArray;
pub use crate::lowlevel_cabi::StringArrayConst;

pub fn string_array_to_vec(sa: StringArray) -> Vec<String> {
unsafe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ pub fn set_message_dispatcher(dispatcher: MessageDispatcher) {
}
}

pub fn broadcast_message<T : Message>(message: &T) {
pub fn broadcast_message<T: Message>(message: &T) {
unsafe {
argus_broadcast_message(str_to_cstring(T::get_message_type_id()).as_ptr(),
&message as *const _ as *const std::ffi::c_void);
argus_broadcast_message(
str_to_cstring(T::get_message_type_id()).as_ptr(),
&message as *const _ as *const std::ffi::c_void,
);
}
}
1 change: 1 addition & 0 deletions engine/auxiliary/lowlevel_rustabi/src/lowlevel_cabi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#[allow(unused_imports)]
pub mod bindings;

pub use bindings::*;
32 changes: 26 additions & 6 deletions engine/auxiliary/lowlevel_rustabi/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,50 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use std::ffi::{CStr, CString, c_char};
use std::ffi::{c_char, CStr, CString};

pub struct CStringArray {
vec: Vec<CString>,
}

impl Into<*const *const c_char> for CStringArray {
fn into(self) -> *const *const c_char {
return self
.vec
.iter()
.map(|s| s.as_ptr())
.collect::<Vec<_>>()
.as_ptr();
}
}

impl Into<(*const *const c_char, usize)> for CStringArray {
fn into(self) -> (*const *const c_char, usize) {
return (self.vec.iter().map(|s| s.as_ptr()).collect::<Vec<_>>().as_ptr(), self.vec.len());
let len = self.vec.len();
return (self.into(), len);
}
}

pub fn string_to_cstring(s: &String) -> CString {
return CString::new(s.to_string()).unwrap();
CString::new(s.to_string()).unwrap()
}

pub fn str_to_cstring(s: &str) -> CString {
return string_to_cstring(&s.to_string());
string_to_cstring(&s.to_string())
}

pub unsafe fn cstr_to_string(s: *const c_char) -> String {
return CStr::from_ptr(s).to_str().unwrap().to_string();
CStr::from_ptr(s).to_str().unwrap().to_string()
}

pub unsafe fn cstr_to_str<'a>(s: *const c_char) -> &'a str {
CStr::from_ptr(s)
.to_str()
.expect("Failed to convert C string to Rust string")
}

pub unsafe fn string_vec_to_cstr_arr(v: Vec<String>) -> CStringArray {
return CStringArray { vec: v.iter().map(string_to_cstring).collect() };
CStringArray {
vec: v.iter().map(string_to_cstring).collect(),
}
}
2 changes: 1 addition & 1 deletion engine/auxiliary/wm_rustabi/module.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ name = wm_rustabi
type = aux
languages = RUST
engine_module_deps = wm
engine_aux_deps = lowlevel_rustabi;core_rustabi
engine_aux_deps = lowlevel_rustabi,core_rustabi
16 changes: 7 additions & 9 deletions engine/auxiliary/wm_rustabi/src/argus/wm/api_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
*/

use std::ffi::{c_char, c_void};
use std::mem;
use std::ptr::null_mut;

use lowlevel_rustabi::util::*;
use num_enum::{IntoPrimitive, TryFromPrimitive};

use crate::argus::wm::Window;
use crate::wm_cabi;
use crate::wm_cabi::*;

pub struct GLContext {
Expand Down Expand Up @@ -54,7 +52,7 @@ impl VkSurface {
return self.handle;
}

pub unsafe fn get_mut_handle(&mut self) -> *mut c_void {
pub unsafe fn get_handle_mut(&mut self) -> *mut c_void {
return self.handle;
}
}
Expand Down Expand Up @@ -85,7 +83,7 @@ pub fn gl_unload_library() {
pub fn gl_create_context(window: &mut Window, version_major: i32, version_minor: i32, flags: GLContextFlags)
-> GLContext {
unsafe {
let handle = argus_gl_create_context(window.get_mut_handle(), version_major, version_minor, flags.into());
let handle = argus_gl_create_context(window.get_handle_mut(), version_major, version_minor, flags.into());
return GLContext { handle };
}
}
Expand All @@ -104,7 +102,7 @@ pub fn gl_is_context_current(context: &GLContext) -> bool {

pub fn gl_make_context_current(window: &mut Window, context: &GLContext) {
unsafe {
argus_gl_make_context_current(window.get_mut_handle(), context.handle);
argus_gl_make_context_current(window.get_handle_mut(), context.handle);
}
}

Expand All @@ -120,7 +118,7 @@ pub fn gl_swap_interval(interval: i32) {

pub fn gl_swap_buffers(window: &mut Window) {
unsafe {
argus_gl_swap_buffers(window.get_mut_handle());
argus_gl_swap_buffers(window.get_handle_mut());
}
}

Expand All @@ -133,18 +131,18 @@ pub fn vk_is_supported() -> bool {
pub fn vk_create_surface(window: &mut Window, instance: &VkInstance) -> VkSurface {
unsafe {
let mut surface: *mut c_void = null_mut();
argus_vk_create_surface(window.get_mut_handle(), instance.handle, &mut surface);
argus_vk_create_surface(window.get_handle_mut(), instance.handle, &mut surface);
return VkSurface::of(surface);
}
}

pub fn vk_get_required_instance_extensions(window: &mut Window) -> Vec<String> {
unsafe {
let mut count: u32 = 0;
argus_vk_get_required_instance_extensions(window.get_mut_handle(), &mut count, null_mut());
argus_vk_get_required_instance_extensions(window.get_handle_mut(), &mut count, null_mut());
let mut exts = Vec::<*const c_char>::new();
exts.reserve(count as usize);
argus_vk_get_required_instance_extensions(window.get_mut_handle(), null_mut(), exts.as_mut_ptr());
argus_vk_get_required_instance_extensions(window.get_handle_mut(), null_mut(), exts.as_mut_ptr());
return exts.into_iter().map(|s| cstr_to_string(s)).collect();
}
}
Loading

0 comments on commit 05b2a2a

Please sign in to comment.