more pynput startup try

This commit is contained in:
rustdesk 2022-03-08 17:59:26 +08:00
parent ffb0fa4349
commit 88ac9e9d11

View File

@ -499,38 +499,57 @@ fn start_pynput_service(rx: mpsc::Receiver<(PyMsg, bool)>) {
let status = if username.is_empty() { let status = if username.is_empty() {
std::process::Command::new("python3") std::process::Command::new("python3")
.arg(&py) .arg(&py)
.arg(IPC_FILE).status() .arg(IPC_FILE)
.status()
.map(|x| x.success())
} else { } else {
std::process::Command::new("sudo").args(vec![ let mut status = Ok(true);
for i in 0..100 {
log::info!("#{} try to start pynput server", i);
status = std::process::Command::new("sudo")
.args(vec![
&format!("XDG_RUNTIME_DIR=/run/user/{}", userid) as &str, &format!("XDG_RUNTIME_DIR=/run/user/{}", userid) as &str,
"-u", "-u",
&username, &username,
"python3", "python3",
&py, &py,
IPC_FILE, IPC_FILE,
]).status() ])
.status()
.map(|x| x.success());
match status {
Ok(true) => break,
_ => {}
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
status
}; };
log::info!( log::info!(
"pynput server exit: {:?}", status "pynput server exit with username/id {}/{}: {:?}",
username,
userid,
status
); );
unsafe { unsafe {
PYNPUT_EXIT = true; PYNPUT_EXIT = true;
} }
}); });
std::thread::spawn(move || { std::thread::spawn(move || {
// wait for pynput_server.py for _ in 0..300 {
std::thread::sleep(std::time::Duration::from_millis(1000)); std::thread::sleep(std::time::Duration::from_millis(100));
let mut conn = match std::os::unix::net::UnixStream::connect(IPC_FILE) { let mut conn = match std::os::unix::net::UnixStream::connect(IPC_FILE) {
Ok(conn) => conn, Ok(conn) => conn,
Err(err) => { Err(err) => {
log::error!("Failed to connect to {}: {}", IPC_FILE, err); log::error!("Failed to connect to {}: {}", IPC_FILE, err);
return; continue;
} }
}; };
if let Err(err) = conn.set_nonblocking(true) { if let Err(err) = conn.set_nonblocking(true) {
log::error!("Failed to set ipc nonblocking: {}", err); log::error!("Failed to set ipc nonblocking: {}", err);
return; return;
} }
log::info!("Conntected to pynput server");
let d = std::time::Duration::from_millis(30); let d = std::time::Duration::from_millis(30);
unsafe { unsafe {
PYNPUT_REDAY = true; PYNPUT_REDAY = true;
@ -543,7 +562,9 @@ fn start_pynput_service(rx: mpsc::Receiver<(PyMsg, bool)>) {
match rx.recv_timeout(d) { match rx.recv_timeout(d) {
Ok((msg, is_press)) => { Ok((msg, is_press)) => {
let msg = match msg { let msg = match msg {
PyMsg::Char(chr) => format!("{}{}", if is_press { 'p' } else { 'r' }, chr), PyMsg::Char(chr) => {
format!("{}{}", if is_press { 'p' } else { 'r' }, chr)
}
PyMsg::Str(s) => format!("{}{}", if is_press { 'p' } else { 'r' }, s), PyMsg::Str(s) => format!("{}{}", if is_press { 'p' } else { 'r' }, s),
}; };
let n = msg.len(); let n = msg.len();
@ -566,5 +587,7 @@ fn start_pynput_service(rx: mpsc::Receiver<(PyMsg, bool)>) {
unsafe { unsafe {
PYNPUT_REDAY = false; PYNPUT_REDAY = false;
} }
break;
}
}); });
} }