使用XCB获取窗口标题 [英] Get window title with XCB

查看:388
本文介绍了使用XCB获取窗口标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取有关焦点窗口的信息.看来我从xcb_get_input_focus_reply_t->focus获得了正确的窗口ID:对于我的Eclipse IDE(56623164)来说,它保持不变,而对于其他任何聚焦的窗口来说,它都是另一个.但是,XCB_ATOM_WM_NAME的值长度始终为0.

I am trying to get information about the window in focus. It seems that I get a correct window id from xcb_get_input_focus_reply_t->focus: it stays the same for my Eclipse IDE (56623164) and is another for any other window in focus. However, the value length is always 0 for XCB_ATOM_WM_NAME.

缩短代码

cookie = xcb_get_property(c, 0, fr->focus, XCB_ATOM_WM_NAME,
            XCB_ATOM_STRING, 0, 0);
if ((reply = xcb_get_property_reply(c, cookie, NULL))) {
    int len = xcb_get_property_value_length(reply);
    if (len == 0) {
        printf("Zero Length\n");
        free(reply);
        return;
    }
    printf("WM_NAME is %.*s\n", len, (char*) xcb_get_property_value(reply));
}

Eclipse调试器

reply xcb_get_property_reply_t * 0x60bd40
    response_type uint8_t            1 '\001'
    format        uint8_t            0 '\0'
    sequence      uint16_t           2
    length        uint32_t           0
    type          xcb_atom_t         0
    bytes_after   uint32_t           0
    value_len     uint32_t           0
    pad0          unsigned char [12] 0x60bd54   

没有错误(我通过并检查了xcb_generic_error_t).您知道什么地方可能出问题吗?也许我应该改用Xlib ...

There is no error (I passed and inspected a xcb_generic_error_t). Do you have any idea what could go wrong? Maybe I should use Xlib instead...

推荐答案

此代码对我有用,它在js-ctypes中,但是您可以忽略该部分并在API使用中看到它:

This code works for me, it is in js-ctypes but you can ignore that part and see this for API use:

var win = aXcbWindowT;
// console.log('win:', win);

var req_title = ostypes.API('xcb_get_property')(ostypes.HELPER.cachedXCBConn(), 0, win, ostypes.CONST.XCB_ATOM_WM_NAME, ostypes.CONST.XCB_ATOM_STRING, 0, 100); // `100` means it will get 100*4 so 400 bytes, so that 400 char, so `rez_title.bytes_after` should be `0` but i can loop till it comes out to be 0
var rez_title = ostypes.API('xcb_get_property_reply')(ostypes.HELPER.cachedXCBConn(), req_title, null);
// console.log('rez_title:', rez_title);

var title_len = ostypes.API('xcb_get_property_value_length')(rez_title); // length is not null terminated so "Console - chrome://nativeshot/content/resources/scripts/MainWorker.js?0.01966718940939427" will be length of `88`, this matches `rez_title.length` but the docs recommend to use this call to get the value, i dont know why
console.log('title_len:', title_len, 'rez_title.contents.length:', rez_title.contents.length); // i think `rez_title.contents.length` is the actual length DIVIDED by 4, and rez_title_len is not dividied by 4

var title_buf = ostypes.API('xcb_get_property_value')(rez_title); // "title_len: 89 rez_title.contents.length: 23" for test case of "Console - chrome://nativeshot/content/resources/scripts/MainWorker.js?0.01966718940939427"
// console.log('title_buf:', title_buf);

var title = ctypes.cast(title_buf, ctypes.char.array(title_len).ptr).contents.readString();
console.log('title:', title);

ostypes.API('free')(rez_title);

return title;

有时xcb_get_input_focus_reply_t->focus返回的不是执行操作的窗口.我发现有时它没有标题,但是如果您使用xcb_query_tree,则可以找到其父窗口,其标题有一个标题:

Sometimes though what is returned by xcb_get_input_focus_reply_t->focus is not the window to act on. I have found that sometimes it doesn't have a title, but if you use xcb_query_tree you can find its parent window probaby has a title:

    var req_query = ostypes.API('xcb_query_tree')(ostypes.HELPER.cachedXCBConn(), win);
    var rez_query = ostypes.API('xcb_query_tree_reply')(ostypes.HELPER.cachedXCBConn(), req_query, null);
    console.log('rez_query.contents:', rez_query.contents);
    if (root === -1) {
        root = rez_query.contents.root;
    }
    win = rez_query.contents.parent; // this win should have the title

这篇关于使用XCB获取窗口标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