From 40ad3c85517e9e000891ac7df12117df1370b0a1 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 1 Aug 2021 23:09:09 +0200 Subject: [PATCH] fix(info): use alternative method to gather active window on X11 --- espanso-info/src/x11/native.cpp | 40 ++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/espanso-info/src/x11/native.cpp b/espanso-info/src/x11/native.cpp index 12faaad..910d345 100644 --- a/espanso-info/src/x11/native.cpp +++ b/espanso-info/src/x11/native.cpp @@ -88,6 +88,22 @@ char *xwm_get_win_title(Display *disp, Window win) return nwname ? nwname : (wname ? wname : NULL); } +// Function taken from the wmctrl tool source code +Window get_active_window(Display *disp) { + char *prop; + unsigned long size; + Window ret = (Window)0; + + prop = get_property(disp, DefaultRootWindow(disp), XA_WINDOW, + "_NET_ACTIVE_WINDOW", &size); + if (prop) { + ret = *((Window*)prop); + XFree(prop); + } + + return(ret); +} + int32_t info_get_title(char *buffer, int32_t buffer_size) { Display *display = XOpenDisplay(0); @@ -97,14 +113,12 @@ int32_t info_get_title(char *buffer, int32_t buffer_size) return -1; } - Window focused; - int revert_to; - int ret = XGetInputFocus(display, &focused, &revert_to); + Window focused = get_active_window(display); int result = 1; - if (!ret) + if (!focused) { - fprintf(stderr, "xdo_get_active_window reported an error\n"); + fprintf(stderr, "get_active_window reported an error\n"); result = -2; } else @@ -130,14 +144,12 @@ int32_t info_get_exec(char *buffer, int32_t buffer_size) return -1; } - Window focused; - int revert_to; - int ret = XGetInputFocus(display, &focused, &revert_to); + Window focused = get_active_window(display); int result = 1; - if (!ret) + if (!focused) { - fprintf(stderr, "xdo_get_active_window reported an error\n"); + fprintf(stderr, "get_active_window reported an error\n"); result = -2; } else @@ -176,14 +188,12 @@ int32_t info_get_class(char *buffer, int32_t buffer_size) return -1; } - Window focused; - int revert_to; - int ret = XGetInputFocus(display, &focused, &revert_to); + Window focused = get_active_window(display); int result = 1; - if (!ret) + if (!focused) { - fprintf(stderr, "xdo_get_active_window reported an error\n"); + fprintf(stderr, "get_active_window reported an error\n"); result = -2; } else