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(64*1024); /* X has some *huge* buffers in openobject */
25 /* and they're even bigger on SuSE */
26 XLookupString((XKeyEvent*)e,NULL,0,&k,NULL);
27 if(k == XK_Multi_key || k == NoSymbol)
100 case XK_Meta_L: /* Shift Alt on PCs */
102 case XK_Meta_R: /* Shift Alt on PCs */
105 default: /* not ISO-1 or tty control */
107 k = _p9keysym2ucs(k);
113 /* Compensate for servers that call a minus a hyphen */
116 /* Do control mapping ourselves if translator doesn't */
117 if(e->xkey.state&ControlMask)
127 xtoplan9latin1(XEvent *e)
130 static int alting, nk;
134 r = __xtoplan9kbd(e);
139 * Kludge for Mac's X11 3-button emulation.
140 * It treats Command+Button as button 3, but also
141 * ends up sending XK_Meta_L twice.
160 /* n < -1, need more input */
174 _xtoplan9kbd(XEvent *e)
178 if(e == (XEvent*)-1){
184 r = xtoplan9latin1(e);
191 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
197 if(_x.putsnarf != _x.assertsnarf){
198 _x.assertsnarf = _x.putsnarf;
199 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);
200 if(_x.clipboard != None)
201 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);
207 be = (XButtonEvent*)e;
209 * Fake message, just sent to make us announce snarf.
210 * Apparently state and button are 16 and 8 bits on
211 * the wire, since they are truncated by the time they
215 && (~be->state&0xFFFF)==0
216 && (~be->button&0xFF)==0)
218 /* BUG? on mac need to inherit these from elsewhere? */
242 be = (XButtonEvent*)e;
267 me = (XMotionEvent*)e;
295 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
320 XFreeCursor(_x.display, _x.cursor);
323 XUndefineCursor(_x.display, _x.drawable);
329 _xsetcursor(Cursor *c)
335 uchar src[2*16], mask[2*16];
341 for(i=0; i<2*16; i++){
342 src[i] = revbyte(c->set[i]);
343 mask[i] = revbyte(c->set[i] | c->clr[i]);
348 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
349 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
350 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
352 XDefineCursor(_x.display, _x.drawable, xc);
354 XFreeCursor(_x.display, _x.cursor);
357 XFreePixmap(_x.display, xsrc);
358 XFreePixmap(_x.display, xmask);
368 _xgetsnarf(XDisplay *xd)
371 Atom clipboard, type, prop;
372 ulong len, lastlen, dummy;
378 * Have we snarfed recently and the X server hasn't caught up?
380 if(_x.putsnarf != _x.assertsnarf)
384 * Is there a primary selection (highlighted text in an xterm)?
386 clipboard = XA_PRIMARY;
387 w = XGetSelectionOwner(xd, XA_PRIMARY);
388 if(w == _x.drawable){
390 data = (uchar*)strdup(clip.buf);
395 * If not, is there a clipboard selection?
397 if(w == None && _x.clipboard != None){
398 clipboard = _x.clipboard;
399 w = XGetSelectionOwner(xd, _x.clipboard);
413 * We should be waiting for SelectionNotify here, but it might never
414 * come, and we have no way to time out. Instead, we will clear
415 * local property #1, request our buddy to fill it in for us, and poll
416 * until he's done or we get tired of waiting.
418 * We should try to go for _x.utf8string instead of XA_STRING,
419 * but that would add to the polling.
422 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
423 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
426 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
428 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
429 &type, &fmt, &dummy, &len, &data);
430 if(lastlen == len && len > 0)
438 /* get the property */
440 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
441 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
442 if((type != XA_STRING && type != _x.utf8string) || len == 0){
448 data = (uchar*)strdup((char*)xdata);
459 _xputsnarf(XDisplay *xd, char *data)
463 if(strlen(data) >= SnarfSize)
466 strcpy(clip.buf, data);
468 /* leave note for mouse proc to assert selection ownership */
471 /* send mouse a fake event so snarf is announced */
472 memset(&e, 0, sizeof e);
473 e.type = ButtonPress;
474 e.window = _x.drawable;
477 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
484 _xselect(XEvent *e, XDisplay *xd)
488 XSelectionRequestEvent *xe;
491 memset(&r, 0, sizeof r);
492 xe = (XSelectionRequestEvent*)e;
493 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
494 xe->target, xe->requestor, xe->property, xe->selection);
495 r.xselection.property = xe->property;
496 if(xe->target == _x.targets){
498 a[1] = _x.utf8string;
500 a[3] = _x.compoundtext;
502 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
503 8, PropModeReplace, (uchar*)a, sizeof a);
504 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){
505 /* if the target is STRING we're supposed to reply with Latin1 XXX */
507 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
508 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
511 name = XGetAtomName(xd, xe->target);
512 if(strcmp(name, "TIMESTAMP") != 0)
513 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
514 r.xselection.property = None;
517 r.xselection.display = xe->display;
518 /* r.xselection.property filled above */
519 r.xselection.target = xe->target;
520 r.xselection.type = SelectionNotify;
521 r.xselection.requestor = xe->requestor;
522 r.xselection.time = xe->time;
523 r.xselection.send_event = True;
524 r.xselection.selection = xe->selection;
525 XSendEvent(xd, xe->requestor, False, 0, &r);
533 _xputsnarf(_x.snarfcon, data);
539 return _xgetsnarf(_x.snarfcon);