strata-ui 0.1.0

Universal frontend framework in pure Rust with pixel-perfect CSS emulation and React/TSX ergonomics
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Model Context Protocol (MCP) Dev Bridge and In-Process IPC Dispatcher.
//!
//! Provides the bidirectional communication pipeline connecting external AI agents
//! to active Strata runtime windows, emulating mouse movements with authentic Yaru
//! cursor overlays, taking high-resolution screenshots, and safely batching UI actions.

use serde_json::{json, Value};
use std::io::{BufRead, BufReader, Write};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::PathBuf;
use std::sync::mpsc::{channel, Sender};
use std::sync::Mutex;
use std::time::Duration;

use crate::runtime::AppRuntime;
use strata_platform::{KeyCode, Modifiers, MouseButton};

type PendingItem = (String, Sender<String>);

fn get_pending_queue() -> &'static Mutex<Vec<PendingItem>> {
    static QUEUE: std::sync::OnceLock<Mutex<Vec<PendingItem>>> = std::sync::OnceLock::new();
    QUEUE.get_or_init(|| Mutex::new(Vec::new()))
}

#[cfg(unix)]
fn get_current_uid() -> u32 {
    extern "C" {
        fn getuid() -> u32;
    }
    unsafe { getuid() }
}

#[cfg(target_os = "linux")]
fn verify_peer_credentials(stream: &UnixStream) -> Result<(), std::io::Error> {
    use std::os::unix::io::AsRawFd;

    #[repr(C)]
    struct UCred {
        pid: i32,
        uid: u32,
        gid: u32,
    }

    extern "C" {
        fn getsockopt(
            sockfd: std::os::raw::c_int,
            level: std::os::raw::c_int,
            optname: std::os::raw::c_int,
            optval: *mut std::os::raw::c_void,
            optlen: *mut u32,
        ) -> std::os::raw::c_int;
    }

    let fd = stream.as_raw_fd();
    let mut ucred = UCred {
        pid: 0,
        uid: u32::MAX,
        gid: u32::MAX,
    };
    let mut len = std::mem::size_of::<UCred>() as u32;

    const SOL_SOCKET: std::os::raw::c_int = 1;
    const SO_PEERCRED: std::os::raw::c_int = 17;

    let res = unsafe {
        getsockopt(
            fd,
            SOL_SOCKET,
            SO_PEERCRED,
            &mut ucred as *mut _ as *mut std::os::raw::c_void,
            &mut len as *mut u32,
        )
    };

    if res != 0 {
        return Err(std::io::Error::last_os_error());
    }

    let my_uid = get_current_uid();
    if ucred.uid != my_uid {
        return Err(std::io::Error::new(
            std::io::ErrorKind::PermissionDenied,
            format!(
                "Peer UID {} does not match process UID {}",
                ucred.uid, my_uid
            ),
        ));
    }

    Ok(())
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
fn verify_peer_credentials(stream: &UnixStream) -> Result<(), std::io::Error> {
    use std::os::unix::io::AsRawFd;

    extern "C" {
        fn getpeereid(
            s: std::os::raw::c_int,
            euid: *mut u32,
            egid: *mut u32,
        ) -> std::os::raw::c_int;
    }

    let fd = stream.as_raw_fd();
    let mut euid = u32::MAX;
    let mut egid = u32::MAX;
    let res = unsafe { getpeereid(fd, &mut euid, &mut egid) };
    if res != 0 {
        return Err(std::io::Error::last_os_error());
    }

    let my_uid = get_current_uid();
    if euid != my_uid {
        return Err(std::io::Error::new(
            std::io::ErrorKind::PermissionDenied,
            format!("Peer UID {} does not match process UID {}", euid, my_uid),
        ));
    }

    Ok(())
}

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "ios")))]
fn verify_peer_credentials(_stream: &UnixStream) -> Result<(), std::io::Error> {
    Ok(())
}

