~amavect/pal

12f948d747c2d5490b11e08dfcffe3bfdee3c779 — amavect 4 years ago e00c445
close when stdin closes
2 files changed, 71 insertions(+), 7 deletions(-)

M pal.c
M pal.man
M pal.c => pal.c +67 -5
@@ 245,6 245,69 @@ initpicker(void)
}

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);
}

Channel *
initkbd(void)
{
	Channel *c;
	c = chancreate(sizeof(Rune), 32);
	proccreate(kbdproc, c, 0x2000);
	return c;
}

void
stdinproc(void *)
{
	char in[11];
	long n;
	
	threadsetname("stdinproc");
	
	for(;;){
		n = read(0, in, sizeof(in));
		if(n <= 0)
			break;
	}
	threadexitsall(nil);
}

void
initstdin(void)
{
	proccreate(stdinproc, nil, 0x2000);
}

void
usage(void)
{
	fprint(2, "usage: %s [-c] [file]\n", argv0);


@@ 254,13 317,13 @@ usage(void)
void
threadmain(int argc, char **argv)
{
	Keyboardctl *kctl;
	Mousectl *mctl;
	Mouse m;
	Rune r;
	ulong col;
	ulong tcol;
	Channel *pc;
	Channel *kbdc;
	
	ARGBEGIN{
	case 'c':


@@ 294,9 357,8 @@ threadmain(int argc, char **argv)
	draw(tile, Rect(0,0,DX/4,DY/4), display->black, nil, ZP);
	draw(tile, Rect(DX/4,DY/4,DX/2,DY/2), display->black, nil, ZP);
	
	kctl = initkeyboard(nil);
	if(kctl == nil)
		error("no keyboard");
	initstdin();
	kbdc = initkbd();
	mctl = initmouse(nil, screen);
	if(mctl == nil)
		error("no mouse");


@@ 306,7 368,7 @@ threadmain(int argc, char **argv)
	Alt alts[] = {
		{mctl->c, &m, CHANRCV},
		{mctl->resizec, nil, CHANRCV},
		{kctl->c, &r, CHANRCV},
		{kbdc, &r, CHANRCV},
		{nil, nil, CHANEND},
		{nil, nil, CHANEND}
	};

M pal.man => pal.man +4 -2
@@ 18,10 18,12 @@ the color will output the hex color on stdout.
By default, the colors are the C64 color palette, as in 
.IR paint (1).
Passing a file with a hex color on each line will initialize
.I Pal
.I pal
with those colors, instead.
.PP
Hex colors from stdin are currently ignored.
Hex colors from stdin are currently ignored. However,
.I Pal
will close when stdin closes.
.PP
Mouse 2 will open up a menu, which has a few entries:
.TF Delete