diff --git a/Cargo.lock b/Cargo.lock index a86b558..2e16150 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -329,7 +329,7 @@ dependencies = [ [[package]] name = "espanso" -version = "0.2.3" +version = "0.2.4" dependencies = [ "backtrace 0.3.37 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index a6716ec..caa07c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "espanso" -version = "0.2.3" +version = "0.2.4" authors = ["Federico Terzi "] license = "GPL-3.0" description = "Cross-platform Text Expander written in Rust" diff --git a/ci/build-linux.yml b/ci/build-linux.yml index 6fb6721..a8e2dd8 100644 --- a/ci/build-linux.yml +++ b/ci/build-linux.yml @@ -5,5 +5,6 @@ steps: tar czf "espanso-linux.tar.gz" espanso cd ../.. cp target/release/espanso-*.gz . + sha256sum espanso-*.gz | awk '{ print $1 }' > espanso-linux-sha256.txt ls -la displayName: "Cargo build and packaging for Linux" \ No newline at end of file diff --git a/native/liblinuxbridge/bridge.cpp b/native/liblinuxbridge/bridge.cpp index 6cb4bf8..3f6369e 100644 --- a/native/liblinuxbridge/bridge.cpp +++ b/native/liblinuxbridge/bridge.cpp @@ -291,83 +291,96 @@ char *xwm_get_win_title(Display *disp, Window win) } int32_t get_active_window_name(char * buffer, int32_t size) { - Display *disp = XOpenDisplay(NULL); + xdo_t * x = xdo_new(NULL); - if (!disp) { + if (!x) { return -1; } // Get the active window Window win; - int revert_to_return; - XGetInputFocus(disp, &win, &revert_to_return); + int ret = xdo_get_active_window(x, &win); + int result = 1; + if (ret) { + fprintf(stderr, "xdo_get_active_window reported an error\n"); + result = -2; + }else{ + char * title = xwm_get_win_title(x->xdpy, win); - char * title = xwm_get_win_title(disp, win); + snprintf(buffer, size, "%s", title); - snprintf(buffer, size, "%s", title); + XFree(title); + } - XFree(title); + xdo_free(x); - XCloseDisplay(disp); - - return 1; + return result; } int32_t get_active_window_class(char * buffer, int32_t size) { - Display *disp = XOpenDisplay(NULL); + xdo_t * x = xdo_new(NULL); - if (!disp) { + if (!x) { return -1; } // Get the active window Window win; - int revert_to_return; - XGetInputFocus(disp, &win, &revert_to_return); + int ret = xdo_get_active_window(x, &win); + int result = 1; + if (ret) { + fprintf(stderr, "xdo_get_active_window reported an error\n"); + result = -2; + }else{ + XClassHint hint; - XClassHint hint; - - if (XGetClassHint(disp, win, &hint)) { - snprintf(buffer, size, "%s", hint.res_class); - XFree(hint.res_name); - XFree(hint.res_class); + if (XGetClassHint(x->xdpy, win, &hint)) { + snprintf(buffer, size, "%s", hint.res_class); + XFree(hint.res_name); + XFree(hint.res_class); + } } - XCloseDisplay(disp); + xdo_free(x); - return 1; + return result; } int32_t get_active_window_executable(char *buffer, int32_t size) { - Display *disp = XOpenDisplay(NULL); + xdo_t * x = xdo_new(NULL); - if (!disp) { + if (!x) { return -1; } // Get the active window Window win; - int revert_to_return; - XGetInputFocus(disp, &win, &revert_to_return); + int ret = xdo_get_active_window(x, &win); + int result = 1; + if (ret) { + fprintf(stderr, "xdo_get_active_window reported an error\n"); + result = -2; + }else{ + // Get the window process PID + char *pid_raw = (char*)get_property(x->xdpy,win, XA_CARDINAL, "_NET_WM_PID", NULL); + if (pid_raw == NULL) { + result = -3; + }else{ + int pid = pid_raw[0] | pid_raw[1] << 8 | pid_raw[2] << 16 | pid_raw[3] << 24; - // Get the window process PID - char *pid_raw = (char*)get_property(disp,win, XA_CARDINAL, "_NET_WM_PID", NULL); - if (pid_raw == NULL) { - return -2; + // Get the executable path from it + char proc_path[250]; + snprintf(proc_path, 250, "/proc/%d/exe", pid); + + readlink(proc_path, buffer, size); + + XFree(pid_raw); + } } - int pid = pid_raw[0] | pid_raw[1] << 8 | pid_raw[2] << 16 | pid_raw[3] << 24; + xdo_free(x); - // Get the executable path from it - char proc_path[250]; - snprintf(proc_path, 250, "/proc/%d/exe", pid); - - readlink(proc_path, buffer, size); - - XFree(pid_raw); - XCloseDisplay(disp); - - return 1; + return result; } int32_t is_current_window_terminal() { diff --git a/src/main.rs b/src/main.rs index 2cd9e96..0620c70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,6 +72,11 @@ fn main() { .arg(Arg::with_name("package_name") .help("Package name")); + let uninstall_subcommand = SubCommand::with_name("uninstall") + .about("Remove an installed package. Equivalent to 'espanso package uninstall'") + .arg(Arg::with_name("package_name") + .help("Package name")); + let mut clap_instance = App::new("espanso") .version(VERSION) .author("Federico Terzi") @@ -122,19 +127,18 @@ fn main() { .subcommand(SubCommand::with_name("package") .about("Espanso package manager commands") .subcommand(install_subcommand.clone()) + .subcommand(uninstall_subcommand.clone()) .subcommand(SubCommand::with_name("list") .about("List all installed packages") .arg(Arg::with_name("full") .help("Print all package info") .long("full"))) - .subcommand(SubCommand::with_name("remove") - .about("Remove an installed package") - .arg(Arg::with_name("package_name") - .help("Package name"))) + .subcommand(SubCommand::with_name("refresh") .about("Update espanso package index")) ) - .subcommand(install_subcommand); + .subcommand(install_subcommand) + .subcommand(uninstall_subcommand); let matches = clap_instance.clone().get_matches(); @@ -223,12 +227,17 @@ fn main() { return; } + if let Some(matches) = matches.subcommand_matches("uninstall") { + remove_package_main(config_set, matches); + return; + } + if let Some(matches) = matches.subcommand_matches("package") { if let Some(matches) = matches.subcommand_matches("install") { install_main(config_set, matches); return; } - if let Some(matches) = matches.subcommand_matches("remove") { + if let Some(matches) = matches.subcommand_matches("uninstall") { remove_package_main(config_set, matches); return; }