Skip to main content

rdesktop_core/
global.rs

1//! Bridge that forwards global hotkey / input events into the renderer's
2//! outbox, so the frontend receives them as unnamed `window.__RDESKTOP_PUSH__`
3//! messages (matching the IPC contract used by the Node extension host).
4//!
5//! `PushHandler` implements both [`HotkeyHandler`](crate::hotkeys::HotkeyHandler)
6//! and [`GlobalInputHandler`](crate::input::GlobalInputHandler) and writes a
7//! JSON envelope into a shared `outbox` queue drained every frame by the
8//! backend event loop.
9
10use crate::hotkeys::{Hotkey, HotkeyHandler};
11use crate::input::{GlobalInputEvent, GlobalInputHandler};
12use serde_json::json;
13use std::sync::{Arc, Mutex};
14
15/// Shared queue of JSON strings emitted to the frontend each frame.
16pub type Outbox = Arc<Mutex<Vec<String>>>;
17
18/// Forwards global events to the frontend via the renderer outbox.
19pub struct PushHandler {
20    outbox: Outbox,
21}
22
23impl PushHandler {
24    pub fn new(outbox: Outbox) -> Arc<Self> {
25        Arc::new(Self { outbox })
26    }
27}
28
29impl HotkeyHandler for PushHandler {
30    fn on_hotkey(&self, id: u32, hotkey: &Hotkey) {
31        if let Ok(s) =
32            serde_json::to_string(&json!({ "cmd": "rdesktop.globalHotkey", "payload": { "id": id, "combo": hotkey.to_string() } }))
33        {
34            self.outbox.lock().unwrap().push(s);
35        }
36    }
37}
38
39impl GlobalInputHandler for PushHandler {
40    fn on_event(&self, event: GlobalInputEvent) {
41        if let Ok(s) =
42            serde_json::to_string(&json!({ "cmd": "rdesktop.globalInput", "payload": event }))
43        {
44            self.outbox.lock().unwrap().push(s);
45        }
46    }
47}