git: 9front

Download patch

ref: 8afbac4ac6fdc20830ffa4caf6bfa83e4dd0d468
parent: 21e7afa6399a82eb6e2d6e8dee6ad6b6812eae78
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Thu Jun 2 02:47:50 EDT 2011

games/doom:
removed libthread dependency, its not needed

fix menu crash bug

introduce ev_char event type and use it for text
entering like savename and chat.

convert Kaltgr to KEY_RALT

fix skipped initialization when opening /dev/audio
fails. also dont try to write to fd -1

--- a/sys/src/games/doom/d_event.h
+++ b/sys/src/games/doom/d_event.h
@@ -37,7 +37,8 @@
     ev_keydown,
     ev_keyup,
     ev_mouse,
-    ev_joystick
+    ev_joystick,
+    ev_char,
 } evtype_t;
 
 // Event structure.
@@ -44,9 +45,9 @@
 typedef struct
 {
     evtype_t	type;
-    int		data1;		// raw keys / mouse/joystick buttons
-    int		data2;		// composed key down, mouse/joystick x move
-    int		data3;		// raw key down, mouse/joystick y move
+    int		data1;		// char / key / mouse/joystick buttons
+    int		data2;		// mouse/joystick x move
+    int		data3;		// mouse/joystick y move
 } event_t;
 
  
--- a/sys/src/games/doom/d_main.c
+++ b/sys/src/games/doom/d_main.c
@@ -657,7 +657,8 @@
 	    handle = fopen (&myargv[i][1],"rb");
 	    if (!handle)
 	    {
-                threadexitsall("No such response file!");
+		fprint(2, "No such response file!\n");
+		exits("open");
 	    }
 	    printf("Found response file %s!\n",&myargv[i][1]);
 	    fseek (handle,0,SEEK_END);
--- a/sys/src/games/doom/doomdef.h
+++ b/sys/src/games/doom/doomdef.h
@@ -26,7 +26,6 @@
 #include <u.h>
 #include <libc.h>
 #include <stdio.h>
-#include <thread.h>
 
 //
 // Global parameters/defines.
--- a/sys/src/games/doom/hu_stuff.c
+++ b/sys/src/games/doom/hu_stuff.c
@@ -507,7 +507,6 @@
     static char		lastmessage[HU_MAXLINELENGTH+1];
     char*		macromessage;
     boolean		eatkey = false;
-    static boolean	shiftdown = false;
     static boolean	altdown = false;
     int			c;
     int			i;
@@ -527,20 +526,21 @@
     for (i=0 ; i<MAXPLAYERS ; i++)
 	numplayers += playeringame[i];
 
