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"
13 #undef time
16 static int
17 __xtoplan9kbd(XEvent *e)
18 {
19 int ind, k, md;
21 md = e->xkey.state;
22 ind = 0;
23 if(md & ShiftMask)
24 ind = 1;
26 k = XKeycodeToKeysym(e->xany.display, (KeyCode)e->xkey.keycode, ind);
27 if(k == XK_Multi_key || k == NoSymbol)
28 return -1;
30 if(k&0xFF00){
31 switch(k){
32 case XK_BackSpace:
33 case XK_Tab:
34 case XK_Escape:
35 case XK_Delete:
36 case XK_KP_0:
37 case XK_KP_1:
38 case XK_KP_2:
39 case XK_KP_3:
40 case XK_KP_4:
41 case XK_KP_5:
42 case XK_KP_6:
43 case XK_KP_7:
44 case XK_KP_8:
45 case XK_KP_9:
46 case XK_KP_Divide:
47 case XK_KP_Multiply:
48 case XK_KP_Subtract:
49 case XK_KP_Add:
50 case XK_KP_Decimal:
51 k &= 0x7F;
52 break;
53 case XK_Linefeed:
54 k = '\r';
55 break;
56 case XK_KP_Space:
57 k = ' ';
58 break;
59 case XK_Home:
60 case XK_KP_Home:
61 k = Khome;
62 break;
63 case XK_Left:
64 case XK_KP_Left:
65 k = Kleft;
66 break;
67 case XK_Up:
68 case XK_KP_Up:
69 k = Kup;
70 break;
71 case XK_Down:
72 case XK_KP_Down:
73 k = Kdown;
74 break;
75 case XK_Right:
76 case XK_KP_Right:
77 k = Kright;
78 break;
79 case XK_Page_Down:
80 case XK_KP_Page_Down:
81 k = Kpgdown;
82 break;
83 case XK_End:
84 case XK_KP_End:
85 k = Kend;
86 break;
87 case XK_Page_Up:
88 case XK_KP_Page_Up:
89 k = Kpgup;
90 break;
91 case XK_Insert:
92 case XK_KP_Insert:
93 k = Kins;
94 break;
95 case XK_KP_Enter:
96 case XK_Return:
97 k = '\n';
98 break;
99 case XK_Alt_L:
100 case XK_Alt_R:
101 k = Kalt;
102 break;
103 default: /* not ISO-1 or tty control */
104 return -1;
108 /* Compensate for servers that call a minus a hyphen */
109 if(k == XK_hyphen)
110 k = XK_minus;
111 /* Do control mapping ourselves if translator doesn't */
112 if(e->xkey.state&ControlMask)
113 k &= 0x9f;
114 if(k == NoSymbol) {
115 return -1;
118 /* BUG: could/should do Alt translation here! */
119 return k;
122 static Rune*
123 xtoplan9latin1(XEvent *e)
125 static Rune k[10];
126 static int alting, nk;
127 int n;
128 int r;
130 r = __xtoplan9kbd(e);
131 if(r < 0)
132 return nil;
133 if(alting){
134 k[nk++] = r;
135 n = _latin1(k, nk);
136 if(n > 0){
137 alting = 0;
138 k[0] = n;
139 k[1] = 0;
140 return k;
142 if(n == -1){
143 alting = 0;
144 k[nk] = 0;
145 return k;
147 /* n < -1, need more input */
148 return nil;
149 }else if(r == Kalt){
150 alting = 1;
151 nk = 0;
152 return nil;
153 }else{
154 k[0] = r;
155 k[1] = 0;
156 return k;
160 int
161 _xtoplan9kbd(XEvent *e)
163 static Rune *r;
165 if(e == (XEvent*)-1){
166 assert(r);
167 r--;
168 return 0;
170 if(e)
171 r = xtoplan9latin1(e);
172 if(r && *r)
173 return *r++;
174 return -1;
177 int
178 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)
180 int s;
181 XButtonEvent *be;
182 XMotionEvent *me;
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);
189 XFlush(xd);
192 switch(e->type){
193 case ButtonPress:
194 be = (XButtonEvent*)e;
195 /*
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
199 * get to us.
200 */
201 if(be->send_event
202 && (~be->state&0xFFFF)==0
203 && (~be->button&0xFF)==0)
204 return -1;
205 /* BUG? on mac need to inherit these from elsewhere? */
206 m->xy.x = be->x;
207 m->xy.y = be->y;
208 s = be->state;
209 m->msec = be->time;
210 switch(be->button){
211 case 1:
212 s |= Button1Mask;
213 break;
214 case 2:
215 s |= Button2Mask;
216 break;
217 case 3:
218 s |= Button3Mask;
219 break;
221 break;
222 case ButtonRelease:
223 be = (XButtonEvent*)e;
224 m->xy.x = be->x;
225 m->xy.y = be->y;
226 s = be->state;
227 m->msec = be->time;
228 switch(be->button){
229 case 1:
230 s &= ~Button1Mask;
231 break;
232 case 2:
233 s &= ~Button2Mask;
234 break;
235 case 3:
236 s &= ~Button3Mask;
237 break;
239 break;
241 case MotionNotify:
242 me = (XMotionEvent*)e;
243 s = me->state;
244 m->xy.x = me->x;
245 m->xy.y = me->y;
246 m->msec = me->time;
247 break;
249 default:
250 return -1;
253 m->buttons = 0;
254 if(s & Button1Mask)
255 m->buttons |= 1;
256 if(s & Button2Mask)
257 m->buttons |= 2;
258 if(s & Button3Mask)
259 m->buttons |= 4;
261 return 0;
264 void
265 _xmoveto(Point p)
267 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
268 XFlush(_x.display);
271 static int
272 revbyte(int b)
274 int r;
276 r = 0;
277 r |= (b&0x01) << 7;
278 r |= (b&0x02) << 5;
279 r |= (b&0x04) << 3;
280 r |= (b&0x08) << 1;
281 r |= (b&0x10) >> 1;
282 r |= (b&0x20) >> 3;
283 r |= (b&0x40) >> 5;
284 r |= (b&0x80) >> 7;
285 return r;
288 static void
289 xcursorarrow(void)
291 if(_x.cursor != 0){
292 XFreeCursor(_x.display, _x.cursor);
293 _x.cursor = 0;
295 XUndefineCursor(_x.display, _x.drawable);
296 XFlush(_x.display);
300 void
301 _xsetcursor(Cursor *c)
303 XColor fg, bg;
304 XCursor xc;
305 Pixmap xsrc, xmask;
306 int i;
307 uchar src[2*16], mask[2*16];
309 if(c == nil){
310 xcursorarrow();
311 return;
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]);
318 fg = _x.map[0];
319 bg = _x.map[255];
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);
323 if(xc != 0) {
324 XDefineCursor(_x.display, _x.drawable, xc);
325 if(_x.cursor != 0)
326 XFreeCursor(_x.display, _x.cursor);
327 _x.cursor = xc;
329 XFreePixmap(_x.display, xsrc);
330 XFreePixmap(_x.display, xmask);
331 XFlush(_x.display);
334 struct {
335 char buf[SnarfSize];
336 QLock lk;
337 } clip;
339 char*
340 _xgetsnarf(XDisplay *xd)
342 uchar *data, *xdata;
343 Atom clipboard, type, prop;
344 ulong len, lastlen, dummy;
345 int fmt, i;
346 XWindow w;
348 qlock(&clip.lk);
349 /*
350 * Is there a primary selection (highlighted text in an xterm)?
351 */
352 clipboard = XA_PRIMARY;
353 w = XGetSelectionOwner(xd, XA_PRIMARY);
354 if(w == _x.drawable){
355 mine:
356 data = (uchar*)strdup(clip.buf);
357 goto out;
360 /*
361 * If not, is there a clipboard selection?
362 */
363 if(w == None && _x.clipboard != None){
364 clipboard = _x.clipboard;
365 w = XGetSelectionOwner(xd, _x.clipboard);
366 if(w == _x.drawable)
367 goto mine;
370 /*
371 * If not, give up.
372 */
373 if(w == None){
374 data = nil;
375 goto out;
378 /*
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.
386 */
387 prop = 1;
388 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
389 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
390 XFlush(xd);
391 lastlen = 0;
392 for(i=0; i<10 || (lastlen!=0 && i<30); i++){
393 usleep(100*1000);
394 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
395 &type, &fmt, &dummy, &len, &data);
396 if(lastlen == len && len > 0)
397 break;
398 lastlen = len;
400 if(i == 10){
401 data = nil;
402 goto out;
404 /* get the property */
405 data = nil;
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){
409 if(xdata)
410 XFree(xdata);
411 data = nil;
412 }else{
413 if(xdata){
414 data = (uchar*)strdup((char*)xdata);
415 XFree(xdata);
416 }else
417 data = nil;
419 out:
420 qunlock(&clip.lk);
421 return (char*)data;
424 void
425 _xputsnarf(XDisplay *xd, char *data)
427 XButtonEvent e;
429 if(strlen(data) >= SnarfSize)
430 return;
431 qlock(&clip.lk);
432 strcpy(clip.buf, data);
434 /* leave note for mouse proc to assert selection ownership */
435 _x.putsnarf++;
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;
441 e.state = ~0;
442 e.button = ~0;
443 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
444 XFlush(xd);
446 qunlock(&clip.lk);
449 int
450 _xselect(XEvent *e, XDisplay *xd)
452 char *name;
453 XEvent r;
454 XSelectionRequestEvent *xe;
455 Atom a[4];
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){
463 a[0] = XA_STRING;
464 a[1] = _x.utf8string;
465 a[2] = _x.text;
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 */
472 qlock(&clip.lk);
473 XChangeProperty(xd, xe->requestor, xe->property, xe->target,
474 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
475 qunlock(&clip.lk);
476 }else{
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);
491 XFlush(xd);
492 return 0;
495 void
496 putsnarf(char *data)
498 _xputsnarf(_x.snarfcon, data);
501 char*
502 getsnarf(void)
504 return _xgetsnarf(_x.snarfcon);