util.c
changeset 15 00d4d52b231f
parent 14 5c078b66347b
child 16 359b6e563b95
equal deleted inserted replaced
14:5c078b66347b 15:00d4d52b231f
     8 #include <stdlib.h>
     8 #include <stdlib.h>
     9 #include <string.h>
     9 #include <string.h>
    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>
       
    14 
    13 
    15 #include "util.h"
    14 #include "util.h"
    16 
    15 
    17 void
    16 void
    18 error(char *errstr, ...) {
    17 error(char *errstr, ...) {
   137 		close(pfd[0]);
   136 		close(pfd[0]);
   138 		buf[n - 1] = 0;
   137 		buf[n - 1] = 0;
   139 	}
   138 	}
   140 	wait(0);
   139 	wait(0);
   141 }
   140 }
   142 
       
   143 
       
   144 unsigned char *
       
   145 getselection(unsigned long offset, unsigned long *len, unsigned long *remain)
       
   146 {
       
   147 	Display *dpy;
       
   148 	Atom xa_clip_string;
       
   149 	Window w;
       
   150 	XEvent ev;
       
   151 	Atom typeret;
       
   152 	int format;
       
   153 	unsigned char *data;
       
   154 	unsigned char *result = NULL;
       
   155 
       
   156 	dpy = XOpenDisplay(0);
       
   157 	if(!dpy)
       
   158 		return NULL;
       
   159 	xa_clip_string = XInternAtom(dpy, "_SEL_STRING", False);
       
   160 	w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200,
       
   161 			1, CopyFromParent, CopyFromParent);
       
   162 	XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string,
       
   163 			w, CurrentTime);
       
   164 	XFlush(dpy);
       
   165 	XNextEvent(dpy, &ev);
       
   166 	if(ev.type == SelectionNotify && ev.xselection.property != None) {
       
   167 		XGetWindowProperty(dpy, w, ev.xselection.property, offset, 4096L, False,
       
   168 				AnyPropertyType, &typeret, &format, len, remain, &data);
       
   169 		if(*len) {
       
   170 			result = emalloc(sizeof(unsigned char) * *len);
       
   171 			memcpy(result, data, *len);
       
   172 		}
       
   173 		XDeleteProperty(dpy, w, ev.xselection.property);
       
   174 	}
       
   175 	XDestroyWindow(dpy, w);
       
   176 	XCloseDisplay(dpy);
       
   177 	return result;
       
   178 }