Skip to content

Instantly share code, notes, and snippets.

@lytithwyn
Created February 24, 2011 23:28
Show Gist options
  • Save lytithwyn/843127 to your computer and use it in GitHub Desktop.
Save lytithwyn/843127 to your computer and use it in GitHub Desktop.
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
Display *dpy = XOpenDisplay(NULL);
assert(dpy);
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
200, 100, 0, 0, 0);
XSelectInput(dpy, w, StructureNotifyMask);
XMapWindow(dpy, w);
XEvent e;
for(;;) {
XNextEvent(dpy, &e);
if (e.type == MapNotify) break;
}
XFlush(dpy);
// Copy from application
Atom a1, a2, type;
XSelectInput(dpy, w, StructureNotifyMask+ExposureMask);
int format, result;
unsigned long len, bytes_left, dummy;
unsigned char *data;
Window Sown;
for (int ii = 0; ii < 50; ii++) {
Sown = XGetSelectionOwner (dpy, XA_PRIMARY);
printf ("Selection owner%i\n", (int)Sown);
if (Sown != None) {
XConvertSelection (dpy, XA_PRIMARY, XA_STRING, None,
Sown, CurrentTime);
XFlush (dpy);
//
// Do not get any data, see how much data is there
//
XGetWindowProperty (dpy, Sown,
XA_STRING, // Tricky..
0, 0, // offset - len
0, // Delete 0==FALSE
AnyPropertyType, //flag
&type, // return type
&format, // return format
&len, &bytes_left, //that
&data);
printf ("type:%i len:%lu format:%i byte_left:%lu\n",
(int)type, len, format, bytes_left);
// DATA is There
if (bytes_left > 0)
{
result = XGetWindowProperty (dpy, Sown,
XA_STRING, 0,bytes_left,0,
AnyPropertyType, &type,&format,
&len, &dummy, &data);
if (result == Success)
printf ("DATA IS HERE!!```%s'''\n",
data);
else printf ("FAIL\n");
XFree (data);
}
}
sleep(2);
}
return 0;
}
@lytithwyn
Copy link
Author

I know I said I didn't care that much...I lied...to myself as well. ;)

@BinaryMuse
Copy link

Gross

@lytithwyn
Copy link
Author

Yeah, and right now it doesn't seem to work right. It's more-or-less copy and paste from a website I found that was explaining how x selections work, but it only seems to work from things like xterm; when I select something in Firefox, the program puts out that someone owns the selection but that it's empty.

I did fix a few things that were causing warnings so at least it compiles cleanly in c99 mode. ;)

I'll probably only get time here and there to work on this so it'll probably take a while, but I'm getting the hint that Ruby isn't the language for this particular problem. I'll probably have to implement it in c/c++, though I could create an app that works like xsel (only better) and call that from a Ruby script. We'll see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment