Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MacOS: set_is_main_thread #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cocoa = "0.22"
core-graphics = {version = "0.19.0", features = ["highsierra"]}
core-foundation = {version = "0.7"}
core-foundation-sys = {version = "0.7"}
dispatch = "0.2"


[target.'cfg(target_os = "linux")'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub use crate::rdev::{
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use crate::macos::Keyboard;
pub use crate::macos::{set_is_main_thread, Keyboard};
#[cfg(target_os = "macos")]
use crate::macos::{display_size as _display_size, listen as _listen, simulate as _simulate};

Expand Down
4 changes: 4 additions & 0 deletions src/macos/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub type QCallback = unsafe extern "C" fn(
user_info: *mut c_void,
) -> CGEventRef;

pub fn set_is_main_thread(b: bool) {
KEYBOARD_STATE.lock().unwrap().set_is_main_thread(b);
}

pub unsafe fn convert(
_type: CGEventType,
cg_event: &CGEvent,
Expand Down
20 changes: 19 additions & 1 deletion src/macos/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ static NSEventModifierFlagCommand: u64 = 1 << 20;

const BUF_LEN: usize = 4;

lazy_static::lazy_static! {
static ref QUEUE: dispatch::Queue = dispatch::Queue::main();
}

#[cfg(target_os = "macos")]
#[link(name = "Cocoa", kind = "framework")]
#[link(name = "Carbon", kind = "framework")]
Expand All @@ -56,19 +60,25 @@ extern "C" {
}

pub struct Keyboard {
is_main_thread: bool,
dead_state: u32,
shift: bool,
caps_lock: bool,
}
impl Keyboard {
pub fn new() -> Option<Keyboard> {
Some(Keyboard {
is_main_thread: true,
dead_state: 0,
shift: false,
caps_lock: false,
})
}

pub fn set_is_main_thread(&mut self, b: bool) {
self.is_main_thread = b;
}

fn modifier_state(&self) -> ModifierState {
if self.caps_lock || self.shift {
2
Expand All @@ -83,7 +93,15 @@ impl Keyboard {
flags: CGEventFlags,
) -> Option<String> {
let modifier_state = flags_to_state(flags.bits());
self.string_from_code(code, modifier_state)

if self.is_main_thread {
self.string_from_code(code, modifier_state)
} else {
QUEUE.exec_sync(move || {
// ignore all modifiers for name
self.string_from_code(code, modifier_state)
})
}
}

pub(crate) unsafe fn string_from_code(
Expand Down
1 change: 1 addition & 0 deletions src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod keycodes;
mod listen;
mod simulate;

pub use crate::macos::common::set_is_main_thread;
pub use crate::macos::display::display_size;
#[cfg(feature = "unstable_grab")]
pub use crate::macos::grab::grab;
Expand Down