From 854f2430801958cdbd14c86715814a9d9a097ac4 Mon Sep 17 00:00:00 2001 From: rodri Date: Wed, 4 Aug 2021 19:23:46 +0000 Subject: implemented keyboard key send/recv procedures. --- musw.c | 24 +++++++++++++++++++++++- muswd.c | 26 +++++++++++++++++++++++--- pack.c | 22 ++++++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/musw.c b/musw.c index 330695d..6b42bf7 100644 --- a/musw.c +++ b/musw.c @@ -40,6 +40,7 @@ RFrame screenrf; Ball bouncer; Universe *universe; VModel *needlemdl; +Channel *kchan; char winspec[32]; int debug; @@ -194,6 +195,8 @@ kbdproc(void *) if(debug) fprint(2, "kdown %.*lub\n", sizeof(kdown)*8, kdown); + + nbsendul(kchan, kdown); } } @@ -204,7 +207,7 @@ threadnetrecv(void *arg) int fd, n; Ioproc *io; - fd = *((int*)arg); + fd = *(int*)arg; io = ioproc(); while((n = ioread(io, fd, buf, sizeof buf)) > 0){ @@ -216,6 +219,23 @@ threadnetrecv(void *arg) closeioproc(io); } +void +threadnetsend(void *arg) +{ + uchar buf[1024]; + int fd, n; + ulong kdown; + + fd = *(int*)arg; + + for(;;){ + kdown = recvul(kchan); + n = pack(buf, sizeof buf, "k", kdown); + if(write(fd, buf, n) != n) + sysfatal("write: %r"); + } +} + void resize(void); void @@ -320,6 +340,7 @@ threadmain(int argc, char *argv[]) screenrf.bx = Vec2(1, 0); screenrf.by = Vec2(0,-1); + kchan = chancreate(sizeof kdown, 1); proccreate(kbdproc, nil, 4096); fd = dial(server, nil, nil, nil); @@ -335,6 +356,7 @@ threadmain(int argc, char *argv[]) universe->star.spr = readsprite("assets/spr/earth.pic", ZP, Rect(0,0,32,32), 5, 20e3); threadcreate(threadnetrecv, &fd, 4096); + threadcreate(threadnetsend, &fd, 4096); threadcreate(threadresize, mc, 4096); then = nanosec(); diff --git a/muswd.c b/muswd.c index 6b0cdf2..88d1bb7 100644 --- a/muswd.c +++ b/muswd.c @@ -28,10 +28,29 @@ iolisten(Ioproc *io, char *adir, char *ldir) return iocall(io, _iolisten, adir, ldir); } +void +procnetrecv(void *arg) +{ + uchar buf[1024]; + int fd, n; + ulong kdown; + Ioproc *io; + + fd = *(int*)arg; + io = ioproc(); + + while((n = ioread(io, fd, buf, sizeof buf)) > 0){ + unpack(buf, n, "k", &kdown); + fprint(2, "%d (%d) [%d] rcvd %.*lub\n", threadid(), threadpid(threadid()), getpid(), sizeof(kdown)*8, kdown); + } + closeioproc(io); + threadexits(nil); +} + void threadlisten(void *arg) { - int lcfd, dfd; + int lcfd, dfd, pid; char *adir, ldir[40]; Ioproc *io; @@ -56,9 +75,10 @@ threadlisten(void *arg) lobby->takeseat(lobby, ldir, lcfd, dfd); + pid = proccreate(procnetrecv, &dfd, 4096); + if(debug) - fprint(2, "added conn for %lud conns at %lud max\n", - lobby->nseats, lobby->cap); + fprint(2, "forked %d for new conn\n", pid); } } diff --git a/pack.c b/pack.c index 50d72f1..be9b170 100644 --- a/pack.c +++ b/pack.c @@ -21,6 +21,7 @@ static int vpack(uchar *p, int n, char *fmt, va_list a) { uchar *p0 = p, *e = p+n; + ulong k; FPdbleword d; Point2 P; @@ -45,6 +46,16 @@ vpack(uchar *p, int n, char *fmt, va_list a) goto err; pack(p, n, "ddd", P.x, P.y, P.w), p += 3*8; + + break; + case 'k': + k = va_arg(a, ulong); + + if(p+4 > e) + goto err; + + put4(p, k), p += 4; + break; } } @@ -56,6 +67,7 @@ static int vunpack(uchar *p, int n, char *fmt, va_list a) { uchar *p0 = p, *e = p+n; + ulong k; FPdbleword d; Point2 P; @@ -78,6 +90,16 @@ vunpack(uchar *p, int n, char *fmt, va_list a) unpack(p, n, "ddd", &P.x, &P.y, &P.w), p += 3*8; *va_arg(a, Point2*) = P; + + break; + case 'k': + if(p+4 > e) + goto err; + + k = get4(p), p += 4; + *va_arg(a, ulong*) = k; + + break; } } err: -- cgit v1.2.3