1 /* input event and data structure translation */
7 #define Boolean AppleBoolean
9 #define EventMask AppleEventMask
10 #define Point ApplePoint
11 #define Cursor AppleCursor
12 #include <Carbon/Carbon.h>
26 #include "x11-memdraw.h"
27 #include "x11-keysym2ucs.h"
31 __xtoplan9kbd(XEvent *e)
35 if(e->xany.type != KeyPress)
37 needstack(64*1024); /* X has some *huge* buffers in openobject */
38 /* and they're even bigger on SuSE */
39 XLookupString((XKeyEvent*)e,NULL,0,&k,NULL);
40 if(k == XK_Multi_key || k == NoSymbol)
113 case XK_Meta_L: /* Shift Alt on PCs */
115 case XK_Meta_R: /* Shift Alt on PCs */
118 default: /* not ISO-1 or tty control */
120 k = _p9keysym2ucs(k);
126 /* Compensate for servers that call a minus a hyphen */
129 /* Do control mapping ourselves if translator doesn't */
130 if(e->xkey.state&ControlMask)
140 xtoplan9latin1(XEvent *e)
143 static int alting, nk;
147 r = __xtoplan9kbd(e);
152 * Kludge for Mac's X11 3-button emulation.
153 * It treats Command+Button as button 3, but also
154 * ends up sending XK_Meta_L twice.
173 /* n < -1, need more input */
187 _xtoplan9kbd(XEvent *e)
191 if(e == (XEvent*)-1){
197 r = xtoplan9latin1(e);
204 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
210 if(_x.putsnarf != _x.assertsnarf){
211 _x.assertsnarf = _x.putsnarf;
212 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);
213 if(_x.clipboard != None)
214 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);
220 be = (XButtonEvent*)e;
222 * Fake message, just sent to make us announce snarf.
223 * Apparently state and button are 16 and 8 bits on
224 * the wire, since they are truncated by the time they
228 && (~be->state&0xFFFF)==0
229 && (~be->button&0xFF)==0)
231 /* BUG? on mac need to inherit these from elsewhere? */
255 be = (XButtonEvent*)e;
280 me = (XMotionEvent*)e;
308 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
333 XFreeCursor(_x.display, _x.cursor);
336 XUndefineCursor(_x.display, _x.drawable);
342 _xsetcursor(Cursor *c)
348 uchar src[2*16], mask[2*16];
354 for(i=0; i<2*16; i++){
355 src[i] = revbyte(c->set[i]);
356 mask[i] = revbyte(c->set[i] | c->clr[i]);
361 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
362 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
363 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
365 XDefineCursor(_x.display, _x.drawable, xc);
367 XFreeCursor(_x.display, _x.cursor);
370 XFreePixmap(_x.display, xsrc);
371 XFreePixmap(_x.display, xmask);
379 Rune rbuf[SnarfSize];
385 _xgetsnarf(XDisplay *xd)
388 Atom clipboard, type, prop;
389 ulong len, lastlen, dummy;
395 * Have we snarfed recently and the X server hasn't caught up?
397 if(_x.putsnarf != _x.assertsnarf)
401 * Is there a primary selection (highlighted text in an xterm)?
403 clipboard = XA_PRIMARY;
404 w = XGetSelectionOwner(xd, XA_PRIMARY);
405 if(w == _x.drawable){
407 data = (uchar*)strdup(clip.buf);
412 * If not, is there a clipboard selection?
414 if(w == None && _x.clipboard != None){
415 clipboard = _x.clipboard;
416 w = XGetSelectionOwner(xd, _x.clipboard);
430 * We should be waiting for SelectionNotify here, but it might never
431 * come, and we have no way to time out. Instead, we will clear
432 * local property #1, request our buddy to fill it in for us, and poll
433 * until he's done or we get tired of waiting.
435 * We should try to go for _x.utf8string instead of XA_STRING,
436 * but that would add to the polling.
439 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
440 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
443 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
445 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
446 &type, &fmt, &dummy, &len, &data);
447 if(lastlen == len && len > 0)
455 /* get the property */
457 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
458 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
459 if((type != XA_STRING && type != _x.utf8string) || len == 0){
465 data = (uchar*)strdup((char*)xdata);
476 _xputsnarf(XDisplay *xd, char *data)
480 if(strlen(data) >= SnarfSize)
483 strcpy(clip.buf, data);
484 /* leave note for mouse proc to assert selection ownership */
487 /* send mouse a fake event so snarf is announced */
488 memset(&e, 0, sizeof e);
489 e.type = ButtonPress;
490 e.window = _x.drawable;
493 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
499 _xselect(XEvent *e, XDisplay *xd)
503 XSelectionRequestEvent *xe;
506 memset(&r, 0, sizeof r);
507 xe = (XSelectionRequestEvent*)e;
508 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
509 xe->target, xe->requestor, xe->property, xe->selection);
510 r.xselection.property = xe->property;
511 if(xe->target == _x.targets){
513 a[1] = _x.utf8string;
515 a[3] = _x.compoundtext;
517 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
518 8, PropModeReplace, (uchar*)a, sizeof a);
519 }else if(xe->target == XA_STRING
520 || xe->target == _x.utf8string
521 || xe->target == _x.text
522 || xe->target == _x.compoundtext
523 || ((name = XGetAtomName(xd, xe->target)) && strcmp(name, "text/plain;charset=UTF-8") == 0)){
524 /* text/plain;charset=UTF-8 seems nonstandard but is used by Synergy */
525 /* if the target is STRING we're supposed to reply with Latin1 XXX */
527 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
528 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
531 if(strcmp(name, "TIMESTAMP") != 0)
532 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
533 r.xselection.property = None;
536 r.xselection.display = xe->display;
537 /* r.xselection.property filled above */
538 r.xselection.target = xe->target;
539 r.xselection.type = SelectionNotify;
540 r.xselection.requestor = xe->requestor;
541 r.xselection.time = xe->time;
542 r.xselection.send_event = True;
543 r.xselection.selection = xe->selection;
544 XSendEvent(xd, xe->requestor, False, 0, &r);
556 CFIndex nflavor, ndata, j;
560 PasteboardSyncFlags flags;
563 /* fprint(2, "applegetsnarf\n"); */
565 if(clip.apple == nil){
566 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
567 fprint(2, "apple pasteboard create failed\n");
572 flags = PasteboardSynchronize(clip.apple);
573 if(flags&kPasteboardClientIsOwner){
574 s = strdup(clip.buf);
578 if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){
579 fprint(2, "apple pasteboard get item count failed\n");
583 for(i=1; i<=nitem; i++){
584 if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr)
586 if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr)
588 nflavor = CFArrayGetCount(flavors);
589 for(j=0; j<nflavor; j++){
590 type = (CFStringRef)CFArrayGetValueAtIndex(flavors, j);
591 if(!UTTypeConformsTo(type, CFSTR("public.utf16-plain-text")))
593 if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr)
595 ndata = CFDataGetLength(data);
597 s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data));
612 appleputsnarf(char *s)
615 PasteboardSyncFlags flags;
617 /* fprint(2, "appleputsnarf\n"); */
619 if(strlen(s) >= SnarfSize)
623 runesnprint(clip.rbuf, nelem(clip.rbuf), "%s", s);
624 if(clip.apple == nil){
625 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
626 fprint(2, "apple pasteboard create failed\n");
631 if(PasteboardClear(clip.apple) != noErr){
632 fprint(2, "apple pasteboard clear failed\n");
636 flags = PasteboardSynchronize(clip.apple);
637 if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
638 fprint(2, "apple pasteboard cannot assert ownership\n");
642 cfdata = CFDataCreate(kCFAllocatorDefault,
643 (uchar*)clip.rbuf, runestrlen(clip.rbuf)*2);
645 fprint(2, "apple pasteboard cfdatacreate failed\n");
649 if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1,
650 CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
651 fprint(2, "apple pasteboard putitem failed\n");
656 /* CFRelease(cfdata); ??? */
659 #endif /* APPLESNARF */
667 _xputsnarf(_x.snarfcon, data);
673 return _xgetsnarf(_x.snarfcon);