@@ -25,9 +25,11 @@ pub enum TransferState { |
| 25 | 25 | started_at: Instant, |
| 26 | 26 | /// True if transfer was triggered via keyboard (Super+Arrow) |
| 27 | 27 | keyboard_initiated: bool, |
| 28 | + /// If we initiated from ReceivedControl, this is the original source |
| 29 | + relay_from: Option<Direction>, |
| 28 | 30 | }, |
| 29 | 31 | |
| 30 | | - /// We sent control away, forwarding input |
| 32 | + /// We sent control away, forwarding input (from local devices) |
| 31 | 33 | RemoteActive { |
| 32 | 34 | target: Direction, |
| 33 | 35 | transfer_id: u64, |
@@ -36,6 +38,14 @@ pub enum TransferState { |
| 36 | 38 | keyboard_initiated: bool, |
| 37 | 39 | }, |
| 38 | 40 | |
| 41 | + /// We are relaying input from one machine to another (no local devices) |
| 42 | + Relaying { |
| 43 | + from: Direction, |
| 44 | + to: Direction, |
| 45 | + transfer_id: u64, |
| 46 | + entered_at: Instant, |
| 47 | + }, |
| 48 | + |
| 39 | 49 | /// We received control from another machine |
| 40 | 50 | ReceivedControl { |
| 41 | 51 | from: Direction, |
@@ -53,6 +63,10 @@ impl TransferState { |
| 53 | 63 | matches!(self, TransferState::RemoteActive { .. }) |
| 54 | 64 | } |
| 55 | 65 | |
| 66 | + pub fn is_relaying(&self) -> bool { |
| 67 | + matches!(self, TransferState::Relaying { .. }) |
| 68 | + } |
| 69 | + |
| 56 | 70 | pub fn is_receiving(&self) -> bool { |
| 57 | 71 | matches!(self, TransferState::ReceivedControl { .. }) |
| 58 | 72 | } |
@@ -61,12 +75,17 @@ impl TransferState { |
| 61 | 75 | /// Events from the transfer manager |
| 62 | 76 | #[derive(Debug, Clone)] |
| 63 | 77 | pub enum TransferEvent { |
| 64 | | - /// Start capturing and forwarding input |
| 78 | + /// Start capturing and forwarding input (from local devices) |
| 65 | 79 | /// `keyboard_initiated` is true if the transfer was triggered via keyboard (Super+Arrow), |
| 66 | 80 | /// false if triggered via CLI or other non-keyboard means |
| 67 | 81 | StartCapture { direction: Direction, keyboard_initiated: bool }, |
| 68 | 82 | /// Stop capturing, return to local |
| 69 | 83 | StopCapture, |
| 84 | + /// Start relaying input from one direction to another (no local devices needed) |
| 85 | + /// Used when a deviceless machine needs to forward input through |
| 86 | + StartRelay { from: Direction, to: Direction }, |
| 87 | + /// Stop relaying |
| 88 | + StopRelay, |
| 70 | 89 | /// Start injecting received input |
| 71 | 90 | StartInjection { from: Direction }, |
| 72 | 91 | /// Stop injecting |
@@ -124,9 +143,10 @@ impl TransferManager { |
| 124 | 143 | ) -> Result<(), TransferError> { |
| 125 | 144 | let mut state = self.state.write().await; |
| 126 | 145 | |
| 127 | | - // Only transfer from local or received state |
| 128 | | - match &*state { |
| 129 | | - TransferState::Local | TransferState::ReceivedControl { .. } => {} |
| 146 | + // Track if we're initiating from ReceivedControl (for relay mode) |
| 147 | + let relay_from = match &*state { |
| 148 | + TransferState::Local => None, |
| 149 | + TransferState::ReceivedControl { from, .. } => Some(*from), |
| 130 | 150 | TransferState::Initiating { .. } => { |
| 131 | 151 | return Err(TransferError::AlreadyTransferring); |
| 132 | 152 | } |
@@ -135,7 +155,12 @@ impl TransferManager { |
| 135 | 155 | "Already in remote active state".to_string(), |
| 136 | 156 | )); |
| 137 | 157 | } |
| 138 | | - } |
| 158 | + TransferState::Relaying { .. } => { |
| 159 | + return Err(TransferError::InvalidState( |
| 160 | + "Already relaying".to_string(), |
| 161 | + )); |
| 162 | + } |
| 163 | + }; |
| 139 | 164 | |
| 140 | 165 | let transfer_id = self.next_transfer_id(); |
| 141 | 166 | |
@@ -166,6 +191,7 @@ impl TransferManager { |
| 166 | 191 | transfer_id, |
| 167 | 192 | started_at: Instant::now(), |
| 168 | 193 | keyboard_initiated, |
| 194 | + relay_from, |
| 169 | 195 | }; |
| 170 | 196 | |
| 171 | 197 | // Send Enter message |
@@ -196,6 +222,7 @@ impl TransferManager { |
| 196 | 222 | target, |
| 197 | 223 | transfer_id, |
| 198 | 224 | keyboard_initiated, |
| 225 | + relay_from, |
| 199 | 226 | .. |
| 200 | 227 | } => { |
| 201 | 228 | if *transfer_id != ack.transfer_id { |
@@ -209,34 +236,67 @@ impl TransferManager { |
| 209 | 236 | |
| 210 | 237 | if !ack.success { |
| 211 | 238 | tracing::warn!("EnterAck rejected: {:?}", ack.error); |
| 212 | | - *state = TransferState::Local; |
| 239 | + // If we were relaying, go back to ReceivedControl |
| 240 | + if let Some(from) = relay_from { |
| 241 | + *state = TransferState::ReceivedControl { |
| 242 | + from: *from, |
| 243 | + transfer_id: *transfer_id, |
| 244 | + entered_at: Instant::now(), |
| 245 | + }; |
| 246 | + } else { |
| 247 | + *state = TransferState::Local; |
| 248 | + } |
| 213 | 249 | return Err(TransferError::Rejected( |
| 214 | 250 | ack.error.unwrap_or_else(|| "Unknown".to_string()), |
| 215 | 251 | )); |
| 216 | 252 | } |
| 217 | 253 | |
| 218 | | - tracing::info!( |
| 219 | | - "Transfer accepted, cursor at {:?}, keyboard_initiated={}", |
| 220 | | - ack.actual_cursor_pos, |
| 221 | | - keyboard_initiated |
| 222 | | - ); |
| 223 | | - |
| 224 | 254 | let direction = *target; |
| 225 | 255 | let tid = *transfer_id; |
| 226 | 256 | let kbd_init = *keyboard_initiated; |
| 257 | + let from_dir = *relay_from; |
| 227 | 258 | |
| 228 | | - *state = TransferState::RemoteActive { |
| 229 | | - target: direction, |
| 230 | | - transfer_id: tid, |
| 231 | | - entered_at: Instant::now(), |
| 232 | | - keyboard_initiated: kbd_init, |
| 233 | | - }; |
| 259 | + // Check if this is a relay (initiated from ReceivedControl) or direct transfer |
| 260 | + if let Some(from) = from_dir { |
| 261 | + tracing::info!( |
| 262 | + "Relay transfer accepted: {:?} -> {:?}, cursor at {:?}", |
| 263 | + from, |
| 264 | + direction, |
| 265 | + ack.actual_cursor_pos |
| 266 | + ); |
| 234 | 267 | |
| 235 | | - // Start capturing input |
| 236 | | - self.event_tx |
| 237 | | - .send(TransferEvent::StartCapture { direction, keyboard_initiated: kbd_init }) |
| 238 | | - .await |
| 239 | | - .map_err(|_| TransferError::ChannelClosed)?; |
| 268 | + *state = TransferState::Relaying { |
| 269 | + from, |
| 270 | + to: direction, |
| 271 | + transfer_id: tid, |
| 272 | + entered_at: Instant::now(), |
| 273 | + }; |
| 274 | + |
| 275 | + // Start relaying input (don't grab local devices, forward from source) |
| 276 | + self.event_tx |
| 277 | + .send(TransferEvent::StartRelay { from, to: direction }) |
| 278 | + .await |
| 279 | + .map_err(|_| TransferError::ChannelClosed)?; |
| 280 | + } else { |
| 281 | + tracing::info!( |
| 282 | + "Transfer accepted, cursor at {:?}, keyboard_initiated={}", |
| 283 | + ack.actual_cursor_pos, |
| 284 | + keyboard_initiated |
| 285 | + ); |
| 286 | + |
| 287 | + *state = TransferState::RemoteActive { |
| 288 | + target: direction, |
| 289 | + transfer_id: tid, |
| 290 | + entered_at: Instant::now(), |
| 291 | + keyboard_initiated: kbd_init, |
| 292 | + }; |
| 293 | + |
| 294 | + // Start capturing input from local devices |
| 295 | + self.event_tx |
| 296 | + .send(TransferEvent::StartCapture { direction, keyboard_initiated: kbd_init }) |
| 297 | + .await |
| 298 | + .map_err(|_| TransferError::ChannelClosed)?; |
| 299 | + } |
| 240 | 300 | |
| 241 | 301 | // Trigger clipboard sync (if enabled, handled by main loop) |
| 242 | 302 | self.event_tx |
@@ -283,6 +343,11 @@ impl TransferManager { |
| 283 | 343 | // This shouldn't happen normally - they should send Leave, not Enter |
| 284 | 344 | tracing::warn!("Received Enter while in RemoteActive - unusual but accepting"); |
| 285 | 345 | } |
| 346 | + TransferState::Relaying { .. } => { |
| 347 | + // We're relaying input, but receiving a new Enter |
| 348 | + // This is unusual but we'll accept it |
| 349 | + tracing::warn!("Received Enter while Relaying - unusual but accepting"); |
| 350 | + } |
| 286 | 351 | } |
| 287 | 352 | |
| 288 | 353 | // Calculate actual cursor position using proper screen bounds |
@@ -440,8 +505,45 @@ impl TransferManager { |
| 440 | 505 | *state = TransferState::Local; |
| 441 | 506 | Ok(()) |
| 442 | 507 | } |
| 508 | + TransferState::Relaying { from, transfer_id, .. } => { |
| 509 | + if *transfer_id != payload.transfer_id { |
| 510 | + tracing::warn!("Leave transfer_id mismatch (relay)"); |
| 511 | + } |
| 512 | + |
| 513 | + let from_dir = *from; |
| 514 | + tracing::info!("Relay target returned control, resuming ReceivedControl from {:?}", from_dir); |
| 515 | + |
| 516 | + // Stop relaying |
| 517 | + self.event_tx |
| 518 | + .send(TransferEvent::StopRelay) |
| 519 | + .await |
| 520 | + .map_err(|_| TransferError::ChannelClosed)?; |
| 521 | + |
| 522 | + // Send LeaveAck to the target |
| 523 | + let direction = payload.to_direction.opposite(); |
| 524 | + self.event_tx |
| 525 | + .send(TransferEvent::SendMessage { |
| 526 | + direction, |
| 527 | + message: Message::LeaveAck, |
| 528 | + }) |
| 529 | + .await |
| 530 | + .map_err(|_| TransferError::ChannelClosed)?; |
| 531 | + |
| 532 | + // Resume injection from original source |
| 533 | + self.event_tx |
| 534 | + .send(TransferEvent::StartInjection { from: from_dir }) |
| 535 | + .await |
| 536 | + .map_err(|_| TransferError::ChannelClosed)?; |
| 537 | + |
| 538 | + *state = TransferState::ReceivedControl { |
| 539 | + from: from_dir, |
| 540 | + transfer_id: *transfer_id, |
| 541 | + entered_at: Instant::now(), |
| 542 | + }; |
| 543 | + Ok(()) |
| 544 | + } |
| 443 | 545 | _ => Err(TransferError::InvalidState( |
| 444 | | - "Not in RemoteActive state".to_string(), |
| 546 | + "Not in RemoteActive or Relaying state".to_string(), |
| 445 | 547 | )), |
| 446 | 548 | } |
| 447 | 549 | } |
@@ -451,15 +553,34 @@ impl TransferManager { |
| 451 | 553 | let mut state = self.state.write().await; |
| 452 | 554 | |
| 453 | 555 | match &*state { |
| 454 | | - TransferState::Initiating { .. } => { |
| 556 | + TransferState::Initiating { relay_from, .. } => { |
| 455 | 557 | tracing::warn!("Aborting pending transfer"); |
| 456 | | - *state = TransferState::Local; |
| 558 | + // If we were initiating from ReceivedControl, go back there |
| 559 | + if let Some(from) = relay_from { |
| 560 | + *state = TransferState::ReceivedControl { |
| 561 | + from: *from, |
| 562 | + transfer_id: 0, |
| 563 | + entered_at: Instant::now(), |
| 564 | + }; |
| 565 | + } else { |
| 566 | + *state = TransferState::Local; |
| 567 | + } |
| 457 | 568 | } |
| 458 | 569 | TransferState::RemoteActive { .. } => { |
| 459 | 570 | tracing::warn!("Aborting remote active state"); |
| 460 | 571 | let _ = self.event_tx.send(TransferEvent::StopCapture).await; |
| 461 | 572 | *state = TransferState::Local; |
| 462 | 573 | } |
| 574 | + TransferState::Relaying { from, .. } => { |
| 575 | + tracing::warn!("Aborting relay state"); |
| 576 | + let _ = self.event_tx.send(TransferEvent::StopRelay).await; |
| 577 | + // Go back to receiving from original source |
| 578 | + *state = TransferState::ReceivedControl { |
| 579 | + from: *from, |
| 580 | + transfer_id: 0, |
| 581 | + entered_at: Instant::now(), |
| 582 | + }; |
| 583 | + } |
| 463 | 584 | TransferState::ReceivedControl { .. } => { |
| 464 | 585 | tracing::warn!("Aborting received control state"); |
| 465 | 586 | let _ = self.event_tx.send(TransferEvent::StopInjection).await; |