sselp

simple X selection printer to stdout
git clone git://git.suckless.org/sselp
Log | Files | Refs | README | LICENSE

sselp.c (1141B)


      1 /* See LICENSE file for license details. */
      2 #include <stdio.h>
      3 #include <string.h>
      4 #include <X11/Xlib.h>
      5 #include <X11/Xatom.h>
      6 
      7 int
      8 main(int argc, char *argv[]) {
      9 	Atom clip, utf8, type;
     10 	Display *dpy;
     11 	Window win;
     12 	XEvent ev;
     13 	int fmt;
     14 	long off = 0;
     15 	unsigned char *data;
     16 	unsigned long len, more;
     17 
     18 	if(argc > 1 && !strcmp(argv[1], "-v")) {
     19 		fputs("sselp-"VERSION", © 2006-2010 Anselm R Garbe\n", stdout);
     20 		return 0;
     21 	}
     22 	if(!(dpy = XOpenDisplay(NULL)))
     23 		return 1;
     24 
     25 	utf8 = XInternAtom(dpy, "UTF8_STRING", False);
     26 	clip = XInternAtom(dpy, "_SSELP_STRING", False);
     27 	win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0,
     28 	                          CopyFromParent, CopyFromParent);
     29 	XConvertSelection(dpy, XA_PRIMARY, utf8, clip, win, CurrentTime);
     30 
     31 	XNextEvent(dpy, &ev);
     32 	if(ev.type == SelectionNotify && ev.xselection.property != None) {
     33 		do {
     34 			XGetWindowProperty(dpy, win, ev.xselection.property, off, BUFSIZ,
     35 				           False, utf8, &type, &fmt, &len, &more, &data);
     36 			fwrite(data, 1, len, stdout);
     37 			XFree(data);
     38 			off += len/4;
     39 		}
     40 		while(more > 0);
     41 		putchar('\n');
     42 	}
     43 	XCloseDisplay(dpy);
     44 	return 0;
     45 }