sselp

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

commit c3c53904786edb0c646dafaf15e2382eaf8999d2
parent c720ff0f08d0844f85fd61636c0b441d9b9da493
Author: Connor Lane Smith <cls@lubutu.com>
Date:   Mon,  9 Aug 2010 11:58:17 +0100

simpler + faster
Diffstat:
sselp.c | 77+++++++++++++++++++++++++++--------------------------------------------------
1 file changed, 27 insertions(+), 50 deletions(-)

diff --git a/sselp.c b/sselp.c @@ -1,67 +1,44 @@ /* See LICENSE file for license details. */ -#include <stdlib.h> #include <stdio.h> #include <string.h> #include <X11/Xlib.h> #include <X11/Xatom.h> -#include <X11/Xutil.h> -static unsigned char * -getsel(unsigned long offset, unsigned long *len, unsigned long *remain) { +int +main(int argc, char *argv[]) { + Atom utf8, type; Display *dpy; - Atom utf8_string; - Atom xa_clip_string; - Window w; + Window win; XEvent ev; - Atom typeret; - int format; + int fmt; + long off = 0; unsigned char *data; - unsigned char *result = NULL; + unsigned long len, more; + + if(argc > 1 && !strcmp(argv[1], "-v")) { + fputs("sselp-"VERSION", © 2006-2010 Anselm R Garbe\n", stdout); + return 0; + } + if(!(dpy = XOpenDisplay(NULL))) + return 1; + + utf8 = XInternAtom(dpy, "UTF8_STRING", False); + win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, + CopyFromParent, CopyFromParent); + XConvertSelection(dpy, XA_PRIMARY, utf8, None, win, CurrentTime); - dpy = XOpenDisplay(NULL); - if(!dpy) - return NULL; - utf8_string = XInternAtom(dpy, "UTF8_STRING", False); - xa_clip_string = XInternAtom(dpy, "_SSELP_STRING", False); - w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200, - 1, CopyFromParent, CopyFromParent); - XConvertSelection(dpy, XA_PRIMARY, utf8_string, xa_clip_string, - w, CurrentTime); - XFlush(dpy); XNextEvent(dpy, &ev); if(ev.type == SelectionNotify && ev.xselection.property != None) { - XGetWindowProperty(dpy, w, ev.xselection.property, offset, 4096L, False, - AnyPropertyType, &typeret, &format, len, remain, &data); - if(*len) { - result = malloc(sizeof(unsigned char) * *len); - memcpy(result, data, *len); + do { + XGetWindowProperty(dpy, win, utf8, off, BUFSIZ, False, + utf8, &type, &fmt, &len, &more, &data); + fwrite(data, 1, len, stdout); + XFree(data); + off += len; } - XDeleteProperty(dpy, w, ev.xselection.property); + while(more > 0); + putchar('\n'); } - XDestroyWindow(dpy, w); XCloseDisplay(dpy); - return result; -} - -int -main(int argc, char **argv) { - unsigned char *data; - unsigned long i, offset, len, remain; - - if((argc > 1) && !strncmp(argv[1], "-v", 3)) { - fputs("sselp-"VERSION", © 2006-2008 Anselm R Garbe\n", stdout); - exit(EXIT_SUCCESS); - } - len = offset = remain = 0; - do { - data = getsel(offset, &len, &remain); - for(i = 0; i < len; i++) - putchar(data[i]); - offset += len; - free(data); - } - while(remain); - if(offset) - putchar('\n'); return 0; }