Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Federico Terzi 2020-06-14 18:02:09 +02:00
commit d19346e732
4 changed files with 66 additions and 18 deletions

View File

@ -87,6 +87,16 @@ void send_string(const char * string) {
// Send the event
// Check if the shift key is down, and if so, release it
// To see why: https://github.com/federico-terzi/espanso/issues/279
if (CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, 0x38)) {
CGEventRef e2 = CGEventCreateKeyboardEvent(NULL, 0x38, false);
CGEventPost(kCGHIDEventTap, e2);
CFRelease(e2);
usleep(2000);
}
// Because of a bug ( or undocumented limit ) of the CGEventKeyboardSetUnicodeString method
// the string gets truncated after 20 characters, so we need to send multiple events.

View File

@ -4,6 +4,11 @@
<dict>
<key>Label</key>
<string>com.federicoterzi.espanso</string>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>{{{PATH}}}</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>{{{espanso_path}}}</string>

View File

@ -61,6 +61,12 @@ pub fn register(_config_set: ConfigSet) {
espanso_path.to_str().unwrap_or_default(),
);
// Copy the user PATH variable and inject it in the Plist file so that
// it gets loaded by Launchd.
// To see why this is necessary: https://github.com/federico-terzi/espanso/issues/233
let user_path = std::env::var("PATH").unwrap_or("".to_owned());
let plist_content = plist_content.replace("{{{PATH}}}", &user_path);
std::fs::write(plist_file.clone(), plist_content).expect("Unable to write plist file");
println!("Entry created correctly!")

View File

@ -91,12 +91,6 @@ impl MacSystemManager {
/// Check whether an application is currently holding the Secure Input.
/// Return None if no application has claimed SecureInput, Some((AppName, AppPath)) otherwise.
pub fn get_secure_input_application() -> Option<(String, String)> {
use regex::Regex;
lazy_static! {
static ref APP_REGEX: Regex = Regex::new("/([^/]+).app/").unwrap();
};
unsafe {
let pid = MacSystemManager::get_secure_input_pid();
@ -112,26 +106,59 @@ impl MacSystemManager {
if let Ok(path) = string {
if !path.trim().is_empty() {
let process = path.trim().to_string();
let caps = APP_REGEX.captures(&process);
let app_name = if let Some(caps) = caps {
caps.get(1).map_or("", |m| m.as_str()).to_owned()
let app_name = if let Some(name) = Self::get_app_name_from_path(&process) {
name
} else {
process.to_owned()
};
Some((app_name, process))
} else {
None
return Some((app_name, process));
}
} else {
None
}
} else {
None
}
} else {
None
}
None
}
}
fn get_app_name_from_path(path: &str) -> Option<String> {
use regex::Regex;
lazy_static! {
static ref APP_REGEX: Regex = Regex::new("/([^/]+).(app|bundle)/").unwrap();
};
let caps = APP_REGEX.captures(&path);
if let Some(caps) = caps {
Some(caps.get(1).map_or("", |m| m.as_str()).to_owned())
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_app_name_from_path() {
let app_name = MacSystemManager::get_app_name_from_path("/Applications/iTerm.app/Contents/MacOS/iTerm2");
assert_eq!(app_name.unwrap(), "iTerm")
}
#[test]
fn test_get_app_name_from_path_no_app_name() {
let app_name = MacSystemManager::get_app_name_from_path("/another/directory");
assert!(app_name.is_none())
}
#[test]
fn test_get_app_name_from_path_security_bundle() {
let app_name = MacSystemManager::get_app_name_from_path("/System/Library/Frameworks/Security.framework/Versions/A/MachServices/SecurityAgent.bundle/Contents/MacOS/SecurityAgent");
assert_eq!(app_name.unwrap(), "SecurityAgent")
}
}