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 break;
436 lastlen = len;
437 XFree(xdata);
439 if(len == 0)
440 return nil;
442 /* get the property */
443 xdata = nil;
444 XGetWindowProperty(_x.display, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
445 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
446 if((type != target && type != XA_STRING && type != _x.utf8string) || len == 0){
447 if(xdata)
448 XFree(xdata);
449 return nil;
451 if(xdata){
452 data = (uchar*)strdup((char*)xdata);
453 XFree(xdata);
454 return data;
456 return nil;
459 char*
460 _xgetsnarf(void)
462 uchar *data;
463 Atom clipboard;
464 XWindow w;
466 qlock(&clip.lk);
467 /*
468 * Have we snarfed recently and the X server hasn't caught up?
469 */
470 if(_x.putsnarf != _x.assertsnarf)
471 goto mine;
473 /*
474 * Is there a primary selection (highlighted text in an xterm)?
475 */
476 clipboard = XA_PRIMARY;
477 w = XGetSelectionOwner(_x.display, XA_PRIMARY);
478 if(w == _x.drawable){
479 mine:
480 data = (uchar*)strdup(clip.buf);
481 goto out;
484 /*
485 * If not, is there a clipboard selection?
486 */
487 if(w == None && _x.clipboard != None){
488 clipboard = _x.clipboard;
489 w = XGetSelectionOwner(_x.display, _x.clipboard);
490 if(w == _x.drawable)
491 goto mine;
494 /*
495 * If not, give up.
496 */
497 if(w == None){
498 data = nil;
499 goto out;
502 if((data = _xgetsnarffrom(w, clipboard, _x.utf8string, 10, 100)) == nil)
503 if((data = _xgetsnarffrom(w, clipboard, XA_STRING, 10, 100)) == nil){
504 /* nothing left to do */
507 out:
508 qunlock(&clip.lk);
509 return (char*)data;
512 void
513 __xputsnarf(char *data)
515 XButtonEvent e;
517 if(strlen(data) >= SnarfSize)
518 return;
519 qlock(&clip.lk);
520 strcpy(clip.buf, data);
521 /* leave note for mouse proc to assert selection ownership */
522 _x.putsnarf++;
524 /* send mouse a fake event so snarf is announced */
525 memset(&e, 0, sizeof e);
526 e.type = ButtonPress;
527 e.window = _x.drawable;
528 e.state = ~0;
529 e.button = ~0;
530 XSendEvent(_x.display, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
531 XFlush(_x.display);
532 qunlock(&clip.lk);
535 int
536 _xselect(XEvent *e)
538 char *name;
539 XEvent r;
540 XSelectionRequestEvent *xe;
541 Atom a[4];
543 memset(&r, 0, sizeof r);
544 xe = (XSelectionRequestEvent*)e;
545 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d (sizeof atom=%d)\n",
546 xe->target, xe->requestor, xe->property, xe->selection, sizeof a[0]);
547 r.xselection.property = xe->property;
548 if(xe->target == _x.targets){
549 a[0] = _x.utf8string;
550 a[1] = XA_STRING;
551 a[2] = _x.text;
552 a[3] = _x.compoundtext;
553 XChangeProperty(_x.display, xe->requestor, xe->property, XA_ATOM,
554 32, PropModeReplace, (uchar*)a, nelem(a));
555 }else if(xe->target == XA_STRING
556 || xe->target == _x.utf8string
557 || xe->target == _x.text
558 || xe->target == _x.compoundtext
559 || ((name = XGetAtomName(_x.display, xe->target)) && strcmp(name, "text/plain;charset=UTF-8") == 0)){
560 /* text/plain;charset=UTF-8 seems nonstandard but is used by Synergy */
561 /* if the target is STRING we're supposed to reply with Latin1 XXX */
562 qlock(&clip.lk);
563 XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
564 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
565 qunlock(&clip.lk);
566 }else{
567 if(strcmp(name, "TIMESTAMP") != 0)
568 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
569 r.xselection.property = None;
572 r.xselection.display = xe->display;
573 /* r.xselection.property filled above */
574 r.xselection.target = xe->target;
575 r.xselection.type = SelectionNotify;
576 r.xselection.requestor = xe->requestor;
577 r.xselection.time = xe->time;
578 r.xselection.send_event = True;
579 r.xselection.selection = xe->selection;
580 XSendEvent(_x.display, xe->requestor, False, 0, &r);
581 XFlush(_x.display);
582 return 0;
585 #ifdef APPLESNARF
586 char*
587 _applegetsnarf(void)
589 char *s, *t;
590 CFArrayRef flavors;
591 CFDataRef data;
592 CFIndex nflavor, ndata, j;
593 CFStringRef type;
594 ItemCount nitem;
595 PasteboardItemID id;
596 PasteboardSyncFlags flags;
597 UInt32 i;
599 /* fprint(2, "applegetsnarf\n"); */
600 qlock(&clip.lk);
601 if(clip.apple == nil){
602 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
603 fprint(2, "apple pasteboard create failed\n");
604 qunlock(&clip.lk);
605 return nil;
608 flags = PasteboardSynchronize(clip.apple);
609 if(flags&kPasteboardClientIsOwner){
610 s = strdup(clip.buf);
611 qunlock(&clip.lk);
612 return s;
614 if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){
615 fprint(2, "apple pasteboard get item count failed\n");
616 qunlock(&clip.lk);
617 return nil;
619 for(i=1; i<=nitem; i++){
620 if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr)
621 continue;
622 if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr)
623 continue;
624 nflavor = CFArrayGetCount(flavors);
625 for(j=0; j<nflavor; j++){
626 type = (CFStringRef)CFArrayGetValueAtIndex(flavors, j);
627 if(!UTTypeConformsTo(type, CFSTR("public.utf16-plain-text")))
628 continue;
629 if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr)
630 continue;
631 ndata = CFDataGetLength(data);
632 qunlock(&clip.lk);
633 s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data));
634 CFRelease(flavors);
635 CFRelease(data);
636 for(t=s; *t; t++)
637 if(*t == '\r')
638 *t = '\n';
639 return s;
641 CFRelease(flavors);
643 qunlock(&clip.lk);
644 return nil;
647 void
648 _appleputsnarf(char *s)
650 CFDataRef cfdata;
651 PasteboardSyncFlags flags;
653 /* fprint(2, "appleputsnarf\n"); */
655 if(strlen(s) >= SnarfSize)
656 return;
657 qlock(&clip.lk);
658 strcpy(clip.buf, s);
659 runesnprint(clip.rbuf, nelem(clip.rbuf), "%s", s);
660 if(clip.apple == nil){
661 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
662 fprint(2, "apple pasteboard create failed\n");
663 qunlock(&clip.lk);
664 return;
667 if(PasteboardClear(clip.apple) != noErr){
668 fprint(2, "apple pasteboard clear failed\n");
669 qunlock(&clip.lk);
670 return;
672 flags = PasteboardSynchronize(clip.apple);
673 if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
674 fprint(2, "apple pasteboard cannot assert ownership\n");
675 qunlock(&clip.lk);
676 return;
678 cfdata = CFDataCreate(kCFAllocatorDefault,
679 (uchar*)clip.rbuf, runestrlen(clip.rbuf)*2);
680 if(cfdata == nil){
681 fprint(2, "apple pasteboard cfdatacreate failed\n");
682 qunlock(&clip.lk);
683 return;
685 if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1,
686 CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
687 fprint(2, "apple pasteboard putitem failed\n");
688 CFRelease(cfdata);
689 qunlock(&clip.lk);
690 return;
692 /* CFRelease(cfdata); ??? */
693 qunlock(&clip.lk);
695 #endif /* APPLESNARF */
697 void
698 _xputsnarf(char *data)
700 #ifdef APPLESNARF
701 _appleputsnarf(data);
702 #endif
703 __xputsnarf(data);
706 /*
707 * Send the mouse event back to the window manager.
708 * So that 9term can tell rio to pop up its button3 menu.
709 */
710 void
711 _xbouncemouse(Mouse *m)
713 XButtonEvent e;
714 XWindow dw;
716 e.type = ButtonPress;
717 e.state = 0;
718 e.button = 0;
719 if(m->buttons&1)
720 e.button = 1;
721 else if(m->buttons&2)
722 e.button = 2;
723 else if(m->buttons&4)
724 e.button = 3;
725 e.same_screen = 1;
726 XTranslateCoordinates(_x.display, _x.drawable,
727 DefaultRootWindow(_x.display),
728 m->xy.x, m->xy.y, &e.x_root, &e.y_root, &dw);
729 e.root = DefaultRootWindow(_x.display);
730 e.window = e.root;
731 e.subwindow = None;
732 e.x = e.x_root;
733 e.y = e.y_root;
734 #undef time
735 e.time = CurrentTime;
736 XUngrabPointer(_x.display, m->msec);
737 XSendEvent(_x.display, e.root, True, ButtonPressMask, (XEvent*)&e);
738 XFlush(_x.display);