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() {
std::process::Command::new("python3")
.arg(&py)
.arg(IPC_FILE).status()
.arg(IPC_FILE)
.status()
.map(|x| x.success())
} 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,
"-u",
&username,
"python3",
&py,
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!(
"pynput server exit: {:?}", status
"pynput server exit with username/id {}/{}: {:?}",
username,
userid,
status
);
unsafe {
PYNPUT_EXIT = true;
}
});
std::thread::spawn(move || {
// wait for pynput_server.py
std::thread::sleep(std::time::Duration::from_millis(1000));
for _ in 0..300 {
std::thread::sleep(std::time::Duration::from_millis(100));
let mut conn = match std::os::unix::net::UnixStream::connect(IPC_FILE) {
Ok(conn) => conn,
Err(err) => {
log::error!("Failed to connect to {}: {}", IPC_FILE, err);
return;
continue;
}
};
if let Err(err) = conn.set_nonblocking(true) {
log::error!("Failed to set ipc nonblocking: {}", err);
return;
}
log::info!("Conntected to pynput server");
let d = std::time::Duration::from_millis(30);
unsafe {
PYNPUT_REDAY = true;
@ -543,7 +562,9 @@ fn start_pynput_service(rx: mpsc::Receiver<(PyMsg, bool)>) {
match rx.recv_timeout(d) {
Ok((msg, is_press)) => {
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),
};
let n = msg.len();
@ -566,5 +587,7 @@ fn start_pynput_service(rx: mpsc::Receiver<(PyMsg, bool)>) {
unsafe {
PYNPUT_REDAY = false;
}
break;
}
});
}