Blob


1 /* input event and data structure translation */
3 #include <u.h>
4 #include "x11-inc.h"
5 #include <libc.h>
6 #include <draw.h>
7 #include <memdraw.h>
8 #include <mouse.h>
9 #include <cursor.h>
10 #include <keyboard.h>
11 #include "x11-memdraw.h"
12 #include "x11-keysym2ucs.h"
14 #undef time
17 static KeySym
18 __xtoplan9kbd(XEvent *e)
19 {
20 KeySym k;
22 if(e->xany.type != KeyPress)
23 return -1;
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)
27 return -1;
29 if(k&0xFF00){
30 switch(k){
31 case XK_BackSpace:
32 case XK_Tab:
33 case XK_Escape:
34 case XK_Delete:
35 case XK_KP_0:
36 case XK_KP_1:
37 case XK_KP_2:
38 case XK_KP_3:
39 case XK_KP_4:
40 case XK_KP_5:
41 case XK_KP_6:
42 case XK_KP_7:
43 case XK_KP_8:
44 case XK_KP_9:
45 case XK_KP_Divide:
46 case XK_KP_Multiply:
47 case XK_KP_Subtract:
48 case XK_KP_Add:
49 case XK_KP_Decimal:
50 k &= 0x7F;
51 break;
52 case XK_Linefeed:
53 k = '\r';
54 break;
55 case XK_KP_Space:
56 k = ' ';
57 break;
58 case XK_Home:
59 case XK_KP_Home:
60 k = Khome;
61 break;
62 case XK_Left:
63 case XK_KP_Left:
64 k = Kleft;
65 break;
66 case XK_Up:
67 case XK_KP_Up:
68 k = Kup;
69 break;
70 case XK_Down:
71 case XK_KP_Down:
72 k = Kdown;
73 break;
74 case XK_Right:
75 case XK_KP_Right:
76 k = Kright;
77 break;
78 case XK_Page_Down:
79 case XK_KP_Page_Down:
80 k = Kpgdown;
81 break;
82 case XK_End:
83 case XK_KP_End:
84 k = Kend;
85 break;
86 case XK_Page_Up:
87 case XK_KP_Page_Up:
88 k = Kpgup;
89 break;
90 case XK_Insert:
91 case XK_KP_Insert:
92 k = Kins;
93 break;
94 case XK_KP_Enter:
95 case XK_Return:
96 k = '\n';
97 break;
98 case XK_Alt_L:
99 case XK_Meta_L: /* Shift Alt on PCs */
100 case XK_Alt_R:
101 case XK_Meta_R: /* Shift Alt on PCs */
102 k = Kalt;
103 break;
104 default: /* not ISO-1 or tty control */
105 if(k>0xff) {
106 k = keysym2ucs(k);
107 if(k==-1) return -1;
112 /* Compensate for servers that call a minus a hyphen */
113 if(k == XK_hyphen)
114 k = XK_minus;
115 /* Do control mapping ourselves if translator doesn't */
116 if(e->xkey.state&ControlMask)
117 k &= 0x9f;
118 if(k == NoSymbol) {
119 return -1;
122 return k+0;
125 static Rune*
126 xtoplan9latin1(XEvent *e)
128 static Rune k[10];
129 static int alting, nk;
130 int n;
131 int r;
133 r = __xtoplan9kbd(e);
134 if(r < 0)
135 return nil;
136 if(alting){
137 /*
138 * Kludge for Mac's X11 3-button emulation.
139 * It treats Command+Button as button 3, but also
140 * ends up sending XK_Meta_L twice.
141 */
142 if(r == Kalt){
143 alting = 0;
144 return nil;
146 k[nk++] = r;
147 n = _latin1(k, nk);
148 if(n > 0){
149 alting = 0;
150 k[0] = n;
151 k[1] = 0;
152 return k;
154 if(n == -1){
155 alting = 0;
156 k[nk] = 0;
157 return k;
159 /* n < -1, need more input */
160 return nil;
161 }else if(r == Kalt){
162 alting = 1;
163 nk = 0;
164 return nil;
165 }else{
166 k[0] = r;
167 k[1] = 0;
168 return k;
172 int
173 _xtoplan9kbd(XEvent *e)
175 static Rune *r;
177 if(e == (XEvent*)-1){
178 assert(r);
179 r--;
180 return 0;
182 if(e)
183 r = xtoplan9latin1(e);
184 if(r && *r)
185 return *r++;
186 return -1;
189 int
190 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
192 int s;
193 XButtonEvent *be;
194 XMotionEvent *me;
196 if(_x.putsnarf != _x.assertsnarf){
197 _x.assertsnarf = _x.putsnarf;
198 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);
199 if(_x.clipboard != None)
200 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);
201 XFlush(xd);
204 switch(e->type){
205 case ButtonPress:
206 be = (XButtonEvent*)e;
207 /*
208 * Fake message, just sent to make us announce snarf.
209 * Apparently state and button are 16 and 8 bits on
210 * the wire, since they are truncated by the time they
211 * get to us.
212 */
213 if(be->send_event
214 && (~be->state&0xFFFF)==0
215 && (~be->button&0xFF)==0)
216 return -1;
217 /* BUG? on mac need to inherit these from elsewhere? */
218 m->xy.x = be->x;
219 m->xy.y = be->y;
220 s = be->state;
221 m->msec = be->time;
222 switch(be->button){
223 case 1:
224 s |= Button1Mask;
225 break;
226 case 2:
227 s |= Button2Mask;
228 break;
229 case 3:
230 s |= Button3Mask;
231 break;
232 case 4:
233 s |= Button4Mask;
234 break;
235 case 5:
236 s |= Button5Mask;
237 break;
239 break;
240 case ButtonRelease:
241 be = (XButtonEvent*)e;
242 m->xy.x = be->x;
243 m->xy.y = be->y;
244 s = be->state;
245 m->msec = be->time;
246 switch(be->button){
247 case 1:
248 s &= ~Button1Mask;
249 break;
250 case 2:
251 s &= ~Button2Mask;
252 break;
253 case 3:
254 s &= ~Button3Mask;
255 break;
256 case 4:
257 s &= ~Button4Mask;
258 break;
259 case 5:
260 s &= ~Button5Mask;
261 break;
263 break;
265 case MotionNotify:
266 me = (XMotionEvent*)e;
267 s = me->state;
268 m->xy.x = me->x;
269 m->xy.y = me->y;
270 m->msec = me->time;
271 break;
273 default:
274 return -1;
277 m->buttons = 0;
278 if(s & Button1Mask)
279 m->buttons |= 1;
280 if(s & Button2Mask)
281 m->buttons |= 2;
282 if(s & Button3Mask)
283 m->buttons |= 4;
284 if(s & Button4Mask)
285 m->buttons |= 8;
286 if(s & Button5Mask)
287 m->buttons |= 16;
288 return 0;
291 void
292 _xmoveto(Point p)
294 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
295 XFlush(_x.display);
298 static int
299 revbyte(int b)
301 int r;
303 r = 0;
304 r |= (b&0x01) << 7;
305 r |= (b&0x02) << 5;
306 r |= (b&0x04) << 3;
307 r |= (b&0x08) << 1;
308 r |= (b&0x10) >> 1;
309 r |= (b&0x20) >> 3;
310 r |= (b&0x40) >> 5;
311 r |= (b&0x80) >> 7;
312 return r;
315 static void
316 xcursorarrow(void)
318 if(_x.cursor != 0){
319 XFreeCursor(_x.display, _x.cursor);
320 _x.cursor = 0;
322 XUndefineCursor(_x.display, _x.drawable);
323 XFlush(_x.display);
327 void
328 _xsetcursor(Cursor *c)
330 XColor fg, bg;
331 XCursor xc;
332 Pixmap xsrc, xmask;
333 int i;
334 uchar src[2*16], mask[2*16];
336 if(c == nil){
337 xcursorarrow();
338 return;
340 for(i=0; i<2*16; i++){
341 src[i] = revbyte(c->set[i]);
342 mask[i] = revbyte(c->set[i] | c->clr[i]);
345 fg = _x.map[0];
346 bg = _x.map[255];
347 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
348 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
349 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
350 if(xc != 0) {
351 XDefineCursor(_x.display, _x.drawable, xc);
352 if(_x.cursor != 0)
353 XFreeCursor(_x.display, _x.cursor);
354 _x.cursor = xc;
356 XFreePixmap(_x.display, xsrc);
357 XFreePixmap(_x.display, xmask);
358 XFlush(_x.display);
361 struct {
362 char buf[SnarfSize];
363 QLock lk;
364 } clip;
366 char*
367 _xgetsnarf(XDisplay *xd)
369 uchar *data, *xdata;
370 Atom clipboard, type, prop;
371 ulong len, lastlen, dummy;
372 int fmt, i;
373 XWindow w;
375 qlock(&clip.lk);
376 /*
377 * Is there a primary selection (highlighted text in an xterm)?
378 */
379 clipboard = XA_PRIMARY;
380 w = XGetSelectionOwner(xd, XA_PRIMARY);
381 if(w == _x.drawable){
382 mine:
383 data = (uchar*)strdup(clip.buf);
384 goto out;
387 /*
388 * If not, is there a clipboard selection?
389 */
390 if(w == None && _x.clipboard != None){
391 clipboard = _x.clipboard;
392 w = XGetSelectionOwner(xd, _x.clipboard);
393 if(w == _x.drawable)
394 goto mine;
397 /*
398 * If not, give up.
399 */
400 if(w == None){
401 data = nil;
402 goto out;
405 /*
406 * We should be waiting for SelectionNotify here, but it might never
407 * come, and we have no way to time out. Instead, we will clear
408 * local property #1, request our buddy to fill it in for us, and poll
409 * until he's done or we get tired of waiting.
411 * We should try to go for _x.utf8string instead of XA_STRING,
412 * but that would add to the polling.
413 */
414 prop = 1;
415 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
416 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
417 XFlush(xd);
418 lastlen = 0;
419 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
420 usleep(100*1000);
421 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
422 &type, &fmt, &dummy, &len, &data);
423 if(lastlen == len && len > 0)
424 break;
425 lastlen = len;
427 if(i == 10){
428 data = nil;
429 goto out;
431 /* get the property */
432 data = nil;
433 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
434 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
435 if((type != XA_STRING && type != _x.utf8string) || len == 0){
436 if(xdata)
437 XFree(xdata);
438 data = nil;
439 }else{
440 if(xdata){
441 data = (uchar*)strdup((char*)xdata);
442 XFree(xdata);
443 }else
444 data = nil;
446 out:
447 qunlock(&clip.lk);
448 return (char*)data;
451 void
452 _xputsnarf(XDisplay *xd, char *data)
454 XButtonEvent e;
456 if(strlen(data) >= SnarfSize)
457 return;
458 qlock(&clip.lk);
459 strcpy(clip.buf, data);
461 /* leave note for mouse proc to assert selection ownership */
462 _x.putsnarf++;
464 /* send mouse a fake event so snarf is announced */
465 memset(&e, 0, sizeof e);
466 e.type = ButtonPress;
467 e.window = _x.drawable;
468 e.state = ~0;
469 e.button = ~0;
470 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
471 XFlush(xd);
473 qunlock(&clip.lk);
476 int
477 _xselect(XEvent *e, XDisplay *xd)
479 char *name;
480 XEvent r;
481 XSelectionRequestEvent *xe;
482 Atom a[4];
484 memset(&r, 0, sizeof r);
485 xe = (XSelectionRequestEvent*)e;
486 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
487 xe->target, xe->requestor, xe->property, xe->selection);
488 r.xselection.property = xe->property;
489 if(xe->target == _x.targets){
490 a[0] = XA_STRING;
491 a[1] = _x.utf8string;
492 a[2] = _x.text;
493 a[3] = _x.compoundtext;
495 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
496 8, PropModeReplace, (uchar*)a, sizeof a);
497 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){
498 /* if the target is STRING we're supposed to reply with Latin1 XXX */
499 qlock(&clip.lk);
500 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
501 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
502 qunlock(&clip.lk);
503 }else{
504 name = XGetAtomName(xd, xe->target);
505 if(strcmp(name, "TIMESTAMP") != 0)
506 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
507 r.xselection.property = None;
510 r.xselection.display = xe->display;
511 /* r.xselection.property filled above */
512 r.xselection.target = xe->target;
513 r.xselection.type = SelectionNotify;
514 r.xselection.requestor = xe->requestor;
515 r.xselection.time = xe->time;
516 r.xselection.send_event = True;
517 r.xselection.selection = xe->selection;
518 XSendEvent(xd, xe->requestor, False, 0, &r);
519 XFlush(xd);
520 return 0;
523 void
524 putsnarf(char *data)
526 _xputsnarf(_x.snarfcon, data);
529 char*
530 getsnarf(void)
532 return _xgetsnarf(_x.snarfcon);