rdesktop-core 0.1.2

Core abstractions for rdesktop - a dual-engine Rust desktop framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Application configuration for rdesktop.
//!
//! This module defines all configuration types used by the framework.
//! Configuration can be loaded from `rdesktop.toml` or constructed programmatically.
//!
//! ## Agent-First Development
//!
//! rdesktop supports a special `dev` mode designed for AI agent workflows:
//! - `rdesktop dev` starts a local HTTP server serving the frontend
//! - The app runs in the user's browser (localhost:PORT)
//! - AI agents can use Playwright/Puppeteer MCP tools to inspect and interact
//! - No native window needed during development
//! - Same code works in both dev (browser) and prod (native window) modes

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Configuration for the entire rdesktop application.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
    /// Application identifier (reverse domain notation, e.g. "com.example.myapp")
    pub identifier: String,

    /// Application name displayed to users
    pub name: String,

    /// Application version
    pub version: String,

    /// Renderer to use (default: WebView)
    #[serde(default)]
    pub renderer: RendererConfig,

    /// Window configuration
    #[serde(default)]
    pub window: WindowConfig,

    /// Development server configuration (Agent-first)
    #[serde(default)]
    pub dev: DevConfig,

    /// Bundle configuration for packaging
    #[serde(default)]
    pub bundle: BundleConfig,

    /// Custom IPC command handlers
    #[serde(default)]
    pub commands: HashMap<String, CommandConfig>,

    /// Global hotkeys registered at the OS level (fire even when the window is
    /// unfocused). Each entry parses its `combo` via `Hotkey::from_str`
    /// (e.g. "Ctrl+Shift+K", "Alt+F4", "Meta+Space").
    #[serde(default)]
    pub hotkeys: Vec<HotkeyConfig>,

    /// Global input hook configuration (system-wide keyboard/mouse capture).
    /// Off by default — enable explicitly to observe raw input events.
    #[serde(default)]
    pub global_input: GlobalInputConfig,
}

impl Default for AppConfig {
    fn default() -> Self {
        Self {
            identifier: "com.example.app".to_string(),
            name: "rdesktop App".to_string(),
            version: "0.1.0".to_string(),
            renderer: RendererConfig::default(),
            window: WindowConfig::default(),
            dev: DevConfig::default(),
            bundle: BundleConfig::default(),
            commands: HashMap::new(),
            hotkeys: Vec::new(),
            global_input: GlobalInputConfig::default(),
        }
    }
}

/// Renderer backend selection.
///
/// - `WebView`: Uses system WebView (WebView2/WebKit). Lightweight (~5MB).
/// - `Chrome`: Uses Chrome Embedded Framework. Pixel-perfect (~150MB).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RendererKind {
    /// Use system WebView (WebView2 on Windows, WebKit on macOS/Linux)
    /// Default, lightweight, ~5MB overhead
    #[serde(rename = "webview")]
    WebView,

    /// Use Chrome Embedded Framework for cross-platform pixel consistency
    /// Larger bundle (~150MB) but guaranteed identical rendering
    #[serde(rename = "chrome")]
    Chrome,
}

impl Default for RendererKind {
    fn default() -> Self {
        Self::WebView
    }
}

/// Renderer configuration (deserialized from `[renderer]` section in TOML).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RendererConfig {
    /// Which renderer backend to use
    #[serde(default)]
    pub kind: RendererKind,

    /// Enable WebGPU in the web context so the frontend can drive native
    /// shaders (Wallpaper-Engine-style effects). Passed as Chromium flags.
    #[serde(default = "default_true")]
    pub webgpu: bool,
}

impl Default for RendererConfig {
    fn default() -> Self {
        Self {
            kind: RendererKind::default(),
            webgpu: true,
        }
    }
}

/// Development server configuration.
///
/// This is the core of rdesktop's Agent-first development story.
/// During `rdesktop dev`, the app is served as a web page that AI agents
/// can interact with using mature browser automation tools (Playwright, Puppeteer).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DevConfig {
    /// Port for the development server (default: 1420, same as Tauri)
    #[serde(default = "default_dev_port")]
    pub port: u16,

    /// Host to bind to (default: "localhost")
    /// Use "0.0.0.0" to allow remote agent access
    #[serde(default = "default_dev_host")]
    pub host: String,

    /// Whether to open the browser automatically
    #[serde(default = "default_true")]
    pub open_browser: bool,

    /// Enable hot reload on file changes
    #[serde(default = "default_true")]
    pub hot_reload: bool,

    /// Enable the Agent MCP endpoint for structured interaction
    /// When enabled, exposes /__rdesktop__/agent/* endpoints for:
    ///   - DOM inspection (without screenshots)
    ///   - Element querying (CSS selectors, text content)
    ///   - Action execution (click, type, scroll)
    ///   - State snapshots (full DOM + computed styles)
    #[serde(default = "default_true")]
    pub agent_mode: bool,

    /// Enable the devtools overlay in browser mode
    #[serde(default = "default_true")]
    pub devtools: bool,
}

impl Default for DevConfig {
    fn default() -> Self {
        Self {
            port: default_dev_port(),
            host: default_dev_host(),
            open_browser: true,
            hot_reload: true,
            agent_mode: true,
            devtools: true,
        }
    }
}

/// Window layer/kind for native mode.
///
/// Extends Tauri v2's model with a `Wallpaper` layer (desktop-level,
/// click-through) and an `Overlay` layer (always-on-top HUD), enabling
/// Wallpaper-Engine-style and HUD/PIP scenarios.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WindowKind {
    /// Standard application window (default)
    #[serde(rename = "normal")]
    Normal,

    /// Always-on-top overlay (HUD / PIP / floating toolbar)
    #[serde(rename = "overlay")]
    Overlay,

    /// Desktop wallpaper layer: sits behind icons, clicks pass through.
    /// Implies `click_through = true`.
    #[serde(rename = "wallpaper")]
    Wallpaper,
}

