Blob
- Date:
- Message:
- Apparently SuSE 9.1's X distribution uses even bigger buffers than the standard ones. 64kB appears to be enough for a stack in that case, but let's just go nuts and make the stacks enormous, so that it takes a few more doublings of X's stack needs before we run into this problem again. The VM system should take care of not actually using most of the memory anyway.
- Actions:
- History | Blame | Raw File
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 time17 static KeySym18 __xtoplan9kbd(XEvent *e)19 {20 KeySym k;22 if(e->xany.type != KeyPress)23 return -1;24 needstack(64*1024); /* X has some *huge* buffers in openobject */25 /* and they're even bigger on SuSE */26 XLookupString((XKeyEvent*)e,NULL,0,&k,NULL);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_Meta_L: /* Shift Alt on PCs */101 case XK_Alt_R:102 case XK_Meta_R: /* Shift Alt on PCs */103 k = Kalt;104 break;105 default: /* not ISO-1 or tty control */106 if(k>0xff) {107 k = _p9keysym2ucs(k);108 if(k==-1) return -1;109 }110 }111 }113 /* Compensate for servers that call a minus a hyphen */114 if(k == XK_hyphen)115 k = XK_minus;116 /* Do control mapping ourselves if translator doesn't */117 if(e->xkey.state&ControlMask)118 k &= 0x9f;119 if(k == NoSymbol) {120 return -1;121 }123 return k+0;124 }126 static Rune*127 xtoplan9latin1(XEvent *e)128 {129 static Rune k[10];130 static int alting, nk;131 int n;132 int r;134 r = __xtoplan9kbd(e);135 if(r < 0)136 return nil;137 if(alting){138 /*139 * Kludge for Mac's X11 3-button emulation.140 * It treats Command+Button as button 3, but also141 * ends up sending XK_Meta_L twice.142 */143 if(r == Kalt){144 alting = 0;145 return nil;146 }147 k[nk++] = r;148 n = _latin1(k, nk);149 if(n > 0){150 alting = 0;151 k[0] = n;152 k[1] = 0;153 return k;154 }155 if(n == -1){156 alting = 0;157 k[nk] = 0;158 return k;159 }160 /* n < -1, need more input */161 return nil;162 }else if(r == Kalt){163 alting = 1;164 nk = 0;165 return nil;166 }else{167 k[0] = r;168 k[1] = 0;169 return k;170 }171 }173 int174 _xtoplan9kbd(XEvent *e)175 {176 static Rune *r;178 if(e == (XEvent*)-1){179 assert(r);180 r--;181 return 0;182 }183 if(e)184 r = xtoplan9latin1(e);185 if(r && *r)186 return *r++;187 return -1;188 }190 int191 _xtoplan9mouse(XDisplay *xd, XEvent *e, Mouse *m)192 {193 int s;194 XButtonEvent *be;195 XMotionEvent *me;197 if(_x.putsnarf != _x.assertsnarf){198 _x.assertsnarf = _x.putsnarf;199 XSetSelectionOwner(_x.mousecon, XA_PRIMARY, _x.drawable, CurrentTime);200 if(_x.clipboard != None)201 XSetSelectionOwner(_x.mousecon, _x.clipboard, _x.drawable, CurrentTime);202 XFlush(xd);203 }205 switch(e->type){206 case ButtonPress:207 be = (XButtonEvent*)e;208 /*209 * Fake message, just sent to make us announce snarf.210 * Apparently state and button are 16 and 8 bits on211 * the wire, since they are truncated by the time they212 * get to us.213 */214 if(be->send_event215 && (~be->state&0xFFFF)==0216 && (~be->button&0xFF)==0)217 return -1;218 /* BUG? on mac need to inherit these from elsewhere? */219 m->xy.x = be->x;220 m->xy.y = be->y;221 s = be->state;222 m->msec = be->time;223 switch(be->button){224 case 1:225 s |= Button1Mask;226 break;227 case 2:228 s |= Button2Mask;229 break;230 case 3:231 s |= Button3Mask;232 break;233 case 4:234 s |= Button4Mask;235 break;236 case 5:237 s |= Button5Mask;238 break;239 }240 break;241 case ButtonRelease:242 be = (XButtonEvent*)e;243 m->xy.x = be->x;244 m->xy.y = be->y;245 s = be->state;246 m->msec = be->time;247 switch(be->button){248 case 1:249 s &= ~Button1Mask;250 break;251 case 2:252 s &= ~Button2Mask;253 break;254 case 3:255 s &= ~Button3Mask;256 break;257 case 4:258 s &= ~Button4Mask;259 break;260 case 5:261 s &= ~Button5Mask;262 break;263 }264 break;266 case MotionNotify:267 me = (XMotionEvent*)e;268 s = me->state;269 m->xy.x = me->x;270 m->xy.y = me->y;271 m->msec = me->time;272 break;274 default:275 return -1;276 }278 m->buttons = 0;279 if(s & Button1Mask)280 m->buttons |= 1;281 if(s & Button2Mask)282 m->buttons |= 2;283 if(s & Button3Mask)284 m->buttons |= 4;285 if(s & Button4Mask)286 m->buttons |= 8;287 if(s & Button5Mask)288 m->buttons |= 16;289 return 0;290 }292 void293 _xmoveto(Point p)294 {295 XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);296 XFlush(_x.display);297 }299 static int300 revbyte(int b)301 {302 int r;304 r = 0;305 r |= (b&0x01) << 7;306 r |= (b&0x02) << 5;307 r |= (b&0x04) << 3;308 r |= (b&0x08) << 1;309 r |= (b&0x10) >> 1;310 r |= (b&0x20) >> 3;311 r |= (b&0x40) >> 5;312 r |= (b&0x80) >> 7;313 return r;314 }316 static void317 xcursorarrow(void)318 {319 if(_x.cursor != 0){320 XFreeCursor(_x.display, _x.cursor);321 _x.cursor = 0;322 }323 XUndefineCursor(_x.display, _x.drawable);324 XFlush(_x.display);325 }328 void329 _xsetcursor(Cursor *c)330 {331 XColor fg, bg;332 XCursor xc;333 Pixmap xsrc, xmask;334 int i;335 uchar src[2*16], mask[2*16];337 if(c == nil){338 xcursorarrow();339 return;340 }341 for(i=0; i<2*16; i++){342 src[i] = revbyte(c->set[i]);343 mask[i] = revbyte(c->set[i] | c->clr[i]);344 }346 fg = _x.map[0];347 bg = _x.map[255];348 xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);349 xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);350 xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);351 if(xc != 0) {352 XDefineCursor(_x.display, _x.drawable, xc);353 if(_x.cursor != 0)354 XFreeCursor(_x.display, _x.cursor);355 _x.cursor = xc;356 }357 XFreePixmap(_x.display, xsrc);358 XFreePixmap(_x.display, xmask);359 XFlush(_x.display);360 }362 struct {363 char buf[SnarfSize];364 QLock lk;365 } clip;367 char*368 _xgetsnarf(XDisplay *xd)369 {370 uchar *data, *xdata;371 Atom clipboard, type, prop;372 ulong len, lastlen, dummy;373 int fmt, i;374 XWindow w;376 qlock(&clip.lk);377 /*378 * Is there a primary selection (highlighted text in an xterm)?379 */380 clipboard = XA_PRIMARY;381 w = XGetSelectionOwner(xd, XA_PRIMARY);382 if(w == _x.drawable){383 mine:384 data = (uchar*)strdup(clip.buf);385 goto out;386 }388 /*389 * If not, is there a clipboard selection?390 */391 if(w == None && _x.clipboard != None){392 clipboard = _x.clipboard;393 w = XGetSelectionOwner(xd, _x.clipboard);394 if(w == _x.drawable)395 goto mine;396 }398 /*399 * If not, give up.400 */401 if(w == None){402 data = nil;403 goto out;404 }406 /*407 * We should be waiting for SelectionNotify here, but it might never408 * come, and we have no way to time out. Instead, we will clear409 * local property #1, request our buddy to fill it in for us, and poll410 * until he's done or we get tired of waiting.411 *412 * We should try to go for _x.utf8string instead of XA_STRING,413 * but that would add to the polling.414 */415 prop = 1;416 XChangeProperty(xd, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);417 XConvertSelection(xd, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);418 XFlush(xd);419 lastlen = 0;420 for(i=0; i<10 || (lastlen!=0 && i<30); i++){421 usleep(100*1000);422 XGetWindowProperty(xd, _x.drawable, prop, 0, 0, 0, AnyPropertyType,423 &type, &fmt, &dummy, &len, &data);424 if(lastlen == len && len > 0)425 break;426 lastlen = len;427 }428 if(i == 10){429 data = nil;430 goto out;431 }432 /* get the property */433 data = nil;434 XGetWindowProperty(xd, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0,435 AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);436 if((type != XA_STRING && type != _x.utf8string) || len == 0){437 if(xdata)438 XFree(xdata);439 data = nil;440 }else{441 if(xdata){442 data = (uchar*)strdup((char*)xdata);443 XFree(xdata);444 }else445 data = nil;446 }447 out:448 qunlock(&clip.lk);449 return (char*)data;450 }452 void453 _xputsnarf(XDisplay *xd, char *data)454 {455 XButtonEvent e;457 if(strlen(data) >= SnarfSize)458 return;459 qlock(&clip.lk);460 strcpy(clip.buf, data);462 /* leave note for mouse proc to assert selection ownership */463 _x.putsnarf++;465 /* send mouse a fake event so snarf is announced */466 memset(&e, 0, sizeof e);467 e.type = ButtonPress;468 e.window = _x.drawable;469 e.state = ~0;470 e.button = ~0;471 XSendEvent(xd, _x.drawable, True, ButtonPressMask, (XEvent*)&e);472 XFlush(xd);474 qunlock(&clip.lk);475 }477 int478 _xselect(XEvent *e, XDisplay *xd)479 {480 char *name;481 XEvent r;482 XSelectionRequestEvent *xe;483 Atom a[4];485 memset(&r, 0, sizeof r);486 xe = (XSelectionRequestEvent*)e;487 if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",488 xe->target, xe->requestor, xe->property, xe->selection);489 r.xselection.property = xe->property;490 if(xe->target == _x.targets){491 a[0] = XA_STRING;492 a[1] = _x.utf8string;493 a[2] = _x.text;494 a[3] = _x.compoundtext;496 XChangeProperty(xd, xe->requestor, xe->property, xe->target,497 8, PropModeReplace, (uchar*)a, sizeof a);498 }else if(xe->target == XA_STRING || xe->target == _x.utf8string || xe->target == _x.text || xe->target == _x.compoundtext){499 /* if the target is STRING we're supposed to reply with Latin1 XXX */500 qlock(&clip.lk);501 XChangeProperty(xd, xe->requestor, xe->property, xe->target,502 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));503 qunlock(&clip.lk);504 }else{505 name = XGetAtomName(xd, xe->target);506 if(strcmp(name, "TIMESTAMP") != 0)507 fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);508 r.xselection.property = None;509 }511 r.xselection.display = xe->display;512 /* r.xselection.property filled above */513 r.xselection.target = xe->target;514 r.xselection.type = SelectionNotify;515 r.xselection.requestor = xe->requestor;516 r.xselection.time = xe->time;517 r.xselection.send_event = True;518 r.xselection.selection = xe->selection;519 XSendEvent(xd, xe->requestor, False, 0, &r);520 XFlush(xd);521 return 0;522 }524 void525 putsnarf(char *data)526 {527 _xputsnarf(_x.snarfcon, data);528 }530 char*531 getsnarf(void)532 {533 return _xgetsnarf(_x.snarfcon);534 }