2 * Print contents of primary X selection. Inspired by the `xclip' utility by Kim Saunders.
13 static void die(char *msg)
15 fprintf(stderr, "xclipcat: %s\n", msg);
21 /* Create display and window */
22 if (!(dpy = XOpenDisplay(NULL)))
23 die("Cannot open display");
24 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, 0, 0);
26 /* Select which events we want to listen to */
27 XSelectInput(dpy, win, PropertyChangeMask);
29 /* Atoms we will need */
30 Atom pty = XInternAtom(dpy, "XCLIPCAT_CONTENTS", False);
31 Atom incr = XInternAtom(dpy, "INCR", False);
33 /* Request the selection */
34 XConvertSelection(dpy, XA_PRIMARY, XA_STRING, pty, win, CurrentTime);
36 /* Wait for the right event */
40 while (ev.type != SelectionNotify);
42 /* Read type and length of our property */
45 unsigned long pty_items, pty_size;
47 XGetWindowProperty(dpy, win, pty, 0, 0, False, AnyPropertyType, &pty_type, &pty_format, &pty_items, &pty_size, &buf);
50 /* Check type and format */
52 die("Incremental transfer not supported yet");
54 die("Unrecognized property format");
56 /* Read the contents of the property */
57 XGetWindowProperty(dpy, win, pty, 0, pty_size, False, AnyPropertyType, &pty_type, &pty_format, &pty_items, &pty_size, &buf);
59 /* Print the contents */
60 for (unsigned int i=0; i<pty_items; i++)
64 /* Free the buffer and delete the property (just for completeness) */
66 XDeleteProperty(dpy, win, pty);