~amavect/makeu

53a7c95855a2501c101a1fe730f9a85f05c2d9f0 — amavect 1 year, 6 months ago b9d0d46
replace keyboard.h with kbdproc

stdin by default reads /dev/cons
instead, get keyboard input from /dev/kbd
this frees up stdin for input
1 files changed, 77 insertions(+), 10 deletions(-)

M makeu.c
M makeu.c => makeu.c +77 -10
@@ 264,6 264,64 @@ alphaevent(double val)
	setpick();
}

void
stdinproc(void *aux)
{
	Channel *c;
	char in[11]; /* 0xRRGGBBAA\0 or 0xRRGGBBAA\n */
	ulong col;
	long n;
	
	c = aux;
	threadsetname("stdinproc");
	
	for(;;){
		n = read(0, in, sizeof(in));
		if(n <= 0)
			break;
		in[10] = 0;
		if(n != 0){
			
			fprint(2, "hello %ld %s\n", n, in);
		}
		col = strtocolor(in);
		if(col != DNotacolor)
			send(c, &col);
	}
	threadexits(nil);
}

void
kbdproc(void *aux)
{
	char buf[128];
	Channel *kbdc;
	int kfd;
	int n, i;
	Rune r;
	
	kbdc = aux;
	threadsetname("kbdproc");
	if((kfd = open("/dev/kbd", OREAD)) < 0)
		sysfatal("/dev/kbd: %r");
	
	for(;;){
		/* could be improved with detecting incomplete
		   runes toward the end of the buffer */
		n = read(kfd, buf, sizeof(buf)-1);
		if(n <= 0)
			break;
		buf[n] = 0;
		for(i = 0; i < n; i++){
			if(buf[i] != 'c')
				continue;
			if(chartorune(&r, buf+1) > 0 && r != Runeerror)
				send(kbdc, &r);
		}
	}

	threadexits(nil);
}

vlong startt, stopt;
#define STARTTIME() startt = nsec()


@@ 297,11 355,12 @@ usage(void)
void
threadmain(int argc, char **argv)
{
	Keyboardctl *kctl;
	Rune r;
	Mousectl *mctl;
	Mouse m;
	ulong incolor;
	Channel *stdinc;
	Channel *kbdc;
	
	incolor = 0xFFFFFFFF;
	ARGBEGIN{


@@ 323,8 382,11 @@ threadmain(int argc, char **argv)
		sysfatal("%r");
	if((mctl = initmouse(nil, screen)) == nil)
		sysfatal("%r");
	if((kctl = initkeyboard(nil)) == nil)
		sysfatal("%r");
	kbdc = chancreate(sizeof(Rune), 32);
	proccreate(kbdproc, kbdc, 0x2000);
	stdinc = chancreate(sizeof(ulong), 8);
	proccreate(stdinproc, stdinc, 0x2000);
	
	spa.mctl = mctl;
	hsva.mctl = mctl;
	


@@ 350,12 412,13 @@ threadmain(int argc, char **argv)
	root->resize(root, screenbuf->r);
	show();
	
	enum { MOUSE, RESIZE, KEYS, NONE };
	enum { MOUSE, RESIZE, KBDRUNE, STDIN, NONE };
	Alt alts[] = {
		[MOUSE] =  {mctl->c, &m, CHANRCV},
		[RESIZE] =  {mctl->resizec, nil, CHANRCV},
		[KEYS] = {kctl->c, &r, CHANRCV},
		[NONE] =  {nil, nil, CHANEND}
		[MOUSE]  = {mctl->c, &m, CHANRCV},
		[RESIZE] = {mctl->resizec, nil, CHANRCV},
		[KBDRUNE]   = {kbdc, &r, CHANRCV},
		[STDIN]  = {stdinc, &incolor, CHANRCV},
		[NONE]   = {nil, nil, CHANEND}
	};
	
	/* DEBUG frame counter */


@@ 363,8 426,9 @@ threadmain(int argc, char **argv)
	Rectangle rekt;
	ulong frames = 0;
	char ftext[10];
	*/
	STARTTIME();
	*/
	
	for(;;){
		/* DEBUG frame counter */
		/*


@@ 399,10 463,13 @@ threadmain(int argc, char **argv)
			root->resize(root, screenbuf->r);
			show();
			break;
		case KEYS:
		case KBDRUNE:
			if(r == Kdel)
				threadexitsall(nil);
			break;
		case STDIN:
			allevent(incolor);
			break;
		case NONE:
			print("I'm a woodchuck, not a woodchucker! (thanks for playing)\n");
			break;