use alloc::string::String;
use core::ffi::{CStr, c_char, c_void};
pub type Id = *mut c_void;
pub type Sel = *const c_void;
pub const NIL: Id = core::ptr::null_mut();
#[link(name = "objc")]
unsafe extern "C" {
pub fn objc_msgSend();
#[cfg(target_arch = "x86_64")]
pub fn objc_msgSend_stret();
pub fn objc_getClass(name: *const c_char) -> Id;
pub fn sel_registerName(name: *const c_char) -> Sel;
pub fn objc_autoreleasePoolPush() -> *mut c_void;
pub fn objc_autoreleasePoolPop(pool: *mut c_void);
}
#[link(name = "Foundation", kind = "framework")]
unsafe extern "C" {}
#[link(name = "Metal", kind = "framework")]
unsafe extern "C" {
pub fn MTLCreateSystemDefaultDevice() -> Id;
#[cfg(target_os = "macos")]
pub fn MTLCopyAllDevices() -> Id;
}
#[link(name = "QuartzCore", kind = "framework")]
unsafe extern "C" {}
pub fn class(name: &CStr) -> Id {
unsafe { objc_getClass(name.as_ptr()) }
}
pub fn sel(name: &CStr) -> Sel {
unsafe { sel_registerName(name.as_ptr()) }
}
#[inline(always)]
pub fn msg_entry(size: usize) -> *const () {
#[cfg(target_arch = "x86_64")]
if size > 16 {
return objc_msgSend_stret as *const ();
}
let _ = size;
objc_msgSend as *const ()
}
#[inline(always)]
pub fn bool_of(b: i8) -> bool {
b != 0
}
macro_rules! objc_msg {
($obj:expr, $sel:expr $(, $a:expr => $t:ty)* ; bool) => {{
$crate::mtl_objc::bool_of($crate::mtl_objc::msg!($obj, $sel $(, $a => $t)* ; i8))
}};
($obj:expr, $sel:expr $(, $a:expr => $t:ty)* ; $r:ty) => {{
let f: unsafe extern "C" fn($crate::mtl_objc::Id, $crate::mtl_objc::Sel $(, $t)*) -> $r =
core::mem::transmute($crate::mtl_objc::msg_entry(core::mem::size_of::<$r>()));
f($obj as $crate::mtl_objc::Id, $crate::mtl_objc::sel($sel) $(, $a)*)
}};
}
pub(crate) use objc_msg as msg;
pub struct Obj(Id);
unsafe impl Send for Obj {}
unsafe impl Sync for Obj {}
impl Obj {
pub fn new(id: Id) -> Option<Obj> {
if id.is_null() { None } else { Some(Obj(id)) }
}
pub fn retained(id: Id) -> Option<Obj> {
if id.is_null() {
return None;
}
unsafe { msg!(id, c"retain"; Id) };
Some(Obj(id))
}
pub fn id(&self) -> Id {
self.0
}
}
impl Clone for Obj {
fn clone(&self) -> Obj {
unsafe { msg!(self.0, c"retain"; Id) };
Obj(self.0)
}
}
impl Drop for Obj {
fn drop(&mut self) {
unsafe { msg!(self.0, c"release"; ()) };
}
}
pub struct Pool(*mut c_void);
impl Pool {
pub fn new() -> Pool {
Pool(unsafe { objc_autoreleasePoolPush() })
}
}
impl Drop for Pool {
fn drop(&mut self) {
unsafe { objc_autoreleasePoolPop(self.0) };
}
}
pub fn nsstring(s: &str) -> Option<Obj> {
let id = unsafe {
let o: Id = msg!(class(c"NSString"), c"alloc"; Id);
msg!(o, c"initWithBytes:length:encoding:", s.as_ptr() => *const u8, s.len() => usize, 4usize => usize; Id)
};
Obj::new(id)
}
pub fn string_of(obj: Id) -> String {
if obj.is_null() {
return String::new();
}
unsafe {
let p: *const c_char = msg!(obj, c"UTF8String"; *const c_char);
if p.is_null() { String::new() } else { CStr::from_ptr(p).to_string_lossy().into_owned() }
}
}
pub fn error_text(err: Id) -> String {
if err.is_null() {
return String::from("(no error object)");
}
unsafe { string_of(msg!(err, c"localizedDescription"; Id)) }
}
#[repr(C)]
#[derive(Clone, Copy, Default, Debug)]
pub struct MTLOrigin {
pub x: usize,
pub y: usize,
pub z: usize,
}
#[repr(C)]
#[derive(Clone, Copy, Default, Debug)]
pub struct MTLSize {
pub w: usize,
pub h: usize,
pub d: usize,
}
#[repr(C)]
#[derive(Clone, Copy, Default, Debug)]
pub struct MTLViewport {
pub x: f64,
pub y: f64,
pub w: f64,
pub h: f64,
pub znear: f64,
pub zfar: f64,
}
#[repr(C)]
#[derive(Clone, Copy, Default, Debug)]
pub struct MTLScissorRect {
pub x: usize,
pub y: usize,
pub w: usize,
pub h: usize,
}
#[repr(C)]
#[derive(Clone, Copy, Default, Debug)]
pub struct MTLClearColor {
pub r: f64,
pub g: f64,
pub b: f64,
pub a: f64,
}
#[repr(C)]
#[derive(Clone, Copy, Default, Debug)]
pub struct CGSize {
pub w: f64,
pub h: f64,
}
#[repr(C)]
#[derive(Clone, Copy, Default, Debug)]
pub struct NSRange {
pub location: usize,
pub length: usize,
}
pub const STORAGE_SHARED: usize = 0 << 4;
pub const STORAGE_PRIVATE: usize = 2 << 4;
pub const CPU_CACHE_WRITE_COMBINED: usize = 1;