Blob


1 /* input event and data structure translation */
3 #include <u.h>
4 #include "x11-inc.h"
5 #ifdef __APPLE__
6 #define APPLESNARF
7 #define Boolean AppleBoolean
8 #define Rect AppleRect
9 #define EventMask AppleEventMask
10 #define Point ApplePoint
11 #define Cursor AppleCursor
12 #include <Carbon/Carbon.h>
13 AUTOFRAMEWORK(Carbon)
14 #undef Boolean
15 #undef Rect
16 #undef EventMask
17 #undef Point
18 #undef Cursor
19 #endif
20 #include <libc.h>
21 #include <draw.h>
22 #include <memdraw.h>
23 #include <mouse.h>
24 #include <cursor.h>
25 #include <keyboard.h>
26 #include "x11-memdraw.h"
27 #include "x11-keysym2ucs.h"
28 #undef time
30 static KeySym
31 __xtoplan9kbd(XEvent *e)
32 {
33 KeySym k;
35 if(e->xany.type != KeyPress)
36 return -1;
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 == NoSymbol)
41 return -1;
43 if(k&0xFF00){
44 switch(k){
45 case XK_BackSpace:
46 case XK_Tab:
47 case XK_Escape:
48 case XK_Delete:
49 case XK_KP_0:
50 case XK_KP_1:
51 case XK_KP_2:
52 case XK_KP_3:
53 case XK_KP_4:
54 case XK_KP_5:
55 case XK_KP_6:
56 case XK_KP_7:
57 case XK_KP_8:
58 case XK_KP_9:
59 case XK_KP_Divide:
60 case XK_KP_Multiply:
61 case XK_KP_Subtract:
62 case XK_KP_Add:
63 case XK_KP_Decimal:
64 k &= 0x7F;
65 break;
66 case XK_Linefeed:
67 k = '\r';
68 break;
69 case XK_KP_Space:
70 k = ' ';
71 break;
72 case XK_Home:
73 case XK_KP_Home:
74 k = Khome;
75 break;
76 case XK_Left:
77 case XK_KP_Left:
78 k = Kleft;
79 break;
80 case XK_Up:
81 case XK_KP_Up:
82 k = Kup;
83 break;
84 case XK_Down:
85 case XK_KP_Down:
86 k = Kdown;
87 break;
88 case XK_Right:
89 case XK_KP_Right:
90 k = Kright;
91 break;
92 case XK_Page_Down:
93 case XK_KP_Page_Down:
94 k = Kpgdown;
95 break;
96 case XK_End:
97 case XK_KP_End:
98 k = Kend;
99 break;
100 case XK_Page_Up:
101 case XK_KP_Page_Up:
102 k = Kpgup;
103 break;
104 case XK_Insert:
105 case XK_KP_Insert:
106 k = Kins;
107 break;
108 case XK_KP_Enter:
109 case XK_Return:
110 k = '\n';
111 break;
112 case XK_Alt_L:
113 case XK_Meta_L: /* Shift Alt on PCs */
114 case XK_Alt_R:
115 case XK_Meta_R: /* Shift Alt on PCs */
116 case XK_Multi_key:
117 return -1;
118 default: /* not ISO-1 or tty control */
119 if(k>0xff) {
120 k = _p9keysym2ucs(k);
121 if(k==-1) return -1;
126 /* Compensate for servers that call a minus a hyphen */
127 if(k == XK_hyphen)
128 k = XK_minus;
129 /* Do control mapping ourselves if translator doesn't */
130 if(e->xkey.state&ControlMask)
131 k &= 0x9f;
132 if(k == NoSymbol) {
133 return -1;
136 return k+0;
139 int alting;
141 void
142 abortcompose(void)
144 alting = 0;
147 static Rune* sendrune(Rune);
149 extern int _latin1(Rune*, int);
150 static Rune*
151 xtoplan9latin1(XEvent *e)
153 Rune r;
155 r = __xtoplan9kbd(e);
156 if(r == -1)
157 return nil;
158 return sendrune(r);
161 void
162 sendalt(void)
164 sendrune(Kalt);
167 static Rune*
168 sendrune(Rune r)
170 static Rune k[10];
171 static int nk;
172 int n;
174 if(alting){
175 /*
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.
179 */
180 if(r == Kalt){
181 alting = 0;
182 return nil;
184 k[nk++] = r;
185 n = _latin1(k, nk);
186 if(n > 0){
187 alting = 0;
188 k[0] = n;
189 k[1] = 0;
190 return k;
192 if(n == -1){
193 alting = 0;
194 k[nk] = 0;
195 return k;
197 /* n < -1, need more input */
198 return nil;
199 }else if(r == Kalt){
200 alting = 1;
201 nk = 0;
202 return nil;
203 }else{
204 k[0] = r;
205 k[1] = 0;
206 return k;
210 int
211 _xtoplan9kbd(XEvent *e)
213 static Rune *r;
215 if(e == (XEvent*)-1){
216 assert(r);
217 r--;
218 return 0;
220 if(e)
221 r = xtoplan9latin1(e);
222 if(r && *r)
223 return *r++;
224 return -1;
227 int
228 _xtoplan9mouse(XEvent *e, Mouse *m)
230 int s;
231 XButtonEvent *be;
232 XMotionEvent *me;
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);
239 XFlush(_x.display);
242 switch(e->type){
243 case ButtonPress:
244 be = (XButtonEvent*)e;
246 /*
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
250 * get to us.
251 */
252 if(be->send_event
253 && (~be->state&0xFFFF)==0
254 && (~be->button&0xFF)==0)
255 return -1;
256 /* BUG? on mac need to inherit these from elsewhere? */
257 m->xy.x = be->x;
258 m->xy.y = be->y;
259 s = be->state;
260 m->msec = be->time;
261 switch(be->button){
262 case 1:
263 s |= Button1Mask;
264 break;
265 case 2:
266 s |= Button2Mask;
267 break;
268 case 3:
269 s |= Button3Mask;
270 break;
271 case 4:
272 s |= Button4Mask;
273 break;
274 case 5:
275 s |= Button5Mask;
276 break;
278 break;
279 case ButtonRelease:
280 be = (XButtonEvent*)e;
281 m->xy.x = be->x;
282 m->xy.y = be->y;
283 s = be->state;
284 m->msec = be->time;
285 switch(be->button){
286 case 1:
287 s &= ~Button1Mask;
288 break;
289 case 2:
290 s &= ~Button2Mask;
291 break;
292 case 3:
293 s &= ~Button3Mask;
294 break;
295 case 4:
296 s &= ~Button4Mask;
297 break;
298 case 5:
299 s &= ~Button5Mask;
300 break;
302 break;
304 case MotionNotify:
305 me = (XMotionEvent*)e;
306 s = me->state;
307 m->xy.x = me->x;
308 m->xy.y = me->y;
309 m->msec = me->time;
310 return 0; // do not set buttons
312 default:
313 return -1;
316 m->buttons = 0;
317 if(s & Button1Mask)
318 m->buttons |= 1;
319 if(s & Button2Mask)
320 m->buttons |= 2;
321 if(s & Button3Mask)
322 m->buttons |= 4;
323 if(s & Button4Mask)
324 m->buttons |= 8;
325 if(s & Button5Mask)
326 m->buttons |= 16;
327 return 0;
330 void
331 _xmoveto(Point p)
333 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
334 XFlush(_x.display);
337 static int
338 revbyte(int b)
340 int r;
342 r = 0;
343 r |= (b&0x01) << 7;
344 r |= (b&0x02) << 5;
345 r |= (b&0x04) << 3;
346 r |= (b&0x08) << 1;
347 r |= (b&0x10) >> 1;
348 r |= (b&0x20) >> 3;
349 r |= (b&0x40) >> 5;
350 r |= (b&0x80) >> 7;
351 return r;
354 static void
355 xcursorarrow(void)
357 if(_x.cursor != 0){
358 XFreeCursor(_x.display, _x.cursor);
359 _x.cursor = 0;
361 XUndefineCursor(_x.display, _x.drawable);
362 XFlush(_x.display);
366 void
367 _xsetcursor(Cursor *c)
369 XColor fg, bg;
370 XCursor xc;
371 Pixmap xsrc, xmask;
372 int i;
373 uchar src[2*16], mask[2*16];
375 if(c == nil){
376 xcursorarrow();
377 return;
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]);
384 fg = _x.map[0];
385 bg = _x.map[255];
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);
389 if(xc != 0) {
390 XDefineCursor(_x.display, _x.drawable, xc);
391 if(_x.cursor != 0)
392 XFreeCursor(_x.display, _x.cursor);
393 _x.cursor = xc;
395 XFreePixmap(_x.display, xsrc);
396 XFreePixmap(_x.display, xmask);
397 XFlush(_x.display);
400 struct {
401 QLock lk;
402 char buf[SnarfSize];
403 #ifdef APPLESNARF
404 Rune rbuf[SnarfSize];
405 PasteboardRef apple;
406 #endif
407 } clip;
409 static uchar*
410 _xgetsnarffrom(XWindow w, Atom clipboard, Atom target, int timeout0, int timeout)
412 Atom prop, type;
413 ulong len, lastlen, dummy;
414 int fmt, i;
415 uchar *data, *xdata;
417 /*
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.
422 */
423 prop = 1;
424 XChangeProperty(_x.display, _x.drawable, prop, target, 8, PropModeReplace, (uchar*)"", 0);
425 XConvertSelection(_x.display, clipboard, target, prop, _x.drawable, CurrentTime);
426 XFlush(_x.display);
427 lastlen = 0;
428 timeout0 = (timeout0 + 9)/10;
429 timeout = (timeout + 9)/10;
430 for(i=0; i<timeout0 || (lastlen!=0 && i<timeout); i++){
431 usleep(10*1000);
432 XGetWindowProperty(_x.display, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
433 &type, &fmt, &dummy, &len, &xdata);
434 if(lastlen == len && len > 0){
435 XFree(xdata);
436 break;
438 lastlen = len;
439 XFree(xdata);
441 if(len == 0)
442 return nil;
444 /* get the property */
445 xdata = nil;
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){
449 if(xdata)
450 XFree(xdata);
451 return nil;
453 if(xdata){
454 data = (uchar*)strdup((char*)xdata);
455 XFree(xdata);
456 return data;
458 return nil;
461 char*
462 _xgetsnarf(void)
464 uchar *data;
465 Atom clipboard;
466 XWindow w;
468 qlock(&clip.lk);
469 /*
470 * Have we snarfed recently and the X server hasn't caught up?
471 */
472 if(_x.putsnarf != _x.assertsnarf)
473 goto mine;
475 /*
476 * Is there a primary selection (highlighted text in an xterm)?
477 */
478 clipboard = XA_PRIMARY;
479 w = XGetSelectionOwner(_x.display, XA_PRIMARY);
480 if(w == _x.drawable){
481 mine:
482 data = (uchar*)strdup(clip.buf);
483 goto out;
486 /*
487 * If not, is there a clipboard selection?
488 */
489 if(w == None && _x.clipboard != None){
490 clipboard = _x.clipboard;
491 w = XGetSelectionOwner(_x.display, _x.clipboard);
492 if(w == _x.drawable)
493 goto mine;
496 /*
497 * If not, give up.
498 */
499 if(w == None){
500 data = nil;
501 goto out;
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 */
509 out:
510 qunlock(&clip.lk);
511 return (char*)data;
514 void
515 __xputsnarf(char *data)
517 XButtonEvent e;
519 if(strlen(data) >= SnarfSize)
520 return;
521 qlock(&clip.lk);
522 strcpy(clip.buf, data);
523 /* leave note for mouse proc to assert selection ownership */
524 _x.putsnarf++;
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;
530 e.state = ~0;
531 e.button = ~0;
532 XSendEvent(_x.display, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
533 XFlush(_x.display);
534 qunlock(&clip.lk);
537 int
538 _xselect(XEvent *e)
540 char *name;
541 XEvent r;
542 XSelectionRequestEvent *xe;
543 Atom a[4];
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;
552 a[1] = XA_STRING;
553 a[2] = _x.text;
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 */
564 qlock(&clip.lk);
565 XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
566 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
567 qunlock(&clip.lk);
568 }else{
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);
583 XFlush(_x.display);
584 return 0;
587 #ifdef APPLESNARF
588 char*
589 _applegetsnarf(void)
591 char *s, *t;
592 CFArrayRef flavors;
593 CFDataRef data;
594 CFIndex nflavor, ndata, j;
595 CFStringRef type;
596 ItemCount nitem;
597 PasteboardItemID id;
598 PasteboardSyncFlags flags;
599 UInt32 i;
601 /* fprint(2, "applegetsnarf\n"); */
602 qlock(&clip.lk);
603 if(clip.apple == nil){
604 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
605 fprint(2, "apple pasteboard create failed\n");
606 qunlock(&clip.lk);
607 return nil;
610 flags = PasteboardSynchronize(clip.apple);
611 if(flags&kPasteboardClientIsOwner){
612 s = strdup(clip.buf);
613 qunlock(&clip.lk);
614 return s;
616 if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){
617 fprint(2, "apple pasteboard get item count failed\n");
618 qunlock(&clip.lk);
619 return nil;
621 for(i=1; i<=nitem; i++){
622 if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr)
623 continue;
624 if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr)
625 continue;
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")))
630 continue;
631 if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr)
632 continue;
633 ndata = CFDataGetLength(data);
634 qunlock(&clip.lk);
635 s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data));
636 CFRelease(flavors);
637 CFRelease(data);
638 for(t=s; *t; t++)
639 if(*t == '\r')
640 *t = '\n';
641 return s;
643 CFRelease(flavors);
645 qunlock(&clip.lk);
646 return nil;
649 void
650 _appleputsnarf(char *s)
652 CFDataRef cfdata;
653 PasteboardSyncFlags flags;
655 /* fprint(2, "appleputsnarf\n"); */
657 if(strlen(s) >= SnarfSize)
658 return;
659 qlock(&clip.lk);
660 strcpy(clip.buf, s);
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");
665 qunlock(&clip.lk);
666 return;
669 if(PasteboardClear(clip.apple) != noErr){
670 fprint(2, "apple pasteboard clear failed\n");
671 qunlock(&clip.lk);
672 return;
674 flags = PasteboardSynchronize(clip.apple);
675 if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
676 fprint(2, "apple pasteboard cannot assert ownership\n");
677 qunlock(&clip.lk);
678 return;
680 cfdata = CFDataCreate(kCFAllocatorDefault,
681 (uchar*)clip.rbuf, runestrlen(clip.rbuf)*2);
682 if(cfdata == nil){
683 fprint(2, "apple pasteboard cfdatacreate failed\n");
684 qunlock(&clip.lk);
685 return;
687 if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1,
688 CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
689 fprint(2, "apple pasteboard putitem failed\n");
690 CFRelease(cfdata);
691 qunlock(&clip.lk);
692 return;
694 /* CFRelease(cfdata); ??? */
695 qunlock(&clip.lk);
697 #endif /* APPLESNARF */
699 void
700 _xputsnarf(char *data)
702 #ifdef APPLESNARF
703 _appleputsnarf(data);
704 #endif
705 __xputsnarf(data);
708 /*
709 * Send the mouse event back to the window manager.
710 * So that 9term can tell rio to pop up its button3 menu.
711 */
712 void
713 _xbouncemouse(Mouse *m)
715 XButtonEvent e;
716 XWindow dw;
718 e.type = ButtonPress;
719 e.state = 0;
720 e.button = 0;
721 if(m->buttons&1)
722 e.button = 1;
723 else if(m->buttons&2)
724 e.button = 2;
725 else if(m->buttons&4)
726 e.button = 3;
727 e.same_screen = 1;
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);
732 e.window = e.root;
733 e.subwindow = None;
734 e.x = e.x_root;
735 e.y = e.y_root;
736 #undef time
737 e.time = CurrentTime;
738 XUngrabPointer(_x.display, m->msec);
739 XSendEvent(_x.display, e.root, True, ButtonPressMask, (XEvent*)&e);
740 XFlush(_x.display);