[hackers] [sselp] simpler + faster || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Mon, 9 Aug 2010 10:58:21 +0000 (UTC)

changeset: 13:30d96a9ee8a6
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Mon Aug 09 11:58:17 2010 +0100
files: sselp.c
description:
simpler + faster

diff -r beea0ea83046 -r 30d96a9ee8a6 sselp.c
--- a/sselp.c Tue Jul 29 19:24:27 2008 +0100
+++ b/sselp.c Mon Aug 09 11:58:17 2010 +0100
@@ -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;
 
- 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);
+ 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);
+
         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;
 }
Received on Mon Aug 09 2010 - 12:58:21 CEST

This archive was generated by hypermail 2.2.0 : Mon Aug 09 2010 - 13:00:08 CEST