/// Spawns a background thread listening for MCP connections on a Unix domain socket.
pub fn start_mcp_dev_listener(socket_path: Option<PathBuf>) -> Result<(), std::io::Error> {
    let path = socket_path.unwrap_or_else(default_dev_socket_path);

    // Clean up stale socket if present
    if path.exists() {
        let _ = std::fs::remove_file(&path);
    }

    if let Some(parent) = path.parent() {
        #[cfg(unix)]
        {
            use std::os::unix::fs::DirBuilderExt;
            let mut builder = std::fs::DirBuilder::new();
            builder.recursive(true);
            builder.mode(0o700);
            let _ = builder.create(parent);
            use std::os::unix::fs::PermissionsExt;
            let _ = std::fs::set_permissions(parent, std::fs::Permissions::from_mode(0o700));
        }
        #[cfg(not(unix))]
        {
            let _ = std::fs::create_dir_all(parent);
        }
    }

    let listener = UnixListener::bind(&path)?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600));
    }

    std::thread::Builder::new()
        .name("strata-mcp-ipc-listener".to_string())
        .spawn(move || {
            for stream in listener.incoming().flatten() {
                if verify_peer_credentials(&stream).is_ok() {
                    handle_client_connection(stream);
                }
            }
        })?;

    Ok(())
}

fn handle_client_connection(mut stream: UnixStream) {
    let _ = stream.set_read_timeout(Some(Duration::from_secs(10)));
    let _ = stream.set_write_timeout(Some(Duration::from_secs(10)));

    let mut reader = BufReader::new(match stream.try_clone() {
        Ok(s) => s,
        Err(_) => return,
    });

    let mut line = String::new();
    while let Ok(n) = reader.read_line(&mut line) {
        if n == 0 {
            break;
        }
        let trimmed = line.trim();
        if trimmed.is_empty() {
            line.clear();
            continue;
        }

        let (tx, rx) = channel();
        if let Ok(mut q) = get_pending_queue().lock() {
            q.push((trimmed.to_string(), tx));
        }

        // Signal GTK main event loop to wake up and process the queue
        #[cfg(target_os = "linux")]
        strata_platform::post_platform_event(strata_platform::PlatformEvent::RedrawRequested);

        let response = match rx.recv_timeout(Duration::from_secs(5)) {
            Ok(res) => res,
            Err(_) => json!({
                "jsonrpc": "2.0",
                "error": { "code": -32000, "message": "Request timed out waiting for UI thread" }
            })
            .to_string(),
        };

        let mut out_bytes = response.into_bytes();
        out_bytes.push(b'\n');
        if stream.write_all(&out_bytes).is_err() {
            break;
        }
        let _ = stream.flush();

        line.clear();
    }
}

/// Drains and processes all pending MCP requests on the active main UI thread.
///
/// Returns true if any executed command requires a view rebuild or display list redraw.
pub fn drain_and_execute_mcp_requests(runtime: &mut AppRuntime) -> bool {
    let mut items = Vec::new();
    {
        if let Ok(mut lock) = get_pending_queue().lock() {
            if lock.is_empty() {
                return false;
            }
            items = std::mem::take(&mut *lock);
        }
    }

    let mut any_dirty = false;
    for (req_str, tx) in items {
        let (resp_str, dirty) = process_single_mcp_request(runtime, &req_str);
        if dirty {
            any_dirty = true;
        }
        let _ = tx.send(resp_str);
    }

    any_dirty
}

fn process_single_mcp_request(runtime: &mut AppRuntime, req_str: &str) -> (String, bool) {
    let parsed: Value = match serde_json::from_str(req_str) {
        Ok(v) => v,
        Err(e) => {
            let err = json!({
                "jsonrpc": "2.0",
                "error": { "code": -32700, "message": format!("Parse error: {}", e) }
            });
            return (err.to_string(), false);
        }
    };

    let id = parsed.get("id").cloned();
    let method = parsed.get("method").and_then(|m| m.as_str()).unwrap_or("");
    let params = parsed.get("params").cloned().unwrap_or(json!({}));

    let (result, dirty) = execute_mcp_command_core(runtime, method, params);

    let resp = match result {
        Ok(val) => json!({
            "jsonrpc": "2.0",
            "id": id,
            "result": val,
        }),
        Err(err_msg) => json!({
            "jsonrpc": "2.0",
            "id": id,
            "error": { "code": -32603, "message": err_msg }
        }),
    };

    (resp.to_string(), dirty)
}

