#include "dat.h" #include "fns.h" void node_setup(Node* self, Handler* handlers, KuroMemory* memory) { if (self) { self->handlers = handlers; self->memory = memory; self->cpu = chancreate(sizeof(Instruction), 0); self->status = chancreate(sizeof(WindowStatus), 0); } } void node_loop(void* data) { Node* self = (Node*)data; Instruction x; for (;;) { recv(self->cpu, &x); if (self->handlers[x.opcode]) { (*(self->handlers[x.opcode]))(self, x.data); switch(self->memory->status) { case KURO_QUITS: nbsend(self->status, 0); threadexits(0); default: break; } flushimage(self->memory->screen->display, 1); } } } void node_execute(Node* self) { threadcreate(node_loop, self, 1024); } Node* create_node(char* filename) { initdraw(nil, nil, "kuro"); unlockdisplay(display); KuroMemory* self = (KuroMemory*)malloc(sizeof(KuroMemory)); Node* node = (Node*)malloc(sizeof(Node)); Handler* handlers = (Handler*)malloc(TOTAL_OPCODES * sizeof(Handler*)); node_setup(node, handlers, self); self->img = screen; self->screen = _screen; if (filename) { strcpy(self->filepath, filename); } /* node_execute runs the node on a separate thread */ node_execute(node); return node; } void supervise_node(Node* self) { Mousectl* mctl; Keyboardctl* kctl; Mouse mouse; int resize[2]; WindowStatus status; Rune kbd; if ((mctl = initmouse(nil, screen)) == nil) { sysfatal("couldn't initialize mctl"); } if ((kctl = initkeyboard(nil)) == nil) { sysfatal("couldn't initialize kctl"); } Alt alts[TOTAL_PORTS + 1] = { {mctl->c, &mouse, CHANRCV}, {mctl->resizec, resize, CHANRCV}, {kctl->c, &kbd, CHANRCV}, {self->status, &status, CHANRCV}, {nil, nil, CHANEND} }; for (;;) { switch (alt(alts)) { case PORT_MOUSE: print("got a mouse event"); break; case PORT_RESIZE: /*if (getwindow(display, Refnone) < 0) sysfatal("couldn't resize");*/ print("got a resize event"); break; case PORT_KBD: print("got a keypress"); break; case PORT_STATUS: switch(status) { case KURO_QUITS: print("VM died - let's clean up the data"); goto cleanup; default: break; } break; } } cleanup: node_cleanup(self); } void node_cleanup(Node* self) { if (self) { memory_cleanup(self->memory); chanfree(self->cpu); chanfree(self->status); /* TODO: send message on self->fd to tell the server to remove us from NodeTable and filetree */ /* handlers array is shared between all the nodes */ free(self); } } void memory_cleanup(KuroMemory* self) { if (self) { /* do we need to free the img and screen? */ } }