shithub: eesp

ref: c70b508504537491dbee49e5c5e1a5938998ac83
dir: /libeenter.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include <keyboard.h>
#include "genenter.h"

static int
eeinitsave(Enterparams *p)
{
	if(p->save)
		return 0;
	p->save = allocimage(display, p->r, p->b->chan, 0, DNofill);
	if(p->save == nil){
		return 1;
	}
	draw(p->save, p->r, p->b, nil, p->r.min);
	return 0;
}

static int
eegetevent(Enterparams *p)
{
	Event ev;
	int i = Ekeyboard;

	if(p->aux)
		i |= Emouse;

	replclipr(p->b, 0, p->sc);
	i = eread(i, &ev);

	/* screen might have been resized */
	if(p->b != screen || !eqrect(screen->clipr, p->sc)){
		freeimage(p->save);
		p->save = nil;
	}
	p->b = screen;
	p->sc = p->b->clipr;
	replclipr(p->b, 0, p->b->r);
	
	if(i == Emouse){
		p->m = ev.mouse;
		return Gmouse;
	}
	if(i == Ekeyboard){
		p->k = ev.kbdc;
		return Gkeyboard;
	}
	return -1;
}

static void
eeloopcleanup(Enterparams *p)
{
	if(p->save){
		draw(p->b, p->save->r, p->save, nil, p->save->r.min);
		freeimage(p->save);
		p->save = nil;
	}
}

static void
eecleanup(Enterparams *p)
{
	replclipr(p->b, 0, p->sc);
}

int
eenter(char *ask, char *buf, int len, Mouse *m)
{
	Enterparams p;

	while(ecankbd())
		ekbd();

	if(m)
		p.o = m->xy;

	p.b = screen;
	p.sc = p.b->clipr;
	replclipr(p.b, 0, p.b->r);

	p.aux = m;
	p.initsave = eeinitsave;
	p.getevent = eegetevent;
	p.loopcleanup = eeloopcleanup;
	p.cleanup = eecleanup;
	return _genenter(ask, buf, len, &p);
}