注意:这是对 What process created this X11 window? 的回答的延续来自 unix.stackexchange。该答案提到了来自 X-Resource v1.2 extension 的 XResQueryClientIds
.我想知道如何使用它。
如何使用 python's xcffib module查找与提供的 PID 关联的所有 X11 窗口 ID(假设没有竞争条件;没有创建或销毁窗口或进程)。
我不太了解 X11,XCB API documentation似乎不完整,auto-generated xcffib python bindings没有记录。根据我收集到的信息,我需要:
- 创建连接:
xcb_connect
- 获取扩展名:?undocumented?
- 查询扩展:?
xcb_get_extension_data
提到“?QueryExtension requests” - 获取查询响应:
xcb_get_extension_data
- 使用extension documentation和 protocol stub API解压响应。
最佳答案
assuming no race conditions; windows or processes aren't being created or destroyed
如果您知道假设不成立,那么假设就是错误的。幸运的是,您不需要这个假设。只需在您的操作中使用 xcb_grab_server
和 xcb_ungrab_server
,这将不是问题。
现在,对于 XResQueryClientIds
,您实际上只需输入 man xcb_res_query_client_ids
。 XCB只是提供了这个,不需要实际查询扩展。这是一个示例程序。使用 gcc -lxcb -lxcb-res main.c
编译它,然后通过将窗口 ID 作为唯一参数传递来执行它(例如,./a.out 0x2c00004
) .
#include <stdio.h>
#include <stdlib.h>
#include <xcb/xcb.h>
#include <xcb/res.h>
int main(int argc, char *argv[]) {
int screen;
xcb_connection_t *conn = xcb_connect(NULL, &screen);
xcb_res_client_id_spec_t spec = {0};
spec.client = strtol(argv[1], NULL, 0);
spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
xcb_generic_error_t *err = NULL;
xcb_res_query_client_ids_cookie_t cookie = xcb_res_query_client_ids(conn, 1, &spec);
xcb_res_query_client_ids_reply_t *reply = xcb_res_query_client_ids_reply(conn, cookie, &err);
if (reply == NULL) {
fprintf(stderr, "Uh-Oh! :(\n");
return -1;
}
uint32_t *pid = NULL;
xcb_res_client_id_value_iterator_t it = xcb_res_query_client_ids_ids_iterator(reply);
for (; it.rem; xcb_res_client_id_value_next(&it)) {
spec = it.data->spec;
if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
pid = xcb_res_client_id_value_value(it.data);
break;
}
}
free(reply);
xcb_disconnect(conn);
fprintf(stderr, "PID: %d\n", *pid);
}
为了给出正确的归因,我自己也不知道这些,我只是用谷歌搜索 XCB 函数名称并遇到了 this .要了解各个部分,我建议阅读它的 Xlib 文档。正如您所注意到的,XCB 通常是……“文档不足”,但它实际上与 Xlib 相同,只是在大多数情况下其他名称略有不同。
关于python:使用 XResQueryClientIds 将 PID 转换为 X11 窗口 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37283179/