pub const VIRTIO_INPUT_FOCUS_SCHEMA: &str = "turkos.os.virtio_input.focus.v1"; pub const VIRTIO_INPUT_FOCUS_INVARIANT: &str = "Bir session başka pencereye input çalamaz"; pub const VIRTIO_INPUT_FOCUS_POSITIVE: &str = "İki odaklı sırayla uygulama"; pub const VIRTIO_INPUT_FOCUS_COUNTEREXAMPLE: &str = "Eski focus session replay"; pub const VIRTIO_INPUT_FOCUS_ACCEPTANCE: &str = "Yalnız güncel hedef olayı alır"; pub const EXPECTED_SEQUENTIAL_FOCUSED_APPS: u32 = 2; pub const EXPECTED_ACTIVE_TARGET_SESSION_ID: u32 = 0x2260_0002; pub const EXPECTED_ACTIVE_TARGET_EVENTS_DELIVERED: u32 = 4; pub const EXPECTED_STALE_TARGET_EVENTS_DELIVERED: u32 = 0; #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum InputFocusError { AlreadyCommitted, CrossWindowInputTheftAttempt, StaleFocusSessionReplay, SequentialFocusedAppsCountMismatch, ActiveTargetSessionIdMismatch, ActiveTargetEventsMismatch, StaleTargetReceivedEvents, ZeroFocusEpoch, NonMonotonicFocusEpoch, ZeroFocusRoutingDigest, UnboundInputFocusDigest, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct InputFocusRequest { pub cross_window_input_theft: bool, pub stale_focus_session_replay: bool, pub sequential_focused_apps: u32, pub active_target_session_id: u32, pub active_target_events_delivered: u32, pub stale_target_events_delivered: u32, pub only_current_target_receives_events: bool, pub focus_epoch: u64, pub previous_focus_epoch: u64, pub focus_routing_digest: u64, pub focus_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct InputFocusReceipt { pub sequential_focused_apps: u32, pub active_target_session_id: u32, pub active_target_events_delivered: u32, pub stale_target_events_delivered: u32, pub only_current_target_receives_events: bool, pub focus_epoch: u64, pub focus_routing_digest: u64, pub receipt_digest: u64, } pub const fn compute_input_focus_binding_digest( sequential_focused_apps: u32, active_target_session_id: u32, active_target_events_delivered: u32, stale_target_events_delivered: u32, focus_epoch: u64, focus_routing_digest: u64, ) -> u64 { let mut h: u64 = 0xcbf2_9ce4_8422_2325; h = (h ^ 0x2260_5649_4e50_4643).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (sequential_focused_apps as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (active_target_session_id as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (active_target_events_delivered as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ (stale_target_events_delivered as u64)).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ focus_epoch).wrapping_mul(0x0000_0100_0000_01b3); h = (h ^ focus_routing_digest).wrapping_mul(0x0000_0100_0000_01b3); h } pub struct VirtioInputFocusCoordinator { committed: bool, committed_total: u64, rejected_total: u64, theft_replay_refusals: u64, routing_target_refusals: u64, committed_receipt: Option, } impl VirtioInputFocusCoordinator { pub const fn new() -> Self { Self { committed: false, committed_total: 0, rejected_total: 0, theft_replay_refusals: 0, routing_target_refusals: 0, committed_receipt: None, } } pub fn is_committed(&self) -> bool { self.committed } pub fn committed_total(&self) -> u64 { self.committed_total } pub fn rejected_total(&self) -> u64 { self.rejected_total } pub fn theft_replay_refusals(&self) -> u64 { self.theft_replay_refusals } pub fn routing_target_refusals(&self) -> u64 { self.routing_target_refusals } pub fn committed_receipt(&self) -> Option { self.committed_receipt } pub fn commit_focus_routing( &mut self, req: &InputFocusRequest, ) -> Result { if self.committed { self.rejected_total += 1; return Err(InputFocusError::AlreadyCommitted); } if req.cross_window_input_theft { self.rejected_total += 1; self.theft_replay_refusals += 1; return Err(InputFocusError::CrossWindowInputTheftAttempt); } if req.stale_focus_session_replay { self.rejected_total += 1; self.theft_replay_refusals += 1; return Err(InputFocusError::StaleFocusSessionReplay); } if req.sequential_focused_apps != EXPECTED_SEQUENTIAL_FOCUSED_APPS { self.rejected_total += 1; self.routing_target_refusals += 1; return Err(InputFocusError::SequentialFocusedAppsCountMismatch); } if req.active_target_session_id != EXPECTED_ACTIVE_TARGET_SESSION_ID { self.rejected_total += 1; self.routing_target_refusals += 1; return Err(InputFocusError::ActiveTargetSessionIdMismatch); } if req.active_target_events_delivered != EXPECTED_ACTIVE_TARGET_EVENTS_DELIVERED { self.rejected_total += 1; self.routing_target_refusals += 1; return Err(InputFocusError::ActiveTargetEventsMismatch); } if req.stale_target_events_delivered != EXPECTED_STALE_TARGET_EVENTS_DELIVERED || !req.only_current_target_receives_events { self.rejected_total += 1; self.routing_target_refusals += 1; return Err(InputFocusError::StaleTargetReceivedEvents); } if req.focus_epoch == 0 { self.rejected_total += 1; return Err(InputFocusError::ZeroFocusEpoch); } if req.focus_epoch <= req.previous_focus_epoch { self.rejected_total += 1; return Err(InputFocusError::NonMonotonicFocusEpoch); } if req.focus_routing_digest == 0 { self.rejected_total += 1; return Err(InputFocusError::ZeroFocusRoutingDigest); } let expected_binding = compute_input_focus_binding_digest( req.sequential_focused_apps, req.active_target_session_id, req.active_target_events_delivered, req.stale_target_events_delivered, req.focus_epoch, req.focus_routing_digest, ); if req.focus_binding_digest != expected_binding { self.rejected_total += 1; self.routing_target_refusals += 1; return Err(InputFocusError::UnboundInputFocusDigest); } self.committed = true; self.committed_total += 1; let receipt = InputFocusReceipt { sequential_focused_apps: req.sequential_focused_apps, active_target_session_id: req.active_target_session_id, active_target_events_delivered: req.active_target_events_delivered, stale_target_events_delivered: req.stale_target_events_delivered, only_current_target_receives_events: true, focus_epoch: req.focus_epoch, focus_routing_digest: req.focus_routing_digest, receipt_digest: expected_binding ^ 0x2260_ACE0_464f_4353, }; self.committed_receipt = Some(receipt); Ok(receipt) } }