shithub: libacme

ref: 1597080e847d73678ccc1746350d1e29aa7e226b
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);
	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->eventfd = awinfsopen(w, "event", ORDWR);
	w->bodyfd = awinfsopen(w, "body", OWRITE);

	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->ctlfd);
		close(temp->addrfd);
		close(temp->datafd);
		close(temp->eventfd);
		close(temp->bodyfd);
		
		awins = temp->next;
		free(temp);
		temp = awins;
		
	}
}

int
awinload(AWin *w, char *file)
{
	int fd;
	char buf[8192];
	long n;
	
	fd = open(file, OREAD);
	if(fd < 0) {
		werrstr("awinload open failed: %r");
		return -1;
	}
		
	fprint(w->addrfd, ",");
	while((n = read(fd, buf, sizeof(buf))) > 0)
		write(w->datafd, buf, n);
		
	if(n < 0) {
		werrstr("awinload read failed: %r");
		close(fd);
		return -1;
	}
	
	return 0;
}

int
awinput(AWin *w, char *file)
{
	int fd;
	char buf[8192];
	long n;
	
	fd = open(file, OWRITE);
	if(fd < 0) {
		werrstr("awinput open failed: %r");
		return -1;
	}
		
	fprint(w->addrfd, ",");
	while((n = read(fd, buf, sizeof(buf))) > 0)
		write(w->datafd, buf, n);
		
	if(n < 0) {
		werrstr("awinput read failed: %r");
		close(fd);
		return -1;
	}
	
	return 0;
}

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

int
awinsettag(AWin *w, char *tag)
{
	int fd, ret;
	
	fd = awinfsopen(w, "tag", OWRITE);
	fprint(w->ctlfd, "cleartag");
	ret = fprint(fd, "%s", tag);
	close(fd);
	
	return ret;
}