gpt4 book ai didi

c - X11 根据区域位置检索最上面的窗口

转载 作者:行者123 更新时间:2023-11-30 14:46:45 25 4
gpt4 key购买 nike

我当前正在寻找检索某个位置存在的窗口。我已经检索了窗口列表,但我不确定如何继续检索最上面的窗口。

这是我迄今为止所完成的工作(包含测试主要内容):

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>

typedef struct
{
int x;
int y;
int w;
int h;
} area_coords_t;

static inline Window get_toplevel_parent(Display * display, Window window)
{
Window parent;
Window root;
Window * children;
unsigned int num_children;

while (1) {
if (0 == XQueryTree(display, window, &root, &parent, &children, &num_children)) {
fprintf(stderr, "XQueryTree error\n");
abort(); //change to whatever error handling you prefer
}
if (children) { //must test for null
XFree(children);
}
if (window == root || parent == root) {
return window;
}
else {
window = parent;
}
}
}

static inline void print_windows(Display *display, area_coords_t *area)
{
Window *list;
Window toplevel;
Atom prop = XInternAtom(display, "_NET_CLIENT_LIST",False);
Atom type;
unsigned long len;
XWindowAttributes attr;

if (XGetWindowProperty(display, XDefaultRootWindow(display), prop,
0, 1024, False, XA_WINDOW, &type, &(int) { 0 }, &len, &(unsigned long) { 0 }, (unsigned char **) &list) != Success)
return;
for (unsigned long it = 0; it < len; ++it)
{
toplevel = get_toplevel_parent(display, list[it]);
XGetWindowAttributes(display, toplevel, &attr);
/* Retrieve only windows in the given area */
//if (area->x >= attr.x && area->x <= attr.x + attr.width && area->y >= attr.y && area->y <= attr.y + attr.height)
printf("%dx%dx%dx%d\n", attr.x, attr.y, attr.width, attr.height);
}
}

int main(void)
{
Display *display = XOpenDisplay(NULL);
/* Should retrieve a window occupying at least a pixel at of the bottom right part of the screen of a 1920x1080 screen */
area_coords_t coords = { 1920 / 2, 1080 / 2, 1920 / 2, 1080 / 2 };

print_windows(display, &coords);
return 0;
}

问题是窗口似乎没有排序。

如何检索最顶层的窗口以确保获得给定位置的适当窗口?

最佳答案

XQueryTree()按堆叠顺序返回窗口,最上面的最后一个。

Description

The XQueryTree() function returns the root ID, the parent window ID, a pointer to the list of children windows (NULL when there are no children), and the number of children in the list for the specified window. The children are listed in current stacking order, from bottommost (first) to topmost (last). XQueryTree() returns zero if it fails and nonzero if it succeeds. To free a non-NULL children list when it is no longer needed, use XFree().

我从未使用过该调用来实现此目的,并且多年来没有直接进行 X11 编程,但从那以后我想您可以循环遍历查询根窗口返回的列表并保存窗口 ID每次覆盖您所需位置的任何窗口到同一变量。由于找到的最后一个覆盖该位置的窗口是最上面的(至少是在您进行调用时...),因此这将是该位置处可见窗口的窗口 ID。

关于c - X11 根据区域位置检索最上面的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51965353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com