shithub: asif

ref: d82bab87ecc28965c91621f1193efa8badc909df
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"

mainstacksize = 512*1024;

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 [-D4] [-s width[,height]]\n", argv0);
	threadexits("usage");
}

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

	mapwidth = 64;
	mapheight = 64;
	ARGBEGIN{
	case 'D':
		if(++debuglevel >= Logparanoid)
			mainmem->flags |= POOL_NOREUSE | POOL_PARANOIA | POOL_LOGGING;
		break;
	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
	if(mapwidth <= 0 || mapwidth > 512
	|| mapheight <= 0 || mapheight > 512)
		sysfatal("invalid map size, must be in ]0,512]");
	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");
}