From e607d7915643f2d825c7dcb6df81d040fd0ffe41 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 20 Jul 2007 19:19:00 +0200 Subject: [PATCH] Added xclipcat. --- Makefile | 3 +++ xclipcat.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 xclipcat.c diff --git a/Makefile b/Makefile index ff28ffe..fa20c23 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,12 @@ all: @grep '^[^ ]*:' Makefile parrot: parrot.c +xclipcat: xclipcat.c batt: CFLAGS+=$(shell xosd-config --cflags) batt: LDFLAGS+=$(shell xosd-config --libs) +xclipcat: LDFLAGS+=-L/usr/X11R6/lib -lX11 + clean: rm -f `find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core -or -name .depend -or -name .#*` diff --git a/xclipcat.c b/xclipcat.c new file mode 100644 index 0000000..16fcd2e --- /dev/null +++ b/xclipcat.c @@ -0,0 +1,69 @@ +/* + * Print contents of primary X selection. Inspired by the `xclip' utility by Kim Saunders. + */ + +#include +#include +#include +#include + +static Display *dpy; +static Window win; + +static void die(char *msg) +{ + fprintf(stderr, "xclipcat: %s\n", msg); + exit(1); +} + +int main(void) +{ + /* Create display and window */ + if (!(dpy = XOpenDisplay(NULL))) + die("Cannot open display"); + win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, 0, 0); + + /* Select which events we want to listen to */ + XSelectInput(dpy, win, PropertyChangeMask); + + /* Atoms we will need */ + Atom pty = XInternAtom(dpy, "XCLIPCAT_CONTENTS", False); + Atom incr = XInternAtom(dpy, "INCR", False); + + /* Request the selection */ + XConvertSelection(dpy, XA_PRIMARY, XA_STRING, pty, win, CurrentTime); + + /* Wait for the right event */ + XEvent ev; + do + XNextEvent(dpy, &ev); + while (ev.type != SelectionNotify); + + /* Read type and length of our property */ + Atom pty_type; + int pty_format; + unsigned long pty_items, pty_size; + unsigned char *buf; + XGetWindowProperty(dpy, win, pty, 0, 0, False, AnyPropertyType, &pty_type, &pty_format, &pty_items, &pty_size, &buf); + XFree(buf); + + /* Check type and format */ + if (pty_type == incr) + die("Incremental transfer not supported yet"); + if (pty_format != 8) + die("Unrecognized property format"); + + /* Read the contents of the property */ + XGetWindowProperty(dpy, win, pty, 0, pty_size, False, AnyPropertyType, &pty_type, &pty_format, &pty_items, &pty_size, &buf); + + /* Print the contents */ + for (unsigned int i=0; i