/// Core execution routine for MCP commands directly modifying `AppRuntime`.
pub fn execute_mcp_command_core(
    runtime: &mut AppRuntime,
    method: &str,
    params: Value,
) -> (Result<Value, String>, bool) {
    match method {
        "session_info" => {
            let cursor_info = runtime.mcp_cursor.as_ref().map(|c| {
                json!({
                    "x": c.current_x,
                    "y": c.current_y,
                    "target_x": c.target_x,
                    "target_y": c.target_y,
                    "is_moving": c.is_moving,
                    "fade_opacity": c.fade_opacity,
                    "cursor_icon": format!("{:?}", c.cursor_icon),
                    "is_down": c.is_down,
                })
            });

            let focus_info = runtime.active_focus.as_ref().map(|a| {
                json!({
                    "input_index": a.input_index,
                    "input_id": a.input_id,
                    "text": a.state.text.get(),
                    "cursor": a.state.cursor_index.get(),
                })
            });

            let info = json!({
                "viewport_width": runtime.viewport_width,
                "viewport_height": runtime.viewport_height,
                "dpr": runtime.dpr,
                "frame_count": runtime.frame_count,
                "cursor": cursor_info,
                "active_focus": focus_info,
                "cursor_theme": runtime.cursor_theme().name,
            });

            (Ok(info), false)
        }
        "mouse_move" => {
            let x = params.get("x").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            let y = params.get("y").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            let smooth = params
                .get("smooth")
                .and_then(|v| v.as_bool())
                .unwrap_or(true);

            let dirty = runtime.mcp_move_pointer(x, y, smooth);
            let cur_icon = runtime
                .mcp_cursor
                .as_ref()
                .map(|c| format!("{:?}", c.cursor_icon))
                .unwrap_or_else(|| "Default".to_string());

            (
                Ok(json!({
                    "x": x,
                    "y": y,
                    "smooth": smooth,
                    "cursor_icon": cur_icon
                })),
                dirty || true,
            )
        }
        "mouse_click" => {
            let x = params.get("x").and_then(|v| v.as_f64()).map(|f| f as f32);
            let y = params.get("y").and_then(|v| v.as_f64()).map(|f| f as f32);
            let btn_str = params
                .get("button")
                .and_then(|b| b.as_str())
                .unwrap_or("left");
            let count = params.get("count").and_then(|c| c.as_u64()).unwrap_or(1) as usize;
            let button = parse_mouse_button(btn_str);

            let dirty = runtime.mcp_mouse_click(button, x, y, count);
            (Ok(json!({ "clicked": true })), dirty || true)
        }
        "mouse_down" => {
            let x = params.get("x").and_then(|v| v.as_f64()).map(|f| f as f32);
            let y = params.get("y").and_then(|v| v.as_f64()).map(|f| f as f32);
            let btn_str = params
                .get("button")
                .and_then(|b| b.as_str())
                .unwrap_or("left");
            let button = parse_mouse_button(btn_str);

            let dirty = runtime.mcp_mouse_down(button, x, y);
            (Ok(json!({ "down": true })), dirty || true)
        }
        "mouse_up" => {
            let x = params.get("x").and_then(|v| v.as_f64()).map(|f| f as f32);
            let y = params.get("y").and_then(|v| v.as_f64()).map(|f| f as f32);
            let btn_str = params
                .get("button")
                .and_then(|b| b.as_str())
                .unwrap_or("left");
            let button = parse_mouse_button(btn_str);

            let dirty = runtime.mcp_mouse_up(button, x, y);
            (Ok(json!({ "up": true })), dirty || true)
        }
        "mouse_scroll" => {
            let delta_x = params
                .get("delta_x")
                .and_then(|v| v.as_f64())
                .unwrap_or(0.0) as f32;
            let delta_y = params
                .get("delta_y")
                .and_then(|v| v.as_f64())
                .unwrap_or(0.0) as f32;
            let x = params.get("x").and_then(|v| v.as_f64()).map(|f| f as f32);
            let y = params.get("y").and_then(|v| v.as_f64()).map(|f| f as f32);

            let dirty = runtime.mcp_mouse_scroll(delta_x, delta_y, x, y);
            (Ok(json!({ "scrolled": true })), dirty || true)
        }
        "keyboard_type" => {
            let text = params.get("text").and_then(|t| t.as_str()).unwrap_or("");
            let dirty = runtime.mcp_keyboard_type(text);
            (
                Ok(json!({ "typed_chars": text.chars().count() })),
                dirty || true,
            )
        }
        "keyboard_key" => {
            let key_str = params.get("key").and_then(|k| k.as_str()).unwrap_or("");
            let key = parse_key_code(key_str);
            let modifiers = parse_modifiers(params.get("modifiers"));

            let dirty = runtime.mcp_keyboard_key(key, modifiers);
            (Ok(json!({ "key_sent": key_str })), dirty || true)
        }
        "take_screenshot" => match runtime.mcp_capture_screenshot_png() {
            Ok(bytes) => {
                let base64_str = base64_encode(&bytes);
                (
                    Ok(json!({
                        "png_base64": base64_str,
                        "width": runtime.viewport_width,
                        "height": runtime.viewport_height,
                    })),
                    false,
                )
            }
            Err(e) => (Err(e), false),
        },
        "inspect_element" => {
            let x = params.get("x").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            let y = params.get("y").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
            let (tag, classes, bounds, text, cursor_icon) = runtime.mcp_inspect_element(x, y);

            (
                Ok(json!({
                    "tag": tag,
                    "classes": classes,
                    "bounds": {
                        "x": bounds.x,
                        "y": bounds.y,
                        "width": bounds.width,
                        "height": bounds.height,
                    },
                    "text": text,
                    "cursor_icon": format!("{:?}", cursor_icon),
                })),
                false,
            )
        }
        "execute_actions" => {
            let actions_val = params.get("actions").cloned().unwrap_or(json!([]));
            let actions: Vec<Value> = actions_val.as_array().cloned().unwrap_or_default();
            let mut executed = 0;

            for act in actions {
                let act_type = act.get("action").and_then(|a| a.as_str()).unwrap_or("");
                match act_type {
                    "move" => {
                        let x = act.get("x").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
                        let y = act.get("y").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
                        let smooth = act.get("smooth").and_then(|v| v.as_bool()).unwrap_or(true);
                        runtime.mcp_move_pointer(x, y, smooth);
                        executed += 1;
                    }
                    "down" => {
                        let btn = parse_mouse_button(
                            act.get("button").and_then(|b| b.as_str()).unwrap_or("left"),
                        );
                        runtime.mcp_mouse_down(btn, None, None);
                        executed += 1;
                    }
                    "up" => {
                        let btn = parse_mouse_button(
                            act.get("button").and_then(|b| b.as_str()).unwrap_or("left"),
                        );
                        runtime.mcp_mouse_up(btn, None, None);
                        executed += 1;
                    }
                    "click" => {
                        let x = act.get("x").and_then(|v| v.as_f64()).map(|f| f as f32);
                        let y = act.get("y").and_then(|v| v.as_f64()).map(|f| f as f32);
                        let btn = parse_mouse_button(
                            act.get("button").and_then(|b| b.as_str()).unwrap_or("left"),
                        );
                        let count = act.get("count").and_then(|c| c.as_u64()).unwrap_or(1) as usize;
                        runtime.mcp_mouse_click(btn, x, y, count);
                        executed += 1;
                    }
                    "type" => {
                        let text = act.get("text").and_then(|t| t.as_str()).unwrap_or("");
                        runtime.mcp_keyboard_type(text);
                        executed += 1;
                    }
                    "key" => {
                        let key =
                            parse_key_code(act.get("key").and_then(|k| k.as_str()).unwrap_or(""));
                        let mods = parse_modifiers(act.get("modifiers"));
                        runtime.mcp_keyboard_key(key, mods);
                        executed += 1;
                    }
                    "scroll" => {
                        let dx = act.get("delta_x").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
                        let dy = act.get("delta_y").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32;
                        let x = act.get("x").and_then(|v| v.as_f64()).map(|f| f as f32);
                        let y = act.get("y").and_then(|v| v.as_f64()).map(|f| f as f32);
                        runtime.mcp_mouse_scroll(dx, dy, x, y);
                        executed += 1;
                    }
                    "wait" => {
                        let ms = act
                            .get("ms")
                            .and_then(|m| m.as_u64())
                            .unwrap_or(50)
                            .min(5000);
                        std::thread::sleep(Duration::from_millis(ms));
                        executed += 1;
                    }
                    _ => {}
                }
            }

            (Ok(json!({ "executed_actions": executed })), true)
        }
        unknown => (Err(format!("Unknown MCP method: {}", unknown)), false),
    }
}

