I am pretty much exhausted all the possibilities of finding an X11 API to perform the following thing.
I have a thread which is trying to monitor for an event or notification to know when anything is copied into clipboard by any X11 client
. I do not want to monitor a specific Atom Target (clipboard format), but generally looking for changes in clipboards.
Once, I know that something has changed in the clipboard, I can dive in and perform XConvertSelection()
on all the target formats (I want to request server
to give me all the possible ways to convert the copied data), and futher process them into SelectionRequest
event.
Again, I want to generally get request for all the formats (thinking to enumerate between 1 to 1000 to check target Atoms), and not register changes for one specific format. Based on response from the server, if a particular atom is absent, I can check None
as the property member, or else store other target Atom Names in a list.
Can anyone help me with how to monitor for changes in Clipboard? Also, does iterating 1 to 1000 will guarantee exhaustive search of all possible format? Or is there a better way to do just that?
CodePudding user response:
To monitor changes, use XFixes. With XCB it is used like:
// Enable XFixes
auto xfixes = xcb_get_extension_data(connection, &xcb_xfixes_id); // do not free!
ev_selection_change_notify = xfixes->first_event XCB_XFIXES_SELECTION_NOTIFY;
auto *version = xcb_xfixes_query_version_reply(xcb_xfixes_query_version(connection, XCB_XFIXES_MAJOR_VERSION, XCB_XFIXES_MINOR_VERSION));
// Subscribe to clipboard notifications
xcb_xfixes_select_selection_input(connection, root, clipboard, XCB_XFIXES_SELECTION_EVENT_MASK_SET_SELECTION_OWNER);
// Event loop:
auto *event = xcb_poll_for_event(connection);
int etype = event->response_type & 0x7f;
if (etype == ev_selection_change_notify) {
auto *notification = reinterpret_cast<xcb_xfixes_selection_notify_event_t *>(event);
...
}
...
In Xlib it should be similar.
To check the list of available targets, don’t loop to 1000! Simply query the TARGETS
target, it should give you the list of valid targets for the clipboard content.
There is a caveat, though: instead of “the” clipboard, X11 allow applications to use “selections” which can be tagged by arbitrary atoms. Of those CLIPBOARD
is of primary interest but PRIMARY
and (rarely used) SECONDARY
are also there, as well as arbitrary selections for “private communication”.
Reference: https://www.x.org/releases/X11R7.7/doc/xorg-docs/icccm/icccm.html#Use_of_Selection_Atoms