shithub: qk1

Download patch

ref: 89551236f63043869f9f6aa6d030cb72f7a6cc00
parent: c2ef159ea62d1d4c9188624809e9741fc469537d
author: qwx <>
date: Wed Dec 12 18:28:18 EST 2018

qw: use bio for qwsv input

--- a/qw/in.c
+++ b/qw/in.c
@@ -33,45 +33,11 @@
 static int mΔx, mΔy, mb, oldmb;
 
 void
-conscmd(void)
-{
-	char *p;
-
-	if(cls.state != ca_dedicated)
-		return;
-	while(p = nbrecvp(inchan), p != nil){
-		Cbuf_AddText(p);
-		free(p);
-	}
-}
-
-static void
-cproc(void *)
-{
-	char *s;
-	Biobuf *bf;
-
-	if(bf = Bfdopen(0, OREAD), bf == nil)
-		sysfatal("Bfdopen: %r");
-	for(;;){
-		if(s = Brdstr(bf, '\n', 1), s == nil)
-			break;
-		if(sendp(inchan, s) < 0){
-			free(s);
-			break;
-		}
-	}
-	Bterm(bf);
-}
-
-void
 Sys_SendKeyEvents(void)
 {
 	Kev ev;
 	int r;
 
-	if(cls.state == ca_dedicated)
-		return;
 	if(oldmwin != (int)m_windowed.value){
 		oldmwin = (int)m_windowed.value;
 		IN_Grabm(oldmwin);
@@ -88,7 +54,7 @@
 {
 	int b, i, k, r;
 
-	if(!mouseon || cls.state == ca_dedicated)
+	if(!mouseon)
 		return;
 	qlock(&mlck);
 	b = mb;
@@ -332,13 +298,6 @@
 void
 IN_Init(void)
 {
-	if(cls.state == ca_dedicated){
-		if(inchan = chancreate(sizeof(void *), 2), inchan == nil)
-			sysfatal("chancreate: %r");
-		if(proccreate(cproc, nil, 8192) < 0)
-			sysfatal("proccreate iproc: %r");
-		return;
-	}
 	Cvar_RegisterVariable(&m_windowed);
 	Cvar_RegisterVariable(&m_filter);
 	if(inchan = chancreate(sizeof(Kev), Nbuf), inchan == nil)
--- a/qw/qwsv.c
+++ b/qw/qwsv.c
@@ -2,6 +2,7 @@
 #include <libc.h>
 #include <stdio.h>
 #include <thread.h>
+#include <bio.h>
 #include "quakedef.h"
 
 mainstacksize = 256*1024;
@@ -11,7 +12,7 @@
 
 quakeparms_t q;
 
-static int iop = -1, pfd[2];
+static Channel *inchan;
 
 /* FIXME: stupid-ass linking kludges */
 client_static_t	cls;
@@ -22,47 +23,27 @@
 char *
 Sys_ConsoleInput(void)
 {
-	static char buf[256];
-
-	if(iop < 0)
-		return nil;
-	if(flen(pfd[1]) < 1)	/* only poll for input */
-		return nil;
-	if(read(pfd[1], buf, sizeof buf) < 0)
-		sysfatal("Sys_ConsoleInput:read: %r");
-	return buf;
+	return nbrecvp(inchan);
 }
 
-void
-killiop(void)
-{
-	if(iop < 0)
-		return;
-	threadkillgrp(THin);
-	close(pfd[0]);
-	close(pfd[1]);
-	iop = -1;
-}
-
 static void
-iproc(void *)
+cproc(void *)
 {
-	int n;
-	char s[256];
+	char *s;
+	Biobuf *bf;
 
-	threadsetgrp(THin);
-
-	if((iop = pipe(pfd)) < 0)
-		sysfatal("iproc:pipe: %r");
+	if(bf = Bfdopen(0, OREAD), bf == nil)
+		sysfatal("Bfdopen: %r");
 	for(;;){
-		if((n = read(0, s, sizeof s)) <= 0)
+		if(s = Brdstr(bf, '\n', 1), s == nil)
 			break;
-		s[n-1] = 0;
-		if((write(pfd[0], s, n)) != n)
+		if(sendp(inchan, s) < 0){
+			free(s);
 			break;
+		}
 		send(echan, nil);
 	}
-	iop = -1;
+	Bterm(bf);
 }
 
 void
@@ -74,10 +55,11 @@
 	svonly = 1;
 	COM_InitArgv (argc, argv);
 	initparm(&q);
-	if((echan = chancreate(sizeof(int), 1)) == nil)
+	if((echan = chancreate(sizeof(int), 1)) == nil
+	|| (inchan = chancreate(sizeof(void *), 2)) == nil)
 		sysfatal("chancreate: %r");
 	SV_Init(&q);
-	if(proccreate(iproc, nil, 8192) < 0)
+	if(proccreate(cproc, nil, 8192) < 0)
 		sysfatal("proccreate iproc: %r");
 	SV_Frame(0.1);	/* run one frame immediately for first heartbeat */
 	oldtime = Sys_DoubleTime() - 0.1;
--- a/qw/sv_main.c
+++ b/qw/sv_main.c
@@ -77,7 +77,6 @@
 */
 void SV_Shutdown (void)
 {
-	killiop();
 	Master_Shutdown ();
 	if (sv_logfile)
 	{
@@ -1086,14 +1085,11 @@
 */
 void SV_GetConsoleCommands (void)
 {
-	char	*cmd;
+	char *p;
 
-	while (1)
-	{
-		cmd = Sys_ConsoleInput ();
-		if (!cmd)
-			break;
-		Cbuf_AddText (cmd);
+	while((p = Sys_ConsoleInput()) != nil){
+		Cbuf_AddText(p);
+		free(p);
 	}
 }
 
--- a/qw/sys.h
+++ b/qw/sys.h
@@ -26,5 +26,4 @@
 void Sys_Init(void);
 void initparm(quakeparms_t *);
 
-void killiop(void);
 void*	emalloc(ulong);