2 * This program is only intended for OS X, but the
3 * ifdef __APPLE__ below lets us build it on all systems.
4 * On non-OS X systems, you can use it to hold the snarf
5 * buffer around after a program exits.
9 #define Colormap XColormap
10 #define Cursor XCursor
11 #define Display XDisplay
12 #define Drawable XDrawable
16 #define Rectangle XRectangle
17 #define Screen XScreen
18 #define Visual XVisual
19 #define Window XWindow
21 #include <X11/Xatom.h>
22 #include <X11/Xutil.h>
23 #include <X11/keysym.h>
24 #include <X11/IntrinsicP.h>
25 #include <X11/StringDefs.h>
39 #define Boolean AppleBoolean
40 #define Rect AppleRect
41 #define EventMask AppleEventMask
42 #define Point ApplePoint
43 #define Cursor AppleCursor
44 #include <Carbon/Carbon.h>
54 AUTOLIB(draw) /* to cause link of X11 */
59 char snarf[3*SnarfSize+1];
60 Rune rsnarf[SnarfSize+1];
69 void xselectionrequest(XEvent*);
70 char *xgetsnarf(void);
71 void appleputsnarf(void);
77 PasteboardRef appleclip;
83 fprint(2, "usage: snarfer [-v]\n");
88 main(int argc, char **argv)
100 if((xdisplay = XOpenDisplay(nil)) == nil)
101 sysfatal("XOpenDisplay: %r");
102 drawable = XCreateWindow(xdisplay, DefaultRootWindow(xdisplay),
104 InputOutput, DefaultVisual(xdisplay, DefaultScreen(xdisplay)),
107 sysfatal("XCreateWindow: %r");
110 xclipboard = XInternAtom(xdisplay, "CLIPBOARD", False);
111 xutf8string = XInternAtom(xdisplay, "UTF8_STRING", False);
112 xtargets = XInternAtom(xdisplay, "TARGETS", False);
113 xtext = XInternAtom(xdisplay, "TEXT", False);
114 xcompoundtext = XInternAtom(xdisplay, "COMPOUND_TEXT", False);
117 if(PasteboardCreate(kPasteboardClipboard, &appleclip) != noErr)
118 sysfatal("pasteboard create failed");
126 XNextEvent(xdisplay, &xevent);
135 print("snarf{%s}\n", snarf);
137 case SelectionRequest:
138 xselectionrequest(&xevent);
145 xselectionrequest(XEvent *e)
150 XSelectionRequestEvent *xe;
155 memset(&r, 0, sizeof r);
156 xe = (XSelectionRequestEvent*)e;
157 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
158 xe->target, xe->requestor, xe->property, xe->selection);
159 r.xselection.property = xe->property;
160 if(xe->target == xtargets){
164 a[3] = xcompoundtext;
166 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
167 8, PropModeReplace, (uchar*)a, sizeof a);
168 }else if(xe->target == XA_STRING || xe->target == xutf8string || xe->target == xtext || xe->target == xcompoundtext){
169 /* if the target is STRING we're supposed to reply with Latin1 XXX */
170 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
171 8, PropModeReplace, (uchar*)snarf, strlen(snarf));
173 name = XGetAtomName(xd, xe->target);
174 if(strcmp(name, "TIMESTAMP") != 0)
175 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
176 r.xselection.property = None;
179 r.xselection.display = xe->display;
180 /* r.xselection.property filled above */
181 r.xselection.target = xe->target;
182 r.xselection.type = SelectionNotify;
183 r.xselection.requestor = xe->requestor;
184 r.xselection.time = xe->time;
185 r.xselection.send_event = True;
186 r.xselection.selection = xe->selection;
187 XSendEvent(xd, xe->requestor, False, 0, &r);
195 Atom clipboard, type, prop;
196 ulong len, lastlen, dummy;
207 * Is there a primary selection (highlighted text in an xterm)?
210 clipboard = XA_PRIMARY;
211 w = XGetSelectionOwner(xd, XA_PRIMARY);
217 * If not, is there a clipboard selection?
219 if(w == None && xclipboard != None){
220 clipboard = xclipboard;
221 w = XGetSelectionOwner(xd, xclipboard);
233 * We should be waiting for SelectionNotify here, but it might never
234 * come, and we have no way to time out. Instead, we will clear
235 * local property #1, request our buddy to fill it in for us, and poll
236 * until he's done or we get tired of waiting.
238 * We should try to go for _x.utf8string instead of XA_STRING,
239 * but that would add to the polling.
242 XChangeProperty(xd, drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
243 XConvertSelection(xd, clipboard, XA_STRING, prop, drawable, CurrentTime);
246 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
248 XGetWindowProperty(xd, drawable, prop, 0, 0, 0, AnyPropertyType,
249 &type, &fmt, &dummy, &len, &data);
250 if(lastlen == len && len > 0)
256 /* get the property */
258 XGetWindowProperty(xd, drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
259 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
260 if(xdata == nil || (type != XA_STRING && type != xutf8string) || len == 0){
265 if(strlen((char*)xdata) >= SnarfSize){
269 strcpy(snarf, (char*)xdata);
276 if(0) XSetSelectionOwner(xdisplay, XA_PRIMARY, drawable, CurrentTime);
277 if(xclipboard != None)
278 XSetSelectionOwner(xdisplay, xclipboard, drawable, CurrentTime);
287 PasteboardSyncFlags flags;
289 runesnprint(rsnarf, nelem(rsnarf), "%s", snarf);
290 if(PasteboardClear(appleclip) != noErr){
291 fprint(2, "apple pasteboard clear failed\n");
294 flags = PasteboardSynchronize(appleclip);
295 if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
296 fprint(2, "apple pasteboard cannot assert ownership\n");
299 cfdata = CFDataCreate(kCFAllocatorDefault,
300 (uchar*)rsnarf, runestrlen(rsnarf)*2);
302 fprint(2, "apple pasteboard cfdatacreate failed\n");
305 if(PasteboardPutItemFlavor(appleclip, (PasteboardItemID)1,
306 CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
307 fprint(2, "apple pasteboard putitem failed\n");