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);
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)
147 static Rune* sendrune(Rune);
149 extern int _latin1(Rune*, int);
151 xtoplan9latin1(XEvent *e)
155 r = __xtoplan9kbd(e);
176 * Kludge for Mac's X11 3-button emulation.
177 * It treats Command+Button as button 3, but also
178 * ends up sending XK_Meta_L twice.
197 /* n < -1, need more input */
211 _xtoplan9kbd(XEvent *e)
215 if(e == (XEvent*)-1){
221 r = xtoplan9latin1(e);
228 _xtoplan9mouse(XEvent *e, Mouse *m)
234 if(_x.putsnarf != _x.assertsnarf){
235 _x.assertsnarf = _x.putsnarf;
236 XSetSelectionOwner(_x.display, XA_PRIMARY, _x.drawable, CurrentTime);
237 if(_x.clipboard != None)
238 XSetSelectionOwner(_x.display, _x.clipboard, _x.drawable, CurrentTime);
244 be = (XButtonEvent*)e;
247 * Fake message, just sent to make us announce snarf.
248 * Apparently state and button are 16 and 8 bits on
249 * the wire, since they are truncated by the time they
253 && (~be->state&0xFFFF)==0
254 && (~be->button&0xFF)==0)
256 /* BUG? on mac need to inherit these from elsewhere? */
280 be = (XButtonEvent*)e;
305 me = (XMotionEvent*)e;
310 return 0; // do not set buttons
333 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
358 XFreeCursor(_x.display, _x.cursor);
361 XUndefineCursor(_x.display, _x.drawable);
367 _xsetcursor(Cursor *c)
373 uchar src[2*16], mask[2*16];
379 for(i=0; i<2*16; i++){
380 src[i] = revbyte(c->set[i]);
381 mask[i] = revbyte(c->set[i] | c->clr[i]);
386 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
387 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
388 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
390 XDefineCursor(_x.display, _x.drawable, xc);
392 XFreeCursor(_x.display, _x.cursor);
395 XFreePixmap(_x.display, xsrc);
396 XFreePixmap(_x.display, xmask);
404 Rune rbuf[SnarfSize];
410 _xgetsnarffrom(XWindow w, Atom clipboard, Atom target, int timeout0, int timeout)
413 ulong len, lastlen, dummy;
418 * We should be waiting for SelectionNotify here, but it might never
419 * come, and we have no way to time out. Instead, we will clear
420 * local property #1, request our buddy to fill it in for us, and poll
421 * until he's done or we get tired of waiting.
424 XChangeProperty(_x.display, _x.drawable, prop, target, 8, PropModeReplace, (uchar*)"", 0);
425 XConvertSelection(_x.display, clipboard, target, prop, _x.drawable, CurrentTime);
428 timeout0 = (timeout0 + 9)/10;
429 timeout = (timeout + 9)/10;
430 for(i=0; i<timeout0 || (lastlen!=0 && i<timeout); i++){
432 XGetWindowProperty(_x.display, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
433 &type, &fmt, &dummy, &len, &xdata);
434 if(lastlen == len && len > 0){
444 /* get the property */
446 XGetWindowProperty(_x.display, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
447 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
448 if((type != target && type != XA_STRING && type != _x.utf8string) || len == 0){
454 data = (uchar*)strdup((char*)xdata);
470 * Have we snarfed recently and the X server hasn't caught up?
472 if(_x.putsnarf != _x.assertsnarf)
476 * Is there a primary selection (highlighted text in an xterm)?
478 clipboard = XA_PRIMARY;
479 w = XGetSelectionOwner(_x.display, XA_PRIMARY);
480 if(w == _x.drawable){
482 data = (uchar*)strdup(clip.buf);
487 * If not, is there a clipboard selection?
489 if(w == None && _x.clipboard != None){
490 clipboard = _x.clipboard;
491 w = XGetSelectionOwner(_x.display, _x.clipboard);
504 if((data = _xgetsnarffrom(w, clipboard, _x.utf8string, 10, 100)) == nil)
505 if((data = _xgetsnarffrom(w, clipboard, XA_STRING, 10, 100)) == nil){
506 /* nothing left to do */
515 __xputsnarf(char *data)
519 if(strlen(data) >= SnarfSize)
522 strcpy(clip.buf, data);
523 /* leave note for mouse proc to assert selection ownership */
526 /* send mouse a fake event so snarf is announced */
527 memset(&e, 0, sizeof e);
528 e.type = ButtonPress;
529 e.window = _x.drawable;
532 XSendEvent(_x.display, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
542 XSelectionRequestEvent *xe;
545 memset(&r, 0, sizeof r);
546 xe = (XSelectionRequestEvent*)e;
547 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d (sizeof atom=%d)\n",
548 xe->target, xe->requestor, xe->property, xe->selection, sizeof a[0]);
549 r.xselection.property = xe->property;
550 if(xe->target == _x.targets){
551 a[0] = _x.utf8string;
554 a[3] = _x.compoundtext;
555 XChangeProperty(_x.display, xe->requestor, xe->property, XA_ATOM,
556 32, PropModeReplace, (uchar*)a, nelem(a));
557 }else if(xe->target == XA_STRING
558 || xe->target == _x.utf8string
559 || xe->target == _x.text
560 || xe->target == _x.compoundtext
561 || ((name = XGetAtomName(_x.display, xe->target)) && strcmp(name, "text/plain;charset=UTF-8") == 0)){
562 /* text/plain;charset=UTF-8 seems nonstandard but is used by Synergy */
563 /* if the target is STRING we're supposed to reply with Latin1 XXX */
565 XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
566 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
569 if(strcmp(name, "TIMESTAMP") != 0)
570 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
571 r.xselection.property = None;
574 r.xselection.display = xe->display;
575 /* r.xselection.property filled above */
576 r.xselection.target = xe->target;
577 r.xselection.type = SelectionNotify;
578 r.xselection.requestor = xe->requestor;
579 r.xselection.time = xe->time;
580 r.xselection.send_event = True;
581 r.xselection.selection = xe->selection;
582 XSendEvent(_x.display, xe->requestor, False, 0, &r);
594 CFIndex nflavor, ndata, j;
598 PasteboardSyncFlags flags;
601 /* fprint(2, "applegetsnarf\n"); */
603 if(clip.apple == nil){
604 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
605 fprint(2, "apple pasteboard create failed\n");
610 flags = PasteboardSynchronize(clip.apple);
611 if(flags&kPasteboardClientIsOwner){
612 s = strdup(clip.buf);
616 if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){
617 fprint(2, "apple pasteboard get item count failed\n");
621 for(i=1; i<=nitem; i++){
622 if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr)
624 if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr)
626 nflavor = CFArrayGetCount(flavors);
627 for(j=0; j<nflavor; j++){
628 type = (CFStringRef)CFArrayGetValueAtIndex(flavors, j);
629 if(!UTTypeConformsTo(type, CFSTR("public.utf16-plain-text")))
631 if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr)
633 ndata = CFDataGetLength(data);
635 s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data));
650 _appleputsnarf(char *s)
653 PasteboardSyncFlags flags;
655 /* fprint(2, "appleputsnarf\n"); */
657 if(strlen(s) >= SnarfSize)
661 runesnprint(clip.rbuf, nelem(clip.rbuf), "%s", s);
662 if(clip.apple == nil){
663 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
664 fprint(2, "apple pasteboard create failed\n");
669 if(PasteboardClear(clip.apple) != noErr){
670 fprint(2, "apple pasteboard clear failed\n");
674 flags = PasteboardSynchronize(clip.apple);
675 if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
676 fprint(2, "apple pasteboard cannot assert ownership\n");
680 cfdata = CFDataCreate(kCFAllocatorDefault,
681 (uchar*)clip.rbuf, runestrlen(clip.rbuf)*2);
683 fprint(2, "apple pasteboard cfdatacreate failed\n");
687 if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1,
688 CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
689 fprint(2, "apple pasteboard putitem failed\n");
694 /* CFRelease(cfdata); ??? */
697 #endif /* APPLESNARF */
700 _xputsnarf(char *data)
703 _appleputsnarf(data);
709 * Send the mouse event back to the window manager.
710 * So that 9term can tell rio to pop up its button3 menu.
713 _xbouncemouse(Mouse *m)
718 e.type = ButtonPress;
723 else if(m->buttons&2)
725 else if(m->buttons&4)
728 XTranslateCoordinates(_x.display, _x.drawable,
729 DefaultRootWindow(_x.display),
730 m->xy.x, m->xy.y, &e.x_root, &e.y_root, &dw);
731 e.root = DefaultRootWindow(_x.display);
737 e.time = CurrentTime;
738 XUngrabPointer(_x.display, m->msec);
739 XSendEvent(_x.display, e.root, True, ButtonPressMask, (XEvent*)&e);