shithub: libacme

ref: e29aea8358355e6e195ec2782ec0d7b67eac4f1c
dir: /window.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.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;
}

Biobuf *
awinfsbopen(AWin *w, char *file, int mode)
{
	char buf[128];
	Biobuf *fd;
	
	snprint(buf, sizeof(buf), "/mnt/wsys/%d/%s", w->id, file);
	if((fd = Bopen(buf, mode)) == nil)
		sysfatal("bopen %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->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->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->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, "");
}

int
awincat(AWin *w, int fd)
{
	int body;
	char *buf;
	long n;
	
	buf = malloc(8*1024);
	body = awinfsopen(w, "body", OWRITE);
		
	while((n=read(fd, buf, 8*1024)) > 0) {
		if(write(body, buf, n) != n) {
			awinerror(w, "awincat write: %r");
			return -1;
		}
	}
	
	return 0;
}

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);
}