-    if (ev->data1 == KEY_RSHIFT)
-    {
-	shiftdown = ev->type == ev_keydown;
-	return false;
-    }
-    else if (ev->data1 == KEY_RALT || ev->data1 == KEY_LALT)
-    {
+    switch(ev->type){
+    case ev_keydown:
+    case ev_keyup:
+        if (ev->data1 == KEY_RALT || ev->data1 == KEY_LALT)
+        {
 	altdown = ev->type == ev_keydown;
 	return false;
+        }
+        /* no break */
+    case ev_char:
+        break;
+    default:
+        return false;
     }
 
-    if (ev->type != ev_keydown)
-	return false;
-
     if (!chat_on)
     {
 	if (ev->data1 == HU_MSGREFRESH)
@@ -588,9 +588,7 @@
     }
     else
     {
-	c = ev->data2;
-	if(c == -1)
-		return false;
+	c = ev->data1;
 
 	// send a macro
 	if (altdown)
@@ -598,7 +596,6 @@
 	    c = c - '0';
 	    if (c < 0 || c > 9)
 		return false;
-	    // fprintf(stderr, "got here\n");
 	    macromessage = chat_macros[c];
 	    
 	    // kill last message with a '\n'
--- a/sys/src/games/doom/i_main.c
+++ b/sys/src/games/doom/i_main.c
@@ -4,10 +4,9 @@
 #include "m_argv.h"
 #include "d_main.h"
 
-void threadmain(int argc, char **argv)
+void main(int argc, char **argv)
 {
 	myargc = argc; 
 	myargv = argv; 
-	D_DoomMain (); 
-	threadexitsall(nil);
+	D_DoomMain ();
 } 
--- a/sys/src/games/doom/i_sound.c
+++ b/sys/src/games/doom/i_sound.c
@@ -132,10 +132,8 @@
 	int i;
 
 	audio_fd = open("/dev/audio", ORDWR);
-	if(audio_fd < 0) {
+	if(audio_fd < 0) 
 		printf("WARN Failed to open /dev/audio, sound disabled\n");
-		return;
-	}
 
 	/* Initialize external data (all sounds) at start, keep static. */
 	for (i=1 ; i<NUMSFX ; i++)
@@ -276,7 +274,8 @@
 
 void I_SubmitSound(void)
 {
-	write(audio_fd, mixbuffer, MIXBUFFERSIZE);
+	if(audio_fd >= 0)
+		write(audio_fd, mixbuffer, MIXBUFFERSIZE);
 }
 
 void I_ShutdownSound(void)
--- a/sys/src/games/doom/i_system.c
+++ b/sys/src/games/doom/i_system.c
@@ -45,7 +45,7 @@
 	I_ShutdownMusic();
 	M_SaveDefaults ();
 	I_ShutdownGraphics();
-	threadexitsall(nil);
+	exits(nil);
 }
 
 byte* I_AllocLow (int length)
@@ -108,7 +108,7 @@
     D_QuitNetGame ();
     I_ShutdownGraphics();
 
-    threadexitsall("I_Error");
+    exits("I_Error");
 }
 
 int I_FileExists (char *filepath)
--- a/sys/src/games/doom/i_video.c
+++ b/sys/src/games/doom/i_video.c
@@ -15,13 +15,18 @@
 static Rectangle grabout;
 static Point center;
 
-static void kbdproc(void*);
-static void mouseproc(void*);
+static void kbdproc(void);
+static void mouseproc(void);
 
 static uchar cmap[3*256];
 
+static int kbdpid = -1;
+static int mousepid = -1;
+
 void I_InitGraphics(void)
 {
+	int pid;
+
 	if(initdraw(nil, nil, "doom") < 0)
 		I_Error("I_InitGraphics failed");
 
@@ -30,14 +35,31 @@
 	center = addpt(screen->r.min, Pt(Dx(screen->r)/2, Dy(screen->r)/2));
 	grabout = insetrect(screen->r, Dx(screen->r)/8);
 
-	proccreate(kbdproc, nil, 8*1024);
-	proccreate(mouseproc, nil, 8*1024);
+	if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
+		kbdproc();
+		exits(nil);
+	}
+	kbdpid = pid;
 
+	if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
+		mouseproc();
+		exits(nil);
+	}
+	mousepid = pid;
+
 	screens[0] = (unsigned char*) malloc(SCREENWIDTH * SCREENHEIGHT);
 }
 
 void I_ShutdownGraphics(void)
 {
+	if(kbdpid != -1){
+		postnote(PNPROC, kbdpid, "shutdown");
+		kbdpid = -1;
+	}
+	if(mousepid != -1){
+		postnote(PNPROC, mousepid, "shutdown");
+		mousepid = -1;
+	}
 }
 
 void I_SetPalette(byte *palette)
@@ -177,6 +199,8 @@
 	case Kctl:
 		return KEY_RCTRL;
 	case Kalt:
+		return KEY_LALT;
+	case Kaltgr:
 		return KEY_RALT;
 
 	case Kbs:
@@ -197,14 +221,16 @@
 	case KF|11:
 	case KF|12:
 		return KEY_F1+(r-(KF|1));
+
+	default:
+		if(r < 0x80)
+			return r;
 	}
-	if(r > 0x7f)
-		return 0;
-	return r;
+	return 0;
 }
 
 static void
-kbdproc(void *)
+kbdproc(void)
 {
 	char buf[128], buf2[128], *s;
 	int kfd, n;
@@ -211,8 +237,6 @@
 	Rune r;
 	event_t e;
 
-	threadsetname("kbdproc");
-
 	if((kfd = open("/dev/kbd", OREAD)) < 0)
 		sysfatal("can't open kbd: %r");
 
@@ -221,7 +245,21 @@
 	while((n = read(kfd, buf, sizeof(buf))) > 0){
 		buf[n-1] = 0;
 
+		e.data1 = -1;
+		e.data2 = -1;
+		e.data3 = -1;
+
 		switch(buf[0]){
+		case 'c':
+			chartorune(&r, buf+1);
+			if(r){
+				e.data1 = r;
+				e.type = ev_char;
+				D_PostEvent(&e);
+			}
+			/* no break */
+		default:
+			continue;
 		case 'k':
 			s = buf+1;
 			while(*s){
@@ -228,8 +266,6 @@
 				s += chartorune(&r, s);
 				if(utfrune(buf2+1, r) == nil){
 					if(e.data1 = runetokey(r)){
-						e.data2 = *s == 0 ? e.data1 : -1;
-						e.data3 = -1;
 						e.type = ev_keydown;
 						D_PostEvent(&e);
 					}
@@ -242,7 +278,6 @@
 				s += chartorune(&r, s);
 				if(utfrune(buf+1, r) == nil){
 					if(e.data1 = runetokey(r)){
-						e.data2 = e.data3 = -1;
 						e.type = ev_keyup;
 						D_PostEvent(&e);
 					}
@@ -249,8 +284,6 @@
 				}
 			}
 			break;
-		default:
-			continue;
 		}
 		strcpy(buf2, buf);
 	}
@@ -258,7 +291,7 @@
 }
 
 static void
-mouseproc(void *)
+mouseproc(void)
 {
 	int fd, n, nerr;
 	Mouse m, om;
@@ -265,8 +298,6 @@
 	char buf[1+5*12];
 	event_t e;
 
-	threadsetname("mouseproc");
-
 	if((fd = open("/dev/mouse", ORDWR)) < 0)
 		sysfatal("can't open mouse: %r");
 
@@ -276,7 +307,6 @@
 	for(;;){
 		n = read(fd, buf, sizeof buf);
 		if(n != 1+4*12){
-			yield();	/* if error is due to exiting, we'll exit here */
 			fprint(2, "mouse: bad count %d not 49: %r\n", n);
 			if(n<0 || ++nerr>10)
 				break;
--- a/sys/src/games/doom/m_menu.c
+++ b/sys/src/games/doom/m_menu.c
@@ -1086,9 +1086,9 @@
   // We pick index 0 which is language sensitive,
   //  or one at random, between 1 and maximum number.
   if (language != english )
-    sprintf(endstring,"%s\n\n"DOSY, endmsg[0] );
+    snprintf(endstring, sizeof(endstring), "%s\n\n"DOSY, endmsg[0] );
   else
-    sprintf(endstring,"%s\n\n"DOSY, endmsg[ (gametic%(NUM_QUITMESSAGES-2))+1 ]);
+    snprintf(endstring, sizeof(endstring), "%s\n\n"DOSY, endmsg[ (gametic%(NUM_QUITMESSAGES-2))+1 ]);
   
   M_StartMessage(endstring,M_QuitResponse,true);
 }
@@ -1419,15 +1419,14 @@
 	    }
 	}
 	else
-	    if (ev->type == ev_keydown)
+	    if (ev->type == ev_keydown || ev->type == ev_char)
 	    {
-		ch = ev->data2;
+		ch = ev->data1;
 	    }
     }
     
     if (ch == -1)
 	return false;
-
     
     // Save Game string input
     if (saveStringEnter)
@@ -1434,20 +1433,16 @@
     {
 	switch(ch)
 	{
-	  case KEY_BACKSPACE:
-	    if (saveCharIndex > 0)
-	    {
-		saveCharIndex--;
-		savegamestrings[saveSlot][saveCharIndex] = 0;
-	    }
-	    break;
-				
 	  case KEY_ESCAPE:
-	    saveStringEnter = 0;
+	    if(ev->type != ev_keydown)
+		return false;
+  	    saveStringEnter = 0;
 	    strcpy(&savegamestrings[saveSlot][0],saveOldString);
 	    break;
 				
 	  case KEY_ENTER:
+	    if(ev->type != ev_keydown)
+		return false;
 	    saveStringEnter = 0;
 	    if (savegamestrings[saveSlot][0])
 		M_DoSave(saveSlot);
@@ -1454,6 +1449,16 @@
 	    break;
 				
 	  default:
+	    if(ev->type != ev_char)
+		return false;
+	    if (ch == '\b'){
+	    	if (saveCharIndex > 0)
+	    	{
+			saveCharIndex--;
+			savegamestrings[saveSlot][saveCharIndex] = 0;
+		}
+		break;
+	    }
 	    ch = toupper(ch);
 	    if (ch != 32)
 		if (ch-HU_FONTSTART < 0 || ch-HU_FONTSTART >= HU_FONTSIZE)
@@ -1470,6 +1475,9 @@
 	}
 	return true;
     }
+
+    if(ev->type != ev_keydown)
+        return false;
     
     // Take care of any messages that need input
     if (messageToPrint)
@@ -1483,7 +1491,7 @@
 	if (messageRoutine)
 	    messageRoutine(ch);
 			
-	menuactive = false;
+	M_ClearMenus();
 	S_StartSound(NULL,sfx_swtchx);
 	return true;
     }
@@ -1493,8 +1501,7 @@
 	G_ScreenShot ();
 	return true;
     }
-		
-    
+
     // F-Keys
     if (!menuactive)
 	switch(ch)
@@ -1706,7 +1713,7 @@
     if (menuactive)
 	return;
   
-    menuactive = 1;
+    menuactive = true;
     currentMenu = &MainDef;         // JDC
     itemOn = currentMenu->lastOn;   // JDC
 
@@ -1721,15 +1728,15 @@
 //
 void M_Drawer (void)
 {
-    static short	x;
-    static short	y;
-    short		i;
-    short		max;
+    static int	x;
+    static int	y;
+    int		i;
+    int		n;
+    int		max;
     char		string[40];
     int			start;
 
     inhelpscreens = false;
-
     
     // Horiz. & Vertically center string and print it.
     if (messageToPrint)
@@ -1738,18 +1745,18 @@
 	y = 100 - M_StringHeight(messageString)/2;
 	while(*(messageString+start))
 	{
-	    for (i = 0;i < strlen(messageString+start);i++)
+	    n = strlen(messageString+start);
+	    for (i = 0;i < n;i++)
 		if (*(messageString+start+i) == '\n')
 		{
-		    memset(string,0,40);
-		    strncpy(string,messageString+start,i);
+		    snprint(string, sizeof(string), "%.*s", i, messageString+start);
 		    start += i+1;
 		    break;
 		}
 				
-	    if (i == strlen(messageString+start))
+	    if (i == n)
 	    {
-		strcpy(string,messageString+start);
+		snprint(string, sizeof(string), "%s", messageString+start);
 		start += i;
 	    }
 				
@@ -1792,7 +1799,7 @@
 //
 void M_ClearMenus (void)
 {
-    menuactive = 0;
+    menuactive = false;
     I_MouseEnable(1);	// enable mouse grabbing
 
     // if (!netgame && usergame && paused)
@@ -1831,7 +1838,7 @@
 void M_Init (void)
 {
     currentMenu = &MainDef;
-    menuactive = 0;
+    menuactive = false;
     itemOn = currentMenu->lastOn;
     whichSkull = 0;
     skullAnimCounter = 10;
--- a/sys/src/games/doom/st_stuff.c
+++ b/sys/src/games/doom/st_stuff.c
@@ -543,7 +543,7 @@
       // if (gameskill != sk_nightmare) {
       
       // 'dqd' cheat for toggleable god mode
-      if (cht_CheckCheat(&cheat_god, ev->data2))
+      if (cht_CheckCheat(&cheat_god, ev->data1))
       {
 	plyr->cheats ^= CF_GODMODE;
 	if (plyr->cheats & CF_GODMODE)
@@ -558,7 +558,7 @@
 	  plyr->message = STSTR_DQDOFF;
       }
       // 'fa' cheat for killer fucking arsenal
-      else if (cht_CheckCheat(&cheat_ammonokey, ev->data2))
+      else if (cht_CheckCheat(&cheat_ammonokey, ev->data1))
       {
 	plyr->armorpoints = 200;
 	plyr->armortype = 2;
@@ -572,7 +572,7 @@
 	plyr->message = STSTR_FAADDED;
       }
       // 'kfa' cheat for key full ammo
-      else if (cht_CheckCheat(&cheat_ammo, ev->data2))
+      else if (cht_CheckCheat(&cheat_ammo, ev->data1))
       {
 	plyr->armorpoints = 200;
 	plyr->armortype = 2;
@@ -589,7 +589,7 @@
 	plyr->message = STSTR_KFAADDED;
       }
       // 'mus' cheat for changing music
-      else if (cht_CheckCheat(&cheat_mus, ev->data2))
+      else if (cht_CheckCheat(&cheat_mus, ev->data1))
       {
 	
 	char	buf[3];
@@ -619,8 +619,8 @@
       }
       // Simplified, accepting both "noclip" and "idspispopd".
       // no clipping mode cheat
-      else if ( cht_CheckCheat(&cheat_noclip, ev->data2) 
-		|| cht_CheckCheat(&cheat_commercial_noclip,ev->data2) )
+      else if ( cht_CheckCheat(&cheat_noclip, ev->data1) 
+		|| cht_CheckCheat(&cheat_commercial_noclip,ev->data1) )
       {	
 	plyr->cheats ^= CF_NOCLIP;
 	
@@ -632,7 +632,7 @@
       // 'behold?' power-up cheats
       for (i=0;i<6;i++)
       {
-	if (cht_CheckCheat(&cheat_powerup[i], ev->data2))
+	if (cht_CheckCheat(&cheat_powerup[i], ev->data1))
 	{
 	  if (!plyr->powers[i])
 	    P_GivePower( plyr, i);
@@ -646,12 +646,12 @@
       }
       
       // 'behold' power-up menu
-      if (cht_CheckCheat(&cheat_powerup[6], ev->data2))
+      if (cht_CheckCheat(&cheat_powerup[6], ev->data1))
       {
 	plyr->message = STSTR_BEHOLD;
       }
       // 'choppers' invulnerability & chainsaw
-      else if (cht_CheckCheat(&cheat_choppers, ev->data2))
+      else if (cht_CheckCheat(&cheat_choppers, ev->data1))
       {
 	plyr->weaponowned[wp_chainsaw] = true;
 	plyr->powers[pw_invulnerability] = true;
@@ -658,7 +658,7 @@
 	plyr->message = STSTR_CHOPPERS;
       }
       // 'mypos' for player position
-      else if (cht_CheckCheat(&cheat_mypos, ev->data2))
+      else if (cht_CheckCheat(&cheat_mypos, ev->data1))
       {
 	static char	buf[ST_MSGWIDTH];
 	sprintf(buf, "ang=0x%x;x,y=(0x%x,0x%x)",
@@ -670,7 +670,7 @@
     }
     
     // 'clev' change-level cheat
-    if (cht_CheckCheat(&cheat_clev, ev->data2))
+    if (cht_CheckCheat(&cheat_clev, ev->data1))
     {
       char		buf[3];
       int		epsd;
--- a/sys/src/games/doom/v_video.c
+++ b/sys/src/games/doom/v_video.c
@@ -228,6 +228,7 @@
       fprintf( stderr, "Patch at %d,%d exceeds LFB\n", x,y );
       // No I_Error abort - what is up with TNT.WAD?
       fprintf( stderr, "V_DrawPatch: bad patch (ignored)\n");
+abort();
       return;
     }
 #endif 
--