我正在使用 XCB 向 X11 窗口询问其进程的 PID 以及其他属性。我获取各种非字符串属性的代码如下:
xcb_window_t wid;
xcb_connection_t * conn;
template <typename T>
T get_property(xcb_atom_t property, xcb_atom_t type, size_t len = sizeof(T)) {
xcb_generic_error_t *err = nullptr; // can't use unique_ptr here because get_property_reply overwrites pointer value
/*
Specifies how many 32-bit multiples of data should be retrieved
(e.g. if you set long_length to 4, you will receive 16 bytes of data).
*/
uint32_t ret_size =
len/sizeof(uint32_t) /*integer division, like floor()*/ +
!!(len%sizeof(uint32_t)) /*+1 if there was a remainder*/;
xcb_get_property_cookie_t cookie = xcb_get_property(
conn, 0, wid, property, type, 0, ret_size
);
std::unique_ptr<xcb_get_property_reply_t,decltype(&free)> reply {xcb_get_property_reply(conn, cookie, &err),&free};
if (!reply) {
free(err);
throw std::runtime_error("xcb_get_property returned error");
}
return *reinterpret_cast<T*>(
xcb_get_property_value(
reply.get()
)
);
}
xcb_atom_t NET_WM_PID; // initialized using xcb_intern_atom
// according to libxcb-ewmh, CARDINALs are uint32_t
pid_t pid = get_property<uint32_t>(NET_WM_PID, XCB_ATOM_CARDINAL);
错误处理转自xcb-requests(3) .当窗口没有设置 _NET_WM_PID
属性时会出现问题(例如,Worker 文件管理器不会这样做)。在那种情况下,我没有从 xcb_get_property_reply
和非空 err
得到一个 nullptr
,而是得到一个等于 XCB 序列号的数字答案要求。如何正确检查 _NET_WM_PID
或 CARDINAL
类型的其他属性是否未在窗口上设置?
最佳答案
缺少属性不是错误。如果未设置该属性,则回复中的格式、类型和长度都将为零。您可能想要检查所有这些并验证它们是否具有您期望它们具有的值。
关于c++ - 对于不存在的 _NET_WM_PID,无法从 xcb_get_property_reply 获取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43633695/