1
我正在使用XCB向X11窗口詢問其進程的PID以及其他屬性。我的代碼來獲得各種非字符串屬性如下:無法從xcb_get_property_reply獲取不存在的錯誤_NET_WM_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
屬性集時(例如,工作文件管理器不這樣做),會出現問題。在這種情況下,我得到的數字答案等於XCB請求的序列號,而不是從xcb_get_property_reply
和非空err
得到nullptr
。如何正確檢查_NET_WM_PID
或CARDINAL
類型的其他財產是否未在窗口上設置?
謝謝!在我的模板函數中,我現在確保請求的長度和'xcb_get_property_value_length(reply.get())'都不爲零,否則拋出。 – aitap