Skip to content

Instantly share code, notes, and snippets.

@lyricallogical
Created February 25, 2011 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyricallogical/843534 to your computer and use it in GitHub Desktop.
Save lyricallogical/843534 to your computer and use it in GitHub Desktop.
#include <string>
#include <utility>
#include <cstdio>
#include <iostream>
#include <tr1/memory>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xmu/Atoms.h>
using namespace std;
using namespace std::tr1;
size_t formatToByte(int format) {
switch (format) {
case 8:
return sizeof(char);
case 16:
return sizeof(short);
case 32:
return sizeof(long);
}
return 0;
}
typedef shared_ptr<Display> DisplayPtr;
void closeDisplay(Display* display) {
if (display != NULL)
::XCloseDisplay(display);
}
DisplayPtr openDisplay() {
Display* display = ::XOpenDisplay(NULL);
if (display == NULL)
return DisplayPtr();
return DisplayPtr(display, closeDisplay);
}
int main() {
DisplayPtr display = openDisplay();
if (!display)
return 0;
Atom clipboard_atom = XA_CLIPBOARD(display.get());
Window window = ::XCreateSimpleWindow(display.get(), DefaultRootWindow(display.get()), 0, 0, 1, 1, 0, 0, 0);
int select_result = ::XSelectInput(display.get(), window, PropertyChangeMask);
Atom result_atom = ::XInternAtom(display.get(), "FOR_GET_RESULT", False);
int convert_result = ::XConvertSelection(display.get(), clipboard_atom, XA_UTF8_STRING(display.get()), result_atom, window, CurrentTime);
XEvent event;
bool f = false;
for (int i = 0; i < 10; i++) {
int result = ::XNextEvent(display.get(), &event);
if (event.type != SelectionNotify) {
printf("event.type != SelectionNotify. type = %d.\n", event.type);
} else {
f = true;
break;
}
}
if (!f)
return 0;
if (event.xselection.property == None) {
printf("xselection.property == None\n");
return 0;
}
Atom actual_type;
int actual_format;
unsigned long property_size, property_items;
unsigned char* buffer;
int get_property_result = ::XGetWindowProperty(display.get(), window, result_atom, 0, 0, False, AnyPropertyType, &actual_type, &actual_format, &property_size, &property_items, &buffer);
::XFree(buffer);
char* actual_type_name = ::XGetAtomName(display.get(), actual_type);
printf("actual_type_name = %s\n", actual_type_name);
::XFree(actual_type_name);
printf("format = %d, property_size = %lu, property_items = %lu\n", actual_format, property_size, property_items);
get_property_result = ::XGetWindowProperty(display.get(), window, result_atom, 0, static_cast<long>(property_items), False, AnyPropertyType, &actual_type, &actual_format, &property_size, &property_items, &buffer);
printf("format = %d, property_size = %lu, property_items = %lu\n", actual_format, property_size, property_items);
int delete_result = ::XDeleteProperty(display.get(), window, result_atom);
size_t property_buffer_size = property_items * formatToByte(actual_format);
string buf(reinterpret_cast<char*>(buffer));
printf("%s\n", buffer);
cout << buf << endl;
::XFree(buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment