From 1f71dc979cbb728709ac010b093d5c54d116b2fa Mon Sep 17 00:00:00 2001 From: dignow Date: Thu, 29 Jun 2023 18:12:34 +0800 Subject: [PATCH] tmp commit Signed-off-by: dignow --- libs/clipboard/src/cliprdr.h | 2 +- libs/clipboard/src/cliprdr.rs | 2 +- libs/clipboard/src/context_send.rs | 2 +- libs/clipboard/src/lib.rs | 93 ++++++++++++++----------- libs/clipboard/src/windows/wf_cliprdr.c | 8 +-- src/client/io_loop.rs | 25 ++++--- src/clipboard_file.rs | 14 ++++ src/core_main.rs | 10 --- src/flutter.rs | 35 ---------- src/ui_cm_interface.rs | 10 +-- 10 files changed, 89 insertions(+), 112 deletions(-) diff --git a/libs/clipboard/src/cliprdr.h b/libs/clipboard/src/cliprdr.h index 2edb473fd..8b9cecef0 100644 --- a/libs/clipboard/src/cliprdr.h +++ b/libs/clipboard/src/cliprdr.h @@ -168,7 +168,7 @@ extern "C" typedef UINT (*pcCliprdrTempDirectory)(CliprdrClientContext *context, const CLIPRDR_TEMP_DIRECTORY *tempDirectory); - typedef UINT (*pcNotifyClipboardMsg)(const NOTIFICATION_MESSAGE *msg); + typedef UINT (*pcNotifyClipboardMsg)(UINT32 connID, const NOTIFICATION_MESSAGE *msg); typedef UINT (*pcCliprdrClientFormatList)(CliprdrClientContext *context, const CLIPRDR_FORMAT_LIST *formatList); diff --git a/libs/clipboard/src/cliprdr.rs b/libs/clipboard/src/cliprdr.rs index 60475f72b..c787d8ef6 100644 --- a/libs/clipboard/src/cliprdr.rs +++ b/libs/clipboard/src/cliprdr.rs @@ -357,7 +357,7 @@ pub type pcCliprdrTempDirectory = ::std::option::Option< ) -> UINT, >; pub type pcNotifyClipboardMsg = - ::std::option::Option UINT>; + ::std::option::Option UINT>; pub type pcCliprdrClientFormatList = ::std::option::Option< unsafe extern "C" fn( context: *mut CliprdrClientContext, diff --git a/libs/clipboard/src/context_send.rs b/libs/clipboard/src/context_send.rs index 76f8e4d7a..95350effc 100644 --- a/libs/clipboard/src/context_send.rs +++ b/libs/clipboard/src/context_send.rs @@ -2,7 +2,7 @@ use crate::cliprdr::*; use hbb_common::log; use std::sync::Mutex; -const CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS: u32 = 30; +const CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS: u32 = 0; lazy_static::lazy_static! { static ref CONTEXT_SEND: ContextSend = ContextSend{addr: Mutex::new(0)}; diff --git a/libs/clipboard/src/lib.rs b/libs/clipboard/src/lib.rs index ee8f5644c..3a9c6308c 100644 --- a/libs/clipboard/src/lib.rs +++ b/libs/clipboard/src/lib.rs @@ -21,15 +21,14 @@ pub use context_send::*; const ERR_CODE_SERVER_FUNCTION_NONE: u32 = 0x00000001; const ERR_CODE_INVALID_PARAMETER: u32 = 0x00000002; -pub type FnNotifyCallback = fn(r#type: u32, msg: &str, details: &str) -> u32; - -lazy_static::lazy_static! { - static ref NOTIFY_CALLBACK: Arc>> = Default::default(); -} - #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(tag = "t", content = "c")] pub enum ClipboardFile { + NotifyCallback { + r#type: String, + title: String, + text: String, + }, MonitorReady, FormatList { format_list: Vec<(i32, String)>, @@ -174,41 +173,40 @@ pub fn server_clip_file( conn_id: i32, msg: ClipboardFile, ) -> u32 { + let mut ret = 0; match msg { + ClipboardFile::NotifyCallback { .. } => { + // unreachable + } ClipboardFile::MonitorReady => { log::debug!("server_monitor_ready called"); - let ret = server_monitor_ready(context, conn_id); + ret = server_monitor_ready(context, conn_id); log::debug!("server_monitor_ready called, return {}", ret); - ret } ClipboardFile::FormatList { format_list } => { log::debug!("server_format_list called"); - let ret = server_format_list(context, conn_id, format_list); + ret = server_format_list(context, conn_id, format_list); log::debug!("server_format_list called, return {}", ret); - ret } ClipboardFile::FormatListResponse { msg_flags } => { log::debug!("format_list_response called"); - let ret = server_format_list_response(context, conn_id, msg_flags); + ret = server_format_list_response(context, conn_id, msg_flags); log::debug!("server_format_list_response called, return {}", ret); - ret } ClipboardFile::FormatDataRequest { requested_format_id, } => { log::debug!("format_data_request called"); - let ret = server_format_data_request(context, conn_id, requested_format_id); + ret = server_format_data_request(context, conn_id, requested_format_id); log::debug!("server_format_data_request called, return {}", ret); - ret } ClipboardFile::FormatDataResponse { msg_flags, format_data, } => { log::debug!("format_data_response called"); - let ret = server_format_data_response(context, conn_id, msg_flags, format_data); + ret = server_format_data_response(context, conn_id, msg_flags, format_data); log::debug!("server_format_data_response called, return {}", ret); - ret } ClipboardFile::FileContentsRequest { stream_id, @@ -221,7 +219,7 @@ pub fn server_clip_file( clip_data_id, } => { log::debug!("file_contents_request called"); - let ret = server_file_contents_request( + ret = server_file_contents_request( context, conn_id, stream_id, @@ -234,7 +232,6 @@ pub fn server_clip_file( clip_data_id, ); log::debug!("server_file_contents_request called, return {}", ret); - ret } ClipboardFile::FileContentsResponse { msg_flags, @@ -242,7 +239,7 @@ pub fn server_clip_file( requested_data, } => { log::debug!("file_contents_response called"); - let ret = server_file_contents_response( + ret = server_file_contents_response( context, conn_id, msg_flags, @@ -250,9 +247,9 @@ pub fn server_clip_file( requested_data, ); log::debug!("server_file_contents_response called, return {}", ret); - ret } } + ret } pub fn server_monitor_ready(context: &mut Box, conn_id: i32) -> u32 { @@ -469,31 +466,45 @@ pub fn create_cliprdr_context( )?) } -pub fn set_notify_callback(cb: FnNotifyCallback) { - NOTIFY_CALLBACK.lock().unwrap().replace(cb); -} - -extern "C" fn notify_callback(msg: *const NOTIFICATION_MESSAGE) -> UINT { +extern "C" fn notify_callback(conn_id: UINT32, msg: *const NOTIFICATION_MESSAGE) -> UINT { log::debug!("notify_callback called"); - if let Some(cb) = NOTIFY_CALLBACK.lock().unwrap().as_ref() { - unsafe { - let msg = &*msg; - let details = if msg.details.is_null() { - Ok("") - } else { - CStr::from_ptr(msg.details as _).to_str() - }; - match (CStr::from_ptr(msg.msg as _).to_str(), details) { - (Ok(m), Ok(d)) => cb(msg.r#type, m, d), - _ => { - log::error!("notify_callback: failed to convert msg"); - ERR_CODE_INVALID_PARAMETER + let data = unsafe { + let msg = &*msg; + let details = if msg.details.is_null() { + Ok("") + } else { + CStr::from_ptr(msg.details as _).to_str() + }; + match (CStr::from_ptr(msg.msg as _).to_str(), details) { + (Ok(m), Ok(d)) => { + let msgtype = format!( + "{}-nocancel-nook-hasclose", + if msg.r#type == 0 { + "info" + } else if msg.r#type == 1 { + "warn" + } else { + "error" + } + ); + let title = "Clipboard"; + let text = format!("{}: {}", m, d); + ClipboardFile::NotifyCallback { + r#type: msgtype, + title: title.to_string(), + text, } } + _ => { + log::error!("notify_callback: failed to convert msg"); + return ERR_CODE_INVALID_PARAMETER; + } } - } else { - ERR_CODE_SERVER_FUNCTION_NONE - } + }; + // no need to handle result here + send_data(conn_id as _, data); + + 0 } extern "C" fn client_format_list( diff --git a/libs/clipboard/src/windows/wf_cliprdr.c b/libs/clipboard/src/windows/wf_cliprdr.c index 82b97216a..801fa71f3 100644 --- a/libs/clipboard/src/windows/wf_cliprdr.c +++ b/libs/clipboard/src/windows/wf_cliprdr.c @@ -1449,7 +1449,7 @@ static UINT cliprdr_send_format_list(wfClipboard *clipboard, UINT32 connID) return rc; } -UINT wait_response_event(wfClipboard *clipboard, HANDLE event, void **data) +UINT wait_response_event(UINT32 connID, wfClipboard *clipboard, HANDLE event, void **data) { UINT rc = ERROR_SUCCESS; clipboard->context->IsStopped = FALSE; @@ -1495,7 +1495,7 @@ UINT wait_response_event(wfClipboard *clipboard, HANDLE event, void **data) msg.type = 2; msg.msg = "clipboard_wait_response_timeout_tip"; msg.details = NULL; - clipboard->context->NotifyClipboardMsg(&msg); + clipboard->context->NotifyClipboardMsg(connID, &msg); rc = ERROR_INTERNAL_ERROR; if (!ResetEvent(event)) @@ -1535,7 +1535,7 @@ static UINT cliprdr_send_data_request(UINT32 connID, wfClipboard *clipboard, UIN return rc; } - wait_response_event(clipboard, clipboard->response_data_event, &clipboard->hmem); + wait_response_event(connID, clipboard, clipboard->response_data_event, &clipboard->hmem); } UINT cliprdr_send_request_filecontents(wfClipboard *clipboard, UINT32 connID, const void *streamid, ULONG index, @@ -1563,7 +1563,7 @@ UINT cliprdr_send_request_filecontents(wfClipboard *clipboard, UINT32 connID, co return rc; } - return wait_response_event(clipboard, clipboard->req_fevent, (void **)&clipboard->req_fdata); + return wait_response_event(connID, clipboard, clipboard->req_fevent, (void **)&clipboard->req_fdata); } static UINT cliprdr_send_response_filecontents( diff --git a/src/client/io_loop.rs b/src/client/io_loop.rs index d75b53888..ff1387dd0 100644 --- a/src/client/io_loop.rs +++ b/src/client/io_loop.rs @@ -206,16 +206,21 @@ impl Remote { _msg = rx_clip_client.recv() => { #[cfg(windows)] match _msg { - Some(clip) => { - let is_stopping_allowed = clip.is_stopping_allowed(); - let server_file_transfer_enabled = *self.handler.server_file_transfer_enabled.read().unwrap(); - let file_transfer_enabled = self.handler.lc.read().unwrap().enable_file_transfer.v; - let stop = is_stopping_allowed && !(server_file_transfer_enabled && file_transfer_enabled); - log::debug!("Process clipboard message from system, stop: {}, is_stopping_allowed: {}, server_file_transfer_enabled: {}, file_transfer_enabled: {}", stop, is_stopping_allowed, server_file_transfer_enabled, file_transfer_enabled); - if stop { - ContextSend::set_is_stopped(); - } else { - allow_err!(peer.send(&crate::clipboard_file::clip_2_msg(clip)).await); + Some(clip) => match clip { + clipboard::ClipboardFile::NotifyCallback{r#type, title, text} => { + self.handler.msgbox(&r#type, &title, &text, ""); + } + _ => { + let is_stopping_allowed = clip.is_stopping_allowed(); + let server_file_transfer_enabled = *self.handler.server_file_transfer_enabled.read().unwrap(); + let file_transfer_enabled = self.handler.lc.read().unwrap().enable_file_transfer.v; + let stop = is_stopping_allowed && !(server_file_transfer_enabled && file_transfer_enabled); + log::debug!("Process clipboard message from system, stop: {}, is_stopping_allowed: {}, server_file_transfer_enabled: {}, file_transfer_enabled: {}", stop, is_stopping_allowed, server_file_transfer_enabled, file_transfer_enabled); + if stop { + ContextSend::set_is_stopped(); + } else { + allow_err!(peer.send(&crate::clipboard_file::clip_2_msg(clip)).await); + } } } None => { diff --git a/src/clipboard_file.rs b/src/clipboard_file.rs index f0fe41b8d..a4bfc1aef 100644 --- a/src/clipboard_file.rs +++ b/src/clipboard_file.rs @@ -3,6 +3,20 @@ use hbb_common::message_proto::*; pub fn clip_2_msg(clip: ClipboardFile) -> Message { match clip { + ClipboardFile::NotifyCallback { + r#type, + title, + text, + } => Message { + union: Some(message::Union::MessageBox(MessageBox { + msgtype: r#type, + title, + text, + link: "".to_string(), + ..Default::default() + })), + ..Default::default() + }, ClipboardFile::MonitorReady => Message { union: Some(message::Union::Cliprdr(Cliprdr { union: Some(cliprdr::Union::Ready(CliprdrMonitorReady { diff --git a/src/core_main.rs b/src/core_main.rs index cd0067624..5a8638419 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -13,8 +13,6 @@ use hbb_common::platform::register_breakdown_handler; /// If it returns [`Some`], then the process will continue, and flutter gui will be started. #[cfg(not(any(target_os = "android", target_os = "ios")))] pub fn core_main() -> Option> { - use crate::flutter; - let mut args = Vec::new(); let mut flutter_args = Vec::new(); let mut i = 0; @@ -127,14 +125,6 @@ pub fn core_main() -> Option> { #[cfg(windows)] { clipboard::ContextSend::enable(true); - #[cfg(feature = "flutter")] - { - clipboard::set_notify_callback(flutter::msgbox_clipboard_remote); - } - #[cfg(not(feature = "flutter"))] - { - // notify callback for non-flutter is not supported. - } } std::thread::spawn(move || crate::start_server(false)); } else { diff --git a/src/flutter.rs b/src/flutter.rs index 972374f23..4008b2420 100644 --- a/src/flutter.rs +++ b/src/flutter.rs @@ -1089,41 +1089,6 @@ pub fn stop_global_event_stream(app_type: String) { let _ = GLOBAL_EVENT_STREAM.write().unwrap().remove(&app_type); } -fn msgbox_clipboard_(channel: &str, r#type: u32, msg: &str, details: &str) -> u32 { - let msgtype = format!( - "{}-nocancel-nook-hasclose", - if r#type == 0 { - "info" - } else if r#type == 1 { - "warn" - } else { - "error" - } - ); - let text = format!("{} {}", msg, details); - if let Ok(event) = serde_json::ser::to_string(&HashMap::from([ - ("name", "msgbox"), - ("type", &msgtype as &str), - ("title", "clipboard"), - ("text", &text), - ("link", ""), - ("hasRetry", ""), - ])) { - push_global_event(channel, event); - } - 0 -} - -#[inline] -pub fn msgbox_clipboard_remote(r#type: u32, msg: &str, details: &str) -> u32 { - msgbox_clipboard_(APP_TYPE_DESKTOP_REMOTE, r#type, msg, details) -} - -#[inline] -pub fn msgbox_clipboard_cm(r#type: u32, msg: &str, details: &str) -> u32 { - msgbox_clipboard_(APP_TYPE_CM, r#type, msg, details) -} - #[no_mangle] unsafe extern "C" fn get_rgba() {} diff --git a/src/ui_cm_interface.rs b/src/ui_cm_interface.rs index 2031d7f06..e5a5c8e3b 100644 --- a/src/ui_cm_interface.rs +++ b/src/ui_cm_interface.rs @@ -423,7 +423,7 @@ impl IpcTaskRunner { Data::ClipboardFileEnabled(_enabled) => { #[cfg(windows)] { - self.file_transfer_enabled_peer =_enabled; + self.file_transfer_enabled_peer = _enabled; } } Data::Theme(dark) => { @@ -538,14 +538,6 @@ pub async fn start_ipc(cm: ConnectionManager) { #[cfg(target_os = "windows")] ContextSend::enable(Config::get_option("enable-file-transfer").is_empty()); - #[cfg(feature = "flutter")] - { - clipboard::set_notify_callback(crate::flutter::msgbox_clipboard_cm); - } - #[cfg(not(feature = "flutter"))] - { - // notify callback for non-flutter is not supported. - } match ipc::new_listener("_cm").await { Ok(mut incoming) => {