shithub: qk1

Download patch

ref: 26e253158ac93bbc14cc381780f9b37ea7858bd1
parent: fc4eb838ab4af811315b80e91164e00d1ec8d6b9
author: Konstantinn Bonnet <qu7uux@gmail.com>
date: Sat Feb 14 12:35:26 EST 2015

misc fixes

- Sys_Warn for print(2) stuff with %r (otherwise errstr(2) blanked?)
- Sys_Error also printing %r
- in: note handler, killing input on crash
- snd: segfaults on accessing shm (no, sound still is for shit)
- vid: useless draw()

--- a/README
+++ b/README
@@ -1,6 +1,6 @@
-qk1 - port of Quake to 9front
-=============================
-	- based on linux/x11 code from original gpl release
+qk1 - (9) quake
+===============
+	- based on linux/x11 code from original release
 	- setup:
 		. mk install
 		. mkdir id1
@@ -7,31 +7,44 @@
 		. cp /n/quakecd/id1/*.pak id1/
 		. run from same dir...
 	- unfinished and very buggy (both shitty new code and badly ported old code)
-	- sound does not work; assume that you should always launch with -nosound
+	- sound does not work, cdaudio/networking don't exist
 	- assume high resolutions don't work well and are very crash prone (e.g. > 800x600)
 		. exit with: Hunk_Alloc: failed on ... => not enough memory
 		use [-mem megabytes], e.g.:
 			% qk1 -nosound -mem 10
+		. if window size < physical screen size, swipe mouse super fast to exit
+		the window (bug in mproc())
 	- don't use -winsize, -width, -height; just resize the window (bugsbugsbugs)
 	- mouse is grabbed using _windowed_mouse cvar
 		. open console (~)
 		. type _windowed_mouse 1 (can autocomplete w/ tab)
-		. setting now saved in .cfg
-	- assume it can crash with mouse still being captured (in_9.c)
-		. if window size < physical screen size, swipe mouse super to exit the window
-		(bug in mproc)
+		. setting now saved in id1/config.cfg
 	- tested on 386 and amd64 only
 
 
+some cvars useful for workarounds
+---------------------------------
+	-nosound
+	-nostdout
+	-mem %d
+	-noudp
+	(-winsize %d %d)
+	(-width %d)
+	(-height %d)
+
+
 port notes
 ----------
 	- pr_strings: assumed 32bit pointer arithmetic: use PR_GetStr() instead of
 	  dealing with it directly; may be ass
 	- global cppdefotomy, removing code seen as useless for plan9
+	- Sys_Warn added again for print(2) stuff in *_9.c that uses %r, kind of stupid
+	since Con_Printf was often used there, and it outputs to both fd=1 and console,
+	so needs fixing
 
 
-bugs present in linux/x11
--------------------------
+bugs present in linux/x11 (w/ my tweaks in early commits)
+---------------------------------------------------------
 	- compile with PARANOID -> MSG_WriteByte complains about range error and
 	  exits. no idea why.
 	- new game while already ingame -> front falls off
@@ -43,27 +56,32 @@
 		. when paused, if _windowed_mouse 1, can still look around with the mouse, even
 		if world is frozen
 	- fp exceptions galore
+	- high resolutions -> depression, then suicide
+	- resizing to smaller window -> gory death
+	- resizing several times -> genitals chopped off, etc.
+	- seemingly random crashes on startup
 
 
-todo
-----
+todo/issues
+-----------
 	- kill stdio; other than stdio uses, NULL -> nil; same for *printf
 	- vid_9.c: remove/limit stupid
 	- net_9p.c
 	- net_udp.c + net_dgrm.c: actuallyport
 	- snd_9.c: actuallyport
-	- cd_9.c: everything; just specigy via cvar a folder and look for files there?
-	- kill mouse on crash
+	- cd_9.c: everything; just specify via cvar a folder and look for files there?
 	- mproc: better grabout shit
 	- free mouselook without needing +mlook
 	- plan9-like install
-	- fix *Printf functions (console.c, sys_9.c, etc.)
 	- do something about all the fucking shit printed in console
-	- caps lock == Kctl
+	- caps lock == Kctl (← kbdfs)
 	- ungrab mouse when entering console or menus a la games/doom (IN_Grabm())
 	- clean up *_9.c
 	- linking errors with -T cflag
 	- fix potential fp exceptions in code rather than ignoring them
+	- sys_9.c: proper P9 use (bad programming?)
+	- rewrite everything to use plan9 style? (ocd)
+	- proper %r use for *_Printf and Sys_Error
 
 
 legal
--- a/cd_9.c
+++ b/cd_9.c
@@ -294,7 +294,8 @@
 	}
 
 	if((cdfile = open(cd_dev, OREAD)) == -1){
-		Con_Printf("CDAudio_Init %s: %r\n", cd_dev);
+		Sys_Warn("CDAudio_Init %s", cd_dev);
+		Con_Printf("CDAudio_Init %s: failed to initialize\n", cd_dev);
 		cdfile = -1;
 		return -1;
 	}
--- a/in_9.c
+++ b/in_9.c
@@ -213,7 +213,7 @@
 	nerr = 0;
 	for(;;){
 		if((n = read(fd, buf, sizeof buf)) != 1+4*12){
-			Con_Printf("mouse: bad count %d not 49: %r\n", n);
+			Sys_Warn("mproc:read: bad count %d not 49", n);
 			if(n < 0 || ++nerr > 10)
 				break;
 			continue;
@@ -262,7 +262,7 @@
 		return;
 	if(mouseactive = on){
 		if((fd = open("/dev/cursor", ORDWR|OCEXEC)) < 0){
-			Con_Printf("%s IN_Grabm: open: %r", argv0);
+			Sys_Warn("IN_Grabm:open");
 			return;
 		}
 		write(fd, nocurs, sizeof(nocurs));
@@ -286,6 +286,13 @@
 	mouse_avail = 0;
 }
 
+void sucks(void *, char *note)
+{
+	if(!strncmp(note, "sys:", 4))
+		IN_Shutdown();	/* FIXME: safe? */
+	noted(NDFLT);
+}
+
 void IN_Init (void)
 {
 	int pid;
@@ -292,6 +299,7 @@
 
 	Cvar_RegisterVariable(&_windowed_mouse);
 	Cvar_RegisterVariable(&m_filter);
+	notify(sucks);
 	IN_Grabm(1);
 	if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
 		kproc();
--- a/snd_9.c
+++ b/snd_9.c
@@ -4,6 +4,7 @@
 
 int audio_fd;
 int snd_inited;
+int wpos;
 
 int tryrates[] = { 11025, 22051, 44100, 8000 };
 
@@ -14,17 +15,11 @@
 	snd_inited = 0;
 
 	if((audio_fd = open("/dev/audio", OWRITE)) < 0){
-		Con_Printf("%s open /dev/audio: %r\n", argv0);
+		Sys_Warn("SNDDMA_Init:open");
+		Con_Printf("Could not open /dev/audio\n");
 		return 0;
 	}
 
-	// get size of dma buffer
-	/*
-	rc = ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
-	rc = ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps);
-	rc = ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info);
-	*/
-
 	shm = &sn;
 	shm->splitbuffer = 0;
 
@@ -41,34 +36,19 @@
 	shm->channels = 2;
 	if(COM_CheckParm("-sndmono") != 0)
 		shm->channels = 1;
-	else if(COM_CheckParm("-sndstereo") != 0)
-		shm->channels = 2;
+	//else if(COM_CheckParm("-sndstereo") != 0)
+	//	shm->channels = 2;
 
 	//shm->samples = info.fragstotal * info.fragsize / (shm->samplebits/8);
 	shm->samples = 1024;
 	shm->submission_chunk = 1;
 
-	// memory map the dma buffer
-	//shm->buffer = mmap(NULL, info.frags.total * info.fragsize, PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
-
-	if((shm->buffer = malloc(shm->samplebits/8 * shm->samples)) == nil){
-		Con_Printf("%s malloc: %r\n", argv0);
+	/* FIXME: use Hunk_AllocName? */
+	if((shm->buffer = mallocz(shm->samplebits/8 * shm->samples, 1)) == nil){
+		Sys_Warn("SNDDMA_Init:malloc");
 		close(audio_fd);
 		return 0;
 	}
-
-	/*
-	rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &shm->speed);
-	rc = shm->samplebits == 16 ? AFMT_S16_LE : AFMT_U8;
-	rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
-
-	// toggle the trigger & start her up
-	rc = 0;
-	rc  = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &rc);
-	rc = PCM_ENABLE_OUTPUT;
-	rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
-	*/
-
 	shm->samplepos = 0;
 	snd_inited = 1;
 	return 1;
@@ -79,10 +59,11 @@
 	if (!snd_inited)
 		return 0;
 
-	/* FIXME */
+	/* FIXME: utter bullshit */
 	//shm->samplepos = count.bytes/shm->samplebits/8 & shm->samples-1;
 	//shm->samplepos = count.ptr / (shm->samplebits / 8);
-
+	shm->samplepos = wpos / shm->samplebits/8 & shm->samples-1;
+	//wpos = 0;
 	return shm->samplepos;
 }
 
@@ -90,7 +71,7 @@
 {
 	if(snd_inited){
 		close(audio_fd);
-		free(shm->buffer);	/* FIXME: fault read */
+		free(shm->buffer);
 		snd_inited = 0;
 	}
 }
@@ -97,7 +78,12 @@
 
 void SNDDMA_Submit(void)
 {
-	/* FIXME */
-	//write(audio_fd, shm->buffer, sizeof shm->buffer);
-	write(audio_fd, shm->buffer, shm->samplebits/8 * shm->samples);
+	int n;
+
+	/* FIXME: utter bullshit */
+	if((n = write(audio_fd, shm->buffer, shm->samplebits/8 * shm->samples)) < 0){
+		Sys_Warn("SNDDMA_Submit:write");
+		SNDDMA_Shutdown();
+	}
+	n += wpos;
 }
--- a/snd_dma.c
+++ b/snd_dma.c
@@ -204,7 +204,8 @@
 		shm->buffer = Hunk_AllocName(1<<16, "shmbuf");
 	}
 
