Commit Diff


commit - f97f53744067400bec4dbd7aae5eafde54e99b7f
commit + c66b52501b630f6c0cdee1ed6cf81ad153e1183f
blob - /dev/null
blob + 6b6dca6e83274c4b3cf227eea002fc78c11e14e3 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/devdraw.c
@@ -0,0 +1,1607 @@
+/*
+ * /dev/draw simulator -- handles the messages prepared by the draw library.
+ * Doesn't simulate the file system part, just the messages.
+ */
+
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include <memlayer.h>
+#include "devdraw.h"
+
+extern void _flushmemscreen(Rectangle);
+
+#define NHASH (1<<5)
+#define HASHMASK (NHASH-1)
+
+typedef struct Client Client;
+typedef struct Draw Draw;
+typedef struct DImage DImage;
+typedef struct DScreen DScreen;
+typedef struct CScreen CScreen;
+typedef struct FChar FChar;
+typedef struct Refresh Refresh;
+typedef struct Refx Refx;
+typedef struct DName DName;
+
+struct Draw
+{
+	QLock		lk;
+	int		clientid;
+	int		nclient;
+	Client*		client[1];
+	int		nname;
+	DName*		name;
+	int		vers;
+	int		softscreen;
+};
+
+struct Client
+{
+	/*Ref		r;*/
+	DImage*		dimage[NHASH];
+	CScreen*	cscreen;
+	Refresh*	refresh;
+	Rendez		refrend;
+	uchar*		readdata;
+	int		nreaddata;
+	int		busy;
+	int		clientid;
+	int		slot;
+	int		refreshme;
+	int		infoid;
+	int		op;
+};
+
+struct Refresh
+{
+	DImage*		dimage;
+	Rectangle	r;
+	Refresh*	next;
+};
+
+struct Refx
+{
+	Client*		client;
+	DImage*		dimage;
+};
+
+struct DName
+{
+	char			*name;
+	Client	*client;
+	DImage*		dimage;
+	int			vers;
+};
+
+struct FChar
+{
+	int		minx;	/* left edge of bits */
+	int		maxx;	/* right edge of bits */
+	uchar		miny;	/* first non-zero scan-line */
+	uchar		maxy;	/* last non-zero scan-line + 1 */
+	schar		left;	/* offset of baseline */
+	uchar		width;	/* width of baseline */
+};
+
+/*
+ * Reference counts in DImages:
+ *	one per open by original client
+ *	one per screen image or fill
+ * 	one per image derived from this one by name
+ */
+struct DImage
+{
+	int		id;
+	int		ref;
+	char		*name;
+	int		vers;
+	Memimage*	image;
+	int		ascent;
+	int		nfchar;
+	FChar*		fchar;
+	DScreen*	dscreen;	/* 0 if not a window */
+	DImage*	fromname;	/* image this one is derived from, by name */
+	DImage*		next;
+};
+
+struct CScreen
+{
+	DScreen*	dscreen;
+	CScreen*	next;
+};
+
+struct DScreen
+{
+	int		id;
+	int		public;
+	int		ref;
+	DImage	*dimage;
+	DImage	*dfill;
+	Memscreen*	screen;
+	Client*		owner;
+	DScreen*	next;
+};
+
+static	Draw		sdraw;
+static	Client		*client0;
+static	Memimage	*screenimage;
+static	Rectangle	flushrect;
+static	int		waste;
+static	DScreen*	dscreen;
+static	int		drawuninstall(Client*, int);
+static	Memimage*	drawinstall(Client*, int, Memimage*, DScreen*);
+static	void		drawfreedimage(DImage*);
+
+void
+_initdisplaymemimage(Memimage *m)
+{
+	screenimage = m;
+	m->screenref = 1;
+	client0 = mallocz(sizeof(Client), 1);
+	if(client0 == nil){
+		fprint(2, "initdraw: allocating client0: out of memory");
+		abort();
+	}
+	client0->slot = 0;
+	client0->clientid = ++sdraw.clientid;
+	client0->op = SoverD;
+	sdraw.client[0] = client0;
+	sdraw.nclient = 1;
+	sdraw.softscreen = 1;
+}
+
+void
+_drawreplacescreenimage(Memimage *m)
+{
+	/*
+	 * Replace the screen image because the screen
+	 * was resized.
+	 * 
+	 * In theory there should only be one reference
+	 * to the current screen image, and that's through
+	 * client0's image 0, installed a few lines above.
+	 * Once the client drops the image, the underlying backing 
+	 * store freed properly.  The client is being notified
+	 * about the resize through external means, so all we
+	 * need to do is this assignment.
+	 */
+	Memimage *om;
+
+	qlock(&sdraw.lk);
+	om = screenimage;
+	screenimage = m;
+	m->screenref = 1;
+	if(om && --om->screenref == 0){
+		_freememimage(om);
+	}
+	qunlock(&sdraw.lk);
+}
+
+static
+void
+drawrefreshscreen(DImage *l, Client *client)
+{
+	while(l != nil && l->dscreen == nil)
+		l = l->fromname;
+	if(l != nil && l->dscreen->owner != client)
+		l->dscreen->owner->refreshme = 1;
+}
+
+static
+void
+drawrefresh(Memimage *m, Rectangle r, void *v)
+{
+	Refx *x;
+	DImage *d;
+	Client *c;
+	Refresh *ref;
+
+	USED(m);
+
+	if(v == 0)
+		return;
+	x = v;
+	c = x->client;
+	d = x->dimage;
+	for(ref=c->refresh; ref; ref=ref->next)
+		if(ref->dimage == d){
+			combinerect(&ref->r, r);
+			return;
+		}
+	ref = mallocz(sizeof(Refresh), 1);
+	if(ref){
+		ref->dimage = d;
+		ref->r = r;
+		ref->next = c->refresh;
+		c->refresh = ref;
+	}
+}
+
+static void
+addflush(Rectangle r)
+{
+	int abb, ar, anbb;
+	Rectangle nbb;
+
+	if(sdraw.softscreen==0 || !rectclip(&r, screenimage->r))
+		return;
+
+	if(flushrect.min.x >= flushrect.max.x){
+		flushrect = r;
+		waste = 0;
+		return;
+	}
+	nbb = flushrect;
+	combinerect(&nbb, r);
+	ar = Dx(r)*Dy(r);
+	abb = Dx(flushrect)*Dy(flushrect);
+	anbb = Dx(nbb)*Dy(nbb);
+	/*
+	 * Area of new waste is area of new bb minus area of old bb,
+	 * less the area of the new segment, which we assume is not waste.
+	 * This could be negative, but that's OK.
+	 */
+	waste += anbb-abb - ar;
+	if(waste < 0)
+		waste = 0;
+	/*
+	 * absorb if:
+	 *	total area is small
+	 *	waste is less than half total area
+	 * 	rectangles touch
+	 */
+	if(anbb<=1024 || waste*2<anbb || rectXrect(flushrect, r)){
+		flushrect = nbb;
+		return;
+	}
+	/* emit current state */
+	if(flushrect.min.x < flushrect.max.x)
+		_flushmemscreen(flushrect);
+	flushrect = r;
+	waste = 0;
+}
+
+static
+void
+dstflush(int dstid, Memimage *dst, Rectangle r)
+{
+	Memlayer *l;
+
+	if(dstid == 0){
+		combinerect(&flushrect, r);
+		return;
+	}
+	/* how can this happen? -rsc, dec 12 2002 */
+	if(dst == 0){
+		print("nil dstflush\n");
+		return;
+	}
+	l = dst->layer;
+	if(l == nil)
+		return;
+	do{
+		if(l->screen->image->data != screenimage->data)
+			return;
+		r = rectaddpt(r, l->delta);
+		l = l->screen->image->layer;
+	}while(l);
+	addflush(r);
+}
+
+static
+void
+drawflush(void)
+{
+	if(flushrect.min.x < flushrect.max.x)
+		_flushmemscreen(flushrect);
+	flushrect = Rect(10000, 10000, -10000, -10000);
+}
+
+static
+int
+drawcmp(char *a, char *b, int n)
+{
+	if(strlen(a) != n)
+		return 1;
+	return memcmp(a, b, n);
+}
+
+static
+DName*
+drawlookupname(int n, char *str)
+{
+	DName *name, *ename;
+
+	name = sdraw.name;
+	ename = &name[sdraw.nname];
+	for(; name<ename; name++)
+		if(drawcmp(name->name, str, n) == 0)
+			return name;
+	return 0;
+}
+
+static
+int
+drawgoodname(DImage *d)
+{
+	DName *n;
+
+	/* if window, validate the screen's own images */
+	if(d->dscreen)
+		if(drawgoodname(d->dscreen->dimage) == 0
+		|| drawgoodname(d->dscreen->dfill) == 0)
+			return 0;
+	if(d->name == nil)
+		return 1;
+	n = drawlookupname(strlen(d->name), d->name);
+	if(n==nil || n->vers!=d->vers)
+		return 0;
+	return 1;
+}
+
+static
+DImage*
+drawlookup(Client *client, int id, int checkname)
+{
+	DImage *d;
+
+	d = client->dimage[id&HASHMASK];
+	while(d){
+		if(d->id == id){
+			/*
+			 * BUG: should error out but too hard.
+			 * Return 0 instead.
+			 */
+			if(checkname && !drawgoodname(d))
+				return 0;
+			return d;
+		}
+		d = d->next;
+	}
+	return 0;
+}
+
+static
+DScreen*
+drawlookupdscreen(int id)
+{
+	DScreen *s;
+
+	s = dscreen;
+	while(s){
+		if(s->id == id)
+			return s;
+		s = s->next;
+	}
+	return 0;
+}
+
+static
+DScreen*
+drawlookupscreen(Client *client, int id, CScreen **cs)
+{
+	CScreen *s;
+
+	s = client->cscreen;
+	while(s){
+		if(s->dscreen->id == id){
+			*cs = s;
+			return s->dscreen;
+		}
+		s = s->next;
+	}
+	/* caller must check! */
+	return 0;
+}
+
+static
+Memimage*
+drawinstall(Client *client, int id, Memimage *i, DScreen *dscreen)
+{
+	DImage *d;
+
+	d = mallocz(sizeof(DImage), 1);
+	if(d == 0)
+		return 0;
+	d->id = id;
+	d->ref = 1;
+	d->name = 0;
+	d->vers = 0;
+	d->image = i;
+	if(i->screenref)
+		++i->screenref;
+	d->nfchar = 0;
+	d->fchar = 0;
+	d->fromname = 0;
+	d->dscreen = dscreen;
+	d->next = client->dimage[id&HASHMASK];
+	client->dimage[id&HASHMASK] = d;
+	return i;
+}
+
+static
+Memscreen*
+drawinstallscreen(Client *client, DScreen *d, int id, DImage *dimage, DImage *dfill, int public)
+{
+	Memscreen *s;
+	CScreen *c;
+
+	c = mallocz(sizeof(CScreen), 1);
+	if(dimage && dimage->image && dimage->image->chan == 0){
+		print("bad image %p in drawinstallscreen", dimage->image);
+		abort();
+	}
+
+	if(c == 0)
+		return 0;
+	if(d == 0){
+		d = mallocz(sizeof(DScreen), 1);
+		if(d == 0){
+			free(c);
+			return 0;
+		}
+		s = mallocz(sizeof(Memscreen), 1);
+		if(s == 0){
+			free(c);
+			free(d);
+			return 0;
+		}
+		s->frontmost = 0;
+		s->rearmost = 0;
+		d->dimage = dimage;
+		if(dimage){
+			s->image = dimage->image;
+			dimage->ref++;
+		}
+		d->dfill = dfill;
+		if(dfill){
+			s->fill = dfill->image;
+			dfill->ref++;
+		}
+		d->ref = 0;
+		d->id = id;
+		d->screen = s;
+		d->public = public;
+		d->next = dscreen;
+		d->owner = client;
+		dscreen = d;
+	}
+	c->dscreen = d;
+	d->ref++;
+	c->next = client->cscreen;
+	client->cscreen = c;
+	return d->screen;
+}
+
+static
+void
+drawdelname(DName *name)
+{
+	int i;
+
+	i = name-sdraw.name;
+	memmove(name, name+1, (sdraw.nname-(i+1))*sizeof(DName));
+	sdraw.nname--;
+}
+
+static
+void
+drawfreedscreen(DScreen *this)
+{
+	DScreen *ds, *next;
+
+	this->ref--;
+	if(this->ref < 0)
+		print("negative ref in drawfreedscreen\n");
+	if(this->ref > 0)
+		return;
+	ds = dscreen;
+	if(ds == this){
+		dscreen = this->next;
+		goto Found;
+	}
+	while(next = ds->next){	/* assign = */
+		if(next == this){
+			ds->next = this->next;
+			goto Found;
+		}
+		ds = next;
+	}
+	/*
+	 * Should signal Enodrawimage, but too hard.
+	 */
+	return;
+
+    Found:
+	if(this->dimage)
+		drawfreedimage(this->dimage);
+	if(this->dfill)
+		drawfreedimage(this->dfill);
+	free(this->screen);
+	free(this);
+}
+
+static
+void
+drawfreedimage(DImage *dimage)
+{
+	int i;
+	Memimage *l;
+	DScreen *ds;
+
+	dimage->ref--;
+	if(dimage->ref < 0)
+		print("negative ref in drawfreedimage\n");
+	if(dimage->ref > 0)
+		return;
+
+	/* any names? */
+	for(i=0; i<sdraw.nname; )
+		if(sdraw.name[i].dimage == dimage)
+			drawdelname(sdraw.name+i);
+		else
+			i++;
+	if(dimage->fromname){	/* acquired by name; owned by someone else*/
+		drawfreedimage(dimage->fromname);
+		goto Return;
+	}
+	ds = dimage->dscreen;
+	l = dimage->image;
+	dimage->dscreen = nil;	/* paranoia */
+	dimage->image = nil;
+	if(ds){
+		if(l->data == screenimage->data)
+			addflush(l->layer->screenr);
+		if(l->layer->refreshfn == drawrefresh)	/* else true owner will clean up */
+			free(l->layer->refreshptr);
+		l->layer->refreshptr = nil;
+		if(drawgoodname(dimage))
+			memldelete(l);
+		else
+			memlfree(l);
+		drawfreedscreen(ds);
+	}else{
+		if(l->screenref==0)
+			freememimage(l);
+		else if(--l->screenref==0)
+			_freememimage(l);
+	}
+    Return:
+	free(dimage->fchar);
+	free(dimage);
+}
+
+static
+void
+drawuninstallscreen(Client *client, CScreen *this)
+{
+	CScreen *cs, *next;
+
+	cs = client->cscreen;
+	if(cs == this){
+		client->cscreen = this->next;
+		drawfreedscreen(this->dscreen);
+		free(this);
+		return;
+	}
+	while(next = cs->next){	/* assign = */
+		if(next == this){
+			cs->next = this->next;
+			drawfreedscreen(this->dscreen);
+			free(this);
+			return;
+		}
+		cs = next;
+	}
+}
+
+static
+int
+drawuninstall(Client *client, int id)
+{
+	DImage *d, **l;
+
+	for(l=&client->dimage[id&HASHMASK]; (d=*l) != nil; l=&d->next){
+		if(d->id == id){
+			*l = d->next;
+			drawfreedimage(d);
+			return 0;
+		}
+	}
+	return -1;
+}
+
+static
+int
+drawaddname(Client *client, DImage *di, int n, char *str, char **err)
+{
+	DName *name, *ename, *new, *t;
+	char *ns;
+
+	name = sdraw.name;
+	ename = &name[sdraw.nname];
+	for(; name<ename; name++)
+		if(drawcmp(name->name, str, n) == 0){
+			*err = "image name in use";
+			return -1;
+		}
+	t = mallocz((sdraw.nname+1)*sizeof(DName), 1);
+	ns = malloc(n+1);
+	if(t == nil || ns == nil){
+		free(t);
+		free(ns);
+		*err = "out of memory";
+		return -1;
+	}
+	memmove(t, sdraw.name, sdraw.nname*sizeof(DName));
+	free(sdraw.name);
+	sdraw.name = t;
+	new = &sdraw.name[sdraw.nname++];
+	new->name = ns;
+	memmove(new->name, str, n);
+	new->name[n] = 0;
+	new->dimage = di;
+	new->client = client;
+	new->vers = ++sdraw.vers;
+	return 0;
+}
+
+static int
+drawclientop(Client *cl)
+{
+	int op;
+
+	op = cl->op;
+	cl->op = SoverD;
+	return op;
+}
+
+static
+Memimage*
+drawimage(Client *client, uchar *a)
+{
+	DImage *d;
+
+	d = drawlookup(client, BGLONG(a), 1);
+	if(d == nil)
+		return nil;	/* caller must check! */
+	return d->image;
+}
+
+static
+void
+drawrectangle(Rectangle *r, uchar *a)
+{
+	r->min.x = BGLONG(a+0*4);
+	r->min.y = BGLONG(a+1*4);
+	r->max.x = BGLONG(a+2*4);
+	r->max.y = BGLONG(a+3*4);
+}
+
+static
+void
+drawpoint(Point *p, uchar *a)
+{
+	p->x = BGLONG(a+0*4);
+	p->y = BGLONG(a+1*4);
+}
+
+static
+Point
+drawchar(Memimage *dst, Point p, Memimage *src, Point *sp, DImage *font, int index, int op)
+{
+	FChar *fc;
+	Rectangle r;
+	Point sp1;
+
+	fc = &font->fchar[index];
+	r.min.x = p.x+fc->left;
+	r.min.y = p.y-(font->ascent-fc->miny);
+	r.max.x = r.min.x+(fc->maxx-fc->minx);
+	r.max.y = r.min.y+(fc->maxy-fc->miny);
+	sp1.x = sp->x+fc->left;
+	sp1.y = sp->y+fc->miny;
+	memdraw(dst, r, src, sp1, font->image, Pt(fc->minx, fc->miny), op);
+	p.x += fc->width;
+	sp->x += fc->width;
+	return p;
+}
+
+static
+uchar*
+drawcoord(uchar *p, uchar *maxp, int oldx, int *newx)
+{
+	int b, x;
+
+	if(p >= maxp)
+		return nil;
+	b = *p++;
+	x = b & 0x7F;
+	if(b & 0x80){
+		if(p+1 >= maxp)
+			return nil;
+		x |= *p++ << 7;
+		x |= *p++ << 15;
+		if(x & (1<<22))
+			x |= ~0<<23;
+	}else{
+		if(b & 0x40)
+			x |= ~0<<7;
+		x += oldx;
+	}
+	*newx = x;
+	return p;
+}
+
+int
+_drawmsgread(void *a, int n)
+{
+	Client *cl;
+
+	qlock(&sdraw.lk);
+	cl = client0;
+	if(cl->readdata == nil){
+		werrstr("no draw data");
+		goto err;
+	}
+	if(n < cl->nreaddata){
+		werrstr("short read");
+		goto err;
+	}
+	n = cl->nreaddata;
+	memmove(a, cl->readdata, cl->nreaddata);
+	free(cl->readdata);
+	cl->readdata = nil;
+	qunlock(&sdraw.lk);
+	return n;
+
+err:
+	qunlock(&sdraw.lk);
+	return -1;
+}
+
+int
+_drawmsgwrite(void *v, int n)
+{
+	char cbuf[40], *err, ibuf[12*12+1], *s;
+	int c, ci, doflush, dstid, e0, e1, esize, j, m;
+	int ni, nw, oesize, oldn, op, ox, oy, repl, scrnid, y; 
+	uchar *a, refresh, *u;
+	u32int chan, value;
+	Client *client;
+	CScreen *cs;
+	DImage *di, *ddst, *dsrc, *font, *ll;
+	DName *dn;
+	DScreen *dscrn;
+	FChar *fc;
+	Memimage *dst, *i, *l, **lp, *mask, *src;
+	Memscreen *scrn;
+	Point p, *pp, q, sp;
+	Rectangle clipr, r;
+	Refreshfn reffn;
+	Refx *refx;
+
+	qlock(&sdraw.lk);
+	a = v;
+	m = 0;
+	oldn = n;
+	client = client0;
+
+	while((n-=m) > 0){
+		a += m;
+/*fprint(2, "msgwrite %d(%d)...", n, *a); */
+		switch(*a){
+		default:
+/*fprint(2, "bad command %d\n", *a); */
+			err = "bad draw command";
+			goto error;
+
+		/* allocate: 'b' id[4] screenid[4] refresh[1] chan[4] repl[1]
+			R[4*4] clipR[4*4] rrggbbaa[4]
+		 */
+		case 'b':
+			m = 1+4+4+1+4+1+4*4+4*4+4;
+			if(n < m)
+				goto Eshortdraw;
+			dstid = BGLONG(a+1);
+			scrnid = BGSHORT(a+5);
+			refresh = a[9];
+			chan = BGLONG(a+10);
+			repl = a[14];
+			drawrectangle(&r, a+15);
+			drawrectangle(&clipr, a+31);
+			value = BGLONG(a+47);
+			if(drawlookup(client, dstid, 0))
+				goto Eimageexists;
+			if(scrnid){
+				dscrn = drawlookupscreen(client, scrnid, &cs);
+				if(!dscrn)
+					goto Enodrawscreen;
+				scrn = dscrn->screen;
+				if(repl || chan!=scrn->image->chan){
+					err = "image parameters incompatibile with screen";
+					goto error;
+				}
+				reffn = 0;
+				switch(refresh){
+				case Refbackup:
+					break;
+				case Refnone:
+					reffn = memlnorefresh;
+					break;
+				case Refmesg:
+					reffn = drawrefresh;
+					break;
+				default:
+					err = "unknown refresh method";
+					goto error;
+				}
+				l = memlalloc(scrn, r, reffn, 0, value);
+				if(l == 0)
+					goto Edrawmem;
+				addflush(l->layer->screenr);
+				l->clipr = clipr;
+				rectclip(&l->clipr, r);
+				if(drawinstall(client, dstid, l, dscrn) == 0){
+					memldelete(l);
+					goto Edrawmem;
+				}
+				dscrn->ref++;
+				if(reffn){
+					refx = nil;
+					if(reffn == drawrefresh){
+						refx = mallocz(sizeof(Refx), 1);
+						if(refx == 0){
+							if(drawuninstall(client, dstid) < 0)
+								goto Enodrawimage;
+							goto Edrawmem;
+						}
+						refx->client = client;
+						refx->dimage = drawlookup(client, dstid, 1);
+					}
+					memlsetrefresh(l, reffn, refx);
+				}
+				continue;
+			}
+			i = allocmemimage(r, chan);
+			if(i == 0)
+				goto Edrawmem;
+			if(repl)
+				i->flags |= Frepl;
+			i->clipr = clipr;
+			if(!repl)
+				rectclip(&i->clipr, r);
+			if(drawinstall(client, dstid, i, 0) == 0){
+				freememimage(i);
+				goto Edrawmem;
+			}
+			memfillcolor(i, value);
+			continue;
+
+		/* allocate screen: 'A' id[4] imageid[4] fillid[4] public[1] */
+		case 'A':
+			m = 1+4+4+4+1;
+			if(n < m)
+				goto Eshortdraw;
+			dstid = BGLONG(a+1);
+			if(dstid == 0)
+				goto Ebadarg;
+			if(drawlookupdscreen(dstid))
+				goto Escreenexists;
+			ddst = drawlookup(client, BGLONG(a+5), 1);
+			dsrc = drawlookup(client, BGLONG(a+9), 1);
+			if(ddst==0 || dsrc==0)
+				goto Enodrawimage;
+			if(drawinstallscreen(client, 0, dstid, ddst, dsrc, a[13]) == 0)
+				goto Edrawmem;
+			continue;
+
+		/* set repl and clip: 'c' dstid[4] repl[1] clipR[4*4] */
+		case 'c':
+			m = 1+4+1+4*4;
+			if(n < m)
+				goto Eshortdraw;
+			ddst = drawlookup(client, BGLONG(a+1), 1);
+			if(ddst == nil)
+				goto Enodrawimage;
+			if(ddst->name){
+				err = "can't change repl/clipr of shared image";
+				goto error;
+			}
+			dst = ddst->image;
+			if(a[5])
+				dst->flags |= Frepl;
+			drawrectangle(&dst->clipr, a+6);
+			continue;
+
+		/* draw: 'd' dstid[4] srcid[4] maskid[4] R[4*4] P[2*4] P[2*4] */
+		case 'd':
+			m = 1+4+4+4+4*4+2*4+2*4;
+			if(n < m)
+				goto Eshortdraw;
+			dst = drawimage(client, a+1);
+			dstid = BGLONG(a+1);
+			src = drawimage(client, a+5);
+			mask = drawimage(client, a+9);
+			if(!dst || !src || !mask)
+				goto Enodrawimage;
+			drawrectangle(&r, a+13);
+			drawpoint(&p, a+29);
+			drawpoint(&q, a+37);
+			op = drawclientop(client);
+			memdraw(dst, r, src, p, mask, q, op);
+			dstflush(dstid, dst, r);
+			continue;
+
+		/* toggle debugging: 'D' val[1] */
+		case 'D':
+			m = 1+1;
+			if(n < m)
+				goto Eshortdraw;
+			drawdebug = a[1];
+			continue;
+
+		/* ellipse: 'e' dstid[4] srcid[4] center[2*4] a[4] b[4] thick[4] sp[2*4] alpha[4] phi[4]*/
+		case 'e':
+		case 'E':
+			m = 1+4+4+2*4+4+4+4+2*4+2*4;
+			if(n < m)
+				goto Eshortdraw;
+			dst = drawimage(client, a+1);
+			dstid = BGLONG(a+1);
+			src = drawimage(client, a+5);
+			if(!dst || !src)
+				goto Enodrawimage;
+			drawpoint(&p, a+9);
+			e0 = BGLONG(a+17);
+			e1 = BGLONG(a+21);
+			if(e0<0 || e1<0){
+				err = "invalid ellipse semidiameter";
+				goto error;
+			}
+			j = BGLONG(a+25);
+			if(j < 0){
+				err = "negative ellipse thickness";
+				goto error;
+			}
+			
+			drawpoint(&sp, a+29);
+			c = j;
+			if(*a == 'E')
+				c = -1;
+			ox = BGLONG(a+37);
+			oy = BGLONG(a+41);
+			op = drawclientop(client);
+			/* high bit indicates arc angles are present */
+			if(ox & ((ulong)1<<31)){
+				if((ox & ((ulong)1<<30)) == 0)
+					ox &= ~((ulong)1<<31);
+				memarc(dst, p, e0, e1, c, src, sp, ox, oy, op);
+			}else
+				memellipse(dst, p, e0, e1, c, src, sp, op);
+			dstflush(dstid, dst, Rect(p.x-e0-j, p.y-e1-j, p.x+e0+j+1, p.y+e1+j+1));
+			continue;
+
+		/* free: 'f' id[4] */
+		case 'f':
+			m = 1+4;
+			if(n < m)
+				goto Eshortdraw;
+			ll = drawlookup(client, BGLONG(a+1), 0);
+			if(ll && ll->dscreen && ll->dscreen->owner != client)
+				ll->dscreen->owner->refreshme = 1;
+			if(drawuninstall(client, BGLONG(a+1)) < 0)
+				goto Enodrawimage;
+			continue;
+
+		/* free screen: 'F' id[4] */
+		case 'F':
+			m = 1+4;
+			if(n < m)
+				goto Eshortdraw;
+			if(!drawlookupscreen(client, BGLONG(a+1), &cs))
+				goto Enodrawscreen;
+			drawuninstallscreen(client, cs);
+			continue;
+
+		/* initialize font: 'i' fontid[4] nchars[4] ascent[1] */
+		case 'i':
+			m = 1+4+4+1;
+			if(n < m)
+				goto Eshortdraw;
+			dstid = BGLONG(a+1);
+			if(dstid == 0){
+				err = "can't use display as font";
+				goto error;
+			}
+			font = drawlookup(client, dstid, 1);
+			if(font == 0)
+				goto Enodrawimage;
+			if(font->image->layer){
+				err = "can't use window as font";
+				goto error;
+			}
+			ni = BGLONG(a+5);
+			if(ni<=0 || ni>4096){
+				err = "bad font size (4096 chars max)";
+				goto error;
+			}
+			free(font->fchar);	/* should we complain if non-zero? */
+			font->fchar = mallocz(ni*sizeof(FChar), 1);
+			if(font->fchar == 0){
+				err = "no memory for font";
+				goto error;
+			}
+			memset(font->fchar, 0, ni*sizeof(FChar));
+			font->nfchar = ni;
+			font->ascent = a[9];
+			continue;
+
+		/* set image 0 to screen image */
+		case 'J':
+			m = 1;
+			if(n < m)
+				goto Eshortdraw;
+			if(drawlookup(client, 0, 0))
+				goto Eimageexists;
+			drawinstall(client, 0, screenimage, 0);
+			client->infoid = 0;
+			continue;
+
+		/* get image info: 'I' */
+		case 'I':
+			m = 1;
+			if(n < m)
+				goto Eshortdraw;
+			if(client->infoid < 0)
+				goto Enodrawimage;
+			if(client->infoid == 0){
+				i = screenimage;
+				if(i == nil)
+					goto Enodrawimage;
+			}else{
+				di = drawlookup(client, client->infoid, 1);
+				if(di == nil)
+					goto Enodrawimage;
+				i = di->image;
+			}
+			ni = sprint(ibuf, "%11d %11d %11s %11d %11d %11d %11d %11d"
+					" %11d %11d %11d %11d ",
+					client->clientid,
+					client->infoid,	
+					chantostr(cbuf, i->chan),
+					(i->flags&Frepl)==Frepl,
+					i->r.min.x, i->r.min.y, i->r.max.x, i->r.max.y,
+					i->clipr.min.x, i->clipr.min.y, 
+					i->clipr.max.x, i->clipr.max.y);
+			free(client->readdata);
+			client->readdata = malloc(ni);
+			if(client->readdata == nil)
+				goto Enomem;
+			memmove(client->readdata, ibuf, ni);
+			client->nreaddata = ni;
+			client->infoid = -1;
+			continue;	
+
+		/* load character: 'l' fontid[4] srcid[4] index[2] R[4*4] P[2*4] left[1] width[1] */
+		case 'l':
+			m = 1+4+4+2+4*4+2*4+1+1;
+			if(n < m)
+				goto Eshortdraw;
+			font = drawlookup(client, BGLONG(a+1), 1);
+			if(font == 0)
+				goto Enodrawimage;
+			if(font->nfchar == 0)
+				goto Enotfont;
+			src = drawimage(client, a+5);
+			if(!src)
+				goto Enodrawimage;
+			ci = BGSHORT(a+9);
+			if(ci >= font->nfchar)
+				goto Eindex;
+			drawrectangle(&r, a+11);
+			drawpoint(&p, a+27);
+			memdraw(font->image, r, src, p, memopaque, p, S);
+			fc = &font->fchar[ci];
+			fc->minx = r.min.x;
+			fc->maxx = r.max.x;
+			fc->miny = r.min.y;
+			fc->maxy = r.max.y;
+			fc->left = a[35];
+			fc->width = a[36];
+			continue;
+
+		/* draw line: 'L' dstid[4] p0[2*4] p1[2*4] end0[4] end1[4] radius[4] srcid[4] sp[2*4] */
+		case 'L':
+			m = 1+4+2*4+2*4+4+4+4+4+2*4;
+			if(n < m)
+				goto Eshortdraw;
+			dst = drawimage(client, a+1);
+			dstid = BGLONG(a+1);
+			drawpoint(&p, a+5);
+			drawpoint(&q, a+13);
+			e0 = BGLONG(a+21);
+			e1 = BGLONG(a+25);
+			j = BGLONG(a+29);
+			if(j < 0){
+				err = "negative line width";
+				goto error;
+			}
+			src = drawimage(client, a+33);
+			if(!dst || !src)
+				goto Enodrawimage;
+			drawpoint(&sp, a+37);
+			op = drawclientop(client);
+			memline(dst, p, q, e0, e1, j, src, sp, op);
+			/* avoid memlinebbox if possible */
+			if(dstid==0 || dst->layer!=nil){
+				/* BUG: this is terribly inefficient: update maximal containing rect*/
+				r = memlinebbox(p, q, e0, e1, j);
+				dstflush(dstid, dst, insetrect(r, -(1+1+j)));
+			}
+			continue;
+
+		/* create image mask: 'm' newid[4] id[4] */
+/*
+ *
+		case 'm':
+			m = 4+4;
+			if(n < m)
+				goto Eshortdraw;
+			break;
+ *
+ */
+
+		/* attach to a named image: 'n' dstid[4] j[1] name[j] */
+		case 'n':
+			m = 1+4+1;
+			if(n < m)
+				goto Eshortdraw;
+			j = a[5];
+			if(j == 0)	/* give me a non-empty name please */
+				goto Eshortdraw;
+			m += j;
+			if(n < m)
+				goto Eshortdraw;
+			dstid = BGLONG(a+1);
+			if(drawlookup(client, dstid, 0))
+				goto Eimageexists;
+			dn = drawlookupname(j, (char*)a+6);
+			if(dn == nil)
+				goto Enoname;
+			s = malloc(j+1);
+			if(s == nil)
+				goto Enomem;
+			if(drawinstall(client, dstid, dn->dimage->image, 0) == 0)
+				goto Edrawmem;
+			di = drawlookup(client, dstid, 0);
+			if(di == 0)
+				goto Eoldname;
+			di->vers = dn->vers;
+			di->name = s;
+			di->fromname = dn->dimage;
+			di->fromname->ref++;
+			memmove(di->name, a+6, j);
+			di->name[j] = 0;
+			client->infoid = dstid;
+			continue;
+
+		/* name an image: 'N' dstid[4] in[1] j[1] name[j] */
+		case 'N':
+			m = 1+4+1+1;
+			if(n < m)
+				goto Eshortdraw;
+			c = a[5];
+			j = a[6];
+			if(j == 0)	/* give me a non-empty name please */
+				goto Eshortdraw;
+			m += j;
+			if(n < m)
+				goto Eshortdraw;
+			di = drawlookup(client, BGLONG(a+1), 0);
+			if(di == 0)
+				goto Enodrawimage;
+			if(di->name)
+				goto Enamed;
+			if(c)
+				if(drawaddname(client, di, j, (char*)a+7, &err) < 0)
+					goto error;
+			else{
+				dn = drawlookupname(j, (char*)a+7);
+				if(dn == nil)
+					goto Enoname;
+				if(dn->dimage != di)
+					goto Ewrongname;
+				drawdelname(dn);
+			}
+			continue;
+
+		/* position window: 'o' id[4] r.min [2*4] screenr.min [2*4] */
+		case 'o':
+			m = 1+4+2*4+2*4;
+			if(n < m)
+				goto Eshortdraw;
+			dst = drawimage(client, a+1);
+			if(!dst)
+				goto Enodrawimage;
+			if(dst->layer){
+				drawpoint(&p, a+5);
+				drawpoint(&q, a+13);
+				r = dst->layer->screenr;
+				ni = memlorigin(dst, p, q);
+				if(ni < 0){
+					err = "image origin failed";
+					goto error;
+				}
+				if(ni > 0){
+					addflush(r);
+					addflush(dst->layer->screenr);
+					ll = drawlookup(client, BGLONG(a+1), 1);
+					drawrefreshscreen(ll, client);
+				}
+			}
+			continue;
+
+		/* set compositing operator for next draw operation: 'O' op */
+		case 'O':
+			m = 1+1;
+			if(n < m)
+				goto Eshortdraw;
+			client->op = a[1];
+			continue;
+
+		/* filled polygon: 'P' dstid[4] n[2] wind[4] ignore[2*4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
+		/* polygon: 'p' dstid[4] n[2] end0[4] end1[4] radius[4] srcid[4] sp[2*4] p0[2*4] dp[2*2*n] */
+		case 'p':
+		case 'P':
+			m = 1+4+2+4+4+4+4+2*4;
+			if(n < m)
+				goto Eshortdraw;
+			dstid = BGLONG(a+1);
+			dst = drawimage(client, a+1);
+			ni = BGSHORT(a+5);
+			if(ni < 0){
+				err = "negative cout in polygon";
+				goto error;
+			}
+			e0 = BGLONG(a+7);
+			e1 = BGLONG(a+11);
+			j = 0;
+			if(*a == 'p'){
+				j = BGLONG(a+15);
+				if(j < 0){
+					err = "negative polygon line width";
+					goto error;
+				}
+			}
+			src = drawimage(client, a+19);
+			if(!dst || !src)
+				goto Enodrawimage;
+			drawpoint(&sp, a+23);
+			drawpoint(&p, a+31);
+			ni++;
+			pp = mallocz(ni*sizeof(Point), 1);
+			if(pp == nil)
+				goto Enomem;
+			doflush = 0;
+			if(dstid==0 || (dst->layer && dst->layer->screen->image->data == screenimage->data))
+				doflush = 1;	/* simplify test in loop */
+			ox = oy = 0;
+			esize = 0;
+			u = a+m;
+			for(y=0; y<ni; y++){
+				q = p;
+				oesize = esize;
+				u = drawcoord(u, a+n, ox, &p.x);
+				if(!u)
+					goto Eshortdraw;
+				u = drawcoord(u, a+n, oy, &p.y);
+				if(!u)
+					goto Eshortdraw;
+				ox = p.x;
+				oy = p.y;
+				if(doflush){
+					esize = j;
+					if(*a == 'p'){
+						if(y == 0){
+							c = memlineendsize(e0);
+							if(c > esize)
+								esize = c;
+						}
+						if(y == ni-1){
+							c = memlineendsize(e1);
+							if(c > esize)
+								esize = c;
+						}
+					}
+					if(*a=='P' && e0!=1 && e0 !=~0)
+						r = dst->clipr;
+					else if(y > 0){
+						r = Rect(q.x-oesize, q.y-oesize, q.x+oesize+1, q.y+oesize+1);
+						combinerect(&r, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
+					}
+					if(rectclip(&r, dst->clipr))		/* should perhaps be an arg to dstflush */
+						dstflush(dstid, dst, r);
+				}
+				pp[y] = p;
+			}
+			if(y == 1)
+				dstflush(dstid, dst, Rect(p.x-esize, p.y-esize, p.x+esize+1, p.y+esize+1));
+			op = drawclientop(client);
+			if(*a == 'p')
+				mempoly(dst, pp, ni, e0, e1, j, src, sp, op);
+			else
+				memfillpoly(dst, pp, ni, e0, src, sp, op);
+			free(pp);
+			m = u-a;
+			continue;
+
+		/* read: 'r' id[4] R[4*4] */
+		case 'r':
+			m = 1+4+4*4;
+			if(n < m)
+				goto Eshortdraw;
+			i = drawimage(client, a+1);
+			if(!i)
+				goto Enodrawimage;
+			drawrectangle(&r, a+5);
+			if(!rectinrect(r, i->r))
+				goto Ereadoutside;
+			c = bytesperline(r, i->depth);
+			c *= Dy(r);
+			free(client->readdata);
+			client->readdata = mallocz(c, 0);
+			if(client->readdata == nil){
+				err = "readimage malloc failed";
+				goto error;
+			}
+			client->nreaddata = memunload(i, r, client->readdata, c);
+			if(client->nreaddata < 0){
+				free(client->readdata);
+				client->readdata = nil;
+				err = "bad readimage call";
+				goto error;
+			}
+			continue;
+
+		/* string: 's' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] ni*(index[2]) */
+		/* stringbg: 'x' dstid[4] srcid[4] fontid[4] P[2*4] clipr[4*4] sp[2*4] ni[2] bgid[4] bgpt[2*4] ni*(index[2]) */
+		case 's':
+		case 'x':
+			m = 1+4+4+4+2*4+4*4+2*4+2;
+			if(*a == 'x')
+				m += 4+2*4;
+			if(n < m)
+				goto Eshortdraw;
+
+			dst = drawimage(client, a+1);
+			dstid = BGLONG(a+1);
+			src = drawimage(client, a+5);
+			if(!dst || !src)
+				goto Enodrawimage;
+			font = drawlookup(client, BGLONG(a+9), 1);
+			if(font == 0)
+				goto Enodrawimage;
+			if(font->nfchar == 0)
+				goto Enotfont;
+			drawpoint(&p, a+13);
+			drawrectangle(&r, a+21);
+			drawpoint(&sp, a+37);
+			ni = BGSHORT(a+45);
+			u = a+m;
+			m += ni*2;
+			if(n < m)
+				goto Eshortdraw;
+			clipr = dst->clipr;
+			dst->clipr = r;
+			op = drawclientop(client);
+			if(*a == 'x'){
+				/* paint background */
+				l = drawimage(client, a+47);
+				if(!l)
+					goto Enodrawimage;
+				drawpoint(&q, a+51);
+				r.min.x = p.x;
+				r.min.y = p.y-font->ascent;
+				r.max.x = p.x;
+				r.max.y = r.min.y+Dy(font->image->r);
+				j = ni;
+				while(--j >= 0){
+					ci = BGSHORT(u);
+					if(ci<0 || ci>=font->nfchar){
+						dst->clipr = clipr;
+						goto Eindex;
+					}
+					r.max.x += font->fchar[ci].width;
+					u += 2;
+				}
+				memdraw(dst, r, l, q, memopaque, ZP, op);
+				u -= 2*ni;
+			}
+			q = p;
+			while(--ni >= 0){
+				ci = BGSHORT(u);
+				if(ci<0 || ci>=font->nfchar){
+					dst->clipr = clipr;
+					goto Eindex;
+				}
+				q = drawchar(dst, q, src, &sp, font, ci, op);
+				u += 2;
+			}
+			dst->clipr = clipr;
+			p.y -= font->ascent;
+			dstflush(dstid, dst, Rect(p.x, p.y, q.x, p.y+Dy(font->image->r)));
+			continue;
+
+		/* use public screen: 'S' id[4] chan[4] */
+		case 'S':
+			m = 1+4+4;
+			if(n < m)
+				goto Eshortdraw;
+			dstid = BGLONG(a+1);
+			if(dstid == 0)
+				goto Ebadarg;
+			dscrn = drawlookupdscreen(dstid);
+			if(dscrn==0 || (dscrn->public==0 && dscrn->owner!=client))
+				goto Enodrawscreen;
+			if(dscrn->screen->image->chan != BGLONG(a+5)){
+				err = "inconsistent chan";
+				goto error;
+			}
+			if(drawinstallscreen(client, dscrn, 0, 0, 0, 0) == 0)
+				goto Edrawmem;
+			continue;
+
+		/* top or bottom windows: 't' top[1] nw[2] n*id[4] */
+		case 't':
+			m = 1+1+2;
+			if(n < m)
+				goto Eshortdraw;
+			nw = BGSHORT(a+2);
+			if(nw < 0)
+				goto Ebadarg;
+			if(nw == 0)
+				continue;
+			m += nw*4;
+			if(n < m)
+				goto Eshortdraw;
+			lp = mallocz(nw*sizeof(Memimage*), 1);
+			if(lp == 0)
+				goto Enomem;
+			for(j=0; j<nw; j++){
+				lp[j] = drawimage(client, a+1+1+2+j*4);
+				if(lp[j] == nil){
+					free(lp);
+					goto Enodrawimage;
+				}
+			}
+			if(lp[0]->layer == 0){
+				err = "images are not windows";
+				free(lp);
+				goto error;
+			}
+			for(j=1; j<nw; j++)
+				if(lp[j]->layer->screen != lp[0]->layer->screen){
+					err = "images not on same screen";
+					free(lp);
+					goto error;
+				}
+			if(a[1])
+				memltofrontn(lp, nw);
+			else
+				memltorearn(lp, nw);
+			if(lp[0]->layer->screen->image->data == screenimage->data)
+				for(j=0; j<nw; j++)
+					addflush(lp[j]->layer->screenr);
+			free(lp);
+			ll = drawlookup(client, BGLONG(a+1+1+2), 1);
+			drawrefreshscreen(ll, client);
+			continue;
+
+		/* visible: 'v' */
+		case 'v':
+			m = 1;
+			drawflush();
+			continue;
+
+		/* write: 'y' id[4] R[4*4] data[x*1] */
+		/* write from compressed data: 'Y' id[4] R[4*4] data[x*1] */
+		case 'y':
+		case 'Y':
+			m = 1+4+4*4;
+			if(n < m)
+				goto Eshortdraw;
+			dstid = BGLONG(a+1);
+			dst = drawimage(client, a+1);
+			if(!dst)
+				goto Enodrawimage;
+			drawrectangle(&r, a+5);
+			if(!rectinrect(r, dst->r))
+				goto Ewriteoutside;
+			y = memload(dst, r, a+m, n-m, *a=='Y');
+			if(y < 0){
+				err = "bad writeimage call";
+				goto error;
+			}
+			dstflush(dstid, dst, r);
+			m += y;
+			continue;
+		}
+	}
+	qunlock(&sdraw.lk);
+	return oldn - n;
+
+Enodrawimage:
+	err = "unknown id for draw image";
+	goto error;
+Enodrawscreen:
+	err = "unknown id for draw screen";
+	goto error;
+Eshortdraw:
+	err = "short draw message";
+	goto error;
+/*
+Eshortread:
+	err = "draw read too short";
+	goto error;
+*/
+Eimageexists:
+	err = "image id in use";
+	goto error;
+Escreenexists:
+	err = "screen id in use";
+	goto error;
+Edrawmem:
+	err = "image memory allocation failed";
+	goto error;
+Ereadoutside:
+	err = "readimage outside image";
+	goto error;
+Ewriteoutside:
+	err = "writeimage outside image";
+	goto error;
+Enotfont:
+	err = "image not a font";
+	goto error;
+Eindex:
+	err = "character index out of range";
+	goto error;
+/*
+Enoclient:
+	err = "no such draw client";
+	goto error;
+Edepth:
+	err = "image has bad depth";
+	goto error;
+Enameused:
+	err = "image name in use";
+	goto error;
+*/
+Enoname:
+	err = "no image with that name";
+	goto error;
+Eoldname:
+	err = "named image no longer valid";
+	goto error;
+Enamed:
+	err = "image already has name";
+	goto error;
+Ewrongname:
+	err = "wrong name for image";
+	goto error;
+Enomem:
+	err = "out of memory";
+	goto error;
+Ebadarg:
+	err = "bad argument in draw message";
+	goto error;
+
+error:
+	werrstr("%s", err);
+	qunlock(&sdraw.lk);
+	return -1;
+}
+
+
blob - /dev/null
blob + 533fa8438f0367336f277c1d553109ddf29dbffd (mode 644)
--- /dev/null
+++ src/cmd/devdraw/devdraw.h
@@ -0,0 +1,4 @@
+int _drawmsgread(void*, int);
+int _drawmsgwrite(void*, int);
+void _initdisplaymemimage(Memimage*);
+int _latin1(Rune*, int);
blob - /dev/null
blob + 87df3f18c9512bba360673087f384f7f71786923 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/drawclient.c
@@ -0,0 +1,128 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <draw.h>
+#include <mouse.h>
+#include <cursor.h>
+#include <drawsrv.h>
+
+typedef struct Cmd Cmd;
+struct Cmd {
+	char *cmd;
+	void	(*fn)(int, char**);
+};
+
+Biobuf b;
+int fd;
+uchar buf[64*1024];
+
+void
+startsrv(void)
+{
+	int pid, p[2];
+	
+	if(pipe(p) < 0)
+		sysfatal("pipe");
+	if((pid=fork()) < 0)
+		sysfatal("fork");
+	if(pid == 0){
+		close(p[0]);
+		dup(p[1], 0);
+		dup(p[1], 1);
+		execl("o.drawsrv", "o.drawsrv", "-D", nil);
+		sysfatal("exec: %r");
+	}
+	close(p[1]);
+	fd = p[0];
+}
+
+int
+domsg(Wsysmsg *m)
+{
+	int n, nn;
+
+	n = convW2M(m, buf, sizeof buf);
+fprint(2, "write %d to %d\n", n, fd);
+	write(fd, buf, n);
+	n = readwsysmsg(fd, buf, sizeof buf);
+	nn = convM2W(buf, n, m);
+	assert(nn == n);
+	if(m->op == Rerror)
+		return -1;
+	return 0;
+}
+
+void
+cmdinit(int argc, char **argv)
+{
+	Wsysmsg m;
+	
+	memset(&m, 0, sizeof m);
+	m.op = Tinit;
+	m.winsize = "100x100";
+	m.label = "label";
+	m.font = "";
+	if(domsg(&m) < 0)
+		sysfatal("domsg");
+}
+
+void
+cmdmouse(int argc, char **argv)
+{
+	Wsysmsg m;
+	
+	memset(&m, 0, sizeof m);
+	m.op = Trdmouse;
+	if(domsg(&m) < 0)
+		sysfatal("domsg");
+	print("%c %d %d %d\n",
+		m.resized ? 'r' : 'm',
+		m.mouse.xy.x,
+		m.mouse.xy.y,
+		m.mouse.buttons);
+}
+
+void
+cmdkbd(int argc, char **argv)
+{
+	Wsysmsg m;
+	
+	memset(&m, 0, sizeof m);
+	m.op = Trdkbd;
+	if(domsg(&m) < 0)
+		sysfatal("domsg");
+	print("%s\n", m.runes);
+}
+
+Cmd cmdtab[] = {
+	{ "init", cmdinit, },
+	{ "mouse", cmdmouse, },
+	{ "kbd", cmdkbd, },
+};
+
+void
+main(int argc, char **argv)
+{
+	char *p, *f[20];
+	int i, nf;
+
+	startsrv();
+
+fprint(2, "started...\n");
+	Binit(&b, 0, OREAD);
+	while((p = Brdstr(&b, '\n', 1)) != nil){
+fprint(2, "%s...\n", p);
+		nf = tokenize(p, f, nelem(f));
+		for(i=0; i<nelem(cmdtab); i++){
+			if(strcmp(cmdtab[i].cmd, f[0]) == 0){
+				cmdtab[i].fn(nf, f);
+				break;
+			}
+		}
+		if(i == nelem(cmdtab))
+			print("! unrecognized command %s\n", f[0]);
+		free(p);
+	}
+	exits(0);
+}
+
blob - /dev/null
blob + 82892460d5360b3bd108b0f545d0b6c9c666bc1d (mode 644)
--- /dev/null
+++ src/cmd/devdraw/latin1.c
@@ -0,0 +1,179 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+
+/*
+ * The code makes two assumptions: strlen(ld) is 1 or 2; latintab[i].ld can be a
+ * prefix of latintab[j].ld only when j<i.
+ */
+static struct cvlist
+{
+	char	*ld;		/* must be seen before using this conversion */
+	char	*si;		/* options for last input characters */
+	Rune	so[60];		/* the corresponding Rune for each si entry */
+} latintab[] = {
+	" ", " i",	{ 0x2423, 0x0131 },
+	"!~", "-=~",	{ 0x2244, 0x2247, 0x2249 },
+	"!", "!<=>?bmp",	{ 0x00a1, 0x226e, 0x2260, 0x226f, 0x203d, 0x2284, 0x2209, 0x2285 },
+	"\"*", "IUiu",	{ 0x03aa, 0x03ab, 0x03ca, 0x03cb },
+	"\"", "\"AEIOUYaeiouy",	{ 0x00a8, 0x00c4, 0x00cb, 0x00cf, 0x00d6, 0x00dc, 0x0178, 0x00e4, 0x00eb, 0x00ef, 0x00f6, 0x00fc, 0x00ff },
+	"$*", "fhk",	{ 0x03d5, 0x03d1, 0x03f0 },
+	"$", "BEFHILMRVaefglopv",	{ 0x212c, 0x2130, 0x2131, 0x210b, 0x2110, 0x2112, 0x2133, 0x211b, 0x01b2, 0x0251, 0x212f, 0x0192, 0x210a, 0x2113, 0x2134, 0x2118, 0x028b },
+	"\'\"", "Uu",	{ 0x01d7, 0x01d8 },
+	"\'", "\'ACEILNORSUYZacegilnorsuyz",	{ 0x00b4, 0x00c1, 0x0106, 0x00c9, 0x00cd, 0x0139, 0x0143, 0x00d3, 0x0154, 0x015a, 0x00da, 0x00dd, 0x0179, 0x00e1, 0x0107, 0x00e9, 0x0123, 0x00ed, 0x013a, 0x0144, 0x00f3, 0x0155, 0x015b, 0x00fa, 0x00fd, 0x017a },
+	"*", "*ABCDEFGHIKLMNOPQRSTUWXYZabcdefghiklmnopqrstuwxyz",	{ 0x2217, 0x0391, 0x0392, 0x039e, 0x0394, 0x0395, 0x03a6, 0x0393, 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039f, 0x03a0, 0x03a8, 0x03a1, 0x03a3, 0x03a4, 0x03a5, 0x03a9, 0x03a7, 0x0397, 0x0396, 0x03b1, 0x03b2, 0x03be, 0x03b4, 0x03b5, 0x03c6, 0x03b3, 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03bf, 0x03c0, 0x03c8, 0x03c1, 0x03c3, 0x03c4, 0x03c5, 0x03c9, 0x03c7, 0x03b7, 0x03b6 },
+	"+", "-O",	{ 0x00b1, 0x2295 },
+	",", ",ACEGIKLNORSTUacegiklnorstu",	{ 0x00b8, 0x0104, 0x00c7, 0x0118, 0x0122, 0x012e, 0x0136, 0x013b, 0x0145, 0x01ea, 0x0156, 0x015e, 0x0162, 0x0172, 0x0105, 0x00e7, 0x0119, 0x0123, 0x012f, 0x0137, 0x013c, 0x0146, 0x01eb, 0x0157, 0x015f, 0x0163, 0x0173 },
+	"-*", "l",	{ 0x019b },
+	"-", "+-2:>DGHILOTZbdghiltuz~",	{ 0x2213, 0x00ad, 0x01bb, 0x00f7, 0x2192, 0x00d0, 0x01e4, 0x0126, 0x0197, 0x0141, 0x2296, 0x0166, 0x01b5, 0x0180, 0x00f0, 0x01e5, 0x210f, 0x0268, 0x0142, 0x0167, 0x0289, 0x01b6, 0x2242 },
+	".", ".CEGILOZceglz",	{ 0x00b7, 0x010a, 0x0116, 0x0120, 0x0130, 0x013f, 0x2299, 0x017b, 0x010b, 0x0117, 0x0121, 0x0140, 0x017c },
+	"/", "Oo",	{ 0x00d8, 0x00f8 },
+	"1", "234568",	{ 0x00bd, 0x2153, 0x00bc, 0x2155, 0x2159, 0x215b },
+	"2", "-35",	{ 0x01bb, 0x2154, 0x2156 },
+	"3", "458",	{ 0x00be, 0x2157, 0x215c },
+	"4", "5",	{ 0x2158 },
+	"5", "68",	{ 0x215a, 0x215d },
+	"7", "8",	{ 0x215e },
+	":", "()-=",	{ 0x2639, 0x263a, 0x00f7, 0x2254 },
+	"<!", "=~",	{ 0x2268, 0x22e6 },
+	"<", "-<=>~",	{ 0x2190, 0x00ab, 0x2264, 0x2276, 0x2272 },
+	"=", ":<=>OV",	{ 0x2255, 0x22dc, 0x2261, 0x22dd, 0x229c, 0x21d2 },
+	">!", "=~",	{ 0x2269, 0x22e7 },
+	">", "<=>~",	{ 0x2277, 0x2265, 0x00bb, 0x2273 },
+	"?", "!?",	{ 0x203d, 0x00bf },
+	"@\'", "\'",	{ 0x044a },
+	"@@", "\'EKSTYZekstyz",	{ 0x044c, 0x0415, 0x041a, 0x0421, 0x0422, 0x042b, 0x0417, 0x0435, 0x043a, 0x0441, 0x0442, 0x044b, 0x0437 },
+	"@C", "Hh",	{ 0x0427, 0x0427 },
+	"@E", "Hh",	{ 0x042d, 0x042d },
+	"@K", "Hh",	{ 0x0425, 0x0425 },
+	"@S", "CHch",	{ 0x0429, 0x0428, 0x0429, 0x0428 },
+	"@T", "Ss",	{ 0x0426, 0x0426 },
+	"@Y", "AEOUaeou",	{ 0x042f, 0x0415, 0x0401, 0x042e, 0x042f, 0x0415, 0x0401, 0x042e },
+	"@Z", "Hh",	{ 0x0416, 0x0416 },
+	"@c", "h",	{ 0x0447 },
+	"@e", "h",	{ 0x044d },
+	"@k", "h",	{ 0x0445 },
+	"@s", "ch",	{ 0x0449, 0x0448 },
+	"@t", "s",	{ 0x0446 },
+	"@y", "aeou",	{ 0x044f, 0x0435, 0x0451, 0x044e },
+	"@z", "h",	{ 0x0436 },
+	"@", "ABDFGIJLMNOPRUVXabdfgijlmnopruvx",	{ 0x0410, 0x0411, 0x0414, 0x0424, 0x0413, 0x0418, 0x0419, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f, 0x0420, 0x0423, 0x0412, 0x0425, 0x0430, 0x0431, 0x0434, 0x0444, 0x0433, 0x0438, 0x0439, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f, 0x0440, 0x0443, 0x0432, 0x0445 },
+	"A", "E",	{ 0x00c6 },
+	"C", "ACU",	{ 0x22c2, 0x2102, 0x22c3 },
+	"Dv", "Zz",	{ 0x01c4, 0x01c5 },
+	"D", "-e",	{ 0x00d0, 0x2206 },
+	"G", "-",	{ 0x01e4 },
+	"H", "-H",	{ 0x0126, 0x210d },
+	"I", "-J",	{ 0x0197, 0x0132 },
+	"L", "&-Jj|",	{ 0x22c0, 0x0141, 0x01c7, 0x01c8, 0x22c1 },
+	"M", "#48bs",	{ 0x266e, 0x2669, 0x266a, 0x266d, 0x266f },
+	"N", "JNj",	{ 0x01ca, 0x2115, 0x01cb },
+	"O", "*+-./=EIcoprx",	{ 0x229b, 0x2295, 0x2296, 0x2299, 0x2298, 0x229c, 0x0152, 0x01a2, 0x00a9, 0x229a, 0x2117, 0x00ae, 0x2297 },
+	"P", "P",	{ 0x2119 },
+	"Q", "Q",	{ 0x211a },
+	"R", "R",	{ 0x211d },
+	"S", "123S",	{ 0x00b9, 0x00b2, 0x00b3, 0x00a7 },
+	"T", "-u",	{ 0x0166, 0x22a8 },
+	"V", "=",	{ 0x21d0 },
+	"Y", "R",	{ 0x01a6 },
+	"Z", "-ACSZ",	{ 0x01b5, 0xf015, 0xf017, 0xf016, 0x2124 },
+	"^", "ACEGHIJOSUWYaceghijosuwy",	{ 0x00c2, 0x0108, 0x00ca, 0x011c, 0x0124, 0x00ce, 0x0134, 0x00d4, 0x015c, 0x00db, 0x0174, 0x0176, 0x00e2, 0x0109, 0x00ea, 0x011d, 0x0125, 0x00ee, 0x0135, 0x00f4, 0x015d, 0x00fb, 0x0175, 0x0177 },
+	"_\"", "AUau",	{ 0x01de, 0x01d5, 0x01df, 0x01d6 },
+	"_,", "Oo",	{ 0x01ec, 0x01ed },
+	"_.", "Aa",	{ 0x01e0, 0x01e1 },
+	"_", "AEIOU_aeiou",	{ 0x0100, 0x0112, 0x012a, 0x014c, 0x016a, 0x00af, 0x0101, 0x0113, 0x012b, 0x014d, 0x016b },
+	"`\"", "Uu",	{ 0x01db, 0x01dc },
+	"`", "AEIOUaeiou",	{ 0x00c0, 0x00c8, 0x00cc, 0x00d2, 0x00d9, 0x00e0, 0x00e8, 0x00ec, 0x00f2, 0x00f9 },
+	"a", "ben",	{ 0x2194, 0x00e6, 0x2220 },
+	"b", "()+-0123456789=bknpqru",	{ 0x208d, 0x208e, 0x208a, 0x208b, 0x2080, 0x2081, 0x2082, 0x2083, 0x2084, 0x2085, 0x2086, 0x2087, 0x2088, 0x2089, 0x208c, 0x265d, 0x265a, 0x265e, 0x265f, 0x265b, 0x265c, 0x2022 },
+	"c", "$Oagu",	{ 0x00a2, 0x00a9, 0x2229, 0x2245, 0x222a },
+	"dv", "z",	{ 0x01c6 },
+	"d", "-adegz",	{ 0x00f0, 0x2193, 0x2021, 0x00b0, 0x2020, 0x02a3 },
+	"e", "$lmns",	{ 0x20ac, 0x22ef, 0x2014, 0x2013, 0x2205 },
+	"f", "a",	{ 0x2200 },
+	"g", "$-r",	{ 0x00a4, 0x01e5, 0x2207 },
+	"h", "-v",	{ 0x210f, 0x0195 },
+	"i", "-bfjps",	{ 0x0268, 0x2286, 0x221e, 0x0133, 0x2287, 0x222b },
+	"l", "\"$&\'-jz|",	{ 0x201c, 0x00a3, 0x2227, 0x2018, 0x0142, 0x01c9, 0x22c4, 0x2228 },
+	"m", "iou",	{ 0x00b5, 0x2208, 0x00d7 },
+	"n", "jo",	{ 0x01cc, 0x00ac },
+	"o", "AOUaeiu",	{ 0x00c5, 0x229a, 0x016e, 0x00e5, 0x0153, 0x01a3, 0x016f },
+	"p", "Odgrt",	{ 0x2117, 0x2202, 0x00b6, 0x220f, 0x221d },
+	"r", "\"\'O",	{ 0x201d, 0x2019, 0x00ae },
+	"s", "()+-0123456789=abnoprstu",	{ 0x207d, 0x207e, 0x207a, 0x207b, 0x2070, 0x2071, 0x2072, 0x2073, 0x2074, 0x2075, 0x2076, 0x2077, 0x2078, 0x2079, 0x207c, 0x00aa, 0x2282, 0x207f, 0x00ba, 0x2283, 0x221a, 0x00df, 0x220d, 0x2211 },
+	"t", "-efmsu",	{ 0x0167, 0x2203, 0x2234, 0x2122, 0x03c2, 0x22a2 },
+	"u", "-AEGIOUaegiou",	{ 0x0289, 0x0102, 0x0114, 0x011e, 0x012c, 0x014e, 0x016c, 0x2191, 0x0115, 0x011f, 0x012d, 0x014f, 0x016d },
+	"v\"", "Uu",	{ 0x01d9, 0x01da },
+	"v", "ACDEGIKLNORSTUZacdegijklnorstuz",	{ 0x01cd, 0x010c, 0x010e, 0x011a, 0x01e6, 0x01cf, 0x01e8, 0x013d, 0x0147, 0x01d1, 0x0158, 0x0160, 0x0164, 0x01d3, 0x017d, 0x01ce, 0x010d, 0x010f, 0x011b, 0x01e7, 0x01d0, 0x01f0, 0x01e9, 0x013e, 0x0148, 0x01d2, 0x0159, 0x0161, 0x0165, 0x01d4, 0x017e },
+	"w", "bknpqr",	{ 0x2657, 0x2654, 0x2658, 0x2659, 0x2655, 0x2656 },
+	"x", "O",	{ 0x2297 },
+	"y", "$",	{ 0x00a5 },
+	"z", "-",	{ 0x01b6 },
+	"|", "Pp|",	{ 0x00de, 0x00fe, 0x00a6 },
+	"~!", "=",	{ 0x2246 },
+	"~", "-=AINOUainou~",	{ 0x2243, 0x2245, 0x00c3, 0x0128, 0x00d1, 0x00d5, 0x0168, 0x00e3, 0x0129, 0x00f1, 0x00f5, 0x0169, 0x2248 },
+	0, 0, { 0 }
+};
+
+/*
+ * Given 5 characters k[0]..k[4], find the rune or return -1 for failure.
+ */
+static long
+unicode(Rune *k)
+{
+	long i, c;
+
+	k++;	/* skip 'X' */
+	c = 0;
+	for(i=0; i<4; i++,k++){
+		c <<= 4;
+		if('0'<=*k && *k<='9')
+			c += *k-'0';
+		else if('a'<=*k && *k<='f')
+			c += 10 + *k-'a';
+		else if('A'<=*k && *k<='F')
+			c += 10 + *k-'A';
+		else
+			return -1;
+	}
+	return c;
+}
+
+/*
+ * Given n characters k[0]..k[n-1], find the corresponding rune or return -1 for
+ * failure, or something < -1 if n is too small.  In the latter case, the result
+ * is minus the required n.
+ */
+int
+_latin1(Rune *k, int n)
+{
+	struct cvlist *l;
+	int c;
+	char* p;
+
+	if(k[0] == 'X'){
+		if(n>=5)
+			return unicode(k);
+		else
+			return -5;
+	}
+	
+	for(l=latintab; l->ld!=0; l++)
+		if(k[0] == l->ld[0]){
+			if(n == 1)
+				return -2;
+			if(l->ld[1] == 0)
+				c = k[1];
+			else if(l->ld[1] != k[1])
+				continue;
+			else if(n == 2)
+				return -3;
+			else
+				c = k[2];
+			for(p=l->si; *p!=0; p++)
+				if(*p == c)
+					return l->so[p - l->si];
+			return -1;
+		}
+	return -1;
+}
blob - /dev/null
blob + 3952b869d1d19a0fba4f2c9422fefddb0e2f0cca (mode 644)
--- /dev/null
+++ src/cmd/devdraw/mkfile
@@ -0,0 +1,20 @@
+<$PLAN9/src/mkhdr
+
+TARG=devdraw
+
+WSYSOFILES=\
+	devdraw.$O\
+	latin1.$O\
+	
+<|sh ./mkwsysrules.sh
+
+OFILES=$WSYSOFILES
+
+HFILES=\
+	devdraw.h\
+
+<$PLAN9/src/mkone
+
+$O.drawclient: drawclient.$O drawfcall.$O
+	$LD -o $target $prereq
+
blob - /dev/null
blob + 665dd9dd6da8d9bb12f92e68c2336eea29666093 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/mkwsysrules.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+[ -f $PLAN9/config ] && . $PLAN9/config
+
+if [ "x$X11" = "x" ]; then 
+	if [ -d /usr/X11R6 ]; then
+		X11=/usr/X11R6
+	elif [ -d /usr/local/X11R6 ]; then
+		X11=/usr/local/X11R6
+	elif [ -d /usr/X ]; then
+		X11=/usr/X
+	elif [ -d /usr/openwin ]; then	# for Sun
+		X11=/usr/openwin
+	else
+		X11=noX11dir
+	fi
+fi
+
+if [ "x$WSYSTYPE" = "x" ]; then
+	if [ -d "$X11" ]; then
+		WSYSTYPE=x11
+	else
+		WSYSTYPE=nowsys
+	fi
+fi
+
+if [ "x$WSYSTYPE" = "xx11" -a "x$X11H" = "x" ]; then
+	if [ -d "$X11/include" ]; then
+		X11H="-I$X11/include"
+	else
+		X11H=""
+	fi
+fi
+	
+
+echo 'WSYSTYPE='$WSYSTYPE
+echo 'X11='$X11
+
+if [ $WSYSTYPE = x11 ]; then
+	echo 'CFLAGS=$CFLAGS '$X11H
+	echo 'HFILES=$HFILES $XHFILES'
+	XO=`ls x11-*.c | sed 's/\.c$/.o/'`
+	echo 'WSYSOFILES=$WSYSOFILES '$XO
+fi
+if [ $WSYSTYPE = nowsys ]; then
+	echo 'WSYSOFILES=nowsys.o'
+fi
blob - /dev/null
blob + fd8b7ee164ccfdcb52970bc3daf9cb0d93e49ca8 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/nowsys.c
@@ -0,0 +1,40 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <mouse.h>
+#include <cursor.h>
+#include <drawfcall.h>
+
+void
+usage(void)
+{
+	fprint(2, "usage: devdraw (don't run  directly)\n");
+	exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+	int n;
+	uchar buf[1024*1024];
+	Wsysmsg m;
+	
+	ARGBEGIN{
+	case 'D':
+		break;
+	default:
+		usage();
+	}ARGEND
+	
+	if(argc != 0)
+		usage();
+
+	while((n = readwsysmsg(0, buf, sizeof buf)) > 0){
+		convM2W(buf, n, &m);
+		m.type = Rerror;
+		m.error = "no window system present";
+		n = convW2M(&m, buf, sizeof buf);
+		write(1, buf, n);
+	}
+	exits(0);
+}
blob - /dev/null
blob + 5792864ffd5abc741208cfcbe361b0f17e18df3a (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-alloc.c
@@ -0,0 +1,122 @@
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include "x11-memdraw.h"
+
+/*
+ * Allocate a Memimage with an optional pixmap backing on the X server.
+ */
+Memimage*
+_xallocmemimage(Rectangle r, u32int chan, int pixmap)
+{
+	int d, offset;
+	Memimage *m;
+	Xmem *xm;
+	XImage *xi;
+
+	m = _allocmemimage(r, chan);
+	if(chan != GREY1 && chan != _x.chan)
+		return m;
+	if(_x.display == 0)
+		return m;
+
+	/*
+	 * For bootstrapping, don't bother storing 1x1 images
+	 * on the X server.  Memimageinit needs to allocate these
+	 * and we memimageinit before we do the rest of the X stuff.
+	 * Of course, 1x1 images on the server are useless anyway.
+	 */
+	if(Dx(r)==1 && Dy(r)==1)
+		return m;
+
+	xm = mallocz(sizeof(Xmem), 1);
+	if(xm == nil){
+		freememimage(m);
+		return nil;
+	}
+
+	/*
+	 * Allocate backing store.
+	 */
+	if(chan == GREY1)
+		d = 1;
+	else
+		d = _x.depth;
+	if(pixmap != PMundef)
+		xm->pixmap = pixmap;
+	else
+		xm->pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), d);
+
+	/*
+	 * We want to align pixels on word boundaries.
+	 */
+	if(m->depth == 24)
+		offset = r.min.x&3;
+	else
+		offset = r.min.x&(31/m->depth);
+	r.min.x -= offset;
+	assert(wordsperline(r, m->depth) <= m->width);
+
+	/*
+	 * Wrap our data in an XImage structure.
+	 */
+	xi = XCreateImage(_x.display, _x.vis, d,
+		ZPixmap, 0, (char*)m->data->bdata, Dx(r), Dy(r),
+		32, m->width*sizeof(u32int));
+	if(xi == nil){
+		freememimage(m);
+		if(xm->pixmap != pixmap)
+			XFreePixmap(_x.display, xm->pixmap);
+		return nil;
+	}
+
+	xm->xi = xi;
+	xm->r = r;
+
+	/*
+	 * Set the XImage parameters so that it looks exactly like
+	 * a Memimage -- we're using the same data.
+	 */
+	if(m->depth < 8 || m->depth == 24)
+		xi->bitmap_unit = 8;
+	else
+		xi->bitmap_unit = m->depth;
+	xi->byte_order = LSBFirst;
+	xi->bitmap_bit_order = MSBFirst;
+	xi->bitmap_pad = 32;
+	XInitImage(xi);
+	XFlush(_x.display);
+
+	m->X = xm;
+	return m;
+}
+
+Memimage*
+allocmemimage(Rectangle r, u32int chan)
+{
+	return _xallocmemimage(r, chan, PMundef);
+}
+
+void
+freememimage(Memimage *m)
+{
+	Xmem *xm;
+
+	if(m == nil)
+		return;
+
+	xm = m->X;
+	if(xm && m->data->ref == 1){
+		if(xm->xi){
+			xm->xi->data = nil;
+			XFree(xm->xi);
+		}
+		XFreePixmap(_x.display, xm->pixmap);
+		free(xm);
+		m->X = nil;
+	}
+	_freememimage(m);
+}
+
blob - /dev/null
blob + 33e3170a52527aa897bf1460928201a8ca5eeb98 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-cload.c
@@ -0,0 +1,18 @@
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include "x11-memdraw.h"
+
+int
+cloadmemimage(Memimage *i, Rectangle r, uchar *data, int ndata)
+{
+	int n;
+
+	n = _cloadmemimage(i, r, data, ndata);
+	if(n > 0 && i->X)
+		_xputxdata(i, r);
+	return n;
+}
+
blob - /dev/null
blob + 6c41daffd1cf889f03ce7842461391e1ba9b4ea5 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-draw.c
@@ -0,0 +1,144 @@
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include "x11-memdraw.h"
+
+static int xdraw(Memdrawparam*);
+
+/*
+ * The X acceleration doesn't fit into the standard hwaccel
+ * model because we have the extra steps of pulling the image
+ * data off the server and putting it back when we're done.
+ */
+void
+memimagedraw(Memimage *dst, Rectangle r, Memimage *src, Point sp,
+	Memimage *mask, Point mp, int op)
+{
+	Memdrawparam *par;
+
+	if((par = _memimagedrawsetup(dst, r, src, sp, mask, mp, op)) == nil)
+		return;
+
+	/* only fetch dst data if we need it */
+	if((par->state&(Simplemask|Fullmask)) != (Simplemask|Fullmask))
+		_xgetxdata(par->dst, par->r);
+
+	/* always fetch source and mask */
+	_xgetxdata(par->src, par->sr);
+	_xgetxdata(par->mask, par->mr);
+
+	/* now can run memimagedraw on the in-memory bits */
+	_memimagedraw(par);
+
+	if(xdraw(par))
+		return;
+
+	/* put bits back on x server */
+	_xputxdata(par->dst, par->r);
+}
+
+static int
+xdraw(Memdrawparam *par)
+{
+	u32int sdval;
+	uint m, state;
+	Memimage *src, *dst, *mask;
+	Point dp, mp, sp;
+	Rectangle r;
+	Xmem *xdst, *xmask, *xsrc;
+	XGC gc;
+
+	if(par->dst->X == nil)
+		return 0;
+
+	dst   = par->dst;
+	mask  = par->mask;
+	r     = par->r;
+	src   = par->src;
+	state = par->state;
+
+	/*
+	 * If we have an opaque mask and source is one opaque pixel,
+	 * we can convert to the destination format and just XFillRectangle.
+	 */
+	m = Simplesrc|Fullsrc|Simplemask|Fullmask;
+	if((state&m) == m){
+		_xfillcolor(dst, r, par->sdval);
+	/*	xdirtyxdata(dst, r); */
+		return 1;
+	}
+
+	/*
+	 * If no source alpha and an opaque mask, we can just copy
+	 * the source onto the destination.  If the channels are the
+	 * same and the source is not replicated, XCopyArea works.
+	 */
+	m = Simplemask|Fullmask;
+	if((state&(m|Replsrc))==m && src->chan==dst->chan && src->X){
+		xdst = dst->X;
+		xsrc = src->X;
+		dp = subpt(r.min,       dst->r.min);
+		sp = subpt(par->sr.min, src->r.min);
+		gc = dst->chan==GREY1 ?  _x.gccopy0 : _x.gccopy;
+
+		XCopyArea(_x.display, xsrc->pixmap, xdst->pixmap, gc,
+			sp.x, sp.y, Dx(r), Dy(r), dp.x, dp.y);
+	/*	xdirtyxdata(dst, r); */
+		return 1;
+	}
+
+	/*
+	 * If no source alpha, a 1-bit mask, and a simple source,
+	 * we can copy through the mask onto the destination.
+	 */
+	if(dst->X && mask->X && !(mask->flags&Frepl)
+	&& mask->chan==GREY1 && (state&Simplesrc)){
+		xdst = dst->X;
+		xmask = mask->X;
+		sdval = par->sdval;
+
+		dp = subpt(r.min, dst->r.min);
+		mp = subpt(r.min, subpt(par->mr.min, mask->r.min));
+
+		if(dst->chan == GREY1){
+			gc = _x.gcsimplesrc0;
+			if(_x.gcsimplesrc0color != sdval){
+				XSetForeground(_x.display, gc, sdval);
+				_x.gcsimplesrc0color = sdval;
+			}
+			if(_x.gcsimplesrc0pixmap != xmask->pixmap){
+				XSetStipple(_x.display, gc, xmask->pixmap);
+				_x.gcsimplesrc0pixmap = xmask->pixmap;
+			}
+		}else{
+			/* this doesn't work on rob's mac?  */
+			return 0;
+			/* gc = _x.gcsimplesrc;
+			if(dst->chan == CMAP8 && _x.usetable)
+				sdval = _x.tox11[sdval];
+
+			if(_x.gcsimplesrccolor != sdval){
+				XSetForeground(_x.display, gc, sdval);
+				_x.gcsimplesrccolor = sdval;
+			}
+			if(_x.gcsimplesrcpixmap != xmask->pixmap){
+				XSetStipple(_x.display, gc, xmask->pixmap);
+				_x.gcsimplesrcpixmap = xmask->pixmap;
+			}
+			*/
+		}
+		XSetTSOrigin(_x.display, gc, mp.x, mp.y);
+		XFillRectangle(_x.display, xdst->pixmap, gc, dp.x, dp.y,
+			Dx(r), Dy(r));
+	/*	xdirtyxdata(dst, r); */
+		return 1;
+	}
+
+	/*
+	 * Can't accelerate.
+	 */
+	return 0;
+}
+
blob - /dev/null
blob + fc43a684f7c42dcfb4a19643c00377221f5bd5b2 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-fill.c
@@ -0,0 +1,56 @@
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include "x11-memdraw.h"
+
+void
+memfillcolor(Memimage *m, u32int val)
+{
+	_memfillcolor(m, val);
+	if(m->X == nil)
+		return;
+	if((val & 0xFF) == 0xFF)	/* full alpha */
+		_xfillcolor(m, m->r, _rgbatoimg(m, val));
+	else
+		_xputxdata(m, m->r);
+}
+
+void
+_xfillcolor(Memimage *m, Rectangle r, u32int v)
+{
+	Point p;
+	Xmem *xm;
+	XGC gc;
+	
+	xm = m->X;
+	assert(xm != nil);
+
+	/*
+	 * Set up fill context appropriately.
+	 */
+	if(m->chan == GREY1){
+		gc = _x.gcfill0;
+		if(_x.gcfill0color != v){
+			XSetForeground(_x.display, gc, v);
+			_x.gcfill0color = v;
+		}
+	}else{
+		if(m->chan == CMAP8 && _x.usetable)
+			v = _x.tox11[v];
+		gc = _x.gcfill;
+		if(_x.gcfillcolor != v){
+			XSetForeground(_x.display, gc, v);
+			_x.gcfillcolor = v;
+		}
+	}
+
+	/*
+	 * XFillRectangle takes coordinates relative to image rectangle.
+	 */
+	p = subpt(r.min, m->r.min);
+	XFillRectangle(_x.display, xm->pixmap, gc, p.x, p.y, Dx(r), Dy(r));
+}
+
+
blob - /dev/null
blob + 395f45559c7c5461e0c2fe47c3e2202f50d106d1 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-get.c
@@ -0,0 +1,112 @@
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include "x11-memdraw.h"
+
+static void
+addrect(Rectangle *rp, Rectangle r)
+{
+	if(rp->min.x >= rp->max.x)
+		*rp = r;
+	else
+		combinerect(rp, r);
+}
+
+XImage*
+_xgetxdata(Memimage *m, Rectangle r)
+{
+	int x, y;
+	uchar *p;
+	Point tp, xdelta, delta;
+	Xmem *xm;
+	
+	xm = m->X;
+	if(xm == nil)
+		return nil;
+
+	if(xm->dirty == 0)
+		return xm->xi;
+
+	abort();	/* should never call this now */
+
+	r = xm->dirtyr;
+	if(Dx(r)==0 || Dy(r)==0)
+		return xm->xi;
+
+	delta = subpt(r.min, m->r.min);
+
+	tp = xm->r.min;	/* need temp for Digital UNIX */
+	xdelta = subpt(r.min, tp);
+
+	XGetSubImage(_x.display, xm->pixmap, delta.x, delta.y, Dx(r), Dy(r),
+		AllPlanes, ZPixmap, xm->xi, xdelta.x, delta.y);
+
+	if(_x.usetable && m->chan==CMAP8){
+		for(y=r.min.y; y<r.max.y; y++)
+		for(x=r.min.x, p=byteaddr(m, Pt(x,y)); x<r.max.x; x++, p++)
+			*p = _x.toplan9[*p];
+	}
+	xm->dirty = 0;
+	xm->dirtyr = Rect(0,0,0,0);
+	return xm->xi;
+}
+
+void
+_xputxdata(Memimage *m, Rectangle r)
+{
+	int offset, x, y;
+	uchar *p;
+	Point tp, xdelta, delta;
+	Xmem *xm;
+	XGC gc;
+	XImage *xi;
+
+	xm = m->X;
+	if(xm == nil)
+		return;
+
+	xi = xm->xi;
+	gc = m->chan==GREY1 ? _x.gccopy0 : _x.gccopy;
+	if(m->depth == 24)
+		offset = r.min.x & 3;
+	else
+		offset = r.min.x & (31/m->depth);
+
+	delta = subpt(r.min, m->r.min);
+
+	tp = xm->r.min;	/* need temporary on Digital UNIX */
+	xdelta = subpt(r.min, tp);
+
+	if(_x.usetable && m->chan==CMAP8){
+		for(y=r.min.y; y<r.max.y; y++)
+		for(x=r.min.x, p=byteaddr(m, Pt(x,y)); x<r.max.x; x++, p++)
+			*p = _x.tox11[*p];
+	}
+
+	XPutImage(_x.display, xm->pixmap, gc, xi, xdelta.x, xdelta.y, delta.x, delta.y,
+		Dx(r), Dy(r));
+	
+	if(_x.usetable && m->chan==CMAP8){
+		for(y=r.min.y; y<r.max.y; y++)
+		for(x=r.min.x, p=byteaddr(m, Pt(x,y)); x<r.max.x; x++, p++)
+			*p = _x.toplan9[*p];
+	}
+}
+
+void
+_xdirtyxdata(Memimage *m, Rectangle r)
+{
+	Xmem *xm;
+
+	xm = m->X;
+	if(xm == nil)
+		return;
+
+	xm->dirty = 1;
+	addrect(&xm->dirtyr, r);
+}
+
+
+
blob - /dev/null
blob + 4baf4b1afca1b06cee1cbe12d5a32c2b33449bc5 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-inc.h
@@ -0,0 +1,31 @@
+#define Colormap	XColormap
+#define Cursor		XCursor
+#define Display		XDisplay
+#define Drawable	XDrawable
+#define Font		XFont
+#define GC		XGC
+#define Point		XPoint
+#define Rectangle	XRectangle
+#define Screen		XScreen
+#define Visual		XVisual
+#define Window		XWindow
+
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+#include <X11/Xutil.h>
+#include <X11/keysym.h>
+#include <X11/IntrinsicP.h>
+#include <X11/StringDefs.h>
+
+#undef Colormap
+#undef Cursor
+#undef Display
+#undef Drawable
+#undef Font
+#undef GC
+#undef Point
+#undef Rectangle
+#undef Screen
+#undef Visual
+#undef Window
+
blob - /dev/null
blob + 9820ea2445584353fd58b4047c6c4244c844558f (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-init.c
@@ -0,0 +1,788 @@
+/*
+ * Some of the stuff in this file is not X-dependent and should be elsewhere.
+ */
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include <keyboard.h>
+#include <mouse.h>
+#include <cursor.h>
+#include "x11-memdraw.h"
+
+static int parsewinsize(char*, Rectangle*, int*);
+
+static void	plan9cmap(void);
+static int	setupcmap(XWindow);
+static XGC	xgc(XDrawable, int, int);
+
+Xprivate _x;
+
+static int
+xerror(XDisplay *d, XErrorEvent *e)
+{
+	char buf[200];
+
+	if(e->request_code == 42) /* XSetInputFocus */
+		return 0;
+	if(e->request_code == 18) /* XChangeProperty */
+		return 0;
+
+	print("X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
+		e->error_code, e->request_code, e->minor_code, d);
+	XGetErrorText(d, e->error_code, buf, sizeof buf);
+	print("%s\n", buf);
+	return 0;
+}
+
+static int
+xioerror(XDisplay *d)
+{
+	/*print("X I/O error\n"); */
+	sysfatal("X I/O error\n");
+	abort();
+	return -1;
+}
+
+
+Memimage*
+_xattach(char *label, char *winsize)
+{
+	char *argv[2], *disp;
+	int i, havemin, height, mask, n, width, x, xrootid, y;
+	Rectangle r;
+	XClassHint classhint;
+	XDrawable pmid;
+	XPixmapFormatValues *pfmt;
+	XScreen *xscreen;
+	XSetWindowAttributes attr;
+	XSizeHints normalhint;
+	XTextProperty name;
+	XVisualInfo xvi;
+	XWindow xrootwin;
+	XWindowAttributes wattr;
+	XWMHints hint;
+	Atom atoms[2];
+
+	/*
+	if(XInitThreads() == 0){
+		fprint(2, "XInitThreads failed\n");
+		abort();
+	}
+	*/
+
+	/*
+	 * Connect to X server.
+	 */
+	_x.display = XOpenDisplay(NULL);
+	if(_x.display == nil){
+		disp = getenv("DISPLAY");
+		werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
+		free(disp);
+		return nil;
+	}
+	_x.fd = ConnectionNumber(_x.display);
+	XSetErrorHandler(xerror);
+	XSetIOErrorHandler(xioerror);
+	xrootid = DefaultScreen(_x.display);
+	xrootwin = DefaultRootWindow(_x.display);
+
+	/* 
+	 * Figure out underlying screen format.
+	 */
+	if(XMatchVisualInfo(_x.display, xrootid, 16, TrueColor, &xvi)
+	|| XMatchVisualInfo(_x.display, xrootid, 16, DirectColor, &xvi)){
+		_x.vis = xvi.visual;
+		_x.depth = 16;
+	}
+	else
+	if(XMatchVisualInfo(_x.display, xrootid, 15, TrueColor, &xvi)
+	|| XMatchVisualInfo(_x.display, xrootid, 15, DirectColor, &xvi)){
+		_x.vis = xvi.visual;
+		_x.depth = 15;
+	}
+	else
+	if(XMatchVisualInfo(_x.display, xrootid, 24, TrueColor, &xvi)
+	|| XMatchVisualInfo(_x.display, xrootid, 24, DirectColor, &xvi)){
+		_x.vis = xvi.visual;
+		_x.depth = 24;
+	}
+	else
+	if(XMatchVisualInfo(_x.display, xrootid, 8, PseudoColor, &xvi)
+	|| XMatchVisualInfo(_x.display, xrootid, 8, StaticColor, &xvi)){
+		if(_x.depth > 8){
+			werrstr("can't deal with colormapped depth %d screens",
+				_x.depth);
+			goto err0;
+		}
+		_x.vis = xvi.visual;
+		_x.depth = 8;
+	}
+	else{
+		_x.depth = DefaultDepth(_x.display, xrootid);
+		if(_x.depth != 8){
+			werrstr("can't understand depth %d screen", _x.depth);
+			goto err0;
+		}
+		_x.vis = DefaultVisual(_x.display, xrootid);
+	}
+
+	if(DefaultDepth(_x.display, xrootid) == _x.depth)
+		_x.usetable = 1;
+
+	/*
+	 * _x.depth is only the number of significant pixel bits,
+	 * not the total number of pixel bits.  We need to walk the
+	 * display list to find how many actual bits are used
+	 * per pixel.
+	 */
+	_x.chan = 0;
+	pfmt = XListPixmapFormats(_x.display, &n);
+	for(i=0; i<n; i++){
+		if(pfmt[i].depth == _x.depth){
+			switch(pfmt[i].bits_per_pixel){
+			case 1:	/* untested */
+				_x.chan = GREY1;
+				break;
+			case 2:	/* untested */
+				_x.chan = GREY2;
+				break;
+			case 4:	/* untested */
+				_x.chan = GREY4;
+				break;
+			case 8:
+				_x.chan = CMAP8;
+				break;
+			case 15:
+				_x.chan = RGB15;
+				break;
+			case 16: /* how to tell RGB15? */
+				_x.chan = RGB16;
+				break;
+			case 24: /* untested (impossible?) */
+				_x.chan = RGB24;
+				break;
+			case 32:
+				_x.chan = XRGB32;
+				break;
+			}
+		}
+	}
+	if(_x.chan == 0){
+		werrstr("could not determine screen pixel format");
+		goto err0;
+	}
+
+	/*
+	 * Set up color map if necessary.
+	 */
+	xscreen = DefaultScreenOfDisplay(_x.display);
+	_x.cmap = DefaultColormapOfScreen(xscreen);
+	if(_x.vis->class != StaticColor){
+		plan9cmap();
+		setupcmap(xrootwin);
+	}
+
+	/*
+	 * We get to choose the initial rectangle size.
+	 * This is arbitrary.  In theory we should read the
+	 * command line and allow the traditional X options.
+	 */
+	mask = 0;
+	x = 0;
+	y = 0;
+	if(winsize && winsize[0]){
+		if(parsewinsize(winsize, &r, &havemin) < 0)
+			sysfatal("%r");
+	}else{
+		/*
+		 * Parse the various X resources.  Thanks to Peter Canning.
+		 */
+		char *screen_resources, *display_resources, *geom, 
+			*geomrestype, *home, *file;
+		XrmDatabase database;
+		XrmValue geomres;
+
+		database = XrmGetDatabase(_x.display);
+		screen_resources = XScreenResourceString(xscreen);
+		if(screen_resources != nil){
+			XrmCombineDatabase(XrmGetStringDatabase(screen_resources), &database, False);
+			XFree(screen_resources);
+		}
+
+		display_resources = XResourceManagerString(_x.display);
+		if(display_resources == nil){
+			home = getenv("HOME");
+			if(home!=nil && (file=smprint("%s/.Xdefaults", home)) != nil){
+				XrmCombineFileDatabase(file, &database, False);
+				free(file);
+			}
+			free(home);
+		}else
+			XrmCombineDatabase(XrmGetStringDatabase(display_resources), &database, False);
+
+		geom = smprint("%s.geometry", label);
+		if(geom && XrmGetResource(database, geom, nil, &geomrestype, &geomres))
+			mask = XParseGeometry(geomres.addr, &x, &y, (uint*)&width, (uint*)&height);
+		free(geom);
+
+		if((mask & WidthValue) && (mask & HeightValue)){
+			r = Rect(0, 0, width, height);
+		}else{
+			r = Rect(0, 0, WidthOfScreen(xscreen)*3/4,
+					HeightOfScreen(xscreen)*3/4);
+			if(Dx(r) > Dy(r)*3/2)
+				r.max.x = r.min.x + Dy(r)*3/2;
+			if(Dy(r) > Dx(r)*3/2)
+				r.max.y = r.min.y + Dx(r)*3/2;
+		}
+		if(mask & XNegative){
+			x += WidthOfScreen(xscreen);
+		}
+		if(mask & YNegative){
+			y += HeightOfScreen(xscreen);
+		}
+		havemin = 0;
+	}
+
+	memset(&attr, 0, sizeof attr);
+	attr.colormap = _x.cmap;
+	attr.background_pixel = ~0;
+	attr.border_pixel = 0;
+	_x.drawable = XCreateWindow(
+		_x.display,	/* display */
+		xrootwin,	/* parent */
+		x,		/* x */
+		y,		/* y */
+		Dx(r),		/* width */
+	 	Dy(r),		/* height */
+		0,		/* border width */
+		_x.depth,	/* depth */
+		InputOutput,	/* class */
+		_x.vis,		/* visual */
+				/* valuemask */
+		CWBackPixel|CWBorderPixel|CWColormap,
+		&attr		/* attributes (the above aren't?!) */
+	);
+
+	/*
+	 * Label and other properties required by ICCCCM.
+	 */
+	memset(&name, 0, sizeof name);
+	if(label == nil)
+		label = "pjw-face-here";
+	name.value = (uchar*)label;
+	name.encoding = XA_STRING;
+	name.format = 8;
+	name.nitems = strlen((char*)name.value);
+
+	memset(&normalhint, 0, sizeof normalhint);
+	normalhint.flags = PSize|PMaxSize;
+	if(winsize && winsize[0]){
+		normalhint.flags &= ~PSize;
+		normalhint.flags |= USSize;
+		normalhint.width = Dx(r);
+		normalhint.height = Dy(r);
+	}else{
+		if((mask & WidthValue) && (mask & HeightValue)){
+			normalhint.flags &= ~PSize;
+			normalhint.flags |= USSize;
+			normalhint.width = width;
+			normalhint.height = height;
+		}
+		if((mask & WidthValue) && (mask & HeightValue)){
+			normalhint.flags |= USPosition;
+			normalhint.x = x;
+			normalhint.y = y;
+		}
+	}
+
+	normalhint.max_width = WidthOfScreen(xscreen);
+	normalhint.max_height = HeightOfScreen(xscreen);
+
+	memset(&hint, 0, sizeof hint);
+	hint.flags = InputHint|StateHint;
+	hint.input = 1;
+	hint.initial_state = NormalState;
+
+	memset(&classhint, 0, sizeof classhint);
+	classhint.res_name = label;
+	classhint.res_class = label;
+
+	argv[0] = label;
+	argv[1] = nil;
+
+	XSetWMProperties(
+		_x.display,	/* display */
+		_x.drawable,	/* window */
+		&name,		/* XA_WM_NAME property */
+		&name,		/* XA_WM_ICON_NAME property */
+		argv,		/* XA_WM_COMMAND */
+		1,		/* argc */
+		&normalhint,	/* XA_WM_NORMAL_HINTS */
+		&hint,		/* XA_WM_HINTS */
+		&classhint	/* XA_WM_CLASSHINTS */
+	);
+	XFlush(_x.display);
+
+	if(havemin){
+		XWindowChanges ch;
+
+		memset(&ch, 0, sizeof ch);
+		ch.x = r.min.x;
+		ch.y = r.min.y;
+		XConfigureWindow(_x.display, _x.drawable, CWX|CWY, &ch);
+		/*
+		 * Must pretend origin is 0,0 for X.
+		 */
+		r = Rect(0,0,Dx(r),Dy(r));
+	}
+	/*
+	 * Look up clipboard atom.
+	 */
+	_x.clipboard = XInternAtom(_x.display, "CLIPBOARD", False);
+	_x.utf8string = XInternAtom(_x.display, "UTF8_STRING", False);
+	_x.targets = XInternAtom(_x.display, "TARGETS", False);
+	_x.text = XInternAtom(_x.display, "TEXT", False);
+	_x.compoundtext = XInternAtom(_x.display, "COMPOUND_TEXT", False);
+	_x.takefocus = XInternAtom(_x.display, "WM_TAKE_FOCUS", False);
+	_x.losefocus = XInternAtom(_x.display, "_9WM_LOSE_FOCUS", False);
+	_x.wmprotos = XInternAtom(_x.display, "WM_PROTOCOLS", False);
+
+	atoms[0] = _x.takefocus;
+	atoms[1] = _x.losefocus;
+	XChangeProperty(_x.display, _x.drawable, _x.wmprotos, XA_ATOM, 32,
+		PropModeReplace, (uchar*)atoms, 2);
+
+	/*
+	 * Put the window on the screen, check to see what size we actually got.
+	 */
+	XMapWindow(_x.display, _x.drawable);
+	XSync(_x.display, False);
+
+	if(!XGetWindowAttributes(_x.display, _x.drawable, &wattr))
+		fprint(2, "XGetWindowAttributes failed\n");
+	else if(wattr.width && wattr.height){
+		if(wattr.width != Dx(r) || wattr.height != Dy(r)){
+			r.max.x = wattr.width;
+			r.max.y = wattr.height;
+		}
+	}else
+		fprint(2, "XGetWindowAttributes: bad attrs\n");
+
+	/*
+	 * Allocate our local backing store.
+	 */
+	_x.screenr = r;
+	_x.screenpm = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
+	_x.nextscreenpm = _x.screenpm;
+	_x.screenimage = _xallocmemimage(r, _x.chan, _x.screenpm);
+
+	/*
+	 * Allocate some useful graphics contexts for the future.
+	 */
+	_x.gcfill	= xgc(_x.screenpm, FillSolid, -1);
+	_x.gccopy	= xgc(_x.screenpm, -1, -1);
+	_x.gcsimplesrc 	= xgc(_x.screenpm, FillStippled, -1);
+	_x.gczero	= xgc(_x.screenpm, -1, -1);
+	_x.gcreplsrc	= xgc(_x.screenpm, FillTiled, -1);
+
+	pmid = XCreatePixmap(_x.display, _x.drawable, 1, 1, 1);
+	_x.gcfill0	= xgc(pmid, FillSolid, 0);
+	_x.gccopy0	= xgc(pmid, -1, -1);
+	_x.gcsimplesrc0	= xgc(pmid, FillStippled, -1);
+	_x.gczero0	= xgc(pmid, -1, -1);
+	_x.gcreplsrc0	= xgc(pmid, FillTiled, -1);
+	XFreePixmap(_x.display, pmid);
+
+	return _x.screenimage;
+
+err0:
+	/*
+	 * Should do a better job of cleaning up here.
+	 */
+	XCloseDisplay(_x.display);
+	return nil;
+}
+
+int
+_xsetlabel(char *label)
+{
+	XTextProperty name;
+
+	/*
+	 * Label and other properties required by ICCCCM.
+	 */
+	memset(&name, 0, sizeof name);
+	if(label == nil)
+		label = "pjw-face-here";
+	name.value = (uchar*)label;
+	name.encoding = XA_STRING;
+	name.format = 8;
+	name.nitems = strlen((char*)name.value);
+
+	XSetWMProperties(
+		_x.display,	/* display */
+		_x.drawable,	/* window */
+		&name,		/* XA_WM_NAME property */
+		&name,		/* XA_WM_ICON_NAME property */
+		nil,		/* XA_WM_COMMAND */
+		0,		/* argc */
+		nil,		/* XA_WM_NORMAL_HINTS */
+		nil,		/* XA_WM_HINTS */
+		nil	/* XA_WM_CLASSHINTS */
+	);
+	XFlush(_x.display);
+	return 0;
+}
+
+/*
+ * Create a GC with a particular fill style and XXX.
+ * Disable generation of GraphicsExpose/NoExpose events in the GC.
+ */
+static XGC
+xgc(XDrawable d, int fillstyle, int foreground)
+{
+	XGC gc;
+	XGCValues v;
+
+	memset(&v, 0, sizeof v);
+	v.function = GXcopy;
+	v.graphics_exposures = False;
+	gc = XCreateGC(_x.display, d, GCFunction|GCGraphicsExposures, &v);
+	if(fillstyle != -1)
+		XSetFillStyle(_x.display, gc, fillstyle);
+	if(foreground != -1)
+		XSetForeground(_x.display, gc, 0);
+	return gc;
+}
+
+
+/*
+ * Initialize map with the Plan 9 rgbv color map.
+ */
+static void
+plan9cmap(void)
+{
+	int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
+	static int once;
+
+	if(once)
+		return;
+	once = 1;
+
+	for(r=0; r!=4; r++)
+	for(g = 0; g != 4; g++)
+	for(b = 0; b!=4; b++)
+	for(v = 0; v!=4; v++){
+		den=r;
+		if(g > den)
+			den=g;
+		if(b > den)
+			den=b;
+		/* divide check -- pick grey shades */
+		if(den==0)
+			cr=cg=cb=v*17;
+		else {
+			num=17*(4*den+v);
+			cr=r*num/den;
+			cg=g*num/den;
+			cb=b*num/den;
+		}
+		idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
+		_x.map[idx].red = cr*0x0101;
+		_x.map[idx].green = cg*0x0101;
+		_x.map[idx].blue = cb*0x0101;
+		_x.map[idx].pixel = idx;
+		_x.map[idx].flags = DoRed|DoGreen|DoBlue;
+
+		v7 = v >> 1;
+		idx7 = r*32 + v7*16 + g*4 + b;
+		if((v & 1) == v7){
+			_x.map7to8[idx7][0] = idx;
+			if(den == 0) { 		/* divide check -- pick grey shades */
+				cr = ((255.0/7.0)*v7)+0.5;
+				cg = cr;
+				cb = cr;
+			}
+			else {
+				num=17*15*(4*den+v7*2)/14;
+				cr=r*num/den;
+				cg=g*num/den;
+				cb=b*num/den;
+			}
+			_x.map7[idx7].red = cr*0x0101;
+			_x.map7[idx7].green = cg*0x0101;
+			_x.map7[idx7].blue = cb*0x0101;
+			_x.map7[idx7].pixel = idx7;
+			_x.map7[idx7].flags = DoRed|DoGreen|DoBlue;
+		}
+		else
+			_x.map7to8[idx7][1] = idx;
+	}
+}
+
+/*
+ * Initialize and install the rgbv color map as a private color map
+ * for this application.  It gets the best colors when it has the
+ * cursor focus.
+ *
+ * We always choose the best depth possible, but that might not
+ * be the default depth.  On such "suboptimal" systems, we have to allocate an
+ * empty color map anyway, according to Axel Belinfante.
+ */
+static int 
+setupcmap(XWindow w)
+{
+	char buf[30];
+	int i;
+	u32int p, pp;
+	XColor c;
+
+	if(_x.depth <= 1)
+		return 0;
+
+	if(_x.depth >= 24) {
+		if(_x.usetable == 0)
+			_x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone); 
+
+		/*
+		 * The pixel value returned from XGetPixel needs to
+		 * be converted to RGB so we can call rgb2cmap()
+		 * to translate between 24 bit X and our color. Unfortunately,
+		 * the return value appears to be display server endian 
+		 * dependant. Therefore, we run some heuristics to later
+		 * determine how to mask the int value correctly.
+		 * Yeah, I know we can look at _x.vis->byte_order but 
+		 * some displays say MSB even though they run on LSB.
+		 * Besides, this is more anal.
+		 */
+		c = _x.map[19];	/* known to have different R, G, B values */
+		if(!XAllocColor(_x.display, _x.cmap, &c)){
+			werrstr("XAllocColor: %r");
+			return -1;
+		}
+		p  = c.pixel;
+		pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
+		if(pp != _x.map[19].pixel) {
+			/* check if endian is other way */
+			pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
+			if(pp != _x.map[19].pixel){
+				werrstr("cannot detect X server byte order");
+				return -1;
+			}
+
+			switch(_x.chan){
+			case RGB24:
+				_x.chan = BGR24;
+				break;
+			case XRGB32:
+				_x.chan = XBGR32;
+				break;
+			default:
+				werrstr("cannot byteswap channel %s",
+					chantostr(buf, _x.chan));
+				break;
+			}
+		}
+	}else if(_x.vis->class == TrueColor || _x.vis->class == DirectColor){
+		/*
+		 * Do nothing.  We have no way to express a
+		 * mixed-endian 16-bit screen, so pretend they don't exist.
+		 */
+		if(_x.usetable == 0)
+			_x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocNone);
+	}else if(_x.vis->class == PseudoColor){
+		if(_x.usetable == 0){
+			_x.cmap = XCreateColormap(_x.display, w, _x.vis, AllocAll); 
+			XStoreColors(_x.display, _x.cmap, _x.map, 256);
+			for(i = 0; i < 256; i++){
+				_x.tox11[i] = i;
+				_x.toplan9[i] = i;
+			}
+		}else{
+			for(i = 0; i < 128; i++){
+				c = _x.map7[i];
+				if(!XAllocColor(_x.display, _x.cmap, &c)){
+					werrstr("can't allocate colors in 7-bit map");
+					return -1;
+				}
+				_x.tox11[_x.map7to8[i][0]] = c.pixel;
+				_x.tox11[_x.map7to8[i][1]] = c.pixel;
+				_x.toplan9[c.pixel] = _x.map7to8[i][0];
+			}
+		}
+	}else{
+		werrstr("unsupported visual class %d", _x.vis->class);
+		return -1;
+	}
+	return 0;
+}
+
+void
+_flushmemscreen(Rectangle r)
+{
+	if(_x.nextscreenpm != _x.screenpm){
+		qlock(&_x.screenlock);
+		XSync(_x.display, False);
+		XFreePixmap(_x.display, _x.screenpm);
+		_x.screenpm = _x.nextscreenpm;
+		qunlock(&_x.screenlock);
+	}
+
+	if(r.min.x >= r.max.x || r.min.y >= r.max.y)
+		return;
+	XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
+		Dx(r), Dy(r), r.min.x, r.min.y);
+	XFlush(_x.display);
+}
+
+void
+_xexpose(XEvent *e)
+{
+	XExposeEvent *xe;
+	Rectangle r;
+
+	qlock(&_x.screenlock);
+	if(_x.screenpm != _x.nextscreenpm){
+		qunlock(&_x.screenlock);
+		return;
+	}
+	xe = (XExposeEvent*)e;
+	r.min.x = xe->x;
+	r.min.y = xe->y;
+	r.max.x = xe->x+xe->width;
+	r.max.y = xe->y+xe->height;
+	XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
+		Dx(r), Dy(r), r.min.x, r.min.y);
+	XSync(_x.display, False);
+	qunlock(&_x.screenlock);
+}
+
+int
+_xdestroy(XEvent *e)
+{
+	XDestroyWindowEvent *xe;
+
+	xe = (XDestroyWindowEvent*)e;
+	if(xe->window == _x.drawable){
+		_x.destroyed = 1;
+		return 1;
+	}
+	return 0;
+}
+
+int
+_xconfigure(XEvent *e)
+{
+	Rectangle r;
+	XConfigureEvent *xe = (XConfigureEvent*)e;
+
+	if(xe->width == Dx(_x.screenr) && xe->height == Dy(_x.screenr))
+		return 0;
+	if(xe->width==0 || xe->height==0)
+		fprint(2, "ignoring resize to %dx%d\n", xe->width, xe->height);
+	r = Rect(0, 0, xe->width, xe->height);
+	qlock(&_x.screenlock);
+	if(_x.screenpm != _x.nextscreenpm){
+		XCopyArea(_x.display, _x.screenpm, _x.drawable, _x.gccopy, r.min.x, r.min.y,
+			Dx(r), Dy(r), r.min.x, r.min.y);
+		XSync(_x.display, False);
+	}
+	qunlock(&_x.screenlock);
+	_x.newscreenr = r;
+	return 1;
+}
+
+int
+_xreplacescreenimage(void)
+{
+	Memimage *m;
+	XDrawable pixmap;
+	Rectangle r;
+
+	r = _x.newscreenr;
+	if(eqrect(_x.screenr, r))
+		return 0;
+
+	pixmap = XCreatePixmap(_x.display, _x.drawable, Dx(r), Dy(r), _x.depth);
+	m = _xallocmemimage(r, _x.chan, pixmap);
+	if(_x.nextscreenpm != _x.screenpm)
+		XFreePixmap(_x.display, _x.nextscreenpm);
+	_x.nextscreenpm = pixmap;
+	_x.screenr = r;
+	_drawreplacescreenimage(m);
+	return 1;
+}
+
+static int
+parsewinsize(char *s, Rectangle *r, int *havemin)
+{
+	char c, *os;
+	int i, j, k, l;
+
+	os = s;
+	*havemin = 0;
+	*r = Rect(0,0,0,0);
+	if(!isdigit((uchar)*s))
+		goto oops;
+	i = strtol(s, &s, 0);
+	if(*s == 'x'){
+		s++;
+		if(!isdigit((uchar)*s))
+			goto oops;
+		j = strtol(s, &s, 0);
+		r->max.x = i;
+		r->max.y = j;
+		if(*s == 0)
+			return 0;
+		if(*s != '@')
+			goto oops;
+
+		s++;
+		if(!isdigit((uchar)*s))
+			goto oops;
+		i = strtol(s, &s, 0);
+		if(*s != ',' && *s != ' ')
+			goto oops;
+		s++;
+		if(!isdigit((uchar)*s))
+			goto oops;
+		j = strtol(s, &s, 0);
+		if(*s != 0)
+			goto oops;
+		*r = rectaddpt(*r, Pt(i,j));
+		*havemin = 1;
+		return 0;
+	}
+
+	c = *s;
+	if(c != ' ' && c != ',')
+		goto oops;
+	s++;
+	if(!isdigit((uchar)*s))
+		goto oops;
+	j = strtol(s, &s, 0);
+	if(*s != c)
+		goto oops;
+	s++;
+	if(!isdigit((uchar)*s))
+		goto oops;
+	k = strtol(s, &s, 0);
+	if(*s != c)
+		goto oops;
+	s++;
+	if(!isdigit((uchar)*s))
+		goto oops;
+	l = strtol(s, &s, 0);
+	if(*s != 0)
+		goto oops;
+	*r = Rect(i,j,k,l);
+	*havemin = 1;
+	return 0;
+
+oops:
+	werrstr("bad syntax in window size '%s'", os);
+	return -1;
+}
blob - /dev/null
blob + 81c5351cec2acb4d0de9a0a7903a5a1687844941 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-itrans.c
@@ -0,0 +1,704 @@
+/* input event and data structure translation */
+
+#include <u.h>
+#include "x11-inc.h"
+#ifdef __APPLE__
+#define APPLESNARF
+#define Boolean AppleBoolean
+#define Rect AppleRect
+#define EventMask AppleEventMask
+#define Point ApplePoint
+#define Cursor AppleCursor
+#include <Carbon/Carbon.h>
+AUTOFRAMEWORK(Carbon)
+#undef Boolean
+#undef Rect
+#undef EventMask
+#undef Point
+#undef Cursor
+#endif
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include <mouse.h>
+#include <cursor.h>
+#include <keyboard.h>
+#include "x11-memdraw.h"
+#include "x11-keysym2ucs.h"
+#undef time
+
+static KeySym
+__xtoplan9kbd(XEvent *e)
+{
+	KeySym k;
+
+	if(e->xany.type != KeyPress)
+		return -1;
+	needstack(64*1024);	/* X has some *huge* buffers in openobject */
+		/* and they're even bigger on SuSE */
+	XLookupString((XKeyEvent*)e,NULL,0,&k,NULL);
+	if(k == XK_Multi_key || k == NoSymbol)
+		return -1;
+
+	if(k&0xFF00){
+		switch(k){
+		case XK_BackSpace:
+		case XK_Tab:
+		case XK_Escape:
+		case XK_Delete:
+		case XK_KP_0:
+		case XK_KP_1:
+		case XK_KP_2:
+		case XK_KP_3:
+		case XK_KP_4:
+		case XK_KP_5:
+		case XK_KP_6:
+		case XK_KP_7:
+		case XK_KP_8:
+		case XK_KP_9:
+		case XK_KP_Divide:
+		case XK_KP_Multiply:
+		case XK_KP_Subtract:
+		case XK_KP_Add:
+		case XK_KP_Decimal:
+			k &= 0x7F;
+			break;
+		case XK_Linefeed:
+			k = '\r';
+			break;
+		case XK_KP_Space:
+			k = ' ';
+			break;
+		case XK_Home:
+		case XK_KP_Home:
+			k = Khome;
+			break;
+		case XK_Left:
+		case XK_KP_Left:
+			k = Kleft;
+			break;
+		case XK_Up:
+		case XK_KP_Up:
+			k = Kup;
+			break;
+		case XK_Down:
+		case XK_KP_Down:
+			k = Kdown;
+			break;
+		case XK_Right:
+		case XK_KP_Right:
+			k = Kright;
+			break;
+		case XK_Page_Down:
+		case XK_KP_Page_Down:
+			k = Kpgdown;
+			break;
+		case XK_End:
+		case XK_KP_End:
+			k = Kend;
+			break;
+		case XK_Page_Up:	
+		case XK_KP_Page_Up:
+			k = Kpgup;
+			break;
+		case XK_Insert:
+		case XK_KP_Insert:
+			k = Kins;
+			break;
+		case XK_KP_Enter:
+		case XK_Return:
+			k = '\n';
+			break;
+		case XK_Alt_L:
+		case XK_Meta_L:	/* Shift Alt on PCs */
+		case XK_Alt_R:
+		case XK_Meta_R:	/* Shift Alt on PCs */
+			k = Kalt;
+			break;
+		default:		/* not ISO-1 or tty control */
+			if(k>0xff) {
+				k = _p9keysym2ucs(k);
+				if(k==-1) return -1;
+			}
+		}
+	}
+
+	/* Compensate for servers that call a minus a hyphen */
+	if(k == XK_hyphen)
+		k = XK_minus;
+	/* Do control mapping ourselves if translator doesn't */
+	if(e->xkey.state&ControlMask)
+		k &= 0x9f;
+	if(k == NoSymbol) {
+		return -1;
+	}
+
+	return k+0;
+}
+
+extern int _latin1(Rune*, int);
+static Rune*
+xtoplan9latin1(XEvent *e)
+{
+	static Rune k[10];
+	static int alting, nk;
+	int n;
+	int r;
+
+	r = __xtoplan9kbd(e);
+	if(r < 0)
+		return nil;
+	if(alting){
+		/*
+		 * Kludge for Mac's X11 3-button emulation.
+		 * It treats Command+Button as button 3, but also
+		 * ends up sending XK_Meta_L twice.
+		 */
+		if(r == Kalt){
+			alting = 0;
+			return nil;
+		}
+		k[nk++] = r;
+		n = _latin1(k, nk);
+		if(n > 0){
+			alting = 0;
+			k[0] = n;
+			k[1] = 0;
+			return k;
+		}
+		if(n == -1){
+			alting = 0;
+			k[nk] = 0;
+			return k;
+		}
+		/* n < -1, need more input */
+		return nil;
+	}else if(r == Kalt){
+		alting = 1;
+		nk = 0;
+		return nil;
+	}else{
+		k[0] = r;
+		k[1] = 0;
+		return k;
+	}
+}
+
+int
+_xtoplan9kbd(XEvent *e)
+{
+	static Rune *r;
+
+	if(e == (XEvent*)-1){
+		assert(r);
+		r--;
+		return 0;
+	}
+	if(e)
+		r = xtoplan9latin1(e);
+	if(r && *r)
+		return *r++;
+	return -1;
+}
+
+int
+_xtoplan9mouse(XEvent *e, Mouse *m)
+{
+	int s;
+	XButtonEvent *be;
+	XMotionEvent *me;
+
+	if(_x.putsnarf != _x.assertsnarf){
+		_x.assertsnarf = _x.putsnarf;
+		XSetSelectionOwner(_x.display, XA_PRIMARY, _x.drawable, CurrentTime);
+		if(_x.clipboard != None)
+			XSetSelectionOwner(_x.display, _x.clipboard, _x.drawable, CurrentTime);
+		XFlush(_x.display);
+	}
+
+	switch(e->type){
+	case ButtonPress:
+		be = (XButtonEvent*)e;
+		/* 
+		 * Fake message, just sent to make us announce snarf.
+		 * Apparently state and button are 16 and 8 bits on
+		 * the wire, since they are truncated by the time they
+		 * get to us.
+		 */
+		if(be->send_event
+		&& (~be->state&0xFFFF)==0
+		&& (~be->button&0xFF)==0)
+			return -1;
+		/* BUG? on mac need to inherit these from elsewhere? */
+		m->xy.x = be->x;
+		m->xy.y = be->y;
+		s = be->state;
+		m->msec = be->time;
+		switch(be->button){
+		case 1:
+			s |= Button1Mask;
+			break;
+		case 2:
+			s |= Button2Mask;
+			break;
+		case 3:
+			s |= Button3Mask;
+			break;
+		case 4:
+			s |= Button4Mask;
+			break;
+		case 5:
+			s |= Button5Mask;
+			break;
+		}
+		break;
+	case ButtonRelease:
+		be = (XButtonEvent*)e;
+		m->xy.x = be->x;
+		m->xy.y = be->y;
+		s = be->state;
+		m->msec = be->time;
+		switch(be->button){
+		case 1:
+			s &= ~Button1Mask;
+			break;
+		case 2:
+			s &= ~Button2Mask;
+			break;
+		case 3:
+			s &= ~Button3Mask;
+			break;
+		case 4:
+			s &= ~Button4Mask;
+			break;
+		case 5:
+			s &= ~Button5Mask;
+			break;
+		}
+		break;
+
+	case MotionNotify:
+		me = (XMotionEvent*)e;
+		s = me->state;
+		m->xy.x = me->x;
+		m->xy.y = me->y;
+		m->msec = me->time;
+		break;
+
+	default:
+		return -1;
+	}
+
+	m->buttons = 0;
+	if(s & Button1Mask)
+		m->buttons |= 1;
+	if(s & Button2Mask)
+		m->buttons |= 2;
+	if(s & Button3Mask)
+		m->buttons |= 4;
+	if(s & Button4Mask)
+		m->buttons |= 8;
+	if(s & Button5Mask)
+		m->buttons |= 16;
+	return 0;
+}
+
+void
+_xmoveto(Point p)
+{
+	XWarpPointer(_x.display, None, _x.drawable, 0, 0, 0, 0, p.x, p.y);
+	XFlush(_x.display);
+}
+
+static int
+revbyte(int b)
+{
+	int r;
+
+	r = 0;
+	r |= (b&0x01) << 7;
+	r |= (b&0x02) << 5;
+	r |= (b&0x04) << 3;
+	r |= (b&0x08) << 1;
+	r |= (b&0x10) >> 1;
+	r |= (b&0x20) >> 3;
+	r |= (b&0x40) >> 5;
+	r |= (b&0x80) >> 7;
+	return r;
+}
+
+static void
+xcursorarrow(void)
+{
+	if(_x.cursor != 0){
+		XFreeCursor(_x.display, _x.cursor);
+		_x.cursor = 0;
+	}
+	XUndefineCursor(_x.display, _x.drawable);
+	XFlush(_x.display);
+}
+
+
+void
+_xsetcursor(Cursor *c)
+{
+	XColor fg, bg;
+	XCursor xc;
+	Pixmap xsrc, xmask;
+	int i;
+	uchar src[2*16], mask[2*16];
+
+	if(c == nil){
+		xcursorarrow();
+		return;
+	}
+	for(i=0; i<2*16; i++){
+		src[i] = revbyte(c->set[i]);
+		mask[i] = revbyte(c->set[i] | c->clr[i]);
+	}
+
+	fg = _x.map[0];
+	bg = _x.map[255];
+	xsrc = XCreateBitmapFromData(_x.display, _x.drawable, (char*)src, 16, 16);
+	xmask = XCreateBitmapFromData(_x.display, _x.drawable, (char*)mask, 16, 16);
+	xc = XCreatePixmapCursor(_x.display, xsrc, xmask, &fg, &bg, -c->offset.x, -c->offset.y);
+	if(xc != 0) {
+		XDefineCursor(_x.display, _x.drawable, xc);
+		if(_x.cursor != 0)
+			XFreeCursor(_x.display, _x.cursor);
+		_x.cursor = xc;
+	}
+	XFreePixmap(_x.display, xsrc);
+	XFreePixmap(_x.display, xmask);
+	XFlush(_x.display);
+}
+
+struct {
+	QLock lk;
+	char buf[SnarfSize];
+#ifdef APPLESNARF
+	Rune rbuf[SnarfSize];
+	PasteboardRef apple;
+#endif
+} clip;
+
+char*
+_xgetsnarf(void)
+{
+	uchar *data, *xdata;
+	Atom clipboard, type, prop;
+	ulong len, lastlen, dummy;
+	int fmt, i;
+	XWindow w;
+
+	qlock(&clip.lk);
+	/*
+	 * Have we snarfed recently and the X server hasn't caught up?
+	 */
+	if(_x.putsnarf != _x.assertsnarf)
+		goto mine;
+
+	/*
+	 * Is there a primary selection (highlighted text in an xterm)?
+	 */
+	clipboard = XA_PRIMARY;
+	w = XGetSelectionOwner(_x.display, XA_PRIMARY);
+	if(w == _x.drawable){
+	mine:
+		data = (uchar*)strdup(clip.buf);
+		goto out;
+	}
+
+	/*
+	 * If not, is there a clipboard selection?
+	 */
+	if(w == None && _x.clipboard != None){
+		clipboard = _x.clipboard;
+		w = XGetSelectionOwner(_x.display, _x.clipboard);
+		if(w == _x.drawable)
+			goto mine;
+	}
+
+	/*
+	 * If not, give up.
+	 */
+	if(w == None){
+		data = nil;
+		goto out;
+	}
+		
+	/*
+	 * We should be waiting for SelectionNotify here, but it might never
+	 * come, and we have no way to time out.  Instead, we will clear
+	 * local property #1, request our buddy to fill it in for us, and poll
+	 * until he's done or we get tired of waiting.
+	 *
+	 * We should try to go for _x.utf8string instead of XA_STRING,
+	 * but that would add to the polling.
+	 */
+	prop = 1;
+	XChangeProperty(_x.display, _x.drawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
+	XConvertSelection(_x.display, clipboard, XA_STRING, prop, _x.drawable, CurrentTime);
+	XFlush(_x.display);
+	lastlen = 0;
+	for(i=0; i<10 || (lastlen!=0 && i<30); i++){
+		usleep(100*1000);
+		XGetWindowProperty(_x.display, _x.drawable, prop, 0, 0, 0, AnyPropertyType,
+			&type, &fmt, &dummy, &len, &data);
+		if(lastlen == len && len > 0)
+			break;
+		lastlen = len;
+	}
+	if(i == 10){
+		data = nil;
+		goto out;
+	}
+	/* get the property */
+	data = nil;
+	XGetWindowProperty(_x.display, _x.drawable, prop, 0, SnarfSize/sizeof(ulong), 0, 
+		AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
+	if((type != XA_STRING && type != _x.utf8string) || len == 0){
+		if(xdata)
+			XFree(xdata);
+		data = nil;
+	}else{
+		if(xdata){
+			data = (uchar*)strdup((char*)xdata);
+			XFree(xdata);
+		}else
+			data = nil;
+	}
+out:
+	qunlock(&clip.lk);
+	return (char*)data;
+}
+
+void
+__xputsnarf(char *data)
+{
+	XButtonEvent e;
+
+	if(strlen(data) >= SnarfSize)
+		return;
+	qlock(&clip.lk);
+	strcpy(clip.buf, data);
+	/* leave note for mouse proc to assert selection ownership */
+	_x.putsnarf++;
+
+	/* send mouse a fake event so snarf is announced */
+	memset(&e, 0, sizeof e);
+	e.type = ButtonPress;
+	e.window = _x.drawable;
+	e.state = ~0;
+	e.button = ~0;
+	XSendEvent(_x.display, _x.drawable, True, ButtonPressMask, (XEvent*)&e);
+	XFlush(_x.display);
+	qunlock(&clip.lk);
+}
+
+int
+_xselect(XEvent *e)
+{
+	char *name;
+	XEvent r;
+	XSelectionRequestEvent *xe;
+	Atom a[4];
+
+	memset(&r, 0, sizeof r);
+	xe = (XSelectionRequestEvent*)e;
+if(0) fprint(2, "xselect target=%d requestor=%d property=%d selection=%d\n",
+	xe->target, xe->requestor, xe->property, xe->selection);
+	r.xselection.property = xe->property;
+	if(xe->target == _x.targets){
+		a[0] = XA_STRING;
+		a[1] = _x.utf8string;
+		a[2] = _x.text;
+		a[3] = _x.compoundtext;
+
+		XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
+			8, PropModeReplace, (uchar*)a, sizeof a);
+	}else if(xe->target == XA_STRING 
+	|| xe->target == _x.utf8string 
+	|| xe->target == _x.text 
+	|| xe->target == _x.compoundtext
+	|| ((name = XGetAtomName(_x.display, xe->target)) && strcmp(name, "text/plain;charset=UTF-8") == 0)){
+		/* text/plain;charset=UTF-8 seems nonstandard but is used by Synergy */
+		/* if the target is STRING we're supposed to reply with Latin1 XXX */
+		qlock(&clip.lk);
+		XChangeProperty(_x.display, xe->requestor, xe->property, xe->target,
+			8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
+		qunlock(&clip.lk);
+	}else{
+		if(strcmp(name, "TIMESTAMP") != 0)
+			fprint(2, "%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
+		r.xselection.property = None;
+	}
+
+	r.xselection.display = xe->display;
+	/* r.xselection.property filled above */
+	r.xselection.target = xe->target;
+	r.xselection.type = SelectionNotify;
+	r.xselection.requestor = xe->requestor;
+	r.xselection.time = xe->time;
+	r.xselection.send_event = True;
+	r.xselection.selection = xe->selection;
+	XSendEvent(_x.display, xe->requestor, False, 0, &r);
+	XFlush(_x.display);
+	return 0;
+}
+
+#ifdef APPLESNARF
+char*
+_applegetsnarf(void)
+{
+	char *s, *t;
+	CFArrayRef flavors;
+	CFDataRef data;
+	CFIndex nflavor, ndata, j;
+	CFStringRef type;
+	ItemCount nitem;
+	PasteboardItemID id;
+	PasteboardSyncFlags flags;
+	UInt32 i;
+
+/*	fprint(2, "applegetsnarf\n"); */
+	qlock(&clip.lk);
+	if(clip.apple == nil){
+		if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
+			fprint(2, "apple pasteboard create failed\n");
+			qunlock(&clip.lk);
+			return nil;
+		}
+	}
+	flags = PasteboardSynchronize(clip.apple);
+	if(flags&kPasteboardClientIsOwner){
+		s = strdup(clip.buf);
+		qunlock(&clip.lk);
+		return s;
+	}
+	if(PasteboardGetItemCount(clip.apple, &nitem) != noErr){
+		fprint(2, "apple pasteboard get item count failed\n");
+		qunlock(&clip.lk);
+		return nil;
+	}
+	for(i=1; i<=nitem; i++){
+		if(PasteboardGetItemIdentifier(clip.apple, i, &id) != noErr)
+			continue;
+		if(PasteboardCopyItemFlavors(clip.apple, id, &flavors) != noErr)
+			continue;
+		nflavor = CFArrayGetCount(flavors);
+		for(j=0; j<nflavor; j++){
+			type = (CFStringRef)CFArrayGetValueAtIndex(flavors, j);
+			if(!UTTypeConformsTo(type, CFSTR("public.utf16-plain-text")))
+				continue;
+			if(PasteboardCopyItemFlavorData(clip.apple, id, type, &data) != noErr)
+				continue;
+			ndata = CFDataGetLength(data);
+			qunlock(&clip.lk);
+			s = smprint("%.*S", ndata/2, (Rune*)CFDataGetBytePtr(data));
+			CFRelease(flavors);
+			CFRelease(data);
+			for(t=s; *t; t++)
+				if(*t == '\r')
+					*t = '\n';
+			return s;
+		}
+		CFRelease(flavors);
+	}
+	qunlock(&clip.lk);
+	return nil;		
+}
+
+void
+_appleputsnarf(char *s)
+{
+	CFDataRef cfdata;
+	PasteboardSyncFlags flags;
+
+/*	fprint(2, "appleputsnarf\n"); */
+
+	if(strlen(s) >= SnarfSize)
+		return;
+	qlock(&clip.lk);
+	strcpy(clip.buf, s);
+	runesnprint(clip.rbuf, nelem(clip.rbuf), "%s", s);
+	if(clip.apple == nil){
+		if(PasteboardCreate(kPasteboardClipboard, &clip.apple) != noErr){
+			fprint(2, "apple pasteboard create failed\n");
+			qunlock(&clip.lk);
+			return;
+		}
+	}
+	if(PasteboardClear(clip.apple) != noErr){
+		fprint(2, "apple pasteboard clear failed\n");
+		qunlock(&clip.lk);
+		return;
+	}
+	flags = PasteboardSynchronize(clip.apple);
+	if((flags&kPasteboardModified) || !(flags&kPasteboardClientIsOwner)){
+		fprint(2, "apple pasteboard cannot assert ownership\n");
+		qunlock(&clip.lk);
+		return;
+	}
+	cfdata = CFDataCreate(kCFAllocatorDefault, 
+		(uchar*)clip.rbuf, runestrlen(clip.rbuf)*2);
+	if(cfdata == nil){
+		fprint(2, "apple pasteboard cfdatacreate failed\n");
+		qunlock(&clip.lk);
+		return;
+	}
+	if(PasteboardPutItemFlavor(clip.apple, (PasteboardItemID)1,
+		CFSTR("public.utf16-plain-text"), cfdata, 0) != noErr){
+		fprint(2, "apple pasteboard putitem failed\n");
+		CFRelease(cfdata);
+		qunlock(&clip.lk);
+		return;
+	}
+	/* CFRelease(cfdata); ??? */
+	qunlock(&clip.lk);
+}
+#endif	/* APPLESNARF */
+
+void
+_xputsnarf(char *data)
+{
+#ifdef APPLESNARF
+	_appleputsnarf(data);
+#endif
+	__xputsnarf(data);
+}
+
+/*
+ * Send the mouse event back to the window manager.
+ * So that 9term can tell rio to pop up its button3 menu.
+ */
+void
+_xbouncemouse(Mouse *m)
+{
+	XButtonEvent e;
+	XWindow dw;
+
+	e.type = ButtonPress;
+	e.state = 0;
+	e.button = 0;
+	if(m->buttons&1)
+		e.button = 1;
+	else if(m->buttons&2)
+		e.button = 2;
+	else if(m->buttons&4)
+		e.button = 3;
+	e.same_screen = 1;
+	XTranslateCoordinates(_x.display, _x.drawable,
+		DefaultRootWindow(_x.display),
+		m->xy.x, m->xy.y, &e.x_root, &e.y_root, &dw);
+	e.root = DefaultRootWindow(_x.display);
+	e.window = e.root;
+	e.subwindow = None;
+	e.x = e.x_root;
+	e.y = e.y_root;
+#undef time
+	e.time = CurrentTime;
+	XUngrabPointer(_x.display, m->msec);
+	XSendEvent(_x.display, e.root, True, ButtonPressMask, (XEvent*)&e);
+	XFlush(_x.display);
+}
blob - /dev/null
blob + 572f01d393ebdeeafa5584f2f191d0ee6f00d9ff (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-keysym2ucs.c
@@ -0,0 +1,857 @@
+/* $XFree86: xc/programs/xterm/keysym2ucs.c,v 1.5 2001/06/18 19:09:26 dickey Exp $
+ * This module converts keysym values into the corresponding ISO 10646
+ * (UCS, Unicode) values.
+ *
+ * The array keysymtab[] contains pairs of X11 keysym values for graphical
+ * characters and the corresponding Unicode value. The function
+ * keysym2ucs() maps a keysym onto a Unicode value using a binary search,
+ * therefore keysymtab[] must remain SORTED by keysym value.
+ *
+ * The keysym -> UTF-8 conversion will hopefully one day be provided
+ * by Xlib via XmbLookupString() and should ideally not have to be
+ * done in X applications. But we are not there yet.
+ *
+ * We allow to represent any UCS character in the range U-00000000 to
+ * U-00FFFFFF by a keysym value in the range 0x01000000 to 0x01ffffff.
+ * This admittedly does not cover the entire 31-bit space of UCS, but
+ * it does cover all of the characters up to U-10FFFF, which can be
+ * represented by UTF-16, and more, and it is very unlikely that higher
+ * UCS codes will ever be assigned by ISO. So to get Unicode character
+ * U+ABCD you can directly use keysym 0x0100abcd.
+ *
+ * NOTE: The comments in the table below contain the actual character
+ * encoded in UTF-8, so for viewing and editing best use an editor in
+ * UTF-8 mode.
+ *
+ * Author: Markus G. Kuhn <mkuhn@acm.org>, University of Cambridge, April 2001
+ *
+ * Special thanks to Richard Verhoeven <river@win.tue.nl> for preparing
+ * an initial draft of the mapping table.
+ *
+ * This software is in the public domain. Share and enjoy!
+ *
+ * AUTOMATICALLY GENERATED FILE, DO NOT EDIT !!! (unicode/convmap.pl)
+ */
+
+#ifndef KEYSYM2UCS_INCLUDED
+  
+#include "x11-keysym2ucs.h"
+#define VISIBLE /* */
+
+#else
+
+#define VISIBLE static
+
+#endif
+
+static struct codepair {
+  unsigned short keysym;
+  unsigned short ucs;
+} keysymtab[] = {
+  { 0x01a1, 0x0104 }, /*                     Aogonek Ą LATIN CAPITAL LETTER A WITH OGONEK */
+  { 0x01a2, 0x02d8 }, /*                       breve ˘ BREVE */
+  { 0x01a3, 0x0141 }, /*                     Lstroke Ł LATIN CAPITAL LETTER L WITH STROKE */
+  { 0x01a5, 0x013d }, /*                      Lcaron Ľ LATIN CAPITAL LETTER L WITH CARON */
+  { 0x01a6, 0x015a }, /*                      Sacute Ś LATIN CAPITAL LETTER S WITH ACUTE */
+  { 0x01a9, 0x0160 }, /*                      Scaron Š LATIN CAPITAL LETTER S WITH CARON */
+  { 0x01aa, 0x015e }, /*                    Scedilla Ş LATIN CAPITAL LETTER S WITH CEDILLA */
+  { 0x01ab, 0x0164 }, /*                      Tcaron Ť LATIN CAPITAL LETTER T WITH CARON */
+  { 0x01ac, 0x0179 }, /*                      Zacute Ź LATIN CAPITAL LETTER Z WITH ACUTE */
+  { 0x01ae, 0x017d }, /*                      Zcaron Ž LATIN CAPITAL LETTER Z WITH CARON */
+  { 0x01af, 0x017b }, /*                   Zabovedot Ż LATIN CAPITAL LETTER Z WITH DOT ABOVE */
+  { 0x01b1, 0x0105 }, /*                     aogonek ą LATIN SMALL LETTER A WITH OGONEK */
+  { 0x01b2, 0x02db }, /*                      ogonek ˛ OGONEK */
+  { 0x01b3, 0x0142 }, /*                     lstroke ł LATIN SMALL LETTER L WITH STROKE */
+  { 0x01b5, 0x013e }, /*                      lcaron ľ LATIN SMALL LETTER L WITH CARON */
+  { 0x01b6, 0x015b }, /*                      sacute ś LATIN SMALL LETTER S WITH ACUTE */
+  { 0x01b7, 0x02c7 }, /*                       caron ˇ CARON */
+  { 0x01b9, 0x0161 }, /*                      scaron š LATIN SMALL LETTER S WITH CARON */
+  { 0x01ba, 0x015f }, /*                    scedilla ş LATIN SMALL LETTER S WITH CEDILLA */
+  { 0x01bb, 0x0165 }, /*                      tcaron ť LATIN SMALL LETTER T WITH CARON */
+  { 0x01bc, 0x017a }, /*                      zacute ź LATIN SMALL LETTER Z WITH ACUTE */
+  { 0x01bd, 0x02dd }, /*                 doubleacute ˝ DOUBLE ACUTE ACCENT */
+  { 0x01be, 0x017e }, /*                      zcaron ž LATIN SMALL LETTER Z WITH CARON */
+  { 0x01bf, 0x017c }, /*                   zabovedot ż LATIN SMALL LETTER Z WITH DOT ABOVE */
+  { 0x01c0, 0x0154 }, /*                      Racute Ŕ LATIN CAPITAL LETTER R WITH ACUTE */
+  { 0x01c3, 0x0102 }, /*                      Abreve Ă LATIN CAPITAL LETTER A WITH BREVE */
+  { 0x01c5, 0x0139 }, /*                      Lacute Ĺ LATIN CAPITAL LETTER L WITH ACUTE */
+  { 0x01c6, 0x0106 }, /*                      Cacute Ć LATIN CAPITAL LETTER C WITH ACUTE */
+  { 0x01c8, 0x010c }, /*                      Ccaron Č LATIN CAPITAL LETTER C WITH CARON */
+  { 0x01ca, 0x0118 }, /*                     Eogonek Ę LATIN CAPITAL LETTER E WITH OGONEK */
+  { 0x01cc, 0x011a }, /*                      Ecaron Ě LATIN CAPITAL LETTER E WITH CARON */
+  { 0x01cf, 0x010e }, /*                      Dcaron Ď LATIN CAPITAL LETTER D WITH CARON */
+  { 0x01d0, 0x0110 }, /*                     Dstroke Đ LATIN CAPITAL LETTER D WITH STROKE */
+  { 0x01d1, 0x0143 }, /*                      Nacute Ń LATIN CAPITAL LETTER N WITH ACUTE */
+  { 0x01d2, 0x0147 }, /*                      Ncaron Ň LATIN CAPITAL LETTER N WITH CARON */
+  { 0x01d5, 0x0150 }, /*                Odoubleacute Ő LATIN CAPITAL LETTER O WITH DOUBLE ACUTE */
+  { 0x01d8, 0x0158 }, /*                      Rcaron Ř LATIN CAPITAL LETTER R WITH CARON */
+  { 0x01d9, 0x016e }, /*                       Uring Ů LATIN CAPITAL LETTER U WITH RING ABOVE */
+  { 0x01db, 0x0170 }, /*                Udoubleacute Ű LATIN CAPITAL LETTER U WITH DOUBLE ACUTE */
+  { 0x01de, 0x0162 }, /*                    Tcedilla Ţ LATIN CAPITAL LETTER T WITH CEDILLA */
+  { 0x01e0, 0x0155 }, /*                      racute ŕ LATIN SMALL LETTER R WITH ACUTE */
+  { 0x01e3, 0x0103 }, /*                      abreve ă LATIN SMALL LETTER A WITH BREVE */
+  { 0x01e5, 0x013a }, /*                      lacute ĺ LATIN SMALL LETTER L WITH ACUTE */
+  { 0x01e6, 0x0107 }, /*                      cacute ć LATIN SMALL LETTER C WITH ACUTE */
+  { 0x01e8, 0x010d }, /*                      ccaron č LATIN SMALL LETTER C WITH CARON */
+  { 0x01ea, 0x0119 }, /*                     eogonek ę LATIN SMALL LETTER E WITH OGONEK */
+  { 0x01ec, 0x011b }, /*                      ecaron ě LATIN SMALL LETTER E WITH CARON */
+  { 0x01ef, 0x010f }, /*                      dcaron ď LATIN SMALL LETTER D WITH CARON */
+  { 0x01f0, 0x0111 }, /*                     dstroke đ LATIN SMALL LETTER D WITH STROKE */
+  { 0x01f1, 0x0144 }, /*                      nacute ń LATIN SMALL LETTER N WITH ACUTE */
+  { 0x01f2, 0x0148 }, /*                      ncaron ň LATIN SMALL LETTER N WITH CARON */
+  { 0x01f5, 0x0151 }, /*                odoubleacute ő LATIN SMALL LETTER O WITH DOUBLE ACUTE */
+  { 0x01f8, 0x0159 }, /*                      rcaron ř LATIN SMALL LETTER R WITH CARON */
+  { 0x01f9, 0x016f }, /*                       uring ů LATIN SMALL LETTER U WITH RING ABOVE */
+  { 0x01fb, 0x0171 }, /*                udoubleacute ű LATIN SMALL LETTER U WITH DOUBLE ACUTE */
+  { 0x01fe, 0x0163 }, /*                    tcedilla ţ LATIN SMALL LETTER T WITH CEDILLA */
+  { 0x01ff, 0x02d9 }, /*                    abovedot ˙ DOT ABOVE */
+  { 0x02a1, 0x0126 }, /*                     Hstroke Ħ LATIN CAPITAL LETTER H WITH STROKE */
+  { 0x02a6, 0x0124 }, /*                 Hcircumflex Ĥ LATIN CAPITAL LETTER H WITH CIRCUMFLEX */
+  { 0x02a9, 0x0130 }, /*                   Iabovedot İ LATIN CAPITAL LETTER I WITH DOT ABOVE */
+  { 0x02ab, 0x011e }, /*                      Gbreve Ğ LATIN CAPITAL LETTER G WITH BREVE */
+  { 0x02ac, 0x0134 }, /*                 Jcircumflex Ĵ LATIN CAPITAL LETTER J WITH CIRCUMFLEX */
+  { 0x02b1, 0x0127 }, /*                     hstroke ħ LATIN SMALL LETTER H WITH STROKE */
+  { 0x02b6, 0x0125 }, /*                 hcircumflex ĥ LATIN SMALL LETTER H WITH CIRCUMFLEX */
+  { 0x02b9, 0x0131 }, /*                    idotless ı LATIN SMALL LETTER DOTLESS I */
+  { 0x02bb, 0x011f }, /*                      gbreve ğ LATIN SMALL LETTER G WITH BREVE */
+  { 0x02bc, 0x0135 }, /*                 jcircumflex ĵ LATIN SMALL LETTER J WITH CIRCUMFLEX */
+  { 0x02c5, 0x010a }, /*                   Cabovedot Ċ LATIN CAPITAL LETTER C WITH DOT ABOVE */
+  { 0x02c6, 0x0108 }, /*                 Ccircumflex Ĉ LATIN CAPITAL LETTER C WITH CIRCUMFLEX */
+  { 0x02d5, 0x0120 }, /*                   Gabovedot Ġ LATIN CAPITAL LETTER G WITH DOT ABOVE */
+  { 0x02d8, 0x011c }, /*                 Gcircumflex Ĝ LATIN CAPITAL LETTER G WITH CIRCUMFLEX */
+  { 0x02dd, 0x016c }, /*                      Ubreve Ŭ LATIN CAPITAL LETTER U WITH BREVE */
+  { 0x02de, 0x015c }, /*                 Scircumflex Ŝ LATIN CAPITAL LETTER S WITH CIRCUMFLEX */
+  { 0x02e5, 0x010b }, /*                   cabovedot ċ LATIN SMALL LETTER C WITH DOT ABOVE */
+  { 0x02e6, 0x0109 }, /*                 ccircumflex ĉ LATIN SMALL LETTER C WITH CIRCUMFLEX */
+  { 0x02f5, 0x0121 }, /*                   gabovedot ġ LATIN SMALL LETTER G WITH DOT ABOVE */
+  { 0x02f8, 0x011d }, /*                 gcircumflex ĝ LATIN SMALL LETTER G WITH CIRCUMFLEX */
+  { 0x02fd, 0x016d }, /*                      ubreve ŭ LATIN SMALL LETTER U WITH BREVE */
+  { 0x02fe, 0x015d }, /*                 scircumflex ŝ LATIN SMALL LETTER S WITH CIRCUMFLEX */
+  { 0x03a2, 0x0138 }, /*                         kra ĸ LATIN SMALL LETTER KRA */
+  { 0x03a3, 0x0156 }, /*                    Rcedilla Ŗ LATIN CAPITAL LETTER R WITH CEDILLA */
+  { 0x03a5, 0x0128 }, /*                      Itilde Ĩ LATIN CAPITAL LETTER I WITH TILDE */
+  { 0x03a6, 0x013b }, /*                    Lcedilla Ļ LATIN CAPITAL LETTER L WITH CEDILLA */
+  { 0x03aa, 0x0112 }, /*                     Emacron Ē LATIN CAPITAL LETTER E WITH MACRON */
+  { 0x03ab, 0x0122 }, /*                    Gcedilla Ģ LATIN CAPITAL LETTER G WITH CEDILLA */
+  { 0x03ac, 0x0166 }, /*                      Tslash Ŧ LATIN CAPITAL LETTER T WITH STROKE */
+  { 0x03b3, 0x0157 }, /*                    rcedilla ŗ LATIN SMALL LETTER R WITH CEDILLA */
+  { 0x03b5, 0x0129 }, /*                      itilde ĩ LATIN SMALL LETTER I WITH TILDE */
+  { 0x03b6, 0x013c }, /*                    lcedilla ļ LATIN SMALL LETTER L WITH CEDILLA */
+  { 0x03ba, 0x0113 }, /*                     emacron ē LATIN SMALL LETTER E WITH MACRON */
+  { 0x03bb, 0x0123 }, /*                    gcedilla ģ LATIN SMALL LETTER G WITH CEDILLA */
+  { 0x03bc, 0x0167 }, /*                      tslash ŧ LATIN SMALL LETTER T WITH STROKE */
+  { 0x03bd, 0x014a }, /*                         ENG Ŋ LATIN CAPITAL LETTER ENG */
+  { 0x03bf, 0x014b }, /*                         eng ŋ LATIN SMALL LETTER ENG */
+  { 0x03c0, 0x0100 }, /*                     Amacron Ā LATIN CAPITAL LETTER A WITH MACRON */
+  { 0x03c7, 0x012e }, /*                     Iogonek Į LATIN CAPITAL LETTER I WITH OGONEK */
+  { 0x03cc, 0x0116 }, /*                   Eabovedot Ė LATIN CAPITAL LETTER E WITH DOT ABOVE */
+  { 0x03cf, 0x012a }, /*                     Imacron Ī LATIN CAPITAL LETTER I WITH MACRON */
+  { 0x03d1, 0x0145 }, /*                    Ncedilla Ņ LATIN CAPITAL LETTER N WITH CEDILLA */
+  { 0x03d2, 0x014c }, /*                     Omacron Ō LATIN CAPITAL LETTER O WITH MACRON */
+  { 0x03d3, 0x0136 }, /*                    Kcedilla Ķ LATIN CAPITAL LETTER K WITH CEDILLA */
+  { 0x03d9, 0x0172 }, /*                     Uogonek Ų LATIN CAPITAL LETTER U WITH OGONEK */
+  { 0x03dd, 0x0168 }, /*                      Utilde Ũ LATIN CAPITAL LETTER U WITH TILDE */
+  { 0x03de, 0x016a }, /*                     Umacron Ū LATIN CAPITAL LETTER U WITH MACRON */
+  { 0x03e0, 0x0101 }, /*                     amacron ā LATIN SMALL LETTER A WITH MACRON */
+  { 0x03e7, 0x012f }, /*                     iogonek į LATIN SMALL LETTER I WITH OGONEK */
+  { 0x03ec, 0x0117 }, /*                   eabovedot ė LATIN SMALL LETTER E WITH DOT ABOVE */
+  { 0x03ef, 0x012b }, /*                     imacron ī LATIN SMALL LETTER I WITH MACRON */
+  { 0x03f1, 0x0146 }, /*                    ncedilla ņ LATIN SMALL LETTER N WITH CEDILLA */
+  { 0x03f2, 0x014d }, /*                     omacron ō LATIN SMALL LETTER O WITH MACRON */
+  { 0x03f3, 0x0137 }, /*                    kcedilla ķ LATIN SMALL LETTER K WITH CEDILLA */
+  { 0x03f9, 0x0173 }, /*                     uogonek ų LATIN SMALL LETTER U WITH OGONEK */
+  { 0x03fd, 0x0169 }, /*                      utilde ũ LATIN SMALL LETTER U WITH TILDE */
+  { 0x03fe, 0x016b }, /*                     umacron ū LATIN SMALL LETTER U WITH MACRON */
+  { 0x047e, 0x203e }, /*                    overline ‾ OVERLINE */
+  { 0x04a1, 0x3002 }, /*               kana_fullstop 。 IDEOGRAPHIC FULL STOP */
+  { 0x04a2, 0x300c }, /*         kana_openingbracket 「 LEFT CORNER BRACKET */
+  { 0x04a3, 0x300d }, /*         kana_closingbracket 」 RIGHT CORNER BRACKET */
+  { 0x04a4, 0x3001 }, /*                  kana_comma 、 IDEOGRAPHIC COMMA */
+  { 0x04a5, 0x30fb }, /*            kana_conjunctive ・ KATAKANA MIDDLE DOT */
+  { 0x04a6, 0x30f2 }, /*                     kana_WO ヲ KATAKANA LETTER WO */
+  { 0x04a7, 0x30a1 }, /*                      kana_a ァ KATAKANA LETTER SMALL A */
+  { 0x04a8, 0x30a3 }, /*                      kana_i ィ KATAKANA LETTER SMALL I */
+  { 0x04a9, 0x30a5 }, /*                      kana_u ゥ KATAKANA LETTER SMALL U */
+  { 0x04aa, 0x30a7 }, /*                      kana_e ェ KATAKANA LETTER SMALL E */
+  { 0x04ab, 0x30a9 }, /*                      kana_o ォ KATAKANA LETTER SMALL O */
+  { 0x04ac, 0x30e3 }, /*                     kana_ya ャ KATAKANA LETTER SMALL YA */
+  { 0x04ad, 0x30e5 }, /*                     kana_yu ュ KATAKANA LETTER SMALL YU */
+  { 0x04ae, 0x30e7 }, /*                     kana_yo ョ KATAKANA LETTER SMALL YO */
+  { 0x04af, 0x30c3 }, /*                    kana_tsu ッ KATAKANA LETTER SMALL TU */
+  { 0x04b0, 0x30fc }, /*              prolongedsound ー KATAKANA-HIRAGANA PROLONGED SOUND MARK */
+  { 0x04b1, 0x30a2 }, /*                      kana_A ア KATAKANA LETTER A */
+  { 0x04b2, 0x30a4 }, /*                      kana_I イ KATAKANA LETTER I */
+  { 0x04b3, 0x30a6 }, /*                      kana_U ウ KATAKANA LETTER U */
+  { 0x04b4, 0x30a8 }, /*                      kana_E エ KATAKANA LETTER E */
+  { 0x04b5, 0x30aa }, /*                      kana_O オ KATAKANA LETTER O */
+  { 0x04b6, 0x30ab }, /*                     kana_KA カ KATAKANA LETTER KA */
+  { 0x04b7, 0x30ad }, /*                     kana_KI キ KATAKANA LETTER KI */
+  { 0x04b8, 0x30af }, /*                     kana_KU ク KATAKANA LETTER KU */
+  { 0x04b9, 0x30b1 }, /*                     kana_KE ケ KATAKANA LETTER KE */
+  { 0x04ba, 0x30b3 }, /*                     kana_KO コ KATAKANA LETTER KO */
+  { 0x04bb, 0x30b5 }, /*                     kana_SA サ KATAKANA LETTER SA */
+  { 0x04bc, 0x30b7 }, /*                    kana_SHI シ KATAKANA LETTER SI */
+  { 0x04bd, 0x30b9 }, /*                     kana_SU ス KATAKANA LETTER SU */
+  { 0x04be, 0x30bb }, /*                     kana_SE セ KATAKANA LETTER SE */
+  { 0x04bf, 0x30bd }, /*                     kana_SO ソ KATAKANA LETTER SO */
+  { 0x04c0, 0x30bf }, /*                     kana_TA タ KATAKANA LETTER TA */
+  { 0x04c1, 0x30c1 }, /*                    kana_CHI チ KATAKANA LETTER TI */
+  { 0x04c2, 0x30c4 }, /*                    kana_TSU ツ KATAKANA LETTER TU */
+  { 0x04c3, 0x30c6 }, /*                     kana_TE テ KATAKANA LETTER TE */
+  { 0x04c4, 0x30c8 }, /*                     kana_TO ト KATAKANA LETTER TO */
+  { 0x04c5, 0x30ca }, /*                     kana_NA ナ KATAKANA LETTER NA */
+  { 0x04c6, 0x30cb }, /*                     kana_NI ニ KATAKANA LETTER NI */
+  { 0x04c7, 0x30cc }, /*                     kana_NU ヌ KATAKANA LETTER NU */
+  { 0x04c8, 0x30cd }, /*                     kana_NE ネ KATAKANA LETTER NE */
+  { 0x04c9, 0x30ce }, /*                     kana_NO ノ KATAKANA LETTER NO */
+  { 0x04ca, 0x30cf }, /*                     kana_HA ハ KATAKANA LETTER HA */
+  { 0x04cb, 0x30d2 }, /*                     kana_HI ヒ KATAKANA LETTER HI */
+  { 0x04cc, 0x30d5 }, /*                     kana_FU フ KATAKANA LETTER HU */
+  { 0x04cd, 0x30d8 }, /*                     kana_HE ヘ KATAKANA LETTER HE */
+  { 0x04ce, 0x30db }, /*                     kana_HO ホ KATAKANA LETTER HO */
+  { 0x04cf, 0x30de }, /*                     kana_MA マ KATAKANA LETTER MA */
+  { 0x04d0, 0x30df }, /*                     kana_MI ミ KATAKANA LETTER MI */
+  { 0x04d1, 0x30e0 }, /*                     kana_MU ム KATAKANA LETTER MU */
+  { 0x04d2, 0x30e1 }, /*                     kana_ME メ KATAKANA LETTER ME */
+  { 0x04d3, 0x30e2 }, /*                     kana_MO モ KATAKANA LETTER MO */
+  { 0x04d4, 0x30e4 }, /*                     kana_YA ヤ KATAKANA LETTER YA */
+  { 0x04d5, 0x30e6 }, /*                     kana_YU ユ KATAKANA LETTER YU */
+  { 0x04d6, 0x30e8 }, /*                     kana_YO ヨ KATAKANA LETTER YO */
+  { 0x04d7, 0x30e9 }, /*                     kana_RA ラ KATAKANA LETTER RA */
+  { 0x04d8, 0x30ea }, /*                     kana_RI リ KATAKANA LETTER RI */
+  { 0x04d9, 0x30eb }, /*                     kana_RU ル KATAKANA LETTER RU */
+  { 0x04da, 0x30ec }, /*                     kana_RE レ KATAKANA LETTER RE */
+  { 0x04db, 0x30ed }, /*                     kana_RO ロ KATAKANA LETTER RO */
+  { 0x04dc, 0x30ef }, /*                     kana_WA ワ KATAKANA LETTER WA */
+  { 0x04dd, 0x30f3 }, /*                      kana_N ン KATAKANA LETTER N */
+  { 0x04de, 0x309b }, /*                 voicedsound ゛ KATAKANA-HIRAGANA VOICED SOUND MARK */
+  { 0x04df, 0x309c }, /*             semivoicedsound ゜ KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK */
+  { 0x05ac, 0x060c }, /*                Arabic_comma ، ARABIC COMMA */
+  { 0x05bb, 0x061b }, /*            Arabic_semicolon ؛ ARABIC SEMICOLON */
+  { 0x05bf, 0x061f }, /*        Arabic_question_mark ؟ ARABIC QUESTION MARK */
+  { 0x05c1, 0x0621 }, /*                Arabic_hamza ء ARABIC LETTER HAMZA */
+  { 0x05c2, 0x0622 }, /*          Arabic_maddaonalef آ ARABIC LETTER ALEF WITH MADDA ABOVE */
+  { 0x05c3, 0x0623 }, /*          Arabic_hamzaonalef أ ARABIC LETTER ALEF WITH HAMZA ABOVE */
+  { 0x05c4, 0x0624 }, /*           Arabic_hamzaonwaw ؤ ARABIC LETTER WAW WITH HAMZA ABOVE */
+  { 0x05c5, 0x0625 }, /*       Arabic_hamzaunderalef إ ARABIC LETTER ALEF WITH HAMZA BELOW */
+  { 0x05c6, 0x0626 }, /*           Arabic_hamzaonyeh ئ ARABIC LETTER YEH WITH HAMZA ABOVE */
+  { 0x05c7, 0x0627 }, /*                 Arabic_alef ا ARABIC LETTER ALEF */
+  { 0x05c8, 0x0628 }, /*                  Arabic_beh ب ARABIC LETTER BEH */
+  { 0x05c9, 0x0629 }, /*           Arabic_tehmarbuta ة ARABIC LETTER TEH MARBUTA */
+  { 0x05ca, 0x062a }, /*                  Arabic_teh ت ARABIC LETTER TEH */
+  { 0x05cb, 0x062b }, /*                 Arabic_theh ث ARABIC LETTER THEH */
+  { 0x05cc, 0x062c }, /*                 Arabic_jeem ج ARABIC LETTER JEEM */
+  { 0x05cd, 0x062d }, /*                  Arabic_hah ح ARABIC LETTER HAH */
+  { 0x05ce, 0x062e }, /*                 Arabic_khah خ ARABIC LETTER KHAH */
+  { 0x05cf, 0x062f }, /*                  Arabic_dal د ARABIC LETTER DAL */
+  { 0x05d0, 0x0630 }, /*                 Arabic_thal ذ ARABIC LETTER THAL */
+  { 0x05d1, 0x0631 }, /*                   Arabic_ra ر ARABIC LETTER REH */
+  { 0x05d2, 0x0632 }, /*                 Arabic_zain ز ARABIC LETTER ZAIN */
+  { 0x05d3, 0x0633 }, /*                 Arabic_seen س ARABIC LETTER SEEN */
+  { 0x05d4, 0x0634 }, /*                Arabic_sheen ش ARABIC LETTER SHEEN */
+  { 0x05d5, 0x0635 }, /*                  Arabic_sad ص ARABIC LETTER SAD */
+  { 0x05d6, 0x0636 }, /*                  Arabic_dad ض ARABIC LETTER DAD */
+  { 0x05d7, 0x0637 }, /*                  Arabic_tah ط ARABIC LETTER TAH */
+  { 0x05d8, 0x0638 }, /*                  Arabic_zah ظ ARABIC LETTER ZAH */
+  { 0x05d9, 0x0639 }, /*                  Arabic_ain ع ARABIC LETTER AIN */
+  { 0x05da, 0x063a }, /*                Arabic_ghain غ ARABIC LETTER GHAIN */
+  { 0x05e0, 0x0640 }, /*              Arabic_tatweel ـ ARABIC TATWEEL */
+  { 0x05e1, 0x0641 }, /*                  Arabic_feh ف ARABIC LETTER FEH */
+  { 0x05e2, 0x0642 }, /*                  Arabic_qaf ق ARABIC LETTER QAF */
+  { 0x05e3, 0x0643 }, /*                  Arabic_kaf ك ARABIC LETTER KAF */
+  { 0x05e4, 0x0644 }, /*                  Arabic_lam ل ARABIC LETTER LAM */
+  { 0x05e5, 0x0645 }, /*                 Arabic_meem م ARABIC LETTER MEEM */
+  { 0x05e6, 0x0646 }, /*                 Arabic_noon ن ARABIC LETTER NOON */
+  { 0x05e7, 0x0647 }, /*                   Arabic_ha ه ARABIC LETTER HEH */
+  { 0x05e8, 0x0648 }, /*                  Arabic_waw و ARABIC LETTER WAW */
+  { 0x05e9, 0x0649 }, /*          Arabic_alefmaksura ى ARABIC LETTER ALEF MAKSURA */
+  { 0x05ea, 0x064a }, /*                  Arabic_yeh ي ARABIC LETTER YEH */
+  { 0x05eb, 0x064b }, /*             Arabic_fathatan ً ARABIC FATHATAN */
+  { 0x05ec, 0x064c }, /*             Arabic_dammatan ٌ ARABIC DAMMATAN */
+  { 0x05ed, 0x064d }, /*             Arabic_kasratan ٍ ARABIC KASRATAN */
+  { 0x05ee, 0x064e }, /*                Arabic_fatha َ ARABIC FATHA */
+  { 0x05ef, 0x064f }, /*                Arabic_damma ُ ARABIC DAMMA */
+  { 0x05f0, 0x0650 }, /*                Arabic_kasra ِ ARABIC KASRA */
+  { 0x05f1, 0x0651 }, /*               Arabic_shadda ّ ARABIC SHADDA */
+  { 0x05f2, 0x0652 }, /*                Arabic_sukun ْ ARABIC SUKUN */
+  { 0x06a1, 0x0452 }, /*                 Serbian_dje ђ CYRILLIC SMALL LETTER DJE */
+  { 0x06a2, 0x0453 }, /*               Macedonia_gje ѓ CYRILLIC SMALL LETTER GJE */
+  { 0x06a3, 0x0451 }, /*                 Cyrillic_io ё CYRILLIC SMALL LETTER IO */
+  { 0x06a4, 0x0454 }, /*                Ukrainian_ie є CYRILLIC SMALL LETTER UKRAINIAN IE */
+  { 0x06a5, 0x0455 }, /*               Macedonia_dse ѕ CYRILLIC SMALL LETTER DZE */
+  { 0x06a6, 0x0456 }, /*                 Ukrainian_i і CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I */
+  { 0x06a7, 0x0457 }, /*                Ukrainian_yi ї CYRILLIC SMALL LETTER YI */
+  { 0x06a8, 0x0458 }, /*                 Cyrillic_je ј CYRILLIC SMALL LETTER JE */
+  { 0x06a9, 0x0459 }, /*                Cyrillic_lje љ CYRILLIC SMALL LETTER LJE */
+  { 0x06aa, 0x045a }, /*                Cyrillic_nje њ CYRILLIC SMALL LETTER NJE */
+  { 0x06ab, 0x045b }, /*                Serbian_tshe ћ CYRILLIC SMALL LETTER TSHE */
+  { 0x06ac, 0x045c }, /*               Macedonia_kje ќ CYRILLIC SMALL LETTER KJE */
+  { 0x06ae, 0x045e }, /*         Byelorussian_shortu ў CYRILLIC SMALL LETTER SHORT U */
+  { 0x06af, 0x045f }, /*               Cyrillic_dzhe џ CYRILLIC SMALL LETTER DZHE */
+  { 0x06b0, 0x2116 }, /*                  numerosign № NUMERO SIGN */
+  { 0x06b1, 0x0402 }, /*                 Serbian_DJE Ђ CYRILLIC CAPITAL LETTER DJE */
+  { 0x06b2, 0x0403 }, /*               Macedonia_GJE Ѓ CYRILLIC CAPITAL LETTER GJE */
+  { 0x06b3, 0x0401 }, /*                 Cyrillic_IO Ё CYRILLIC CAPITAL LETTER IO */
+  { 0x06b4, 0x0404 }, /*                Ukrainian_IE Є CYRILLIC CAPITAL LETTER UKRAINIAN IE */
+  { 0x06b5, 0x0405 }, /*               Macedonia_DSE Ѕ CYRILLIC CAPITAL LETTER DZE */
+  { 0x06b6, 0x0406 }, /*                 Ukrainian_I І CYRILLIC CAPITAL LETTER BYELORUSSIAN-UKRAINIAN I */
+  { 0x06b7, 0x0407 }, /*                Ukrainian_YI Ї CYRILLIC CAPITAL LETTER YI */
+  { 0x06b8, 0x0408 }, /*                 Cyrillic_JE Ј CYRILLIC CAPITAL LETTER JE */
+  { 0x06b9, 0x0409 }, /*                Cyrillic_LJE Љ CYRILLIC CAPITAL LETTER LJE */
+  { 0x06ba, 0x040a }, /*                Cyrillic_NJE Њ CYRILLIC CAPITAL LETTER NJE */
+  { 0x06bb, 0x040b }, /*                Serbian_TSHE Ћ CYRILLIC CAPITAL LETTER TSHE */
+  { 0x06bc, 0x040c }, /*               Macedonia_KJE Ќ CYRILLIC CAPITAL LETTER KJE */
+  { 0x06be, 0x040e }, /*         Byelorussian_SHORTU Ў CYRILLIC CAPITAL LETTER SHORT U */
+  { 0x06bf, 0x040f }, /*               Cyrillic_DZHE Џ CYRILLIC CAPITAL LETTER DZHE */
+  { 0x06c0, 0x044e }, /*                 Cyrillic_yu ю CYRILLIC SMALL LETTER YU */
+  { 0x06c1, 0x0430 }, /*                  Cyrillic_a а CYRILLIC SMALL LETTER A */
+  { 0x06c2, 0x0431 }, /*                 Cyrillic_be б CYRILLIC SMALL LETTER BE */
+  { 0x06c3, 0x0446 }, /*                Cyrillic_tse ц CYRILLIC SMALL LETTER TSE */
+  { 0x06c4, 0x0434 }, /*                 Cyrillic_de д CYRILLIC SMALL LETTER DE */
+  { 0x06c5, 0x0435 }, /*                 Cyrillic_ie е CYRILLIC SMALL LETTER IE */
+  { 0x06c6, 0x0444 }, /*                 Cyrillic_ef ф CYRILLIC SMALL LETTER EF */
+  { 0x06c7, 0x0433 }, /*                Cyrillic_ghe г CYRILLIC SMALL LETTER GHE */
+  { 0x06c8, 0x0445 }, /*                 Cyrillic_ha х CYRILLIC SMALL LETTER HA */
+  { 0x06c9, 0x0438 }, /*                  Cyrillic_i и CYRILLIC SMALL LETTER I */
+  { 0x06ca, 0x0439 }, /*             Cyrillic_shorti й CYRILLIC SMALL LETTER SHORT I */
+  { 0x06cb, 0x043a }, /*                 Cyrillic_ka к CYRILLIC SMALL LETTER KA */
+  { 0x06cc, 0x043b }, /*                 Cyrillic_el л CYRILLIC SMALL LETTER EL */
+  { 0x06cd, 0x043c }, /*                 Cyrillic_em м CYRILLIC SMALL LETTER EM */
+  { 0x06ce, 0x043d }, /*                 Cyrillic_en н CYRILLIC SMALL LETTER EN */
+  { 0x06cf, 0x043e }, /*                  Cyrillic_o о CYRILLIC SMALL LETTER O */
+  { 0x06d0, 0x043f }, /*                 Cyrillic_pe п CYRILLIC SMALL LETTER PE */
+  { 0x06d1, 0x044f }, /*                 Cyrillic_ya я CYRILLIC SMALL LETTER YA */
+  { 0x06d2, 0x0440 }, /*                 Cyrillic_er р CYRILLIC SMALL LETTER ER */
+  { 0x06d3, 0x0441 }, /*                 Cyrillic_es с CYRILLIC SMALL LETTER ES */
+  { 0x06d4, 0x0442 }, /*                 Cyrillic_te т CYRILLIC SMALL LETTER TE */
+  { 0x06d5, 0x0443 }, /*                  Cyrillic_u у CYRILLIC SMALL LETTER U */
+  { 0x06d6, 0x0436 }, /*                Cyrillic_zhe ж CYRILLIC SMALL LETTER ZHE */
+  { 0x06d7, 0x0432 }, /*                 Cyrillic_ve в CYRILLIC SMALL LETTER VE */
+  { 0x06d8, 0x044c }, /*           Cyrillic_softsign ь CYRILLIC SMALL LETTER SOFT SIGN */
+  { 0x06d9, 0x044b }, /*               Cyrillic_yeru ы CYRILLIC SMALL LETTER YERU */
+  { 0x06da, 0x0437 }, /*                 Cyrillic_ze з CYRILLIC SMALL LETTER ZE */
+  { 0x06db, 0x0448 }, /*                Cyrillic_sha ш CYRILLIC SMALL LETTER SHA */
+  { 0x06dc, 0x044d }, /*                  Cyrillic_e э CYRILLIC SMALL LETTER E */
+  { 0x06dd, 0x0449 }, /*              Cyrillic_shcha щ CYRILLIC SMALL LETTER SHCHA */
+  { 0x06de, 0x0447 }, /*                Cyrillic_che ч CYRILLIC SMALL LETTER CHE */
+  { 0x06df, 0x044a }, /*           Cyrillic_hardsign ъ CYRILLIC SMALL LETTER HARD SIGN */
+  { 0x06e0, 0x042e }, /*                 Cyrillic_YU Ю CYRILLIC CAPITAL LETTER YU */
+  { 0x06e1, 0x0410 }, /*                  Cyrillic_A А CYRILLIC CAPITAL LETTER A */
+  { 0x06e2, 0x0411 }, /*                 Cyrillic_BE Б CYRILLIC CAPITAL LETTER BE */
+  { 0x06e3, 0x0426 }, /*                Cyrillic_TSE Ц CYRILLIC CAPITAL LETTER TSE */
+  { 0x06e4, 0x0414 }, /*                 Cyrillic_DE Д CYRILLIC CAPITAL LETTER DE */
+  { 0x06e5, 0x0415 }, /*                 Cyrillic_IE Е CYRILLIC CAPITAL LETTER IE */
+  { 0x06e6, 0x0424 }, /*                 Cyrillic_EF Ф CYRILLIC CAPITAL LETTER EF */
+  { 0x06e7, 0x0413 }, /*                Cyrillic_GHE Г CYRILLIC CAPITAL LETTER GHE */
+  { 0x06e8, 0x0425 }, /*                 Cyrillic_HA Х CYRILLIC CAPITAL LETTER HA */
+  { 0x06e9, 0x0418 }, /*                  Cyrillic_I И CYRILLIC CAPITAL LETTER I */
+  { 0x06ea, 0x0419 }, /*             Cyrillic_SHORTI Й CYRILLIC CAPITAL LETTER SHORT I */
+  { 0x06eb, 0x041a }, /*                 Cyrillic_KA К CYRILLIC CAPITAL LETTER KA */
+  { 0x06ec, 0x041b }, /*                 Cyrillic_EL Л CYRILLIC CAPITAL LETTER EL */
+  { 0x06ed, 0x041c }, /*                 Cyrillic_EM М CYRILLIC CAPITAL LETTER EM */
+  { 0x06ee, 0x041d }, /*                 Cyrillic_EN Н CYRILLIC CAPITAL LETTER EN */
+  { 0x06ef, 0x041e }, /*                  Cyrillic_O О CYRILLIC CAPITAL LETTER O */
+  { 0x06f0, 0x041f }, /*                 Cyrillic_PE П CYRILLIC CAPITAL LETTER PE */
+  { 0x06f1, 0x042f }, /*                 Cyrillic_YA Я CYRILLIC CAPITAL LETTER YA */
+  { 0x06f2, 0x0420 }, /*                 Cyrillic_ER Р CYRILLIC CAPITAL LETTER ER */
+  { 0x06f3, 0x0421 }, /*                 Cyrillic_ES С CYRILLIC CAPITAL LETTER ES */
+  { 0x06f4, 0x0422 }, /*                 Cyrillic_TE Т CYRILLIC CAPITAL LETTER TE */
+  { 0x06f5, 0x0423 }, /*                  Cyrillic_U У CYRILLIC CAPITAL LETTER U */
+  { 0x06f6, 0x0416 }, /*                Cyrillic_ZHE Ж CYRILLIC CAPITAL LETTER ZHE */
+  { 0x06f7, 0x0412 }, /*                 Cyrillic_VE В CYRILLIC CAPITAL LETTER VE */
+  { 0x06f8, 0x042c }, /*           Cyrillic_SOFTSIGN Ь CYRILLIC CAPITAL LETTER SOFT SIGN */
+  { 0x06f9, 0x042b }, /*               Cyrillic_YERU Ы CYRILLIC CAPITAL LETTER YERU */
+  { 0x06fa, 0x0417 }, /*                 Cyrillic_ZE З CYRILLIC CAPITAL LETTER ZE */
+  { 0x06fb, 0x0428 }, /*                Cyrillic_SHA Ш CYRILLIC CAPITAL LETTER SHA */
+  { 0x06fc, 0x042d }, /*                  Cyrillic_E Э CYRILLIC CAPITAL LETTER E */
+  { 0x06fd, 0x0429 }, /*              Cyrillic_SHCHA Щ CYRILLIC CAPITAL LETTER SHCHA */
+  { 0x06fe, 0x0427 }, /*                Cyrillic_CHE Ч CYRILLIC CAPITAL LETTER CHE */
+  { 0x06ff, 0x042a }, /*           Cyrillic_HARDSIGN Ъ CYRILLIC CAPITAL LETTER HARD SIGN */
+  { 0x07a1, 0x0386 }, /*           Greek_ALPHAaccent Ά GREEK CAPITAL LETTER ALPHA WITH TONOS */
+  { 0x07a2, 0x0388 }, /*         Greek_EPSILONaccent Έ GREEK CAPITAL LETTER EPSILON WITH TONOS */
+  { 0x07a3, 0x0389 }, /*             Greek_ETAaccent Ή GREEK CAPITAL LETTER ETA WITH TONOS */
+  { 0x07a4, 0x038a }, /*            Greek_IOTAaccent Ί GREEK CAPITAL LETTER IOTA WITH TONOS */
+  { 0x07a5, 0x03aa }, /*         Greek_IOTAdiaeresis Ϊ GREEK CAPITAL LETTER IOTA WITH DIALYTIKA */
+  { 0x07a7, 0x038c }, /*         Greek_OMICRONaccent Ό GREEK CAPITAL LETTER OMICRON WITH TONOS */
+  { 0x07a8, 0x038e }, /*         Greek_UPSILONaccent Ύ GREEK CAPITAL LETTER UPSILON WITH TONOS */
+  { 0x07a9, 0x03ab }, /*       Greek_UPSILONdieresis Ϋ GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA */
+  { 0x07ab, 0x038f }, /*           Greek_OMEGAaccent Ώ GREEK CAPITAL LETTER OMEGA WITH TONOS */
+  { 0x07ae, 0x0385 }, /*        Greek_accentdieresis ΅ GREEK DIALYTIKA TONOS */
+  { 0x07af, 0x2015 }, /*              Greek_horizbar ― HORIZONTAL BAR */
+  { 0x07b1, 0x03ac }, /*           Greek_alphaaccent ά GREEK SMALL LETTER ALPHA WITH TONOS */
+  { 0x07b2, 0x03ad }, /*         Greek_epsilonaccent έ GREEK SMALL LETTER EPSILON WITH TONOS */
+  { 0x07b3, 0x03ae }, /*             Greek_etaaccent ή GREEK SMALL LETTER ETA WITH TONOS */
+  { 0x07b4, 0x03af }, /*            Greek_iotaaccent ί GREEK SMALL LETTER IOTA WITH TONOS */
+  { 0x07b5, 0x03ca }, /*          Greek_iotadieresis ϊ GREEK SMALL LETTER IOTA WITH DIALYTIKA */
+  { 0x07b6, 0x0390 }, /*    Greek_iotaaccentdieresis ΐ GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS */
+  { 0x07b7, 0x03cc }, /*         Greek_omicronaccent ό GREEK SMALL LETTER OMICRON WITH TONOS */
+  { 0x07b8, 0x03cd }, /*         Greek_upsilonaccent ύ GREEK SMALL LETTER UPSILON WITH TONOS */
+  { 0x07b9, 0x03cb }, /*       Greek_upsilondieresis ϋ GREEK SMALL LETTER UPSILON WITH DIALYTIKA */
+  { 0x07ba, 0x03b0 }, /* Greek_upsilonaccentdieresis ΰ GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS */
+  { 0x07bb, 0x03ce }, /*           Greek_omegaaccent ώ GREEK SMALL LETTER OMEGA WITH TONOS */
+  { 0x07c1, 0x0391 }, /*                 Greek_ALPHA Α GREEK CAPITAL LETTER ALPHA */
+  { 0x07c2, 0x0392 }, /*                  Greek_BETA Β GREEK CAPITAL LETTER BETA */
+  { 0x07c3, 0x0393 }, /*                 Greek_GAMMA Γ GREEK CAPITAL LETTER GAMMA */
+  { 0x07c4, 0x0394 }, /*                 Greek_DELTA Δ GREEK CAPITAL LETTER DELTA */
+  { 0x07c5, 0x0395 }, /*               Greek_EPSILON Ε GREEK CAPITAL LETTER EPSILON */
+  { 0x07c6, 0x0396 }, /*                  Greek_ZETA Ζ GREEK CAPITAL LETTER ZETA */
+  { 0x07c7, 0x0397 }, /*                   Greek_ETA Η GREEK CAPITAL LETTER ETA */
+  { 0x07c8, 0x0398 }, /*                 Greek_THETA Θ GREEK CAPITAL LETTER THETA */
+  { 0x07c9, 0x0399 }, /*                  Greek_IOTA Ι GREEK CAPITAL LETTER IOTA */
+  { 0x07ca, 0x039a }, /*                 Greek_KAPPA Κ GREEK CAPITAL LETTER KAPPA */
+  { 0x07cb, 0x039b }, /*                Greek_LAMBDA Λ GREEK CAPITAL LETTER LAMDA */
+  { 0x07cc, 0x039c }, /*                    Greek_MU Μ GREEK CAPITAL LETTER MU */
+  { 0x07cd, 0x039d }, /*                    Greek_NU Ν GREEK CAPITAL LETTER NU */
+  { 0x07ce, 0x039e }, /*                    Greek_XI Ξ GREEK CAPITAL LETTER XI */
+  { 0x07cf, 0x039f }, /*               Greek_OMICRON Ο GREEK CAPITAL LETTER OMICRON */
+  { 0x07d0, 0x03a0 }, /*                    Greek_PI Π GREEK CAPITAL LETTER PI */
+  { 0x07d1, 0x03a1 }, /*                   Greek_RHO Ρ GREEK CAPITAL LETTER RHO */
+  { 0x07d2, 0x03a3 }, /*                 Greek_SIGMA Σ GREEK CAPITAL LETTER SIGMA */
+  { 0x07d4, 0x03a4 }, /*                   Greek_TAU Τ GREEK CAPITAL LETTER TAU */
+  { 0x07d5, 0x03a5 }, /*               Greek_UPSILON Υ GREEK CAPITAL LETTER UPSILON */
+  { 0x07d6, 0x03a6 }, /*                   Greek_PHI Φ GREEK CAPITAL LETTER PHI */
+  { 0x07d7, 0x03a7 }, /*                   Greek_CHI Χ GREEK CAPITAL LETTER CHI */
+  { 0x07d8, 0x03a8 }, /*                   Greek_PSI Ψ GREEK CAPITAL LETTER PSI */
+  { 0x07d9, 0x03a9 }, /*                 Greek_OMEGA Ω GREEK CAPITAL LETTER OMEGA */
+  { 0x07e1, 0x03b1 }, /*                 Greek_alpha α GREEK SMALL LETTER ALPHA */
+  { 0x07e2, 0x03b2 }, /*                  Greek_beta β GREEK SMALL LETTER BETA */
+  { 0x07e3, 0x03b3 }, /*                 Greek_gamma γ GREEK SMALL LETTER GAMMA */
+  { 0x07e4, 0x03b4 }, /*                 Greek_delta δ GREEK SMALL LETTER DELTA */
+  { 0x07e5, 0x03b5 }, /*               Greek_epsilon ε GREEK SMALL LETTER EPSILON */
+  { 0x07e6, 0x03b6 }, /*                  Greek_zeta ζ GREEK SMALL LETTER ZETA */
+  { 0x07e7, 0x03b7 }, /*                   Greek_eta η GREEK SMALL LETTER ETA */
+  { 0x07e8, 0x03b8 }, /*                 Greek_theta θ GREEK SMALL LETTER THETA */
+  { 0x07e9, 0x03b9 }, /*                  Greek_iota ι GREEK SMALL LETTER IOTA */
+  { 0x07ea, 0x03ba }, /*                 Greek_kappa κ GREEK SMALL LETTER KAPPA */
+  { 0x07eb, 0x03bb }, /*                Greek_lambda λ GREEK SMALL LETTER LAMDA */
+  { 0x07ec, 0x03bc }, /*                    Greek_mu μ GREEK SMALL LETTER MU */
+  { 0x07ed, 0x03bd }, /*                    Greek_nu ν GREEK SMALL LETTER NU */
+  { 0x07ee, 0x03be }, /*                    Greek_xi ξ GREEK SMALL LETTER XI */
+  { 0x07ef, 0x03bf }, /*               Greek_omicron ο GREEK SMALL LETTER OMICRON */
+  { 0x07f0, 0x03c0 }, /*                    Greek_pi π GREEK SMALL LETTER PI */
+  { 0x07f1, 0x03c1 }, /*                   Greek_rho ρ GREEK SMALL LETTER RHO */
+  { 0x07f2, 0x03c3 }, /*                 Greek_sigma σ GREEK SMALL LETTER SIGMA */
+  { 0x07f3, 0x03c2 }, /*       Greek_finalsmallsigma ς GREEK SMALL LETTER FINAL SIGMA */
+  { 0x07f4, 0x03c4 }, /*                   Greek_tau τ GREEK SMALL LETTER TAU */
+  { 0x07f5, 0x03c5 }, /*               Greek_upsilon υ GREEK SMALL LETTER UPSILON */
+  { 0x07f6, 0x03c6 }, /*                   Greek_phi φ GREEK SMALL LETTER PHI */
+  { 0x07f7, 0x03c7 }, /*                   Greek_chi χ GREEK SMALL LETTER CHI */
+  { 0x07f8, 0x03c8 }, /*                   Greek_psi ψ GREEK SMALL LETTER PSI */
+  { 0x07f9, 0x03c9 }, /*                 Greek_omega ω GREEK SMALL LETTER OMEGA */
+  { 0x08a1, 0x23b7 }, /*                 leftradical ⎷ ??? */
+  { 0x08a2, 0x250c }, /*              topleftradical ┌ BOX DRAWINGS LIGHT DOWN AND RIGHT */
+  { 0x08a3, 0x2500 }, /*              horizconnector ─ BOX DRAWINGS LIGHT HORIZONTAL */
+  { 0x08a4, 0x2320 }, /*                 topintegral ⌠ TOP HALF INTEGRAL */
+  { 0x08a5, 0x2321 }, /*                 botintegral ⌡ BOTTOM HALF INTEGRAL */
+  { 0x08a6, 0x2502 }, /*               vertconnector │ BOX DRAWINGS LIGHT VERTICAL */
+  { 0x08a7, 0x23a1 }, /*            topleftsqbracket ⎡ ??? */
+  { 0x08a8, 0x23a3 }, /*            botleftsqbracket ⎣ ??? */
+  { 0x08a9, 0x23a4 }, /*           toprightsqbracket ⎤ ??? */
+  { 0x08aa, 0x23a6 }, /*           botrightsqbracket ⎦ ??? */
+  { 0x08ab, 0x239b }, /*               topleftparens ⎛ ??? */
+  { 0x08ac, 0x239d }, /*               botleftparens ⎝ ??? */
+  { 0x08ad, 0x239e }, /*              toprightparens ⎞ ??? */
+  { 0x08ae, 0x23a0 }, /*              botrightparens ⎠ ??? */
+  { 0x08af, 0x23a8 }, /*        leftmiddlecurlybrace ⎨ ??? */
+  { 0x08b0, 0x23ac }, /*       rightmiddlecurlybrace ⎬ ??? */
+/*  0x08b1                          topleftsummation ? ??? */
+/*  0x08b2                          botleftsummation ? ??? */
+/*  0x08b3                 topvertsummationconnector ? ??? */
+/*  0x08b4                 botvertsummationconnector ? ??? */
+/*  0x08b5                         toprightsummation ? ??? */
+/*  0x08b6                         botrightsummation ? ??? */
+/*  0x08b7                      rightmiddlesummation ? ??? */
+  { 0x08bc, 0x2264 }, /*               lessthanequal ≤ LESS-THAN OR EQUAL TO */
+  { 0x08bd, 0x2260 }, /*                    notequal ≠ NOT EQUAL TO */
+  { 0x08be, 0x2265 }, /*            greaterthanequal ≥ GREATER-THAN OR EQUAL TO */
+  { 0x08bf, 0x222b }, /*                    integral ∫ INTEGRAL */
+  { 0x08c0, 0x2234 }, /*                   therefore ∴ THEREFORE */
+  { 0x08c1, 0x221d }, /*                   variation ∝ PROPORTIONAL TO */
+  { 0x08c2, 0x221e }, /*                    infinity ∞ INFINITY */
+  { 0x08c5, 0x2207 }, /*                       nabla ∇ NABLA */
+  { 0x08c8, 0x223c }, /*                 approximate ∼ TILDE OPERATOR */
+  { 0x08c9, 0x2243 }, /*                similarequal ≃ ASYMPTOTICALLY EQUAL TO */
+  { 0x08cd, 0x21d4 }, /*                    ifonlyif ⇔ LEFT RIGHT DOUBLE ARROW */
+  { 0x08ce, 0x21d2 }, /*                     implies ⇒ RIGHTWARDS DOUBLE ARROW */
+  { 0x08cf, 0x2261 }, /*                   identical ≡ IDENTICAL TO */
+  { 0x08d6, 0x221a }, /*                     radical √ SQUARE ROOT */
+  { 0x08da, 0x2282 }, /*                  includedin ⊂ SUBSET OF */
+  { 0x08db, 0x2283 }, /*                    includes ⊃ SUPERSET OF */
+  { 0x08dc, 0x2229 }, /*                intersection ∩ INTERSECTION */
+  { 0x08dd, 0x222a }, /*                       union ∪ UNION */
+  { 0x08de, 0x2227 }, /*                  logicaland ∧ LOGICAL AND */
+  { 0x08df, 0x2228 }, /*                   logicalor ∨ LOGICAL OR */
+  { 0x08ef, 0x2202 }, /*           partialderivative ∂ PARTIAL DIFFERENTIAL */
+  { 0x08f6, 0x0192 }, /*                    function ƒ LATIN SMALL LETTER F WITH HOOK */
+  { 0x08fb, 0x2190 }, /*                   leftarrow ← LEFTWARDS ARROW */
+  { 0x08fc, 0x2191 }, /*                     uparrow ↑ UPWARDS ARROW */
+  { 0x08fd, 0x2192 }, /*                  rightarrow → RIGHTWARDS ARROW */
+  { 0x08fe, 0x2193 }, /*                   downarrow ↓ DOWNWARDS ARROW */
+/*  0x09df                                     blank ? ??? */
+  { 0x09e0, 0x25c6 }, /*                soliddiamond ◆ BLACK DIAMOND */
+  { 0x09e1, 0x2592 }, /*                checkerboard ▒ MEDIUM SHADE */
+  { 0x09e2, 0x2409 }, /*                          ht ␉ SYMBOL FOR HORIZONTAL TABULATION */
+  { 0x09e3, 0x240c }, /*                          ff ␌ SYMBOL FOR FORM FEED */
+  { 0x09e4, 0x240d }, /*                          cr ␍ SYMBOL FOR CARRIAGE RETURN */
+  { 0x09e5, 0x240a }, /*                          lf ␊ SYMBOL FOR LINE FEED */
+  { 0x09e8, 0x2424 }, /*                          nl ␤ SYMBOL FOR NEWLINE */
+  { 0x09e9, 0x240b }, /*                          vt ␋ SYMBOL FOR VERTICAL TABULATION */
+  { 0x09ea, 0x2518 }, /*              lowrightcorner ┘ BOX DRAWINGS LIGHT UP AND LEFT */
+  { 0x09eb, 0x2510 }, /*               uprightcorner ┐ BOX DRAWINGS LIGHT DOWN AND LEFT */
+  { 0x09ec, 0x250c }, /*                upleftcorner ┌ BOX DRAWINGS LIGHT DOWN AND RIGHT */
+  { 0x09ed, 0x2514 }, /*               lowleftcorner └ BOX DRAWINGS LIGHT UP AND RIGHT */
+  { 0x09ee, 0x253c }, /*               crossinglines ┼ BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL */
+  { 0x09ef, 0x23ba }, /*              horizlinescan1 ⎺ HORIZONTAL SCAN LINE-1 (Unicode 3.2 draft) */
+  { 0x09f0, 0x23bb }, /*              horizlinescan3 ⎻ HORIZONTAL SCAN LINE-3 (Unicode 3.2 draft) */
+  { 0x09f1, 0x2500 }, /*              horizlinescan5 ─ BOX DRAWINGS LIGHT HORIZONTAL */
+  { 0x09f2, 0x23bc }, /*              horizlinescan7 ⎼ HORIZONTAL SCAN LINE-7 (Unicode 3.2 draft) */
+  { 0x09f3, 0x23bd }, /*              horizlinescan9 ⎽ HORIZONTAL SCAN LINE-9 (Unicode 3.2 draft) */
+  { 0x09f4, 0x251c }, /*                       leftt ├ BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
+  { 0x09f5, 0x2524 }, /*                      rightt ┤ BOX DRAWINGS LIGHT VERTICAL AND LEFT */
+  { 0x09f6, 0x2534 }, /*                        bott ┴ BOX DRAWINGS LIGHT UP AND HORIZONTAL */
+  { 0x09f7, 0x252c }, /*                        topt ┬ BOX DRAWINGS LIGHT DOWN AND HORIZONTAL */
+  { 0x09f8, 0x2502 }, /*                     vertbar │ BOX DRAWINGS LIGHT VERTICAL */
+  { 0x0aa1, 0x2003 }, /*                     emspace   EM SPACE */
+  { 0x0aa2, 0x2002 }, /*                     enspace   EN SPACE */
+  { 0x0aa3, 0x2004 }, /*                    em3space   THREE-PER-EM SPACE */
+  { 0x0aa4, 0x2005 }, /*                    em4space   FOUR-PER-EM SPACE */
+  { 0x0aa5, 0x2007 }, /*                  digitspace   FIGURE SPACE */
+  { 0x0aa6, 0x2008 }, /*                  punctspace   PUNCTUATION SPACE */
+  { 0x0aa7, 0x2009 }, /*                   thinspace   THIN SPACE */
+  { 0x0aa8, 0x200a }, /*                   hairspace   HAIR SPACE */
+  { 0x0aa9, 0x2014 }, /*                      emdash — EM DASH */
+  { 0x0aaa, 0x2013 }, /*                      endash – EN DASH */
+/*  0x0aac                               signifblank ? ??? */
+  { 0x0aae, 0x2026 }, /*                    ellipsis … HORIZONTAL ELLIPSIS */
+  { 0x0aaf, 0x2025 }, /*             doubbaselinedot ‥ TWO DOT LEADER */
+  { 0x0ab0, 0x2153 }, /*                    onethird ⅓ VULGAR FRACTION ONE THIRD */
+  { 0x0ab1, 0x2154 }, /*                   twothirds ⅔ VULGAR FRACTION TWO THIRDS */
+  { 0x0ab2, 0x2155 }, /*                    onefifth ⅕ VULGAR FRACTION ONE FIFTH */
+  { 0x0ab3, 0x2156 }, /*                   twofifths ⅖ VULGAR FRACTION TWO FIFTHS */
+  { 0x0ab4, 0x2157 }, /*                 threefifths ⅗ VULGAR FRACTION THREE FIFTHS */
+  { 0x0ab5, 0x2158 }, /*                  fourfifths ⅘ VULGAR FRACTION FOUR FIFTHS */
+  { 0x0ab6, 0x2159 }, /*                    onesixth ⅙ VULGAR FRACTION ONE SIXTH */
+  { 0x0ab7, 0x215a }, /*                  fivesixths ⅚ VULGAR FRACTION FIVE SIXTHS */
+  { 0x0ab8, 0x2105 }, /*                      careof ℅ CARE OF */
+  { 0x0abb, 0x2012 }, /*                     figdash ‒ FIGURE DASH */
+  { 0x0abc, 0x2329 }, /*            leftanglebracket 〈 LEFT-POINTING ANGLE BRACKET */
+/*  0x0abd                              decimalpoint ? ??? */
+  { 0x0abe, 0x232a }, /*           rightanglebracket 〉 RIGHT-POINTING ANGLE BRACKET */
+/*  0x0abf                                    marker ? ??? */
+  { 0x0ac3, 0x215b }, /*                   oneeighth ⅛ VULGAR FRACTION ONE EIGHTH */
+  { 0x0ac4, 0x215c }, /*                threeeighths ⅜ VULGAR FRACTION THREE EIGHTHS */
+  { 0x0ac5, 0x215d }, /*                 fiveeighths ⅝ VULGAR FRACTION FIVE EIGHTHS */
+  { 0x0ac6, 0x215e }, /*                seveneighths ⅞ VULGAR FRACTION SEVEN EIGHTHS */
+  { 0x0ac9, 0x2122 }, /*                   trademark ™ TRADE MARK SIGN */
+  { 0x0aca, 0x2613 }, /*               signaturemark ☓ SALTIRE */
+/*  0x0acb                         trademarkincircle ? ??? */
+  { 0x0acc, 0x25c1 }, /*            leftopentriangle ◁ WHITE LEFT-POINTING TRIANGLE */
+  { 0x0acd, 0x25b7 }, /*           rightopentriangle ▷ WHITE RIGHT-POINTING TRIANGLE */
+  { 0x0ace, 0x25cb }, /*                emopencircle ○ WHITE CIRCLE */
+  { 0x0acf, 0x25af }, /*             emopenrectangle ▯ WHITE VERTICAL RECTANGLE */
+  { 0x0ad0, 0x2018 }, /*         leftsinglequotemark ‘ LEFT SINGLE QUOTATION MARK */
+  { 0x0ad1, 0x2019 }, /*        rightsinglequotemark ’ RIGHT SINGLE QUOTATION MARK */
+  { 0x0ad2, 0x201c }, /*         leftdoublequotemark “ LEFT DOUBLE QUOTATION MARK */
+  { 0x0ad3, 0x201d }, /*        rightdoublequotemark ” RIGHT DOUBLE QUOTATION MARK */
+  { 0x0ad4, 0x211e }, /*                prescription ℞ PRESCRIPTION TAKE */
+  { 0x0ad6, 0x2032 }, /*                     minutes ′ PRIME */
+  { 0x0ad7, 0x2033 }, /*                     seconds ″ DOUBLE PRIME */
+  { 0x0ad9, 0x271d }, /*                  latincross ✝ LATIN CROSS */
+/*  0x0ada                                  hexagram ? ??? */
+  { 0x0adb, 0x25ac }, /*            filledrectbullet ▬ BLACK RECTANGLE */
+  { 0x0adc, 0x25c0 }, /*         filledlefttribullet ◀ BLACK LEFT-POINTING TRIANGLE */
+  { 0x0add, 0x25b6 }, /*        filledrighttribullet ▶ BLACK RIGHT-POINTING TRIANGLE */
+  { 0x0ade, 0x25cf }, /*              emfilledcircle ● BLACK CIRCLE */
+  { 0x0adf, 0x25ae }, /*                emfilledrect ▮ BLACK VERTICAL RECTANGLE */
+  { 0x0ae0, 0x25e6 }, /*            enopencircbullet ◦ WHITE BULLET */
+  { 0x0ae1, 0x25ab }, /*          enopensquarebullet ▫ WHITE SMALL SQUARE */
+  { 0x0ae2, 0x25ad }, /*              openrectbullet ▭ WHITE RECTANGLE */
+  { 0x0ae3, 0x25b3 }, /*             opentribulletup △ WHITE UP-POINTING TRIANGLE */
+  { 0x0ae4, 0x25bd }, /*           opentribulletdown ▽ WHITE DOWN-POINTING TRIANGLE */
+  { 0x0ae5, 0x2606 }, /*                    openstar ☆ WHITE STAR */
+  { 0x0ae6, 0x2022 }, /*          enfilledcircbullet • BULLET */
+  { 0x0ae7, 0x25aa }, /*            enfilledsqbullet ▪ BLACK SMALL SQUARE */
+  { 0x0ae8, 0x25b2 }, /*           filledtribulletup ▲ BLACK UP-POINTING TRIANGLE */
+  { 0x0ae9, 0x25bc }, /*         filledtribulletdown ▼ BLACK DOWN-POINTING TRIANGLE */
+  { 0x0aea, 0x261c }, /*                 leftpointer ☜ WHITE LEFT POINTING INDEX */
+  { 0x0aeb, 0x261e }, /*                rightpointer ☞ WHITE RIGHT POINTING INDEX */
+  { 0x0aec, 0x2663 }, /*                        club ♣ BLACK CLUB SUIT */
+  { 0x0aed, 0x2666 }, /*                     diamond ♦ BLACK DIAMOND SUIT */
+  { 0x0aee, 0x2665 }, /*                       heart ♥ BLACK HEART SUIT */
+  { 0x0af0, 0x2720 }, /*                maltesecross ✠ MALTESE CROSS */
+  { 0x0af1, 0x2020 }, /*                      dagger † DAGGER */
+  { 0x0af2, 0x2021 }, /*                doubledagger ‡ DOUBLE DAGGER */
+  { 0x0af3, 0x2713 }, /*                   checkmark ✓ CHECK MARK */
+  { 0x0af4, 0x2717 }, /*                 ballotcross ✗ BALLOT X */
+  { 0x0af5, 0x266f }, /*                musicalsharp ♯ MUSIC SHARP SIGN */
+  { 0x0af6, 0x266d }, /*                 musicalflat ♭ MUSIC FLAT SIGN */
+  { 0x0af7, 0x2642 }, /*                  malesymbol ♂ MALE SIGN */
+  { 0x0af8, 0x2640 }, /*                femalesymbol ♀ FEMALE SIGN */
+  { 0x0af9, 0x260e }, /*                   telephone ☎ BLACK TELEPHONE */
+  { 0x0afa, 0x2315 }, /*           telephonerecorder ⌕ TELEPHONE RECORDER */
+  { 0x0afb, 0x2117 }, /*         phonographcopyright ℗ SOUND RECORDING COPYRIGHT */
+  { 0x0afc, 0x2038 }, /*                       caret ‸ CARET */
+  { 0x0afd, 0x201a }, /*          singlelowquotemark ‚ SINGLE LOW-9 QUOTATION MARK */
+  { 0x0afe, 0x201e }, /*          doublelowquotemark „ DOUBLE LOW-9 QUOTATION MARK */
+/*  0x0aff                                    cursor ? ??? */
+  { 0x0ba3, 0x003c }, /*                   leftcaret < LESS-THAN SIGN */
+  { 0x0ba6, 0x003e }, /*                  rightcaret > GREATER-THAN SIGN */
+  { 0x0ba8, 0x2228 }, /*                   downcaret ∨ LOGICAL OR */
+  { 0x0ba9, 0x2227 }, /*                     upcaret ∧ LOGICAL AND */
+  { 0x0bc0, 0x00af }, /*                     overbar ¯ MACRON */
+  { 0x0bc2, 0x22a5 }, /*                    downtack ⊥ UP TACK */
+  { 0x0bc3, 0x2229 }, /*                      upshoe ∩ INTERSECTION */
+  { 0x0bc4, 0x230a }, /*                   downstile ⌊ LEFT FLOOR */
+  { 0x0bc6, 0x005f }, /*                    underbar _ LOW LINE */
+  { 0x0bca, 0x2218 }, /*                         jot ∘ RING OPERATOR */
+  { 0x0bcc, 0x2395 }, /*                        quad ⎕ APL FUNCTIONAL SYMBOL QUAD */
+  { 0x0bce, 0x22a4 }, /*                      uptack ⊤ DOWN TACK */
+  { 0x0bcf, 0x25cb }, /*                      circle ○ WHITE CIRCLE */
+  { 0x0bd3, 0x2308 }, /*                     upstile ⌈ LEFT CEILING */
+  { 0x0bd6, 0x222a }, /*                    downshoe ∪ UNION */
+  { 0x0bd8, 0x2283 }, /*                   rightshoe ⊃ SUPERSET OF */
+  { 0x0bda, 0x2282 }, /*                    leftshoe ⊂ SUBSET OF */
+  { 0x0bdc, 0x22a2 }, /*                    lefttack ⊢ RIGHT TACK */
+  { 0x0bfc, 0x22a3 }, /*                   righttack ⊣ LEFT TACK */
+  { 0x0cdf, 0x2017 }, /*        hebrew_doublelowline ‗ DOUBLE LOW LINE */
+  { 0x0ce0, 0x05d0 }, /*                hebrew_aleph א HEBREW LETTER ALEF */
+  { 0x0ce1, 0x05d1 }, /*                  hebrew_bet ב HEBREW LETTER BET */
+  { 0x0ce2, 0x05d2 }, /*                hebrew_gimel ג HEBREW LETTER GIMEL */
+  { 0x0ce3, 0x05d3 }, /*                hebrew_dalet ד HEBREW LETTER DALET */
+  { 0x0ce4, 0x05d4 }, /*                   hebrew_he ה HEBREW LETTER HE */
+  { 0x0ce5, 0x05d5 }, /*                  hebrew_waw ו HEBREW LETTER VAV */
+  { 0x0ce6, 0x05d6 }, /*                 hebrew_zain ז HEBREW LETTER ZAYIN */
+  { 0x0ce7, 0x05d7 }, /*                 hebrew_chet ח HEBREW LETTER HET */
+  { 0x0ce8, 0x05d8 }, /*                  hebrew_tet ט HEBREW LETTER TET */
+  { 0x0ce9, 0x05d9 }, /*                  hebrew_yod י HEBREW LETTER YOD */
+  { 0x0cea, 0x05da }, /*            hebrew_finalkaph ך HEBREW LETTER FINAL KAF */
+  { 0x0ceb, 0x05db }, /*                 hebrew_kaph כ HEBREW LETTER KAF */
+  { 0x0cec, 0x05dc }, /*                hebrew_lamed ל HEBREW LETTER LAMED */
+  { 0x0ced, 0x05dd }, /*             hebrew_finalmem ם HEBREW LETTER FINAL MEM */
+  { 0x0cee, 0x05de }, /*                  hebrew_mem מ HEBREW LETTER MEM */
+  { 0x0cef, 0x05df }, /*             hebrew_finalnun ן HEBREW LETTER FINAL NUN */
+  { 0x0cf0, 0x05e0 }, /*                  hebrew_nun נ HEBREW LETTER NUN */
+  { 0x0cf1, 0x05e1 }, /*               hebrew_samech ס HEBREW LETTER SAMEKH */
+  { 0x0cf2, 0x05e2 }, /*                 hebrew_ayin ע HEBREW LETTER AYIN */
+  { 0x0cf3, 0x05e3 }, /*              hebrew_finalpe ף HEBREW LETTER FINAL PE */
+  { 0x0cf4, 0x05e4 }, /*                   hebrew_pe פ HEBREW LETTER PE */
+  { 0x0cf5, 0x05e5 }, /*            hebrew_finalzade ץ HEBREW LETTER FINAL TSADI */
+  { 0x0cf6, 0x05e6 }, /*                 hebrew_zade צ HEBREW LETTER TSADI */
+  { 0x0cf7, 0x05e7 }, /*                 hebrew_qoph ק HEBREW LETTER QOF */
+  { 0x0cf8, 0x05e8 }, /*                 hebrew_resh ר HEBREW LETTER RESH */
+  { 0x0cf9, 0x05e9 }, /*                 hebrew_shin ש HEBREW LETTER SHIN */
+  { 0x0cfa, 0x05ea }, /*                  hebrew_taw ת HEBREW LETTER TAV */
+  { 0x0da1, 0x0e01 }, /*                  Thai_kokai ก THAI CHARACTER KO KAI */
+  { 0x0da2, 0x0e02 }, /*                Thai_khokhai ข THAI CHARACTER KHO KHAI */
+  { 0x0da3, 0x0e03 }, /*               Thai_khokhuat ฃ THAI CHARACTER KHO KHUAT */
+  { 0x0da4, 0x0e04 }, /*               Thai_khokhwai ค THAI CHARACTER KHO KHWAI */
+  { 0x0da5, 0x0e05 }, /*                Thai_khokhon ฅ THAI CHARACTER KHO KHON */
+  { 0x0da6, 0x0e06 }, /*             Thai_khorakhang ฆ THAI CHARACTER KHO RAKHANG */
+  { 0x0da7, 0x0e07 }, /*                 Thai_ngongu ง THAI CHARACTER NGO NGU */
+  { 0x0da8, 0x0e08 }, /*                Thai_chochan จ THAI CHARACTER CHO CHAN */
+  { 0x0da9, 0x0e09 }, /*               Thai_choching ฉ THAI CHARACTER CHO CHING */
+  { 0x0daa, 0x0e0a }, /*               Thai_chochang ช THAI CHARACTER CHO CHANG */
+  { 0x0dab, 0x0e0b }, /*                   Thai_soso ซ THAI CHARACTER SO SO */
+  { 0x0dac, 0x0e0c }, /*                Thai_chochoe ฌ THAI CHARACTER CHO CHOE */
+  { 0x0dad, 0x0e0d }, /*                 Thai_yoying ญ THAI CHARACTER YO YING */
+  { 0x0dae, 0x0e0e }, /*                Thai_dochada ฎ THAI CHARACTER DO CHADA */
+  { 0x0daf, 0x0e0f }, /*                Thai_topatak ฏ THAI CHARACTER TO PATAK */
+  { 0x0db0, 0x0e10 }, /*                Thai_thothan ฐ THAI CHARACTER THO THAN */
+  { 0x0db1, 0x0e11 }, /*          Thai_thonangmontho ฑ THAI CHARACTER THO NANGMONTHO */
+  { 0x0db2, 0x0e12 }, /*             Thai_thophuthao ฒ THAI CHARACTER THO PHUTHAO */
+  { 0x0db3, 0x0e13 }, /*                  Thai_nonen ณ THAI CHARACTER NO NEN */
+  { 0x0db4, 0x0e14 }, /*                  Thai_dodek ด THAI CHARACTER DO DEK */
+  { 0x0db5, 0x0e15 }, /*                  Thai_totao ต THAI CHARACTER TO TAO */
+  { 0x0db6, 0x0e16 }, /*               Thai_thothung ถ THAI CHARACTER THO THUNG */
+  { 0x0db7, 0x0e17 }, /*              Thai_thothahan ท THAI CHARACTER THO THAHAN */
+  { 0x0db8, 0x0e18 }, /*               Thai_thothong ธ THAI CHARACTER THO THONG */
+  { 0x0db9, 0x0e19 }, /*                   Thai_nonu น THAI CHARACTER NO NU */
+  { 0x0dba, 0x0e1a }, /*               Thai_bobaimai บ THAI CHARACTER BO BAIMAI */
+  { 0x0dbb, 0x0e1b }, /*                  Thai_popla ป THAI CHARACTER PO PLA */
+  { 0x0dbc, 0x0e1c }, /*               Thai_phophung ผ THAI CHARACTER PHO PHUNG */
+  { 0x0dbd, 0x0e1d }, /*                   Thai_fofa ฝ THAI CHARACTER FO FA */
+  { 0x0dbe, 0x0e1e }, /*                Thai_phophan พ THAI CHARACTER PHO PHAN */
+  { 0x0dbf, 0x0e1f }, /*                  Thai_fofan ฟ THAI CHARACTER FO FAN */
+  { 0x0dc0, 0x0e20 }, /*             Thai_phosamphao ภ THAI CHARACTER PHO SAMPHAO */
+  { 0x0dc1, 0x0e21 }, /*                   Thai_moma ม THAI CHARACTER MO MA */
+  { 0x0dc2, 0x0e22 }, /*                  Thai_yoyak ย THAI CHARACTER YO YAK */
+  { 0x0dc3, 0x0e23 }, /*                  Thai_rorua ร THAI CHARACTER RO RUA */
+  { 0x0dc4, 0x0e24 }, /*                     Thai_ru ฤ THAI CHARACTER RU */
+  { 0x0dc5, 0x0e25 }, /*                 Thai_loling ล THAI CHARACTER LO LING */
+  { 0x0dc6, 0x0e26 }, /*                     Thai_lu ฦ THAI CHARACTER LU */
+  { 0x0dc7, 0x0e27 }, /*                 Thai_wowaen ว THAI CHARACTER WO WAEN */
+  { 0x0dc8, 0x0e28 }, /*                 Thai_sosala ศ THAI CHARACTER SO SALA */
+  { 0x0dc9, 0x0e29 }, /*                 Thai_sorusi ษ THAI CHARACTER SO RUSI */
+  { 0x0dca, 0x0e2a }, /*                  Thai_sosua ส THAI CHARACTER SO SUA */
+  { 0x0dcb, 0x0e2b }, /*                  Thai_hohip ห THAI CHARACTER HO HIP */
+  { 0x0dcc, 0x0e2c }, /*                Thai_lochula ฬ THAI CHARACTER LO CHULA */
+  { 0x0dcd, 0x0e2d }, /*                   Thai_oang อ THAI CHARACTER O ANG */
+  { 0x0dce, 0x0e2e }, /*               Thai_honokhuk ฮ THAI CHARACTER HO NOKHUK */
+  { 0x0dcf, 0x0e2f }, /*              Thai_paiyannoi ฯ THAI CHARACTER PAIYANNOI */
+  { 0x0dd0, 0x0e30 }, /*                  Thai_saraa ะ THAI CHARACTER SARA A */
+  { 0x0dd1, 0x0e31 }, /*             Thai_maihanakat ั THAI CHARACTER MAI HAN-AKAT */
+  { 0x0dd2, 0x0e32 }, /*                 Thai_saraaa า THAI CHARACTER SARA AA */
+  { 0x0dd3, 0x0e33 }, /*                 Thai_saraam ำ THAI CHARACTER SARA AM */
+  { 0x0dd4, 0x0e34 }, /*                  Thai_sarai ิ THAI CHARACTER SARA I */
+  { 0x0dd5, 0x0e35 }, /*                 Thai_saraii ี THAI CHARACTER SARA II */
+  { 0x0dd6, 0x0e36 }, /*                 Thai_saraue ึ THAI CHARACTER SARA UE */
+  { 0x0dd7, 0x0e37 }, /*                Thai_sarauee ื THAI CHARACTER SARA UEE */
+  { 0x0dd8, 0x0e38 }, /*                  Thai_sarau ุ THAI CHARACTER SARA U */
+  { 0x0dd9, 0x0e39 }, /*                 Thai_sarauu ู THAI CHARACTER SARA UU */
+  { 0x0dda, 0x0e3a }, /*                Thai_phinthu ฺ THAI CHARACTER PHINTHU */
+/*  0x0dde                    Thai_maihanakat_maitho ? ??? */
+  { 0x0ddf, 0x0e3f }, /*                   Thai_baht ฿ THAI CURRENCY SYMBOL BAHT */
+  { 0x0de0, 0x0e40 }, /*                  Thai_sarae เ THAI CHARACTER SARA E */
+  { 0x0de1, 0x0e41 }, /*                 Thai_saraae แ THAI CHARACTER SARA AE */
+  { 0x0de2, 0x0e42 }, /*                  Thai_sarao โ THAI CHARACTER SARA O */
+  { 0x0de3, 0x0e43 }, /*          Thai_saraaimaimuan ใ THAI CHARACTER SARA AI MAIMUAN */
+  { 0x0de4, 0x0e44 }, /*         Thai_saraaimaimalai ไ THAI CHARACTER SARA AI MAIMALAI */
+  { 0x0de5, 0x0e45 }, /*            Thai_lakkhangyao ๅ THAI CHARACTER LAKKHANGYAO */
+  { 0x0de6, 0x0e46 }, /*               Thai_maiyamok ๆ THAI CHARACTER MAIYAMOK */
+  { 0x0de7, 0x0e47 }, /*              Thai_maitaikhu ็ THAI CHARACTER MAITAIKHU */
+  { 0x0de8, 0x0e48 }, /*                  Thai_maiek ่ THAI CHARACTER MAI EK */
+  { 0x0de9, 0x0e49 }, /*                 Thai_maitho ้ THAI CHARACTER MAI THO */
+  { 0x0dea, 0x0e4a }, /*                 Thai_maitri ๊ THAI CHARACTER MAI TRI */
+  { 0x0deb, 0x0e4b }, /*            Thai_maichattawa ๋ THAI CHARACTER MAI CHATTAWA */
+  { 0x0dec, 0x0e4c }, /*            Thai_thanthakhat ์ THAI CHARACTER THANTHAKHAT */
+  { 0x0ded, 0x0e4d }, /*               Thai_nikhahit ํ THAI CHARACTER NIKHAHIT */
+  { 0x0df0, 0x0e50 }, /*                 Thai_leksun ๐ THAI DIGIT ZERO */
+  { 0x0df1, 0x0e51 }, /*                Thai_leknung ๑ THAI DIGIT ONE */
+  { 0x0df2, 0x0e52 }, /*                Thai_leksong ๒ THAI DIGIT TWO */
+  { 0x0df3, 0x0e53 }, /*                 Thai_leksam ๓ THAI DIGIT THREE */
+  { 0x0df4, 0x0e54 }, /*                  Thai_leksi ๔ THAI DIGIT FOUR */
+  { 0x0df5, 0x0e55 }, /*                  Thai_lekha ๕ THAI DIGIT FIVE */
+  { 0x0df6, 0x0e56 }, /*                 Thai_lekhok ๖ THAI DIGIT SIX */
+  { 0x0df7, 0x0e57 }, /*                Thai_lekchet ๗ THAI DIGIT SEVEN */
+  { 0x0df8, 0x0e58 }, /*                Thai_lekpaet ๘ THAI DIGIT EIGHT */
+  { 0x0df9, 0x0e59 }, /*                 Thai_lekkao ๙ THAI DIGIT NINE */
+  { 0x0ea1, 0x3131 }, /*               Hangul_Kiyeog ㄱ HANGUL LETTER KIYEOK */
+  { 0x0ea2, 0x3132 }, /*          Hangul_SsangKiyeog ㄲ HANGUL LETTER SSANGKIYEOK */
+  { 0x0ea3, 0x3133 }, /*           Hangul_KiyeogSios ㄳ HANGUL LETTER KIYEOK-SIOS */
+  { 0x0ea4, 0x3134 }, /*                Hangul_Nieun ㄴ HANGUL LETTER NIEUN */
+  { 0x0ea5, 0x3135 }, /*           Hangul_NieunJieuj ㄵ HANGUL LETTER NIEUN-CIEUC */
+  { 0x0ea6, 0x3136 }, /*           Hangul_NieunHieuh ㄶ HANGUL LETTER NIEUN-HIEUH */
+  { 0x0ea7, 0x3137 }, /*               Hangul_Dikeud ㄷ HANGUL LETTER TIKEUT */
+  { 0x0ea8, 0x3138 }, /*          Hangul_SsangDikeud ㄸ HANGUL LETTER SSANGTIKEUT */
+  { 0x0ea9, 0x3139 }, /*                Hangul_Rieul ㄹ HANGUL LETTER RIEUL */
+  { 0x0eaa, 0x313a }, /*          Hangul_RieulKiyeog ㄺ HANGUL LETTER RIEUL-KIYEOK */
+  { 0x0eab, 0x313b }, /*           Hangul_RieulMieum ㄻ HANGUL LETTER RIEUL-MIEUM */
+  { 0x0eac, 0x313c }, /*           Hangul_RieulPieub ㄼ HANGUL LETTER RIEUL-PIEUP */
+  { 0x0ead, 0x313d }, /*            Hangul_RieulSios ㄽ HANGUL LETTER RIEUL-SIOS */
+  { 0x0eae, 0x313e }, /*           Hangul_RieulTieut ㄾ HANGUL LETTER RIEUL-THIEUTH */
+  { 0x0eaf, 0x313f }, /*          Hangul_RieulPhieuf ㄿ HANGUL LETTER RIEUL-PHIEUPH */
+  { 0x0eb0, 0x3140 }, /*           Hangul_RieulHieuh ㅀ HANGUL LETTER RIEUL-HIEUH */
+  { 0x0eb1, 0x3141 }, /*                Hangul_Mieum ㅁ HANGUL LETTER MIEUM */
+  { 0x0eb2, 0x3142 }, /*                Hangul_Pieub ㅂ HANGUL LETTER PIEUP */
+  { 0x0eb3, 0x3143 }, /*           Hangul_SsangPieub ㅃ HANGUL LETTER SSANGPIEUP */
+  { 0x0eb4, 0x3144 }, /*            Hangul_PieubSios ㅄ HANGUL LETTER PIEUP-SIOS */
+  { 0x0eb5, 0x3145 }, /*                 Hangul_Sios ㅅ HANGUL LETTER SIOS */
+  { 0x0eb6, 0x3146 }, /*            Hangul_SsangSios ㅆ HANGUL LETTER SSANGSIOS */
+  { 0x0eb7, 0x3147 }, /*                Hangul_Ieung ㅇ HANGUL LETTER IEUNG */
+  { 0x0eb8, 0x3148 }, /*                Hangul_Jieuj ㅈ HANGUL LETTER CIEUC */
+  { 0x0eb9, 0x3149 }, /*           Hangul_SsangJieuj ㅉ HANGUL LETTER SSANGCIEUC */
+  { 0x0eba, 0x314a }, /*                Hangul_Cieuc ㅊ HANGUL LETTER CHIEUCH */
+  { 0x0ebb, 0x314b }, /*               Hangul_Khieuq ㅋ HANGUL LETTER KHIEUKH */
+  { 0x0ebc, 0x314c }, /*                Hangul_Tieut ㅌ HANGUL LETTER THIEUTH */
+  { 0x0ebd, 0x314d }, /*               Hangul_Phieuf ㅍ HANGUL LETTER PHIEUPH */
+  { 0x0ebe, 0x314e }, /*                Hangul_Hieuh ㅎ HANGUL LETTER HIEUH */
+  { 0x0ebf, 0x314f }, /*                    Hangul_A ㅏ HANGUL LETTER A */
+  { 0x0ec0, 0x3150 }, /*                   Hangul_AE ㅐ HANGUL LETTER AE */
+  { 0x0ec1, 0x3151 }, /*                   Hangul_YA ㅑ HANGUL LETTER YA */
+  { 0x0ec2, 0x3152 }, /*                  Hangul_YAE ㅒ HANGUL LETTER YAE */
+  { 0x0ec3, 0x3153 }, /*                   Hangul_EO ㅓ HANGUL LETTER EO */
+  { 0x0ec4, 0x3154 }, /*                    Hangul_E ㅔ HANGUL LETTER E */
+  { 0x0ec5, 0x3155 }, /*                  Hangul_YEO ㅕ HANGUL LETTER YEO */
+  { 0x0ec6, 0x3156 }, /*                   Hangul_YE ㅖ HANGUL LETTER YE */
+  { 0x0ec7, 0x3157 }, /*                    Hangul_O ㅗ HANGUL LETTER O */
+  { 0x0ec8, 0x3158 }, /*                   Hangul_WA ㅘ HANGUL LETTER WA */
+  { 0x0ec9, 0x3159 }, /*                  Hangul_WAE ㅙ HANGUL LETTER WAE */
+  { 0x0eca, 0x315a }, /*                   Hangul_OE ㅚ HANGUL LETTER OE */
+  { 0x0ecb, 0x315b }, /*                   Hangul_YO ㅛ HANGUL LETTER YO */
+  { 0x0ecc, 0x315c }, /*                    Hangul_U ㅜ HANGUL LETTER U */
+  { 0x0ecd, 0x315d }, /*                  Hangul_WEO ㅝ HANGUL LETTER WEO */
+  { 0x0ece, 0x315e }, /*                   Hangul_WE ㅞ HANGUL LETTER WE */
+  { 0x0ecf, 0x315f }, /*                   Hangul_WI ㅟ HANGUL LETTER WI */
+  { 0x0ed0, 0x3160 }, /*                   Hangul_YU ㅠ HANGUL LETTER YU */
+  { 0x0ed1, 0x3161 }, /*                   Hangul_EU ㅡ HANGUL LETTER EU */
+  { 0x0ed2, 0x3162 }, /*                   Hangul_YI ㅢ HANGUL LETTER YI */
+  { 0x0ed3, 0x3163 }, /*                    Hangul_I ㅣ HANGUL LETTER I */
+  { 0x0ed4, 0x11a8 }, /*             Hangul_J_Kiyeog ᆨ HANGUL JONGSEONG KIYEOK */
+  { 0x0ed5, 0x11a9 }, /*        Hangul_J_SsangKiyeog ᆩ HANGUL JONGSEONG SSANGKIYEOK */
+  { 0x0ed6, 0x11aa }, /*         Hangul_J_KiyeogSios ᆪ HANGUL JONGSEONG KIYEOK-SIOS */
+  { 0x0ed7, 0x11ab }, /*              Hangul_J_Nieun ᆫ HANGUL JONGSEONG NIEUN */
+  { 0x0ed8, 0x11ac }, /*         Hangul_J_NieunJieuj ᆬ HANGUL JONGSEONG NIEUN-CIEUC */
+  { 0x0ed9, 0x11ad }, /*         Hangul_J_NieunHieuh ᆭ HANGUL JONGSEONG NIEUN-HIEUH */
+  { 0x0eda, 0x11ae }, /*             Hangul_J_Dikeud ᆮ HANGUL JONGSEONG TIKEUT */
+  { 0x0edb, 0x11af }, /*              Hangul_J_Rieul ᆯ HANGUL JONGSEONG RIEUL */
+  { 0x0edc, 0x11b0 }, /*        Hangul_J_RieulKiyeog ᆰ HANGUL JONGSEONG RIEUL-KIYEOK */
+  { 0x0edd, 0x11b1 }, /*         Hangul_J_RieulMieum ᆱ HANGUL JONGSEONG RIEUL-MIEUM */
+  { 0x0ede, 0x11b2 }, /*         Hangul_J_RieulPieub ᆲ HANGUL JONGSEONG RIEUL-PIEUP */
+  { 0x0edf, 0x11b3 }, /*          Hangul_J_RieulSios ᆳ HANGUL JONGSEONG RIEUL-SIOS */
+  { 0x0ee0, 0x11b4 }, /*         Hangul_J_RieulTieut ᆴ HANGUL JONGSEONG RIEUL-THIEUTH */
+  { 0x0ee1, 0x11b5 }, /*        Hangul_J_RieulPhieuf ᆵ HANGUL JONGSEONG RIEUL-PHIEUPH */
+  { 0x0ee2, 0x11b6 }, /*         Hangul_J_RieulHieuh ᆶ HANGUL JONGSEONG RIEUL-HIEUH */
+  { 0x0ee3, 0x11b7 }, /*              Hangul_J_Mieum ᆷ HANGUL JONGSEONG MIEUM */
+  { 0x0ee4, 0x11b8 }, /*              Hangul_J_Pieub ᆸ HANGUL JONGSEONG PIEUP */
+  { 0x0ee5, 0x11b9 }, /*          Hangul_J_PieubSios ᆹ HANGUL JONGSEONG PIEUP-SIOS */
+  { 0x0ee6, 0x11ba }, /*               Hangul_J_Sios ᆺ HANGUL JONGSEONG SIOS */
+  { 0x0ee7, 0x11bb }, /*          Hangul_J_SsangSios ᆻ HANGUL JONGSEONG SSANGSIOS */
+  { 0x0ee8, 0x11bc }, /*              Hangul_J_Ieung ᆼ HANGUL JONGSEONG IEUNG */
+  { 0x0ee9, 0x11bd }, /*              Hangul_J_Jieuj ᆽ HANGUL JONGSEONG CIEUC */
+  { 0x0eea, 0x11be }, /*              Hangul_J_Cieuc ᆾ HANGUL JONGSEONG CHIEUCH */
+  { 0x0eeb, 0x11bf }, /*             Hangul_J_Khieuq ᆿ HANGUL JONGSEONG KHIEUKH */
+  { 0x0eec, 0x11c0 }, /*              Hangul_J_Tieut ᇀ HANGUL JONGSEONG THIEUTH */
+  { 0x0eed, 0x11c1 }, /*             Hangul_J_Phieuf ᇁ HANGUL JONGSEONG PHIEUPH */
+  { 0x0eee, 0x11c2 }, /*              Hangul_J_Hieuh ᇂ HANGUL JONGSEONG HIEUH */
+  { 0x0eef, 0x316d }, /*     Hangul_RieulYeorinHieuh ㅭ HANGUL LETTER RIEUL-YEORINHIEUH */
+  { 0x0ef0, 0x3171 }, /*    Hangul_SunkyeongeumMieum ㅱ HANGUL LETTER KAPYEOUNMIEUM */
+  { 0x0ef1, 0x3178 }, /*    Hangul_SunkyeongeumPieub ㅸ HANGUL LETTER KAPYEOUNPIEUP */
+  { 0x0ef2, 0x317f }, /*              Hangul_PanSios ㅿ HANGUL LETTER PANSIOS */
+  { 0x0ef3, 0x3181 }, /*    Hangul_KkogjiDalrinIeung ㆁ HANGUL LETTER YESIEUNG */
+  { 0x0ef4, 0x3184 }, /*   Hangul_SunkyeongeumPhieuf ㆄ HANGUL LETTER KAPYEOUNPHIEUPH */
+  { 0x0ef5, 0x3186 }, /*          Hangul_YeorinHieuh ㆆ HANGUL LETTER YEORINHIEUH */
+  { 0x0ef6, 0x318d }, /*                Hangul_AraeA ㆍ HANGUL LETTER ARAEA */
+  { 0x0ef7, 0x318e }, /*               Hangul_AraeAE ㆎ HANGUL LETTER ARAEAE */
+  { 0x0ef8, 0x11eb }, /*            Hangul_J_PanSios ᇫ HANGUL JONGSEONG PANSIOS */
+  { 0x0ef9, 0x11f0 }, /*  Hangul_J_KkogjiDalrinIeung ᇰ HANGUL JONGSEONG YESIEUNG */
+  { 0x0efa, 0x11f9 }, /*        Hangul_J_YeorinHieuh ᇹ HANGUL JONGSEONG YEORINHIEUH */
+  { 0x0eff, 0x20a9 }, /*                  Korean_Won ₩ WON SIGN */
+  { 0x13a4, 0x20ac }, /*                        Euro € EURO SIGN */
+  { 0x13bc, 0x0152 }, /*                          OE ΠLATIN CAPITAL LIGATURE OE */
+  { 0x13bd, 0x0153 }, /*                          oe œ LATIN SMALL LIGATURE OE */
+  { 0x13be, 0x0178 }, /*                  Ydiaeresis Ÿ LATIN CAPITAL LETTER Y WITH DIAERESIS */
+  { 0x20ac, 0x20ac }, /*                    EuroSign € EURO SIGN */
+};
+
+VISIBLE
+long _p9keysym2ucs(KeySym keysym)
+{
+    int min = 0;
+    int max = sizeof(keysymtab) / sizeof(struct codepair) - 1;
+    int mid;
+
+    /* first check for Latin-1 characters (1:1 mapping) */
+    if ((keysym >= 0x0020 && keysym <= 0x007e) ||
+        (keysym >= 0x00a0 && keysym <= 0x00ff))
+        return keysym;
+
+    /* also check for directly encoded 24-bit UCS characters */
+    if ((keysym & 0xff000000) == 0x01000000)
+	return keysym & 0x00ffffff;
+
+    /* binary search in table */
+    while (max >= min) {
+	mid = (min + max) / 2;
+	if (keysymtab[mid].keysym < keysym)
+	    min = mid + 1;
+	else if (keysymtab[mid].keysym > keysym)
+	    max = mid - 1;
+	else {
+	    /* found it */
+	    return keysymtab[mid].ucs;
+	}
+    }
+
+    /* no matching Unicode value found */
+    return -1;
+}
blob - /dev/null
blob + 77050e1b74c4008782d2e2642f8d9f7b8ba049af (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-keysym2ucs.h
@@ -0,0 +1,9 @@
+/* $XFree86: xc/programs/xterm/keysym2ucs.h,v 1.1 1999/06/12 15:37:18 dawes Exp $ */
+/*
+ * This module converts keysym values into the corresponding ISO 10646-1
+ * (UCS, Unicode) values.
+ */
+
+#include <X11/X.h>
+
+long _p9keysym2ucs(KeySym keysym);
blob - /dev/null
blob + a7446f37dda49dee73b4b595e685401fd333f08a (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-load.c
@@ -0,0 +1,18 @@
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include "x11-memdraw.h"
+
+int
+loadmemimage(Memimage *i, Rectangle r, uchar *data, int ndata)
+{
+	int n;
+
+	n = _loadmemimage(i, r, data, ndata);
+	if(n > 0 && i->X)
+		_xputxdata(i, r);
+	return n;
+}
+
blob - /dev/null
blob + eba9e0dbfa45afa9beaed5d4633807b621183e77 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-memdraw.h
@@ -0,0 +1,113 @@
+/*
+ * Structure pointed to by X field of Memimage
+ */
+
+typedef struct Xmem Xmem;
+typedef struct Xprivate Xprivate;
+
+enum
+{
+	PMundef = ~0
+};
+
+struct Xmem
+{
+	int		pixmap;	/* pixmap id */
+	XImage		*xi;	/* local image */
+	int		dirty;	/* is the X server ahead of us?  */
+	Rectangle	dirtyr;	/* which pixels? */
+	Rectangle	r;	/* size of image */
+};
+
+struct Xprivate {
+	u32int		chan;
+	XColormap	cmap;
+	XCursor		cursor;
+	XDisplay	*display;
+	int		fd;	/* of display */
+	int		depth;				/* of screen */
+	XDrawable	drawable;
+	XColor		map[256];
+	XColor		map7[128];
+	uchar		map7to8[128][2];
+	XGC		gccopy;
+	XGC		gccopy0;
+	XGC		gcfill;
+	u32int		gcfillcolor;
+	XGC		gcfill0;
+	u32int		gcfill0color;
+	XGC		gcreplsrc;
+	u32int		gcreplsrctile;
+	XGC		gcreplsrc0;
+	u32int		gcreplsrc0tile;
+	XGC		gcsimplesrc;
+	u32int		gcsimplesrccolor;
+	u32int		gcsimplesrcpixmap;
+	XGC		gcsimplesrc0;
+	u32int		gcsimplesrc0color;
+	u32int		gcsimplesrc0pixmap;
+	XGC		gczero;
+	u32int		gczeropixmap;
+	XGC		gczero0;
+	u32int		gczero0pixmap;
+	Rectangle	newscreenr;
+	Memimage*	screenimage;
+	QLock		screenlock;
+	XDrawable	screenpm;
+	XDrawable	nextscreenpm;
+	Rectangle	screenr;
+	int		toplan9[256];
+	int		tox11[256];
+	int		usetable;
+	XVisual		*vis;
+	Atom		clipboard;
+	Atom		utf8string;
+	Atom		targets;
+	Atom		text;
+	Atom		compoundtext;
+	Atom		takefocus;
+	Atom		losefocus;
+	Atom		wmprotos;
+	uint		putsnarf;
+	uint		assertsnarf;
+	int		destroyed;
+};
+
+extern Xprivate _x;
+
+extern Memimage *_xallocmemimage(Rectangle, u32int, int);
+extern XImage	*_xallocxdata(Memimage*, Rectangle);
+extern void	_xdirtyxdata(Memimage*, Rectangle);
+extern void	_xfillcolor(Memimage*, Rectangle, u32int);
+extern void	_xfreexdata(Memimage*);
+extern XImage	*_xgetxdata(Memimage*, Rectangle);
+extern void	_xputxdata(Memimage*, Rectangle);
+
+struct Mouse;
+extern int	_xtoplan9mouse(XEvent*, struct Mouse*);
+extern int	_xtoplan9kbd(XEvent*);
+extern void	_xexpose(XEvent*);
+extern int	_xselect(XEvent*);
+extern int	_xconfigure(XEvent*);
+extern int	_xdestroy(XEvent*);
+extern void	_flushmemscreen(Rectangle);
+extern void	_xmoveto(Point);
+struct Cursor;
+extern void	_xsetcursor(struct Cursor*);
+extern void	_xbouncemouse(Mouse*);
+extern int		_xsetlabel(char*);
+extern Memimage*	_xattach(char*, char*);
+extern char*		_xgetsnarf(void);
+extern void		_xputsnarf(char *data);
+extern void		_xtopwindow(void);
+extern void		_xresizewindow(Rectangle);
+extern int		_xreplacescreenimage(void);
+
+#define MouseMask (\
+	ButtonPressMask|\
+	ButtonReleaseMask|\
+	PointerMotionMask|\
+	Button1MotionMask|\
+	Button2MotionMask|\
+	Button3MotionMask)
+
blob - /dev/null
blob + 5cbdded84267122b86eac07b0ff296456c1691c1 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-pixelbits.c
@@ -0,0 +1,16 @@
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include "x11-memdraw.h"
+
+u32int
+pixelbits(Memimage *m, Point p)
+{
+	if(m->X)
+		_xgetxdata(m, Rect(p.x, p.y, p.x+1, p.y+1));
+	return _pixelbits(m, p);
+}
+
+
blob - /dev/null
blob + a5c5bf114078c7439cfd92210b2e3228059fde73 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-srv.c
@@ -0,0 +1,481 @@
+/*
+ * Window system protocol server.
+ * Use select and a single proc and single stack
+ * to avoid aggravating the X11 library, which is
+ * subtle and quick to anger.
+ */
+
+#include <u.h>
+#include <sys/select.h>
+#include <errno.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include <memlayer.h>
+#include <keyboard.h>
+#include <mouse.h>
+#include <cursor.h>
+#include <drawfcall.h>
+#include "x11-memdraw.h"
+#include "devdraw.h"
+
+#undef time
+
+#define MouseMask (\
+	ButtonPressMask|\
+	ButtonReleaseMask|\
+	PointerMotionMask|\
+	Button1MotionMask|\
+	Button2MotionMask|\
+	Button3MotionMask)
+
+#define Mask MouseMask|ExposureMask|StructureNotifyMask|KeyPressMask|EnterWindowMask|LeaveWindowMask
+
+typedef struct Kbdbuf Kbdbuf;
+typedef struct Mousebuf Mousebuf;
+typedef struct Fdbuf Fdbuf;
+typedef struct Tagbuf Tagbuf;
+
+struct Kbdbuf
+{
+	Rune r[32];
+	int ri;
+	int wi;
+	int stall;
+};
+
+struct Mousebuf
+{
+	Mouse m[32];
+	int resized[32];
+	int ri;
+	int wi;
+	int stall;
+};
+
+struct Tagbuf
+{
+	int t[32];
+	int ri;
+	int wi;
+};
+
+struct Fdbuf
+{
+	uchar buf[2*MAXWMSG];
+	uchar *rp;
+	uchar *wp;
+	uchar *ep;
+};
+
+Kbdbuf kbd;
+Mousebuf mouse;
+Fdbuf fdin;
+Fdbuf fdout;
+Tagbuf kbdtags;
+Tagbuf mousetags;
+Tagbuf resizetags;
+
+void fdslide(Fdbuf*);
+void runmsg(Wsysmsg*);
+void replymsg(Wsysmsg*);
+void runxevent(XEvent*);
+void matchkbd(void);
+void matchmouse(void);
+void matchresized(void);
+int fdnoblock(int);
+
+int chatty;
+
+void
+usage(void)
+{
+	fprint(2, "usage: devdraw (don't run  directly)\n");
+	exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+	int n, top, firstx;
+	fd_set rd, wr, xx;
+	Wsysmsg m;
+	XEvent event;
+
+	ARGBEGIN{
+	case 'D':
+		chatty++;
+		break;
+	default:
+		usage();
+	}ARGEND
+	
+	if(argc != 0)
+		usage();
+
+	fdin.rp = fdin.wp = fdin.buf;
+	fdin.ep = fdin.buf+sizeof fdin.buf;
+	
+	fdout.rp = fdout.wp = fdout.buf;
+	fdout.ep = fdout.buf+sizeof fdout.buf;
+
+	fdnoblock(0);
+	fdnoblock(1);
+
+	firstx = 1;
+	_x.fd = -1;
+	for(;;){
+		/* set up file descriptors */
+		FD_ZERO(&rd);
+		FD_ZERO(&wr);
+		FD_ZERO(&xx);
+		/*
+		 * Don't read unless there's room *and* we haven't
+		 * already filled the output buffer too much.
+		 */
+		if(fdout.wp < fdout.buf+MAXWMSG && fdin.wp < fdin.ep)
+			FD_SET(0, &rd);
+		if(fdout.wp > fdout.rp)
+			FD_SET(1, &wr);
+		FD_SET(0, &xx);
+		FD_SET(1, &xx);
+		top = 1;
+		if(_x.fd >= 0){
+			if(firstx){
+				firstx = 0;
+				XSelectInput(_x.display, _x.drawable, Mask);
+			}
+			FD_SET(_x.fd, &rd);
+			FD_SET(_x.fd, &xx);
+			XFlush(_x.display);
+			top = _x.fd;
+		}
+
+		if(chatty)
+			fprint(2, "select %d...\n", top+1);
+		/* wait for something to happen */
+		if(select(top+1, &rd, &wr, &xx, NULL) < 0){
+			if(chatty)
+				fprint(2, "select failure\n");
+			exits(0);
+		}
+		if(chatty)
+			fprint(2, "got select...\n");
+
+		{
+			/* read what we can */
+			n = 1;
+			while(fdin.wp < fdin.ep && (n = read(0, fdin.wp, fdin.ep-fdin.wp)) > 0)
+				fdin.wp += n;
+			if(n == 0){
+				if(chatty)
+					fprint(2, "eof\n");
+				exits(0);
+			}
+			if(n < 0 && errno != EAGAIN)
+				sysfatal("reading wsys msg: %r");
+
+			/* pick off messages one by one */
+			while((n = convM2W(fdin.rp, fdin.wp-fdin.rp, &m)) > 0){
+				runmsg(&m);
+				fdin.rp += n;
+			}
+			
+			/* slide data to beginning of buf */
+			fdslide(&fdin);
+		}
+		{
+			/* write what we can */
+			n = 1;
+			while(fdout.rp < fdout.wp && (n = write(1, fdout.rp, fdout.wp-fdout.rp)) > 0)
+				fdout.rp += n;
+			if(n == 0)
+				sysfatal("short write writing wsys");
+			if(n < 0 && errno != EAGAIN)
+				sysfatal("writing wsys msg: %r");
+
+			/* slide data to beginning of buf */
+			fdslide(&fdout);
+		}
+		{
+			/*
+			 * Read an X message if we can.
+			 * (XPending actually calls select to make sure
+			 * the display's fd is readable and then reads
+			 * in any waiting data before declaring whether
+			 * there are events on the queue.)
+			 */
+			while(XPending(_x.display)){
+				XNextEvent(_x.display, &event);
+				runxevent(&event);
+			}
+		}
+	}
+}
+
+int
+fdnoblock(int fd)
+{
+	return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
+}
+
+void
+fdslide(Fdbuf *fb)
+{
+	int n;
+
+	n = fb->wp - fb->rp;
+	if(n > 0)
+		memmove(fb->buf, fb->rp, n);
+	fb->rp = fb->buf;
+	fb->wp = fb->rp+n;
+}
+
+void
+replyerror(Wsysmsg *m)
+{
+	char err[256];
+	
+	rerrstr(err, sizeof err);
+	m->type = Rerror;
+	m->error = err;
+	replymsg(m);
+}
+
+/* 
+ * Handle a single wsysmsg. 
+ * Might queue for later (kbd, mouse read)
+ */
+void
+runmsg(Wsysmsg *m)
+{
+	uchar buf[65536];
+	int n;
+	Memimage *i;
+	
+	switch(m->type){
+	case Tinit:
+		memimageinit();
+		i = _xattach(m->label, m->winsize);
+		_initdisplaymemimage(i);
+		replymsg(m);
+		break;
+
+	case Trdmouse:
+		mousetags.t[mousetags.wi++] = m->tag;
+		if(mousetags.wi == nelem(mousetags.t))
+			mousetags.wi = 0;
+		if(mousetags.wi == mousetags.ri)
+			sysfatal("too many queued mouse reads");
+		mouse.stall = 0;
+		matchmouse();
+		break;
+
+	case Trdkbd:
+		kbdtags.t[kbdtags.wi++] = m->tag;
+		if(kbdtags.wi == nelem(kbdtags.t))
+			kbdtags.wi = 0;
+		if(kbdtags.wi == kbdtags.ri)
+			sysfatal("too many queued keyboard reads");
+		kbd.stall = 0;
+		matchkbd();
+		break;
+
+	case Tmoveto:
+		_xmoveto(m->mouse.xy);
+		replymsg(m);
+		break;
+
+	case Tcursor:
+		if(m->arrowcursor)
+			_xsetcursor(nil);
+		else
+			_xsetcursor(&m->cursor);
+		replymsg(m);
+		break;
+			
+	case Tbouncemouse:
+		_xbouncemouse(&m->mouse);
+		replymsg(m);
+		break;
+
+	case Tlabel:
+		_xsetlabel(m->label);
+		replymsg(m);
+		break;
+
+	case Trdsnarf:
+		m->snarf = _xgetsnarf();
+		replymsg(m);
+		free(m->snarf);
+		break;
+
+	case Twrsnarf:
+		_xputsnarf(m->snarf);
+		replymsg(m);
+		break;
+
+	case Trddraw:
+		n = m->count;
+		if(n > sizeof buf)
+			n = sizeof buf;
+		n = _drawmsgread(buf, n);
+		if(n < 0)
+			replyerror(m);
+		else{
+			m->count = n;
+			m->data = buf;
+			replymsg(m);
+		}
+		break;
+
+	case Twrdraw:
+		if(_drawmsgwrite(m->data, m->count) < 0)
+			replyerror(m);
+		else
+			replymsg(m);
+		break;
+	
+	case Ttop:
+		_xtopwindow();
+		replymsg(m);
+		break;
+	
+	case Tresize:
+		_xresizewindow(m->rect);
+		replymsg(m);
+		break;
+	}
+}
+
+/*
+ * Reply to m.
+ */
+void
+replymsg(Wsysmsg *m)
+{
+	int n;
+
+	/* T -> R msg */
+	if(m->type%2 == 0)
+		m->type++;
+		
+	/* copy to output buffer */
+	n = sizeW2M(m);
+	if(fdout.wp+n > fdout.ep)
+		sysfatal("out of space for reply message");
+	convW2M(m, fdout.wp, n);
+	fdout.wp += n;
+}
+
+/*
+ * Match queued kbd reads with queued kbd characters.
+ */
+void
+matchkbd(void)
+{
+	Wsysmsg m;
+	
+	if(kbd.stall)
+		return;
+	while(kbd.ri != kbd.wi && kbdtags.ri != kbdtags.wi){
+		m.type = Rrdkbd;
+		m.tag = kbdtags.t[kbdtags.ri++];
+		if(kbdtags.ri == nelem(kbdtags.t))
+			kbdtags.ri = 0;
+		m.rune = kbd.r[kbd.ri++];
+		if(kbd.ri == nelem(kbd.r))
+			kbd.ri = 0;
+		replymsg(&m);
+	}
+}
+
+/*
+ * Match queued mouse reads with queued mouse events.
+ */
+void
+matchmouse(void)
+{
+	Wsysmsg m;
+	
+	if(mouse.stall)
+		return;
+	while(mouse.ri != mouse.wi && mousetags.ri != mousetags.wi){
+		m.type = Rrdmouse;
+		m.tag = mousetags.t[mousetags.ri++];
+		if(mousetags.ri == nelem(mousetags.t))
+			mousetags.ri = 0;
+		m.mouse = mouse.m[mouse.ri];
+		m.resized = mouse.resized[mouse.ri];
+		mouse.ri++;
+		if(mouse.ri == nelem(mouse.m))
+			mouse.ri = 0;
+		replymsg(&m);
+	}
+}
+
+/*
+ * Handle an incoming X event.
+ */
+void
+runxevent(XEvent *xev)
+{
+	int c;
+	static Mouse m;
+
+	switch(xev->type){
+	case Expose:
+		_xexpose(xev);
+		break;
+	
+	case DestroyNotify:
+		if(_xdestroy(xev))
+			exits(0);
+		break;
+
+	case ConfigureNotify:
+		if(_xconfigure(xev)){
+			mouse.resized[mouse.wi] = 1;
+			_xreplacescreenimage();
+			goto addmouse;
+		}
+		break;
+
+	case ButtonPress:
+	case ButtonRelease:
+	case MotionNotify:
+		if(mouse.stall)
+			return;
+		if(_xtoplan9mouse(xev, &m) < 0)
+			return;
+		mouse.resized[mouse.wi] = 0;
+	addmouse:
+		mouse.m[mouse.wi] = m;
+		mouse.wi++;
+		if(mouse.wi == nelem(mouse.m))
+			mouse.wi = 0;
+		if(mouse.wi == mouse.ri)
+			mouse.stall = 1;
+		matchmouse();
+		break;
+	
+	case KeyPress:
+		if(kbd.stall)
+			return;
+		if((c = _xtoplan9kbd(xev)) < 0)
+			return;
+		kbd.r[kbd.wi++] = c;
+		if(kbd.wi == nelem(kbd.r))
+			kbd.wi = 0;
+		if(kbd.ri == kbd.wi)
+			kbd.stall = 1;
+		matchkbd();
+		break;
+	
+	case SelectionRequest:
+		_xselect(xev);
+		break;
+	}
+}
+
blob - /dev/null
blob + d01a232ff99e9d228100ff1724e918f3c2a7f52f (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-unload.c
@@ -0,0 +1,15 @@
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include "x11-memdraw.h"
+
+int
+unloadmemimage(Memimage *i, Rectangle r, uchar *data, int ndata)
+{
+	if(i->X)
+		_xgetxdata(i, r);
+	return _unloadmemimage(i, r, data, ndata);
+}
+
blob - /dev/null
blob + fcb40fdf764687f835b958fd64152f681e6a07c9 (mode 644)
--- /dev/null
+++ src/cmd/devdraw/x11-wsys.c
@@ -0,0 +1,29 @@
+#include <u.h>
+#include "x11-inc.h"
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+#include "x11-memdraw.h"
+
+void
+_xtopwindow(void)
+{
+	XMapRaised(_x.display, _x.drawable);
+	XSetInputFocus(_x.display, _x.drawable, RevertToPointerRoot,
+		CurrentTime);
+	XFlush(_x.display);
+}
+
+void
+_xresizewindow(Rectangle r)
+{
+	XWindowChanges e;
+	int value_mask;
+
+	memset(&e, 0, sizeof e);
+	value_mask = CWWidth|CWHeight;
+	e.width = Dx(r);
+	e.height = Dy(r);
+	XConfigureWindow(_x.display, _x.drawable, value_mask, &e);
+	XFlush(_x.display);
+}