shithub: libacme

ref: 1bced94ed0ad5e160db18e553e2e0a58b158035b
dir: /window.c/

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

#include "acme.h"

static AWin *awins;

int
awinfsopen(AWin *w, char *file, int mode)
{
	char buf[128];
	int fd;

	snprint(buf, sizeof(buf), "/mnt/wsys/%d/%s", w->id, file);
	if((fd = open(buf, mode|OCEXEC)) < 0)
		sysfatal("open %s: %r", file);

	return fd;
}

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

	w->next = awins;
	awins = w;
	
	return w;
}

void
awinclose(AWin *w)
{
	AWin *temp;
	
	fprint(w->ctlfd, "delete");

	close(w->eventfd);
	close(w->addrfd);
	close(w->datafd);
	
	temp = awins;
	while(temp) {
		if(awins->next == w) {
			temp->next = w->next;
			free(w);
			return;
		}
		
		temp = awins->next;
	}
}

void
awincloseall(void)
{
	AWin *temp;
	
	temp = awins;
	while(temp) {
		fprint(temp->ctlfd, "delete");
		
		close(temp->eventfd);
		close(temp->addrfd);
		close(temp->datafd);
		
		awins = temp->next;
		free(temp);
		temp = awins;
		
	}
}

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

void
awinclear(AWin *w)
{
	fprint(w->addrfd, ",");
	fprint(w->datafd, "");
}

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

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

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

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

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