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 XLookupString((XKeyEvent*)e,NULL,0,&k,NULL);
25 if(k == XK_Multi_key || k == NoSymbol)
101 default: /* not ISO-1 or tty control */
109 /* Compensate for servers that call a minus a hyphen */
112 /* Do control mapping ourselves if translator doesn't */
113 if(e->xkey.state&ControlMask)
119 /* BUG: could/should do Alt translation here! */
124 xtoplan9latin1(XEvent *e)
127 static int alting, nk;
131 r = __xtoplan9kbd(e);
148 /* n < -1, need more input */
162 _xtoplan9kbd(XEvent *e)
166 if(e == (XEvent*)-1){
172 r = xtoplan9latin1(e);
179 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
185 if(_x.putsnarf != _x.assertsnarf){
186 _x.assertsnarf = _x.putsnarf;
187 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);
188 if(_x.clipboard != None)
189 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);
195 be = (XButtonEvent*)e;
197 * Fake message, just sent to make us announce snarf.
198 * Apparently state and button are 16 and 8 bits on
199 * the wire, since they are truncated by the time they
203 && (~be->state&0xFFFF)==0
204 && (~be->button&0xFF)==0)
206 /* BUG? on mac need to inherit these from elsewhere? */
224 be = (XButtonEvent*)e;
243 me = (XMotionEvent*)e;
268 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
293 XFreeCursor(_x.display, _x.cursor);
296 XUndefineCursor(_x.display, _x.drawable);
302 _xsetcursor(Cursor *c)
308 uchar src[2*16], mask[2*16];
314 for(i=0; i<2*16; i++){
315 src[i] = revbyte(c->set[i]);
316 mask[i] = revbyte(c->set[i] | c->clr[i]);
321 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
322 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
323 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
325 XDefineCursor(_x.display, _x.drawable, xc);
327 XFreeCursor(_x.display, _x.cursor);
330 XFreePixmap(_x.display, xsrc);
331 XFreePixmap(_x.display, xmask);
341 _xgetsnarf(XDisplay *xd)
344 Atom clipboard, type, prop;
345 ulong len, lastlen, dummy;
351 * Is there a primary selection (highlighted text in an xterm)?
353 clipboard = XA_PRIMARY;
354 w = XGetSelectionOwner(xd, XA_PRIMARY);
355 if(w == _x.drawable){
357 data = (uchar*)strdup(clip.buf);
362 * If not, is there a clipboard selection?
364 if(w == None && _x.clipboard != None){
365 clipboard = _x.clipboard;
366 w = XGetSelectionOwner(xd, _x.clipboard);
380 * We should be waiting for SelectionNotify here, but it might never
381 * come, and we have no way to time out. Instead, we will clear
382 * local property #1, request our buddy to fill it in for us, and poll
383 * until he's done or we get tired of waiting.
385 * We should try to go for _x.utf8string instead of XA_STRING,
386 * but that would add to the polling.
389 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
390 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
393 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
395 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
396 &type, &fmt, &dummy, &len, &data);
397 if(lastlen == len && len > 0)
405 /* get the property */
407 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
408 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
409 if((type != XA_STRING && type != _x.utf8string) || len == 0){
415 data = (uchar*)strdup((char*)xdata);
426 _xputsnarf(XDisplay *xd, char *data)
430 if(strlen(data) >= SnarfSize)
433 strcpy(clip.buf, data);
435 /* leave note for mouse proc to assert selection ownership */
438 /* send mouse a fake event so snarf is announced */
439 memset(&e, 0, sizeof e);
440 e.type = ButtonPress;
441 e.window = _x.drawable;
444 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
451 _xselect(XEvent *e, XDisplay *xd)
455 XSelectionRequestEvent *xe;
458 memset(&r, 0, sizeof r);
459 xe = (XSelectionRequestEvent*)e;
460 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
461 xe->target, xe->requestor, xe->property, xe->selection);
462 r.xselection.property = xe->property;
463 if(xe->target == _x.targets){
465 a[1] = _x.utf8string;
467 a[3] = _x.compoundtext;
469 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
470 8, PropModeReplace, (uchar*)a, sizeof a);
471 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){
472 /* if the target is STRING we're supposed to reply with Latin1 XXX */
474 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
475 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
478 name = XGetAtomName(xd, xe->target);
479 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
480 r.xselection.property = None;
483 r.xselection.display = xe->display;
484 /* r.xselection.property filled above */
485 r.xselection.target = xe->target;
486 r.xselection.type = SelectionNotify;
487 r.xselection.requestor = xe->requestor;
488 r.xselection.time = xe->time;
489 r.xselection.send_event = True;
490 r.xselection.selection = xe->selection;
491 XSendEvent(xd, xe->requestor, False, 0, &r);
499 _xputsnarf(_x.snarfcon, data);
505 return _xgetsnarf(_x.snarfcon);