Sweep every first-party crate source (1956 .rs files) to the project comment guidelines: delete redundant restatements, decorative banners, change narration, and end-of-line comments; keep and tighten the crucial ones (invariants, bug rationale, SAFETY blocks, ported-source attribution). No functional code changed. Every edit is proven comment-only against the prior tree by a comment-stripping lexer (string/char/raw-string aware) plus a separate doctest-fence check. Where removing a comment made rustfmt or clippy want to re-lay-out adjacent code, the minimal triggering comment is restored so code tokens stay byte-identical. Gates green: cargo fmt --all --check (0 diffs), cargo check and cargo clippy --workspace --all-targets (0 warnings). Adds scripts/check_codegen_comment_guidelines.py — the enforcement gate for these guidelines (flags banners, end-of-line comments, change narration, and commented-out code).
52 lines
1.6 KiB
Rust
52 lines
1.6 KiB
Rust
//! Native macOS modifier key detection via CoreGraphics.
|
|
//!
|
|
//! Side-channels around the PTY directly accessing CoreGraphics.
|
|
|
|
// CoreGraphics CGEventSourceFlagsState — returns the current global
|
|
// modifier flags without requiring any special permissions.
|
|
#[link(name = "CoreGraphics", kind = "framework")]
|
|
unsafe extern "C" {
|
|
fn CGEventSourceFlagsState(stateID: i32) -> u64;
|
|
}
|
|
|
|
const K_CG_EVENT_SOURCE_STATE_HID_SYSTEM_STATE: i32 = 1;
|
|
|
|
// CGEventFlags bitmask constants from <CoreGraphics/CGEventTypes.h>
|
|
const K_CG_EVENT_FLAG_MASK_SHIFT: u64 = 0x0002_0000;
|
|
const K_CG_EVENT_FLAG_MASK_CONTROL: u64 = 0x0004_0000;
|
|
// Option key
|
|
const K_CG_EVENT_FLAG_MASK_ALTERNATE: u64 = 0x0008_0000;
|
|
const K_CG_EVENT_FLAG_MASK_COMMAND: u64 = 0x0010_0000;
|
|
|
|
fn flags() -> u64 {
|
|
// SAFETY: CGEventSourceFlagsState is a stable, public CoreGraphics API
|
|
// available since macOS 10.4. Integer in, integer out, no pointers
|
|
// cross the boundary.
|
|
unsafe { CGEventSourceFlagsState(K_CG_EVENT_SOURCE_STATE_HID_SYSTEM_STATE) }
|
|
}
|
|
|
|
/// One CG syscall, all modifier bits decoded.
|
|
pub fn snapshot() -> super::ModifierState {
|
|
let f = flags();
|
|
super::ModifierState {
|
|
command: f & K_CG_EVENT_FLAG_MASK_COMMAND != 0,
|
|
option: f & K_CG_EVENT_FLAG_MASK_ALTERNATE != 0,
|
|
shift: f & K_CG_EVENT_FLAG_MASK_SHIFT != 0,
|
|
control: f & K_CG_EVENT_FLAG_MASK_CONTROL != 0,
|
|
}
|
|
}
|
|
|
|
#[cfg(all(test, target_os = "macos"))]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn smoke_test_modifier_detection() {
|
|
let s = snapshot();
|
|
let _ = s.command;
|
|
let _ = s.option;
|
|
let _ = s.shift;
|
|
let _ = s.control;
|
|
}
|
|
}
|