shithub: libacme

ref: 15b9d1f9b76b6c3b0240f535c452b4023353b227
dir: /window.c/

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

#include "acme.h"

int
winopen(Win *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;
}

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

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

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

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

void
winclear(Win *w)
{
	fprint(w->addr, ",");
	fprint(w->data, "");
}

void
winappend(Win *w, char *fmt, ...)
{
	va_list args;
	
	va_start(args, fmt);
	vfprint(w->body, fmt, args);
	va_end(args);
}

void
winerror(Win *w, char *fmt, ...)
{
	int errorfd;
	va_list args;
	
	errorfd = winopen(w, "errors", OWRITE);
	
	va_start(args, fmt);
	vfprint(w->body, fmt, args);
	va_end(args);
	
	close(errorfd);
}