Skip to content

Commit

Permalink
chore: base remote
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Aug 26, 2023
1 parent 168c67a commit 6a5122b
Show file tree
Hide file tree
Showing 7 changed files with 629 additions and 14 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,24 @@ libwayshot = { git = "https://github.com/waycrate/wayshot.git" }
libspa_sys = { package = "libspa-sys", git = "https://gitlab.freedesktop.org/pipewire/pipewire-rs" }
rustix = { version = "0.38.8", features = ["fs", "use-libc"] }

# REMOTE
wayland-protocols = { version = "0.30.0", default-features = false, features = [
"unstable",
"client",
] }
#wayland-protocols = { version = "=0.30.0-beta.13", features = ["client", "unstable"] }


wayland-protocols-wlr = { version = "0.1.0", default-features = false, features = [
"client",
] }
wayland-client = { version = "0.30.2" }

wayland-protocols-misc = { version = "0.1.0", features = ["client"] }
xkbcommon = "0.5.0"
tempfile = "3.5.0"
thiserror = "1.0.47"


[build-dependencies]
slint-build = "1.1.0"
192 changes: 182 additions & 10 deletions src/remote.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
mod dispatch;
mod remote_thread;
mod state;

use remote_thread::RemoteControl;

use std::collections::HashMap;

use enumflags2::BitFlags;
Expand All @@ -18,6 +24,8 @@ use crate::PortalResponse;

use crate::screencast::SelectSourcesOptions;

use self::remote_thread::KeyOrPointerRequest;

#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
/// Specified options for a [`Screencast::create_session`] request.
#[zvariant(signature = "dict")]
Expand Down Expand Up @@ -48,24 +56,25 @@ struct RemoteStartReturnValue {
clipboard_enabled: bool,
}

pub type RemoteSessionData = (String, ScreencastThread);
pub static CAST_SESSIONS: Lazy<Arc<Mutex<Vec<RemoteSessionData>>>> =
pub type RemoteSessionData = (String, ScreencastThread, RemoteControl);
pub static REMOTE_SESSIONS: Lazy<Arc<Mutex<Vec<RemoteSessionData>>>> =
Lazy::new(|| Arc::new(Mutex::new(Vec::new())));

pub async fn append_remote_session(session: RemoteSessionData) {
let mut sessions = CAST_SESSIONS.lock().await;
let mut sessions = REMOTE_SESSIONS.lock().await;
sessions.push(session)
}

pub async fn remove_remote_session(path: &str) {
let mut sessions = CAST_SESSIONS.lock().await;
let mut sessions = REMOTE_SESSIONS.lock().await;
let Some(index) = sessions
.iter()
.position(|the_session| the_session.0 == path)
else {
return;
};
sessions[index].1.stop();
sessions[index].2.stop();
tracing::info!("session {} is stopped", sessions[index].0);
sessions.remove(index);
}
Expand All @@ -81,7 +90,7 @@ impl RemoteBackend {

#[dbus_interface(property)]
fn available_device_types(&self) -> u32 {
(DeviceTypes::KEYBOARD | DeviceTypes::POINTER).bits()
(DeviceTypes::Keyboard | DeviceTypes::Pointer).bits()
}

async fn create_session(
Expand Down Expand Up @@ -132,7 +141,7 @@ impl RemoteBackend {
return Ok(PortalResponse::Other);
}
locked_sessions[index].set_options(options);
locked_sessions[index].set_device_type(DeviceTypes::KEYBOARD | DeviceTypes::POINTER);
locked_sessions[index].set_device_type(DeviceTypes::Keyboard | DeviceTypes::Pointer);
Ok(PortalResponse::Success(HashMap::new()))
}

Expand All @@ -144,8 +153,8 @@ impl RemoteBackend {
_parent_window: String,
_options: HashMap<String, Value<'_>>,
) -> zbus::fdo::Result<PortalResponse<RemoteStartReturnValue>> {
let cast_sessions = CAST_SESSIONS.lock().await;
if let Some(session) = cast_sessions
let remote_sessions = REMOTE_SESSIONS.lock().await;
if let Some(session) = remote_sessions
.iter()
.find(|session| session.0 == session_handle.to_string())
{
Expand All @@ -154,7 +163,7 @@ impl RemoteBackend {
..Default::default()
}));
}
drop(cast_sessions);
drop(remote_sessions);

let locked_sessions = SESSIONS.lock().await;
let Some(index) = locked_sessions
Expand Down Expand Up @@ -211,13 +220,176 @@ impl RemoteBackend {
.await
.map_err(|e| zbus::Error::Failure(format!("cannot start pipewire stream, error: {e}")))?;

let remote_control = RemoteControl::init();
let node_id = cast_thread.node_id();

append_remote_session((session_handle.to_string(), cast_thread)).await;
append_remote_session((session_handle.to_string(), cast_thread, remote_control)).await;

Ok(PortalResponse::Success(RemoteStartReturnValue {
streams: vec![Stream(node_id, StreamProperties::default())],
..Default::default()
}))
}

