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 == XK_Multi_key || 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 k = Kalt;
117 break;
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 extern int _latin1(Rune*, int);
140 static Rune*
141 xtoplan9latin1(XEvent *e)
143 static Rune k[10];
144 static int alting, nk;
145 int n;
146 int r;
148 r = __xtoplan9kbd(e);
149 if(r < 0)
150 return nil;
151 if(alting){
152 /*
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.
156 */
157 if(r == Kalt){
158 alting = 0;
159 return nil;
161 k[nk++] = r;
162 n = _latin1(k, nk);
163 if(n > 0){
164 alting = 0;
165 k[0] = n;
166 k[1] = 0;
167 return k;
169 if(n == -1){
170 alting = 0;
171 k[nk] = 0;
172 return k;
174 /* n < -1, need more input */
175 return nil;
176 }else if(r == Kalt){
177 alting = 1;
178 nk = 0;
179 return nil;
180 }else{
181 k[0] = r;
182 k[1] = 0;
183 return k;
187 int
188 _xtoplan9kbd(XEvent *e)
190 static Rune *r;
192 if(e == (XEvent*)-1){
193 assert(r);
194 r--;
195 return 0;
197 if(e)
198 r = xtoplan9latin1(e);
199 if(r && *r)
200 return *r++;
201 return -1;
204 int
205 _xtoplan9mouse(XEvent *e, Mouse *m)
207 int s;
208 XButtonEvent *be;
209 XMotionEvent *me;
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);
216 XFlush(_x.display);
219 switch(e->type){
220 case ButtonPress:
221 be = (XButtonEvent*)e;
222 /*
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
226 * get to us.
227 */
228 if(be->send_event
229 && (~be->state&0xFFFF)==0
230 && (~be->button&0xFF)==0)
231 return -1;
232 /* BUG? on mac need to inherit these from elsewhere? */
233 m->xy.x = be->x;
234 m->xy.y = be->y;
235 s = be->state;
236 m->msec = be->time;
237 switch(be->button){
238 case 1:
239 s |= Button1Mask;
240 break;
241 case 2:
242 s |= Button2Mask;
243 break;
244 case 3:
245 s |= Button3Mask;
246 break;
247 case 4:
248 s |= Button4Mask;
249 break;
250 case 5:
251 s |= Button5Mask;
252 break;
254 break;
255 case ButtonRelease:
256 be = (XButtonEvent*)e;
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;
280 case MotionNotify:
281 me = (XMotionEvent*)e;
282 s = me->state;
283 m->xy.x = me->x;
284 m->xy.y = me->y;
285 m->msec = me->time;
286 break;
288 default:
289 return -1;
292 m->buttons = 0;
293 if(s & Button1Mask)
294 m->buttons |= 1;
295 if(s & Button2Mask)
296 m->buttons |= 2;
297 if(s & Button3Mask)
298 m->buttons |= 4;
299 if(s & Button4Mask)
300 m->buttons |= 8;
301 if(s & Button5Mask)
302 m->buttons |= 16;
303 return 0;
306 void
307 _xmoveto(Point p)
309 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
310 XFlush(_x.display);
313 static int
314 revbyte(int b)
316 int r;
318 r = 0;
319 r |= (b&0x01) << 7;
320 r |= (b&0x02) << 5;
321 r |= (b&0x04) << 3;
322 r |= (b&0x08) << 1;
323 r |= (b&0x10) >> 1;
324 r |= (b&0x20) >> 3;
325 r |= (b&0x40) >> 5;
326 r |= (b&0x80) >> 7;
327 return r;
330 static void
331 xcursorarrow(void)
333 if(_x.cursor != 0){
334 XFreeCursor(_x.display, _x.cursor);
335 _x.cursor = 0;
337 XUndefineCursor(_x.display, _x.drawable);
338 XFlush(_x.display);
342 void
343 _xsetcursor(Cursor *c)
345 XColor fg, bg;
346 XCursor xc;
347 Pixmap xsrc, xmask;
348 int i;
349 uchar src[2*16], mask[2*16];
351 if(c == nil){
352 xcursorarrow();
353 return;
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]);
360 fg = _x.map[0];
361 bg = _x.map[255];
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);
365 if(xc != 0) {
366 XDefineCursor(_x.display, _x.drawable, xc);
367 if(_x.cursor != 0)
368 XFreeCursor(_x.display, _x.cursor);
369 _x.cursor = xc;
371 XFreePixmap(_x.display, xsrc);
372 XFreePixmap(_x.display, xmask);
373 XFlush(_x.display);
376 struct {
377 QLock lk;
378 char buf[SnarfSize];
379 #ifdef APPLESNARF
380 Rune rbuf[SnarfSize];
381 PasteboardRef apple;
382 #endif
383 } clip;
385 char*
386 _xgetsnarf(void)
388 uchar *data, *xdata;
389 Atom clipboard, type, prop;
390 ulong len, lastlen, dummy;
391 int fmt, i;
392 XWindow w;
394 qlock(&clip.lk);
395 /*
396 * Have we snarfed recently and the X server hasn't caught up?
397 */
398 if(_x.putsnarf != _x.assertsnarf)
399 goto mine;
401 /*
402 * Is there a primary selection (highlighted text in an xterm)?
403 */
404 clipboard = XA_PRIMARY;
405 w = XGetSelectionOwner(_x.display, XA_PRIMARY);
406 if(w == _x.drawable){
407 mine:
408 data = (uchar*)strdup(clip.buf);
409 goto out;
412 /*
413 * If not, is there a clipboard selection?
414 */
415 if(w == None && _x.clipboard != None){
416 clipboard = _x.clipboard;
417 w = XGetSelectionOwner(_x.display, _x.clipboard);
418 if(w == _x.drawable)
419 goto mine;
422 /*
423 * If not, give up.
424 */
425 if(w == None){
426 data = nil;
427 goto out;
430 /*
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.
438 */
439 prop = 1;
440 XChangeProperty(_x.display, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
441 XConvertSelection(_x.display, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
442 XFlush(_x.display);
443 lastlen = 0;
444 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
445 usleep(100*1000);
446 XGetWindowProperty(_x.display, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
447 &type, &fmt, &dummy, &len, &data);
448 if(lastlen == len && len > 0)
449 break;
450 lastlen = len;
452 if(i == 10){
453 data = nil;
454 goto out;
456 /* get the property */
457 data = nil;
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){
461 if(xdata)
462 XFree(xdata);
463 data = nil;
464 }else{
465 if(xdata){
466 data = (uchar*)strdup((char*)xdata);
467 XFree(xdata);
468 }else
469 data = nil;
471 out:
472 qunlock(&clip.lk);
473 return (char*)data;
476 void
477 __xputsnarf(char *data)
479 XButtonEvent e;
481 if(strlen(data) >= SnarfSize)
482 return;
483 qlock(&clip.lk);
484 strcpy(clip.buf, data);
485 /* leave note for mouse proc to assert selection ownership */
486 _x.putsnarf++;
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;
492 e.state = ~0;
493 e.button = ~0;
494 XSendEvent(_x.display, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
495 XFlush(_x.display);
496 qunlock(&clip.lk);
499 int
500 _xselect(XEvent *e)
502 char *name;
503 XEvent r;
504 XSelectionRequestEvent *xe;
505 Atom a[4];
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){
513 a[0] = XA_STRING;
514 a[1] = _x.utf8string;
515 a[2] = _x.text;
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 */
527 qlock(&clip.lk);
528 XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
529 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
530 qunlock(&clip.lk);
531 }else{
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);
546 XFlush(_x.display);
547 return 0;
550 #ifdef APPLESNARF
551 char*
552 _applegetsnarf(void)
554 char *s, *t;
555 CFArrayRef flavors;
556 CFDataRef data;
557 CFIndex nflavor, ndata, j;
558 CFStringRef type;
559 ItemCount nitem;
560 PasteboardItemID id;
561 PasteboardSyncFlags flags;
562 UInt32 i;
564 /* fprint(2, "applegetsnarf\n"); */
565 qlock(&clip.lk);
566 if(clip.apple == nil){
567 if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
568 fprint(2, "apple pasteboard create failed\n");
569 qunlock(&clip.lk);
570 return nil;
573 flags = PasteboardSynchronize(clip.apple);
574 if(flags&kPasteboardClientIsOwner){
575 s = strdup(clip.buf);
576 qunlock(&clip.lk);
577 return s;
579 if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){
580 fprint(2, "apple pasteboard get item count failed\n");
581 qunlock(&clip.lk);
582 return nil;
584 for(i=1; i<=nitem; i++){
585 if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr)
586 continue;
587 if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr)
588 continue;
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")))
593 continue;
594 if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr)
595 continue;
596 ndata = CFDataGetLength(data);
597 qunlock(&clip.lk);
598 s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data));
599 CFRelease(flavors);
600 CFRelease(data);
601 for(t=s; *t; t++)
602 if(*t == '\r')
603 *t = '\n';
604 return s;
606 CFRelease(flavors);
608 qunlock(&clip.lk);
609 return nil;
612 void
613 _appleputsnarf(char *s)
615 CFDataRef cfdata;
616 PasteboardSyncFlags flags;
618 /* fprint(2, "appleputsnarf\n"); */
620 if(strlen(s) >= SnarfSize)
621 return;
622 qlock(&clip.lk);
623 strcpy(clip.buf, s);
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");
628 qunlock(&clip.lk);
629 return;
632 if(PasteboardClear(clip.apple) != noErr){
633 fprint(2, "apple pasteboard clear failed\n");
634 qunlock(&clip.lk);
635 return;
637 flags = PasteboardSynchronize(clip.apple);
638 if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
639 fprint(2, "apple pasteboard cannot assert ownership\n");
640 qunlock(&clip.lk);
641 return;
643 cfdata = CFDataCreate(kCFAllocatorDefault,
644 (uchar*)clip.rbuf, runestrlen(clip.rbuf)*2);
645 if(cfdata == nil){
646 fprint(2, "apple pasteboard cfdatacreate failed\n");
647 qunlock(&clip.lk);
648 return;
650 if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1,
651 CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
652 fprint(2, "apple pasteboard putitem failed\n");
653 CFRelease(cfdata);
654 qunlock(&clip.lk);
655 return;
657 /* CFRelease(cfdata); ??? */
658 qunlock(&clip.lk);
660 #endif /* APPLESNARF */
662 void
663 _xputsnarf(char *data)
665 #ifdef APPLESNARF
666 _appleputsnarf(data);
667 #endif
668 __xputsnarf(data);
671 /*
672 * Send the mouse event back to the window manager.
673 * So that 9term can tell rio to pop up its button3 menu.
674 */
675 void
676 _xbouncemouse(Mouse *m)
678 XButtonEvent e;
679 XWindow dw;
681 e.type = ButtonPress;
682 e.state = 0;
683 e.button = 0;
684 if(m->buttons&1)
685 e.button = 1;
686 else if(m->buttons&2)
687 e.button = 2;
688 else if(m->buttons&4)
689 e.button = 3;
690 e.same_screen = 1;
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);
695 e.window = e.root;
696 e.subwindow = None;
697 e.x = e.x_root;
698 e.y = e.y_root;
699 #undef time
700 e.time = CurrentTime;
701 XUngrabPointer(_x.display, m->msec);
702 XSendEvent(_x.display, e.root, True, ButtonPressMask, (XEvent*)&e);
703 XFlush(_x.display);