hex/crates/capture/src/win/privacy.rs
Kit Langton fb617e5082 Import scrawl-capture into crates/capture
Copy Scrawl's `crates/capture` from kitlangton/scrawl, branch
`engine/split`, commit e351ddb28be47c77481cb6821cc72d7f8fe7c769 (e351ddb),
without history, keeping the package name `scrawl-capture`, and its
`docs/app-handover-spec.md` as `docs/plans/capture-handover-spec.md`. Hex
depends on it by path instead of the Git pin at 86d7d6c; the Nix
`outputHashes."scrawl-capture-0.3.13"` entry goes.

Workspace: `crates/capture` is in `[workspace] exclude`, so `cargo build`,
`cargo test`, and `--workspace` never build it (Linux, Windows, and Nix
included). It compiles only as the `capture` feature's macOS dependency,
sharing the root Cargo.lock and target/. The root package stays a member
without being listed, since listing "." defeats `exclude`. On macOS
`scripts/check` and the macOS workflow name it (`-p voice-control
-p scrawl-capture --features capture`) to lint and test its 257 tests.

API adaptations in capture_host:
- `Finished` gains `gaps` and `missing_transcript`; the host records them
  and `hex capture status` (human and JSON `last`) shows them.
- `Engine::new` bridges the crate's log to `Host::log` when no sink is
  set, so Hex's own `TracingLog` sink goes and each line is logged once.
- Scrawl now writes its bundle files 0600; Hex still tightens
  recording.mp4, the window log, and folders.
- Nothing on Hex's path reads ~/.config/scrawl or writes under
  Application Support/scrawl: the crate does that only after
  `enable_scrawl_host_defaults`/`enable_file_log`, which Hex never calls.

Scrubbed (synthetic values): a file:///Users/<name> path in ax.rs, the
com.kitlangton.UpDown / dev.kitlangton.UpDown test bundle IDs in
handoff.rs and phone.rs (now com.example.UpDown), and a personal device
name in a phone.rs test. Kept: Scrawl's own product IDs
(dev.kitlangton.scrawl, .helper) and its public GitHub releases URL.

fmt and clippy -D warnings, no behaviour change: `cargo fmt` on five
files; `chunks_exact(N)` to `as_chunks::<N>()` (19 sites); collapsed
`if let` chains in capture/macos.rs and pipeline.rs and a `match` in
page.rs; `type` aliases for a drag start and two ingest hooks; explicit
lower bounds on two test `match` range arms in keyframes.rs;
`slice::from_ref` for two one-element slices; `and_then` for
`map().flatten()`; a dropped `to_owned`; `#[allow]` for one
`too_many_arguments` and one `single_range_in_vec_init`.
2026-10-25 19:14:23 -07:00

79 lines
2.6 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Windows privacy switches. Desktop apps need none for screen capture, input hooks or sending
//! keystrokes; the microphone (and camera) can be turned off for all desktop apps in
//! Settings › Privacy & security, which the onboarding checklist shows.
use std::collections::BTreeMap;
use windows::Win32::System::Registry::{
HKEY, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, RRF_RT_REG_SZ, RegGetValueW,
};
use windows::core::PCWSTR;
use super::wide;
use crate::permissions::{Permission, Status};
const CONSENT: &str =
r"Software\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore";
fn read_string(root: HKEY, key: &str, value: &str) -> Option<String> {
let key = wide(key);
let value = wide(value);
let mut buffer = [0_u16; 64];
let mut size = (buffer.len() * 2) as u32;
let status = unsafe {
RegGetValueW(
root,
PCWSTR(key.as_ptr()),
PCWSTR(value.as_ptr()),
RRF_RT_REG_SZ,
None,
Some(buffer.as_mut_ptr().cast()),
Some(&mut size),
)
};
if status.is_err() {
return None;
}
let chars = (size as usize / 2).saturating_sub(1).min(buffer.len());
Some(String::from_utf16_lossy(&buffer[..chars]))
}
/// A capability's switch: denied when the device-wide, per-user or desktop-app switch is off.
fn capability(name: &str) -> Status {
let switches = [
(HKEY_LOCAL_MACHINE, format!(r"{CONSENT}\{name}")),
(HKEY_CURRENT_USER, format!(r"{CONSENT}\{name}")),
(HKEY_CURRENT_USER, format!(r"{CONSENT}\{name}\NonPackaged")),
];
let denied = switches.iter().any(|(root, key)| {
read_string(*root, key, "Value").is_some_and(|value| value.eq_ignore_ascii_case("Deny"))
});
if denied {
Status::Denied
} else {
Status::Granted
}
}
pub fn permissions() -> BTreeMap<Permission, Status> {
BTreeMap::from([
(Permission::ScreenRecording, Status::Granted),
(Permission::Microphone, capability("microphone")),
(Permission::InputMonitoring, Status::Granted),
(Permission::Accessibility, Status::Granted),
(Permission::Camera, capability("webcam")),
])
}
/// The Settings page that turns a permission on.
pub fn settings_uri(permission: Permission) -> &'static str {
match permission {
Permission::Microphone => "ms-settings:privacy-microphone",
Permission::Camera => "ms-settings:privacy-webcam",
_ => "ms-settings:privacy",
}
}
pub fn open_settings(permission: Permission) {
let _ = super::shell::open(std::path::Path::new(settings_uri(permission)));
}