我只想在窗口对用户不可见时执行某个操作xdotool
,这包括最小化的窗口,还包括 100% 被其他窗口覆盖的窗口(如果至少不使用透明度)。忽略透明度问题有没有简单的方法可以做到这一点?
xdotool
有一个--onlyvisible
选项,但只包括最小化的窗口,不包括覆盖的窗口。当然,可以选择循环遍历所有可见窗口,获取它们的窗口几何形状并计算它们覆盖了多少感兴趣的窗口,但我确实希望有一个比在 bash 中执行此操作更简单、更快的解决方案。
这里有一个好的问题的说明,但它只是列出窗口,也适用于 Max OS X。这问题只有一个提示的答案,但没有显示如何通过列出所有可见窗口及其各自的 z 顺序并手动计算可见区域来做到这一点。
答案1
为了完整起见,这里是天真的/暴力解决方案,我希望它已经在其他一些实用程序中实现。评论中@Gilles 链接中的通知事件fullyobscured
听起来很有希望,但我不确定如何让它发挥作用,而且这个解决方案实施起来也很有趣。
该脚本只是计算所有重叠窗口的覆盖面积减去重复计算的面积,并检查它是否与窗口面积一样大。因为它正确地包含了框架边框,所以代码看起来比实际要复杂一些。如果完全覆盖则返回退出代码 0,否则返回退出代码 1。它采用窗口 ID 作为参数。例如调用它if xcovered 0x1a00003; then echo 'covered!'; fi
不包括注释、调试注释和错误检查,它可能只有 40 行长,甚至更少。我实际上想使用 bc 而不是 python,但我找不到一种简单的方法将 bash 数组传输到 bc 数组。
#!/bin/bash
# Name: xcovered.sh
# Find out if C is completely or only partially covered by A or B
# +-----------+
# | +===+ |
# | | +-------+
# | C | | B |
# | | +-------+
# +---| A |---+
# +---+
# @return 0 if window ist not visible, 1 if visible
# Note: Only tested with three windows like in sketch above, but
# it should also work for an arbitrary amount of overlapping windwows
wid=$1
if ! xwininfo -id $wid -stats | 'grep' -q 'IsViewable'; then return 0; fi
# get all stacked window ids after and including the given wid
wids=($(xprop -root | 'sed' -nE "/_NET_CLIENT_LIST_STACKING\(WINDOW\)/{ s|.*($wid)|\1|; s|,||g; p }"))
if [ ${#wids} -eq 0 ]; then
echo -e "\e[31mCouldn't find specified window id $wid in _NET_CLIENT_LIST_STACKING(WINDOW)"'!'"\e[0m"
return 2
fi
if [ ${#wids} -eq 1 ]; then return 0; fi
# Gather geometry of all windows in higher zorder / possibly lying on top
coords=(); frames=()
for owid in ${wids[@]}; do
#xwininfo -id $owid | grep xwininfo
if xwininfo -id $owid -stats | 'grep' -q 'IsViewable'; then
# _NET_WM_ICON_GEOMETRY doesn't exist for xfce4-panel, thereby making this more difficult
#coords=$(xprop -id $owid _NET_WM_ICON_GEOMETRY)
#frames=$(xprop -id $owid _NET_FRAME_EXTENTS)
x=($(xwininfo -id $owid -stats -wm | sed -nE '
s|^[ \t]*Absolute upper-left X:[ \t]*([0-9]+).*|\1|Ip;
s|^[ \t]*Absolute upper-left Y:[ \t]*([0-9]+).*|\1|Ip;
s|^[ \t]*Width:[ \t]*([0-9]+).*|\1|Ip;
s|^[ \t]*Height:[ \t]*([0-9]+).*|\1|Ip;
/Frame extents:/I{ s|^[ \t}Frame Extents:[ \t]*||I; s|,||g; p; };
' | sed ':a; N; $!b a; s/\n/ /g '))
if [ ! ${#x[@]} -eq 8 ]; then
echo -e "\e[31mSomething went wrong when parsing the output of 'xwininfo -id $owid -stats -wm':\e[0m"
xwininfo -id $owid -stats -wm
exit 1
fi
# apply the frame width to the coordinates and window width
# 0:x 1:y 2:w 3:h, border widths 4:left 5:right 6:top 7:bottom
coords+=( "${x[0]}-${x[4]}, ${x[1]}-${x[6]}, ${x[2]}+${x[4]}+${x[5]}, ${x[3]}+${x[6]}+${x[7]}" )
fi
done
IFS=','; python - <<EOF #| python
# Calculates the area of the union of all overlapping areas. If that area
# is equal to the window of interest area / size, then the window is covered.
# Note that the calcualted area can't be larger than that!
# 1
# D---C => overlap given by H and B
# | H-|---G x-overlap: max(0, xleft2-xright1)
# A---B | -> '1' and '2' is not known, that's why for left and right
# | 2 | use min, each
# E-----F -> max(0, min(xright1,xright2) - max(xleft1,xleft2) )
# Note that because of xleft<xright this can only
# result in xright1-xleft2 or xright2-xleft1
# All cases: 1 | +--+ | +--+ | +--+ | +--+ |
# 2 | +--+ | +--+ | +--+ | +--+ |
# overlap | 0 | 2 | 2 | 0 |
def overlap( x1,y1,w1,h1, x2,y2,w2,h2, x3=0,y3=0,w3=65535,h3=65535 ):
return max( 0, min(x1+w1,x2+w2,x3+w3) - max(x1,x2,x3) ) * \
max( 0, min(y1+h1,y2+h2,y3+h3) - max(y1,y2,y3) )
x=[ ${coords[*]} ]
area=0
# Calculate overlap with window in question
# 0:x 1:y 2:w 3:h, border widths 0:left 1:right 2:top 3:bottom
for i in range( 4,len(x),4 ):
area += overlap( *( x[0:4]+x[i:i+4] ) )
# subtract double counted areas i.e. areas overlapping to the window
# of interest and two other windows on top ... This is n**2
for i in range( 4,len(x),4 ):
for j in range( i+4,len(x),4 ):
area -= overlap( *( x[0:4]+x[i:i+4]+x[j:j+4] ) )
print "area =",area
print "woi =",x[2]*x[3]
# exit code 0: if not fully covered, 1: if fully covered
exit( area < x[2]*x[3] )
EOF
exit $?
答案2
@mxmlnkn 给出的答案是一个很好的开始,但不幸的是,当目标窗口顶部有 3 个或更多窗口时,计算覆盖面积的方法并不正确。
要了解原因,请假设目标窗口顶部有 3 个窗口(我们将其称为X
、Y
和) ;进一步假设所有这些窗口具有相同的坐标。然后给定的解决方案将首先添加,然后减去,从而得出净面积计算结果。这里的错误是我们实际上是在“重复计算”,试图补偿“重复计算”。为了纠正这个问题,我们添加了.这个概念通过包含-排除原则被形式化(参见Z
T
|X ∩ T|+|Y ∩ T|+|Z ∩ T|=3*windowArea
|(X U Y) ∩ T| + |(X U Z) ∩ T| + |(Y U Z) ∩ T| =3*windowArea
0
|X ∩ Y ∩ Z ∩ T|
这里)。
“覆盖区域”可以定义为(请原谅随意的数学符号,unix.stackexchange.com
不允许LaTeX
)
(A_1 U A_2 U ... U A_n) ∩ B
其中A_1, A_2, ..., A_n
是位于目标窗口顶部的窗口,B
是目标窗口。
我们可以用包含排除原理来扩展(A_1 U A_2 U ... U A_n)
。然后我们可以将交集分布B
到这个结果上。
具体来说,这会产生以下算法(C++):
bool windowIsVisible(Display *display, Window window, float threshold) {
// Indicates whether a window is fully covered
if (!windowIsViewable(display, window)) {
return false;
}
auto rootWindow = DefaultRootWindow(display);
auto coords = getWindowCoords(display, rootWindow, window);
if (coords.size() <= 1) {
return true;
}
float area = (coords[0][2]-coords[0][0]) * (coords[0][3]-coords[0][1]);
float coveredArea = 0;
auto selector = std::vector<bool>(coords.size()-1);
for (int i = 0; i < selector.size(); i++) {
std::fill(selector.begin(), selector.begin()+i+1, true);
std::fill(selector.begin()+i+1, selector.end(), false);
auto selectedWindows = std::vector<std::vector<int>>(i);
do {
selectedWindows.clear();
for (int j = 0; j < selector.size(); j++) {
if (selector[j]) selectedWindows.push_back(coords[j+1]);
}
selectedWindows.push_back(coords[0]);
coveredArea += pow(-1, i)*calculateWindowOverlap(selectedWindows);
} while (std::prev_permutation(selector.begin(), selector.end()));
}
float tol = 1e-4;
return (1 - ((float)coveredArea)/((float)area) + tol) >= threshold;
}
int calculateWindowOverlap(std::vector<std::vector<int>> windowCoords) {
if (windowCoords.size() == 0) {
return 0;
}
std::vector<int> intersect = windowCoords[0];
for (int i = 1; i < windowCoords.size(); i++) {
intersect[0] = std::max(intersect[0], windowCoords[i][0]);
intersect[1] = std::max(intersect[1], windowCoords[i][1]);
intersect[2] = std::min(intersect[2], windowCoords[i][2]);
intersect[3] = std::min(intersect[3], windowCoords[i][3]);
}
return std::max(0, intersect[2]-intersect[0]) *
std::max(0, intersect[3]-intersect[1]);
}
std::vector<std::vector<int>> getWindowCoords(Display *display,
Window queryWindow, Window targetWindow,
bool *reachedTargetPtr = nullptr, int absX = 0, int absY = 0) {
// Gather geometry of all windows in higher zorder
std::vector<std::vector<int>> coords = {};
bool reachedTarget = false;
if (!reachedTargetPtr) {
reachedTargetPtr = &reachedTarget;
}
Window rWindow;
Window parentWindow;
Window *childrenWindows;
unsigned int numChildren;
XQueryTree(display, queryWindow, &rWindow, &parentWindow,
&childrenWindows, &numChildren);
for (int i = 0; i < numChildren; i++) {
if (childrenWindows[i] == targetWindow) {
*reachedTargetPtr = true;
}
XWindowAttributes windowAttributes;
XGetWindowAttributes(display, childrenWindows[i], &windowAttributes);
if (*reachedTargetPtr && windowAttributes.map_state == IsViewable &&
windowAttributes.c_class != InputOnly) {
coords.push_back(std::vector<int> {
windowAttributes.x + absX,
windowAttributes.y + absY,
windowAttributes.x + absX + windowAttributes.width,
windowAttributes.y + absY + windowAttributes.height });
}
if (childrenWindows[i] != targetWindow) {
auto childCoords = getWindowCoords(display, childrenWindows[i],
targetWindow, reachedTargetPtr, absX + windowAttributes.x,
absY + windowAttributes.y);
coords.reserve(coords.size() + childCoords.size());
coords.insert(coords.end(), childCoords.begin(), childCoords.end());
}
}
return coords;
}
基本上,对于k=1,2,...,n
,我们找到 的所有组合n choose k
。然后,我们计算这些窗口以及目标窗口的相交面积,并添加/减去运行区域的结果(根据(-1)^(k-1)
包含-排除原理中的术语)。
我已经在我制作的一个简单工具中实现了这个这里。此外,这本质上是矩形区域二来自leetcode的问题。有一些更有效的方法可以做到这一点(查看解决方案部分),但我个人发现数学直观的方法取得了足够的性能。