fn parse_mouse_button(s: &str) -> MouseButton {
    match s.to_ascii_lowercase().as_str() {
        "middle" | "wheel" => MouseButton::Middle,
        "right" => MouseButton::Right,
        _ => MouseButton::Left,
    }
}

fn parse_key_code(s: &str) -> KeyCode {
    match s.to_ascii_lowercase().as_str() {
        "enter" | "return" => KeyCode::Enter,
        "escape" | "esc" => KeyCode::Escape,
        "backspace" => KeyCode::Backspace,
        "delete" | "del" => KeyCode::Delete,
        "tab" => KeyCode::Tab,
        "space" => KeyCode::Space,
        "arrowleft" | "left" => KeyCode::ArrowLeft,
        "arrowright" | "right" => KeyCode::ArrowRight,
        "arrowup" | "up" => KeyCode::ArrowUp,
        "arrowdown" | "down" => KeyCode::ArrowDown,
        "home" => KeyCode::Home,
        "end" => KeyCode::End,
        "pageup" => KeyCode::PageUp,
        "pagedown" => KeyCode::PageDown,
        "a" | "keya" => KeyCode::KeyA,
        "c" | "keyc" => KeyCode::KeyC,
        "v" | "keyv" => KeyCode::KeyV,
        "x" | "keyx" => KeyCode::KeyX,
        "z" | "keyz" => KeyCode::KeyZ,
        "y" | "keyy" => KeyCode::KeyY,
        _ => KeyCode::Space,
    }
}