-	Con_Printf ("Sound sampling rate: %i\n", shm->speed);
+	if(shm != nil)
+		Con_Printf ("Sound sampling rate: %i\n", shm->speed);
 
 	// provides a tick sound until washed clean
 
@@ -231,13 +232,11 @@
 	if (shm)
 		shm->gamealive = 0;
 
-	shm = 0;
+	//shm = 0;	/* why? */
 	sound_started = 0;
 
 	if (!fakedma)
-	{
 		SNDDMA_Shutdown();
-	}
 }
 
 
--- a/sys.h
+++ b/sys.h
@@ -16,6 +16,7 @@
 //
 // system IO
 //
+void Sys_Warn (char *msg, ...);
 void Sys_Error (char *error, ...);
 void Sys_Printf (char *fmt, ...);
 void Sys_Quit (void);
--- a/sys_9.c
+++ b/sys_9.c
@@ -12,22 +12,21 @@
 void Sys_Printf (char *fmt, ...)
 {
 	char buf[1024];
-	va_list arg;
 	uchar *p;
+	va_list arg;
 
-	/* FIXME: does not work, fix your megashit code */
 	if(nostdout)
 		return;
 	va_start(arg, fmt);
 	vseprint(buf, buf+sizeof(buf), fmt, arg);
 	va_end(arg);
-
+	//write(1, buf, out-buf);
 	for(p = (uchar *)buf; *p; p++){
 		*p &= 0x7f;
 		if((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
 			print("[%02x]", *p);
 		else
-			write(1, p, sizeof *p);
+			print("%c", *p);
 	}
 }
 
@@ -34,38 +33,44 @@
 void Sys_Quit (void)
 {
 	Host_Shutdown();
-	//fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
 	exits(nil);
 }
 
-void Sys_Error (char *error, ...)
+void Sys_Warn (char *msg, ...)
 {
 	char buf[1024], *out;
 	va_list arg;
 
-	/* FIXME: does not work, fix your megashit code */
-	//fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
 	out = seprint(buf, buf+sizeof(buf), "%s: ", argv0);
+	va_start(arg, msg);
+	out = vseprint(out, buf+sizeof(buf), msg, arg);
+	va_end(arg);
+	out = seprint(out, buf+sizeof(buf), ": %r\n");
+	write(2, buf, out-buf);
+}
+
+void Sys_Error (char *error, ...)
+{
+	char buf[1024], *out;
+	va_list arg;
+
 	va_start(arg, error);
-	out = vseprint(out, buf+sizeof(buf), error, arg);
+	out = vseprint(buf, buf+sizeof(buf), error, arg);
 	va_end(arg);
+	out = seprint(out, buf+sizeof(buf), "\n%s: %r\n", argv0);
 	write(2, buf, out-buf);
-	/* FIXME: newline */
 	Host_Shutdown();
-	exits("ending");
+	sysfatal("ending");
 } 
 
 int Sys_FileTime (char *path)
 {
-	/* FIXME: man 5 intro, man 2 stat, man 2 fcall */
 	uchar	sb[1024];
 
 	if(stat(path, sb, sizeof sb) < 0){
-		fprint(2, "%s stat %s: %r\n", argv0, path);
+		Sys_Warn("Sys_FileTime:stat");
 		return -1;
 	}
-
-	/* FIXME */
 	return *((int *)(sb+25));
 }
 
@@ -74,7 +79,7 @@
 	int	d;
 
 	if((d = create(path, OREAD, DMDIR|0777)) < 0)
-		fprint(2, "%s create %s: %r\n", argv0, path);
+		Sys_Warn("Sys_mkdir:create %s", path);
 	else
 		close(d);
 }
@@ -89,8 +94,7 @@
 	if(d < 0)
 		return -1;
 	if(fstat(d, bs, sizeof bs) < 0)
-		Sys_Error("%s fstat %s: %r", argv0, path);
-	/* FIXME */
+		Sys_Error("Sys_FileOpenRead %s failed", path);
 	return *((vlong *)(bs+33));
 }
 
@@ -99,7 +103,7 @@
 	int     d;
 
 	if((d = open(path, OREAD|OTRUNC)) < 0)
-		Sys_Error("%s open %s: %r", argv0, path);
+		Sys_Error("Sys_FileOpenWrite %s failed", path);
 	return d;
 }
 
--- a/vid_9.c
+++ b/vid_9.c
@@ -248,7 +248,7 @@
 	}
 
 	if(initdraw(nil, nil, "quake") < 0)
-		Sys_Error("%s initdraw: %r", argv0);
+		Sys_Error("VID_Init:initdraw");
 
 	if(vid.width == -1)
 		vid.width = Dx(screen->r);
@@ -320,7 +320,7 @@
 	if(config_notify){		/* skip this frame if window resize */
 		config_notify = 0;
 		if(getwindow(display, Refnone) < 0)
-			Sys_Error("%s getwindow: %r", argv0);
+			Sys_Error("VID_Update:getwindow");
 		vid.width = Dx(screen->r);
 		vid.height = Dy(screen->r);
 		ResetFrameBuffer();
@@ -333,7 +333,6 @@
 		vid.conheight = vid.height;
 		vid.conrowbytes = vid.rowbytes;
 		vid.recalc_refdef = 1;			// force a surface cache flush
-		draw(screen, screen->r, display->black, nil, ZP);
 		Con_CheckResize();
 		Con_Clear_f();
 		return;