diff options
Diffstat (limited to 'util.c')
| -rw-r--r-- | util.c | 38 |
1 files changed, 38 insertions, 0 deletions
| @@ -10,6 +10,7 @@ | |||
| 10 | #include <sys/types.h> | 10 | #include <sys/types.h> |
| 11 | #include <sys/wait.h> | 11 | #include <sys/wait.h> |
| 12 | #include <unistd.h> | 12 | #include <unistd.h> |
| 13 | #include <X11/Xatom.h> | ||
| 13 | 14 | ||
| 14 | #include "util.h" | 15 | #include "util.h" |
| 15 | 16 | ||
| @@ -147,3 +148,40 @@ pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd) | |||
| 147 | } | 148 | } |
| 148 | wait(0); | 149 | wait(0); |
| 149 | } | 150 | } |
| 151 | |||
| 152 | |||
| 153 | unsigned char * | ||
| 154 | getselection(unsigned long offset, unsigned long *len, unsigned long *remain) | ||
| 155 | { | ||
| 156 | Display *dpy; | ||
| 157 | Atom xa_clip_string; | ||
| 158 | Window w; | ||
| 159 | XEvent ev; | ||
| 160 | Atom typeret; | ||
| 161 | int format; | ||
| 162 | unsigned char *data; | ||
| 163 | unsigned char *result = NULL; | ||
| 164 | |||
| 165 | dpy = XOpenDisplay(0); | ||
| 166 | if(!dpy) | ||
| 167 | return NULL; | ||
| 168 | xa_clip_string = XInternAtom(dpy, "_SEL_STRING", False); | ||
| 169 | w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200, | ||
| 170 | 1, CopyFromParent, CopyFromParent); | ||
| 171 | XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string, | ||
| 172 | w, CurrentTime); | ||
| 173 | XFlush(dpy); | ||
| 174 | XNextEvent(dpy, &ev); | ||
| 175 | if(ev.type == SelectionNotify && ev.xselection.property != None) { | ||
| 176 | XGetWindowProperty(dpy, w, ev.xselection.property, offset, 4096L, False, | ||
| 177 | AnyPropertyType, &typeret, &format, len, remain, &data); | ||
| 178 | if(*len) { | ||
| 179 | result = emalloc(sizeof(unsigned char) * *len); | ||
| 180 | memcpy(result, data, *len); | ||
| 181 | } | ||
| 182 | XDeleteProperty(dpy, w, ev.xselection.property); | ||
| 183 | } | ||
| 184 | XDestroyWindow(dpy, w); | ||
| 185 | XCloseDisplay(dpy); | ||
| 186 | return result; | ||
| 187 | } | ||
