【问题标题】:How to get the active window using X11/Xlib c api?如何使用 X11/Xlib c api 获取活动窗口?
【发布时间】:2022-11-13 02:55:49
【问题描述】:

我在The Xlib Manual 中找不到如何获取活动窗口?

它是我使用XGetInputFocus 获得的“焦点窗口”吗?

还是应该查询根窗口属性_NET_ACTIVE_WINDOW? 根据Wikipedia,这个属性“给出了当前活动的窗口”。

所以我想使用函数XGetWindowProperty 来获取属性_NET_ACTIVE_WINDOW,但我不知道应该给我不理解的参数赋予什么值,例如long_offsetlong_lengthdelete、@987654330 @...

我正在使用 Linux(Ubuntu)。

【问题讨论】:

  • X11 中没有活动窗口的概念。焦点是最接近的概念(它是接收键盘输入的窗口)。 (一些)窗口管理器支持活动窗口的概念,并且(其中一些)允许您使用 _NET_ACTIVE_WINDOW 查询一个。现在问题来了,你需要它做什么?

标签: c window x11 xlib


【解决方案1】:
XGetWindowProperty(
    Display *display; //display object, e.g. via XopenDisplay(NULL)
    Window w;         //root window, e.g. via DefaultRootWindow(display)
    Atom property;    //the requested property, here: _NET_ACTIVE_WINDOW
    long long_offset; //offset into returned data (32 bit quantity)
    long long_length; //length of data to return (32 bit quantity)
    Bool delete;      //False (as long you don't want to delete the property)
    Atom req_type;    //what type we expect, here we expect a window: XA_WINDOW
    Atom *actual_type_return; //an Atom or any XA_* of what is actually returned
    int *actual_format_return; //depends, read the spec of what will be returned, here: 32
    unsigned long *nitems_return; //how many items are returned, we expect 1
    unsigned long *bytes_after_return; //important on partial read
    unsigned char **prop_return; //pointer to the items
);

例子:

//open default display
Display *display = XOpenDisplay(NULL);
//get default root window of display
Window root = DefaultRootWindow(display);
//get the atom of the property
Atom property = XInternAtom(display, "_NET_ACTIVE_WINDOW", False); 

//return values
Atom type_return;
int format_return;
unsigned long nitems_return;
unsigned long bytes_left;
unsigned char *data;

XGetWindowProperty(
    display,
    root,
    property,
    0,              //no offset
    1,              //one Window
    False,
    XA_WINDOW,
    &type_return,   //should be XA_WINDOW
    &format_return, //should be 32
    &nitems_return, //should be 1 (zero if there is no such window)
    &bytes_left,    //should be 0 (i'm not sure but should be atomic read)
    &data           //should be non-null
);

//(return_value == Success) && nitems_return
Window win = ((Window*) data)[0];
XFree(data);

有用的链接:

【讨论】:

  • 我认为倒数第二行末尾有一个多余的“)”
  • @Trevor 是的,你是对的,已更正。
猜你喜欢
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 2021-03-18
相关资源
最近更新 更多