shithub: libacme

ref: b517e0beea8b1adc16160d99f4b5e5e9a4a5d3d6
dir: /window.c/

View raw version
#include <u.h>
#include <libc.h>

#include "acme.h"

int
awinopen(AWin *w, char *f, int mode)
{
	char buf[128];
	int fd;
	
	snprint(buf, sizeof(buf), "/mnt/wsys/%d/%s", w->id, f);
	if((fd = open(buf, mode|OCEXEC)) < 0)
		sysfatal("open %s: %r", buf);
	return fd;
}

AWin *
awincreate(void)
{
	AWin *w;
	char buf[12];
	
	w = mallocz(sizeof(AWin), 1);
	w->ctl = open("/mnt/wsys/new/ctl", ORDWR|OCEXEC);
	if(w->ctl < 0)
		sysfatal("open ctl: %r");
	if(read(w->ctl, buf, sizeof(buf)) != sizeof(buf))
		sysfatal("read ctl: %r");
	
	w->id = atoi(buf);
	w->event = awinopen(w, "event", ORDWR);
	w->addr = awinopen(w, "addr", ORDWR);
	w->data = awinopen(w, "data", ORDWR);
	
	return w;
}

void
awinclose(AWin *w)
{
	fprint(w->ctl, "delete");
	
	close(w->event);
	close(w->addr);
	close(w->data);
	
	free(w);
}

void
awintitle(AWin *w, char *fmt, ...)
{
	char *title;
	va_list args;
	
	va_start(args, fmt);
	title = vsmprint(fmt, args);
	va_end(args);
	
	awinctl(w, "name %s\n", title);
	free(title);
}

void
awinctl(AWin *w, char *fmt, ...)
{
	va_list args;
	
	va_start(args, fmt);
	vfprint(w->ctl, fmt, args);
	va_end(args);
}

void
awinclear(AWin *w)
{
	fprint(w->addr, ",");
	fprint(w->data, "");
}

void
awinappend(AWin *w, char *fmt, ...)
{
	int fd;
	va_list args;
	
	fd = awinopen(w, "body", OWRITE);
	
	va_start(args, fmt);
	vfprint(fd, fmt, args);
	va_end(args);
	
	close(fd);
}

void
awinerror(AWin *w, char *fmt, ...)
{
	int errorfd;
	va_list args;
	
	errorfd = awinopen(w, "errors", OWRITE);
	
	va_start(args, fmt);
	vfprint(errorfd, fmt, args);
	va_end(args);
	
	close(errorfd);
}

void
awinaddtag(AWin *w, char *tag)
{
	int tagfd;
	
	tagfd = awinopen(w, "tag", OWRITE);
	fprint(tagfd, "%s ", tag);
	close(tagfd);
}

void
awinsettag(AWin *w, char *tag)
{
	int tagfd;
	
	awinctl(w, "cleartag");
	tagfd = awinopen(w, "tag", OWRITE);
	fprint(tagfd, "%s", tag);
	close(tagfd);
}