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 == 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 k = Kalt;
118 break;
119 default: /* not ISO-1 or tty control */
120 if(k>0xff) {
121 k = _p9keysym2ucs(k);
122 if(k==-1) return -1;
127 /* Compensate for servers that call a minus a hyphen */
128 if(k == XK_hyphen)
129 k = XK_minus;
130 /* Do control mapping ourselves if translator doesn't */
131 if(e->xkey.state&ControlMask && k != Kalt)
132 k &= 0x9f;
133 if(k == NoSymbol) {
134 return -1;
137 return k+0;
140 extern int _latin1(Rune*, int);
141 static Rune*
142 xtoplan9latin1(XEvent *e)
144 static Rune k[10];
145 static int alting, nk;
146 int n;
147 int r;
149 r = __xtoplan9kbd(e);
150 if(r < 0)
151 return nil;
152 if(alting){
153 /*
154 * Kludge for Mac's X11 3-button emulation.
155 * It treats Command+Button as button 3, but also
156 * ends up sending XK_Meta_L twice.
157 */
158 if(r == Kalt){
159 alting = 0;
160 return nil;
162 k[nk++] = r;
163 n = _latin1(k, nk);
164 if(n > 0){
165 alting = 0;
166 k[0] = n;
167 k[1] = 0;
168 return k;
170 if(n == -1){
171 alting = 0;
172 k[nk] = 0;
173 return k;
175 /* n < -1, need more input */
176 return nil;
177 }else if(r == Kalt){
178 alting = 1;
179 nk = 0;
180 return nil;
181 }else{
182 k[0] = r;
183 k[1] = 0;
184 return k;
188 int
189 _xtoplan9kbd(XEvent *e)
191 static Rune *r;
193 if(e == (XEvent*)-1){
194 assert(r);
195 r--;
196 return 0;
198 if(e)
199 r = xtoplan9latin1(e);
200 if(r && *r)
201 return *r++;
202 return -1;
205 int
206 _xtoplan9mouse(XEvent *e, Mouse *m)
208 int s;
209 XButtonEvent *be;
210 XMotionEvent *me;
212 if(_x.putsnarf != _x.assertsnarf){
213 _x.assertsnarf = _x.putsnarf;
214 XSetSelectionOwner(_x.display, XA_PRIMARY, _x.drawable, CurrentTime);
215 if(_x.clipboard != None)
216 XSetSelectionOwner(_x.display, _x.clipboard, _x.drawable, CurrentTime);
217 XFlush(_x.display);
220 switch(e->type){
221 case ButtonPress:
222 be = (XButtonEvent*)e;
223 /*
224 * Fake message, just sent to make us announce snarf.
225 * Apparently state and button are 16 and 8 bits on
226 * the wire, since they are truncated by the time they
227 * get to us.
228 */
229 if(be->send_event
230 && (~be->state&0xFFFF)==0
231 && (~be->button&0xFF)==0)
232 return -1;
233 /* BUG? on mac need to inherit these from elsewhere? */
234 m->xy.x = be->x;
235 m->xy.y = be->y;
236 s = be->state;
237 m->msec = be->time;
238 switch(be->button){
239 case 1:
240 s |= Button1Mask;
241 break;
242 case 2:
243 s |= Button2Mask;
244 break;
245 case 3:
246 s |= Button3Mask;
247 break;
248 case 4:
249 s |= Button4Mask;
250 break;
251 case 5:
252 s |= Button5Mask;
253 break;
255 break;
256 case ButtonRelease:
257 be = (XButtonEvent*)e;
258 m->xy.x = be->x;
259 m->xy.y = be->y;
260 s = be->state;
261 m->msec = be->time;
262 switch(be->button){
263 case 1:
264 s &= ~Button1Mask;
265 break;
266 case 2:
267 s &= ~Button2Mask;
268 break;
269 case 3:
270 s &= ~Button3Mask;
271 break;
272 case 4:
273 s &= ~Button4Mask;
274 break;
275 case 5:
276 s &= ~Button5Mask;
277 break;
279 break;
281 case MotionNotify:
282 me = (XMotionEvent*)e;
283 s = me->state;
284 m->xy.x = me->x;
285 m->xy.y = me->y;
286 m->msec = me->time;
287 break;
289 default:
290 return -1;
293 m->buttons = 0;
294 if(s & Button1Mask)
295 m->buttons |= 1;
296 if(s & Button2Mask)
297 m->buttons |= 2;
298 if(s & Button3Mask)
299 m->buttons |= 4;
300 if(s & Button4Mask)
301 m->buttons |= 8;
302 if(s & Button5Mask)
303 m->buttons |= 16;
304 return 0;
307 void
308 _xmoveto(Point p)
310 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
311 XFlush(_x.display);
314 static int
315 revbyte(int b)
317 int r;
319 r = 0;
320 r |= (b&0x01) << 7;
321 r |= (b&0x02) << 5;
322 r |= (b&0x04) << 3;
323 r |= (b&0x08) << 1;
324 r |= (b&0x10) >> 1;
325 r |= (b&0x20) >> 3;
326 r |= (b&0x40) >> 5;
327 r |= (b&0x80) >> 7;
328 return r;
331 static void
332 xcursorarrow(void)
334 if(_x.cursor != 0){
335 XFreeCursor(_x.display, _x.cursor);
336 _x.cursor = 0;
338 XUndefineCursor(_x.display, _x.drawable);
339 XFlush(_x.display);
343 void
344 _xsetcursor(Cursor *c)
346 XColor fg, bg;
347 XCursor xc;
348 Pixmap xsrc, xmask;
349 int i;
350 uchar src[2*16], mask[2*16];
352 if(c == nil){
353 xcursorarrow();
354 return;
356 for(i=0; i<2*16; i++){
357 src[i] = revbyte(c->set[i]);
358 mask[i] = revbyte(c->set[i] | c->clr[i]);
361 fg = _x.map[0];
362 bg = _x.map[255];
363 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
364 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
365 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
366 if(xc != 0) {
367 XDefineCursor(_x.display, _x.drawable, xc);
368 if(_x.cursor != 0)
369 XFreeCursor(_x.display, _x.cursor);
370 _x.cursor = xc;
372 XFreePixmap(_x.display, xsrc);
373 XFreePixmap(_x.display, xmask);
374 XFlush(_x.display);
377 struct {
378 QLock lk;
379 char buf[SnarfSize];
380 #ifdef APPLESNARF
381 Rune rbuf[SnarfSize];
382 PasteboardRef apple;
383 #endif
384 } clip;
386 static uchar*
387 _xgetsnarffrom(XWindow w, Atom clipboard, Atom target, int timeout0, int timeout)
389 Atom prop, type;
390 ulong len, lastlen, dummy;
391 int fmt, i;
392 uchar *data, *xdata;
394 /*
395 * We should be waiting for SelectionNotify here, but it might never
396 * come, and we have no way to time out. Instead, we will clear
397 * local property #1, request our buddy to fill it in for us, and poll
398 * until he's done or we get tired of waiting.
399 */
400 prop = 1;
401 XChangeProperty(_x.display, _x.drawable, prop, target, 8, PropModeReplace, (uchar*)"", 0);
402 XConvertSelection(_x.display, clipboard, target, prop, _x.drawable, CurrentTime);
403 XFlush(_x.display);
404 lastlen = 0;
405 timeout0 = (timeout0 + 9)/10;
406 timeout = (timeout + 9)/10;
407 for(i=0; i<timeout0 || (lastlen!=0 && i<timeout); i++){
408 usleep(10*1000);
409 XGetWindowProperty(_x.display, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
410 &type, &fmt, &dummy, &len, &xdata);
411 if(lastlen == len && len > 0)
412 break;
413 lastlen = len;
414 XFree(xdata);
416 if(len == 0)
417 return nil;
419 /* get the property */
420 xdata = nil;
421 XGetWindowProperty(_x.display, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
422 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
423 if((type != target && type != XA_STRING && type != _x.utf8string) || len == 0){
424 if(xdata)
425 XFree(xdata);
426 return nil;
428 if(xdata){
429 data = (uchar*)strdup((char*)xdata);
430 XFree(xdata);
431 return data;
433 return nil;
436 char*
437 _xgetsnarf(void)
439 uchar *data;
440 Atom clipboard;
441 XWindow w;
443 qlock(&clip.lk);
444 /*
445 * Have we snarfed recently and the X server hasn't caught up?
446 */
447 if(_x.putsnarf != _x.assertsnarf)
448 goto mine;
450 /*
451 * Is there a primary selection (highlighted text in an xterm)?
452 */
453 clipboard = XA_PRIMARY;
454 w = XGetSelectionOwner(_x.display, XA_PRIMARY);
455 if(w == _x.drawable){
456 mine:
457 data = (uchar*)strdup(clip.buf);
458 goto out;
461 /*
462 * If not, is there a clipboard selection?
463 */
464 if(w == None && _x.clipboard != None){
465 clipboard = _x.clipboard;
466 w = XGetSelectionOwner(_x.display, _x.clipboard);
467 if(w == _x.drawable)
468 goto mine;
471 /*
472 * If not, give up.
473 */
474 if(w == None){
475 data = nil;
476 goto out;
479 if((data = _xgetsnarffrom(w, clipboard, _x.utf8string, 10, 100)) == nil)
480 if((data = _xgetsnarffrom(w, clipboard, XA_STRING, 10, 100)) == nil){
481 /* nothing left to do */
484 out:
485 qunlock(&clip.lk);
486 return (char*)data;
489 void
490 __xputsnarf(char *data)
492 XButtonEvent e;
494 if(strlen(data) >= SnarfSize)
495 return;
496 qlock(&clip.lk);
497 strcpy(clip.buf, data);
498 /* leave note for mouse proc to assert selection ownership */
499 _x.putsnarf++;
501 /* send mouse a fake event so snarf is announced */
502 memset(&e, 0, sizeof e);
503 e.type = ButtonPress;
504 e.window = _x.drawable;
505 e.state = ~0;
506 e.button = ~0;
507 XSendEvent(_x.display, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
508 XFlush(_x.display);
509 qunlock(&clip.lk);
512 int
513 _xselect(XEvent *e)
515 char *name;
516 XEvent r;
517 XSelectionRequestEvent *xe;
518 Atom a[4];
520 memset(&r, 0, sizeof r);
521 xe = (XSelectionRequestEvent*)e;
522 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d (sizeof atom=%d)\n",
523 xe->target, xe->requestor, xe->property, xe->selection, sizeof a[0]);
524 r.xselection.property = xe->property;
525 if(xe->target == _x.targets){
526 a[0] = _x.utf8string;
527 a[1] = XA_STRING;
528 a[2] = _x.text;
529 a[3] = _x.compoundtext;
530 XChangeProperty(_x.display, xe->requestor, xe->property, XA_ATOM,
531 32, 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 */
539 qlock(&clip.lk);
540 XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
541 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
542 qunlock(&clip.lk);
543 }else{
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);
558 XFlush(_x.display);
559 return 0;
562 #ifdef APPLESNARF
563 char*
564 _applegetsnarf(void)
566 char *s, *t;
567 CFArrayRef flavors;
568 CFDataRef data;
569 CFIndex nflavor, ndata, j;
570 CFStringRef type;
571 ItemCount nitem;
572 PasteboardItemID id;
573 PasteboardSyncFlags flags;
574 UInt32 i;
576 /* fprint(2, "applegetsnarf\n"); */
577 qlock(&clip.lk);
578 if(clip.apple == nil){
579 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
580 fprint(2, "apple pasteboard create failed\n");
581 qunlock(&clip.lk);
582 return nil;
585 flags = PasteboardSynchronize(clip.apple);
586 if(flags&kPasteboardClientIsOwner){
587 s = strdup(clip.buf);
588 qunlock(&clip.lk);
589 return s;
591 if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){
592 fprint(2, "apple pasteboard get item count failed\n");
593 qunlock(&clip.lk);
594 return nil;
596 for(i=1; i<=nitem; i++){
597 if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr)
598 continue;
599 if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr)
600 continue;
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")))
605 continue;
606 if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr)
607 continue;
608 ndata = CFDataGetLength(data);
609 qunlock(&clip.lk);
610 s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data));
611 CFRelease(flavors);
612 CFRelease(data);
613 for(t=s; *t; t++)
614 if(*t == '\r')
615 *t = '\n';
616 return s;
618 CFRelease(flavors);
620 qunlock(&clip.lk);
621 return nil;
624 void
625 _appleputsnarf(char *s)
627 CFDataRef cfdata;
628 PasteboardSyncFlags flags;
630 /* fprint(2, "appleputsnarf\n"); */
632 if(strlen(s) >= SnarfSize)
633 return;
634 qlock(&clip.lk);
635 strcpy(clip.buf, s);
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");
640 qunlock(&clip.lk);
641 return;
644 if(PasteboardClear(clip.apple) != noErr){
645 fprint(2, "apple pasteboard clear failed\n");
646 qunlock(&clip.lk);
647 return;
649 flags = PasteboardSynchronize(clip.apple);
650 if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
651 fprint(2, "apple pasteboard cannot assert ownership\n");
652 qunlock(&clip.lk);
653 return;
655 cfdata = CFDataCreate(kCFAllocatorDefault,
656 (uchar*)clip.rbuf, runestrlen(clip.rbuf)*2);
657 if(cfdata == nil){
658 fprint(2, "apple pasteboard cfdatacreate failed\n");
659 qunlock(&clip.lk);
660 return;
662 if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1,
663 CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
664 fprint(2, "apple pasteboard putitem failed\n");
665 CFRelease(cfdata);
666 qunlock(&clip.lk);
667 return;
669 /* CFRelease(cfdata); ??? */
670 qunlock(&clip.lk);
672 #endif /* APPLESNARF */
674 void
675 _xputsnarf(char *data)
677 #ifdef APPLESNARF
678 _appleputsnarf(data);
679 #endif
680 __xputsnarf(data);
683 /*
684 * Send the mouse event back to the window manager.
685 * So that 9term can tell rio to pop up its button3 menu.
686 */
687 void
688 _xbouncemouse(Mouse *m)
690 XButtonEvent e;
691 XWindow dw;
693 e.type = ButtonPress;
694 e.state = 0;
695 e.button = 0;
696 if(m->buttons&1)
697 e.button = 1;
698 else if(m->buttons&2)
699 e.button = 2;
700 else if(m->buttons&4)
701 e.button = 3;
702 e.same_screen = 1;
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);
707 e.window = e.root;
708 e.subwindow = None;
709 e.x = e.x_root;
710 e.y = e.y_root;
711 #undef time
712 e.time = CurrentTime;
713 XUngrabPointer(_x.display, m->msec);
714 XSendEvent(_x.display, e.root, True, ButtonPressMask, (XEvent*)&e);
715 XFlush(_x.display);