1 /* input event and data structure translation */
11 #include "x11-memdraw.h"
12 #include "x11-keysym2ucs.h"
18 __xtoplan9kbd(XEvent *e)
22 if(e->xany.type != KeyPress)
24 needstack(20*1024); /* X has some *huge* buffers in openobject */
25 XLookupString((XKeyEvent*)e,NULL,0,&k,NULL);
26 if(k == XK_Multi_key || k == NoSymbol)
99 case XK_Meta_L: /* Shift Alt on PCs */
101 case XK_Meta_R: /* Shift Alt on PCs */
104 default: /* not ISO-1 or tty control */
106 k = _p9keysym2ucs(k);
112 /* Compensate for servers that call a minus a hyphen */
115 /* Do control mapping ourselves if translator doesn't */
116 if(e->xkey.state&ControlMask)
126 xtoplan9latin1(XEvent *e)
129 static int alting, nk;
133 r = __xtoplan9kbd(e);
138 * Kludge for Mac's X11 3-button emulation.
139 * It treats Command+Button as button 3, but also
140 * ends up sending XK_Meta_L twice.
159 /* n < -1, need more input */
173 _xtoplan9kbd(XEvent *e)
177 if(e == (XEvent*)-1){
183 r = xtoplan9latin1(e);
190 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
196 if(_x.putsnarf != _x.assertsnarf){
197 _x.assertsnarf = _x.putsnarf;
198 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);
199 if(_x.clipboard != None)
200 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);
206 be = (XButtonEvent*)e;
208 * Fake message, just sent to make us announce snarf.
209 * Apparently state and button are 16 and 8 bits on
210 * the wire, since they are truncated by the time they
214 && (~be->state&0xFFFF)==0
215 && (~be->button&0xFF)==0)
217 /* BUG? on mac need to inherit these from elsewhere? */
241 be = (XButtonEvent*)e;
266 me = (XMotionEvent*)e;
294 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
319 XFreeCursor(_x.display, _x.cursor);
322 XUndefineCursor(_x.display, _x.drawable);
328 _xsetcursor(Cursor *c)
334 uchar src[2*16], mask[2*16];
340 for(i=0; i<2*16; i++){
341 src[i] = revbyte(c->set[i]);
342 mask[i] = revbyte(c->set[i] | c->clr[i]);
347 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
348 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
349 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
351 XDefineCursor(_x.display, _x.drawable, xc);
353 XFreeCursor(_x.display, _x.cursor);
356 XFreePixmap(_x.display, xsrc);
357 XFreePixmap(_x.display, xmask);
367 _xgetsnarf(XDisplay *xd)
370 Atom clipboard, type, prop;
371 ulong len, lastlen, dummy;
377 * Is there a primary selection (highlighted text in an xterm)?
379 clipboard = XA_PRIMARY;
380 w = XGetSelectionOwner(xd, XA_PRIMARY);
381 if(w == _x.drawable){
383 data = (uchar*)strdup(clip.buf);
388 * If not, is there a clipboard selection?
390 if(w == None && _x.clipboard != None){
391 clipboard = _x.clipboard;
392 w = XGetSelectionOwner(xd, _x.clipboard);
406 * We should be waiting for SelectionNotify here, but it might never
407 * come, and we have no way to time out. Instead, we will clear
408 * local property #1, request our buddy to fill it in for us, and poll
409 * until he's done or we get tired of waiting.
411 * We should try to go for _x.utf8string instead of XA_STRING,
412 * but that would add to the polling.
415 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
416 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
419 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
421 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
422 &type, &fmt, &dummy, &len, &data);
423 if(lastlen == len && len > 0)
431 /* get the property */
433 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
434 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
435 if((type != XA_STRING && type != _x.utf8string) || len == 0){
441 data = (uchar*)strdup((char*)xdata);
452 _xputsnarf(XDisplay *xd, char *data)
456 if(strlen(data) >= SnarfSize)
459 strcpy(clip.buf, data);
461 /* leave note for mouse proc to assert selection ownership */
464 /* send mouse a fake event so snarf is announced */
465 memset(&e, 0, sizeof e);
466 e.type = ButtonPress;
467 e.window = _x.drawable;
470 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
477 _xselect(XEvent *e, XDisplay *xd)
481 XSelectionRequestEvent *xe;
484 memset(&r, 0, sizeof r);
485 xe = (XSelectionRequestEvent*)e;
486 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
487 xe->target, xe->requestor, xe->property, xe->selection);
488 r.xselection.property = xe->property;
489 if(xe->target == _x.targets){
491 a[1] = _x.utf8string;
493 a[3] = _x.compoundtext;
495 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
496 8, PropModeReplace, (uchar*)a, sizeof a);
497 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){
498 /* if the target is STRING we're supposed to reply with Latin1 XXX */
500 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
501 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
504 name = XGetAtomName(xd, xe->target);
505 if(strcmp(name, "TIMESTAMP") != 0)
506 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
507 r.xselection.property = None;
510 r.xselection.display = xe->display;
511 /* r.xselection.property filled above */
512 r.xselection.target = xe->target;
513 r.xselection.type = SelectionNotify;
514 r.xselection.requestor = xe->requestor;
515 r.xselection.time = xe->time;
516 r.xselection.send_event = True;
517 r.xselection.selection = xe->selection;
518 XSendEvent(xd, xe->requestor, False, 0, &r);
526 _xputsnarf(_x.snarfcon, data);
532 return _xgetsnarf(_x.snarfcon);