From a92c68fb9f2d67b8841bf63f94387a224b3c8d02 Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Sat, 6 Jun 2020 20:06:29 +0200 Subject: [PATCH] linux-capture: Capture windows by id first Previously we only captured by window name and class. This prevented capture of windows with the same name and class, and caused captures to switch from one window to another of the same name and class. --- plugins/linux-capture/xcompcap-main.cpp | 43 +++++++++++++++++-------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/plugins/linux-capture/xcompcap-main.cpp b/plugins/linux-capture/xcompcap-main.cpp index 1d7cd6df3..b1b9d23fb 100644 --- a/plugins/linux-capture/xcompcap-main.cpp +++ b/plugins/linux-capture/xcompcap-main.cpp @@ -218,33 +218,50 @@ static Window getWindowFromString(std::string wstr) } size_t firstMark = wstr.find(WIN_STRING_DIV); + size_t lastMark = wstr.rfind(WIN_STRING_DIV); size_t markSize = strlen(WIN_STRING_DIV); + // wstr only consists of the window-id if (firstMark == std::string::npos) return (Window)std::stol(wstr); - Window wid = 0; - - wstr = wstr.substr(firstMark + markSize); - - size_t lastMark = wstr.rfind(WIN_STRING_DIV); - std::string wname = wstr.substr(0, lastMark); + // wstr also contains window-name and window-class + std::string wid = wstr.substr(0, firstMark); + std::string wname = wstr.substr(firstMark + markSize, + lastMark - firstMark - markSize); std::string wcls = wstr.substr(lastMark + markSize); - Window matchedNameWin = wid; + Window winById = (Window)std::stol(wid); + + // first try to find a match by the window-id + for (Window cwin : XCompcap::getTopLevelWindows()) { + // match by window-id + if (cwin == winById) { + blog(LOG_INFO, "Found Window '%s' by Window-ID %s", + wname.c_str(), wid.c_str()); + + return cwin; + } + } + + // then try to find a match by name & class for (Window cwin : XCompcap::getTopLevelWindows()) { std::string cwinname = XCompcap::getWindowName(cwin); std::string ccls = XCompcap::getWindowClass(cwin); - if (cwin == wid && wname == cwinname && wcls == ccls) - return wid; + // match by name and class + if (wname == cwinname && wcls == ccls) { + blog(LOG_INFO, "Found Window '%s' by Name & Class", + wname.c_str()); - if (wname == cwinname || - (!matchedNameWin && !wcls.empty() && wcls == ccls)) - matchedNameWin = cwin; + return cwin; + } } - return matchedNameWin; + // no match + blog(LOG_DEBUG, "Did not find Window By ID %s, Name '%s' or Class '%s'", + wid.c_str(), wname.c_str(), wcls.c_str()); + return 0; } static void xcc_cleanup(XCompcapMain_private *p)