Wayland: Skip remote desktop portal calls when uinput is available (#6758)

* skip rdp when uinput is available

Signed-off-by: Sahil Yeole <sahilyeole93@gmail.com>

* fix is_server_running

* remove clones

Signed-off-by: Sahil Yeole <sahilyeole93@gmail.com>

---------

Signed-off-by: Sahil Yeole <sahilyeole93@gmail.com>
This commit is contained in:
Sahil Yeole 2023-12-28 08:10:58 +05:30 committed by GitHub
parent 4b581c385c
commit 20b4ce3213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
use std::process::Command;
use std::sync::{atomic::AtomicBool, Arc, Mutex}; use std::sync::{atomic::AtomicBool, Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use tracing::{debug, trace, warn}; use tracing::{debug, trace, warn};
@ -509,7 +510,12 @@ pub fn request_remote_desktop() -> Result<
// between the caller subscribing to the signal after receiving the reply for the method call and the signal getting emitted, // between the caller subscribing to the signal after receiving the reply for the method call and the signal getting emitted,
// a convention for Request object paths has been established that allows // a convention for Request object paths has been established that allows
// the caller to subscribe to the signal before making the method call. // the caller to subscribe to the signal before making the method call.
let path = remote_desktop_portal::create_session(&portal, args)?; let path;
if is_server_running() {
path = screencast_portal::create_session(&portal, args)?;
} else {
path = remote_desktop_portal::create_session(&portal, args)?;
}
handle_response( handle_response(
&conn, &conn,
path, path,
@ -561,15 +567,6 @@ fn on_create_session_response(
&dbus::Message, &dbus::Message,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
move |r: OrgFreedesktopPortalRequestResponse, c, _| { move |r: OrgFreedesktopPortalRequestResponse, c, _| {
let portal = get_portal(c);
let mut args: PropMap = HashMap::new();
args.insert(
"handle_token".to_string(),
Variant(Box::new("u2".to_string())),
);
args.insert("types".to_string(), Variant(Box::new(7u32)));
let ses: dbus::Path = r let ses: dbus::Path = r
.results .results
.get("session_handle") .get("session_handle")
@ -586,22 +583,53 @@ fn on_create_session_response(
let mut session = match session.lock() { let mut session = match session.lock() {
Ok(session) => session, Ok(session) => session,
Err(_) => { Err(_) => return Err(Box::new(DBusError("Failed to lock session.".into()))),
return Err(Box::new(DBusError(
"Failed to lock session.".into(),
)))
}
}; };
session.replace(ses.clone()); session.replace(ses.clone());
let path = portal.select_devices(ses.clone(), args)?; let portal = get_portal(c);
handle_response( let mut args: PropMap = HashMap::new();
c, if is_server_running() {
path, if let Ok(version) = screencast_portal::version(&portal) {
on_select_devices_response(fd.clone(), streams.clone(), failure.clone(), ses), if version >= 4 {
failure.clone(), let restore_token = config::LocalConfig::get_option(RESTORE_TOKEN_CONF_KEY);
)?; if !restore_token.is_empty() {
args.insert(RESTORE_TOKEN.to_string(), Variant(Box::new(restore_token)));
}
// persist_mode may be configured by the user.
args.insert("persist_mode".to_string(), Variant(Box::new(2u32)));
}
}
args.insert(
"handle_token".to_string(),
Variant(Box::new("u3".to_string())),
);
// https://flatpak.github.io/xdg-desktop-portal/portal-docs.html#gdbus-method-org-freedesktop-portal-ScreenCast.SelectSources
args.insert("multiple".into(), Variant(Box::new(true)));
args.insert("types".into(), Variant(Box::new(1u32))); //| 2u32)));
let path = portal.select_sources(ses.clone(), args)?;
handle_response(
c,
path,
on_select_sources_response(fd.clone(), streams.clone(), failure.clone(), ses),
failure.clone(),
)?;
} else {
args.insert(
"handle_token".to_string(),
Variant(Box::new("u2".to_string())),
);
args.insert("types".to_string(), Variant(Box::new(7u32)));
let path = portal.select_devices(ses.clone(), args)?;
handle_response(
c,
path,
on_select_devices_response(fd.clone(), streams.clone(), failure.clone(), ses),
failure.clone(),
)?;
}
Ok(()) Ok(())
} }
@ -620,16 +648,6 @@ fn on_select_devices_response(
move |_: OrgFreedesktopPortalRequestResponse, c, _| { move |_: OrgFreedesktopPortalRequestResponse, c, _| {
let portal = get_portal(c); let portal = get_portal(c);
let mut args: PropMap = HashMap::new(); let mut args: PropMap = HashMap::new();
if let Ok(version) = remote_desktop_portal::version(&portal) {
if version >= 4 {
let restore_token = config::LocalConfig::get_option(RESTORE_TOKEN_CONF_KEY);
if !restore_token.is_empty() {
args.insert(RESTORE_TOKEN.to_string(), Variant(Box::new(restore_token)));
}
// persist_mode may be configured by the user.
args.insert("persist_mode".to_string(), Variant(Box::new(2u32)));
}
}
args.insert( args.insert(
"handle_token".to_string(), "handle_token".to_string(),
Variant(Box::new("u3".to_string())), Variant(Box::new("u3".to_string())),
@ -643,12 +661,7 @@ fn on_select_devices_response(
handle_response( handle_response(
c, c,
path, path,
on_select_sources_response( on_select_sources_response(fd.clone(), streams.clone(), failure.clone(), session),
fd.clone(),
streams.clone(),
failure.clone(),
session.clone(),
),
failure.clone(), failure.clone(),
)?; )?;
@ -673,7 +686,12 @@ fn on_select_sources_response(
"handle_token".to_string(), "handle_token".to_string(),
Variant(Box::new("u4".to_string())), Variant(Box::new("u4".to_string())),
); );
let path = remote_desktop_portal::start(&portal, session.clone(), "", args)?; let path;
if is_server_running() {
path = screencast_portal::start(&portal, session.clone(), "", args)?;
} else {
path = remote_desktop_portal::start(&portal, session.clone(), "", args)?;
}
handle_response( handle_response(
c, c,
path, path,
@ -696,14 +714,16 @@ fn on_start_response(
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
move |r: OrgFreedesktopPortalRequestResponse, c, _| { move |r: OrgFreedesktopPortalRequestResponse, c, _| {
let portal = get_portal(c); let portal = get_portal(c);
if let Ok(version) = remote_desktop_portal::version(&portal) { if is_server_running() {
if version >= 4 { if let Ok(version) = screencast_portal::version(&portal) {
if let Some(restore_token) = r.results.get(RESTORE_TOKEN) { if version >= 4 {
if let Some(restore_token) = restore_token.as_str() { if let Some(restore_token) = r.results.get(RESTORE_TOKEN) {
config::LocalConfig::set_option( if let Some(restore_token) = restore_token.as_str() {
RESTORE_TOKEN_CONF_KEY.to_owned(), config::LocalConfig::set_option(
restore_token.to_owned(), RESTORE_TOKEN_CONF_KEY.to_owned(),
); restore_token.to_owned(),
);
}
} }
} }
} }
@ -756,3 +776,20 @@ pub fn get_capturables() -> Result<Vec<PipeWireCapturable>, Box<dyn Error>> {
.map(|s| PipeWireCapturable::new(rdp_res.conn.clone(), rdp_res.fd.clone(), s)) .map(|s| PipeWireCapturable::new(rdp_res.conn.clone(), rdp_res.fd.clone(), s))
.collect()) .collect())
} }
fn is_server_running() -> bool {
let output = match Command::new("sh")
.arg("-c")
.arg("ps aux | grep rustdesk")
.output()
{
Ok(output) => output,
Err(_) => {
return false;
}
};
let output_str = String::from_utf8_lossy(&output.stdout);
let is_running = output_str.contains("rustdesk --server");
is_running
}