简体   繁体   中英

Assign user data to x11/xcb window

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.

Is there an equivalent in xcb?

So far I tried...

  • creating a map, which maps window ids to user data.
  • assigning a custom properties/atoms to windows, which hold an pointer/index to my data.

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 : How can I annotate a window with custom data in xcb ?

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;

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);

Then to get the value back

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));

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM