shithub: qk1

Download patch

ref: 4f671e3574df2fba337e601646d31e206f733abc
parent: 779ab09145bc25536e93051873d59b3fce8fc761
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Dec 27 23:37:04 EST 2023

cd: split/group platform-specific/independent logic

--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,7 @@
 
 OBJS=\
 	3rd/parg/parg.o\
+	cd.o\
 	chase.o\
 	cl_demo.o\
 	cl_input.o\
--- a/cd.c
+++ b/cd.c
@@ -1,114 +1,11 @@
 #include "quakedef.h"
-#include <thread.h>
-#include <regexp.h>
 
-cvar_t bgmvolume = {"bgmvolume", "1", 1};
+cvar_t bgmvolume = {"bgmvolume", "0.5", true};
 
-static char cdfile[13];
-static int trk, chtrk, ntrk;
-static int cdread, cdloop;
+int cdtrk = 0, cdntrk = 0;
+bool cdloop = false;
 
-static void
-cproc(void *)
-{
-	int n, afd, fd, v;
-	char f[32];
-	s16int buf[4096];
-	short *p;
-	double vol;
-
-	if((afd = open("/dev/audio", OWRITE)) < 0){
-		Con_DPrintf("cd: open: %r\n");
-		return;
-	}
-	fd = -1;
-	for(;;){
-		if(chtrk > 0){
-			close(fd);
-			trk = chtrk;
-			snprint(f, sizeof f, "%s%03ud", cdfile, trk);
-			if((fd = open(f, OREAD)) < 0)
-				Con_DPrintf("cd: open: %r\n");
-			chtrk = 0;
-		}
-		if(!cdread || fd < 0){
-			sleep(1);
-			continue;
-		}
-		if((n = read(fd, buf, sizeof buf)) < 0)
-			break;
-		if(n == 0){
-			if(cdloop)
-				seek(fd, 0, 0);
-			else{
-				close(fd);
-				fd = -1;
-			}
-			continue;
-		}
-		vol = bgmvolume.value;
-		for(p=buf; p<buf+n/sizeof*buf; p++){
-			v = *p * vol;
-			if(v > 0x7fff)
-				v = 0x7fff;
-			else if(v < -0x8000)
-				v = -0x8000;
-			*p = v;
-		}
-		if(write(afd, buf, n) != n)
-			break;
-	}
-}
-
 void
-stopcd(void)
-{
-	cdread = 0;
-	cdloop = 0;
-}
-
-void
-pausecd(void)
-{
-	cdread = 0;
-}
-
-void
-resumecd(void)
-{
-	cdread = 1;
-}
-
-void
-shutcd(void)
-{
-	stopcd();
-}
-
-void
-stepcd(void)
-{
-	if(bgmvolume.value <= 0.0 || cdread == 0)
-		return;
-	cdread = bgmvolume.value > 0.0;
-}
-
-void
-playcd(int nt, int loop)
-{
-	if(ntrk < 1)
-		return;
-	nt -= 1;	/* d001 assumed part of track list */
-	if(nt < 1 || nt > ntrk){
-		Con_Printf("cd: invalid track number %d\n", nt);
-		return;
-	}
-	chtrk = nt;
-	cdloop = loop;
-	cdread = bgmvolume.value > 0.0;
-}
-
-static void
 cdcmd(void)
 {
 	char *c;
@@ -134,51 +31,7 @@
 	else if(cistrcmp(c, "resume") == 0)
 		resumecd();
 	else if(cistrcmp(c, "info") == 0)
-		Con_Printf("track %d/%d; loop %d; vol %.1f\n", trk, ntrk, cdloop, bgmvolume.value);
+		Con_Printf("track %d/%d; loop %d; vol %.1f\n", cdtrk, cdntrk, cdloop, bgmvolume.value);
 	else
 		goto usage;
-}
-
-static int
-cdinfo(void)
-{
-	int fd, i, n;
-	char type;
-	Reprog *pat;
-	Dir *d;
-
-	ntrk = 0;
-	if((fd = open("/mnt/cd", OREAD)) < 0)
-		return -1;
-	n = dirreadall(fd, &d);
-	close(fd);
-	if(n < 0)
-		return -1;
-	pat = regcomp("[au][0-9][0-9][0-9]");
-	for(type=0, i=0; i<n; i++)
-		if(regexec(pat, d[i].name, nil, 0)){
-			if(type == 0)
-				type = d[i].name[0];
-			ntrk++;
-		}
-	free(pat);
-	free(d);
-	if(ntrk < 1)
-		return -1;
-	snprint(cdfile, sizeof cdfile, "/mnt/cd/%c", type);
-	return 0;
-}
-
-int
-initcd(void)
-{
-	if(cdinfo() < 0){
-		Con_DPrintf("cdinfo: %r\n");
-		return -1;
-	}
-	if(proccreate(cproc, nil, 16384) < 0)
-		sysfatal("proccreate: %r");
-	Cvar_RegisterVariable(&bgmvolume);
-	Cmd_AddCommand("cd", cdcmd);
-	return 0;
 }
--- /dev/null
+++ b/cd_plan9.c
@@ -1,0 +1,150 @@
+#include "quakedef.h"
+#include <thread.h>
+#include <regexp.h>
+
+static char cdfile[13];
+static int chtrk, cdread;
+
+static void
+cproc(void *)
+{
+	int n, afd, fd, v;
+	char f[32];
+	s16int buf[4096];
+	short *p;
+	double vol;
+
+	if((afd = open("/dev/audio", OWRITE)) < 0){
+		Con_DPrintf("cd: open: %r\n");
+		return;
+	}
+	fd = -1;
+	for(;;){
+		if(chtrk > 0){
+			close(fd);
+			cdtrk = chtrk;
+			snprint(f, sizeof f, "%s%03ud", cdfile, cdtrk);
+			if((fd = open(f, OREAD)) < 0)
+				Con_DPrintf("cd: open: %r\n");
+			chtrk = 0;
+		}
+		if(!cdread || fd < 0){
+			sleep(1);
+			continue;
+		}
+		if((n = read(fd, buf, sizeof buf)) < 0)
+			break;
+		if(n == 0){
+			if(cdloop)
+				seek(fd, 0, 0);
+			else{
+				close(fd);
+				fd = -1;
+			}
+			continue;
+		}
+		vol = bgmvolume.value;
+		for(p=buf; p<buf+n/sizeof*buf; p++){
+			v = *p * vol;
+			if(v > 0x7fff)
+				v = 0x7fff;
+			else if(v < -0x8000)
+				v = -0x8000;
+			*p = v;
+		}
+		if(write(afd, buf, n) != n)
+			break;
+	}
+}
+
+void
+stopcd(void)
+{
+	cdread = 0;
+	cdloop = 0;
+}
+
+void
+pausecd(void)
+{
+	cdread = 0;
+}
+
+void
+resumecd(void)
+{
+	cdread = 1;
+}
+
+void
+shutcd(void)
+{
+	stopcd();
+}
+
+void
+stepcd(void)
+{
+	if(bgmvolume.value <= 0.0 || cdread == 0)
+		return;
+	cdread = bgmvolume.value > 0.0;
+}
+
+void
+playcd(int nt, bool loop)
+{
+	if(cdntrk < 1)
+		return;
+	nt -= 1;	/* d001 assumed part of track list */
+	if(nt < 1 || nt > cdntrk){
+		Con_Printf("cd: invalid track number %d\n", nt);
+		return;
+	}
+	chtrk = nt;
+	cdloop = loop;
+	cdread = bgmvolume.value > 0.0;
+}
+
+static int
+cdinfo(void)
+{
+	int fd, i, n;
+	char type;
+	Reprog *pat;
+	Dir *d;
+
+	cdntrk = 0;
+	if((fd = open("/mnt/cd", OREAD)) < 0)
+		return -1;
+	n = dirreadall(fd, &d);
+	close(fd);
+	if(n < 0)
+		return -1;
+	pat = regcomp("[au][0-9][0-9][0-9]");
+	for(type=0, i=0; i<n; i++)
+		if(regexec(pat, d[i].name, nil, 0)){
+			if(type == 0)
+				type = d[i].name[0];
+			cdntrk++;
+		}
+	free(pat);
+	free(d);
+	if(cdntrk < 1)
+		return -1;
+	snprint(cdfile, sizeof cdfile, "/mnt/cd/%c", type);
+	return 0;
+}
+
+int
+initcd(void)
+{
+	if(cdinfo() < 0){
+		Con_DPrintf("cdinfo: %r\n");
+		return -1;
+	}
+	if(proccreate(cproc, nil, 16384) < 0)
+		sysfatal("proccreate: %r");
+	Cvar_RegisterVariable(&bgmvolume);
+	Cmd_AddCommand("cd", cdcmd);
+	return 0;
+}
--- a/dat.h
+++ b/dat.h
@@ -57,4 +57,9 @@
 #define Spktvol 255.0
 #define Spktatt 1.0
 
