fix, RwLock, remove nested read calls

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-09-17 13:01:27 +08:00
parent a1e1f5aab6
commit ae3efa1151
4 changed files with 69 additions and 87 deletions

View File

@ -1042,24 +1042,6 @@ pub async fn get_key(sync: bool) -> String {
key key
} }
pub fn is_peer_version_ge(v: &str) -> bool {
#[cfg(not(any(feature = "flutter", feature = "cli")))]
if let Some(session) = crate::ui::CUR_SESSION.lock().unwrap().as_ref() {
return session.get_peer_version() >= hbb_common::get_version_number(v);
}
#[cfg(feature = "flutter")]
if let Some(session) = crate::flutter::SESSIONS
.read()
.unwrap()
.get(&*crate::flutter::CUR_SESSION_ID.read().unwrap())
{
return session.get_peer_version() >= hbb_common::get_version_number(v);
}
false
}
pub fn pk_to_fingerprint(pk: Vec<u8>) -> String { pub fn pk_to_fingerprint(pk: Vec<u8>) -> String {
let s: String = pk.iter().map(|u| format!("{:02x}", u)).collect(); let s: String = pk.iter().map(|u| format!("{:02x}", u)).collect();
s.chars() s.chars()

View File

@ -1,5 +1,3 @@
use hbb_common::config::Config;
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
use crate::common::get_default_sound_input; use crate::common::get_default_sound_input;
use crate::{ use crate::{
@ -293,12 +291,12 @@ pub fn session_get_keyboard_mode(session_id: SessionID) -> Option<String> {
pub fn session_set_keyboard_mode(session_id: SessionID, value: String) { pub fn session_set_keyboard_mode(session_id: SessionID, value: String) {
let mut _mode_updated = false; let mut _mode_updated = false;
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) { if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) {
session.save_keyboard_mode(value); session.save_keyboard_mode(value.clone());
_mode_updated = true; _mode_updated = true;
} }
#[cfg(windows)] #[cfg(windows)]
if _mode_updated { if _mode_updated {
crate::keyboard::update_grab_get_key_name(); crate::keyboard::update_grab_get_key_name(&value);
} }
} }
@ -378,7 +376,9 @@ pub fn session_handle_flutter_key_event(
down_or_up: bool, down_or_up: bool,
) { ) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
let keyboard_mode = session.get_keyboard_mode();
session.handle_flutter_key_event( session.handle_flutter_key_event(
&keyboard_mode,
&name, &name,
platform_code, platform_code,
position_code, position_code,
@ -397,11 +397,12 @@ pub fn session_handle_flutter_key_event(
pub fn session_enter_or_leave(_session_id: SessionID, _enter: bool) -> SyncReturn<()> { pub fn session_enter_or_leave(_session_id: SessionID, _enter: bool) -> SyncReturn<()> {
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
if let Some(session) = SESSIONS.read().unwrap().get(&_session_id) { if let Some(session) = SESSIONS.read().unwrap().get(&_session_id) {
let keyboard_mode = session.get_keyboard_mode();
if _enter { if _enter {
set_cur_session_id(_session_id); set_cur_session_id_(_session_id, &keyboard_mode);
session.enter(); session.enter(&keyboard_mode);
} else { } else {
session.leave(); session.leave(&keyboard_mode);
} }
} }
SyncReturn(()) SyncReturn(())
@ -1509,9 +1510,15 @@ pub fn main_update_me() -> SyncReturn<bool> {
} }
pub fn set_cur_session_id(session_id: SessionID) { pub fn set_cur_session_id(session_id: SessionID) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
set_cur_session_id_(session_id, &session.get_keyboard_mode())
}
}
fn set_cur_session_id_(session_id: SessionID, keyboard_mode: &str) {
super::flutter::set_cur_session_id(session_id); super::flutter::set_cur_session_id(session_id);
#[cfg(windows)] #[cfg(windows)]
crate::keyboard::update_grab_get_key_name(); crate::keyboard::update_grab_get_key_name(keyboard_mode);
} }
pub fn install_show_run_without_install() -> SyncReturn<bool> { pub fn install_show_run_without_install() -> SyncReturn<bool> {

View File

@ -56,22 +56,6 @@ pub mod client {
static ref IS_GRAB_STARTED: Arc<Mutex<bool>> = Arc::new(Mutex::new(false)); static ref IS_GRAB_STARTED: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
} }
pub fn get_keyboard_mode() -> String {
#[cfg(not(any(feature = "flutter", feature = "cli")))]
if let Some(session) = CUR_SESSION.lock().unwrap().as_ref() {
return session.get_keyboard_mode();
}
#[cfg(feature = "flutter")]
if let Some(session) = SESSIONS
.read()
.unwrap()
.get(&*CUR_SESSION_ID.read().unwrap())
{
return session.get_keyboard_mode();
}
"legacy".to_string()
}
pub fn start_grab_loop() { pub fn start_grab_loop() {
let mut lock = IS_GRAB_STARTED.lock().unwrap(); let mut lock = IS_GRAB_STARTED.lock().unwrap();
if *lock { if *lock {
@ -82,12 +66,12 @@ pub mod client {
} }
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn change_grab_status(state: GrabState) { pub fn change_grab_status(state: GrabState, keyboard_mode: &str) {
match state { match state {
GrabState::Ready => {} GrabState::Ready => {}
GrabState::Run => { GrabState::Run => {
#[cfg(windows)] #[cfg(windows)]
update_grab_get_key_name(); update_grab_get_key_name(keyboard_mode);
#[cfg(any(target_os = "windows", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "macos"))]
KEYBOARD_HOOKED.swap(true, Ordering::SeqCst); KEYBOARD_HOOKED.swap(true, Ordering::SeqCst);
@ -95,7 +79,10 @@ pub mod client {
rdev::enable_grab(); rdev::enable_grab();
} }
GrabState::Wait => { GrabState::Wait => {
release_remote_keys(); #[cfg(windows)]
rdev::set_get_key_unicode(false);
release_remote_keys(keyboard_mode);
#[cfg(any(target_os = "windows", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "macos"))]
KEYBOARD_HOOKED.swap(false, Ordering::SeqCst); KEYBOARD_HOOKED.swap(false, Ordering::SeqCst);
@ -110,17 +97,16 @@ pub mod client {
} }
} }
pub fn process_event(event: &Event, lock_modes: Option<i32>) -> KeyboardMode { pub fn process_event(keyboard_mode: &str, event: &Event, lock_modes: Option<i32>) {
let keyboard_mode = get_keyboard_mode_enum(); let keyboard_mode = get_keyboard_mode_enum(keyboard_mode);
if is_long_press(&event) { if is_long_press(&event) {
return keyboard_mode; return;
} }
for key_event in event_to_key_events(&event, keyboard_mode, lock_modes) { for key_event in event_to_key_events(&event, keyboard_mode, lock_modes) {
send_key_event(&key_event); send_key_event(&key_event);
} }
keyboard_mode
} }
pub fn get_modifiers_state( pub fn get_modifiers_state(
@ -215,10 +201,11 @@ pub mod client {
} }
#[cfg(windows)] #[cfg(windows)]
pub fn update_grab_get_key_name() { pub fn update_grab_get_key_name(keyboard_mode: &str) {
match get_keyboard_mode_enum() { match keyboard_mode {
KeyboardMode::Map => rdev::set_get_key_unicode(false), "map" => rdev::set_get_key_unicode(false),
KeyboardMode::Translate => rdev::set_get_key_unicode(true), "translate" => rdev::set_get_key_unicode(true),
"legacy" => rdev::set_get_key_unicode(true),
_ => {} _ => {}
}; };
} }
@ -229,6 +216,22 @@ static mut IS_0X021D_DOWN: bool = false;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
static mut IS_LEFT_OPTION_DOWN: bool = false; static mut IS_LEFT_OPTION_DOWN: bool = false;
fn get_keyboard_mode() -> String {
#[cfg(not(any(feature = "flutter", feature = "cli")))]
if let Some(session) = CUR_SESSION.lock().unwrap().as_ref() {
return session.get_keyboard_mode();
}
#[cfg(feature = "flutter")]
if let Some(session) = SESSIONS
.read()
.unwrap()
.get(&*CUR_SESSION_ID.read().unwrap())
{
return session.get_keyboard_mode();
}
"legacy".to_string()
}
pub fn start_grab_loop() { pub fn start_grab_loop() {
std::env::set_var("KEYBOARD_ONLY", "y"); std::env::set_var("KEYBOARD_ONLY", "y");
#[cfg(any(target_os = "windows", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "macos"))]
@ -239,11 +242,10 @@ pub fn start_grab_loop() {
return Some(event); return Some(event);
} }
let mut _keyboard_mode = KeyboardMode::Map;
let _scan_code = event.position_code; let _scan_code = event.position_code;
let _code = event.platform_code as KeyCode; let _code = event.platform_code as KeyCode;
let res = if KEYBOARD_HOOKED.load(Ordering::SeqCst) { let res = if KEYBOARD_HOOKED.load(Ordering::SeqCst) {
_keyboard_mode = client::process_event(&event, None); client::process_event(&get_keyboard_mode(), &event, None);
if is_press { if is_press {
None None
} else { } else {
@ -304,7 +306,7 @@ pub fn start_grab_loop() {
if let Key::Unknown(keycode) = key { if let Key::Unknown(keycode) = key {
log::error!("rdev get unknown key, keycode is : {:?}", keycode); log::error!("rdev get unknown key, keycode is : {:?}", keycode);
} else { } else {
client::process_event(&event, None); client::process_event(&get_keyboard_mode(), &event, None);
} }
None None
} }
@ -330,7 +332,7 @@ pub fn is_long_press(event: &Event) -> bool {
} }
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn release_remote_keys() { pub fn release_remote_keys(keyboard_mode: &str) {
// todo!: client quit suddenly, how to release keys? // todo!: client quit suddenly, how to release keys?
let to_release = TO_RELEASE.lock().unwrap().clone(); let to_release = TO_RELEASE.lock().unwrap().clone();
TO_RELEASE.lock().unwrap().clear(); TO_RELEASE.lock().unwrap().clear();
@ -339,23 +341,16 @@ pub fn release_remote_keys() {
let event = event_type_to_event(event_type); let event = event_type_to_event(event_type);
// to-do: BUG // to-do: BUG
// Release events should be sent to the corresponding sessions, instead of current session. // Release events should be sent to the corresponding sessions, instead of current session.
client::process_event(&event, None); client::process_event(keyboard_mode, &event, None);
} }
} }
pub fn get_keyboard_mode_enum() -> KeyboardMode { pub fn get_keyboard_mode_enum(keyboard_mode: &str) -> KeyboardMode {
match client::get_keyboard_mode().as_str() { match keyboard_mode {
"map" => KeyboardMode::Map, "map" => KeyboardMode::Map,
"translate" => KeyboardMode::Translate, "translate" => KeyboardMode::Translate,
"legacy" => KeyboardMode::Legacy, "legacy" => KeyboardMode::Legacy,
_ => { _ => KeyboardMode::Map,
// Set "map" as default mode if version >= 1.2.0.
if crate::is_peer_version_ge("1.2.0") {
KeyboardMode::Map
} else {
KeyboardMode::Legacy
}
}
} }
} }

View File

@ -168,7 +168,17 @@ impl<T: InvokeUiSession> Session<T> {
} }
pub fn get_keyboard_mode(&self) -> String { pub fn get_keyboard_mode(&self) -> String {
self.lc.read().unwrap().keyboard_mode.clone() let mode = self.lc.read().unwrap().keyboard_mode.clone();
if ["map", "translate", "legacy"].contains(&(&mode as &str)) {
mode
} else {
if self.get_peer_version() > hbb_common::get_version_number("1.2.0") {
"map"
} else {
"legacy"
}
.to_string()
}
} }
pub fn save_keyboard_mode(&mut self, value: String) { pub fn save_keyboard_mode(&mut self, value: String) {
@ -562,28 +572,15 @@ impl<T: InvokeUiSession> Session<T> {
} }
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn enter(&self) { pub fn enter(&self, keyboard_mode: &str) {
#[cfg(target_os = "windows")]
{
match &self.lc.read().unwrap().keyboard_mode as _ {
"legacy" => rdev::set_get_key_unicode(true),
"translate" => rdev::set_get_key_unicode(true),
_ => {}
}
}
IS_IN.store(true, Ordering::SeqCst); IS_IN.store(true, Ordering::SeqCst);
keyboard::client::change_grab_status(GrabState::Run); keyboard::client::change_grab_status(GrabState::Run, keyboard_mode);
} }
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn leave(&self) { pub fn leave(&self, keyboard_mode: &str) {
#[cfg(target_os = "windows")]
{
rdev::set_get_key_unicode(false);
}
IS_IN.store(false, Ordering::SeqCst); IS_IN.store(false, Ordering::SeqCst);
keyboard::client::change_grab_status(GrabState::Wait); keyboard::client::change_grab_status(GrabState::Wait, keyboard_mode);
} }
// flutter only TODO new input // flutter only TODO new input
@ -631,6 +628,7 @@ impl<T: InvokeUiSession> Session<T> {
#[cfg(not(any(target_os = "ios")))] #[cfg(not(any(target_os = "ios")))]
pub fn handle_flutter_key_event( pub fn handle_flutter_key_event(
&self, &self,
keyboard_mode: &str,
_name: &str, _name: &str,
platform_code: i32, platform_code: i32,
position_code: i32, position_code: i32,
@ -663,7 +661,7 @@ impl<T: InvokeUiSession> Session<T> {
#[cfg(any(target_os = "windows", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "macos"))]
extra_data: 0, extra_data: 0,
}; };
keyboard::client::process_event(&event, Some(lock_modes)); keyboard::client::process_event(keyboard_mode, &event, Some(lock_modes));
} }
// flutter only TODO new input // flutter only TODO new input