shithub: libacme

ref: 521670692b07cbadbf6a88964839d76531ba9afd
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);
	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 = -1;
	w->datafd = -1;

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

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

void
awinclear(AWin *w)
{
	w->addrfd = awinfsopen(w, "addr", OWRITE);
	w->datafd = awinfsopen(w, "data", OWRITE);
	
	fprint(w->addrfd, ",");
	write(w->datafd, "", 0);
	
	close(w->datafd);
	close(w->addrfd);
}

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

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

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

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

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