shithub: musw

Download patch

ref: 854f2430801958cdbd14c86715814a9d9a097ac4
parent: 1059aa8707299a104a0ba556d293436d960e0006
author: rodri <rgl@antares-labs.eu>
date: Wed Aug 4 15:23:46 EDT 2021

implemented keyboard key send/recv procedures.

--- a/musw.c
+++ b/musw.c
@@ -40,6 +40,7 @@
 Ball bouncer;
 Universe *universe;
 VModel *needlemdl;
+Channel *kchan;
 char winspec[32];
 int debug;
 
@@ -194,6 +195,8 @@
 		if(debug)
 			fprint(2, "kdown %.*lub\n",
 				sizeof(kdown)*8, kdown);
+
+		nbsendul(kchan, kdown);
 	}
 }
 
@@ -204,7 +207,7 @@
 	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 @@
 	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 @@
 	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 @@
 	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();
--- a/muswd.c
+++ b/muswd.c
@@ -29,9 +29,28 @@
 }
 
 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 @@
 
 		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);
 	}
 }
 
--- a/pack.c
+++ b/pack.c
@@ -21,6 +21,7 @@
 vpack(uchar *p, int n, char *fmt, va_list a)
 {
 	uchar *p0 = p, *e = p+n;
+	ulong k;
 	FPdbleword d;
 	Point2 P;
 
@@ -45,7 +46,17 @@
 				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;
 		}
 	}
 err:
@@ -56,6 +67,7 @@
 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 @@
 
 			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: