Windows Clipboard Programming

This page explains how an X programmer can make use of the Windows Clipboard support in MI/X.

First, you want to not use the normal XA_PRIMARY_SELECTION atom.

   Atom XA_UTF8 = XInternAtom(display, "UTF8", 0);
   Atom XA_UNICODE = XInternAtom(display, "UNICODE", 0);
   Atom XA_CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0);

This declares two Atoms to use. Now, to put something on the Windows clipboard, call...

   XChangeProperty(display, RootWindow(display, 0), XA_CLIPBOARD, XA_UTF8, 8, PropModeReplace, buf, strlen(buf) + 1);
	

If the text you're placing on the clipboard is Unicode instead of UTF8, pass 16 for the 5th parameter and be sure to use wcstrlen() instead of strlen(). The length is still number of characters, not bytes.

To get text off the Windows clipboard, call...

   Atom actual_type;
   int actual_format;
   unsigned long nitems, leftover;
   unsigned char* buf;

   if (XGetWindowProperty(display, RootWindow(display,0), XA_CLIPBOARD, 0, 10000000L, False, XA_UTF8, &actual_type, &actual_format, &nitems, &leftover, &buf) == Success) {;
      /* Use buf */
      free(buf);
      }
	

Of course you can request XA_UNICODE instead of XA_UTF8.