85 lines
2.3 KiB
Rust
Raw Normal View History

2023-04-17 00:28:49 +08:00
use std::ffi::{c_char};
2023-04-10 16:54:50 +08:00
2023-04-14 01:53:34 +08:00
use crate::{
flutter::{FlutterHandler, SESSIONS},
plugins::PLUGIN_REGISTRAR,
ui_session_interface::Session,
};
2023-04-08 17:46:47 +08:00
2023-04-08 18:17:13 +08:00
// API provided by RustDesk.
2023-04-17 00:28:49 +08:00
pub type AddSessionFunc = fn(session_id: String) -> bool;
2023-04-14 01:53:34 +08:00
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 {
2023-04-17 00:28:49 +08:00
OnSessionRgba(fn(String, Vec<i8>) -> Vec<i8>),
2023-04-14 01:53:34 +08:00
}
2023-04-08 17:46:47 +08:00
2023-04-17 00:28:49 +08:00
// #[repr(C)]
2023-04-08 17:46:47 +08:00
pub struct RustDeskApiTable {
2023-04-14 01:53:34 +08:00
pub add_session: AddSessionFunc,
pub remove_session: RemoveSessionFunc,
pub add_session_hook: AddSessionHookFunc,
pub remove_session_hook: RemoveSessionHookFunc,
2023-04-08 17:46:47 +08:00
}
2023-04-10 16:54:50 +08:00
fn load_plugin(path: *const c_char) -> i32 {
2023-04-08 17:46:47 +08:00
PLUGIN_REGISTRAR.load_plugin(path)
}
2023-04-10 16:54:50 +08:00
fn unload_plugin(path: *const c_char) -> i32 {
2023-04-08 17:46:47 +08:00
PLUGIN_REGISTRAR.unload_plugin(path)
}
2023-04-17 00:28:49 +08:00
fn add_session(session_id: String) -> bool {
2023-04-25 21:03:20 +08:00
let mut sessions = SESSIONS.write().unwrap();
// Create a dummy session in SESSIONS.
let mut session: Session<FlutterHandler> = Session::default();
session.id = session_id;
if sessions.contains_key(&session.id) {
return false;
}
let _ = sessions.insert(session.id.to_owned(), session);
2023-04-17 00:28:49 +08:00
// true
false
2023-04-14 01:53:34 +08:00
}
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
}
2023-04-08 17:46:47 +08:00
impl Default for RustDeskApiTable {
fn default() -> Self {
Self {
2023-04-14 01:53:34 +08:00
add_session,
remove_session,
add_session_hook,
remove_session_hook,
2023-04-08 17:46:47 +08:00
}
}
}