aboutsummaryrefslogtreecommitdiff
path: root/src/platform
diff options
context:
space:
mode:
Diffstat (limited to 'src/platform')
-rw-r--r--src/platform/macos.rs28
-rw-r--r--src/platform/mod.rs6
2 files changed, 34 insertions, 0 deletions
diff --git a/src/platform/macos.rs b/src/platform/macos.rs
index 3770a3be..e72d32fe 100644
--- a/src/platform/macos.rs
+++ b/src/platform/macos.rs
@@ -97,6 +97,34 @@ extern "C" {
buffer_size: CfIndex,
encoding: u32,
) -> Boolean;
+
+ #[link_name = "kCFRunLoopDefaultMode"]
+ static CF_RUN_LOOP_DEFAULT_MODE: CfStringRef;
+
+ #[link_name = "CFRunLoopRunInMode"]
+ fn cf_run_loop_run_in_mode(
+ mode: CfStringRef,
+ seconds: f64,
+ return_after_source_handled: Boolean,
+ ) -> libc::c_int;
+}
+
+/// Pump the main thread's run loop once (non-blocking) so the process receives the
+/// `kTISNotifySelectedKeyboardInputSourceChanged` notification and refreshes the per-process cache
+/// that `TISCopyCurrentKeyboardInputSource` reads. That notification arrives only via the main
+/// thread's run loop, so a process that never runs a CFRunLoop (the headless server) reads a stale
+/// source. Must run on the main thread.
+pub(crate) fn pump_input_source_runloop() {
+ debug_assert!(
+ // SAFETY: `pthread_main_np` is always safe to call.
+ unsafe { libc::pthread_main_np() } != 0,
+ "pump_input_source_runloop must run on the main thread"
+ );
+ // SAFETY: `CFRunLoopRunInMode` is thread-safe; a 0-second call drains the ready sources and
+ // returns immediately (no blocking). `CF_RUN_LOOP_DEFAULT_MODE` is a framework-owned constant.
+ unsafe {
+ let _ = cf_run_loop_run_in_mode(CF_RUN_LOOP_DEFAULT_MODE, 0.0, 0);
+ }
}
#[derive(Debug)]
diff --git a/src/platform/mod.rs b/src/platform/mod.rs
index 169b7c1c..889c4ce9 100644
--- a/src/platform/mod.rs
+++ b/src/platform/mod.rs
@@ -154,6 +154,9 @@ pub(crate) fn switch_to_ascii_input_source() -> Option<InputSourceRestore> {
None
}
+#[cfg(not(target_os = "macos"))]
+pub(crate) fn pump_input_source_runloop() {}
+
/// Switches the host keyboard input source while prefix mode is active.
///
/// `App` drives this through a trait so the prefix-mode transitions can be
@@ -179,6 +182,9 @@ pub(crate) struct RealPrefixInputSource {
impl PrefixInputSource for RealPrefixInputSource {
fn switch_to_ascii(&mut self) {
if self.restore.is_none() {
+ // Drain pending input-source-change notifications so the read below is fresh (see
+ // `pump_input_source_runloop`); a no-op on non-macOS.
+ pump_input_source_runloop();
self.restore = switch_to_ascii_input_source();
}
}