From 4acc3052cc905c06aec6cb8452abfe92b0f499c4 Mon Sep 17 00:00:00 2001 From: Kingtous Date: Thu, 27 Apr 2023 21:19:56 +0800 Subject: [PATCH] feat: add callable trait --- src/plugin/api.rs | 82 ++++------------------------------------------- 1 file changed, 6 insertions(+), 76 deletions(-) diff --git a/src/plugin/api.rs b/src/plugin/api.rs index 086cb246d..744032e88 100644 --- a/src/plugin/api.rs +++ b/src/plugin/api.rs @@ -1,84 +1,14 @@ use std::ffi::{c_char}; +use lazy_static::lazy_static; + use crate::{ flutter::{FlutterHandler, SESSIONS}, plugins::PLUGIN_REGISTRAR, ui_session_interface::Session, }; -// API provided by RustDesk. -pub type AddSessionFunc = fn(session_id: String) -> bool; -pub type RemoveSessionFunc = fn(session_id: &String) -> bool; -pub type AddSessionHookFunc = fn(session_id: String, key: String, hook: SessionHook) -> bool; -pub type RemoveSessionHookFunc = fn(session_id: String, key: &String) -> bool; - -/// Hooks for session. -#[derive(Clone)] -pub enum SessionHook { - OnSessionRgba(fn(String, Vec) -> Vec), -} - -// #[repr(C)] -pub struct RustDeskApiTable { - pub add_session: AddSessionFunc, - pub remove_session: RemoveSessionFunc, - pub add_session_hook: AddSessionHookFunc, - pub remove_session_hook: RemoveSessionHookFunc, -} - -fn load_plugin(path: *const c_char) -> i32 { - PLUGIN_REGISTRAR.load_plugin(path) -} - -fn unload_plugin(path: *const c_char) -> i32 { - PLUGIN_REGISTRAR.unload_plugin(path) -} - -fn add_session(session_id: String) -> bool { - let mut sessions = SESSIONS.write().unwrap(); - // Create a dummy session in SESSIONS. - let mut session: Session = Session::default(); - session.id = session_id; - if sessions.contains_key(&session.id) { - return false; - } - let _ = sessions.insert(session.id.to_owned(), session); - // true - false -} - -fn remove_session(session_id: &String) -> bool { - let mut sessions = SESSIONS.write().unwrap(); - if !sessions.contains_key(session_id) { - return false; - } - let _ = sessions.remove(session_id); - true -} - -fn add_session_hook(session_id: String, key: String, hook: SessionHook) -> bool { - let sessions = SESSIONS.read().unwrap(); - if let Some(session) = sessions.get(&session_id) { - return session.add_session_hook(key, hook); - } - false -} - -fn remove_session_hook(session_id: String, key: &String) -> bool { - let sessions = SESSIONS.read().unwrap(); - if let Some(session) = sessions.get(&session_id) { - return session.remove_session_hook(key); - } - false -} - -impl Default for RustDeskApiTable { - fn default() -> Self { - Self { - add_session, - remove_session, - add_session_hook, - remove_session_hook, - } - } -} +pub trait Callable { + // Call + fn onCall(method_name: String) -> bool; +} \ No newline at end of file