try fix clipboard, flutter version

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-06-18 20:23:54 +08:00
parent 22b0b54527
commit ccd2cbd760
14 changed files with 136 additions and 166 deletions

View File

@@ -2336,6 +2336,7 @@ pub enum Data {
CancelJob(i32),
RemovePortForward(i32),
AddPortForward((i32, String, i32)),
#[cfg(not(feature = "flutter"))]
ToggleClipboardFile,
NewRDP,
SetConfirmOverrideFile((i32, i32, bool, bool, bool)),

View File

@@ -6,7 +6,7 @@ use std::sync::{
};
#[cfg(windows)]
use clipboard::{cliprdr::CliprdrClientContext, ContextSend};
use clipboard::{cliprdr::CliprdrClientContext, ClipboardFile, ContextSend};
use crossbeam_queue::ArrayQueue;
use hbb_common::config::{PeerConfig, TransferSerde};
use hbb_common::fs::{
@@ -58,7 +58,7 @@ pub struct Remote<T: InvokeUiSession> {
last_update_jobs_status: (Instant, HashMap<i32, u64>),
first_frame: bool,
#[cfg(windows)]
client_conn_id: i32, // used for clipboard
client_conn_id: i32, // used for file clipboard
data_count: Arc<AtomicUsize>,
frame_count: Arc<AtomicUsize>,
video_format: CodecFormat,
@@ -207,7 +207,15 @@ impl<T: InvokeUiSession> Remote<T> {
#[cfg(windows)]
match _msg {
Some(clip) => {
allow_err!(peer.send(&crate::clipboard_file::clip_2_msg(clip)).await);
let mut send_msg = true;
if clip.is_stopping_allowed() {
if !(*self.handler.server_file_transfer_enabled.read().unwrap() && self.handler.lc.read().unwrap().enable_file_transfer.v) {
send_msg = false;
}
}
if send_msg {
allow_err!(peer.send(&crate::clipboard_file::clip_2_msg(clip)).await);
}
}
None => {
// unreachable!()
@@ -389,6 +397,7 @@ impl<T: InvokeUiSession> Remote<T> {
.handle_login_from_ui(os_username, os_password, password, remember, peer)
.await;
}
#[cfg(not(feature = "flutter"))]
Data::ToggleClipboardFile => {
self.check_clipboard_file_context();
}
@@ -938,6 +947,7 @@ impl<T: InvokeUiSession> Remote<T> {
}
Some(login_response::Union::PeerInfo(pi)) => {
self.handler.handle_peer_info(pi);
#[cfg(not(feature = "flutter"))]
self.check_clipboard_file_context();
if !(self.handler.is_file_transfer() || self.handler.is_port_forward()) {
#[cfg(feature = "flutter")]
@@ -1188,7 +1198,6 @@ impl<T: InvokeUiSession> Remote<T> {
if !p.enabled && self.handler.is_file_transfer() {
return true;
}
self.check_clipboard_file_context();
self.handler.set_permission("file", p.enabled);
}
Ok(Permission::Restart) => {
@@ -1525,12 +1534,13 @@ impl<T: InvokeUiSession> Remote<T> {
true
}
#[cfg(not(feature = "flutter"))]
fn check_clipboard_file_context(&self) {
#[cfg(windows)]
{
let enabled = *self.handler.server_file_transfer_enabled.read().unwrap()
&& self.handler.lc.read().unwrap().enable_file_transfer.v;
ContextSend::enable(enabled);
ContextSend::enable(enabled, false, false);
}
}

View File

@@ -122,6 +122,8 @@ pub fn core_main() -> Option<Vec<String>> {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
init_plugins(&args);
if args.is_empty() {
#[cfg(windows)]
clipboard::ContextSend::enable(true, false, false);
std::thread::spawn(move || crate::start_server(false));
} else {
#[cfg(windows)]

View File

@@ -202,7 +202,6 @@ pub enum Data {
SyncConfig(Option<Box<(Config, Config2)>>),
#[cfg(not(any(target_os = "android", target_os = "ios")))]
ClipboardFile(ClipboardFile),
ClipboardFileEnabled(bool),
PrivacyModeState((i32, PrivacyModeState)),
TestRendezvousServer,
#[cfg(not(any(target_os = "android", target_os = "ios")))]
@@ -242,7 +241,7 @@ pub async fn start(postfix: &str) -> ResultType<()> {
loop {
match stream.next().await {
Err(err) => {
log::trace!("ipc{} connection closed: {}", postfix, err);
log::trace!("ipc '{}' connection closed: {}", postfix, err);
break;
}
Ok(Some(data)) => {

View File

@@ -282,6 +282,7 @@ impl Connection {
keyboard: Connection::permission("enable-keyboard"),
clipboard: Connection::permission("enable-clipboard"),
audio: Connection::permission("enable-audio"),
// to-do: make sure is the option correct here
file: Connection::permission("enable-file-transfer"),
restart: Connection::permission("enable-remote-restart"),
recording: Connection::permission("enable-record-session"),
@@ -424,7 +425,6 @@ impl Connection {
} else if &name == "file" {
conn.file = enabled;
conn.send_permission(Permission::File, enabled).await;
conn.send_to_cm(ipc::Data::ClipboardFileEnabled(conn.file_transfer_enabled()));
} else if &name == "restart" {
conn.restart = enabled;
conn.send_permission(Permission::Restart, enabled).await;
@@ -437,9 +437,9 @@ impl Connection {
allow_err!(conn.stream.send_raw(bytes).await);
}
#[cfg(windows)]
ipc::Data::ClipboardFile(_clip) => {
if conn.file_transfer_enabled() {
allow_err!(conn.stream.send(&clip_2_msg(_clip)).await);
ipc::Data::ClipboardFile(clip) => {
if !clip.is_stopping_allowed() || conn.file_transfer_enabled() {
allow_err!(conn.stream.send(&clip_2_msg(clip)).await);
}
}
ipc::Data::PrivacyModeState((_, state)) => {
@@ -1136,7 +1136,7 @@ impl Connection {
clipboard: self.clipboard,
audio: self.audio,
file: self.file,
file_transfer_enabled: self.file_transfer_enabled(),
file_transfer_enabled: self.file,
restart: self.restart,
recording: self.recording,
from_switch: self.from_switch,
@@ -1636,11 +1636,9 @@ impl Connection {
}
}
Some(message::Union::Cliprdr(_clip)) => {
if self.file_transfer_enabled() {
#[cfg(windows)]
if let Some(clip) = msg_2_clip(_clip) {
self.send_to_cm(ipc::Data::ClipboardFile(clip))
}
#[cfg(windows)]
if let Some(clip) = msg_2_clip(_clip) {
self.send_to_cm(ipc::Data::ClipboardFile(clip))
}
}
Some(message::Union::FileAction(fa)) => {
@@ -2073,9 +2071,6 @@ impl Connection {
if let Ok(q) = o.enable_file_transfer.enum_value() {
if q != BoolOption::NotSet {
self.enable_file_transfer = q == BoolOption::Yes;
self.send_to_cm(ipc::Data::ClipboardFileEnabled(
self.file_transfer_enabled(),
));
}
}
if let Ok(q) = o.disable_clipboard.enum_value() {

View File

@@ -11,14 +11,12 @@ use std::{
},
};
#[cfg(windows)]
use clipboard::{cliprdr::CliprdrClientContext, empty_clipboard, set_conn_enabled, ContextSend};
use serde_derive::Serialize;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use crate::ipc::Connection;
#[cfg(not(any(target_os = "ios")))]
use crate::ipc::{self, Data};
#[cfg(windows)]
use clipboard::{cliprdr::CliprdrClientContext, empty_clipboard, ContextSend};
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::tokio::sync::mpsc::unbounded_channel;
#[cfg(windows)]
@@ -37,6 +35,7 @@ use hbb_common::{
task::spawn_blocking,
},
};
use serde_derive::Serialize;
#[derive(Serialize, Clone)]
pub struct Client {
@@ -131,8 +130,7 @@ impl<T: InvokeUiCM> ConnectionManager<T> {
restart: bool,
recording: bool,
from_switch: bool,
#[cfg(not(any(target_os = "ios")))]
tx: mpsc::UnboundedSender<Data>,
#[cfg(not(any(target_os = "ios")))] tx: mpsc::UnboundedSender<Data>,
) {
let client = Client {
id,
@@ -303,42 +301,12 @@ pub fn switch_back(id: i32) {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
impl<T: InvokeUiCM> IpcTaskRunner<T> {
#[cfg(windows)]
async fn enable_cliprdr_file_context(&mut self, conn_id: i32, enabled: bool) {
if conn_id == 0 {
return;
}
let pre_enabled = ContextSend::is_enabled();
ContextSend::enable(enabled);
if !pre_enabled && ContextSend::is_enabled() {
allow_err!(
self.stream
.send(&Data::ClipboardFile(clipboard::ClipboardFile::MonitorReady))
.await
);
}
set_conn_enabled(conn_id, enabled);
if !enabled {
ContextSend::proc(|context: &mut Box<CliprdrClientContext>| -> u32 {
empty_clipboard(context, conn_id);
0
});
}
}
async fn run(&mut self) {
use hbb_common::config::LocalConfig;
// for tmp use, without real conn id
let mut write_jobs: Vec<fs::TransferJob> = Vec::new();
#[cfg(windows)]
if self.conn_id > 0 {
self.enable_cliprdr_file_context(self.conn_id, self.file_transfer_enabled)
.await;
}
#[cfg(windows)]
let rx_clip1;
let mut rx_clip;
@@ -382,15 +350,11 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
break;
}
Data::Close => {
#[cfg(windows)]
self.enable_cliprdr_file_context(self.conn_id, false).await;
log::info!("cm ipc connection closed from connection request");
break;
}
Data::Disconnected => {
self.close = false;
#[cfg(windows)]
self.enable_cliprdr_file_context(self.conn_id, false).await;
log::info!("cm ipc connection disconnect");
break;
}
@@ -414,21 +378,18 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
handle_fs(fs, &mut write_jobs, &self.tx).await;
}
}
#[cfg(windows)]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
Data::ClipboardFile(_clip) => {
#[cfg(windows)]
{
let conn_id = self.conn_id;
ContextSend::proc(|context: &mut Box<CliprdrClientContext>| -> u32 {
clipboard::server_clip_file(context, conn_id, _clip)
});
if !_clip.is_stopping_allowed() || self.file_transfer_enabled {
let conn_id = self.conn_id;
ContextSend::proc(|context: &mut Box<CliprdrClientContext>| -> u32 {
clipboard::server_clip_file(context, conn_id, _clip)
});
}
}
}
#[cfg(windows)]
Data::ClipboardFileEnabled(_enabled) => {
#[cfg(windows)]
self.enable_cliprdr_file_context(self.conn_id, _enabled).await;
}
Data::Theme(dark) => {
self.cm.change_theme(dark);
}
@@ -457,6 +418,11 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
}
}
Some(data) = self.rx.recv() => {
if let Data::SwitchPermission{name, enabled} = &data {
if name == "file" {
self.file_transfer_enabled = *enabled;
}
}
if self.stream.send(&data).await.is_err() {
break;
}
@@ -464,7 +430,9 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
clip_file = rx_clip.recv() => match clip_file {
Some(_clip) => {
#[cfg(windows)]
allow_err!(self.tx.send(Data::ClipboardFile(_clip)));
if !_clip.is_stopping_allowed() || self.file_transfer_enabled {
allow_err!(self.tx.send(Data::ClipboardFile(_clip)));
}
}
None => {
//
@@ -517,6 +485,13 @@ pub async fn start_ipc<T: InvokeUiCM>(cm: ConnectionManager<T>) {
allow_err!(crate::privacy_win_mag::start());
});
#[cfg(target_os = "windows")]
ContextSend::enable(
Config::get_option("enable-file-transfer").is_empty(),
true,
crate::is_server(),
);
match ipc::new_listener("_cm").await {
Ok(mut incoming) => {
while let Some(result) = incoming.next().await {

View File

@@ -857,6 +857,8 @@ async fn check_connect_status_(reconnect: bool, rx: mpsc::UnboundedReceiver<ipc:
let mut rx = rx;
let mut mouse_time = 0;
let mut id = "".to_owned();
let mut enable_file_transfer = "".to_owned();
loop {
if let Ok(mut c) = ipc::connect(1000, "").await {
let mut timer = time::interval(time::Duration::from_secs(1));
@@ -875,6 +877,19 @@ async fn check_connect_status_(reconnect: bool, rx: mpsc::UnboundedReceiver<ipc:
Ok(Some(ipc::Data::Options(Some(v)))) => {
*OPTIONS.lock().unwrap() = v;
*OPTION_SYNCED.lock().unwrap() = true;
#[cfg(target_os="windows")]
{
let b = OPTIONS.lock().unwrap().get("enable-file-transfer").map(|x| x.to_string()).unwrap_or_default();
if b != enable_file_transfer {
clipboard::ContextSend::enable(
b.is_empty(),
true,
crate::is_server(),
);
enable_file_transfer = b;
}
}
}
Ok(Some(ipc::Data::Config((name, Some(value))))) => {
if name == "id" {

View File

@@ -198,6 +198,7 @@ impl<T: InvokeUiSession> Session<T> {
pub fn toggle_option(&mut self, name: String) {
let msg = self.lc.write().unwrap().toggle_option(name.clone());
#[cfg(not(feature = "flutter"))]
if name == "enable-file-transfer" {
self.send(Data::ToggleClipboardFile);
}