-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathx11paste.c
54 lines (49 loc) · 1.47 KB
/
x11paste.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h> // printf
#include <string.h> // strndup
#include <X11/Xlib.h>
int XA_STRING = 31;
static Display * display;
static Window window;
static Atom UTF8;
char * XPasteType(Atom atom) {
XEvent event;
int format;
unsigned long N, size;
char * data, * s = 0;
Atom target,
CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0),
XSEL_DATA = XInternAtom(display, "XSEL_DATA", 0);
XConvertSelection(display, CLIPBOARD, atom, XSEL_DATA, window, CurrentTime);
XSync(display, 0);
XNextEvent(display, &event);
switch(event.type) {
case SelectionNotify:
if(event.xselection.selection != CLIPBOARD) break;
if(event.xselection.property) {
XGetWindowProperty(event.xselection.display, event.xselection.requestor,
event.xselection.property, 0L,(~0L), 0, AnyPropertyType, &target,
&format, &size, &N,(unsigned char**)&data);
if(target == UTF8 || target == XA_STRING) {
s = strndup(data, size);
XFree(data);
}
XDeleteProperty(event.xselection.display, event.xselection.requestor, event.xselection.property);
}
}
return s;
}
char *XPaste() {
char * c = 0;
UTF8 = XInternAtom(display, "UTF8_STRING", True);
if(UTF8 != None) c = XPasteType(UTF8);
if(!c) c = XPasteType(XA_STRING);
return c;
}
int main(int argc, char *argv[]) {
display = XOpenDisplay(0);
int N = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, N), 0, 0, 1, 1, 0,
BlackPixel(display, N), WhitePixel(display, N)
);
printf("%s\n", XPaste());
}