Skip to content

Instantly share code, notes, and snippets.

@twaik
Created March 8, 2023 01:39
Show Gist options
  • Save twaik/ca58cd2b5a3aabf01ffb966d4d098630 to your computer and use it in GitHub Desktop.
Save twaik/ca58cd2b5a3aabf01ffb966d4d098630 to your computer and use it in GitHub Desktop.
Pretty stupido code printing clipboard contents to stdout. Was written with help of xcb and xclip in xcb.
#include <iostream>
#include <xcb/xcb.h>
#include <xcb/bigreq.h>
#define explicit c_explicit
#include <xcb/xkb.h>
int main() {
xcb_connection_t *c = xcb_connect(":1", nullptr);
xcb_screen_t *s = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
if (xcb_connection_has_error(c)) {
std::cout << "Could not connect" << std::endl;
return 0;
}
xcb_big_requests_enable_reply(c, xcb_big_requests_enable(c), nullptr);
xcb_gcontext_t gc = xcb_generate_id(c);
xcb_create_gc(c, gc, s->root, XCB_GC_BACKGROUND, (const int[]) {0x00ffffff});
xcb_get_property_reply(c, xcb_get_property(c, 0, s->root, XCB_ATOM_RESOURCE_MANAGER, XCB_ATOM_STRING, 0, 100000000), nullptr);
xcb_xkb_use_extension_reply(c, xcb_xkb_use_extension(c, 1, 0), nullptr);
xcb_atom_t clipboard;
xcb_atom_t string;
xcb_atom_t xclip_out;
xcb_atom_t incr;
{
auto reply = xcb_intern_atom_reply(c, xcb_intern_atom(c, 0, 9, "CLIPBOARD"), nullptr);
clipboard = reply->atom;
free(reply);
}
{
auto reply = xcb_intern_atom_reply(c, xcb_intern_atom(c, 0, 11, "UTF8_STRING"), nullptr);
string = reply->atom;
free(reply);
}
xcb_window_t win = xcb_generate_id(c);
int v[] = { 0, 0 };
xcb_create_window_checked(c, 0, win, s->root, 0, 0, 1, 1, 0, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, v);
xcb_change_window_attributes(c, win, XCB_CW_EVENT_MASK, (const int[]) { XCB_EVENT_MASK_PROPERTY_CHANGE });
{
auto reply = xcb_intern_atom_reply(c, xcb_intern_atom(c, 0, 9, "XCLIP_OUT"), nullptr);
xclip_out = reply->atom;
free(reply);
}
{
auto reply = xcb_intern_atom_reply(c, xcb_intern_atom(c, 0, 4, "INCR"), nullptr);
incr = reply->atom;
free(reply);
}
xcb_convert_selection(c, win, clipboard, string, xclip_out, XCB_CURRENT_TIME);
xcb_flush(c);
xcb_generic_event_t *event;
while ((event = xcb_wait_for_event(c)) != nullptr) {
if ((event->response_type & ~0x80) == XCB_SELECTION_NOTIFY) {
std::cout << "We've got selection" << std::endl;
auto reply = xcb_get_property_reply(c, xcb_get_property(c, false, win, xclip_out, XCB_ATOM_ANY, 0, 0), nullptr);
auto reply2 = xcb_get_property_reply(c, xcb_get_property(c, false, win, xclip_out, XCB_ATOM_ANY, 0, reply->bytes_after), nullptr);
std::cout << "Clipboard value: " << std::string((char*) xcb_get_property_value(reply2)) << std::endl;
break;
}
}
xcb_disconnect(c);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment