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];
389 Atom clipboard, type, prop;
390 ulong len, lastlen, dummy;
396 * Have we snarfed recently and the X server hasn't caught up?
398 if(_x.putsnarf != _x.assertsnarf)
402 * Is there a primary selection (highlighted text in an xterm)?
404 clipboard = XA_PRIMARY;
405 w = XGetSelectionOwner(_x.display, XA_PRIMARY);
406 if(w == _x.drawable){
408 data = (uchar*)strdup(clip.buf);
413 * If not, is there a clipboard selection?
415 if(w == None && _x.clipboard != None){
416 clipboard = _x.clipboard;
417 w = XGetSelectionOwner(_x.display, _x.clipboard);
431 * We should be waiting for SelectionNotify here, but it might never
432 * come, and we have no way to time out. Instead, we will clear
433 * local property #1, request our buddy to fill it in for us, and poll
434 * until he's done or we get tired of waiting.
436 * We should try to go for _x.utf8string instead of XA_STRING,
437 * but that would add to the polling.
440 XChangeProperty(_x.display, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
441 XConvertSelection(_x.display, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
444 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
446 XGetWindowProperty(_x.display, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
447 &type, &fmt, &dummy, &len, &data);
448 if(lastlen == len && len > 0)
456 /* get the property */
458 XGetWindowProperty(_x.display, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
459 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
460 if((type != XA_STRING && type != _x.utf8string) || len == 0){
466 data = (uchar*)strdup((char*)xdata);
477 __xputsnarf(char *data)
481 if(strlen(data) >= SnarfSize)
484 strcpy(clip.buf, data);
485 /* leave note for mouse proc to assert selection ownership */
488 /* send mouse a fake event so snarf is announced */
489 memset(&e, 0, sizeof e);
490 e.type = ButtonPress;
491 e.window = _x.drawable;
494 XSendEvent(_x.display, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
504 XSelectionRequestEvent *xe;
507 memset(&r, 0, sizeof r);
508 xe = (XSelectionRequestEvent*)e;
509 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
510 xe->target, xe->requestor, xe->property, xe->selection);
511 r.xselection.property = xe->property;
512 if(xe->target == _x.targets){
514 a[1] = _x.utf8string;
516 a[3] = _x.compoundtext;
518 XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
519 8, PropModeReplace, (uchar*)a, sizeof a);
520 }else if(xe->target == XA_STRING
521 || xe->target == _x.utf8string
522 || xe->target == _x.text
523 || xe->target == _x.compoundtext
524 || ((name = XGetAtomName(_x.display, xe->target)) && strcmp(name, "text/plain;charset=UTF-8") == 0)){
525 /* text/plain;charset=UTF-8 seems nonstandard but is used by Synergy */
526 /* if the target is STRING we're supposed to reply with Latin1 XXX */
528 XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
529 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
532 if(strcmp(name, "TIMESTAMP") != 0)
533 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
534 r.xselection.property = None;
537 r.xselection.display = xe->display;
538 /* r.xselection.property filled above */
539 r.xselection.target = xe->target;
540 r.xselection.type = SelectionNotify;
541 r.xselection.requestor = xe->requestor;
542 r.xselection.time = xe->time;
543 r.xselection.send_event = True;
544 r.xselection.selection = xe->selection;
545 XSendEvent(_x.display, xe->requestor, False, 0, &r);
557 CFIndex nflavor, ndata, j;
561 PasteboardSyncFlags flags;
564 /* fprint(2, "applegetsnarf\n"); */
566 if(clip.apple == nil){
567 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
568 fprint(2, "apple pasteboard create failed\n");
573 flags = PasteboardSynchronize(clip.apple);
574 if(flags&kPasteboardClientIsOwner){
575 s = strdup(clip.buf);
579 if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){
580 fprint(2, "apple pasteboard get item count failed\n");
584 for(i=1; i<=nitem; i++){
585 if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr)
587 if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr)
589 nflavor = CFArrayGetCount(flavors);
590 for(j=0; j<nflavor; j++){
591 type = (CFStringRef)CFArrayGetValueAtIndex(flavors, j);
592 if(!UTTypeConformsTo(type, CFSTR("public.utf16-plain-text")))
594 if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr)
596 ndata = CFDataGetLength(data);
598 s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data));
613 _appleputsnarf(char *s)
616 PasteboardSyncFlags flags;
618 /* fprint(2, "appleputsnarf\n"); */
620 if(strlen(s) >= SnarfSize)
624 runesnprint(clip.rbuf, nelem(clip.rbuf), "%s", s);
625 if(clip.apple == nil){
626 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
627 fprint(2, "apple pasteboard create failed\n");
632 if(PasteboardClear(clip.apple) != noErr){
633 fprint(2, "apple pasteboard clear failed\n");
637 flags = PasteboardSynchronize(clip.apple);
638 if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
639 fprint(2, "apple pasteboard cannot assert ownership\n");
643 cfdata = CFDataCreate(kCFAllocatorDefault,
644 (uchar*)clip.rbuf, runestrlen(clip.rbuf)*2);
646 fprint(2, "apple pasteboard cfdatacreate failed\n");
650 if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1,
651 CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
652 fprint(2, "apple pasteboard putitem failed\n");
657 /* CFRelease(cfdata); ??? */
660 #endif /* APPLESNARF */
663 _xputsnarf(char *data)
666 _appleputsnarf(data);
672 * Send the mouse event back to the window manager.
673 * So that 9term can tell rio to pop up its button3 menu.
676 _xbouncemouse(Mouse *m)
681 e.type = ButtonPress;
686 else if(m->buttons&2)
688 else if(m->buttons&4)
691 XTranslateCoordinates(_x.display, _x.drawable,
692 DefaultRootWindow(_x.display),
693 m->xy.x, m->xy.y, &e.x_root, &e.y_root, &dw);
694 e.root = DefaultRootWindow(_x.display);
700 e.time = CurrentTime;
701 XUngrabPointer(_x.display, m->msec);
702 XSendEvent(_x.display, e.root, True, ButtonPressMask, (XEvent*)&e);