将用户数据分配给 x11/xcb 窗口 [英] Assign user data to x11/xcb window
问题描述
像 Windows 这样的平台允许我们将自定义数据分配给一个窗口.使用 WinAPI,可以调用 SetWindowLongPtr
将自定义数据分配给窗口.使用 AppKit,可以扩展他的 NSWindowDelegate
实现,它被分配给一个窗口,并带有所需的数据.
Platforms like Windows allow us to assign custom data to a window. With WinAPI, one can call SetWindowLongPtr
to assign custom data to a window. With AppKit, one extends his implementation of NSWindowDelegate
, which is assigned to a window, with the data required.
xcb 中是否有等价物?
到目前为止我尝试过...
So far I tried...
- 创建地图,将窗口 ID 映射到用户数据.
- 为窗口分配自定义属性/原子,其中包含指向我的数据的指针/索引.
我不认为任何一种方法都很好,因此我想知道是否有内置方法来实现我所需要的.尤其是后者,因为我不知道如何确保我的属性不会与 WM 定义的其他属性发生冲突.
I don't think either approach is good, thus I am wondering if there is built-in method to achieve what I need. Especially with latter, since I don't know how to make sure, that my properties do not collide with others, defined by the WM.
TL;DR:如何在 xcb 中使用自定义数据注释窗口?
TL;DR: How can I annotate a window with custom data in xcb?
推荐答案
我不知道 xcb 但我以前在 X windows 中做的事情是这样的.假设有一个存储数据的结构
I don't know about xcb but what I used to do in X windows is somethintg like this. Say there is a structure where the data is being stored
struct CustomData
{
...
} custom;
当您为标签中的指针分配空间时,添加 sizeof(void*).假设标签是 Fred.
When you allocate space for a pointer in the label, add sizeof(void*). Say the label is Fred.
const char* tag = "Fred";
size_t tag_len = strlen(tag) + 1;
Custom* custom_ptr = &custom;
size_t ptr_len = sizeof(Custom_ptr);
char* label = (char*) malloc(taglen + sizeof(void*));
strcpy(label, tag);
// Add pointer to custom
memcpy(&label[taglen], &custom_ptr, ptr_len);
XtSetValue(w, XtNLabel, label);
然后取回价值
char* label;
Arg warg[16];
XtSetArg(warg[0], XtNLabel, &label);
XtGetValues(w, warg, 1);
size_t taglen = strlen(label) + 1;
Custom* custom_ptr;
memcpy(&custom_ptr, &label[taglen], sizeof(custom_ptr));
但那是在 80 年代中期,远在 STL 发明之前.现在,我只是按照@Ry 所说的去做 - 创建一个小部件 ID 和数据的映射.
but that was in the mid-80s, way before STL was invented. Nowadays, I'd just do as @Ry said - create a map of widget ids and data.
这篇关于将用户数据分配给 x11/xcb 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!