1 /* input event and data structure translation */
11 #include "x11-memdraw.h"
17 __xtoplan9kbd(XEvent *e)
26 k = XKeycodeToKeysym(e->xany.display, (KeyCode)e->xkey.keycode, ind);
27 if(k == XK_Multi_key || k == NoSymbol)
103 default: /* not ISO-1 or tty control */
108 /* Compensate for servers that call a minus a hyphen */
111 /* Do control mapping ourselves if translator doesn't */
112 if(e->xkey.state&ControlMask)
118 /* BUG: could/should do Alt translation here! */
123 xtoplan9latin1(XEvent *e)
126 static int alting, nk;
130 r = __xtoplan9kbd(e);
147 /* n < -1, need more input */
161 _xtoplan9kbd(XEvent *e)
165 if(e == (XEvent*)-1){
171 r = xtoplan9latin1(e);
178 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
184 if(_x.putsnarf != _x.assertsnarf){
185 _x.assertsnarf = _x.putsnarf;
186 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);
187 if(_x.clipboard != None)
188 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);
194 be = (XButtonEvent*)e;
196 * Fake message, just sent to make us announce snarf.
197 * Apparently state and button are 16 and 8 bits on
198 * the wire, since they are truncated by the time they
202 && (~be->state&0xFFFF)==0
203 && (~be->button&0xFF)==0)
205 /* BUG? on mac need to inherit these from elsewhere? */
223 be = (XButtonEvent*)e;
242 me = (XMotionEvent*)e;
267 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
292 XFreeCursor(_x.display, _x.cursor);
295 XUndefineCursor(_x.display, _x.drawable);
301 _xsetcursor(Cursor *c)
307 uchar src[2*16], mask[2*16];
313 for(i=0; i<2*16; i++){
314 src[i] = revbyte(c->set[i]);
315 mask[i] = revbyte(c->set[i] | c->clr[i]);
320 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
321 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
322 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
324 XDefineCursor(_x.display, _x.drawable, xc);
326 XFreeCursor(_x.display, _x.cursor);
329 XFreePixmap(_x.display, xsrc);
330 XFreePixmap(_x.display, xmask);
340 _xgetsnarf(XDisplay *xd)
343 Atom clipboard, type, prop;
344 ulong len, lastlen, dummy;
350 * Is there a primary selection (highlighted text in an xterm)?
352 clipboard = XA_PRIMARY;
353 w = XGetSelectionOwner(xd, XA_PRIMARY);
354 if(w == _x.drawable){
356 data = (uchar*)strdup(clip.buf);
361 * If not, is there a clipboard selection?
363 if(w == None && _x.clipboard != None){
364 clipboard = _x.clipboard;
365 w = XGetSelectionOwner(xd, _x.clipboard);
379 * We should be waiting for SelectionNotify here, but it might never
380 * come, and we have no way to time out. Instead, we will clear
381 * local property #1, request our buddy to fill it in for us, and poll
382 * until he's done or we get tired of waiting.
384 * We should try to go for _x.utf8string instead of XA_STRING,
385 * but that would add to the polling.
388 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
389 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
392 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
394 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
395 &type, &fmt, &dummy, &len, &data);
396 if(lastlen == len && len > 0)
404 /* get the property */
406 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
407 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
408 if((type != XA_STRING && type != _x.utf8string) || len == 0){
414 data = (uchar*)strdup((char*)xdata);
425 _xputsnarf(XDisplay *xd, char *data)
429 if(strlen(data) >= SnarfSize)
432 strcpy(clip.buf, data);
434 /* leave note for mouse proc to assert selection ownership */
437 /* send mouse a fake event so snarf is announced */
438 memset(&e, 0, sizeof e);
439 e.type = ButtonPress;
440 e.window = _x.drawable;
443 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
450 _xselect(XEvent *e, XDisplay *xd)
454 XSelectionRequestEvent *xe;
457 memset(&r, 0, sizeof r);
458 xe = (XSelectionRequestEvent*)e;
459 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
460 xe->target, xe->requestor, xe->property, xe->selection);
461 r.xselection.property = xe->property;
462 if(xe->target == _x.targets){
464 a[1] = _x.utf8string;
466 a[3] = _x.compoundtext;
468 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
469 8, PropModeReplace, (uchar*)a, sizeof a);
470 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){
471 /* if the target is STRING we're supposed to reply with Latin1 XXX */
473 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
474 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
477 name = XGetAtomName(xd, xe->target);
478 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
479 r.xselection.property = None;
482 r.xselection.display = xe->display;
483 /* r.xselection.property filled above */
484 r.xselection.target = xe->target;
485 r.xselection.type = SelectionNotify;
486 r.xselection.requestor = xe->requestor;
487 r.xselection.time = xe->time;
488 r.xselection.send_event = True;
489 r.xselection.selection = xe->selection;
490 XSendEvent(xd, xe->requestor, False, 0, &r);
498 _xputsnarf(_x.snarfcon, data);
504 return _xgetsnarf(_x.snarfcon);