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)
139 extern int _latin1(Rune*, int);
141 xtoplan9latin1(XEvent *e)
144 static int alting, nk;
148 r = __xtoplan9kbd(e);
153 * Kludge for Mac's X11 3-button emulation.
154 * It treats Command+Button as button 3, but also
155 * ends up sending XK_Meta_L twice.
174 /* n < -1, need more input */
188 _xtoplan9kbd(XEvent *e)
192 if(e == (XEvent*)-1){
198 r = xtoplan9latin1(e);
205 _xtoplan9mouse(XEvent *e, Mouse *m)
211 if(_x.putsnarf != _x.assertsnarf){
212 _x.assertsnarf = _x.putsnarf;
213 XSetSelectionOwner(_x.display, XA_PRIMARY, _x.drawable, CurrentTime);
214 if(_x.clipboard != None)
215 XSetSelectionOwner(_x.display, _x.clipboard, _x.drawable, CurrentTime);
221 be = (XButtonEvent*)e;
223 * Fake message, just sent to make us announce snarf.
224 * Apparently state and button are 16 and 8 bits on
225 * the wire, since they are truncated by the time they
229 && (~be->state&0xFFFF)==0
230 && (~be->button&0xFF)==0)
232 /* BUG? on mac need to inherit these from elsewhere? */
256 be = (XButtonEvent*)e;
281 me = (XMotionEvent*)e;
309 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
334 XFreeCursor(_x.display, _x.cursor);
337 XUndefineCursor(_x.display, _x.drawable);
343 _xsetcursor(Cursor *c)
349 uchar src[2*16], mask[2*16];
355 for(i=0; i<2*16; i++){
356 src[i] = revbyte(c->set[i]);
357 mask[i] = revbyte(c->set[i] | c->clr[i]);
362 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
363 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
364 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
366 XDefineCursor(_x.display, _x.drawable, xc);
368 XFreeCursor(_x.display, _x.cursor);
371 XFreePixmap(_x.display, xsrc);
372 XFreePixmap(_x.display, xmask);
380 Rune rbuf[SnarfSize];
386 _xgetsnarffrom(XWindow w, Atom clipboard, Atom target, int timeout0, int timeout)
389 ulong len, lastlen, dummy;
394 * We should be waiting for SelectionNotify here, but it might never
395 * come, and we have no way to time out. Instead, we will clear
396 * local property #1, request our buddy to fill it in for us, and poll
397 * until he's done or we get tired of waiting.
400 XChangeProperty(_x.display, _x.drawable, prop, target, 8, PropModeReplace, (uchar*)"", 0);
401 XConvertSelection(_x.display, clipboard, target, prop, _x.drawable, CurrentTime);
404 timeout0 = (timeout0 + 9)/10;
405 timeout = (timeout + 9)/10;
406 for(i=0; i<timeout0 || (lastlen!=0 && i<timeout); i++){
408 XGetWindowProperty(_x.display, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
409 &type, &fmt, &dummy, &len, &xdata);
410 if(lastlen == len && len > 0)
418 /* get the property */
420 XGetWindowProperty(_x.display, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
421 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
422 if((type != target && type != XA_STRING && type != _x.utf8string) || len == 0){
428 data = (uchar*)strdup((char*)xdata);
444 * Have we snarfed recently and the X server hasn't caught up?
446 if(_x.putsnarf != _x.assertsnarf)
450 * Is there a primary selection (highlighted text in an xterm)?
452 clipboard = XA_PRIMARY;
453 w = XGetSelectionOwner(_x.display, XA_PRIMARY);
454 if(w == _x.drawable){
456 data = (uchar*)strdup(clip.buf);
461 * If not, is there a clipboard selection?
463 if(w == None && _x.clipboard != None){
464 clipboard = _x.clipboard;
465 w = XGetSelectionOwner(_x.display, _x.clipboard);
478 if((data = _xgetsnarffrom(w, clipboard, _x.utf8string, 10, 100)) == nil)
479 if((data = _xgetsnarffrom(w, clipboard, XA_STRING, 10, 100)) == nil){
480 /* nothing left to do */
489 __xputsnarf(char *data)
493 if(strlen(data) >= SnarfSize)
496 strcpy(clip.buf, data);
497 /* leave note for mouse proc to assert selection ownership */
500 /* send mouse a fake event so snarf is announced */
501 memset(&e, 0, sizeof e);
502 e.type = ButtonPress;
503 e.window = _x.drawable;
506 XSendEvent(_x.display, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
516 XSelectionRequestEvent *xe;
519 memset(&r, 0, sizeof r);
520 xe = (XSelectionRequestEvent*)e;
521 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
522 xe->target, xe->requestor, xe->property, xe->selection);
523 r.xselection.property = xe->property;
524 if(xe->target == _x.targets){
525 a[0] = _x.utf8string;
528 a[3] = _x.compoundtext;
530 XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
531 8*sizeof(a[0]), PropModeReplace, (uchar*)a, nelem(a));
532 }else if(xe->target == XA_STRING
533 || xe->target == _x.utf8string
534 || xe->target == _x.text
535 || xe->target == _x.compoundtext
536 || ((name = XGetAtomName(_x.display, xe->target)) && strcmp(name, "text/plain;charset=UTF-8") == 0)){
537 /* text/plain;charset=UTF-8 seems nonstandard but is used by Synergy */
538 /* if the target is STRING we're supposed to reply with Latin1 XXX */
540 XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
541 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
544 if(strcmp(name, "TIMESTAMP") != 0)
545 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
546 r.xselection.property = None;
549 r.xselection.display = xe->display;
550 /* r.xselection.property filled above */
551 r.xselection.target = xe->target;
552 r.xselection.type = SelectionNotify;
553 r.xselection.requestor = xe->requestor;
554 r.xselection.time = xe->time;
555 r.xselection.send_event = True;
556 r.xselection.selection = xe->selection;
557 XSendEvent(_x.display, xe->requestor, False, 0, &r);
569 CFIndex nflavor, ndata, j;
573 PasteboardSyncFlags flags;
576 /* fprint(2, "applegetsnarf\n"); */
578 if(clip.apple == nil){
579 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
580 fprint(2, "apple pasteboard create failed\n");
585 flags = PasteboardSynchronize(clip.apple);
586 if(flags&kPasteboardClientIsOwner){
587 s = strdup(clip.buf);
591 if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){
592 fprint(2, "apple pasteboard get item count failed\n");
596 for(i=1; i<=nitem; i++){
597 if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr)
599 if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr)
601 nflavor = CFArrayGetCount(flavors);
602 for(j=0; j<nflavor; j++){
603 type = (CFStringRef)CFArrayGetValueAtIndex(flavors, j);
604 if(!UTTypeConformsTo(type, CFSTR("public.utf16-plain-text")))
606 if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr)
608 ndata = CFDataGetLength(data);
610 s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data));
625 _appleputsnarf(char *s)
628 PasteboardSyncFlags flags;
630 /* fprint(2, "appleputsnarf\n"); */
632 if(strlen(s) >= SnarfSize)
636 runesnprint(clip.rbuf, nelem(clip.rbuf), "%s", s);
637 if(clip.apple == nil){
638 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
639 fprint(2, "apple pasteboard create failed\n");
644 if(PasteboardClear(clip.apple) != noErr){
645 fprint(2, "apple pasteboard clear failed\n");
649 flags = PasteboardSynchronize(clip.apple);
650 if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
651 fprint(2, "apple pasteboard cannot assert ownership\n");
655 cfdata = CFDataCreate(kCFAllocatorDefault,
656 (uchar*)clip.rbuf, runestrlen(clip.rbuf)*2);
658 fprint(2, "apple pasteboard cfdatacreate failed\n");
662 if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1,
663 CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
664 fprint(2, "apple pasteboard putitem failed\n");
669 /* CFRelease(cfdata); ??? */
672 #endif /* APPLESNARF */
675 _xputsnarf(char *data)
678 _appleputsnarf(data);
684 * Send the mouse event back to the window manager.
685 * So that 9term can tell rio to pop up its button3 menu.
688 _xbouncemouse(Mouse *m)
693 e.type = ButtonPress;
698 else if(m->buttons&2)
700 else if(m->buttons&4)
703 XTranslateCoordinates(_x.display, _x.drawable,
704 DefaultRootWindow(_x.display),
705 m->xy.x, m->xy.y, &e.x_root, &e.y_root, &dw);
706 e.root = DefaultRootWindow(_x.display);
712 e.time = CurrentTime;
713 XUngrabPointer(_x.display, m->msec);
714 XSendEvent(_x.display, e.root, True, ButtonPressMask, (XEvent*)&e);