impl Default for WindowKind {
    fn default() -> Self {
        Self::Normal
    }
}

/// 32-bit RGBA window icon data used by native window backends.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowIcon {
    /// Pixels in row-major RGBA order.
    pub rgba: Vec<u8>,
    /// Icon width in pixels.
    pub width: u32,
    /// Icon height in pixels.
    pub height: u32,
}

/// Window configuration for native mode.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowConfig {
    /// Window title
    #[serde(default = "default_title")]
    pub title: String,

    /// Window width in logical pixels
    #[serde(default = "default_width")]
    pub width: u32,

    /// Window height in logical pixels
    #[serde(default = "default_height")]
    pub height: u32,

    /// Whether the window is resizable
    #[serde(default = "default_true")]
    pub resizable: bool,

    /// Whether the window has decorations (title bar, borders)
    #[serde(default = "default_true")]
    pub decorations: bool,

    /// Whether the window is transparent
    #[serde(default)]
    pub transparent: bool,

    /// Whether the window is always on top
    #[serde(default)]
    pub always_on_top: bool,

    /// Whether to show in taskbar
    #[serde(default = "default_true")]
    pub visible_on_all_workspaces: bool,

    /// Optional native icon shown in the title bar, taskbar and background previews.
    #[serde(default)]
    pub icon: Option<WindowIcon>,

    /// Window layer/kind. See [`WindowKind`].
    #[serde(default)]
    pub kind: WindowKind,

    /// Click-through: pointer events fall through to whatever is behind the
    /// window. Required for wallpaper; can also be set explicitly on overlays.
    #[serde(default)]
    pub click_through: bool,

    /// Minimum window size (width, height)
    pub min_size: Option<(u32, u32)>,

    /// Maximum window size (width, height)
    pub max_size: Option<(u32, u32)>,
}

impl Default for WindowConfig {
    fn default() -> Self {
        Self {
            title: default_title(),
            width: default_width(),
            height: default_height(),
            resizable: default_true(),
            decorations: default_true(),
            transparent: false,
            always_on_top: false,
            visible_on_all_workspaces: true,
            icon: None,
            kind: WindowKind::default(),
            click_through: false,
            min_size: None,
            max_size: None,
        }
    }
}

/// Bundle configuration for packaging the app.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BundleConfig {
    /// Windows installer format: "nsis", "wix", or "both"
    #[serde(default = "default_windows_installer")]
    pub windows_installer: String,

    /// macOS bundle identifier
    pub macos_bundle_id: Option<String>,

    /// Linux package formats: "appimage", "deb", "rpm", or combinations
    #[serde(default = "default_linux_packages")]
    pub linux_packages: Vec<String>,

    /// Icon path (relative to project root)
    pub icon: Option<String>,

    /// Whether to code-sign on macOS
    #[serde(default)]
    pub macos_sign: bool,

    /// Whether to create a DMG on macOS
    #[serde(default = "default_true")]
    pub macos_dmg: bool,

    /// Copyright notice
    pub copyright: Option<String>,
}

impl Default for BundleConfig {
    fn default() -> Self {
        Self {
            windows_installer: default_windows_installer(),
            macos_bundle_id: None,
            linux_packages: default_linux_packages(),
            icon: None,
            macos_sign: false,
            macos_dmg: true,
            copyright: None,
        }
    }
}

/// Configuration for an IPC command handler.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandConfig {
    /// Command to execute
    pub command: String,

    /// Working directory (optional)
    pub working_dir: Option<String>,

    /// Environment variables
    #[serde(default)]
    pub env: HashMap<String, String>,
}

/// A global hotkey declared in configuration.
///
/// `id` is an optional stable identifier echoed back to the handler; `combo`
/// is parsed by [`crate::hotkeys::Hotkey::from_str`] (case-insensitive,
/// `+`-separated modifiers, e.g. `"Ctrl+Shift+K"`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HotkeyConfig {
    /// Stable identifier for this hotkey (echoed to the handler). If omitted,
    /// the list index is used.
    #[serde(default)]
    pub id: Option<String>,

    /// Key combination string, e.g. "Alt+F4", "Meta+Shift+P".
    pub combo: String,

    /// Optional human-readable title (shown in UI lists).
    #[serde(default)]
    pub title: Option<String>,
}

/// Global input hook configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalInputConfig {
    /// Master switch for global input capture.
    #[serde(default)]
    pub enabled: bool,

    /// Capture keyboard events.
    #[serde(default = "default_true")]
    pub keyboard: bool,

    /// Capture mouse button events.
    #[serde(default = "default_true")]
    pub mouse: bool,

    /// Also forward high-frequency `MouseMove` events (off by default).
    #[serde(default)]
    pub mouse_move: bool,
}

impl Default for GlobalInputConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            keyboard: true,
            mouse: true,
            mouse_move: false,
        }
    }
}

// Default value functions for serde
fn default_title() -> String {
    "rdesktop App".to_string()
}
fn default_width() -> u32 {
    1280
}
fn default_height() -> u32 {
    720
}
fn default_true() -> bool {
    true
}
fn default_dev_port() -> u16 {
    1420
}
fn default_dev_host() -> String {
    "localhost".to_string()
}
fn default_windows_installer() -> String {
    "nsis".to_string()
}
fn default_linux_packages() -> Vec<String> {
    vec!["appimage".to_string()]
}