2022-02-14 17:34:09 +08:00
|
|
|
use cliprdr::*;
|
|
|
|
use hbb_common::{
|
2023-06-29 13:47:55 +08:00
|
|
|
allow_err, lazy_static, log,
|
2022-02-14 17:34:09 +08:00
|
|
|
tokio::sync::{
|
|
|
|
mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
|
|
|
|
Mutex as TokioMutex,
|
|
|
|
},
|
2023-06-06 07:39:44 +08:00
|
|
|
ResultType, SessionID,
|
2022-02-14 17:34:09 +08:00
|
|
|
};
|
2022-02-22 14:17:50 +08:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2022-02-14 17:34:09 +08:00
|
|
|
use std::{
|
|
|
|
boxed::Box,
|
|
|
|
ffi::{CStr, CString},
|
2022-10-25 18:46:38 +08:00
|
|
|
sync::{Arc, Mutex, RwLock},
|
2022-02-14 17:34:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
pub mod cliprdr;
|
2022-10-26 22:37:45 +08:00
|
|
|
pub mod context_send;
|
|
|
|
pub use context_send::*;
|
2022-02-14 17:34:09 +08:00
|
|
|
|
2023-06-18 22:55:38 +08:00
|
|
|
const ERR_CODE_SERVER_FUNCTION_NONE: u32 = 0x00000001;
|
2023-06-29 13:47:55 +08:00
|
|
|
const ERR_CODE_INVALID_PARAMETER: u32 = 0x00000002;
|
|
|
|
|
2022-02-22 14:17:50 +08:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
#[serde(tag = "t", content = "c")]
|
2023-01-09 15:30:21 +08:00
|
|
|
pub enum ClipboardFile {
|
2023-06-29 18:12:34 +08:00
|
|
|
NotifyCallback {
|
|
|
|
r#type: String,
|
|
|
|
title: String,
|
|
|
|
text: String,
|
|
|
|
},
|
2022-10-26 19:42:14 +08:00
|
|
|
MonitorReady,
|
2022-10-25 18:46:38 +08:00
|
|
|
FormatList {
|
2022-02-22 14:17:50 +08:00
|
|
|
format_list: Vec<(i32, String)>,
|
|
|
|
},
|
2022-10-25 18:46:38 +08:00
|
|
|
FormatListResponse {
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags: i32,
|
|
|
|
},
|
2022-10-25 18:46:38 +08:00
|
|
|
FormatDataRequest {
|
2022-02-22 14:17:50 +08:00
|
|
|
requested_format_id: i32,
|
|
|
|
},
|
2022-10-25 18:46:38 +08:00
|
|
|
FormatDataResponse {
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags: i32,
|
|
|
|
format_data: Vec<u8>,
|
|
|
|
},
|
|
|
|
FileContentsRequest {
|
|
|
|
stream_id: i32,
|
|
|
|
list_index: i32,
|
|
|
|
dw_flags: i32,
|
|
|
|
n_position_low: i32,
|
|
|
|
n_position_high: i32,
|
|
|
|
cb_requested: i32,
|
|
|
|
have_clip_data_id: bool,
|
|
|
|
clip_data_id: i32,
|
|
|
|
},
|
|
|
|
FileContentsResponse {
|
|
|
|
msg_flags: i32,
|
|
|
|
stream_id: i32,
|
|
|
|
requested_data: Vec<u8>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:46:38 +08:00
|
|
|
struct MsgChannel {
|
2023-06-06 07:39:44 +08:00
|
|
|
session_uuid: SessionID,
|
2022-10-25 18:46:38 +08:00
|
|
|
conn_id: i32,
|
2023-01-09 15:30:21 +08:00
|
|
|
sender: UnboundedSender<ClipboardFile>,
|
|
|
|
receiver: Arc<TokioMutex<UnboundedReceiver<ClipboardFile>>>,
|
2022-10-25 18:46:38 +08:00
|
|
|
}
|
2022-02-22 22:26:22 +08:00
|
|
|
|
2022-10-25 18:46:38 +08:00
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref VEC_MSG_CHANNEL: RwLock<Vec<MsgChannel>> = Default::default();
|
2023-06-18 20:23:54 +08:00
|
|
|
static ref CLIENT_CONN_ID_COUNTER: Mutex<i32> = Mutex::new(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClipboardFile {
|
|
|
|
pub fn is_stopping_allowed(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
ClipboardFile::MonitorReady
|
|
|
|
| ClipboardFile::FormatList { .. }
|
|
|
|
| ClipboardFile::FormatDataRequest { .. } => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 00:05:23 +08:00
|
|
|
|
|
|
|
pub fn is_stopping_allowed_from_peer(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
ClipboardFile::MonitorReady | ClipboardFile::FormatList { .. } => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
|
|
|
|
2023-06-06 07:39:44 +08:00
|
|
|
pub fn get_client_conn_id(session_uuid: &SessionID) -> Option<i32> {
|
2022-10-27 10:56:14 +08:00
|
|
|
VEC_MSG_CHANNEL
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
2023-06-06 07:39:44 +08:00
|
|
|
.find(|x| x.session_uuid == session_uuid.to_owned())
|
2022-10-27 10:56:14 +08:00
|
|
|
.map(|x| x.conn_id)
|
|
|
|
}
|
|
|
|
|
2023-06-18 20:23:54 +08:00
|
|
|
fn get_conn_id() -> i32 {
|
|
|
|
let mut lock = CLIENT_CONN_ID_COUNTER.lock().unwrap();
|
|
|
|
*lock += 1;
|
|
|
|
*lock
|
|
|
|
}
|
|
|
|
|
2022-10-27 10:56:14 +08:00
|
|
|
pub fn get_rx_cliprdr_client(
|
2023-06-06 07:39:44 +08:00
|
|
|
session_uuid: &SessionID,
|
2023-01-09 15:30:21 +08:00
|
|
|
) -> (i32, Arc<TokioMutex<UnboundedReceiver<ClipboardFile>>>) {
|
2022-10-25 18:46:38 +08:00
|
|
|
let mut lock = VEC_MSG_CHANNEL.write().unwrap();
|
2023-06-06 07:39:44 +08:00
|
|
|
match lock
|
|
|
|
.iter()
|
|
|
|
.find(|x| x.session_uuid == session_uuid.to_owned())
|
|
|
|
{
|
2022-10-25 18:46:38 +08:00
|
|
|
Some(msg_channel) => (msg_channel.conn_id, msg_channel.receiver.clone()),
|
|
|
|
None => {
|
|
|
|
let (sender, receiver) = unbounded_channel();
|
|
|
|
let receiver = Arc::new(TokioMutex::new(receiver));
|
|
|
|
let receiver2 = receiver.clone();
|
2023-06-18 20:23:54 +08:00
|
|
|
let conn_id = get_conn_id();
|
2022-10-25 18:46:38 +08:00
|
|
|
let msg_channel = MsgChannel {
|
2023-06-06 07:39:44 +08:00
|
|
|
session_uuid: session_uuid.to_owned(),
|
2022-10-25 18:46:38 +08:00
|
|
|
conn_id,
|
|
|
|
sender,
|
|
|
|
receiver,
|
|
|
|
};
|
|
|
|
lock.push(msg_channel);
|
|
|
|
(conn_id, receiver2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:30:21 +08:00
|
|
|
pub fn get_rx_cliprdr_server(conn_id: i32) -> Arc<TokioMutex<UnboundedReceiver<ClipboardFile>>> {
|
2022-10-25 18:46:38 +08:00
|
|
|
let mut lock = VEC_MSG_CHANNEL.write().unwrap();
|
|
|
|
match lock.iter().find(|x| x.conn_id == conn_id) {
|
|
|
|
Some(msg_channel) => msg_channel.receiver.clone(),
|
|
|
|
None => {
|
|
|
|
let (sender, receiver) = unbounded_channel();
|
|
|
|
let receiver = Arc::new(TokioMutex::new(receiver));
|
|
|
|
let receiver2 = receiver.clone();
|
|
|
|
let msg_channel = MsgChannel {
|
2023-06-06 07:39:44 +08:00
|
|
|
session_uuid: SessionID::nil(),
|
2022-10-25 18:46:38 +08:00
|
|
|
conn_id,
|
|
|
|
sender,
|
|
|
|
receiver,
|
|
|
|
};
|
|
|
|
lock.push(msg_channel);
|
|
|
|
receiver2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-01-09 15:30:21 +08:00
|
|
|
fn send_data(conn_id: i32, data: ClipboardFile) {
|
2022-10-25 18:46:38 +08:00
|
|
|
// no need to handle result here
|
|
|
|
if let Some(msg_channel) = VEC_MSG_CHANNEL
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.find(|x| x.conn_id == conn_id)
|
|
|
|
{
|
|
|
|
allow_err!(msg_channel.sender.send(data));
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
|
|
|
|
2022-02-24 11:22:19 +08:00
|
|
|
pub fn empty_clipboard(context: &mut Box<CliprdrClientContext>, conn_id: i32) -> bool {
|
|
|
|
unsafe { TRUE == cliprdr::empty_cliprdr(&mut (**context), conn_id as u32) }
|
2022-02-22 23:53:28 +08:00
|
|
|
}
|
|
|
|
|
2022-02-22 14:17:50 +08:00
|
|
|
pub fn server_clip_file(
|
|
|
|
context: &mut Box<CliprdrClientContext>,
|
2022-10-25 18:46:38 +08:00
|
|
|
conn_id: i32,
|
2023-01-09 15:30:21 +08:00
|
|
|
msg: ClipboardFile,
|
2022-02-22 14:17:50 +08:00
|
|
|
) -> u32 {
|
2023-06-29 18:12:34 +08:00
|
|
|
let mut ret = 0;
|
2022-02-22 14:17:50 +08:00
|
|
|
match msg {
|
2023-06-29 18:12:34 +08:00
|
|
|
ClipboardFile::NotifyCallback { .. } => {
|
|
|
|
// unreachable
|
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
ClipboardFile::MonitorReady => {
|
2022-10-26 19:42:14 +08:00
|
|
|
log::debug!("server_monitor_ready called");
|
2023-06-29 18:12:34 +08:00
|
|
|
ret = server_monitor_ready(context, conn_id);
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"server_monitor_ready called, conn_id {}, return {}",
|
|
|
|
conn_id,
|
|
|
|
ret
|
|
|
|
);
|
2022-10-26 19:42:14 +08:00
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
ClipboardFile::FormatList { format_list } => {
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"server_format_list called, conn_id {}, format_list: {:?}",
|
|
|
|
conn_id,
|
|
|
|
&format_list
|
|
|
|
);
|
2023-06-29 18:12:34 +08:00
|
|
|
ret = server_format_list(context, conn_id, format_list);
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"server_format_list called, conn_id {}, return {}",
|
|
|
|
conn_id,
|
|
|
|
ret
|
|
|
|
);
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
ClipboardFile::FormatListResponse { msg_flags } => {
|
2023-07-30 16:42:39 +08:00
|
|
|
log::debug!("server_format_list_response called");
|
2023-06-29 18:12:34 +08:00
|
|
|
ret = server_format_list_response(context, conn_id, msg_flags);
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"server_format_list_response called, conn_id {}, msg_flags {}, return {}",
|
|
|
|
conn_id,
|
|
|
|
msg_flags,
|
|
|
|
ret
|
|
|
|
);
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
ClipboardFile::FormatDataRequest {
|
2022-02-22 14:17:50 +08:00
|
|
|
requested_format_id,
|
|
|
|
} => {
|
2023-07-30 16:42:39 +08:00
|
|
|
log::debug!("server_format_data_request called");
|
2023-06-29 18:12:34 +08:00
|
|
|
ret = server_format_data_request(context, conn_id, requested_format_id);
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"server_format_data_request called, conn_id {}, requested_format_id {}, return {}",
|
|
|
|
conn_id,
|
|
|
|
requested_format_id,
|
|
|
|
ret
|
|
|
|
);
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
ClipboardFile::FormatDataResponse {
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags,
|
|
|
|
format_data,
|
|
|
|
} => {
|
2023-07-30 16:42:39 +08:00
|
|
|
log::debug!("server_format_data_response called");
|
2023-06-29 18:12:34 +08:00
|
|
|
ret = server_format_data_response(context, conn_id, msg_flags, format_data);
|
2023-07-30 16:42:39 +08:00
|
|
|
log::debug!(
|
|
|
|
"server_format_data_response called, conn_id {}, msg_flags: {}, return {}",
|
|
|
|
conn_id,
|
|
|
|
msg_flags,
|
|
|
|
ret
|
|
|
|
);
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
ClipboardFile::FileContentsRequest {
|
2022-02-22 14:17:50 +08:00
|
|
|
stream_id,
|
|
|
|
list_index,
|
|
|
|
dw_flags,
|
|
|
|
n_position_low,
|
|
|
|
n_position_high,
|
|
|
|
cb_requested,
|
|
|
|
have_clip_data_id,
|
|
|
|
clip_data_id,
|
|
|
|
} => {
|
2023-07-30 16:42:39 +08:00
|
|
|
log::debug!("server_file_contents_request called");
|
2023-06-29 18:12:34 +08:00
|
|
|
ret = server_file_contents_request(
|
2022-02-22 14:17:50 +08:00
|
|
|
context,
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id,
|
2022-02-22 14:17:50 +08:00
|
|
|
stream_id,
|
|
|
|
list_index,
|
|
|
|
dw_flags,
|
|
|
|
n_position_low,
|
|
|
|
n_position_high,
|
|
|
|
cb_requested,
|
|
|
|
have_clip_data_id,
|
|
|
|
clip_data_id,
|
|
|
|
);
|
2023-07-30 16:42:39 +08:00
|
|
|
log::debug!("server_file_contents_request called, conn_id {}, stream_id: {}, list_index {}, dw_flags {}, n_position_low {}, n_position_high {}, cb_requested {}, have_clip_data_id {}, clip_data_id {}, return {}", conn_id,
|
|
|
|
stream_id,
|
|
|
|
list_index,
|
|
|
|
dw_flags,
|
|
|
|
n_position_low,
|
|
|
|
n_position_high,
|
|
|
|
cb_requested,
|
|
|
|
have_clip_data_id,
|
|
|
|
clip_data_id,
|
|
|
|
ret
|
|
|
|
);
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
ClipboardFile::FileContentsResponse {
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags,
|
|
|
|
stream_id,
|
|
|
|
requested_data,
|
|
|
|
} => {
|
2023-07-30 16:42:39 +08:00
|
|
|
log::debug!("server_file_contents_response called");
|
2023-06-29 18:12:34 +08:00
|
|
|
ret = server_file_contents_response(
|
2022-02-22 14:17:50 +08:00
|
|
|
context,
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id,
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags,
|
|
|
|
stream_id,
|
|
|
|
requested_data,
|
|
|
|
);
|
2023-07-30 16:42:39 +08:00
|
|
|
log::debug!("server_file_contents_response called, conn_id {}, msg_flags {}, stream_id {}, return {}",
|
|
|
|
conn_id,
|
|
|
|
msg_flags,
|
|
|
|
stream_id,
|
|
|
|
ret
|
|
|
|
);
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
|
|
|
}
|
2023-06-29 18:12:34 +08:00
|
|
|
ret
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
|
|
|
|
2022-10-26 19:42:14 +08:00
|
|
|
pub fn server_monitor_ready(context: &mut Box<CliprdrClientContext>, conn_id: i32) -> u32 {
|
|
|
|
unsafe {
|
|
|
|
let monitor_ready = CLIPRDR_MONITOR_READY {
|
|
|
|
connID: conn_id as UINT32,
|
|
|
|
msgType: 0 as UINT16,
|
|
|
|
msgFlags: 0 as UINT16,
|
|
|
|
dataLen: 0 as UINT32,
|
|
|
|
};
|
2023-06-18 22:55:38 +08:00
|
|
|
if let Some(f) = (**context).MonitorReady {
|
|
|
|
let ret = f(&mut (**context), &monitor_ready);
|
|
|
|
ret as u32
|
|
|
|
} else {
|
|
|
|
ERR_CODE_SERVER_FUNCTION_NONE
|
|
|
|
}
|
2022-10-26 19:42:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:17:50 +08:00
|
|
|
pub fn server_format_list(
|
2022-02-14 17:34:09 +08:00
|
|
|
context: &mut Box<CliprdrClientContext>,
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id: i32,
|
2022-02-22 14:17:50 +08:00
|
|
|
format_list: Vec<(i32, String)>,
|
2022-02-14 17:34:09 +08:00
|
|
|
) -> u32 {
|
|
|
|
unsafe {
|
2022-02-22 14:17:50 +08:00
|
|
|
let num_formats = format_list.len() as UINT32;
|
|
|
|
let mut formats = format_list
|
2022-02-14 17:34:09 +08:00
|
|
|
.into_iter()
|
|
|
|
.map(|format| {
|
2022-02-22 14:17:50 +08:00
|
|
|
if format.1.is_empty() {
|
2022-02-14 17:34:09 +08:00
|
|
|
CLIPRDR_FORMAT {
|
2022-02-22 14:17:50 +08:00
|
|
|
formatId: format.0 as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
formatName: 0 as *mut _,
|
|
|
|
}
|
|
|
|
} else {
|
2022-02-22 14:17:50 +08:00
|
|
|
let n = match CString::new(format.1) {
|
2022-02-14 17:34:09 +08:00
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => CString::new("").unwrap(),
|
|
|
|
};
|
|
|
|
CLIPRDR_FORMAT {
|
2022-02-22 14:17:50 +08:00
|
|
|
formatId: format.0 as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
formatName: n.into_raw(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<CLIPRDR_FORMAT>>();
|
|
|
|
|
|
|
|
let format_list = CLIPRDR_FORMAT_LIST {
|
2022-02-24 11:22:19 +08:00
|
|
|
connID: conn_id as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
msgType: 0 as UINT16,
|
2022-10-26 14:15:24 +08:00
|
|
|
msgFlags: 0 as UINT16,
|
|
|
|
dataLen: 0 as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
numFormats: num_formats,
|
|
|
|
formats: formats.as_mut_ptr(),
|
|
|
|
};
|
|
|
|
|
2023-06-18 22:55:38 +08:00
|
|
|
let ret = if let Some(f) = (**context).ServerFormatList {
|
|
|
|
f(&mut (**context), &format_list)
|
|
|
|
} else {
|
|
|
|
ERR_CODE_SERVER_FUNCTION_NONE
|
|
|
|
};
|
2022-02-14 17:34:09 +08:00
|
|
|
|
|
|
|
for f in formats {
|
|
|
|
if !f.formatName.is_null() {
|
|
|
|
// retake pointer to free memory
|
|
|
|
let _ = CString::from_raw(f.formatName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret as u32
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 18:46:38 +08:00
|
|
|
|
2022-02-22 14:17:50 +08:00
|
|
|
pub fn server_format_list_response(
|
2022-02-14 17:34:09 +08:00
|
|
|
context: &mut Box<CliprdrClientContext>,
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id: i32,
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags: i32,
|
2022-02-14 17:34:09 +08:00
|
|
|
) -> u32 {
|
|
|
|
unsafe {
|
|
|
|
let format_list_response = CLIPRDR_FORMAT_LIST_RESPONSE {
|
2022-02-24 11:22:19 +08:00
|
|
|
connID: conn_id as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
msgType: 0 as UINT16,
|
2022-02-22 14:17:50 +08:00
|
|
|
msgFlags: msg_flags as UINT16,
|
2022-02-14 17:34:09 +08:00
|
|
|
dataLen: 0 as UINT32,
|
|
|
|
};
|
|
|
|
|
2023-06-18 22:55:38 +08:00
|
|
|
if let Some(f) = (**context).ServerFormatListResponse {
|
|
|
|
f(&mut (**context), &format_list_response)
|
|
|
|
} else {
|
|
|
|
ERR_CODE_SERVER_FUNCTION_NONE
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-25 18:46:38 +08:00
|
|
|
|
2022-02-22 14:17:50 +08:00
|
|
|
pub fn server_format_data_request(
|
2022-02-14 17:34:09 +08:00
|
|
|
context: &mut Box<CliprdrClientContext>,
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id: i32,
|
2022-02-22 14:17:50 +08:00
|
|
|
requested_format_id: i32,
|
2022-02-14 17:34:09 +08:00
|
|
|
) -> u32 {
|
|
|
|
unsafe {
|
|
|
|
let format_data_request = CLIPRDR_FORMAT_DATA_REQUEST {
|
2022-02-24 11:22:19 +08:00
|
|
|
connID: conn_id as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
msgType: 0 as UINT16,
|
|
|
|
msgFlags: 0 as UINT16,
|
|
|
|
dataLen: 0 as UINT32,
|
2022-02-22 14:17:50 +08:00
|
|
|
requestedFormatId: requested_format_id as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
};
|
2023-06-18 22:55:38 +08:00
|
|
|
if let Some(f) = (**context).ServerFormatDataRequest {
|
|
|
|
f(&mut (**context), &format_data_request)
|
|
|
|
} else {
|
|
|
|
ERR_CODE_SERVER_FUNCTION_NONE
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-25 18:46:38 +08:00
|
|
|
|
2022-02-22 14:17:50 +08:00
|
|
|
pub fn server_format_data_response(
|
2022-02-14 17:34:09 +08:00
|
|
|
context: &mut Box<CliprdrClientContext>,
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id: i32,
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags: i32,
|
|
|
|
mut format_data: Vec<u8>,
|
2022-02-14 17:34:09 +08:00
|
|
|
) -> u32 {
|
|
|
|
unsafe {
|
|
|
|
let format_data_response = CLIPRDR_FORMAT_DATA_RESPONSE {
|
2022-02-24 11:22:19 +08:00
|
|
|
connID: conn_id as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
msgType: 0 as UINT16,
|
2022-02-22 14:17:50 +08:00
|
|
|
msgFlags: msg_flags as UINT16,
|
|
|
|
dataLen: format_data.len() as UINT32,
|
|
|
|
requestedFormatData: format_data.as_mut_ptr(),
|
2022-02-14 17:34:09 +08:00
|
|
|
};
|
2023-06-18 22:55:38 +08:00
|
|
|
if let Some(f) = (**context).ServerFormatDataResponse {
|
|
|
|
f(&mut (**context), &format_data_response)
|
|
|
|
} else {
|
|
|
|
ERR_CODE_SERVER_FUNCTION_NONE
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-25 18:46:38 +08:00
|
|
|
|
2022-02-22 14:17:50 +08:00
|
|
|
pub fn server_file_contents_request(
|
2022-02-14 17:34:09 +08:00
|
|
|
context: &mut Box<CliprdrClientContext>,
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id: i32,
|
2022-02-22 14:17:50 +08:00
|
|
|
stream_id: i32,
|
|
|
|
list_index: i32,
|
|
|
|
dw_flags: i32,
|
|
|
|
n_position_low: i32,
|
|
|
|
n_position_high: i32,
|
|
|
|
cb_requested: i32,
|
|
|
|
have_clip_data_id: bool,
|
|
|
|
clip_data_id: i32,
|
2022-02-14 17:34:09 +08:00
|
|
|
) -> u32 {
|
|
|
|
unsafe {
|
|
|
|
let file_contents_request = CLIPRDR_FILE_CONTENTS_REQUEST {
|
2022-02-24 11:22:19 +08:00
|
|
|
connID: conn_id as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
msgType: 0 as UINT16,
|
|
|
|
msgFlags: 0 as UINT16,
|
|
|
|
dataLen: 0 as UINT32,
|
2022-02-22 14:17:50 +08:00
|
|
|
streamId: stream_id as UINT32,
|
|
|
|
listIndex: list_index as UINT32,
|
|
|
|
dwFlags: dw_flags as UINT32,
|
|
|
|
nPositionLow: n_position_low as UINT32,
|
|
|
|
nPositionHigh: n_position_high as UINT32,
|
|
|
|
cbRequested: cb_requested as UINT32,
|
|
|
|
haveClipDataId: if have_clip_data_id { TRUE } else { FALSE },
|
|
|
|
clipDataId: clip_data_id as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
};
|
2023-06-18 22:55:38 +08:00
|
|
|
if let Some(f) = (**context).ServerFileContentsRequest {
|
|
|
|
f(&mut (**context), &file_contents_request)
|
|
|
|
} else {
|
|
|
|
ERR_CODE_SERVER_FUNCTION_NONE
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-25 18:46:38 +08:00
|
|
|
|
2022-02-22 14:17:50 +08:00
|
|
|
pub fn server_file_contents_response(
|
2022-02-14 17:34:09 +08:00
|
|
|
context: &mut Box<CliprdrClientContext>,
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id: i32,
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags: i32,
|
|
|
|
stream_id: i32,
|
|
|
|
mut requested_data: Vec<u8>,
|
2022-02-14 17:34:09 +08:00
|
|
|
) -> u32 {
|
|
|
|
unsafe {
|
|
|
|
let file_contents_response = CLIPRDR_FILE_CONTENTS_RESPONSE {
|
2022-02-24 11:22:19 +08:00
|
|
|
connID: conn_id as UINT32,
|
2022-02-14 17:34:09 +08:00
|
|
|
msgType: 0 as UINT16,
|
2022-02-22 14:17:50 +08:00
|
|
|
msgFlags: msg_flags as UINT16,
|
|
|
|
dataLen: 4 + requested_data.len() as UINT32,
|
|
|
|
streamId: stream_id as UINT32,
|
|
|
|
cbRequested: requested_data.len() as UINT32,
|
|
|
|
requestedData: requested_data.as_mut_ptr(),
|
2022-02-14 17:34:09 +08:00
|
|
|
};
|
2023-06-18 22:55:38 +08:00
|
|
|
if let Some(f) = (**context).ServerFileContentsResponse {
|
|
|
|
f(&mut (**context), &file_contents_response)
|
|
|
|
} else {
|
|
|
|
ERR_CODE_SERVER_FUNCTION_NONE
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:17:50 +08:00
|
|
|
pub fn create_cliprdr_context(
|
|
|
|
enable_files: bool,
|
|
|
|
enable_others: bool,
|
2023-06-29 11:28:55 +08:00
|
|
|
response_wait_timeout_secs: u32,
|
2022-02-22 14:17:50 +08:00
|
|
|
) -> ResultType<Box<CliprdrClientContext>> {
|
2022-02-14 17:34:09 +08:00
|
|
|
Ok(CliprdrClientContext::create(
|
|
|
|
enable_files,
|
|
|
|
enable_others,
|
2023-06-29 11:28:55 +08:00
|
|
|
response_wait_timeout_secs,
|
|
|
|
Some(notify_callback),
|
2022-02-14 17:34:09 +08:00
|
|
|
Some(client_format_list),
|
|
|
|
Some(client_format_list_response),
|
|
|
|
Some(client_format_data_request),
|
|
|
|
Some(client_format_data_response),
|
|
|
|
Some(client_file_contents_request),
|
|
|
|
Some(client_file_contents_response),
|
|
|
|
)?)
|
|
|
|
}
|
|
|
|
|
2023-06-29 18:12:34 +08:00
|
|
|
extern "C" fn notify_callback(conn_id: UINT32, msg: *const NOTIFICATION_MESSAGE) -> UINT {
|
2023-06-29 11:28:55 +08:00
|
|
|
log::debug!("notify_callback called");
|
2023-06-29 18:12:34 +08:00
|
|
|
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!(
|
2023-06-29 22:25:01 +08:00
|
|
|
"custom-{}-nocancel-nook-hasclose",
|
2023-06-29 18:12:34 +08:00
|
|
|
if msg.r#type == 0 {
|
|
|
|
"info"
|
|
|
|
} else if msg.r#type == 1 {
|
|
|
|
"warn"
|
|
|
|
} else {
|
|
|
|
"error"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
let title = "Clipboard";
|
2023-06-29 20:41:33 +08:00
|
|
|
let text = if d.is_empty() {
|
|
|
|
m.to_string()
|
|
|
|
} else {
|
|
|
|
format!("{} {}", m, d)
|
|
|
|
};
|
2023-06-29 18:12:34 +08:00
|
|
|
ClipboardFile::NotifyCallback {
|
|
|
|
r#type: msgtype,
|
|
|
|
title: title.to_string(),
|
|
|
|
text,
|
2023-06-29 13:47:55 +08:00
|
|
|
}
|
|
|
|
}
|
2023-06-29 18:12:34 +08:00
|
|
|
_ => {
|
|
|
|
log::error!("notify_callback: failed to convert msg");
|
|
|
|
return ERR_CODE_INVALID_PARAMETER;
|
|
|
|
}
|
2023-06-29 13:47:55 +08:00
|
|
|
}
|
2023-06-29 18:12:34 +08:00
|
|
|
};
|
|
|
|
// no need to handle result here
|
|
|
|
send_data(conn_id as _, data);
|
|
|
|
|
|
|
|
0
|
2023-06-29 11:28:55 +08:00
|
|
|
}
|
|
|
|
|
2022-02-14 17:34:09 +08:00
|
|
|
extern "C" fn client_format_list(
|
|
|
|
_context: *mut CliprdrClientContext,
|
2022-02-22 14:17:50 +08:00
|
|
|
clip_format_list: *const CLIPRDR_FORMAT_LIST,
|
2022-02-14 17:34:09 +08:00
|
|
|
) -> UINT {
|
2022-10-26 19:42:14 +08:00
|
|
|
let conn_id;
|
2022-02-22 14:17:50 +08:00
|
|
|
let mut format_list: Vec<(i32, String)> = Vec::new();
|
2022-02-14 17:34:09 +08:00
|
|
|
unsafe {
|
|
|
|
let mut i = 0u32;
|
2022-02-22 14:17:50 +08:00
|
|
|
while i < (*clip_format_list).numFormats {
|
|
|
|
let format_data = &(*(*clip_format_list).formats.offset(i as isize));
|
2022-02-14 17:34:09 +08:00
|
|
|
if format_data.formatName.is_null() {
|
2022-02-22 14:17:50 +08:00
|
|
|
format_list.push((format_data.formatId as i32, "".to_owned()));
|
2022-02-14 17:34:09 +08:00
|
|
|
} else {
|
|
|
|
let format_name = CStr::from_ptr(format_data.formatName).to_str();
|
|
|
|
let format_name = match format_name {
|
|
|
|
Ok(n) => n.to_owned(),
|
|
|
|
Err(_) => {
|
|
|
|
log::warn!("failed to get format name");
|
|
|
|
"".to_owned()
|
|
|
|
}
|
|
|
|
};
|
2022-02-22 14:17:50 +08:00
|
|
|
format_list.push((format_data.formatId as i32, format_name));
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2022-02-22 14:17:50 +08:00
|
|
|
// log::debug!("format list item {}: format id: {}, format name: {}", i, format_data.formatId, &format_name);
|
2022-02-14 17:34:09 +08:00
|
|
|
i += 1;
|
|
|
|
}
|
2022-10-26 19:42:14 +08:00
|
|
|
conn_id = (*clip_format_list).connID as i32;
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"client_format_list called, client id: {}, format_list: {:?}",
|
|
|
|
conn_id,
|
|
|
|
&format_list
|
|
|
|
);
|
2023-01-09 15:30:21 +08:00
|
|
|
let data = ClipboardFile::FormatList { format_list };
|
2022-02-14 17:34:09 +08:00
|
|
|
// no need to handle result here
|
2022-10-26 19:42:14 +08:00
|
|
|
if conn_id == 0 {
|
2023-06-19 20:49:04 +08:00
|
|
|
// msg_channel is used for debug, VEC_MSG_CHANNEL cannot be inspected by the debugger.
|
|
|
|
let msg_channel = VEC_MSG_CHANNEL.read().unwrap();
|
|
|
|
msg_channel
|
2022-10-26 19:42:14 +08:00
|
|
|
.iter()
|
|
|
|
.for_each(|msg_channel| allow_err!(msg_channel.sender.send(data.clone())));
|
|
|
|
} else {
|
|
|
|
send_data(conn_id, data);
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn client_format_list_response(
|
|
|
|
_context: *mut CliprdrClientContext,
|
|
|
|
format_list_response: *const CLIPRDR_FORMAT_LIST_RESPONSE,
|
|
|
|
) -> UINT {
|
2022-02-24 11:22:19 +08:00
|
|
|
let conn_id;
|
2022-02-22 14:17:50 +08:00
|
|
|
let msg_flags;
|
2022-02-14 17:34:09 +08:00
|
|
|
unsafe {
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id = (*format_list_response).connID as i32;
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags = (*format_list_response).msgFlags as i32;
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"client_format_list_response called, client id: {}, msg_flags: {}",
|
|
|
|
conn_id,
|
|
|
|
msg_flags
|
|
|
|
);
|
2023-01-09 15:30:21 +08:00
|
|
|
let data = ClipboardFile::FormatListResponse { msg_flags };
|
2022-10-25 18:46:38 +08:00
|
|
|
send_data(conn_id, data);
|
2022-02-14 17:34:09 +08:00
|
|
|
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn client_format_data_request(
|
|
|
|
_context: *mut CliprdrClientContext,
|
|
|
|
format_data_request: *const CLIPRDR_FORMAT_DATA_REQUEST,
|
|
|
|
) -> UINT {
|
2022-02-24 11:22:19 +08:00
|
|
|
let conn_id;
|
2022-02-22 14:17:50 +08:00
|
|
|
let requested_format_id;
|
2022-02-14 17:34:09 +08:00
|
|
|
unsafe {
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id = (*format_data_request).connID as i32;
|
2022-02-22 14:17:50 +08:00
|
|
|
requested_format_id = (*format_data_request).requestedFormatId as i32;
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
let data = ClipboardFile::FormatDataRequest {
|
2022-02-22 14:17:50 +08:00
|
|
|
requested_format_id,
|
2022-02-14 17:34:09 +08:00
|
|
|
};
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"client_format_data_request called, conn_id: {}, requested_format_id: {}",
|
|
|
|
conn_id,
|
|
|
|
requested_format_id
|
|
|
|
);
|
2022-02-14 17:34:09 +08:00
|
|
|
// no need to handle result here
|
2022-10-25 18:46:38 +08:00
|
|
|
send_data(conn_id, data);
|
2022-02-14 17:34:09 +08:00
|
|
|
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn client_format_data_response(
|
|
|
|
_context: *mut CliprdrClientContext,
|
|
|
|
format_data_response: *const CLIPRDR_FORMAT_DATA_RESPONSE,
|
|
|
|
) -> UINT {
|
2022-02-24 11:22:19 +08:00
|
|
|
let conn_id;
|
2022-02-22 14:17:50 +08:00
|
|
|
let msg_flags;
|
|
|
|
let format_data;
|
2022-02-14 17:34:09 +08:00
|
|
|
unsafe {
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id = (*format_data_response).connID as i32;
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags = (*format_data_response).msgFlags as i32;
|
2022-02-24 02:32:29 +08:00
|
|
|
if (*format_data_response).requestedFormatData.is_null() {
|
|
|
|
format_data = Vec::new();
|
|
|
|
} else {
|
|
|
|
format_data = std::slice::from_raw_parts(
|
|
|
|
(*format_data_response).requestedFormatData,
|
|
|
|
(*format_data_response).dataLen as usize,
|
|
|
|
)
|
|
|
|
.to_vec();
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"client_format_data_response called, client id: {}, msg_flags: {}",
|
|
|
|
conn_id,
|
|
|
|
msg_flags
|
|
|
|
);
|
2023-01-09 15:30:21 +08:00
|
|
|
let data = ClipboardFile::FormatDataResponse {
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags,
|
|
|
|
format_data,
|
2022-02-14 17:34:09 +08:00
|
|
|
};
|
2022-10-25 18:46:38 +08:00
|
|
|
send_data(conn_id, data);
|
2022-02-14 17:34:09 +08:00
|
|
|
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn client_file_contents_request(
|
|
|
|
_context: *mut CliprdrClientContext,
|
|
|
|
file_contents_request: *const CLIPRDR_FILE_CONTENTS_REQUEST,
|
|
|
|
) -> UINT {
|
|
|
|
// TODO: support huge file?
|
|
|
|
// if (!cliprdr->hasHugeFileSupport)
|
|
|
|
// {
|
|
|
|
// if (((UINT64)fileContentsRequest->cbRequested + fileContentsRequest->nPositionLow) >
|
|
|
|
// UINT32_MAX)
|
|
|
|
// return ERROR_INVALID_PARAMETER;
|
|
|
|
// if (fileContentsRequest->nPositionHigh != 0)
|
|
|
|
// return ERROR_INVALID_PARAMETER;
|
|
|
|
// }
|
|
|
|
|
2022-02-24 11:22:19 +08:00
|
|
|
let conn_id;
|
2022-02-22 14:17:50 +08:00
|
|
|
let stream_id;
|
|
|
|
let list_index;
|
|
|
|
let dw_flags;
|
|
|
|
let n_position_low;
|
|
|
|
let n_position_high;
|
|
|
|
let cb_requested;
|
|
|
|
let have_clip_data_id;
|
|
|
|
let clip_data_id;
|
2022-02-14 17:34:09 +08:00
|
|
|
unsafe {
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id = (*file_contents_request).connID as i32;
|
2022-02-22 14:17:50 +08:00
|
|
|
stream_id = (*file_contents_request).streamId as i32;
|
|
|
|
list_index = (*file_contents_request).listIndex as i32;
|
|
|
|
dw_flags = (*file_contents_request).dwFlags as i32;
|
|
|
|
n_position_low = (*file_contents_request).nPositionLow as i32;
|
|
|
|
n_position_high = (*file_contents_request).nPositionHigh as i32;
|
|
|
|
cb_requested = (*file_contents_request).cbRequested as i32;
|
|
|
|
have_clip_data_id = (*file_contents_request).haveClipDataId == TRUE;
|
|
|
|
clip_data_id = (*file_contents_request).clipDataId as i32;
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
let data = ClipboardFile::FileContentsRequest {
|
2022-02-22 14:17:50 +08:00
|
|
|
stream_id,
|
|
|
|
list_index,
|
|
|
|
dw_flags,
|
|
|
|
n_position_low,
|
|
|
|
n_position_high,
|
|
|
|
cb_requested,
|
|
|
|
have_clip_data_id,
|
|
|
|
clip_data_id,
|
|
|
|
};
|
2023-07-30 17:16:25 +08:00
|
|
|
log::debug!("client_file_contents_request called, data: {:?}", &data);
|
2022-10-25 18:46:38 +08:00
|
|
|
send_data(conn_id, data);
|
2022-02-14 17:34:09 +08:00
|
|
|
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn client_file_contents_response(
|
|
|
|
_context: *mut CliprdrClientContext,
|
|
|
|
file_contents_response: *const CLIPRDR_FILE_CONTENTS_RESPONSE,
|
|
|
|
) -> UINT {
|
2022-02-24 11:22:19 +08:00
|
|
|
let conn_id;
|
2022-02-22 14:17:50 +08:00
|
|
|
let msg_flags;
|
|
|
|
let stream_id;
|
|
|
|
let requested_data;
|
2022-02-14 17:34:09 +08:00
|
|
|
unsafe {
|
2022-02-24 11:22:19 +08:00
|
|
|
conn_id = (*file_contents_response).connID as i32;
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags = (*file_contents_response).msgFlags as i32;
|
|
|
|
stream_id = (*file_contents_response).streamId as i32;
|
2022-02-24 02:32:29 +08:00
|
|
|
if (*file_contents_response).requestedData.is_null() {
|
|
|
|
requested_data = Vec::new();
|
|
|
|
} else {
|
|
|
|
requested_data = std::slice::from_raw_parts(
|
|
|
|
(*file_contents_response).requestedData,
|
|
|
|
(*file_contents_response).cbRequested as usize,
|
|
|
|
)
|
|
|
|
.to_vec();
|
|
|
|
}
|
2022-02-14 17:34:09 +08:00
|
|
|
}
|
2023-01-09 15:30:21 +08:00
|
|
|
let data = ClipboardFile::FileContentsResponse {
|
2022-02-22 14:17:50 +08:00
|
|
|
msg_flags,
|
|
|
|
stream_id,
|
|
|
|
requested_data,
|
|
|
|
};
|
2023-07-31 22:31:26 +08:00
|
|
|
log::debug!(
|
|
|
|
"client_file_contents_response called, conn_id: {}, msg_flags: {}, stream_id: {}",
|
|
|
|
conn_id,
|
|
|
|
msg_flags,
|
|
|
|
stream_id
|
|
|
|
);
|
2022-10-25 18:46:38 +08:00
|
|
|
send_data(conn_id, data);
|
2022-02-14 17:34:09 +08:00
|
|
|
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
// #[test]
|
|
|
|
// fn test_cliprdr_run() {
|
|
|
|
// super::cliprdr_run();
|
|
|
|
// }
|
|
|
|
}
|