1 /* input event and data structure translation */
12 #include "x11-memdraw.h"
15 __xtoplan9kbd(XEvent *e)
24 k = XKeycodeToKeysym(e->xany.display, (KeyCode)e->xkey.keycode, ind);
25 if(k == XK_Multi_key || k == NoSymbol)
101 default: /* not ISO-1 or tty control */
106 /* Compensate for servers that call a minus a hyphen */
109 /* Do control mapping ourselves if translator doesn't */
110 if(e->xkey.state&ControlMask)
116 /* BUG: could/should do Alt translation here! */
121 xtoplan9latin1(XEvent *e)
124 static int alting, nk;
128 r = __xtoplan9kbd(e);
145 /* n < -1, need more input */
159 _xtoplan9kbd(XEvent *e)
163 if(e == (XEvent*)-1){
169 r = xtoplan9latin1(e);
176 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
182 if(_x.putsnarf != _x.assertsnarf){
183 _x.assertsnarf = _x.putsnarf;
184 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);
185 if(_x.clipboard != None)
186 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);
192 be = (XButtonEvent*)e;
194 * Fake message, just sent to make us announce snarf.
195 * Apparently state and button are 16 and 8 bits on
196 * the wire, since they are truncated by the time they
200 && (~be->state&0xFFFF)==0
201 && (~be->button&0xFF)==0)
203 /* BUG? on mac need to inherit these from elsewhere? */
221 be = (XButtonEvent*)e;
240 me = (XMotionEvent*)e;
265 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
290 XFreeCursor(_x.display, _x.cursor);
293 XUndefineCursor(_x.display, _x.drawable);
299 _xsetcursor(Cursor *c)
305 uchar src[2*16], mask[2*16];
311 for(i=0; i<2*16; i++){
312 src[i] = revbyte(c->set[i]);
313 mask[i] = revbyte(c->set[i] | c->clr[i]);
318 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, src, 16, 16);
319 xmask = XCreateBitmapFromData(_x.display, _x.drawable, mask, 16, 16);
320 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
322 XDefineCursor(_x.display, _x.drawable, xc);
324 XFreeCursor(_x.display, _x.cursor);
327 XFreePixmap(_x.display, xsrc);
328 XFreePixmap(_x.display, xmask);
338 _xgetsnarf(XDisplay *xd)
341 Atom clipboard, type, prop;
342 ulong len, lastlen, dummy;
348 * Is there a primary selection (highlighted text in an xterm)?
350 clipboard = XA_PRIMARY;
351 w = XGetSelectionOwner(xd, XA_PRIMARY);
352 if(w == _x.drawable){
354 data = (uchar*)strdup(clip.buf);
359 * If not, is there a clipboard selection?
361 if(w == None && _x.clipboard != None){
362 clipboard = _x.clipboard;
363 w = XGetSelectionOwner(xd, _x.clipboard);
377 * We should be waiting for SelectionNotify here, but it might never
378 * come, and we have no way to time out. Instead, we will clear
379 * local property #1, request our buddy to fill it in for us, and poll
380 * until he's done or we get tired of waiting.
382 * We should try to go for _x.utf8string instead of XA_STRING,
383 * but that would add to the polling.
386 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
387 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
390 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
392 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
393 &type, &fmt, &dummy, &len, &data);
394 if(lastlen == len && len > 0)
402 /* get the property */
404 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
405 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
406 if((type != XA_STRING && type != _x.utf8string) || len == 0){
412 data = strdup((char*)xdata);
423 _xputsnarf(XDisplay *xd, char *data)
427 if(strlen(data) >= SnarfSize)
430 strcpy(clip.buf, data);
432 /* leave note for mouse proc to assert selection ownership */
435 /* send mouse a fake event so snarf is announced */
436 memset(&e, 0, sizeof e);
437 e.type = ButtonPress;
438 e.window = _x.drawable;
441 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
448 _xselect(XEvent *e, XDisplay *xd)
452 XSelectionRequestEvent *xe;
455 memset(&r, 0, sizeof r);
456 xe = (XSelectionRequestEvent*)e;
457 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
458 xe->target, xe->requestor, xe->property, xe->selection);
459 r.xselection.property = xe->property;
460 if(xe->target == _x.targets){
462 a[1] = _x.utf8string;
464 a[3] = _x.compoundtext;
466 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
467 8, PropModeReplace, (uchar*)a, sizeof a);
468 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){
469 /* if the target is STRING we're supposed to reply with Latin1 XXX */
471 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
472 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
475 name = XGetAtomName(xd, xe->target);
476 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
477 r.xselection.property = None;
480 r.xselection.display = xe->display;
481 /* r.xselection.property filled above */
482 r.xselection.target = xe->target;
483 r.xselection.type = SelectionNotify;
484 r.xselection.requestor = xe->requestor;
485 r.xselection.time = xe->time;
486 r.xselection.send_event = True;
487 r.xselection.selection = xe->selection;
488 XSendEvent(xd, xe->requestor, False, 0, &r);
496 _xputsnarf(_x.snarfcon, data);
502 return _xgetsnarf(_x.snarfcon);