no need for a Cell

This commit is contained in:
Ferdinand Schober
2024-11-08 14:07:34 +01:00
parent ff2a2cb1df
commit 46044d0796

View File

@@ -16,7 +16,6 @@ use crate::{connect::LanMouseConnection, service::Service};
pub(crate) struct Capture { pub(crate) struct Capture {
exit_requested: Rc<Cell<bool>>, exit_requested: Rc<Cell<bool>>,
_active: Rc<Cell<Option<CaptureHandle>>>,
request_tx: Sender<CaptureRequest>, request_tx: Sender<CaptureRequest>,
task: JoinHandle<()>, task: JoinHandle<()>,
event_rx: Receiver<ICaptureEvent>, event_rx: Receiver<ICaptureEvent>,
@@ -60,10 +59,8 @@ impl Capture {
) -> Self { ) -> Self {
let (request_tx, request_rx) = channel(); let (request_tx, request_rx) = channel();
let (event_tx, event_rx) = channel(); let (event_tx, event_rx) = channel();
let active = Rc::new(Cell::new(None));
let exit_requested = Rc::new(Cell::new(false)); let exit_requested = Rc::new(Cell::new(false));
let task = spawn_local(Self::run( let task = spawn_local(Self::run(
active.clone(),
exit_requested.clone(), exit_requested.clone(),
service, service,
backend, backend,
@@ -72,7 +69,6 @@ impl Capture {
event_tx, event_tx,
)); ));
Self { Self {
_active: active,
exit_requested, exit_requested,
request_tx, request_tx,
task, task,
@@ -120,7 +116,6 @@ impl Capture {
} }
async fn run( async fn run(
active: Rc<Cell<Option<CaptureHandle>>>,
exit_requested: Rc<Cell<bool>>, exit_requested: Rc<Cell<bool>>,
service: Service, service: Service,
backend: Option<input_capture::Backend>, backend: Option<input_capture::Backend>,
@@ -128,9 +123,10 @@ impl Capture {
mut conn: LanMouseConnection, mut conn: LanMouseConnection,
mut event_tx: Sender<ICaptureEvent>, mut event_tx: Sender<ICaptureEvent>,
) { ) {
let mut active = None;
loop { loop {
if let Err(e) = do_capture( if let Err(e) = do_capture(
&active, &mut active,
&service, &service,
backend, backend,
&mut conn, &mut conn,
@@ -156,7 +152,7 @@ impl Capture {
} }
async fn do_capture( async fn do_capture(
active: &Cell<Option<CaptureHandle>>, active: &mut Option<CaptureHandle>,
service: &Service, service: &Service,
backend: Option<input_capture::Backend>, backend: Option<input_capture::Backend>,
conn: &mut LanMouseConnection, conn: &mut LanMouseConnection,
@@ -216,7 +212,7 @@ async fn create_clients(
} }
async fn do_capture_session( async fn do_capture_session(
active: &Cell<Option<CaptureHandle>>, active: &mut Option<CaptureHandle>,
capture: &mut InputCapture, capture: &mut InputCapture,
conn: &mut LanMouseConnection, conn: &mut LanMouseConnection,
event_tx: &Sender<ICaptureEvent>, event_tx: &Sender<ICaptureEvent>,
@@ -232,8 +228,8 @@ async fn do_capture_session(
None => return Ok(()), None => return Ok(()),
}, },
(handle, event) = conn.recv() => { (handle, event) = conn.recv() => {
if let Some(active) = active.get() { if let Some(active) = active {
if handle != active { if handle != *active {
// we only care about events coming from the client we are currently connected to // we only care about events coming from the client we are currently connected to
// only `Ack` and `Leave` are relevant // only `Ack` and `Leave` are relevant
continue continue
@@ -294,7 +290,7 @@ enum State {
} }
async fn handle_capture_event( async fn handle_capture_event(
active: &Cell<Option<CaptureHandle>>, active: &mut Option<CaptureHandle>,
service: &Service, service: &Service,
capture: &mut InputCapture, capture: &mut InputCapture,
conn: &LanMouseConnection, conn: &LanMouseConnection,
@@ -331,9 +327,9 @@ async fn handle_capture_event(
} }
// activated a new client // activated a new client
if event == CaptureEvent::Begin && Some(handle) != active.get() { if event == CaptureEvent::Begin && Some(handle) != *active {
*state = State::WaitingForAck; *state = State::WaitingForAck;
active.replace(Some(handle)); active.replace(handle);
event_tx event_tx
.send(ICaptureEvent::ClientEntered(handle)) .send(ICaptureEvent::ClientEntered(handle))
.expect("channel closed"); .expect("channel closed");
@@ -363,9 +359,9 @@ async fn handle_capture_event(
async fn release_capture( async fn release_capture(
capture: &mut InputCapture, capture: &mut InputCapture,
active: &Cell<Option<CaptureHandle>>, active: &mut Option<CaptureHandle>,
) -> Result<(), CaptureError> { ) -> Result<(), CaptureError> {
active.replace(None); active.take();
capture.release().await capture.release().await
} }