ios add dummy call and remove some lookup functions

Signed-off-by: dignow <linlong1265@gmail.com>
This commit is contained in:
dignow 2023-08-30 13:49:10 +08:00
parent 24547c11cb
commit 7671ed857d
6 changed files with 39 additions and 98 deletions

View File

@ -13,9 +13,6 @@ import Flutter
} }
public func dummyMethodToEnforceBundling() { public func dummyMethodToEnforceBundling() {
get_rgba(); session_get_rgba(nil);
// free_rgba(nil);
// get_by_name("", "");
// set_by_name("", "");
} }
} }

View File

@ -1,4 +1 @@
void* get_rgba(); const unsigned char* session_get_rgba(const char*);
void free_rgba(void*);
void set_by_name(const char*, const char*);
const char* get_by_name(const char*, const char*);

View File

@ -1693,7 +1693,7 @@ class FFI {
} else { } else {
// Fetch the image buffer from rust codes. // Fetch the image buffer from rust codes.
final sz = platformFFI.getRgbaSize(sessionId); final sz = platformFFI.getRgbaSize(sessionId);
if (sz == null || sz == 0) { if (sz == 0) {
return; return;
} }
final rgba = platformFFI.getRgba(sessionId, sz); final rgba = platformFFI.getRgba(sessionId, sz);

View File

@ -21,16 +21,8 @@ class RgbaFrame extends Struct {
external Pointer<Uint8> data; external Pointer<Uint8> data;
} }
typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>);
typedef F3 = Pointer<Uint8> Function(Pointer<Utf8>); typedef F3 = Pointer<Uint8> Function(Pointer<Utf8>);
typedef F4 = Uint64 Function(Pointer<Utf8>);
typedef F4Dart = int Function(Pointer<Utf8>);
typedef F5 = Void Function(Pointer<Utf8>);
typedef F5Dart = void Function(Pointer<Utf8>);
typedef HandleEvent = Future<void> Function(Map<String, dynamic> evt); typedef HandleEvent = Future<void> Function(Map<String, dynamic> evt);
// pub fn session_register_texture(id: *const char, ptr: usize)
typedef F6 = Void Function(Pointer<Utf8>, Uint64);
typedef F6Dart = void Function(Pointer<Utf8>, int);
/// FFI wrapper around the native Rust core. /// FFI wrapper around the native Rust core.
/// Hides the platform differences. /// Hides the platform differences.
@ -38,7 +30,6 @@ class PlatformFFI {
String _dir = ''; String _dir = '';
// _homeDir is only needed for Android and IOS. // _homeDir is only needed for Android and IOS.
String _homeDir = ''; String _homeDir = '';
F2? _translate;
final _eventHandlers = <String, Map<String, HandleEvent>>{}; final _eventHandlers = <String, Map<String, HandleEvent>>{};
late RustdeskImpl _ffiBind; late RustdeskImpl _ffiBind;
late String _appType; late String _appType;
@ -51,9 +42,6 @@ class PlatformFFI {
RustdeskImpl get ffiBind => _ffiBind; RustdeskImpl get ffiBind => _ffiBind;
F3? _session_get_rgba; F3? _session_get_rgba;
F4Dart? _session_get_rgba_size;
F5Dart? _session_next_rgba;
F6Dart? _session_register_texture;
static get localeName => Platform.localeName; static get localeName => Platform.localeName;
@ -89,18 +77,8 @@ class PlatformFFI {
} }
} }
String translate(String name, String locale) { String translate(String name, String locale) =>
if (_translate == null) return name; _ffiBind.translate(name: name, locale: locale);
var a = name.toNativeUtf8();
var b = locale.toNativeUtf8();
var p = _translate!(a, b);
assert(p != nullptr);
final res = p.toDartString();
calloc.free(p);
calloc.free(a);
calloc.free(b);
return res;
}
Uint8List? getRgba(SessionID sessionId, int bufSize) { Uint8List? getRgba(SessionID sessionId, int bufSize) {
if (_session_get_rgba == null) return null; if (_session_get_rgba == null) return null;
@ -118,30 +96,11 @@ class PlatformFFI {
} }
} }
int? getRgbaSize(SessionID sessionId) { int getRgbaSize(SessionID sessionId) =>
if (_session_get_rgba_size == null) return null; _ffiBind.sessionGetRgbaSize(sessionId: sessionId);
final sessionIdStr = sessionId.toString(); void nextRgba(SessionID sessionId) => _ffiBind.sessionNextRgba(sessionId: sessionId);
var a = sessionIdStr.toNativeUtf8(); void registerTexture(SessionID sessionId, int ptr) =>
final bufferSize = _session_get_rgba_size!(a); _ffiBind.sessionRegisterTexture(sessionId: sessionId, ptr: ptr);
malloc.free(a);
return bufferSize;
}
void nextRgba(SessionID sessionId) {
if (_session_next_rgba == null) return;
final sessionIdStr = sessionId.toString();
final a = sessionIdStr.toNativeUtf8();
_session_next_rgba!(a);
malloc.free(a);
}
void registerTexture(SessionID sessionId, int ptr) {
if (_session_register_texture == null) return;
final sessionIdStr = sessionId.toString();
final a = sessionIdStr.toNativeUtf8();
_session_register_texture!(a, ptr);
malloc.free(a);
}
/// Init the FFI class, loads the native Rust core library. /// Init the FFI class, loads the native Rust core library.
Future<void> init(String appType) async { Future<void> init(String appType) async {
@ -157,14 +116,7 @@ class PlatformFFI {
: DynamicLibrary.process(); : DynamicLibrary.process();
debugPrint('initializing FFI $_appType'); debugPrint('initializing FFI $_appType');
try { try {
_translate = dylib.lookupFunction<F2, F2>('translate');
_session_get_rgba = dylib.lookupFunction<F3, F3>("session_get_rgba"); _session_get_rgba = dylib.lookupFunction<F3, F3>("session_get_rgba");
_session_get_rgba_size =
dylib.lookupFunction<F4, F4Dart>("session_get_rgba_size");
_session_next_rgba =
dylib.lookupFunction<F5, F5Dart>("session_next_rgba");
_session_register_texture =
dylib.lookupFunction<F6, F6Dart>("session_register_texture");
try { try {
// SYSTEM user failed // SYSTEM user failed
_dir = (await getApplicationDocumentsDirectory()).path; _dir = (await getApplicationDocumentsDirectory()).path;

View File

@ -1035,24 +1035,24 @@ fn serialize_resolutions(resolutions: &Vec<Resolution>) -> String {
} }
fn char_to_session_id(c: *const char) -> ResultType<SessionID> { fn char_to_session_id(c: *const char) -> ResultType<SessionID> {
if c.is_null() {
bail!("Session id ptr is null");
}
let cstr = unsafe { std::ffi::CStr::from_ptr(c as _) }; let cstr = unsafe { std::ffi::CStr::from_ptr(c as _) };
let str = cstr.to_str()?; let str = cstr.to_str()?;
SessionID::from_str(str).map_err(|e| anyhow!("{:?}", e)) SessionID::from_str(str).map_err(|e| anyhow!("{:?}", e))
} }
#[no_mangle] pub fn session_get_rgba_size(_session_id: SessionID) -> usize {
pub fn session_get_rgba_size(_session_uuid_str: *const char) -> usize {
#[cfg(not(feature = "flutter_texture_render"))] #[cfg(not(feature = "flutter_texture_render"))]
if let Ok(session_id) = char_to_session_id(_session_uuid_str) { if let Some(session) = SESSIONS.read().unwrap().get(&_session_id) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { return session.rgba.read().unwrap().len();
return session.rgba.read().unwrap().len();
}
} }
0 0
} }
#[no_mangle] #[no_mangle]
pub fn session_get_rgba(session_uuid_str: *const char) -> *const u8 { pub extern "C" fn session_get_rgba(session_uuid_str: *const char) -> *const u8 {
if let Ok(session_id) = char_to_session_id(session_uuid_str) { if let Ok(session_id) = char_to_session_id(session_uuid_str) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
return session.get_rgba(); return session.get_rgba();
@ -1062,23 +1062,17 @@ pub fn session_get_rgba(session_uuid_str: *const char) -> *const u8 {
std::ptr::null() std::ptr::null()
} }
#[no_mangle] pub fn session_next_rgba(session_id: SessionID) {
pub fn session_next_rgba(session_uuid_str: *const char) { if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
if let Ok(session_id) = char_to_session_id(session_uuid_str) { return session.next_rgba();
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
return session.next_rgba();
}
} }
} }
#[inline] #[inline]
#[no_mangle] pub fn session_register_texture(_session_id: SessionID, _ptr: usize) {
pub fn session_register_texture(_session_uuid_str: *const char, _ptr: usize) {
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
if let Ok(session_id) = char_to_session_id(_session_uuid_str) { if let Some(session) = SESSIONS.write().unwrap().get_mut(&_session_id) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { return session.register_texture(_ptr);
return session.register_texture(_ptr);
}
} }
} }
@ -1130,9 +1124,6 @@ pub fn stop_global_event_stream(app_type: String) {
let _ = GLOBAL_EVENT_STREAM.write().unwrap().remove(&app_type); let _ = GLOBAL_EVENT_STREAM.write().unwrap().remove(&app_type);
} }
#[no_mangle]
unsafe extern "C" fn get_rgba() {}
/// Hooks for session. /// Hooks for session.
#[derive(Clone)] #[derive(Clone)]
pub enum SessionHook { pub enum SessionHook {

View File

@ -1410,18 +1410,6 @@ pub fn main_get_build_date() -> String {
crate::BUILD_DATE.to_string() crate::BUILD_DATE.to_string()
} }
#[no_mangle]
unsafe extern "C" fn translate(name: *const c_char, locale: *const c_char) -> *const c_char {
let name = CStr::from_ptr(name);
let locale = CStr::from_ptr(locale);
let res = if let (Ok(name), Ok(locale)) = (name.to_str(), locale.to_str()) {
crate::client::translate_locale(name.to_owned(), locale)
} else {
String::new()
};
CString::from_vec_unchecked(res.into_bytes()).into_raw()
}
fn handle_query_onlines(onlines: Vec<String>, offlines: Vec<String>) { fn handle_query_onlines(onlines: Vec<String>, offlines: Vec<String>) {
let data = HashMap::from([ let data = HashMap::from([
("name", "callback_query_onlines".to_owned()), ("name", "callback_query_onlines".to_owned()),
@ -1434,6 +1422,22 @@ fn handle_query_onlines(onlines: Vec<String>, offlines: Vec<String>) {
); );
} }
pub fn translate(name: String, locale: String) -> SyncReturn<String> {
SyncReturn(crate::client::translate_locale(name, &locale))
}
pub fn session_get_rgba_size(session_id: SessionID) -> SyncReturn<usize> {
SyncReturn(super::flutter::session_get_rgba_size(session_id))
}
pub fn session_next_rgba(session_id: SessionID) -> SyncReturn<()> {
SyncReturn(super::flutter::session_next_rgba(session_id))
}
pub fn session_register_texture(session_id: SessionID, ptr: usize) -> SyncReturn<()> {
SyncReturn(super::flutter::session_register_texture(session_id, ptr))
}
pub fn query_onlines(ids: Vec<String>) { pub fn query_onlines(ids: Vec<String>) {
#[cfg(not(any(target_os = "ios")))] #[cfg(not(any(target_os = "ios")))]
crate::rendezvous_mediator::query_online_states(ids, handle_query_onlines) crate::rendezvous_mediator::query_online_states(ids, handle_query_onlines)