Skip to content

Commit

Permalink
exclude ui module (sciter) for flutter
Browse files Browse the repository at this point in the history
  • Loading branch information
rustdesk committed Feb 10, 2023
1 parent 9d88a06 commit be09728
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 201 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Session {
.lc
.write()
.unwrap()
.initialize(id.to_owned(), ConnType::PORT_FORWARD);
.initialize(id.to_owned(), ConnType::PORT_FORWARD, None);
session
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/file_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub trait FileManager: Interface {
fs::get_home_as_string()
}

#[cfg(not(any(target_os = "android", target_os = "ios", feature = "cli")))]
#[cfg(not(any(target_os = "android", target_os = "ios", feature = "cli", feature = "flutter")))]
fn read_dir(&self, path: String, include_hidden: bool) -> sciter::Value {
match fs::read_dir(&fs::get_path(&path), include_hidden) {
Err(_) => sciter::Value::null(),
Expand All @@ -20,7 +20,7 @@ pub trait FileManager: Interface {
}
}

#[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
#[cfg(any(target_os = "android", target_os = "ios", feature = "cli", feature = "flutter"))]
fn read_dir(&self, path: &str, include_hidden: bool) -> String {
use crate::common::make_fd_to_json;
match fs::read_dir(&fs::get_path(path), include_hidden) {
Expand Down
11 changes: 11 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,14 @@ pub fn make_fd_to_json(id: i32, path: String, entries: &Vec<FileEntry>) -> Strin
fd_json.insert("entries".into(), json!(entries_out));
serde_json::to_string(&fd_json).unwrap_or("".into())
}

/// The function to handle the url scheme sent by the system.
///
/// 1. Try to send the url scheme from ipc.
/// 2. If failed to send the url scheme, we open a new main window to handle this url scheme.
pub fn handle_url_scheme(url: String) {
if let Err(err) = crate::ipc::send_url_scheme(url.clone()) {
log::debug!("Send the url to the existing flutter process failed, {}. Let's open a new program to handle this.", err);
let _ = crate::run_me(vec![url]);
}
}
9 changes: 1 addition & 8 deletions src/flutter_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,13 +1119,6 @@ pub fn cm_switch_back(conn_id: i32) {
crate::ui_cm_interface::switch_back(conn_id);
}

pub fn main_get_icon() -> String {
#[cfg(not(any(target_os = "android", target_os = "ios", feature = "cli")))]
return ui_interface::get_icon();
#[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
return String::new();
}

pub fn main_get_build_date() -> String {
crate::BUILD_DATE.to_string()
}
Expand Down Expand Up @@ -1305,7 +1298,7 @@ pub fn main_start_ipc_url_server() {
#[allow(unused_variables)]
pub fn send_url_scheme(_url: String) {
#[cfg(target_os = "macos")]
std::thread::spawn(move || crate::ui::macos::handle_url_scheme(_url));
std::thread::spawn(move || crate::handle_url_scheme(_url));
}

#[cfg(target_os = "android")]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use self::rendezvous_mediator::*;
pub mod common;
#[cfg(not(any(target_os = "ios")))]
pub mod ipc;
#[cfg(not(any(target_os = "android", target_os = "ios", feature = "cli")))]
#[cfg(not(any(target_os = "android", target_os = "ios", feature = "cli", feature = "flutter")))]
pub mod ui;
mod version;
pub use version::*;
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Requires Rust 1.18.
//#![windows_subsystem = "windows"]

#[cfg(not(feature = "flutter"))]
use librustdesk::*;

#[cfg(any(target_os = "android", target_os = "ios"))]
Expand All @@ -16,7 +17,12 @@ fn main() {
common::global_clean();
}

#[cfg(not(any(target_os = "android", target_os = "ios", feature = "cli")))]
#[cfg(not(any(
target_os = "android",
target_os = "ios",
feature = "cli",
feature = "flutter"
)))]
fn main() {
if !common::global_init() {
return;
Expand All @@ -27,6 +33,11 @@ fn main() {
common::global_clean();
}

#[cfg(feature = "flutter")]
fn main() {
hbb_common::log::info!("Hello world!");
}

#[cfg(feature = "cli")]
fn main() {
if !common::global_init() {
Expand Down
109 changes: 93 additions & 16 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sciter::Value;

use hbb_common::{
allow_err,
config::{self, PeerConfig},
config::{self, LocalConfig, PeerConfig},
log,
};

Expand Down Expand Up @@ -38,6 +38,7 @@ lazy_static::lazy_static! {
#[cfg(not(any(feature = "flutter", feature = "cli")))]
lazy_static::lazy_static! {
pub static ref CUR_SESSION: Arc<Mutex<Option<Session<remote::SciterHandler>>>> = Default::default();
static ref CHILDREN : Children = Default::default();
}

struct UIHostHandler;
Expand Down Expand Up @@ -190,11 +191,11 @@ impl UI {
}

fn get_remote_id(&mut self) -> String {
get_remote_id()
LocalConfig::get_remote_id()
}

fn set_remote_id(&mut self, id: String) {
set_remote_id(id);
LocalConfig::set_remote_id(&id);
}

fn goto_install(&mut self) {
Expand Down Expand Up @@ -309,7 +310,10 @@ impl UI {
}

fn is_release(&self) -> bool {
is_release()
#[cfg(not(debug_assertions))]
return true;
#[cfg(debug_assertions)]
return false;
}

fn is_rdp_service_open(&self) -> bool {
Expand All @@ -329,11 +333,18 @@ impl UI {
}

fn closing(&mut self, x: i32, y: i32, w: i32, h: i32) {
closing(x, y, w, h)
crate::server::input_service::fix_key_down_timeout_at_exit();
LocalConfig::set_size(x, y, w, h);
}

fn get_size(&mut self) -> Value {
Value::from_iter(get_size())
let s = LocalConfig::get_size();
let mut v = Vec::new();
v.push(s.0);
v.push(s.1);
v.push(s.2);
v.push(s.3);
Value::from_iter(v)
}

fn get_mouse_time(&self) -> f64 {
Expand Down Expand Up @@ -388,19 +399,19 @@ impl UI {

fn get_recent_sessions(&mut self) -> Value {
// to-do: limit number of recent sessions, and remove old peer file
let peers: Vec<Value> = get_recent_sessions()
let peers: Vec<Value> = PeerConfig::peers()
.drain(..)
.map(|p| Self::get_peer_value(p.0, p.2))
.collect();
Value::from_iter(peers)
}

fn get_icon(&mut self) -> String {
get_icon()
crate::get_icon()
}

fn remove_peer(&mut self, id: String) {
remove_peer(id)
PeerConfig::remove(&id);
}

fn remove_discovered(&mut self, id: String) {
Expand Down Expand Up @@ -442,7 +453,7 @@ impl UI {
}

fn get_software_update_url(&self) -> String {
get_software_update_url()
crate::SOFTWARE_UPDATE_URL.lock().unwrap().clone()
}

fn get_new_version(&self) -> String {
Expand All @@ -458,14 +469,30 @@ impl UI {
}

fn get_software_ext(&self) -> String {
get_software_ext()
#[cfg(windows)]
let p = "exe";
#[cfg(target_os = "macos")]
let p = "dmg";
#[cfg(target_os = "linux")]
let p = "deb";
p.to_owned()
}

fn get_software_store_path(&self) -> String {
get_software_store_path()
let mut p = std::env::temp_dir();
let name = crate::SOFTWARE_UPDATE_URL
.lock()
.unwrap()
.split("/")
.last()
.map(|x| x.to_owned())
.unwrap_or(crate::get_app_name());
p.push(name);
format!("{}.{}", p.to_string_lossy(), self.get_software_ext())
}

fn create_shortcut(&self, _id: String) {
#[cfg(windows)]
create_shortcut(_id)
}

Expand Down Expand Up @@ -495,7 +522,17 @@ impl UI {
}

fn open_url(&self, url: String) {
open_url(url)
#[cfg(windows)]
let p = "explorer";
#[cfg(target_os = "macos")]
let p = "open";
#[cfg(target_os = "linux")]
let p = if std::path::Path::new("/usr/bin/firefox").exists() {
"firefox"
} else {
"xdg-open"
};
allow_err!(std::process::Command::new(p).arg(url).spawn());
}

fn change_id(&self, id: String) {
Expand All @@ -508,19 +545,19 @@ impl UI {
}

fn is_ok_change_id(&self) -> bool {
is_ok_change_id()
machine_uid::get().is_ok()
}

fn get_async_job_status(&self) -> String {
get_async_job_status()
}

fn t(&self, name: String) -> String {
t(name)
crate::client::translate(name)
}

fn is_xfce(&self) -> bool {
is_xfce()
crate::platform::is_xfce()
}

fn get_api_server(&self) -> String {
Expand Down Expand Up @@ -683,3 +720,43 @@ pub fn value_crash_workaround(values: &[Value]) -> Arc<Vec<Value>> {
STUPID_VALUES.lock().unwrap().push(persist.clone());
persist
}

#[inline]
pub fn new_remote(id: String, remote_type: String) {
let mut lock = CHILDREN.lock().unwrap();
let args = vec![format!("--{}", remote_type), id.clone()];
let key = (id.clone(), remote_type.clone());
if let Some(c) = lock.1.get_mut(&key) {
if let Ok(Some(_)) = c.try_wait() {
lock.1.remove(&key);
} else {
if remote_type == "rdp" {
allow_err!(c.kill());
std::thread::sleep(std::time::Duration::from_millis(30));
c.try_wait().ok();
lock.1.remove(&key);
} else {
return;
}
}
}
match crate::run_me(args) {
Ok(child) => {
lock.1.insert(key, child);
}
Err(err) => {
log::error!("Failed to spawn remote: {}", err);
}
}
}

#[inline]
pub fn recent_sessions_updated() -> bool {
let mut children = CHILDREN.lock().unwrap();
if children.0 {
children.0 = false;
true
} else {
false
}
}
13 changes: 1 addition & 12 deletions src/ui/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,11 @@ extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
}
}

/// The function to handle the url scheme sent by the system.
///
/// 1. Try to send the url scheme from ipc.
/// 2. If failed to send the url scheme, we open a new main window to handle this url scheme.
pub fn handle_url_scheme(url: String) {
if let Err(err) = crate::ipc::send_url_scheme(url.clone()) {
log::debug!("Send the url to the existing flutter process failed, {}. Let's open a new program to handle this.", err);
let _ = crate::run_me(vec![url]);
}
}

extern "C" fn handle_apple_event(_this: &Object, _cmd: Sel, event: u64, _reply: u64) {
let event = event as *mut Object;
let url = fruitbasket::parse_url_event(event);
log::debug!("an event was received: {}", url);
std::thread::spawn(move || handle_url_scheme(url));
std::thread::spawn(move || crate::handle_url_scheme(url));
}

unsafe fn make_menu_item(title: &str, key: &str, tag: u32) -> *mut Object {
Expand Down
Loading

0 comments on commit be09728

Please sign in to comment.