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 k[nk++] = r;
138 n = _latin1(k, nk);
139 if(n > 0){
140 alting = 0;
141 k[0] = n;
142 k[1] = 0;
143 return k;
145 if(n == -1){
146 alting = 0;
147 k[nk] = 0;
148 return k;
150 /* n < -1, need more input */
151 return nil;
152 }else if(r == Kalt){
153 alting = 1;
154 nk = 0;
155 return nil;
156 }else{
157 k[0] = r;
158 k[1] = 0;
159 return k;
163 int
164 _xtoplan9kbd(XEvent *e)
166 static Rune *r;
168 if(e == (XEvent*)-1){
169 assert(r);
170 r--;
171 return 0;
173 if(e)
174 r = xtoplan9latin1(e);
175 if(r && *r)
176 return *r++;
177 return -1;
180 int
181 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
183 int s;
184 XButtonEvent *be;
185 XMotionEvent *me;
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);
192 XFlush(xd);
195 switch(e->type){
196 case ButtonPress:
197 be = (XButtonEvent*)e;
198 /*
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
202 * get to us.
203 */
204 if(be->send_event
205 && (~be->state&0xFFFF)==0
206 && (~be->button&0xFF)==0)
207 return -1;
208 /* BUG? on mac need to inherit these from elsewhere? */
209 m->xy.x = be->x;
210 m->xy.y = be->y;
211 s = be->state;
212 m->msec = be->time;
213 switch(be->button){
214 case 1:
215 s |= Button1Mask;
216 break;
217 case 2:
218 s |= Button2Mask;
219 break;
220 case 3:
221 s |= Button3Mask;
222 break;
223 case 4:
224 s |= Button4Mask;
225 break;
226 case 5:
227 s |= Button5Mask;
228 break;
230 break;
231 case ButtonRelease:
232 be = (XButtonEvent*)e;
233 m->xy.x = be->x;
234 m->xy.y = be->y;
235 s = be->state;
236 m->msec = be->time;
237 switch(be->button){
238 case 1:
239 s &= ~Button1Mask;
240 break;
241 case 2:
242 s &= ~Button2Mask;
243 break;
244 case 3:
245 s &= ~Button3Mask;
246 break;
247 case 4:
248 s &= ~Button4Mask;
249 break;
250 case 5:
251 s &= ~Button5Mask;
252 break;
254 break;
256 case MotionNotify:
257 me = (XMotionEvent*)e;
258 s = me->state;
259 m->xy.x = me->x;
260 m->xy.y = me->y;
261 m->msec = me->time;
262 break;
264 default:
265 return -1;
268 m->buttons = 0;
269 if(s & Button1Mask)
270 m->buttons |= 1;
271 if(s & Button2Mask)
272 m->buttons |= 2;
273 if(s & Button3Mask)
274 m->buttons |= 4;
275 if(s & Button4Mask)
276 m->buttons |= 8;
277 if(s & Button5Mask)
278 m->buttons |= 16;
279 return 0;
282 void
283 _xmoveto(Point p)
285 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
286 XFlush(_x.display);
289 static int
290 revbyte(int b)
292 int r;
294 r = 0;
295 r |= (b&0x01) << 7;
296 r |= (b&0x02) << 5;
297 r |= (b&0x04) << 3;
298 r |= (b&0x08) << 1;
299 r |= (b&0x10) >> 1;
300 r |= (b&0x20) >> 3;
301 r |= (b&0x40) >> 5;
302 r |= (b&0x80) >> 7;
303 return r;
306 static void
307 xcursorarrow(void)
309 if(_x.cursor != 0){
310 XFreeCursor(_x.display, _x.cursor);
311 _x.cursor = 0;
313 XUndefineCursor(_x.display, _x.drawable);
314 XFlush(_x.display);
318 void
319 _xsetcursor(Cursor *c)
321 XColor fg, bg;
322 XCursor xc;
323 Pixmap xsrc, xmask;
324 int i;
325 uchar src[2*16], mask[2*16];
327 if(c == nil){
328 xcursorarrow();
329 return;
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]);
336 fg = _x.map[0];
337 bg = _x.map[255];
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);
341 if(xc != 0) {
342 XDefineCursor(_x.display, _x.drawable, xc);
343 if(_x.cursor != 0)
344 XFreeCursor(_x.display, _x.cursor);
345 _x.cursor = xc;
347 XFreePixmap(_x.display, xsrc);
348 XFreePixmap(_x.display, xmask);
349 XFlush(_x.display);
352 struct {
353 char buf[SnarfSize];
354 QLock lk;
355 } clip;
357 char*
358 _xgetsnarf(XDisplay *xd)
360 uchar *data, *xdata;
361 Atom clipboard, type, prop;
362 ulong len, lastlen, dummy;
363 int fmt, i;
364 XWindow w;
366 qlock(&clip.lk);
367 /*
368 * Is there a primary selection (highlighted text in an xterm)?
369 */
370 clipboard = XA_PRIMARY;
371 w = XGetSelectionOwner(xd, XA_PRIMARY);
372 if(w == _x.drawable){
373 mine:
374 data = (uchar*)strdup(clip.buf);
375 goto out;
378 /*
379 * If not, is there a clipboard selection?
380 */
381 if(w == None && _x.clipboard != None){
382 clipboard = _x.clipboard;
383 w = XGetSelectionOwner(xd, _x.clipboard);
384 if(w == _x.drawable)
385 goto mine;
388 /*
389 * If not, give up.
390 */
391 if(w == None){
392 data = nil;
393 goto out;
396 /*
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.
404 */
405 prop = 1;
406 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
407 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
408 XFlush(xd);
409 lastlen = 0;
410 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
411 usleep(100*1000);
412 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
413 &type, &fmt, &dummy, &len, &data);
414 if(lastlen == len && len > 0)
415 break;
416 lastlen = len;
418 if(i == 10){
419 data = nil;
420 goto out;
422 /* get the property */
423 data = nil;
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){
427 if(xdata)
428 XFree(xdata);
429 data = nil;
430 }else{
431 if(xdata){
432 data = (uchar*)strdup((char*)xdata);
433 XFree(xdata);
434 }else
435 data = nil;
437 out:
438 qunlock(&clip.lk);
439 return (char*)data;
442 void
443 _xputsnarf(XDisplay *xd, char *data)
445 XButtonEvent e;
447 if(strlen(data) >= SnarfSize)
448 return;
449 qlock(&clip.lk);
450 strcpy(clip.buf, data);
452 /* leave note for mouse proc to assert selection ownership */
453 _x.putsnarf++;
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;
459 e.state = ~0;
460 e.button = ~0;
461 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
462 XFlush(xd);
464 qunlock(&clip.lk);
467 int
468 _xselect(XEvent *e, XDisplay *xd)
470 char *name;
471 XEvent r;
472 XSelectionRequestEvent *xe;
473 Atom a[4];
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){
481 a[0] = XA_STRING;
482 a[1] = _x.utf8string;
483 a[2] = _x.text;
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 */
490 qlock(&clip.lk);
491 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
492 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
493 qunlock(&clip.lk);
494 }else{
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);
510 XFlush(xd);
511 return 0;
514 void
515 putsnarf(char *data)
517 _xputsnarf(_x.snarfcon, data);
520 char*
521 getsnarf(void)
523 return _xgetsnarf(_x.snarfcon);