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;
224 break;
225 case ButtonRelease:
226 be = (XButtonEvent*)e;
227 m->xy.x = be->x;
228 m->xy.y = be->y;
229 s = be->state;
230 m->msec = be->time;
231 switch(be->button){
232 case 1:
233 s &= ~Button1Mask;
234 break;
235 case 2:
236 s &= ~Button2Mask;
237 break;
238 case 3:
239 s &= ~Button3Mask;
240 break;
242 break;
244 case MotionNotify:
245 me = (XMotionEvent*)e;
246 s = me->state;
247 m->xy.x = me->x;
248 m->xy.y = me->y;
249 m->msec = me->time;
250 break;
252 default:
253 return -1;
256 m->buttons = 0;
257 if(s & Button1Mask)
258 m->buttons |= 1;
259 if(s & Button2Mask)
260 m->buttons |= 2;
261 if(s & Button3Mask)
262 m->buttons |= 4;
264 return 0;
267 void
268 _xmoveto(Point p)
270 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
271 XFlush(_x.display);
274 static int
275 revbyte(int b)
277 int r;
279 r = 0;
280 r |= (b&0x01) << 7;
281 r |= (b&0x02) << 5;
282 r |= (b&0x04) << 3;
283 r |= (b&0x08) << 1;
284 r |= (b&0x10) >> 1;
285 r |= (b&0x20) >> 3;
286 r |= (b&0x40) >> 5;
287 r |= (b&0x80) >> 7;
288 return r;
291 static void
292 xcursorarrow(void)
294 if(_x.cursor != 0){
295 XFreeCursor(_x.display, _x.cursor);
296 _x.cursor = 0;
298 XUndefineCursor(_x.display, _x.drawable);
299 XFlush(_x.display);
303 void
304 _xsetcursor(Cursor *c)
306 XColor fg, bg;
307 XCursor xc;
308 Pixmap xsrc, xmask;
309 int i;
310 uchar src[2*16], mask[2*16];
312 if(c == nil){
313 xcursorarrow();
314 return;
316 for(i=0; i<2*16; i++){
317 src[i] = revbyte(c->set[i]);
318 mask[i] = revbyte(c->set[i] | c->clr[i]);
321 fg = _x.map[0];
322 bg = _x.map[255];
323 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
324 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
325 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
326 if(xc != 0) {
327 XDefineCursor(_x.display, _x.drawable, xc);
328 if(_x.cursor != 0)
329 XFreeCursor(_x.display, _x.cursor);
330 _x.cursor = xc;
332 XFreePixmap(_x.display, xsrc);
333 XFreePixmap(_x.display, xmask);
334 XFlush(_x.display);
337 struct {
338 char buf[SnarfSize];
339 QLock lk;
340 } clip;
342 char*
343 _xgetsnarf(XDisplay *xd)
345 uchar *data, *xdata;
346 Atom clipboard, type, prop;
347 ulong len, lastlen, dummy;
348 int fmt, i;
349 XWindow w;
351 qlock(&clip.lk);
352 /*
353 * Is there a primary selection (highlighted text in an xterm)?
354 */
355 clipboard = XA_PRIMARY;
356 w = XGetSelectionOwner(xd, XA_PRIMARY);
357 if(w == _x.drawable){
358 mine:
359 data = (uchar*)strdup(clip.buf);
360 goto out;
363 /*
364 * If not, is there a clipboard selection?
365 */
366 if(w == None && _x.clipboard != None){
367 clipboard = _x.clipboard;
368 w = XGetSelectionOwner(xd, _x.clipboard);
369 if(w == _x.drawable)
370 goto mine;
373 /*
374 * If not, give up.
375 */
376 if(w == None){
377 data = nil;
378 goto out;
381 /*
382 * We should be waiting for SelectionNotify here, but it might never
383 * come, and we have no way to time out. Instead, we will clear
384 * local property #1, request our buddy to fill it in for us, and poll
385 * until he's done or we get tired of waiting.
387 * We should try to go for _x.utf8string instead of XA_STRING,
388 * but that would add to the polling.
389 */
390 prop = 1;
391 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
392 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
393 XFlush(xd);
394 lastlen = 0;
395 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
396 usleep(100*1000);
397 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
398 &type, &fmt, &dummy, &len, &data);
399 if(lastlen == len && len > 0)
400 break;
401 lastlen = len;
403 if(i == 10){
404 data = nil;
405 goto out;
407 /* get the property */
408 data = nil;
409 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
410 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
411 if((type != XA_STRING && type != _x.utf8string) || len == 0){
412 if(xdata)
413 XFree(xdata);
414 data = nil;
415 }else{
416 if(xdata){
417 data = (uchar*)strdup((char*)xdata);
418 XFree(xdata);
419 }else
420 data = nil;
422 out:
423 qunlock(&clip.lk);
424 return (char*)data;
427 void
428 _xputsnarf(XDisplay *xd, char *data)
430 XButtonEvent e;
432 if(strlen(data) >= SnarfSize)
433 return;
434 qlock(&clip.lk);
435 strcpy(clip.buf, data);
437 /* leave note for mouse proc to assert selection ownership */
438 _x.putsnarf++;
440 /* send mouse a fake event so snarf is announced */
441 memset(&e, 0, sizeof e);
442 e.type = ButtonPress;
443 e.window = _x.drawable;
444 e.state = ~0;
445 e.button = ~0;
446 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
447 XFlush(xd);
449 qunlock(&clip.lk);
452 int
453 _xselect(XEvent *e, XDisplay *xd)
455 char *name;
456 XEvent r;
457 XSelectionRequestEvent *xe;
458 Atom a[4];
460 memset(&r, 0, sizeof r);
461 xe = (XSelectionRequestEvent*)e;
462 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
463 xe->target, xe->requestor, xe->property, xe->selection);
464 r.xselection.property = xe->property;
465 if(xe->target == _x.targets){
466 a[0] = XA_STRING;
467 a[1] = _x.utf8string;
468 a[2] = _x.text;
469 a[3] = _x.compoundtext;
471 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
472 8, PropModeReplace, (uchar*)a, sizeof a);
473 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){
474 /* if the target is STRING we're supposed to reply with Latin1 XXX */
475 qlock(&clip.lk);
476 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
477 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
478 qunlock(&clip.lk);
479 }else{
480 name = XGetAtomName(xd, xe->target);
481 if(strcmp(name, "TIMESTAMP") != 0)
482 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
483 r.xselection.property = None;
486 r.xselection.display = xe->display;
487 /* r.xselection.property filled above */
488 r.xselection.target = xe->target;
489 r.xselection.type = SelectionNotify;
490 r.xselection.requestor = xe->requestor;
491 r.xselection.time = xe->time;
492 r.xselection.send_event = True;
493 r.xselection.selection = xe->selection;
494 XSendEvent(xd, xe->requestor, False, 0, &r);
495 XFlush(xd);
496 return 0;
499 void
500 putsnarf(char *data)
502 _xputsnarf(_x.snarfcon, data);
505 char*
506 getsnarf(void)
508 return _xgetsnarf(_x.snarfcon);