簡體   English   中英

如何使用 X11/Xlib c api 獲得活動的 window?

[英]How to get the active window using X11/Xlib c api?

我在Xlib 手冊中找不到如何獲得活動的 window?

它是我使用XGetInputFocus獲得的“焦點窗口”嗎?

或者我應該查詢根 window 屬性_NET_ACTIVE_WINDOW嗎? 根據Wikipedia ,此屬性“提供當前活動的窗口”。

因此,我想使用 function XGetWindowProperty來獲取屬性_NET_ACTIVE_WINDOW ,但我不知道應該為我不理解的參數賦予什么值,例如long_offsetlong_lengthdeletereq_type ...

我正在使用 Linux (Ubuntu)。

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);

有用的鏈接:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM