util.c
changeset 11 ea9c08ec4b48
parent 9 d567f430a81d
child 14 5c078b66347b
equal deleted inserted replaced
10:703255003abb 11:ea9c08ec4b48
     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>
    13 
    14 
    14 #include "util.h"
    15 #include "util.h"
    15 
    16 
    16 static char *shell = NULL;
    17 static char *shell = NULL;
    17 
    18 
   145 		close(pfd[0]);
   146 		close(pfd[0]);
   146 		buf[n - 1] = 0;
   147 		buf[n - 1] = 0;
   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 }