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 XLookupString((XKeyEvent*)e,NULL,0,&k,NULL);
25 if(k == XK_Multi_key || k == NoSymbol)
26 return -1;
28 if(k&0xFF00){
29 switch(k){
30 case XK_BackSpace:
31 case XK_Tab:
32 case XK_Escape:
33 case XK_Delete:
34 case XK_KP_0:
35 case XK_KP_1:
36 case XK_KP_2:
37 case XK_KP_3:
38 case XK_KP_4:
39 case XK_KP_5:
40 case XK_KP_6:
41 case XK_KP_7:
42 case XK_KP_8:
43 case XK_KP_9:
44 case XK_KP_Divide:
45 case XK_KP_Multiply:
46 case XK_KP_Subtract:
47 case XK_KP_Add:
48 case XK_KP_Decimal:
49 k &= 0x7F;
50 break;
51 case XK_Linefeed:
52 k = '\r';
53 break;
54 case XK_KP_Space:
55 k = ' ';
56 break;
57 case XK_Home:
58 case XK_KP_Home:
59 k = Khome;
60 break;
61 case XK_Left:
62 case XK_KP_Left:
63 k = Kleft;
64 break;
65 case XK_Up:
66 case XK_KP_Up:
67 k = Kup;
68 break;
69 case XK_Down:
70 case XK_KP_Down:
71 k = Kdown;
72 break;
73 case XK_Right:
74 case XK_KP_Right:
75 k = Kright;
76 break;
77 case XK_Page_Down:
78 case XK_KP_Page_Down:
79 k = Kpgdown;
80 break;
81 case XK_End:
82 case XK_KP_End:
83 k = Kend;
84 break;
85 case XK_Page_Up:
86 case XK_KP_Page_Up:
87 k = Kpgup;
88 break;
89 case XK_Insert:
90 case XK_KP_Insert:
91 k = Kins;
92 break;
93 case XK_KP_Enter:
94 case XK_Return:
95 k = '\n';
96 break;
97 case XK_Alt_L:
98 case XK_Alt_R:
99 k = Kalt;
100 break;
101 default: /* not ISO-1 or tty control */
102 if(k>0xff) {
103 k = keysym2ucs(k);
104 if(k==-1) return -1;
109 /* Compensate for servers that call a minus a hyphen */
110 if(k == XK_hyphen)
111 k = XK_minus;
112 /* Do control mapping ourselves if translator doesn't */
113 if(e->xkey.state&ControlMask)
114 k &= 0x9f;
115 if(k == NoSymbol) {
116 return -1;
119 /* BUG: could/should do Alt translation here! */
120 return k+0;
123 static Rune*
124 xtoplan9latin1(XEvent *e)
126 static Rune k[10];
127 static int alting, nk;
128 int n;
129 int r;
131 r = __xtoplan9kbd(e);
132 if(r < 0)
133 return nil;
134 if(alting){
135 k[nk++] = r;
136 n = _latin1(k, nk);
137 if(n > 0){
138 alting = 0;
139 k[0] = n;
140 k[1] = 0;
141 return k;
143 if(n == -1){
144 alting = 0;
145 k[nk] = 0;
146 return k;
148 /* n < -1, need more input */
149 return nil;
150 }else if(r == Kalt){
151 alting = 1;
152 nk = 0;
153 return nil;
154 }else{
155 k[0] = r;
156 k[1] = 0;
157 return k;
161 int
162 _xtoplan9kbd(XEvent *e)
164 static Rune *r;
166 if(e == (XEvent*)-1){
167 assert(r);
168 r--;
169 return 0;
171 if(e)
172 r = xtoplan9latin1(e);
173 if(r && *r)
174 return *r++;
175 return -1;
178 int
179 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
181 int s;
182 XButtonEvent *be;
183 XMotionEvent *me;
185 if(_x.putsnarf != _x.assertsnarf){
186 _x.assertsnarf = _x.putsnarf;
187 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);
188 if(_x.clipboard != None)
189 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);
190 XFlush(xd);
193 switch(e->type){
194 case ButtonPress:
195 be = (XButtonEvent*)e;
196 /*
197 * Fake message, just sent to make us announce snarf.
198 * Apparently state and button are 16 and 8 bits on
199 * the wire, since they are truncated by the time they
200 * get to us.
201 */
202 if(be->send_event
203 && (~be->state&0xFFFF)==0
204 && (~be->button&0xFF)==0)
205 return -1;
206 /* BUG? on mac need to inherit these from elsewhere? */
207 m->xy.x = be->x;
208 m->xy.y = be->y;
209 s = be->state;
210 m->msec = be->time;
211 switch(be->button){
212 case 1:
213 s |= Button1Mask;
214 break;
215 case 2:
216 s |= Button2Mask;
217 break;
218 case 3:
219 s |= Button3Mask;
220 break;
222 break;
223 case ButtonRelease:
224 be = (XButtonEvent*)e;
225 m->xy.x = be->x;
226 m->xy.y = be->y;
227 s = be->state;
228 m->msec = be->time;
229 switch(be->button){
230 case 1:
231 s &= ~Button1Mask;
232 break;
233 case 2:
234 s &= ~Button2Mask;
235 break;
236 case 3:
237 s &= ~Button3Mask;
238 break;
240 break;
242 case MotionNotify:
243 me = (XMotionEvent*)e;
244 s = me->state;
245 m->xy.x = me->x;
246 m->xy.y = me->y;
247 m->msec = me->time;
248 break;
250 default:
251 return -1;
254 m->buttons = 0;
255 if(s & Button1Mask)
256 m->buttons |= 1;
257 if(s & Button2Mask)
258 m->buttons |= 2;
259 if(s & Button3Mask)
260 m->buttons |= 4;
262 return 0;
265 void
266 _xmoveto(Point p)
268 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
269 XFlush(_x.display);
272 static int
273 revbyte(int b)
275 int r;
277 r = 0;
278 r |= (b&0x01) << 7;
279 r |= (b&0x02) << 5;
280 r |= (b&0x04) << 3;
281 r |= (b&0x08) << 1;
282 r |= (b&0x10) >> 1;
283 r |= (b&0x20) >> 3;
284 r |= (b&0x40) >> 5;
285 r |= (b&0x80) >> 7;
286 return r;
289 static void
290 xcursorarrow(void)
292 if(_x.cursor != 0){
293 XFreeCursor(_x.display, _x.cursor);
294 _x.cursor = 0;
296 XUndefineCursor(_x.display, _x.drawable);
297 XFlush(_x.display);
301 void
302 _xsetcursor(Cursor *c)
304 XColor fg, bg;
305 XCursor xc;
306 Pixmap xsrc, xmask;
307 int i;
308 uchar src[2*16], mask[2*16];
310 if(c == nil){
311 xcursorarrow();
312 return;
314 for(i=0; i<2*16; i++){
315 src[i] = revbyte(c->set[i]);
316 mask[i] = revbyte(c->set[i] | c->clr[i]);
319 fg = _x.map[0];
320 bg = _x.map[255];
321 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
322 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
323 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
324 if(xc != 0) {
325 XDefineCursor(_x.display, _x.drawable, xc);
326 if(_x.cursor != 0)
327 XFreeCursor(_x.display, _x.cursor);
328 _x.cursor = xc;
330 XFreePixmap(_x.display, xsrc);
331 XFreePixmap(_x.display, xmask);
332 XFlush(_x.display);
335 struct {
336 char buf[SnarfSize];
337 QLock lk;
338 } clip;
340 char*
341 _xgetsnarf(XDisplay *xd)
343 uchar *data, *xdata;
344 Atom clipboard, type, prop;
345 ulong len, lastlen, dummy;
346 int fmt, i;
347 XWindow w;
349 qlock(&clip.lk);
350 /*
351 * Is there a primary selection (highlighted text in an xterm)?
352 */
353 clipboard = XA_PRIMARY;
354 w = XGetSelectionOwner(xd, XA_PRIMARY);
355 if(w == _x.drawable){
356 mine:
357 data = (uchar*)strdup(clip.buf);
358 goto out;
361 /*
362 * If not, is there a clipboard selection?
363 */
364 if(w == None && _x.clipboard != None){
365 clipboard = _x.clipboard;
366 w = XGetSelectionOwner(xd, _x.clipboard);
367 if(w == _x.drawable)
368 goto mine;
371 /*
372 * If not, give up.
373 */
374 if(w == None){
375 data = nil;
376 goto out;
379 /*
380 * We should be waiting for SelectionNotify here, but it might never
381 * come, and we have no way to time out. Instead, we will clear
382 * local property #1, request our buddy to fill it in for us, and poll
383 * until he's done or we get tired of waiting.
385 * We should try to go for _x.utf8string instead of XA_STRING,
386 * but that would add to the polling.
387 */
388 prop = 1;
389 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
390 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
391 XFlush(xd);
392 lastlen = 0;
393 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
394 usleep(100*1000);
395 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
396 &type, &fmt, &dummy, &len, &data);
397 if(lastlen == len && len > 0)
398 break;
399 lastlen = len;
401 if(i == 10){
402 data = nil;
403 goto out;
405 /* get the property */
406 data = nil;
407 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,
408 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
409 if((type != XA_STRING && type != _x.utf8string) || len == 0){
410 if(xdata)
411 XFree(xdata);
412 data = nil;
413 }else{
414 if(xdata){
415 data = (uchar*)strdup((char*)xdata);
416 XFree(xdata);
417 }else
418 data = nil;
420 out:
421 qunlock(&clip.lk);
422 return (char*)data;
425 void
426 _xputsnarf(XDisplay *xd, char *data)
428 XButtonEvent e;
430 if(strlen(data) >= SnarfSize)
431 return;
432 qlock(&clip.lk);
433 strcpy(clip.buf, data);
435 /* leave note for mouse proc to assert selection ownership */
436 _x.putsnarf++;
438 /* send mouse a fake event so snarf is announced */
439 memset(&e, 0, sizeof e);
440 e.type = ButtonPress;
441 e.window = _x.drawable;
442 e.state = ~0;
443 e.button = ~0;
444 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
445 XFlush(xd);
447 qunlock(&clip.lk);
450 int
451 _xselect(XEvent *e, XDisplay *xd)
453 char *name;
454 XEvent r;
455 XSelectionRequestEvent *xe;
456 Atom a[4];
458 memset(&r, 0, sizeof r);
459 xe = (XSelectionRequestEvent*)e;
460 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
461 xe->target, xe->requestor, xe->property, xe->selection);
462 r.xselection.property = xe->property;
463 if(xe->target == _x.targets){
464 a[0] = XA_STRING;
465 a[1] = _x.utf8string;
466 a[2] = _x.text;
467 a[3] = _x.compoundtext;
469 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
470 8, PropModeReplace, (uchar*)a, sizeof a);
471 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){
472 /* if the target is STRING we're supposed to reply with Latin1 XXX */
473 qlock(&clip.lk);
474 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
475 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
476 qunlock(&clip.lk);
477 }else{
478 name = XGetAtomName(xd, xe->target);
479 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
480 r.xselection.property = None;
483 r.xselection.display = xe->display;
484 /* r.xselection.property filled above */
485 r.xselection.target = xe->target;
486 r.xselection.type = SelectionNotify;
487 r.xselection.requestor = xe->requestor;
488 r.xselection.time = xe->time;
489 r.xselection.send_event = True;
490 r.xselection.selection = xe->selection;
491 XSendEvent(xd, xe->requestor, False, 0, &r);
492 XFlush(xd);
493 return 0;
496 void
497 putsnarf(char *data)
499 _xputsnarf(_x.snarfcon, data);
502 char*
503 getsnarf(void)
505 return _xgetsnarf(_x.snarfcon);