shithub: asif

ref: 516586152ab26883f5e57436634d9e5a715ff60a
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;
int	mouseinput(Node*, Mouse);
int	keyinput(Rune);
Node*	scrselect(Point);
void	updatedrw(void);

static Keyboardctl *kc;
static Mousectl *mc;

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

	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},
	};
	for(;;){
		switch(alt(a)){
		case Aresize:
			if(getwindow(display, Refnone) < 0)
				sysfatal("resize failed: %r");
			resetdrw();
			break;
		case Amouse:
			m = mc->Mouse;
			if(m.buttons == 0)
				break;
			if((n = scrselect(m.xy)) != nil){
				if(m.buttons & 4)
					n->blocked ^= 1;
				mouseinput(n, m);
			}
			updatedrw();
			break;
		case Akbd:
			switch(r){
			case Kdel: threadexitsall(nil);
			}
			keyinput(r);
			break;
		}
	}
}

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

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

	mapwidth = 64;
	mapheight = 64;
	ARGBEGIN{
	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();
	initgrid();
	initdrw();
	if((kc = initkeyboard(nil)) == nil)
		sysfatal("initkeyboard: %r");
	if((mc = initmouse(nil, screen)) == nil)
		sysfatal("initmouse: %r");
}