Charles.Forsyth20060303a828770d19 years agoHistory
977 lines · 26.1 KB · Brainfuck
RawDownload
implement Blurdemo; include "sys.m";	sys : Sys;include "draw.m";	draw: Draw;	Display, Rect, Image: import draw;include "tk.m";	tk: Tk;include "tkclient.m";	tkclient: Tkclient;include "readdir.m";	readdir: Readdir;include "sh.m";include "registries.m";	registries: Registries;	Registry, Attributes, Service: import registries;include "grid/pathreader.m";	reader: PathReader;include "grid/browser.m";	browser: Browser;	Browse, Select, File, Parameter,	DESELECT, SELECT, TOGGLE: import browser;include "grid/srvbrowse.m";	srvbrowse: Srvbrowse;include "grid/announce.m";	announce: Announce;include "grid/readjpg.m";	readjpg: Readjpg; srvfilter: list of list of (string, string);currstep: int; currsrv: ref Service;currattach: ref Registries->Attached;ctxt: ref Draw->Context;display: ref Draw->Display;sysname : string; IMAGE: con 0;MOUNT: con 4; imgcache: ref Image;br: ref Browse;sel: ref Select; Blurdemo : module {	init : fn (context : ref Draw->Context, argv : list of string);	readpath: fn (dir: File): (array of ref sys->Dir, int);}; init(context : ref Draw->Context, argv: list of string){	ctxt = context;	display = ctxt.display;	sys = load Sys Sys->PATH;	if (sys == nil)		badmod(Sys->PATH);	readdir = load Readdir Readdir->PATH;	if (readdir == nil)		badmod(Readdir->PATH);	draw = load Draw Draw->PATH;	if (draw == nil)		badmod(Draw->PATH);	tk = load Tk Tk->PATH;	if (tk == nil)		badmod(Tk->PATH);	tkclient = load Tkclient Tkclient->PATH;	if (tkclient == nil)		badmod(Tkclient->PATH);	tkclient->init();	registries = load Registries Registries->PATH;	if (registries == nil)		badmod(Registries->PATH);	registries->init();	browser = load Browser Browser->PATH;	if (browser == nil)		badmod(Browser->PATH);	browser->init();	srvbrowse = load Srvbrowse Srvbrowse->PATH;	if (srvbrowse == nil)		badmod(Srvbrowse->PATH);	srvbrowse->init();	announce = load Announce Announce->PATH;	if (announce == nil)		badmod(Announce->PATH);	announce->init();	reader = load PathReader "$self";	if (reader == nil)		badmod("PathReader");	readjpg = load Readjpg Readjpg->PATH;	if (readjpg == nil)		badmod(Readjpg->PATH);	readjpg->init(display);	sys->pctl(sys->FORKNS | sys->NEWPGRP, nil);	if (ctxt == nil) {		sys->print("no draw context found!\n");		exit;	}	sysname = readfile("/dev/sysname");	if (sysname == "")		sysname = "Localhost";	imgcache = nil;	setsrvfilter();	root := "/";	currsrv = nil;		attribs := ("resource", "Cpu Pool") :: nil;	lcpupool := srvbrowse->find(attribs :: nil);	if (lcpupool == nil) {		browser->dialog(ctxt, nil, "ok" :: nil, "Alert","Cannot find a Cpu Pool Resource");		raise "fail: error cannot find a Cpu Pool resource";	} 	(top, titlebar) := tkclient->toplevel(ctxt,"","BlurDemo", tkclient->Appl);	butchan := chan of string;	tk->namechan(top, butchan, "butchan");	browsechan := chan of string;	tk->namechan(top, browsechan, "browsechan");	selectchan := chan of string;	tk->namechan(top, selectchan, "selectchan");	br = Browse.new(top, "browsechan", "services/", "Services", 1, reader);	bropened := array[] of {		"services/",		"services/Data source/",		"services/Camera/",		"/n/remote/",		"/" ,	};	for (i := 0; i < len bropened; i++)		br.addopened(File (bropened[i], nil), 1); 	sel = Select.new(top, "selectchan"); 	for (ik := 0; ik < len mainscreen; ik++)		tkcmd(top,mainscreen[ik]); 	currstep = -1;		sel.addframe("image", "Select a '.bit' image"); 	changestep(top, IMAGE, nil); 	tkcmd(top, "pack .f -fill both -expand 1; pack propagate . 0");	released := 1;	title := "";	resize(top, ref Rect ((0,0), (400,400)));	tkclient->onscreen(top, nil);	tkclient->startinput(top, "kbd"::"ptr"::nil);	tkpath: string;	selected := array[2] of File;	if (tl argv != nil)		spawn initimg(butchan, hd tl argv); 	main: for (;;) {		alt {		s := <-top.ctxt.kbd =>			tk->keyboard(top, s);		s := <-top.ctxt.ptr =>			tk->pointer(top, *s);		inp := <-browsechan =>			(nil, lst) := sys->tokenize(inp, " \n\t");			if (len lst > 1)				tkpath = hd tl lst;			selected[0] = br.getselected(0);			selected[1] = br.getselected(1);			br.defaultaction(lst, nil);			i = -1;			if (!File.eq(selected[0], br.getselected(0)))				i = 0;			if (!File.eq(selected[1], br.getselected(1)))				i = 1;			if (i != -1) {				sel.select(sel.currfname,nil,DESELECT);				actionbutton(top, br.selected[i].file.path, br.selected[i].tkpath);			}			tkcmd(top, "update");		inp := <-selectchan =>			(nil, lst) := sys->tokenize(inp, " \n\t");			case hd lst {				"but3" =>					tkpath = hd tl lst;					x := string (int hd tl tl lst - 5);					y := string (int hd tl tl tl lst - 5); 					path := tkcmd(top, tkpath+" cget -text");					s := blursrvc.attrs.get("name") + " ("+blursrvc.addr+")";					tk->cmd(top, "destroy .m2");					tkcmd(top, "menu .m2 -font /fonts/charon/plain.normal.font");					tkcmd(top, ".m2 add command -label {"+path+"}");					tkcmd(top, ".m2 add separator");					tkcmd(top, ".m2 add command -label {"+s+"}");					tkcmd(top, ".m2 post "+x+" "+y);									"double1" =>					tkpath = hd tl lst;					path := tkcmd(top, tkpath+" cget -text");					qid := "";					(n, nil) := sys->tokenize(path, "/");					if (currstep == IMAGE) {						qid = srvbrowse->getqid(blursrvc);						(res,name) := srvbrowse->getresname(blursrvc);						path = "services/"+res+"/"+name+"/";					}					else if (currsrv.addr != blursrvc.addr)						break;					else if (blursrvc.addr != "Local Machine")						path = "/n/remote" + path;					tkpath = br.gotoselectfile(File(path,qid));					if (tkpath != nil) {						sel.select(sel.currfname, nil, DESELECT);						actionbutton(top, path, tkpath);					}				"but1" =>					if (currstep == IMAGE)						br.selectfile(0, DESELECT, File (nil, nil), nil);					else						br.selectfile(1, DESELECT, File (nil, nil), nil);					sel.defaultaction(lst);					actionbutton(top, sel.getselected(sel.currfname), hd tl lst);				* =>					sel.defaultaction(lst);			}			tkcmd(top, "update");		inp := <-butchan =>			# sys->print("inp: %s\n",inp);			(nil, lst) := sys->tokenize(inp, " \n\t");			if (len lst > 1)				tkpath = hd tl lst;			case hd lst {				"refresh" =>					# ! check to see if anything is mounted first					if (currstep == IMAGE) {						# addlocalservice();						srvbrowse->refreshservices(srvfilter);					}					br.refresh();				"back" =>					changestep(top, IMAGE, nil);				"run" =>					spawn run(ctxt, getcoords(top));				"preview" =>					spawn previewwin(top, butchan, hd tl lst);				"add" =>					additem(top, hd tl lst, int hd tl tl lst);				"del" =>					sel.delselection("image", hd tl lst);					tkcmd (top, ".f.ftop.bn configure -state disabled");					blurimage = nil;					blurtkpath = nil;					blursrvc = nil;					actionbutton(top, sel.getselected(sel.currfname), hd tl lst);				"mount" =>					file := br.getpath(tkpath);					(nsrv, lsrv) := sys->tokenize(file.path, "/");					if (currstep != IMAGE)						break;					if (nsrv != 3)						break;					if (hd tl tl lsrv != "Local Filestore") {						ok := mountsrv(file.path, file.qid, getcoords(top));						if (!ok)							break;						changestep(top, MOUNT, hd tl tl lsrv);					}					else {						srv : Service;						srv.attrs = Attributes.new(("name", sysname) :: nil);						srv.addr = "Local Machine";						currsrv = ref srv;						changestep(top, MOUNT, hd tl tl lsrv);					}			}			tkcmd(top, "update"); 		title = <-top.ctxt.ctl or		title = <-top.wreq or		title = <-titlebar =>			if (title == "exit")				break main;			e := tkclient->wmctl(top, title);			if (e == nil && title[0] == '!') {				(nil, lst) := sys->tokenize(title, " \t\n");				if (len lst >= 2 && hd lst == "!size" && hd tl lst == ".")					resize(top, nil);			}		}	}	currattach = nil;	killg(sys->pctl(0,nil));} resize(top: ref Tk->Toplevel, r: ref Draw->Rect){	if (r != nil) {		sw := (*r).dx();		sh := (*r).dy();		ww := int tkcmd(top, ". cget -actwidth");		wh := int tkcmd(top, ". cget -actheight");		if (ww > sw)			tkcmd(top, ". configure -x 0 -width "+string sw);		if (wh > sh)			tkcmd(top, ". configure -y 0 -height "+string sh);	}	w := int tkcmd(top, ".fselect cget -actwidth");	h := int tkcmd(top, ".fselect cget -actheight");	sel.resize(w,h);} nactionbuttons := 0;actionbutton(top: ref Tk->Toplevel, path, tkpath: string){	for (i := 0; i < nactionbuttons; i++) {		tkcmd(top, "grid forget .f.ftop.baction"+string i);		tkcmd(top, "destroy .f.ftop.baction"+string i);	}	if (path == nil) {		nactionbuttons = 0;		return;	}	buttons : list of (string,string) = nil;	(n, nil) := sys->tokenize(path, "/");	if (len tkpath > 8 && tkpath[:8] == ".fselect")		buttons = ("Remove", "del "+tkpath) :: buttons;	else {		if (currstep == IMAGE) {			if (n == 3)				buttons = ("Mount", "mount "+tkpath) :: buttons;		}		else {			if (len path > 4) {				if (path[len path - 4:] == ".bit") {					buttons = ("Select", "add "+path+" 0") ::							("Preview", "preview "+path) :: buttons;				}				else if (path[len path - 4:] == ".jpg")					buttons = ("Select", "add "+path+" 0") :: buttons;			}		}	}	nactionbuttons = len buttons;	for (i = 0; i < nactionbuttons; i++) {		name := ".f.ftop.baction"+string i+" ";		(text,cmd) := hd buttons;		tkcmd(top, "button "+name+"-text {"+text+"} "+				"-font /fonts/charon/bold.normal.font "+				"-command {send butchan "+cmd+"}");		tkcmd(top, "grid "+name+" -row 0 -column "+string (4+i));		buttons = tl buttons;	}} initimg(butchan: chan of string, imgpath: string){	srv : Service;	srv.attrs = Attributes.new(("name", sysname) :: nil);	srv.addr = "Local Machine";	currsrv = ref srv;	butchan <-= "add "+imgpath+" 0";	butchan <-= "back";} blurimage := "";blurtkpath := "";blursrvc: ref Service; additem(top: ref Tk->Toplevel, path: string, overwrite: int){	if (blurimage != nil) {		if (overwrite || browser->dialog(ctxt, top, "ok" :: "cancel" :: nil,			"Alert","Replace existing image '"			+nopath(blurimage)+"' with '"+nopath(path)+"'?") == 0) {			sel.delselection("image", blurtkpath);		}		else			return;	}	imgpath := path;	if (currsrv.addr != "Local Machine")		path = path[len "/n/remote":];	blurtkpath = sel.addselection("image", path, nil, 0);	tkcmd(top, "update");	blurimage = path;	blursrvc = currsrv;	if (overwrite)		spawn getpreview(blurtkpath, nil, imgcache);	else		spawn getpreview(blurtkpath, imgpath, nil);} nopath(file: string): string{	return file[len browser->prevpath(file):];} runscr := array[] of {	"frame .f",	"frame .f.f1",	"label .f.f1.l -text {Select no of CPUs} -font /fonts/charon/plain.normal.font",	"scale .f.f1.s -orient horizontal -height 16 -showvalue 0 -from 1 -to 20 -command {.f.f1.ls configure -text}",	"label .f.f1.ls -text {1} -font /fonts/charon/plain.normal.font -width 30",	"button .f.f1.b -text {Run} -font /fonts/charon/plain.normal.font -command {send butchan go}",	"pack .f.f1.l .f.f1.s .f.f1.ls .f.f1.b -side left",	"frame .f.f2",	"text .f.f2.t -width 250 -height 150 -borderwidth 1 -bg white -font /fonts/charon/plain.normal.font -yscrollcommand { .f.f2.sy set }",	"scrollbar .f.f2.sy -command { .f.f2.t yview }",	"pack .f.f2.sy -side left -fill y",	"pack .f.f2.t -fill both -expand 1",	"bind .Wm_t <Button-1> +{focus .Wm_t}",	"bind .Wm_t.title <Button-1> +{focus .Wm_t}",	"focus .Wm_t",	"pack .f.f1 -side top",	"pack .f.f2 -fill both -expand 1",}; run(ctxt: ref Draw->Context, coords: draw->Rect){	(top, titlectl) := tkclient->toplevel(ctxt, "", nil, tkclient->Resize);	butchan := chan of string;	sync := chan of int;	quit := chan of int;	tk->namechan(top, butchan, "butchan");	tkcmds(top, runscr);	tkcmd(top, ". configure "+getcentre(top, coords));	tkcmd(top, "pack .f -fill both -expand 1; pack propagate . 0; focus .; update");	tkclient->onscreen(top, "exact");	tkclient->startinput(top, "kbd"::"ptr"::nil);	done := 1;	loop: for (;;) {		alt {		s := <-top.ctxt.kbd =>			tk->keyboard(top, s);		s := <-top.ctxt.ptr =>			tk->pointer(top, *s);		<-sync =>			tkcmd(top, ".f.f1.b configure -state normal; update");			done = 1;		inp := <-butchan =>			(nil, lst) := sys->tokenize(inp, " \n\t");			case hd lst {				"go" =>					tkcmd(top, ".f.f1.b configure -state disabled");					ncpus := int tkcmd(top, ".f.f1.s get");					done = 0;					spawn startit(ncpus, butchan, sync, quit);				"output" =>					tkcmd(top, ".f.f2.t insert end {"+inp[len "output ":]+"}");				"error" =>					tkcmd(top, ".f.f2.t insert end {Error: "+inp[len "error ":]+"\n}");					tkcmd(top, ".f.f1.b configure -state normal");				"fewcpu" =>					i := browser->dialog(ctxt, top, "ok" :: "cancel" :: nil, "Alert",							"Only found "+hd tl lst+" cpus available. Continue?");					quit <-= i;					if (i == 1)						return;			}			tkcmd(top, "update");		s := <-top.ctxt.ctl or		s = <-top.wreq or		s = <- titlectl =>			if (s == "exit") {				if (done)					return;				break loop;			}			else				tkclient->wmctl(top, s);		}	}	top = nil;	for (;;) alt {		<- butchan =>			;		<-sync =>			return;	}} startit(ncpus: int, butchan: chan of string, sync, quit: chan of int){	imgattached : ref Registries->Attached;	imgpath := blurimage;	if (blursrvc.addr != "Local Machine") {		imgattached = blursrvc.attach(nil, nil);		if (imgattached == nil) {			butchan <-= "error cannot connect to data source: "+blursrvc.attrs.get("name");			return;		}		if (sys->mount(imgattached.fd, nil, "/n/local", sys->MREPL, nil) == -1) {			butchan <-= sys->sprint("error img mount failed: %r");			return;		}		imgpath = "/n/local" + imgpath;		butchan <-= "output Found image namespace\n";	}	sys->pctl(sys->FORKNS, nil);	attribs := ("resource", "Cpu Pool") :: nil;	lsrv := srvbrowse->find(attribs :: nil);	if (lsrv == nil) {		butchan <-= "error cannot find Cpu Pool resource";

Lines 1–500 of 977.