diff options
| author | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
| commit | 37da2899f40661e3e9631e497da8dc59b971cbd0 (patch) | |
| tree | cbc6d4680e347d906f5fa7fca73214418741df72 /appl/collab/clients | |
| parent | 54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff) | |
20060303a
Diffstat (limited to 'appl/collab/clients')
| -rw-r--r-- | appl/collab/clients/chat.b | 224 | ||||
| -rw-r--r-- | appl/collab/clients/poll.b | 282 | ||||
| -rw-r--r-- | appl/collab/clients/poller.b | 330 | ||||
| -rw-r--r-- | appl/collab/clients/whiteboard.b | 586 |
4 files changed, 1422 insertions, 0 deletions
diff --git a/appl/collab/clients/chat.b b/appl/collab/clients/chat.b new file mode 100644 index 00000000..d9c78d4b --- /dev/null +++ b/appl/collab/clients/chat.b @@ -0,0 +1,224 @@ +implement Chat; + +include "sys.m"; + sys: Sys; + +include "draw.m"; + draw: Draw; + +include "tk.m"; + tk: Tk; + +include "tkclient.m"; + tkclient: Tkclient; + +Chat: module { + init: fn(ctxt: ref Draw->Context, args: list of string); +}; + +stderr: ref Sys->FD; + +tksetup := array [] of { + "frame .f", + "text .f.t -state disabled -wrap word -yscrollcommand {.f.sb set}", + "scrollbar .f.sb -orient vertical -command {.f.t yview}", + "entry .e -bg white", + "bind .e <Key-\n> {send cmd send}", + "pack .f.sb -in .f -side left -fill y", + "pack .f.t -in .f -side left -fill both -expand 1", + "pack .f -side top -fill both -expand 1", + "pack .e -side bottom -fill x", + "pack propagate . 0", + "update", +}; + +init(ctxt: ref Draw->Context, args: list of string) +{ + sys = load Sys Sys->PATH; + sys->pctl(Sys->NEWPGRP, nil); + stderr = sys->fildes(2); + + draw = load Draw Draw->PATH; + if (draw == nil) + badmodule(Draw->PATH); + + tk = load Tk Tk->PATH; + if (tk == nil) + badmodule(Tk->PATH); + + tkclient = load Tkclient Tkclient->PATH; + if (tkclient == nil) + badmodule(Tkclient->PATH); + + + if (len args < 2) { + sys->fprint(stderr, "usage: chat [servicedir] room\n"); + raise "fail:init"; + } + args = tl args; + + servicedir := "/n/remote/services"; + if(len args == 2) + (servicedir, args) = (hd args, tl args); + room := hd args; + + tkclient->init(); + (win, winctl) := tkclient->toplevel(ctxt, nil, sys->sprint("Chat %s", room), Tkclient->Appl); + + cmd := chan of string; + tk->namechan(win, cmd, "cmd"); + tkcmds(win, tksetup); + tkcmd(win, ". configure -height 300"); + fittoscreen(win); + tkclient->onscreen(win, nil); + tkclient->startinput(win, "kbd"::"ptr"::nil); + + msgs := chan of string; + conn := chan of (string, ref Sys->FD); + spawn connect(servicedir, room, msgs, conn); + msgsfd: ref Sys->FD; + + for (;;) alt { + (e, fd) := <-conn => + if (msgsfd == nil) { + if (e == nil) { + output(win, "*** connected"); + msgsfd = fd; + } else + output(win, "*** " + e); + } else { + output(win, "*** disconnected"); + msgsfd = nil; + } + + txt := <-msgs => + output(win, txt); + + <- cmd => + msg := tkcmd(win, ".e get"); + if (msgsfd != nil && msg != nil) { + tkcmd(win, ".f.t see end"); + tkcmd(win, ".e delete 0 end"); + tkcmd(win, "update"); + d := array of byte msg; + sys->write(msgsfd, d, len d); + } + + s := <-win.ctxt.kbd => + tk->keyboard(win, s); + s := <-win.ctxt.ptr => + tk->pointer(win, *s); + s := <-win.ctxt.ctl or + s = <-win.wreq or + s = <-winctl => + tkclient->wmctl(win, s); + } +} + +err(s: string) +{ + sys->fprint(stderr, "chat: %s\n", s); + raise "fail:err"; +} + +badmodule(path: string) +{ + err(sys->sprint("can't load module %s: %r", path)); +} + +tkcmds(t: ref Tk->Toplevel, cmds: array of string) +{ + for (i := 0; i < len cmds; i++) + tkcmd(t, cmds[i]); +} + +tkcmd(t: ref Tk->Toplevel, cmd: string): string +{ + s := tk->cmd(t, cmd); + if (s != nil && s[0] == '!') + sys->fprint(stderr, "chat: tk error: %s [%s]\n", s, cmd); + return s; +} + +connect(dir, name: string, msgs: chan of string, conn: chan of (string, ref Sys->FD)) +{ + (ctlfd, srvdir, emsg) := opensvc(dir, "chat", name); + if(ctlfd == nil) { + conn <-= (emsg, nil); + return; + } + srvpath := srvdir+"/msgs"; + msgsfd := sys->open(srvpath, Sys->ORDWR); + if(msgsfd == nil) { + conn <-= (sys->sprint("internal error: can't open %s: %r", srvpath), nil); + return; + } + conn <-= (nil, msgsfd); + buf := array[Sys->ATOMICIO] of byte; + while((n := sys->read(msgsfd, buf, len buf)) > 0) + msgs <-= string buf[0:n]; + conn <-= (nil, nil); +} + +opensvc(dir: string, svc: string, name: string): (ref Sys->FD, string, string) +{ + ctlfd := sys->open(dir+"/ctl", Sys->ORDWR); + if(ctlfd == nil) + return (nil, nil, sys->sprint("can't open %s/ctl: %r", dir)); + if(sys->fprint(ctlfd, "%s %s", svc, name) <= 0) + return (nil, nil, sys->sprint("can't access %s service %s: %r", svc, name)); + buf := array [32] of byte; + sys->seek(ctlfd, big 0, Sys->SEEKSTART); + n := sys->read(ctlfd, buf, len buf); + if (n <= 0) + return (nil, nil, sys->sprint("%s/ctl: protocol error: %r", dir)); + return (ctlfd, dir+"/"+string buf[0:n], nil); +} + +firstmsg := 1; +output(win: ref Tk->Toplevel, txt: string) +{ + if (firstmsg) + firstmsg = 0; + else + txt = "\n" + txt; + yview := tkcmd(win, ".f.t yview"); + (nil, toks) := sys->tokenize(yview, " "); + toks = tl toks; + + tkcmd(win, ".f.t insert end '" + txt); + if (hd toks == "1") + tkcmd(win, ".f.t see end"); + tkcmd(win, "update"); +} + +KEYBOARDH: con 90; + +fittoscreen(win: ref Tk->Toplevel) +{ + Point, Rect: import draw; + if (win.image == nil || win.image.screen == nil) + return; + r := win.image.screen.image.r; + scrsize := Point((r.max.x - r.min.x), (r.max.y - r.min.y)- KEYBOARDH); + bd := int tkcmd(win, ". cget -bd"); + winsize := Point(int tkcmd(win, ". cget -actwidth") + bd * 2, int tkcmd(win, ". cget -actheight") + bd * 2); + if (winsize.x > scrsize.x) + tkcmd(win, ". configure -width " + string (scrsize.x - bd * 2)); + if (winsize.y > scrsize.y) + tkcmd(win, ". configure -height " + string (scrsize.y - bd * 2)); + actr: Rect; + actr.min = Point(int tkcmd(win, ". cget -actx"), int tkcmd(win, ". cget -acty")); + actr.max = actr.min.add((int tkcmd(win, ". cget -actwidth") + bd*2, + int tkcmd(win, ". cget -actheight") + bd*2)); + (dx, dy) := (actr.dx(), actr.dy()); + if (actr.max.x > r.max.x) + (actr.min.x, actr.max.x) = (r.min.x - dx, r.max.x - dx); + if (actr.max.y > r.max.y) + (actr.min.y, actr.max.y) = (r.min.y - dy, r.max.y - dy); + if (actr.min.x < r.min.x) + (actr.min.x, actr.max.x) = (r.min.x, r.min.x + dx); + if (actr.min.y < r.min.y) + (actr.min.y, actr.max.y) = (r.min.y, r.min.y + dy); + tkcmd(win, ". configure -x " + string actr.min.x + " -y " + string actr.min.y); +} diff --git a/appl/collab/clients/poll.b b/appl/collab/clients/poll.b new file mode 100644 index 00000000..ffb79c73 --- /dev/null +++ b/appl/collab/clients/poll.b @@ -0,0 +1,282 @@ +implement Poll; + +include "sys.m"; + sys: Sys; + +include "draw.m"; + +include "tk.m"; + tk: Tk; + +include "tkclient.m"; + tkclient: Tkclient; + +include "dialog.m"; + dialog: Dialog; + +include "arg.m"; + +Poll: module +{ + init: fn(nil: ref Draw->Context, nil: list of string); +}; + +Maxanswer: con 4; + +contents := array[] of { + "frame .f", + "frame .a", + "radiobutton .a.a1 -state disabled -variable answer -value A -text {A} -command {send entry A}", + "radiobutton .a.a2 -state disabled -variable answer -value B -text {B} -command {send entry B}", + "radiobutton .a.a3 -state disabled -variable answer -value C -text {C} -command {send entry C}", + "radiobutton .a.a4 -state disabled -variable answer -value D -text {D} -command {send entry D}", + "pack .a.a1 -side top -fill x -expand 1", + "pack .a.a2 -side top -fill x -expand 1", + "pack .a.a3 -side top -fill x -expand 1", + "pack .a.a4 -side top -fill x -expand 1", + "pack .a -side top -fill both -expand 1", + "pack .f -side top -fill both", +}; + +dbcontents := array[] of { + "text .f.t -state disabled -wrap word -yscrollcommand {.f.sb set} -height 4h", + "scrollbar .f.sb -orient vertical -command {.f.t yview}", + "pack .f.sb -side left -fill y", + "pack .f.t -side left -fill both -expand 1", +}; + +usage() +{ + sys->fprint(sys->fildes(2), "usage: poll [-d] [servicedir] pollname\n"); + raise "fail:usage"; +} + +init(ctxt: ref Draw->Context, args: list of string) +{ + sys = load Sys Sys->PATH; + tk = load Tk Tk->PATH; + tkclient = load Tkclient Tkclient->PATH; + dialog = load Dialog Dialog->PATH; + + arg := load Arg Arg->PATH; + if(arg == nil){ + sys->fprint(sys->fildes(2), "poll: can't load %s: %r\n", Arg->PATH); + raise "fail:load"; + } + arg->init(args); + debug := 0; + while((ch := arg->opt()) != 0) + case ch { + 'd' => + debug = 1; + * => + usage(); + } + args = arg->argv(); + arg = nil; + if(len args < 1) + usage(); + sys->pctl(Sys->NEWPGRP, nil); + + servicedir := "/n/remote/services"; + if(len args == 2) + (servicedir, args) = (hd args, tl args); + pollname := hd args; + + (cfd, dir, emsg) := opensvc(servicedir, "mpx", pollname); + if(cfd == nil){ + sys->fprint(sys->fildes(2), "poll: can't access poll %s: %s\n", pollname, emsg); + raise "fail:error"; + } + fd := sys->open(dir+"/leaf", Sys->ORDWR); + if(fd == nil){ + sys->fprint(sys->fildes(2), "poll: can't open %s/leaf: %r\n", dir); + raise "fail:open"; + } + + tkclient->init(); + dialog->init(); + (frame, wmctl) := tkclient->toplevel(ctxt, nil, sys->sprint("Poll %s", pollname), Tkclient->Appl); + entry := chan of string; + tk->namechan(frame, entry, "entry"); + tkcmds(frame, contents); + if(debug) + tkcmds(frame, dbcontents); + tkcmd(frame, "pack propagate . 0"); + fittoscreen(frame); + tk->cmd(frame, "update"); + tkclient->onscreen(frame, nil); + tkclient->startinput(frame, "kbd"::"ptr"::nil); + + in := chan of string; + spawn reader(fd, in); + first := 1; + lastval := -1; + qno := -1; + for(;;) + alt{ + s := <-frame.ctxt.kbd => + tk->keyboard(frame, s); + s := <-frame.ctxt.ptr => + tk->pointer(frame, *s); + s := <-frame.ctxt.ctl or + s = <-frame.wreq or + s = <-wmctl => + tkclient->wmctl(frame, s); + + msg := <-entry => + if(fd == nil){ + dialog->prompt(ctxt, frame.image, "error -fg red", "Error", "Lost connection to polling station", 0, "Dismiss"::nil); + break; + } + n := msg[0]-'A'; + lastval = n; + selectonly(frame, n, Maxanswer, "disabled"); + if(qno >= 0) { + # send our answer to the polling station + if(sys->fprint(fd, "%d %s", qno, msg) < 0){ + sys->fprint(sys->fildes(2), "poll: write error: %r\n"); + fd = nil; + } + qno = -1; # only one go at it + } + + s := <-in => + if(s != nil){ + if(debug){ + t := s; + if(!first) + t = "\n"+t; + first = 0; + tk->cmd(frame, ".f.t insert end '" + t); + tk->cmd(frame, ".f.t see end"); + tk->cmd(frame, "update"); + } + (nf, flds) := sys->tokenize(s, " "); + if(nf > 1 && hd flds == "error:"){ + dialog->prompt(ctxt, frame.image, "error -fg red", "Error", sys->sprint("polling station reports: %s", s), 0, "Dismiss"::nil); + break; + } + if(nf < 4) + break; + # seq clientid op name data + op, name: string; + flds = tl flds; # ignore seq + flds = tl flds; # ignore clientid + (op, flds) = (hd flds, tl flds); + (name, flds) = (hd flds, tl flds); + case op { + "M" => + # poll qno nanswer opt + # stop qno + selectonly(frame, -1, Maxanswer, "disabled"); + if(len flds < 2) + break; + (op, flds) = (hd flds, tl flds); + (s, flds) = (hd flds, tl flds); + case op { + "poll" => + qno = int s; + (s, flds) = (hd flds, tl flds); + n := int s; + if(n > Maxanswer) + n = Maxanswer; + if(n < 2) + n = 2; + selectonly(frame, -1, n, "normal"); + lastval = -1; + "stop" => + selectonly(frame, lastval, Maxanswer, "disabled"); + } + "L" => + dialog->prompt(ctxt, frame.image, "error -fg red", "Notice", sys->sprint("Poller (%s) has gone", name), 0, "Exit"::nil); + tkclient->wmctl(frame, "exit"); + } + }else{ + dialog->prompt(ctxt, frame.image, "error -fg red", "Notice", "Polling station closed", 0, "Exit"::nil); + tkclient->wmctl(frame, "exit"); + } + } +} + +selectonly(t: ref Tk->Toplevel, n: int, top: int, state: string) +{ + for(i := 0; i < top; i++){ + path := sys->sprint(".a.a%d", i+1); + if(i != n) + tkcmd(t, path+" deselect"); + else + tkcmd(t, path+" select"); + tkcmd(t, path+" configure -state "+state); + } + tk->cmd(t, "update"); +} + +reader(fd: ref Sys->FD, c: chan of string) +{ + buf := array[Sys->ATOMICIO] of byte; + while((n := sys->read(fd, buf, len buf)) > 0) + c <-= string buf[0:n]; + if(n < 0) + c <-= sys->sprint("error: %r"); + c <-= nil; +} + +opensvc(dir: string, svc: string, name: string): (ref Sys->FD, string, string) +{ + ctlfd := sys->open(dir+"/ctl", Sys->ORDWR); + if(ctlfd == nil) + return (nil, nil, sys->sprint("can't open %s/ctl: %r", dir)); + if(sys->fprint(ctlfd, "%s %s", svc, name) <= 0) + return (nil, nil, sys->sprint("can't access %s service %s: %r", svc, name)); + buf := array [32] of byte; + sys->seek(ctlfd, big 0, Sys->SEEKSTART); + n := sys->read(ctlfd, buf, len buf); + if (n <= 0) + return (nil, nil, sys->sprint("%s/ctl: protocol error: %r", dir)); + return (ctlfd, dir+"/"+string buf[0:n], nil); +} + +tkcmds(t: ref Tk->Toplevel, cmds: array of string) +{ + for (i := 0; i < len cmds; i++) + tkcmd(t, cmds[i]); +} + +tkcmd(t: ref Tk->Toplevel, cmd: string): string +{ + s := tk->cmd(t, cmd); + if (s != nil && s[0] == '!') + sys->fprint(sys->fildes(2), "poll: tk error: %s [%s]\n", s, cmd); + return s; +} + +fittoscreen(win: ref Tk->Toplevel) +{ + draw := load Draw Draw->PATH; + Point, Rect: import draw; + if (win.image == nil || win.image.screen == nil) + return; + r := win.image.screen.image.r; + scrsize := Point((r.max.x - r.min.x), (r.max.y - r.min.y)); + bd := int tkcmd(win, ". cget -bd"); + winsize := Point(int tkcmd(win, ". cget -actwidth") + bd * 2, int tkcmd(win, ". cget -actheight") + bd * 2); + if (winsize.x > scrsize.x) + tkcmd(win, ". configure -width " + string (scrsize.x - bd * 2)); + if (winsize.y > scrsize.y) + tkcmd(win, ". configure -height " + string (scrsize.y - bd * 2)); + actr: Rect; + actr.min = Point(int tkcmd(win, ". cget -actx"), int tkcmd(win, ". cget -acty")); + actr.max = actr.min.add((int tkcmd(win, ". cget -actwidth") + bd*2, + int tkcmd(win, ". cget -actheight") + bd*2)); + (dx, dy) := (actr.dx(), actr.dy()); + if (actr.max.x > r.max.x) + (actr.min.x, actr.max.x) = (r.min.x - dx, r.max.x - dx); + if (actr.max.y > r.max.y) + (actr.min.y, actr.max.y) = (r.min.y - dy, r.max.y - dy); + if (actr.min.x < r.min.x) + (actr.min.x, actr.max.x) = (r.min.x, r.min.x + dx); + if (actr.min.y < r.min.y) + (actr.min.y, actr.max.y) = (r.min.y, r.min.y + dy); + tkcmd(win, ". configure -x " + string actr.min.x + " -y " + string actr.min.y); +} diff --git a/appl/collab/clients/poller.b b/appl/collab/clients/poller.b new file mode 100644 index 00000000..90365606 --- /dev/null +++ b/appl/collab/clients/poller.b @@ -0,0 +1,330 @@ +implement Poller; + +include "sys.m"; + sys: Sys; + +include "draw.m"; + draw: Draw; + Rect, Point: import draw; + +include "tk.m"; + tk: Tk; + +include "tkclient.m"; + tkclient: Tkclient; + +include "dialog.m"; + dialog: Dialog; + +include "arg.m"; + +Poller: module +{ + init: fn(nil: ref Draw->Context, nil: list of string); +}; + +Maxanswer: con 4; # Tk below isn't parametrised, but could be + +contents := array[] of { + "frame .f", + "frame .f.n", + "label .f.l -anchor nw -text {Number of answers: }", + "radiobutton .f.n.a2 -text {2} -variable nanswer -value 2", + "radiobutton .f.n.a3 -text {3} -variable nanswer -value 3", + "radiobutton .f.n.a4 -text {4} -variable nanswer -value 4", + "pack .f.n.a2 .f.n.a3 .f.n.a4 -side left", + + "frame .f.b", + "button .f.b.start -text {Start} -command {send cmd start}", + "button .f.b.stop -text {Stop} -state disabled -command {send cmd stop}", + "pack .f.b.start .f.b.stop -side left", + + "canvas .f.c -height 230 -width 200", + + "pack .f.l -side top -fill x", + "pack .f.n -side top -fill x", + "pack .f.b -side top -fill x -expand 1", + "pack .f.c -side top -pady 2", + "pack .f -side top -fill both -expand 1", +}; + +dbcontents := array[] of { + "text .f.t -state disabled -wrap word -height 4h -yscrollcommand {.f.sb set}", # message log + "scrollbar .f.sb -orient vertical -command {.f.t yview}", + "pack .f.sb -side left -fill y", + "pack .f.t -side left -fill both", +}; + +Bar: adt { + frame: ref Tk->Toplevel; + canvas: string; + border: string; + inside: string; + label: string; + r: Rect; + v: real; + + draw: fn(nil: self ref Bar); +}; + +usage() +{ + sys->fprint(sys->fildes(2), "usage: poller [-d] [servicedir] pollname\n"); + raise "fail:usage"; +} + +init(ctxt: ref Draw->Context, args: list of string) +{ + sys = load Sys Sys->PATH; + draw = load Draw Draw->PATH; + tk = load Tk Tk->PATH; + tkclient = load Tkclient Tkclient->PATH; + dialog = load Dialog Dialog->PATH; + + arg := load Arg Arg->PATH; + if(arg == nil){ + sys->fprint(sys->fildes(2), "poller: can't load %s: %r\n", Arg->PATH); + raise "fail:load"; + } + arg->init(args); + debug := 0; + while((ch := arg->opt()) != 0) + case ch { + 'd' => + debug = 1; + * => + usage(); + } + args = arg->argv(); + arg = nil; + if(len args < 1) + usage(); + sys->pctl(Sys->NEWPGRP, nil); + + servicedir := "/n/remote/services"; + if(len args == 2) + (servicedir, args) = (hd args, tl args); + pollname := hd args; + + (cfd, dir, emsg) := opensvc(servicedir, "mpx", pollname); + if(cfd == nil){ + sys->fprint(sys->fildes(2), "poller: can't access polling station %s: %s\n", pollname, emsg); + raise "fail:error"; + } + fd := sys->open(dir+"/root", Sys->ORDWR); + if(fd == nil){ + sys->fprint(sys->fildes(2), "poller: can't open %s/root: %r\n", dir); + raise "fail:open"; + } + + tkclient->init(); + dialog->init(); + (frame, wmctl) := tkclient->toplevel(ctxt, nil, sys->sprint("Poller: %s", pollname), Tkclient->Appl); + cmd := chan of string; + tk->namechan(frame, cmd, "cmd"); + tkcmds(frame, contents); + if(debug) + tkcmds(frame, dbcontents); + tkcmd(frame, "pack propagate . 0"); + fittoscreen(frame); + tk->cmd(frame, "update"); + tkclient->onscreen(frame, nil); + tkclient->startinput(frame, "kbd"::"ptr"::nil); + + bars := mkbars(frame, ".f.c", Maxanswer); + count: array of int; + + in := chan of string; + spawn reader(fd, in); + first := 1; + qno := 0; + nanswer := 0; + opt := 0; + total := 0; + for(;;) + alt{ + s := <-frame.ctxt.kbd => + tk->keyboard(frame, s); + s := <-frame.ctxt.ptr => + tk->pointer(frame, *s); + s := <-frame.ctxt.ctl or + s = <-frame.wreq or + s = <-wmctl => + tkclient->wmctl(frame, s); + + c := <-cmd => + if(fd == nil){ + dialog->prompt(ctxt, frame.image, "error -fg red", "Error", "Lost connection to polling station", 0, "Dismiss"::nil); + break; + } + case c { + "start" => + s := tkcmd(frame, "variable nanswer"); + if(s == nil || s[0] == '!'){ + dialog->prompt(ctxt, frame.image, "error -fg red", "Error", "Please select number of answers", 0, "Ok"::nil); + break; + } + nanswer = int s; + count = array[Maxanswer] of {* => 0}; + total = 0; + qno++; + #opt = (int tkcmd(frame, "variable none") << 1) | int tkcmd(frame, "variable all"); + tkcmd(frame, ".f.b.start configure -state disabled"); + tkcmd(frame, ".f.b.stop configure -state normal"); + if(sys->fprint(fd, "poll %d %d %d", qno, nanswer, opt) <= 0) + sys->fprint(sys->fildes(2), "poller: write error: %r\n"); + "stop" => + tkcmd(frame, ".f.b.stop configure -state disabled"); + tkcmd(frame, "update"); + if(sys->fprint(fd, "stop %d", qno) <= 0) + sys->fprint(sys->fildes(2), "poller: write error: %r\n"); + # stop ... + tkcmd(frame, ".f.b.start configure -state normal"); + } + tk->cmd(frame, "update"); + + s := <-in => + if(s != nil){ + if(debug){ + t := s; + if(!first) + t = "\n"+t; + first = 0; + tkcmd(frame, ".f.t insert end '" + t); + tkcmd(frame, ".f.t see end"); + tkcmd(frame, "update"); + } + r := getresult(s, qno); + if(r < 0) + break; + if(r >= 0 && r < len count){ + count[r]++; + total++; + for(i:=0; i < len count; i++){ + bars[i].v = real count[i]/real total; + bars[i].draw(); + } + tk->cmd(frame, "update"); + } + #sys->print("%d %d\n", qno, r); + }else + fd = nil; + } +} + +mkbars(t: ref Tk->Toplevel, canvas: string, nbars: int): array of ref Bar +{ + x := 0; + a := array[nbars] of ref Bar; + for(i := 0; i < nbars; i++){ + b := ref Bar(t, canvas, nil, nil, nil, Rect((x,2),(x+20,202)), 0.0); + b.border = tkcmd(t, sys->sprint("%s create rectangle %d %d %d %d", + canvas, b.r.min.x,b.r.min.y,b.r.max.x,b.r.max.y)); + r := b.r.inset(1); + b.inside = tkcmd(t, sys->sprint("%s create rectangle %d %d %d %d -fill red", + canvas, r.max.x, r.max.y,r.max.x,r.max.y)); + b.label = tkcmd(t, sys->sprint("%s create text %d %d -justify center -anchor n -text '0%%", + canvas, (r.min.x+r.max.x)/2, r.max.y+4)); + a[i] = b; + x += 50; + } + tk->cmd(t, "update"); + return a; +} + +Bar.draw(b: self ref Bar) +{ + r := b.r.inset(2); + y := r.max.y - int (b.v * real r.dy()); + tkcmd(b.frame, sys->sprint("%s coords %s %d %d %d %d", + b.canvas, b.inside, r.min.x, y, r.max.x, r.max.y)); + tkcmd(b.frame, sys->sprint("%s itemconfigure %s -text '%.0f%%", + b.canvas, b.label, b.v*100.0)); +} + +getresult(msg: string, qno: int): int +{ + (nf, flds) := sys->tokenize(msg, " "); + if(nf < 5 || hd flds == "error:") + return -1; # not of interest + op := hd tl tl flds; + flds = tl tl tl flds; + if(op != "m") + return -1; # not a message from leaf + if(len flds < 3) + return -1; # bad format + flds = tl flds; # ignore user name + if(int hd flds != qno) + return -1; # not current question + result := hd tl flds; + if(result[0] >= 'A' && result[0] <= 'D') + return result[0]-'A'; + return -1; +} + +reader(fd: ref Sys->FD, c: chan of string) +{ + buf := array[Sys->ATOMICIO] of byte; + while((n := sys->read(fd, buf, len buf)) > 0) + c <-= string buf[0:n]; + if(n < 0) + c <-= sys->sprint("error: %r"); + c <-= nil; +} + +opensvc(dir: string, svc: string, name: string): (ref Sys->FD, string, string) +{ + ctlfd := sys->open(dir+"/ctl", Sys->ORDWR); + if(ctlfd == nil) + return (nil, nil, sys->sprint("can't open %s/ctl: %r", dir)); + if(sys->fprint(ctlfd, "%s %s", svc, name) <= 0) + return (nil, nil, sys->sprint("can't access %s service %s: %r", svc, name)); + buf := array [32] of byte; + sys->seek(ctlfd, big 0, Sys->SEEKSTART); + n := sys->read(ctlfd, buf, len buf); + if (n <= 0) + return (nil, nil, sys->sprint("%s/ctl: protocol error: %r", dir)); + return (ctlfd, dir+"/"+string buf[0:n], nil); +} + +tkcmds(t: ref Tk->Toplevel, cmds: array of string) +{ + for (i := 0; i < len cmds; i++) + tkcmd(t, cmds[i]); +} + +tkcmd(t: ref Tk->Toplevel, cmd: string): string +{ + s := tk->cmd(t, cmd); + if (s != nil && s[0] == '!') + sys->fprint(sys->fildes(2), "poller: tk error: %s [%s]\n", s, cmd); + return s; +} + +fittoscreen(win: ref Tk->Toplevel) +{ + if (win.image == nil || win.image.screen == nil) + return; + r := win.image.screen.image.r; + scrsize := Point((r.max.x - r.min.x), (r.max.y - r.min.y)); + bd := int tkcmd(win, ". cget -bd"); + winsize := Point(int tkcmd(win, ". cget -actwidth") + bd * 2, int tkcmd(win, ". cget -actheight") + bd * 2); + if (winsize.x > scrsize.x) + tkcmd(win, ". configure -width " + string (scrsize.x - bd * 2)); + if (winsize.y > scrsize.y) + tkcmd(win, ". configure -height " + string (scrsize.y - bd * 2)); + actr: Rect; + actr.min = Point(int tkcmd(win, ". cget -actx"), int tkcmd(win, ". cget -acty")); + actr.max = actr.min.add((int tkcmd(win, ". cget -actwidth") + bd*2, + int tkcmd(win, ". cget -actheight") + bd*2)); + (dx, dy) := (actr.dx(), actr.dy()); + if (actr.max.x > r.max.x) + (actr.min.x, actr.max.x) = (r.min.x - dx, r.max.x - dx); + if (actr.max.y > r.max.y) + (actr.min.y, actr.max.y) = (r.min.y - dy, r.max.y - dy); + if (actr.min.x < r.min.x) + (actr.min.x, actr.max.x) = (r.min.x, r.min.x + dx); + if (actr.min.y < r.min.y) + (actr.min.y, actr.max.y) = (r.min.y, r.min.y + dy); + tkcmd(win, ". configure -x " + string actr.min.x + " -y " + string actr.min.y); +} diff --git a/appl/collab/clients/whiteboard.b b/appl/collab/clients/whiteboard.b new file mode 100644 index 00000000..e05e1b45 --- /dev/null +++ b/appl/collab/clients/whiteboard.b @@ -0,0 +1,586 @@ +implement Whiteboard; + +include "sys.m"; + sys: Sys; + +include "draw.m"; + draw: Draw; + Screen, Display, Image, Rect, Point, Font: import draw; + +include "tk.m"; + tk: Tk; + +include "tkclient.m"; + tkclient: Tkclient; + +Whiteboard: module { + init: fn(ctxt: ref Draw->Context, args: list of string); +}; + +ERASEWIDTH: con 6; + + +stderr: ref Sys->FD; +srvfd: ref Sys->FD; +disp: ref Display; +font: ref Draw->Font; +drawctxt: ref Draw->Context; + +tksetup := array[] of { + "frame .f -bd 2", + "frame .c -bg white -width 234 -height 279", + "menu .penmenu", + ".penmenu add command -command {send cmd pen 0} -bitmap @/icons/whiteboard/0.bit", + ".penmenu add command -command {send cmd pen 1} -bitmap @/icons/whiteboard/1.bit", + ".penmenu add command -command {send cmd pen 2} -bitmap @/icons/whiteboard/2.bit", + ".penmenu add command -command {send cmd pen erase} -bitmap @/icons/whiteboard/erase.bit", + "menubutton .pen -menu .penmenu -bitmap @/icons/whiteboard/1.bit", + "button .colour -bg black -activebackground black -command {send cmd getcolour}", + "pack .c -in .f", + "pack .f -side top -anchor center", + "pack .pen -side left", + "pack .colour -side left -fill both -expand 1", + "update", +}; + +tkconnected := array[] of { + "bind .c <Button-1> {send cmd down %x %y}", + "bind .c <ButtonRelease-1> {send cmd up %x %y}", + "update", +}; + +init(ctxt: ref Draw->Context, args: list of string) +{ + sys = load Sys Sys->PATH; + sys->pctl(Sys->NEWPGRP, nil); + stderr = sys->fildes(2); + + draw = load Draw Draw->PATH; + tk = load Tk Tk->PATH; + + tkclient = load Tkclient Tkclient->PATH; + if (tkclient == nil) + badmod(Tkclient->PATH); + + if (len args < 2) { + sys->fprint(stderr, "Usage: whiteboard [servicedir] id\n"); + raise "fail:init"; + } + + args = tl args; + servicedir := "/n/remote/services"; + if(len args == 2) + (servicedir, args) = (hd args, tl args); + wbid := hd args; + + disp = ctxt.display; + if (disp == nil) { + sys->fprint(stderr, "bad Draw->Context\n"); + raise "fail:init"; + } + drawctxt = ctxt; + + tkclient->init(); + (win, winctl) := tkclient->toplevel(ctxt, nil, "Whiteboard", 0); + font = Font.open(disp, tkcmd(win, ". cget -font")); + if(font == nil) + font = Font.open(disp, "*default*"); + cmd := chan of string; + tk->namechan(win, cmd, "cmd"); + tkcmds(win, tksetup); + tkclient->onscreen(win, nil); + tkclient->startinput(win, "kbd" :: "ptr" :: nil); + cimage := makeimage(win); + + sc := chan of array of (Point, Point); + cc := chan of (string, ref Image, ref Sys->FD, ref Sys->FD); + connected := 0; + sfd: ref Sys->FD; + ctlfd: ref Sys->FD; # must keep this open to keep service active + + showtext(cimage, "connecting..."); + spawn connect(servicedir, wbid, cc); + + err: string; + strokeimg: ref Image; +Connect: + for (;;) alt { + (err, strokeimg, sfd, ctlfd) = <-cc => + if (err == nil) + break Connect; + else + showtext(cimage, "Error: " + err); + + s := <-winctl or + s = <-win.wreq or + s = <-win.ctxt.ctl => + oldimg := win.image; + err = tkclient->wmctl(win, s); + if(s[0] == '!' && err == nil && win.image != oldimg){ + cimage = makeimage(win); + showtext(cimage, "connecting..."); + } + p := <-win.ctxt.ptr => + tk->pointer(win, *p); + c := <-win.ctxt.kbd => + tk->keyboard(win, c); + } + + tkcmd(win, ".c configure -width " + string strokeimg.r.dx()); + tkcmd(win, ".c configure -height " + string strokeimg.r.dy()); + tkcmds(win, tkconnected); + tkcmd(win, "update"); + cimage.draw(cimage.r, strokeimg, nil, strokeimg.r.min); + + strokesin := chan of (int, int, array of Point); + strokesout := chan of (int, int, Point, Point); + spawn reader(sfd, strokesin); + spawn writer(sfd, strokesout); + + pendown := 0; + p0, p1: Point; + + getcolour := 0; + white := disp.white; + whitepen := disp.newimage(Rect(Point(0,0), Point(1,1)), Draw->CMAP8, 1, Draw->White); + pencolour := Draw->Black; + penwidth := 1; + erase := 0; + drawpen := disp.newimage(Rect(Point(0,0), Point(1,1)), Draw->CMAP8, 1, pencolour); + + for (;;) alt { + s := <-winctl or + s = <-win.ctxt.ctl or + s = <-win.wreq => + oldimg := win.image; + err = tkclient->wmctl(win, s); + if(s[0] == '!' && err == nil && win.image != oldimg){ + cimage = makeimage(win); + cimage.draw(cimage.r, strokeimg, nil, strokeimg.r.min); + } + p := <-win.ctxt.ptr => + tk->pointer(win, *p); + c := <-win.ctxt.kbd => + tk->keyboard(win, c); + (colour, width, strokes) := <-strokesin => + if (strokes == nil) + tkclient->settitle(win, "Whiteboard (Disconnected)"); + else { + pen := disp.newimage(Rect(Point(0,0), Point(1,1)), Draw->CMAP8, 1, colour); + drawstrokes(cimage, cimage.r.min, pen, width, strokes); + drawstrokes(strokeimg, strokeimg.r.min, pen, width, strokes); + } + + c := <-cmd => + (nil, toks) := sys->tokenize(c, " "); + case hd toks { + "down" => + toks = tl toks; + x := int hd toks; + y := int hd tl toks; + if (!pendown) { + pendown = 1; + p0 = Point(x, y); + continue; + } + p1 = Point(x, y); + if (p1.x == p0.x && p1.y == p0.y) + continue; + pen := drawpen; + colour := pencolour; + width := penwidth; + if (erase) { + pen = whitepen; + colour = Draw->White; + width = ERASEWIDTH; + } + drawstroke(cimage, cimage.r.min, p0, p1, pen, width); + drawstroke(strokeimg, strokeimg.r.min, p0, p1, pen, width); + strokesout <-= (colour, width, p0, p1); + p0 = p1; + "up" => + pendown = 0; + + "getcolour" => + pendown = 0; + if (!getcolour) + spawn colourmenu(cmd); + "colour" => + pendown = 0; + getcolour = 0; + toks = tl toks; + if (toks == nil) + # colourmenu was dismissed + continue; + erase = 0; + tkcmd(win, ".pen configure -bitmap @/icons/whiteboard/" + string penwidth + ".bit"); + tkcmd(win, "update"); + pencolour = int hd toks; + toks = tl toks; + tkcolour := hd toks; + drawpen = disp.newimage(Rect(Point(0,0), Point(1,1)), Draw->CMAP8, 1, pencolour); + tkcmd(win, ".colour configure -bg " + tkcolour + " -activebackground " + tkcolour); + tkcmd(win, "update"); + + "pen" => + pendown = 0; + p := hd tl toks; + i := ""; + if (p == "erase") { + erase = 1; + i = "erase.bit"; + } else { + erase = 0; + penwidth = int p; + i = p + ".bit"; + } + tkcmd(win, ".pen configure -bitmap @/icons/whiteboard/" + i); + tkcmd(win, "update"); + } + + } +} + +makeimage(win: ref Tk->Toplevel): ref Draw->Image +{ + if(win.image == nil) + return nil; + scr := Screen.allocate(win.image, win.image.display.white, 0); + w := scr.newwindow(tk->rect(win, ".c", Tk->Local), Draw->Refnone, Draw->Nofill); + return w; +} + +showtext(img: ref Image, s: string) +{ + r := img.r; + r.max.y = img.r.min.y + font.height; + img.draw(r, disp.white, nil, (0, 0)); + img.text(r.min, disp.black, (0, 0), font, s); +} + +penmenu(t: ref Tk->Toplevel, p: Point) +{ + topy := int tkcmd(t, ".penmenu yposition 0"); + boty := int tkcmd(t, ".penmenu yposition end"); + dy := boty - topy; + p.y -= dy; + tkcmd(t, ".penmenu post " + string p.x + " " + string p.y); +} + +colourcmds := array[] of { + "label .l -height 10", + "frame .c -height 224 -width 224", + "pack .l -fill x -expand 1", + "pack .c -side bottom -fill both -expand 1", + "pack propagate . 0", + "bind .c <Button-1> {send cmd push %x %y}", + "bind .c <ButtonRelease-1> {send cmd release}", +}; + +lastcolour := "255"; +lasttkcolour := "#000000"; + +colourmenu(c: chan of string) +{ + (t, winctl) := tkclient->toplevel(drawctxt, nil, "Whiteboard", Tkclient->OK); + cmd := chan of string; + tk->namechan(t, cmd, "cmd"); + tkcmds(t, colourcmds); + tkcmd(t, ".l configure -bg " + lasttkcolour); + tkcmd(t, "update"); + tkclient->onscreen(t, "onscreen"); + tkclient->startinput(t, "kbd" :: "ptr" :: nil); + + drawcolours(t.image, tk->rect(t, ".c", Tk->Local)); + + for(;;) alt { + p := <-t.ctxt.ptr => + tk->pointer(t, *p); + s := <-t.ctxt.kbd => + tk->keyboard(t, s); + s := <-winctl or + s = <-t.ctxt.ctl or + s = <-t.wreq => + case s{ + "ok" => + c <-= "colour " + lastcolour + " " + lasttkcolour; + return; + "exit" => + c <-= "colour"; + return; + * => + oldimage := t.image; + e := tkclient->wmctl(t, s); + if(s[0] == '!' && e == nil && oldimage != t.image) + drawcolours(t.image, tk->rect(t, ".c", Tk->Local)); + } + + press := <-cmd => + (n, word) := sys->tokenize(press, " "); + case hd word { + "push" => + (lastcolour, lasttkcolour) = color(int hd tl word, int hd tl tl word, tk->rect(t, ".c", 0).size()); + tkcmd(t, ".l configure -bg " + lasttkcolour); + } + } +} + +drawcolours(img: ref Image, cr: Rect) +{ + # use writepixels because it's much faster than allocating all those colors. + tmp := disp.newimage(((0,0),(cr.dx(),cr.dy()/16+1)), Draw->CMAP8, 0, 0); + if(tmp == nil) + return; + buf := array[tmp.r.dx()*tmp.r.dy()] of byte; + dx := cr.dx(); + dy := cr.dy(); + for(y:=0; y<16; y++){ + for(i:=tmp.r.dx()-1; i>=0; --i) + buf[i] = byte (16*y+(16*i)/dx); + for(k:=tmp.r.dy()-1; k>=1; --k) + buf[dx*k:] = buf[0:dx]; + tmp.writepixels(tmp.r, buf); + r: Rect; + r.min.x = cr.min.x; + r.max.x = cr.max.x; + r.min.y = cr.min.y+(dy*y)/16; + r.max.y = cr.min.y+(dy*(y+1))/16; + img.draw(r, tmp, nil, tmp.r.min); + } +} + +color(x, y: int, size: Point): (string, string) +{ + x = (16*x)/size.x; + y = (16*y)/size.y; + col := 16*y+x; + (r, g, b) := disp.cmap2rgb(col); + tks := sys->sprint("#%.2x%.2x%.2x", r, g, b); + return (string disp.cmap2rgba(col), tks); +} + +opensvc(dir: string, svc: string, name: string): (ref Sys->FD, string, string) +{ + ctlfd := sys->open(dir+"/ctl", Sys->ORDWR); + if(ctlfd == nil) + return (nil, nil, sys->sprint("can't open %s/ctl: %r", dir)); + if(sys->fprint(ctlfd, "%s %s", svc, name) <= 0) + return (nil, nil, sys->sprint("can't access %s service %s: %r", svc, name)); + buf := array [32] of byte; + sys->seek(ctlfd, big 0, Sys->SEEKSTART); + n := sys->read(ctlfd, buf, len buf); + if (n <= 0) + return (nil, nil, sys->sprint("%s/ctl: protocol error: %r", dir)); + return (ctlfd, dir+"/"+string buf[0:n], nil); +} + +connect(dir, name: string, res: chan of (string, ref Image, ref Sys->FD, ref Sys->FD)) +{ + (ctlfd, srvdir, emsg) := opensvc(dir, "whiteboard", name); + if(ctlfd == nil) { + res <-= (emsg, nil, nil, nil); + return; + } + + bitpath := srvdir + "/wb.bit"; + strokepath := srvdir + "/strokes"; + + sfd := sys->open(strokepath, Sys->ORDWR); + if (sfd == nil) { + err := sys->sprint("cannot open whiteboard data: %r"); + res <-= (err, nil, nil, nil); + srvfd = nil; + return; + } + + bfd := sys->open(bitpath, Sys->OREAD); + if (bfd == nil) { + err := sys->sprint("cannot open whiteboard image: %r"); + res <-= (err, nil, nil, nil); + srvfd = nil; + return; + } + + img := disp.readimage(bfd); + if (img == nil) { + err := sys->sprint("cannot read whiteboard image: %r"); + res <-= (err, nil, nil, nil); + srvfd = nil; + return; + } +sys->print("read image ok\n"); + + # make sure image is depth 8 (because of image.line() bug) + if (img.depth != 8) { +sys->print("depth is %d, not 8\n", img.depth); + nimg := disp.newimage(img.r, Draw->CMAP8, 0, 0); + if (nimg == nil) { + res <-= ("cannot allocate local image", nil, nil, nil); + srvfd = nil; + return; + } + nimg.draw(nimg.r, img, nil, img.r.min); + img = nimg; + } + + res <-= (nil, img, sfd, ctlfd); +} + +reader(fd: ref Sys->FD, sc: chan of (int, int, array of Point)) +{ + buf := array [Sys->ATOMICIO] of byte; + + for (;;) { + n := sys->read(fd, buf, len buf); + if (n <= 0) { + sc <-= (0, 0, nil); + return; + } + s := string buf[0:n]; + (npts, toks) := sys->tokenize(s, " "); + if (npts & 1) + # something wrong + npts--; + if (npts < 6) + # ignore + continue; + + colour, width: int; + (colour, toks) = (int hd toks, tl toks); + (width, toks) = (int hd toks, tl toks); + pts := array [(npts - 2)/ 2] of Point; + for (i := 0; toks != nil; i++) { + x, y: int; + (x, toks) = (int hd toks, tl toks); + (y, toks) = (int hd toks, tl toks); + pts[i] = Point(x, y); + } + sc <-= (colour, width, pts); + pts = nil; + } +} + +Wmsg: adt { + data: array of byte; + datalen: int; + next: cyclic ref Wmsg; +}; + +writer(fd: ref Sys->FD, sc: chan of (int, int, Point, Point)) +{ + lastcol := -1; + lastw := -1; + lastpt := Point(-1, -1); + curmsg: ref Wmsg; + nextmsg: ref Wmsg; + + eofc := chan of int; + wc := chan of ref Wmsg; + wseof := 0; + spawn wslave(fd, wc, eofc); + + for (;;) { + colour := -1; + width := 0; + p0, p1: Point; + + if (curmsg == nil || wseof) + (colour, width, p0, p1) = <-sc; + else alt { + wseof = <-eofc => + ; + + (colour, width, p0, p1) = <-sc => + ; + + wc <-= curmsg => + curmsg = curmsg.next; + continue; + } + + newseq := 0; + if (curmsg == nil) { + curmsg = ref Wmsg(array [Sys->ATOMICIO] of byte, 0, nil); + nextmsg = curmsg; + newseq = 1; + } + + if (colour != lastcol || width != lastw || p0.x != lastpt.x || p0.y != lastpt.y) + newseq = 1; + + d: array of byte = nil; + if (!newseq) { + d = sys->aprint(" %d %d", p1.x, p1.y); + if (nextmsg.datalen + len d >= Sys->ATOMICIO) { + nextmsg.next = ref Wmsg(array [Sys->ATOMICIO] of byte, 0, nil); + nextmsg = nextmsg.next; + newseq = 1; + } + } + if (newseq) { + d = sys->aprint(" %d %d %d %d %d %d", colour, width, p0.x, p0.y, p1.x, p1.y); + if (nextmsg.datalen != 0) { + nextmsg.next = ref Wmsg(array [Sys->ATOMICIO] of byte, 0, nil); + nextmsg = nextmsg.next; + } + } + nextmsg.data[nextmsg.datalen:] = d; + nextmsg.datalen += len d; + lastcol = colour; + lastw = width; + lastpt = p1; + } +} + +wslave(fd: ref Sys->FD, wc: chan of ref Wmsg, eof: chan of int) +{ + for (;;) { + wm := <-wc; + n := sys->write(fd, wm.data, wm.datalen); + if (n != wm.datalen) + break; + } + eof <-= 1; +} + +drawstroke(img: ref Image, offset, p0, p1: Point, pen: ref Image, width: int) +{ + p0 = p0.add(offset); + p1 = p1.add(offset); + img.line(p0, p1, Draw->Endsquare, Draw->Endsquare, width, pen, p0); +} + +drawstrokes(img: ref Image, offset: Point, pen: ref Image, width: int, pts: array of Point) +{ + if (len pts < 2) + return; + p0, p1: Point; + p0 = pts[0].add(offset); + for (i := 1; i < len pts; i++) { + p1 = pts[i].add(offset); + img.line(p0, p1, Draw->Endsquare, Draw->Endsquare, width, pen, p0); + p0 = p1; + } +} + +badmod(mod: string) +{ + sys->fprint(stderr, "cannot load %s: %r\n", mod); + raise "fail:bad module"; +} + +tkcmd(t: ref Tk->Toplevel, cmd: string): string +{ + s := tk->cmd(t, cmd); + if (s != nil && s[0] == '!') { + sys->fprint(stderr, "%s\n", cmd); + sys->fprint(stderr, "tk error: %s\n", s); + } + return s; +} + +tkcmds(t: ref Tk->Toplevel, cmds: array of string) +{ + for (i := 0; i < len cmds; i++) + tkcmd(t, cmds[i]); +} |
