aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2021-08-04 19:23:46 +0000
committerrodri <rgl@antares-labs.eu>2021-08-04 19:23:46 +0000
commit854f2430801958cdbd14c86715814a9d9a097ac4 (patch)
treea81e8f1e963e6513f70f4c7a5dd055ea61b0a264
parent1059aa8707299a104a0ba556d293436d960e0006 (diff)
downloadmusw-854f2430801958cdbd14c86715814a9d9a097ac4.tar.gz
musw-854f2430801958cdbd14c86715814a9d9a097ac4.tar.bz2
musw-854f2430801958cdbd14c86715814a9d9a097ac4.zip
implemented keyboard key send/recv procedures.
-rw-r--r--musw.c24
-rw-r--r--muswd.c26
-rw-r--r--pack.c22
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
@@ -29,9 +29,28 @@ iolisten(Ioproc *io, char *adir, char *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: