Fix #67, Fix #66, Fix #65

This commit is contained in:
Federico Terzi 2019-10-04 21:46:33 +02:00
parent 611819c106
commit 8305b494b1
3 changed files with 56 additions and 43 deletions

2
Cargo.lock generated
View File

@ -329,7 +329,7 @@ dependencies = [
[[package]] [[package]]
name = "espanso" name = "espanso"
version = "0.2.3" version = "0.2.4"
dependencies = [ dependencies = [
"backtrace 0.3.37 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "espanso" name = "espanso"
version = "0.2.3" version = "0.2.4"
authors = ["Federico Terzi <federicoterzi96@gmail.com>"] authors = ["Federico Terzi <federicoterzi96@gmail.com>"]
license = "GPL-3.0" license = "GPL-3.0"
description = "Cross-platform Text Expander written in Rust" description = "Cross-platform Text Expander written in Rust"

View File

@ -291,83 +291,96 @@ char *xwm_get_win_title(Display *disp, Window win)
} }
int32_t get_active_window_name(char * buffer, int32_t size) { 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; return -1;
} }
// Get the active window // Get the active window
Window win; Window win;
int revert_to_return; int ret = xdo_get_active_window(x, &win);
XGetInputFocus(disp, &win, &revert_to_return); 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 result;
return 1;
} }
int32_t get_active_window_class(char * buffer, int32_t size) { 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; return -1;
} }
// Get the active window // Get the active window
Window win; Window win;
int revert_to_return; int ret = xdo_get_active_window(x, &win);
XGetInputFocus(disp, &win, &revert_to_return); int result = 1;
if (ret) {
fprintf(stderr, "xdo_get_active_window reported an error\n");
result = -2;
}else{
XClassHint hint;
XClassHint hint; if (XGetClassHint(x->xdpy, win, &hint)) {
snprintf(buffer, size, "%s", hint.res_class);
if (XGetClassHint(disp, win, &hint)) { XFree(hint.res_name);
snprintf(buffer, size, "%s", hint.res_class); XFree(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) { 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; return -1;
} }
// Get the active window // Get the active window
Window win; Window win;
int revert_to_return; int ret = xdo_get_active_window(x, &win);
XGetInputFocus(disp, &win, &revert_to_return); 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 // Get the executable path from it
char *pid_raw = (char*)get_property(disp,win, XA_CARDINAL, "_NET_WM_PID", NULL); char proc_path[250];
if (pid_raw == NULL) { snprintf(proc_path, 250, "/proc/%d/exe", pid);
return -2;
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 return result;
char proc_path[250];
snprintf(proc_path, 250, "/proc/%d/exe", pid);
readlink(proc_path, buffer, size);
XFree(pid_raw);
XCloseDisplay(disp);
return 1;
} }
int32_t is_current_window_terminal() { int32_t is_current_window_terminal() {