shithub: eesp

ref: f664f9d3a8d305e088f259a23cb6e86e664a7b36
dir: /enter.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>

enum {
	Emouse,
	Eresize,
	Ekeyboard,
};

void
threadmain(int argc, char **argv)
{
	Mouse m;
	Rune k;
	Mousectl *mctl;
	Keyboardctl *kctl;
	char buf[512];
	USED(argc, argv);
	
	Alt a[] = {
		{ nil, &m, CHANRCV },
		{ nil, nil, CHANRCV },
		{ nil, &k, CHANRCV },
		{ nil, nil, CHANEND },
	};
	
	if (initdraw(nil, nil, "enter test") < 0)
		sysfatal("initdraw: %r");
	
	mctl = initmouse(nil, screen);
	if (!mctl)
		sysfatal("initmouse: %r");
	
	kctl = initkeyboard(nil);
	if (!kctl)
		sysfatal("initkeyboard: %r");
	a[Emouse].c = mctl->c;
	a[Eresize].c = mctl->resizec;
	a[Ekeyboard].c = kctl->c;
	
	draw(screen, screen->r, display->white, nil, ZP);
	flushimage(display, 1);
	
	for (;;) {
		switch (alt(a)) {
		case Emouse:
			if (m.buttons == 0)
				break;
			snprint(buf, sizeof(buf), "hello world");
			if (enter("Ask:", buf, sizeof(buf), mctl, kctl, nil) > 0)
				fprint(2, "entered: %s\n", buf);
			break;
		case Eresize:
			if (getwindow(display, Refnone) < 0)
				sysfatal("cannot reattach: %r");
			draw(screen, screen->r, display->white, nil, ZP);
			flushimage(display, 1);
			break;
		case Ekeyboard:
			if (k == Kdel || k == 'q')
				threadexitsall(nil);
			break;
		}
	}
}