// keyboard and else

async fn notify_pointer_motion(
&self,
session_handle: ObjectPath<'_>,
_options: HashMap<String, Value<'_>>,
dx: f64,
dy: f64,
) -> zbus::fdo::Result<()> {
let remote_sessions = REMOTE_SESSIONS.lock().await;
let Some(session) = remote_sessions
.iter()
.find(|session| session.0 == session_handle.to_string())
else {
return Ok(());
};
let remote_control = &session.2;
remote_control
.sender
.send(KeyOrPointerRequest::PointerMotion { dx, dy })
.map_err(|_| zbus::Error::Failure("Send failed".to_string()))?;
Ok(())
}

async fn notify_pointer_motion_absolute(
&self,
session_handle: ObjectPath<'_>,
_options: HashMap<String, Value<'_>>,
_steam: u32,
x: f64,
y: f64,
) -> zbus::fdo::Result<()> {
let remote_sessions = REMOTE_SESSIONS.lock().await;
let Some(session) = remote_sessions
.iter()
.find(|session| session.0 == session_handle.to_string())
else {
return Ok(());
};
let remote_control = &session.2;
remote_control
.sender
.send(KeyOrPointerRequest::PointerMotionAbsolute {
x,
y,
x_extent: 2000,
y_extent: 2000,
})
.map_err(|_| zbus::Error::Failure("Send failed".to_string()))?;
Ok(())
}

async fn notify_pointer_button(
&self,
session_handle: ObjectPath<'_>,
_options: HashMap<String, Value<'_>>,
button: i32,
state: u32,
) -> zbus::fdo::Result<()> {
let remote_sessions = REMOTE_SESSIONS.lock().await;
let Some(session) = remote_sessions
.iter()
.find(|session| session.0 == session_handle.to_string())
else {
return Ok(());
};
let remote_control = &session.2;
remote_control
.sender
.send(KeyOrPointerRequest::PointerButton { button, state })
.map_err(|_| zbus::Error::Failure("Send failed".to_string()))?;
Ok(())
}

async fn notify_pointer_axis(
&self,
session_handle: ObjectPath<'_>,
_options: HashMap<String, Value<'_>>,
dx: f64,
dy: f64,
) -> zbus::fdo::Result<()> {
let remote_sessions = REMOTE_SESSIONS.lock().await;
let Some(session) = remote_sessions
.iter()
.find(|session| session.0 == session_handle.to_string())
else {
return Ok(());
};
let remote_control = &session.2;
remote_control
.sender
.send(KeyOrPointerRequest::PointerAxis { dx, dy })
.map_err(|_| zbus::Error::Failure("Send failed".to_string()))?;
Ok(())
}

async fn notify_pointer_axix_discrate(
&self,
session_handle: ObjectPath<'_>,
_options: HashMap<String, Value<'_>>,
axis: u32,
steps: i32,
) -> zbus::fdo::Result<()> {
let remote_sessions = REMOTE_SESSIONS.lock().await;
let Some(session) = remote_sessions
.iter()
.find(|session| session.0 == session_handle.to_string())
else {
return Ok(());
};
let remote_control = &session.2;
remote_control
.sender
.send(KeyOrPointerRequest::PointerAxisDiscrate { axis, steps })
.map_err(|_| zbus::Error::Failure("Send failed".to_string()))?;
Ok(())
}

async fn notify_keyboard_keycode(
&self,
session_handle: ObjectPath<'_>,
_options: HashMap<String, Value<'_>>,
keycode: i32,
state: u32,
) -> zbus::fdo::Result<()> {
let remote_sessions = REMOTE_SESSIONS.lock().await;
let Some(session) = remote_sessions
.iter()
.find(|session| session.0 == session_handle.to_string())
else {
return Ok(());
};
let remote_control = &session.2;
remote_control
.sender
.send(KeyOrPointerRequest::KeyboardKeycode { keycode, state })
.map_err(|_| zbus::Error::Failure("Send failed".to_string()))?;
Ok(())
}

async fn notify_keyboard_keysym(
&self,
session_handle: ObjectPath<'_>,
_options: HashMap<String, Value<'_>>,
keysym: i32,
state: u32,
) -> zbus::fdo::Result<()> {
let remote_sessions = REMOTE_SESSIONS.lock().await;
let Some(session) = remote_sessions
.iter()
.find(|session| session.0 == session_handle.to_string())
else {
return Ok(());
};
let remote_control = &session.2;
remote_control
.sender
.send(KeyOrPointerRequest::KeyboardKeysym { keysym, state })
.map_err(|_| zbus::Error::Failure("Send failed".to_string()))?;
Ok(())
}
}
Loading

0 comments on commit 6a5122b

Please sign in to comment.