Plan 9 from Bell Labs’s /usr/web/sources/extra/i/event.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "i.h"

// globals

Channel*	evchan;

int	Mfmt(Fmt *f);

void
eventinit(void)
{
	evchan = chancreate(sizeof(EV), 0);
	fmtinstall('M', Mfmt);
}

int
Mfmt(Fmt *f)
{
	EV* ev;
	char* s;
	char cbuf[4];
	int i;
	int n;
	Point p;
	char	buf[BIGBUFSIZE];

	ev = va_arg(f->args, EV*);
	n = sizeof(buf) - 1;
	switch(ev->tag) {
	case EVkeytag:
		switch(ev->u.keychar) {
		case ' ':
			s = "<SP>";
			break;
		case '\t':
			s = "<TAB>";
			break;
		case '\n':
			s = "<NL>";
			break;
		case '\r':
			s = "<CR>";
			break;
		case '\b':
			s = "<BS>";
			break;
		case 127:
			s = "<DEL>";
			break;
		case Kup:
			s = "<UP>";
			break;
		case Kdown:
			s = "<DOWN>";
			break;
		case Khome:
			s = "<HOME>";
			break;
		case Kleft:
			s = "<LEFT>";
			break;
		case Kright:
			s = "<RIGHT>";
			break;
		default:
			sprint(cbuf, "%C", ev->u.keychar);
			s = cbuf;
			break;
		}
		i = snprint(buf, n, "key %d = %s", ev->u.keychar, s);
		break;
	case EVmousetag:
		switch(ev->u.mouse.mtype) {
		case Mmove:
			s = "move";
			break;
		case Mlbuttondown:
			s = "lbuttondown";
			break;
		case Mlbuttonup:
			s = "lbuttonup";
			break;
		case Mldrag:
			s = "ldrag";
			break;
		case Mmbuttondown:
			s = "mbuttondown";
			break;
		case Mmbuttonup:
			s = "mbuttonup";
			break;
		case Mmdrag:
			s = "mdrag";
			break;
		case Mrbuttondown:
			s = "rbuttondown";
			break;
		case Mrbuttonup:
			s = "rbuttonup";
			break;
		case Mrdrag:
			s = "rdrag";
			break;
		default:
			s = "?";
		}
		p = ev->u.mouse.p;
		i = snprint(buf, n, "mouse (%d,%d) %s", p.x, p.y, s);
		break;
	case EVmovetag:
		p = ev->u.movept;
		i = snprint(buf, n, "move (%d,%d)", p.x, p.y);
		break;
	case EVresizetag:
		i = snprint(buf, n, "resize (%d,%d) (%d,%d)",
			ev->u.resizer.min.x,
			ev->u.resizer.min.y,
			ev->u.resizer.max.x,
			ev->u.resizer.max.y);
		break;
	case EVexposetag:
		i = snprint(buf, n, "expose");
		break;
	case EVhidetag:
		i = snprint(buf, n, "hide");
		break;
	case EVquittag:
		i = snprint(buf, n, "quit");
		break;
	case EVstoptag:
		i = snprint(buf, n, "stop");
		break;
	case EValerttag:
		i = snprint(buf, n, "alert(%S)", ev->u.alert.msg);
		break;
	case EVformtag:
		switch(ev->u.form.ftype) {
		case EFsubmit:
			s = "form submit";
			break;
		case EFreset:
			s = "form reset";
			break;
		default:
			s = "form unknown";
		}
		i = snprint(buf, n, "%s", s);
		break;
	case EVgotag:
		if(ev->u.go.gtype == EGnormal)
			i = snprint(buf, n, "go(%U)", ev->u.go.url);
		else {
			switch(ev->u.go.gtype) {
			case EGreload:
				s = "RELOAD";
				break;
			case EGforward:
				s = "FORWARD";
				break;
			case EGback:
				s = "BACK";
				break;
			case EGhome:
				s = "HOME";
				break;
			case EGbookmarks:
				s = "BOOKMARKS";
				break;
			default:
				s = "?";
			}
			i = snprint(buf, sizeof(buf), "go(%s)", s);
		}
		break;
	case EVanimtag:
		i = snprint(buf, sizeof(buf), "anim(%U)", ev->u.anim.ai->cim->src);
		break;
	case EVprogresstag:
		switch(ev->u.progress.state) {
		case 0:
			s = "";
			break;
		case Pstart:
			s = "start";
			break;
		case Pconnected:
			s = "connected";
			break;
		case Phavehdr:
			s = "havehdr";
			break;
		case Pdone:
			s = "done";
			break;
		case Perr:
			s = "err";
			break;
		case Paborted:
			s = "aborted";
			break;
		default:
			s = "?";
		}
		i = snprint(buf, sizeof(buf), "progress(%d, %s, %d, %S)",
			ev->u.progress.bsid, s, ev->u.progress.pcnt,
			ev->u.progress.s ? ev->u.progress.s : L"");
		break;
	case EVdelaytag:
		i = snprint(buf, sizeof(buf), "delay(%d) %M", ev->u.delay.millisecs, ev->u.delay.ev);
		break;
	default:
		i = snprint(buf, sizeof(buf), "unknown event");
	}
	buf[i] = 0;
	return fmtstrcpy(f, buf);
}

EV
evgo(ParsedUrl* u, int targ, int gtype, int genframeid)
{
	EV eg;

	eg.tag = EVgotag;
	eg.genframeid = genframeid;
	eg.u.go.url = u;
	eg.u.go.target = targ;
	eg.u.go.gtype = gtype;
	return eg;
}

EV
evmouse(Point p, int mtype)
{
	EV em;

	em.tag = EVmousetag;
	em.genframeid = -1;
	em.u.mouse.p = p;
	em.u.mouse.mtype = mtype;
	return em;
}

EV
evkey(int keycode)
{
	EV ek;

	ek.tag = EVkeytag;
	ek.genframeid = -1;
	ek.u.keychar = keycode;
	return ek;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.