hex/src/keyboard_input_sources/windows.rs
Dax 5c2b6b6580
fix windows annotate search: clicks, keys and click-away (#2113) (#2118)
A Rune overlay never takes the keyboard on Windows (its window is a no-activate
tool window, and Rune hands overlays the keyboard on macOS and Wayland only), and
Hex's Windows hooks never handed the finished capture capsule anything either. So
the search field's clicks fell through to the app in front, typing reached
nothing, and once the search opened there was no Escape, click-away or ✕ to
leave it.

- The search field is a native hit region on Windows (`.hit()`), so the window
  stops being click-through over it.
- The mouse hook reports `CapsuleKey::Press` while the capsule's list or
  suggestions are open, so a click away folds them and starts the hold that lets
  the capsule leave. The click is never consumed, and a press on the capsule's own
  boxes is its click.
- The keyboard hook hands the open list or the suggestions the keys
  `capture_host::list_key` names: text, ⌫, ↑↓, ⏎/⇧⏎, ⇥, Escape and Shift, and it
  keeps the caret's own keys (⌦, ←→, Home and End, Page Up and Page Down, Insert)
  from the app while the search is open. The layout is read only where the capsule
  can take text, with `ToUnicodeEx`'s "change no state" flag and a key state
  built from the hook's own Shift and Caps Lock, so a key that goes on to the app
  is never disturbed and a dead key types nothing. Only keys that edit text are
  taken: the lock, media, browser and launch keys, Print Screen, Sleep, Apps and
  function keys pass. Ctrl, Alt and the Windows key make a chord and stay the
  app's, and so do Hex's own shortcuts and Escape over the suggestions, as on the
  Mac.
- Every press the app gets is remembered until its release, wherever the capsule
  stood when it went down, so a key held while the search opens under it keeps its
  autorepeats and release with the app.

Checked by cross-compiling and by the hooks' unit tests; not observed on a real
Windows PC.
2026-11-20 20:24:28 -05:00

205 lines
7.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.

//! Keyboard layouts (HKLs) as input sources. Windows keeps a layout per
//! thread, so the current source is the keyboard-focused window's, and Hex's
//! own windows name none, so focusing one is not a change. Hex reads them
//! only while keyboard model switching is on.
use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::Globalization::{
GetLocaleInfoEx, LCIDToLocaleName, LOCALE_SLOCALIZEDDISPLAYNAME,
};
use windows_sys::Win32::System::Threading::GetCurrentProcessId;
use windows_sys::Win32::UI::Input::KeyboardAndMouse::{
GetKeyboardLayout, GetKeyboardLayoutList, HKL,
};
use windows_sys::Win32::UI::WindowsAndMessaging::{
GUITHREADINFO, GetForegroundWindow, GetGUIThreadInfo, GetWindowThreadProcessId,
};
use crate::transcription::keyboard_model_switching::{InputSource, source_from_window, source_id};
const LOCALE_NAME_MAX_LENGTH: usize = 85;
const LAYOUTS_KEY: &str = r"SYSTEM\CurrentControlSet\Control\Keyboard Layouts";
/// Every Windows session has keyboard layouts.
pub(crate) fn available() -> bool {
true
}
/// The HKL's 32 bits (x64 sign-extends them) as its stable ID, `04190419`.
fn id(layout: HKL) -> String {
source_id(layout as usize as u32)
}
/// The keyboard-focused window's layout; `None` while one of Hex's own
/// windows has the focus, without a window, as on the lock screen, or for a
/// console that reports none.
pub(crate) fn current() -> Option<String> {
// SAFETY: plain Win32 queries; `focused_window` never yields null, and a
// window destroyed since that read reports no thread and no process.
unsafe {
let window = focused_window()?;
let mut process_id = 0;
let thread = GetWindowThreadProcessId(window, &mut process_id);
// A window destroyed in between owns no thread, and
// `GetKeyboardLayout(0)` would answer for Hex's own runtime thread.
if thread == 0 {
return None;
}
let layout = GetKeyboardLayout(thread);
source_from_window(process_id, GetCurrentProcessId(), layout as usize as u32)
}
}
/// The keyboard layout of the window with keyboard focus, for reading what a
/// key types there; this thread's layout while no window has the focus.
pub(crate) fn foreground_layout() -> HKL {
// SAFETY: plain Win32 queries; a window destroyed since the read reports
// no thread, and `GetKeyboardLayout(0)` answers for this thread then.
unsafe {
if let Some(window) = focused_window() {
let mut process_id = 0;
let thread = GetWindowThreadProcessId(window, &mut process_id);
if thread != 0 {
return GetKeyboardLayout(thread);
}
}
GetKeyboardLayout(0)
}
}
/// The window with keyboard focus: the foreground thread's focused window,
/// else its active one, else the foreground window. `GetGUIThreadInfo` fails
/// when the foreground thread is out of reach, so the last is a fallback.
fn focused_window() -> Option<HWND> {
// SAFETY: `GetGUIThreadInfo` fills the caller-sized struct for the
// foreground thread; a failure leaves its windows null.
unsafe {
let mut info = GUITHREADINFO {
cbSize: size_of::<GUITHREADINFO>() as u32,
..Default::default()
};
if GetGUIThreadInfo(0, &mut info) != 0
&& let Some(window) = non_null(info.hwndFocus).or_else(|| non_null(info.hwndActive))
{
return Some(window);
}
non_null(GetForegroundWindow())
}
}
/// A window handle as an option; Win32 uses null for none.
fn non_null(window: HWND) -> Option<HWND> {
(!window.is_null()).then_some(window)
}
/// The layouts in the user's language bar.
pub(crate) fn enabled() -> Vec<InputSource> {
// SAFETY: the first call sizes the buffer the second fills.
let layouts = unsafe {
let count = GetKeyboardLayoutList(0, std::ptr::null_mut());
let mut layouts = vec![std::ptr::null_mut(); count.max(0) as usize];
let filled = GetKeyboardLayoutList(count, layouts.as_mut_ptr());
layouts.truncate(filled.max(0) as usize);
layouts
};
let mut sources: Vec<InputSource> = layouts
.into_iter()
.map(|layout| {
let bits = layout as usize as u32;
let locale = locale_name(bits & 0xFFFF);
let language = locale.as_deref().and_then(display_name);
let keyboard = layout_text(bits);
let name = match (language, keyboard) {
(Some(language), Some(keyboard)) => format!("{language} – {keyboard}"),
(Some(name), None) | (None, Some(name)) => name,
(None, None) => id(layout),
};
InputSource {
id: id(layout),
name,
languages: locale.into_iter().collect(),
}
})
.collect();
sources.sort_by(|a, b| a.name.cmp(&b.name).then(a.id.cmp(&b.id)));
sources.dedup_by(|a, b| a.id == b.id);
sources
}
/// BCP 47 for a language ID, `ru-RU`.
fn locale_name(language: u32) -> Option<String> {
let mut buffer = [0u16; LOCALE_NAME_MAX_LENGTH];
// SAFETY: the buffer holds LOCALE_NAME_MAX_LENGTH characters.
let length = unsafe { LCIDToLocaleName(language, buffer.as_mut_ptr(), buffer.len() as i32, 0) };
wide(&buffer, length)
}
/// The locale's name in the display language, `Russian (Russia)`.
fn display_name(locale: &str) -> Option<String> {
let locale: Vec<u16> = locale.encode_utf16().chain([0]).collect();
let mut buffer = [0u16; 256];
// SAFETY: both buffers are NUL-terminated or sized as passed.
let length = unsafe {
GetLocaleInfoEx(
locale.as_ptr(),
LOCALE_SLOCALIZEDDISPLAYNAME,
buffer.as_mut_ptr(),
buffer.len() as i32,
)
};
wide(&buffer, length)
}
/// A written length that counts the NUL.
fn wide(buffer: &[u16], length: i32) -> Option<String> {
let length = usize::try_from(length).ok()?.checked_sub(1)?;
let text = String::from_utf16_lossy(buffer.get(..length)?);
(!text.is_empty()).then_some(text)
}
/// The keyboard's own name, `Russian` or `United States-Dvorak`, from the
/// registry entry its HKL's high word selects: an IME's whole HKL, a
/// `Layout Id` for `0xFnnn`, or else the layout's language ID.
fn layout_text(layout: u32) -> Option<String> {
use winreg::RegKey;
use winreg::enums::HKEY_LOCAL_MACHINE;
let layouts = RegKey::predef(HKEY_LOCAL_MACHINE)
.open_subkey(LAYOUTS_KEY)
.ok()?;
let device = layout >> 16;
let klid = match device & 0xF000 {
0xE000 => format!("{layout:08X}"),
0xF000 => {
let wanted = device & 0x0FFF;
layouts.enum_keys().flatten().find(|klid| {
layouts
.open_subkey(klid)
.and_then(|key| key.get_value::<String, _>("Layout Id"))
.is_ok_and(|id| u32::from_str_radix(id.trim(), 16) == Ok(wanted))
})?
}
_ => format!("{device:08X}"),
};
layouts
.open_subkey(klid)
.and_then(|key| key.get_value::<String, _>("Layout Text"))
.ok()
.filter(|text| !text.is_empty())
}
/// Polled by the runtime while switching is on; reports the first source
/// once, like the macOS observer, then only changes.
#[derive(Default)]
pub(crate) struct Watcher {
last: Option<String>,
}
impl Watcher {
pub(crate) fn poll(&mut self) -> Option<Option<String>> {
let current = current()?;
(self.last.as_ref() != Some(&current)).then(|| {
self.last = Some(current.clone());
Some(current)
})
}
}