tmp commit

Signed-off-by: dignow <linlong1265@gmail.com>
This commit is contained in:
dignow 2023-06-29 18:12:34 +08:00
parent aaca56c1f8
commit 1f71dc979c
10 changed files with 89 additions and 112 deletions

View File

@ -168,7 +168,7 @@ extern "C"
typedef UINT (*pcCliprdrTempDirectory)(CliprdrClientContext *context, typedef UINT (*pcCliprdrTempDirectory)(CliprdrClientContext *context,
const CLIPRDR_TEMP_DIRECTORY *tempDirectory); 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, typedef UINT (*pcCliprdrClientFormatList)(CliprdrClientContext *context,
const CLIPRDR_FORMAT_LIST *formatList); const CLIPRDR_FORMAT_LIST *formatList);

View File

@ -357,7 +357,7 @@ pub type pcCliprdrTempDirectory = ::std::option::Option<
) -> UINT, ) -> UINT,
>; >;
pub type pcNotifyClipboardMsg = pub type pcNotifyClipboardMsg =
::std::option::Option<unsafe extern "C" fn(msg: *const NOTIFICATION_MESSAGE) -> UINT>; ::std::option::Option<unsafe extern "C" fn(connID: UINT32, msg: *const NOTIFICATION_MESSAGE) -> UINT>;
pub type pcCliprdrClientFormatList = ::std::option::Option< pub type pcCliprdrClientFormatList = ::std::option::Option<
unsafe extern "C" fn( unsafe extern "C" fn(
context: *mut CliprdrClientContext, context: *mut CliprdrClientContext,

View File

@ -2,7 +2,7 @@ use crate::cliprdr::*;
use hbb_common::log; use hbb_common::log;
use std::sync::Mutex; use std::sync::Mutex;
const CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS: u32 = 30; const CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS: u32 = 0;
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref CONTEXT_SEND: ContextSend = ContextSend{addr: Mutex::new(0)}; static ref CONTEXT_SEND: ContextSend = ContextSend{addr: Mutex::new(0)};

View File

@ -21,15 +21,14 @@ pub use context_send::*;
const ERR_CODE_SERVER_FUNCTION_NONE: u32 = 0x00000001; const ERR_CODE_SERVER_FUNCTION_NONE: u32 = 0x00000001;
const ERR_CODE_INVALID_PARAMETER: u32 = 0x00000002; 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<Mutex<Option<FnNotifyCallback>>> = Default::default();
}
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "t", content = "c")] #[serde(tag = "t", content = "c")]
pub enum ClipboardFile { pub enum ClipboardFile {
NotifyCallback {
r#type: String,
title: String,
text: String,
},
MonitorReady, MonitorReady,
FormatList { FormatList {
format_list: Vec<(i32, String)>, format_list: Vec<(i32, String)>,
@ -174,41 +173,40 @@ pub fn server_clip_file(
conn_id: i32, conn_id: i32,
msg: ClipboardFile, msg: ClipboardFile,
) -> u32 { ) -> u32 {
let mut ret = 0;
match msg { match msg {
ClipboardFile::NotifyCallback { .. } => {
// unreachable
}
ClipboardFile::MonitorReady => { ClipboardFile::MonitorReady => {
log::debug!("server_monitor_ready called"); 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); log::debug!("server_monitor_ready called, return {}", ret);
ret
} }
ClipboardFile::FormatList { format_list } => { ClipboardFile::FormatList { format_list } => {
log::debug!("server_format_list called"); 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); log::debug!("server_format_list called, return {}", ret);
ret
} }
ClipboardFile::FormatListResponse { msg_flags } => { ClipboardFile::FormatListResponse { msg_flags } => {
log::debug!("format_list_response called"); 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); log::debug!("server_format_list_response called, return {}", ret);
ret
} }
ClipboardFile::FormatDataRequest { ClipboardFile::FormatDataRequest {
requested_format_id, requested_format_id,
} => { } => {
log::debug!("format_data_request called"); 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); log::debug!("server_format_data_request called, return {}", ret);
ret
} }
ClipboardFile::FormatDataResponse { ClipboardFile::FormatDataResponse {
msg_flags, msg_flags,
format_data, format_data,
} => { } => {
log::debug!("format_data_response called"); 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); log::debug!("server_format_data_response called, return {}", ret);
ret
} }
ClipboardFile::FileContentsRequest { ClipboardFile::FileContentsRequest {
stream_id, stream_id,
@ -221,7 +219,7 @@ pub fn server_clip_file(
clip_data_id, clip_data_id,
} => { } => {
log::debug!("file_contents_request called"); log::debug!("file_contents_request called");
let ret = server_file_contents_request( ret = server_file_contents_request(
context, context,
conn_id, conn_id,
stream_id, stream_id,
@ -234,7 +232,6 @@ pub fn server_clip_file(
clip_data_id, clip_data_id,
); );
log::debug!("server_file_contents_request called, return {}", ret); log::debug!("server_file_contents_request called, return {}", ret);
ret
} }
ClipboardFile::FileContentsResponse { ClipboardFile::FileContentsResponse {
msg_flags, msg_flags,
@ -242,7 +239,7 @@ pub fn server_clip_file(
requested_data, requested_data,
} => { } => {
log::debug!("file_contents_response called"); log::debug!("file_contents_response called");
let ret = server_file_contents_response( ret = server_file_contents_response(
context, context,
conn_id, conn_id,
msg_flags, msg_flags,
@ -250,9 +247,9 @@ pub fn server_clip_file(
requested_data, requested_data,
); );
log::debug!("server_file_contents_response called, return {}", ret); log::debug!("server_file_contents_response called, return {}", ret);
ret
} }
} }
ret
} }
pub fn server_monitor_ready(context: &mut Box<CliprdrClientContext>, conn_id: i32) -> u32 { pub fn server_monitor_ready(context: &mut Box<CliprdrClientContext>, conn_id: i32) -> u32 {
@ -469,31 +466,45 @@ pub fn create_cliprdr_context(
)?) )?)
} }
pub fn set_notify_callback(cb: FnNotifyCallback) { extern "C" fn notify_callback(conn_id: UINT32, msg: *const NOTIFICATION_MESSAGE) -> UINT {
NOTIFY_CALLBACK.lock().unwrap().replace(cb);
}
extern "C" fn notify_callback(msg: *const NOTIFICATION_MESSAGE) -> UINT {
log::debug!("notify_callback called"); log::debug!("notify_callback called");
if let Some(cb) = NOTIFY_CALLBACK.lock().unwrap().as_ref() { let data = unsafe {
unsafe { let msg = &*msg;
let msg = &*msg; let details = if msg.details.is_null() {
let details = if msg.details.is_null() { Ok("")
Ok("") } else {
} else { CStr::from_ptr(msg.details as _).to_str()
CStr::from_ptr(msg.details as _).to_str() };
}; match (CStr::from_ptr(msg.msg as _).to_str(), details) {
match (CStr::from_ptr(msg.msg as _).to_str(), details) { (Ok(m), Ok(d)) => {
(Ok(m), Ok(d)) => cb(msg.r#type, m, d), let msgtype = format!(
_ => { "{}-nocancel-nook-hasclose",
log::error!("notify_callback: failed to convert msg"); if msg.r#type == 0 {
ERR_CODE_INVALID_PARAMETER "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( extern "C" fn client_format_list(

View File

@ -1449,7 +1449,7 @@ static UINT cliprdr_send_format_list(wfClipboard *clipboard, UINT32 connID)
return rc; 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; UINT rc = ERROR_SUCCESS;
clipboard->context->IsStopped = FALSE; clipboard->context->IsStopped = FALSE;
@ -1495,7 +1495,7 @@ UINT wait_response_event(wfClipboard *clipboard, HANDLE event, void **data)
msg.type = 2; msg.type = 2;
msg.msg = "clipboard_wait_response_timeout_tip"; msg.msg = "clipboard_wait_response_timeout_tip";
msg.details = NULL; msg.details = NULL;
clipboard->context->NotifyClipboardMsg(&msg); clipboard->context->NotifyClipboardMsg(connID, &msg);
rc = ERROR_INTERNAL_ERROR; rc = ERROR_INTERNAL_ERROR;
if (!ResetEvent(event)) if (!ResetEvent(event))
@ -1535,7 +1535,7 @@ static UINT cliprdr_send_data_request(UINT32 connID, wfClipboard *clipboard, UIN
return rc; 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, 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 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( static UINT cliprdr_send_response_filecontents(

View File

@ -206,16 +206,21 @@ impl<T: InvokeUiSession> Remote<T> {
_msg = rx_clip_client.recv() => { _msg = rx_clip_client.recv() => {
#[cfg(windows)] #[cfg(windows)]
match _msg { match _msg {
Some(clip) => { Some(clip) => match clip {
let is_stopping_allowed = clip.is_stopping_allowed(); clipboard::ClipboardFile::NotifyCallback{r#type, title, text} => {
let server_file_transfer_enabled = *self.handler.server_file_transfer_enabled.read().unwrap(); self.handler.msgbox(&r#type, &title, &text, "");
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); let is_stopping_allowed = clip.is_stopping_allowed();
if stop { let server_file_transfer_enabled = *self.handler.server_file_transfer_enabled.read().unwrap();
ContextSend::set_is_stopped(); let file_transfer_enabled = self.handler.lc.read().unwrap().enable_file_transfer.v;
} else { let stop = is_stopping_allowed && !(server_file_transfer_enabled && file_transfer_enabled);
allow_err!(peer.send(&crate::clipboard_file::clip_2_msg(clip)).await); 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 => { None => {

View File

@ -3,6 +3,20 @@ use hbb_common::message_proto::*;
pub fn clip_2_msg(clip: ClipboardFile) -> Message { pub fn clip_2_msg(clip: ClipboardFile) -> Message {
match clip { 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 { ClipboardFile::MonitorReady => Message {
union: Some(message::Union::Cliprdr(Cliprdr { union: Some(message::Union::Cliprdr(Cliprdr {
union: Some(cliprdr::Union::Ready(CliprdrMonitorReady { union: Some(cliprdr::Union::Ready(CliprdrMonitorReady {

View File

@ -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. /// If it returns [`Some`], then the process will continue, and flutter gui will be started.
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn core_main() -> Option<Vec<String>> { pub fn core_main() -> Option<Vec<String>> {
use crate::flutter;
let mut args = Vec::new(); let mut args = Vec::new();
let mut flutter_args = Vec::new(); let mut flutter_args = Vec::new();
let mut i = 0; let mut i = 0;
@ -127,14 +125,6 @@ pub fn core_main() -> Option<Vec<String>> {
#[cfg(windows)] #[cfg(windows)]
{ {
clipboard::ContextSend::enable(true); 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)); std::thread::spawn(move || crate::start_server(false));
} else { } else {

View File

@ -1089,41 +1089,6 @@ pub fn stop_global_event_stream(app_type: String) {
let _ = GLOBAL_EVENT_STREAM.write().unwrap().remove(&app_type); 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] #[no_mangle]
unsafe extern "C" fn get_rgba() {} unsafe extern "C" fn get_rgba() {}

View File

@ -423,7 +423,7 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
Data::ClipboardFileEnabled(_enabled) => { Data::ClipboardFileEnabled(_enabled) => {
#[cfg(windows)] #[cfg(windows)]
{ {
self.file_transfer_enabled_peer =_enabled; self.file_transfer_enabled_peer = _enabled;
} }
} }
Data::Theme(dark) => { Data::Theme(dark) => {
@ -538,14 +538,6 @@ pub async fn start_ipc<T: InvokeUiCM>(cm: ConnectionManager<T>) {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
ContextSend::enable(Config::get_option("enable-file-transfer").is_empty()); 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 { match ipc::new_listener("_cm").await {
Ok(mut incoming) => { Ok(mut incoming) => {