diff --git a/native/libmacbridge/bridge.h b/native/libmacbridge/bridge.h index d6ff506..9a841da 100644 --- a/native/libmacbridge/bridge.h +++ b/native/libmacbridge/bridge.h @@ -42,7 +42,19 @@ void send_vkey(int32_t vk); /* * Send the backspace keypress, *count* times. */ -extern "C" void delete_string(int32_t count); +void delete_string(int32_t count); + +// SYSTEM + +/* + * Return the active NSRunningApplication path + */ +int32_t get_active_app_bundle(char * buffer, int32_t size); + +/* + * Return the active NSRunningApplication bundle identifier + */ +int32_t get_active_app_identifier(char * buffer, int32_t size); }; diff --git a/native/libmacbridge/bridge.mm b/native/libmacbridge/bridge.mm index 898c0e0..237de23 100644 --- a/native/libmacbridge/bridge.mm +++ b/native/libmacbridge/bridge.mm @@ -96,4 +96,24 @@ void send_vkey(int32_t vk) { usleep(2000); }); +} + +int32_t get_active_app_bundle(char * buffer, int32_t size) { + NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; + NSString *bundlePath = [frontApp bundleURL].path; + const char * path = [bundlePath UTF8String]; + + snprintf(buffer, size, "%s", path); + + return 1; +} + +int32_t get_active_app_identifier(char * buffer, int32_t size) { + NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; + NSString *bundlePath = frontApp.bundleIdentifier; + const char * path = [bundlePath UTF8String]; + + snprintf(buffer, size, "%s", path); + + return 1; } \ No newline at end of file diff --git a/src/bridge/macos.rs b/src/bridge/macos.rs index cbd0552..5181608 100644 --- a/src/bridge/macos.rs +++ b/src/bridge/macos.rs @@ -3,6 +3,11 @@ use std::os::raw::{c_void, c_char}; #[allow(improper_ctypes)] #[link(name="macbridge", kind="static")] extern { + // System + pub fn get_active_app_bundle(buffer: *mut c_char, size: i32) -> i32; + pub fn get_active_app_identifier(buffer: *mut c_char, size: i32) -> i32; + + // Keyboard pub fn register_keypress_callback(s: *const c_void, cb: extern fn(_self: *mut c_void, *const u8, i32, i32, i32)); diff --git a/src/config.rs b/src/config.rs index 48bb366..e25e299 100644 --- a/src/config.rs +++ b/src/config.rs @@ -240,6 +240,24 @@ impl ConfigManager for RuntimeConfigManager { }*/ } + let active_calss = self.system_manager.get_current_window_class(); + + if let Some(class) = active_calss { + debug!("=> Class: '{}'", class); + + // TODO + /*for (i, regex) in self.title_regexps.iter().enumerate() { + if let Some(regex) = regex { + if regex.is_match(&title) { + debug!("Matched 'filter_title' for '{}' config, using custom settings.", + self.set.specific[i].name); + + return &self.set.specific[i] + } + } + }*/ + } + // No matches, return the default mapping debug!("No matches for custom configs, using default settings."); &self.set.default diff --git a/src/system/macos.rs b/src/system/macos.rs index 0b5532b..0c5746e 100644 --- a/src/system/macos.rs +++ b/src/system/macos.rs @@ -1,6 +1,7 @@ use std::os::raw::c_char; use std::ffi::CStr; +use crate::bridge::macos::{get_active_app_bundle, get_active_app_identifier}; pub struct MacSystemManager { @@ -8,15 +9,43 @@ pub struct MacSystemManager { impl super::SystemManager for MacSystemManager { fn get_current_window_title(&self) -> Option { - unimplemented!() + self.get_current_window_class() } fn get_current_window_class(&self) -> Option { - unimplemented!(); + unsafe { + let mut buffer : [c_char; 250] = [0; 250]; + let res = get_active_app_identifier(buffer.as_mut_ptr(), buffer.len() as i32); + + if res > 0 { + let c_string = CStr::from_ptr(buffer.as_ptr()); + + let string = c_string.to_str(); + if let Ok(string) = string { + return Some((*string).to_owned()); + } + } + } + + None } fn get_current_window_executable(&self) -> Option { - unimplemented!() + unsafe { + let mut buffer : [c_char; 250] = [0; 250]; + let res = get_active_app_bundle(buffer.as_mut_ptr(), buffer.len() as i32); + + if res > 0 { + let c_string = CStr::from_ptr(buffer.as_ptr()); + + let string = c_string.to_str(); + if let Ok(string) = string { + return Some((*string).to_owned()); + } + } + } + + None } }