fn parse_modifiers(val: Option<&Value>) -> Modifiers {
    let mut mods = Modifiers::default();
    if let Some(v) = val {
        mods.shift = v.get("shift").and_then(|b| b.as_bool()).unwrap_or(false);
        mods.ctrl = v.get("ctrl").and_then(|b| b.as_bool()).unwrap_or(false);
        mods.alt = v.get("alt").and_then(|b| b.as_bool()).unwrap_or(false);
        mods.meta = v.get("meta").and_then(|b| b.as_bool()).unwrap_or(false);
    }
    mods
}

pub fn default_dev_socket_path() -> PathBuf {
    if let Ok(path) = std::env::var("STRATA_DEV_SOCKET") {
        PathBuf::from(path)
    } else if let Ok(runtime_dir) = std::env::var("XDG_RUNTIME_DIR") {
        PathBuf::from(runtime_dir).join("strata-dev.sock")
    } else {
        #[cfg(unix)]
        {
            let uid = get_current_uid();
            let runtime_dir = PathBuf::from(format!("/tmp/strata-runtime-{}", uid));
            runtime_dir.join("strata-dev.sock")
        }
        #[cfg(not(unix))]
        {
            PathBuf::from("/tmp/strata-dev.sock")
        }
    }
}

fn base64_encode(data: &[u8]) -> String {
    const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
    for chunk in data.chunks(3) {
        let b0 = chunk[0];
        let b1 = if chunk.len() > 1 { chunk[1] } else { 0 };
        let b2 = if chunk.len() > 2 { chunk[2] } else { 0 };

        let n = ((b0 as u32) << 16) | ((b1 as u32) << 8) | (b2 as u32);

        out.push(TABLE[((n >> 18) & 63) as usize] as char);
        out.push(TABLE[((n >> 12) & 63) as usize] as char);
        if chunk.len() > 1 {
            out.push(TABLE[((n >> 6) & 63) as usize] as char);
        } else {
            out.push('=');
        }
        if chunk.len() > 2 {
            out.push(TABLE[(n & 63) as usize] as char);
        } else {
            out.push('=');
        }
    }
    out
}