+extern cvar_t bgmvolume;
+extern int cdtrk, cdntrk;
+extern bool cdloop;
+
+
 extern int debug;
--- a/fns.h
+++ b/fns.h
@@ -9,7 +9,9 @@
 void	stopcd(void);
 void	pausecd(void);
 void	resumecd(void);
-void	playcd(int, int);
+void	playcd(int, bool);
+void	cdcmd(void);
+void	shutcd(void);
 void	stopallsfx(void);
 void	stopsfx(int, int);
 void	startsfx(int, int, Sfx *, vec3_t, float, float);
@@ -19,7 +21,6 @@
 void	sfxend(void);
 void	touchsfx(char *);
 Sfx*	precachesfx(char *);
-void	shutcd(void);
 void	shutsnd(void);
 int	initcd(void);
 int	initsnd(void);
--- a/mkfile
+++ b/mkfile
@@ -8,6 +8,7 @@
 	span`{test -f span_$objtype.s && echo -n _$objtype}.$O\
 	span_alpha.$O\
 	cd.$O\
+	cd_plan9.$O\
 	cl_demo.$O\
 	cl_input.$O\
 	cl_main.$O\
--- a/unix/snd_openal.c
+++ b/unix/snd_openal.c
@@ -28,7 +28,6 @@
 };
 
 cvar_t volume = {"volume", "0.7", true};
-cvar_t bgmvolume = {"bgmvolume", "0.5", true};
 
 static struct {
 	ALuint src, buf;
@@ -693,7 +692,7 @@
 }
 
 void
-playcd(int nt, int loop)
+playcd(int nt, bool loop)
 {
 	pid_t pid;
 	FILE *f;
@@ -700,7 +699,7 @@
 	fpos_t off;
 	int len, left, s[2], in[2];
 
-	shutcd();
+	stopcd();
 	if(qalBufferCallbackSOFT == nil)
 		return;
 
@@ -756,6 +755,8 @@
 	close(s[0]);
 	close(in[1]);
 	track.pcm = in[0];
+	cdloop = loop;
+	cdtrk = nt;
 
 	switch((pid = fork())){
 	case 0:
@@ -801,11 +802,6 @@
 	track.playing = true;
 }
 
-static void
-cdcmd(void)
-{
-}
-
 void
 resumecd(void)
 {
@@ -821,11 +817,13 @@
 int
 initcd(void)
 {
+	cdntrk = cdtrk = 0;
+	cdloop = false;
 	return 0;
 }
 
 void
-shutcd(void)
+stopcd(void)
 {
 	if(track.playing){
 		alSourceStop(track.src); ALERR();
@@ -838,6 +836,12 @@
 	track.playing = false;
 	track.pcm = -1;
 	track.decoder = track.reader = -1;
+}
+
+void
+shutcd(void)
+{
+	stopcd();
 }
 
 int