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 */
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);
150 /* n < -1, need more input */
164 _xtoplan9kbd(XEvent *e)
168 if(e == (XEvent*)-1){
174 r = xtoplan9latin1(e);
181 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
187 if(_x.putsnarf != _x.assertsnarf){
188 _x.assertsnarf = _x.putsnarf;
189 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);
190 if(_x.clipboard != None)
191 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);
197 be = (XButtonEvent*)e;
199 * Fake message, just sent to make us announce snarf.
200 * Apparently state and button are 16 and 8 bits on
201 * the wire, since they are truncated by the time they
205 && (~be->state&0xFFFF)==0
206 && (~be->button&0xFF)==0)
208 /* BUG? on mac need to inherit these from elsewhere? */
232 be = (XButtonEvent*)e;
257 me = (XMotionEvent*)e;
285 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
310 XFreeCursor(_x.display, _x.cursor);
313 XUndefineCursor(_x.display, _x.drawable);
319 _xsetcursor(Cursor *c)
325 uchar src[2*16], mask[2*16];
331 for(i=0; i<2*16; i++){
332 src[i] = revbyte(c->set[i]);
333 mask[i] = revbyte(c->set[i] | c->clr[i]);
338 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
339 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
340 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
342 XDefineCursor(_x.display, _x.drawable, xc);
344 XFreeCursor(_x.display, _x.cursor);
347 XFreePixmap(_x.display, xsrc);
348 XFreePixmap(_x.display, xmask);
358 _xgetsnarf(XDisplay *xd)
361 Atom clipboard, type, prop;
362 ulong len, lastlen, dummy;
368 * Is there a primary selection (highlighted text in an xterm)?
370 clipboard = XA_PRIMARY;
371 w = XGetSelectionOwner(xd, XA_PRIMARY);
372 if(w == _x.drawable){
374 data = (uchar*)strdup(clip.buf);
379 * If not, is there a clipboard selection?
381 if(w == None && _x.clipboard != None){
382 clipboard = _x.clipboard;
383 w = XGetSelectionOwner(xd, _x.clipboard);
397 * We should be waiting for SelectionNotify here, but it might never
398 * come, and we have no way to time out. Instead, we will clear
399 * local property #1, request our buddy to fill it in for us, and poll
400 * until he's done or we get tired of waiting.
402 * We should try to go for _x.utf8string instead of XA_STRING,
403 * but that would add to the polling.
406 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
407 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
410 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
412 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
413 &type, &fmt, &dummy, &len, &data);
414 if(lastlen == len && len > 0)
422 /* get the property */
424 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
425 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
426 if((type != XA_STRING && type != _x.utf8string) || len == 0){
432 data = (uchar*)strdup((char*)xdata);
443 _xputsnarf(XDisplay *xd, char *data)
447 if(strlen(data) >= SnarfSize)
450 strcpy(clip.buf, data);
452 /* leave note for mouse proc to assert selection ownership */
455 /* send mouse a fake event so snarf is announced */
456 memset(&e, 0, sizeof e);
457 e.type = ButtonPress;
458 e.window = _x.drawable;
461 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
468 _xselect(XEvent *e, XDisplay *xd)
472 XSelectionRequestEvent *xe;
475 memset(&r, 0, sizeof r);
476 xe = (XSelectionRequestEvent*)e;
477 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
478 xe->target, xe->requestor, xe->property, xe->selection);
479 r.xselection.property = xe->property;
480 if(xe->target == _x.targets){
482 a[1] = _x.utf8string;
484 a[3] = _x.compoundtext;
486 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
487 8, PropModeReplace, (uchar*)a, sizeof a);
488 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){
489 /* if the target is STRING we're supposed to reply with Latin1 XXX */
491 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
492 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
495 name = XGetAtomName(xd, xe->target);
496 if(strcmp(name, "TIMESTAMP") != 0)
497 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
498 r.xselection.property = None;
501 r.xselection.display = xe->display;
502 /* r.xselection.property filled above */
503 r.xselection.target = xe->target;
504 r.xselection.type = SelectionNotify;
505 r.xselection.requestor = xe->requestor;
506 r.xselection.time = xe->time;
507 r.xselection.send_event = True;
508 r.xselection.selection = xe->selection;
509 XSendEvent(xd, xe->requestor, False, 0, &r);
517 _xputsnarf(_x.snarfcon, data);
523 return _xgetsnarf(_x.snarfcon);