shithub: asif

ref: 52530907bc0a349b2c312e559c3ca3a5bb49da95
dir: /path/client.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <pool.h>
#include "../asif.h"
#include "dat.h"
#include "fns.h"

extern QLock drawlock;
Node*	scrselect(Point);
void	updatedrw(void);

int	(*mousefn)(Node*, Mouse, Node*);
int	(*keyfn)(Rune);

static Keyboardctl *kc;
static Mousectl *mc;

void
evloop(void)
{
	Rune r;
	Mouse m;
	Node *n, *p;

	enum{
		Aresize,
		Amouse,
		Akbd,
		Aend,
	};
	Alt a[] = {
		[Aresize] {mc->resizec, nil, CHANRCV},
		[Amouse] {mc->c, &mc->Mouse, CHANRCV},
		[Akbd] {kc->c, &r, CHANRCV},
		[Aend] {nil, nil, CHANEND},
	};
	p = nil;
	for(;;){
		switch(alt(a)){
		case Aresize:
			if(getwindow(display, Refnone) < 0)
				sysfatal("resize failed: %r");
			resetdrw();
			break;
		case Amouse:
			if(mc->buttons == 0){
				p = nil;
				break;
			}
			if((n = scrselect(m.xy)) != nil && p != n){
				if(mousefn(n, mc->Mouse, p) < 0)
					fprint(2, "%r\n");
				p = n;
			}
			updatedrw();
			break;
		case Akbd:
			switch(r){
			case Kdel: threadexitsall(nil);
			case 'r': clearmap(); updatedrw(); break;
			}
			keyfn(r);
			break;
		}
		m = mc->Mouse;
	}
}

static void
usage(void)
{
	fprint(2, "usage: %s [-4] [-s width[,height]]\n", argv0);
	threadexits("usage");
}

void
init(int argc, char **argv)
{
	char *s;

	mapwidth = 64;
	mapheight = 64;
	ARGBEGIN{
	case '4':
		fourdir = 1;
		break;
	case 's':
		mapwidth = strtol(EARGF(usage()), &s, 0);
		if(mapwidth <= 0)
			usage();
		if(*s != ','){
			mapheight = mapwidth;
			break;
		}
		mapheight = strtol(s+1, nil, 0);
		if(mapheight <= 0)
			usage();
		break;
	default: usage();
	}ARGEND
	//mainmem->flags |= POOL_PARANOIA | POOL_NOREUSE;
	fmtinstall('P', Pfmt);
	fmtinstall('R', Rfmt);
	initfs();
	initmap();
	initdrw();
	if((kc = initkeyboard(nil)) == nil)
		sysfatal("initkeyboard: %r");
	if((mc = initmouse(nil, screen)) == nil)
		sysfatal("initmouse: %r");
}